transform-altivec.c 9.2 KB

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