Tue, 06 Jan 2015 21:39:09 +0100
Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.
michael@0 | 1 | /* vim: set ts=8 sw=8 noexpandtab: */ |
michael@0 | 2 | // qcms |
michael@0 | 3 | // Copyright (C) 2009 Mozilla Corporation |
michael@0 | 4 | // Copyright (C) 1998-2007 Marti Maria |
michael@0 | 5 | // |
michael@0 | 6 | // Permission is hereby granted, free of charge, to any person obtaining |
michael@0 | 7 | // a copy of this software and associated documentation files (the "Software"), |
michael@0 | 8 | // to deal in the Software without restriction, including without limitation |
michael@0 | 9 | // the rights to use, copy, modify, merge, publish, distribute, sublicense, |
michael@0 | 10 | // and/or sell copies of the Software, and to permit persons to whom the Software |
michael@0 | 11 | // is furnished to do so, subject to the following conditions: |
michael@0 | 12 | // |
michael@0 | 13 | // The above copyright notice and this permission notice shall be included in |
michael@0 | 14 | // all copies or substantial portions of the Software. |
michael@0 | 15 | // |
michael@0 | 16 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
michael@0 | 17 | // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO |
michael@0 | 18 | // THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
michael@0 | 19 | // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
michael@0 | 20 | // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
michael@0 | 21 | // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
michael@0 | 22 | // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
michael@0 | 23 | |
michael@0 | 24 | #include <altivec.h> |
michael@0 | 25 | |
michael@0 | 26 | #include "qcmsint.h" |
michael@0 | 27 | |
michael@0 | 28 | #define FLOATSCALE (float)(PRECACHE_OUTPUT_SIZE) |
michael@0 | 29 | #define CLAMPMAXVAL (((float) (PRECACHE_OUTPUT_SIZE - 1)) / PRECACHE_OUTPUT_SIZE) |
michael@0 | 30 | static const ALIGN float floatScaleX4 = FLOATSCALE; |
michael@0 | 31 | static const ALIGN float clampMaxValueX4 = CLAMPMAXVAL; |
michael@0 | 32 | |
michael@0 | 33 | inline vector float load_aligned_float(float *dataPtr) |
michael@0 | 34 | { |
michael@0 | 35 | vector float data = vec_lde(0, dataPtr); |
michael@0 | 36 | vector unsigned char moveToStart = vec_lvsl(0, dataPtr); |
michael@0 | 37 | return vec_perm(data, data, moveToStart); |
michael@0 | 38 | } |
michael@0 | 39 | |
michael@0 | 40 | void qcms_transform_data_rgb_out_lut_altivec(qcms_transform *transform, |
michael@0 | 41 | unsigned char *src, |
michael@0 | 42 | unsigned char *dest, |
michael@0 | 43 | size_t length) |
michael@0 | 44 | { |
michael@0 | 45 | unsigned int i; |
michael@0 | 46 | float (*mat)[4] = transform->matrix; |
michael@0 | 47 | char input_back[32]; |
michael@0 | 48 | /* Ensure we have a buffer that's 16 byte aligned regardless of the original |
michael@0 | 49 | * stack alignment. We can't use __attribute__((aligned(16))) or __declspec(align(32)) |
michael@0 | 50 | * because they don't work on stack variables. gcc 4.4 does do the right thing |
michael@0 | 51 | * on x86 but that's too new for us right now. For more info: gcc bug #16660 */ |
michael@0 | 52 | float const *input = (float*)(((uintptr_t)&input_back[16]) & ~0xf); |
michael@0 | 53 | /* share input and output locations to save having to keep the |
michael@0 | 54 | * locations in separate registers */ |
michael@0 | 55 | uint32_t const *output = (uint32_t*)input; |
michael@0 | 56 | |
michael@0 | 57 | /* deref *transform now to avoid it in loop */ |
michael@0 | 58 | const float *igtbl_r = transform->input_gamma_table_r; |
michael@0 | 59 | const float *igtbl_g = transform->input_gamma_table_g; |
michael@0 | 60 | const float *igtbl_b = transform->input_gamma_table_b; |
michael@0 | 61 | |
michael@0 | 62 | /* deref *transform now to avoid it in loop */ |
michael@0 | 63 | const uint8_t *otdata_r = &transform->output_table_r->data[0]; |
michael@0 | 64 | const uint8_t *otdata_g = &transform->output_table_g->data[0]; |
michael@0 | 65 | const uint8_t *otdata_b = &transform->output_table_b->data[0]; |
michael@0 | 66 | |
michael@0 | 67 | /* input matrix values never change */ |
michael@0 | 68 | const vector float mat0 = vec_ldl(0, (vector float*)mat[0]); |
michael@0 | 69 | const vector float mat1 = vec_ldl(0, (vector float*)mat[1]); |
michael@0 | 70 | const vector float mat2 = vec_ldl(0, (vector float*)mat[2]); |
michael@0 | 71 | |
michael@0 | 72 | /* these values don't change, either */ |
michael@0 | 73 | const vector float max = vec_splat(vec_lde(0, (float*)&clampMaxValueX4), 0); |
michael@0 | 74 | const vector float min = (vector float)vec_splat_u32(0); |
michael@0 | 75 | const vector float scale = vec_splat(vec_lde(0, (float*)&floatScaleX4), 0); |
michael@0 | 76 | |
michael@0 | 77 | /* working variables */ |
michael@0 | 78 | vector float vec_r, vec_g, vec_b, result; |
michael@0 | 79 | |
michael@0 | 80 | /* CYA */ |
michael@0 | 81 | if (!length) |
michael@0 | 82 | return; |
michael@0 | 83 | |
michael@0 | 84 | /* one pixel is handled outside of the loop */ |
michael@0 | 85 | length--; |
michael@0 | 86 | |
michael@0 | 87 | /* setup for transforming 1st pixel */ |
michael@0 | 88 | vec_r = load_aligned_float((float*)&igtbl_r[src[0]]); |
michael@0 | 89 | vec_g = load_aligned_float((float*)&igtbl_r[src[1]]); |
michael@0 | 90 | vec_b = load_aligned_float((float*)&igtbl_r[src[2]]); |
michael@0 | 91 | src += 3; |
michael@0 | 92 | |
michael@0 | 93 | /* transform all but final pixel */ |
michael@0 | 94 | |
michael@0 | 95 | for (i=0; i<length; i++) |
michael@0 | 96 | { |
michael@0 | 97 | /* position values from gamma tables */ |
michael@0 | 98 | vec_r = vec_splat(vec_r, 0); |
michael@0 | 99 | vec_g = vec_splat(vec_g, 0); |
michael@0 | 100 | vec_b = vec_splat(vec_b, 0); |
michael@0 | 101 | |
michael@0 | 102 | /* gamma * matrix */ |
michael@0 | 103 | vec_r = vec_madd(vec_r, mat0, min); |
michael@0 | 104 | vec_g = vec_madd(vec_g, mat1, min); |
michael@0 | 105 | vec_b = vec_madd(vec_b, mat2, min); |
michael@0 | 106 | |
michael@0 | 107 | /* crunch, crunch, crunch */ |
michael@0 | 108 | vec_r = vec_add(vec_r, vec_add(vec_g, vec_b)); |
michael@0 | 109 | vec_r = vec_max(min, vec_r); |
michael@0 | 110 | vec_r = vec_min(max, vec_r); |
michael@0 | 111 | result = vec_madd(vec_r, scale, min); |
michael@0 | 112 | |
michael@0 | 113 | /* store calc'd output tables indices */ |
michael@0 | 114 | vec_st(vec_ctu(vec_round(result), 0), 0, (vector unsigned int*)output); |
michael@0 | 115 | |
michael@0 | 116 | /* load for next loop while store completes */ |
michael@0 | 117 | vec_r = load_aligned_float((float*)&igtbl_r[src[0]]); |
michael@0 | 118 | vec_g = load_aligned_float((float*)&igtbl_r[src[1]]); |
michael@0 | 119 | vec_b = load_aligned_float((float*)&igtbl_r[src[2]]); |
michael@0 | 120 | src += 3; |
michael@0 | 121 | |
michael@0 | 122 | /* use calc'd indices to output RGB values */ |
michael@0 | 123 | dest[0] = otdata_r[output[0]]; |
michael@0 | 124 | dest[1] = otdata_g[output[1]]; |
michael@0 | 125 | dest[2] = otdata_b[output[2]]; |
michael@0 | 126 | dest += 3; |
michael@0 | 127 | } |
michael@0 | 128 | |
michael@0 | 129 | /* handle final (maybe only) pixel */ |
michael@0 | 130 | |
michael@0 | 131 | vec_r = vec_splat(vec_r, 0); |
michael@0 | 132 | vec_g = vec_splat(vec_g, 0); |
michael@0 | 133 | vec_b = vec_splat(vec_b, 0); |
michael@0 | 134 | |
michael@0 | 135 | vec_r = vec_madd(vec_r, mat0, min); |
michael@0 | 136 | vec_g = vec_madd(vec_g, mat1, min); |
michael@0 | 137 | vec_b = vec_madd(vec_b, mat2, min); |
michael@0 | 138 | |
michael@0 | 139 | vec_r = vec_add(vec_r, vec_add(vec_g, vec_b)); |
michael@0 | 140 | vec_r = vec_max(min, vec_r); |
michael@0 | 141 | vec_r = vec_min(max, vec_r); |
michael@0 | 142 | result = vec_madd(vec_r, scale, min); |
michael@0 | 143 | |
michael@0 | 144 | vec_st(vec_ctu(vec_round(result),0),0,(vector unsigned int*)output); |
michael@0 | 145 | |
michael@0 | 146 | dest[0] = otdata_r[output[0]]; |
michael@0 | 147 | dest[1] = otdata_g[output[1]]; |
michael@0 | 148 | dest[2] = otdata_b[output[2]]; |
michael@0 | 149 | } |
michael@0 | 150 | |
michael@0 | 151 | void qcms_transform_data_rgba_out_lut_altivec(qcms_transform *transform, |
michael@0 | 152 | unsigned char *src, |
michael@0 | 153 | unsigned char *dest, |
michael@0 | 154 | size_t length) |
michael@0 | 155 | { |
michael@0 | 156 | unsigned int i; |
michael@0 | 157 | float (*mat)[4] = transform->matrix; |
michael@0 | 158 | char input_back[32]; |
michael@0 | 159 | /* Ensure we have a buffer that's 16 byte aligned regardless of the original |
michael@0 | 160 | * stack alignment. We can't use __attribute__((aligned(16))) or __declspec(align(32)) |
michael@0 | 161 | * because they don't work on stack variables. gcc 4.4 does do the right thing |
michael@0 | 162 | * on x86 but that's too new for us right now. For more info: gcc bug #16660 */ |
michael@0 | 163 | float const *input = (float*)(((uintptr_t)&input_back[16]) & ~0xf); |
michael@0 | 164 | /* share input and output locations to save having to keep the |
michael@0 | 165 | * locations in separate registers */ |
michael@0 | 166 | uint32_t const *output = (uint32_t*)input; |
michael@0 | 167 | |
michael@0 | 168 | /* deref *transform now to avoid it in loop */ |
michael@0 | 169 | const float *igtbl_r = transform->input_gamma_table_r; |
michael@0 | 170 | const float *igtbl_g = transform->input_gamma_table_g; |
michael@0 | 171 | const float *igtbl_b = transform->input_gamma_table_b; |
michael@0 | 172 | |
michael@0 | 173 | /* deref *transform now to avoid it in loop */ |
michael@0 | 174 | const uint8_t *otdata_r = &transform->output_table_r->data[0]; |
michael@0 | 175 | const uint8_t *otdata_g = &transform->output_table_g->data[0]; |
michael@0 | 176 | const uint8_t *otdata_b = &transform->output_table_b->data[0]; |
michael@0 | 177 | |
michael@0 | 178 | /* input matrix values never change */ |
michael@0 | 179 | const vector float mat0 = vec_ldl(0, (vector float*)mat[0]); |
michael@0 | 180 | const vector float mat1 = vec_ldl(0, (vector float*)mat[1]); |
michael@0 | 181 | const vector float mat2 = vec_ldl(0, (vector float*)mat[2]); |
michael@0 | 182 | |
michael@0 | 183 | /* these values don't change, either */ |
michael@0 | 184 | const vector float max = vec_splat(vec_lde(0, (float*)&clampMaxValueX4), 0); |
michael@0 | 185 | const vector float min = (vector float)vec_splat_u32(0); |
michael@0 | 186 | const vector float scale = vec_splat(vec_lde(0, (float*)&floatScaleX4), 0); |
michael@0 | 187 | |
michael@0 | 188 | /* working variables */ |
michael@0 | 189 | vector float vec_r, vec_g, vec_b, result; |
michael@0 | 190 | unsigned char alpha; |
michael@0 | 191 | |
michael@0 | 192 | /* CYA */ |
michael@0 | 193 | if (!length) |
michael@0 | 194 | return; |
michael@0 | 195 | |
michael@0 | 196 | /* one pixel is handled outside of the loop */ |
michael@0 | 197 | length--; |
michael@0 | 198 | |
michael@0 | 199 | /* setup for transforming 1st pixel */ |
michael@0 | 200 | vec_r = load_aligned_float((float*)&igtbl_r[src[0]]); |
michael@0 | 201 | vec_g = load_aligned_float((float*)&igtbl_r[src[1]]); |
michael@0 | 202 | vec_b = load_aligned_float((float*)&igtbl_r[src[2]]); |
michael@0 | 203 | alpha = src[3]; |
michael@0 | 204 | src += 4; |
michael@0 | 205 | |
michael@0 | 206 | /* transform all but final pixel */ |
michael@0 | 207 | |
michael@0 | 208 | for (i=0; i<length; i++) |
michael@0 | 209 | { |
michael@0 | 210 | /* position values from gamma tables */ |
michael@0 | 211 | vec_r = vec_splat(vec_r, 0); |
michael@0 | 212 | vec_g = vec_splat(vec_g, 0); |
michael@0 | 213 | vec_b = vec_splat(vec_b, 0); |
michael@0 | 214 | |
michael@0 | 215 | /* gamma * matrix */ |
michael@0 | 216 | vec_r = vec_madd(vec_r, mat0, min); |
michael@0 | 217 | vec_g = vec_madd(vec_g, mat1, min); |
michael@0 | 218 | vec_b = vec_madd(vec_b, mat2, min); |
michael@0 | 219 | |
michael@0 | 220 | /* store alpha for this pixel; load alpha for next */ |
michael@0 | 221 | dest[3] = alpha; |
michael@0 | 222 | alpha = src[3]; |
michael@0 | 223 | |
michael@0 | 224 | /* crunch, crunch, crunch */ |
michael@0 | 225 | vec_r = vec_add(vec_r, vec_add(vec_g, vec_b)); |
michael@0 | 226 | vec_r = vec_max(min, vec_r); |
michael@0 | 227 | vec_r = vec_min(max, vec_r); |
michael@0 | 228 | result = vec_madd(vec_r, scale, min); |
michael@0 | 229 | |
michael@0 | 230 | /* store calc'd output tables indices */ |
michael@0 | 231 | vec_st(vec_ctu(vec_round(result), 0), 0, (vector unsigned int*)output); |
michael@0 | 232 | |
michael@0 | 233 | /* load gamma values for next loop while store completes */ |
michael@0 | 234 | vec_r = load_aligned_float((float*)&igtbl_r[src[0]]); |
michael@0 | 235 | vec_g = load_aligned_float((float*)&igtbl_r[src[1]]); |
michael@0 | 236 | vec_b = load_aligned_float((float*)&igtbl_r[src[2]]); |
michael@0 | 237 | src += 4; |
michael@0 | 238 | |
michael@0 | 239 | /* use calc'd indices to output RGB values */ |
michael@0 | 240 | dest[0] = otdata_r[output[0]]; |
michael@0 | 241 | dest[1] = otdata_g[output[1]]; |
michael@0 | 242 | dest[2] = otdata_b[output[2]]; |
michael@0 | 243 | dest += 4; |
michael@0 | 244 | } |
michael@0 | 245 | |
michael@0 | 246 | /* handle final (maybe only) pixel */ |
michael@0 | 247 | |
michael@0 | 248 | vec_r = vec_splat(vec_r, 0); |
michael@0 | 249 | vec_g = vec_splat(vec_g, 0); |
michael@0 | 250 | vec_b = vec_splat(vec_b, 0); |
michael@0 | 251 | |
michael@0 | 252 | vec_r = vec_madd(vec_r, mat0, min); |
michael@0 | 253 | vec_g = vec_madd(vec_g, mat1, min); |
michael@0 | 254 | vec_b = vec_madd(vec_b, mat2, min); |
michael@0 | 255 | |
michael@0 | 256 | dest[3] = alpha; |
michael@0 | 257 | |
michael@0 | 258 | vec_r = vec_add(vec_r, vec_add(vec_g, vec_b)); |
michael@0 | 259 | vec_r = vec_max(min, vec_r); |
michael@0 | 260 | vec_r = vec_min(max, vec_r); |
michael@0 | 261 | result = vec_madd(vec_r, scale, min); |
michael@0 | 262 | |
michael@0 | 263 | vec_st(vec_ctu(vec_round(result), 0), 0, (vector unsigned int*)output); |
michael@0 | 264 | |
michael@0 | 265 | dest[0] = otdata_r[output[0]]; |
michael@0 | 266 | dest[1] = otdata_g[output[1]]; |
michael@0 | 267 | dest[2] = otdata_b[output[2]]; |
michael@0 | 268 | } |
michael@0 | 269 |