1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/qcms/transform-sse1.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,253 @@ 1.4 +#include <xmmintrin.h> 1.5 + 1.6 +#include "qcmsint.h" 1.7 + 1.8 +/* pre-shuffled: just load these into XMM reg instead of load-scalar/shufps sequence */ 1.9 +#define FLOATSCALE (float)(PRECACHE_OUTPUT_SIZE) 1.10 +#define CLAMPMAXVAL ( ((float) (PRECACHE_OUTPUT_SIZE - 1)) / PRECACHE_OUTPUT_SIZE ) 1.11 +static const ALIGN float floatScaleX4[4] = 1.12 + { FLOATSCALE, FLOATSCALE, FLOATSCALE, FLOATSCALE}; 1.13 +static const ALIGN float clampMaxValueX4[4] = 1.14 + { CLAMPMAXVAL, CLAMPMAXVAL, CLAMPMAXVAL, CLAMPMAXVAL}; 1.15 + 1.16 +void qcms_transform_data_rgb_out_lut_sse1(qcms_transform *transform, 1.17 + unsigned char *src, 1.18 + unsigned char *dest, 1.19 + size_t length) 1.20 +{ 1.21 + unsigned int i; 1.22 + float (*mat)[4] = transform->matrix; 1.23 + char input_back[32]; 1.24 + /* Ensure we have a buffer that's 16 byte aligned regardless of the original 1.25 + * stack alignment. We can't use __attribute__((aligned(16))) or __declspec(align(32)) 1.26 + * because they don't work on stack variables. gcc 4.4 does do the right thing 1.27 + * on x86 but that's too new for us right now. For more info: gcc bug #16660 */ 1.28 + float const * input = (float*)(((uintptr_t)&input_back[16]) & ~0xf); 1.29 + /* share input and output locations to save having to keep the 1.30 + * locations in separate registers */ 1.31 + uint32_t const * output = (uint32_t*)input; 1.32 + 1.33 + /* deref *transform now to avoid it in loop */ 1.34 + const float *igtbl_r = transform->input_gamma_table_r; 1.35 + const float *igtbl_g = transform->input_gamma_table_g; 1.36 + const float *igtbl_b = transform->input_gamma_table_b; 1.37 + 1.38 + /* deref *transform now to avoid it in loop */ 1.39 + const uint8_t *otdata_r = &transform->output_table_r->data[0]; 1.40 + const uint8_t *otdata_g = &transform->output_table_g->data[0]; 1.41 + const uint8_t *otdata_b = &transform->output_table_b->data[0]; 1.42 + 1.43 + /* input matrix values never change */ 1.44 + const __m128 mat0 = _mm_load_ps(mat[0]); 1.45 + const __m128 mat1 = _mm_load_ps(mat[1]); 1.46 + const __m128 mat2 = _mm_load_ps(mat[2]); 1.47 + 1.48 + /* these values don't change, either */ 1.49 + const __m128 max = _mm_load_ps(clampMaxValueX4); 1.50 + const __m128 min = _mm_setzero_ps(); 1.51 + const __m128 scale = _mm_load_ps(floatScaleX4); 1.52 + 1.53 + /* working variables */ 1.54 + __m128 vec_r, vec_g, vec_b, result; 1.55 + 1.56 + /* CYA */ 1.57 + if (!length) 1.58 + return; 1.59 + 1.60 + /* one pixel is handled outside of the loop */ 1.61 + length--; 1.62 + 1.63 + /* setup for transforming 1st pixel */ 1.64 + vec_r = _mm_load_ss(&igtbl_r[src[0]]); 1.65 + vec_g = _mm_load_ss(&igtbl_g[src[1]]); 1.66 + vec_b = _mm_load_ss(&igtbl_b[src[2]]); 1.67 + src += 3; 1.68 + 1.69 + /* transform all but final pixel */ 1.70 + 1.71 + for (i=0; i<length; i++) 1.72 + { 1.73 + /* position values from gamma tables */ 1.74 + vec_r = _mm_shuffle_ps(vec_r, vec_r, 0); 1.75 + vec_g = _mm_shuffle_ps(vec_g, vec_g, 0); 1.76 + vec_b = _mm_shuffle_ps(vec_b, vec_b, 0); 1.77 + 1.78 + /* gamma * matrix */ 1.79 + vec_r = _mm_mul_ps(vec_r, mat0); 1.80 + vec_g = _mm_mul_ps(vec_g, mat1); 1.81 + vec_b = _mm_mul_ps(vec_b, mat2); 1.82 + 1.83 + /* crunch, crunch, crunch */ 1.84 + vec_r = _mm_add_ps(vec_r, _mm_add_ps(vec_g, vec_b)); 1.85 + vec_r = _mm_max_ps(min, vec_r); 1.86 + vec_r = _mm_min_ps(max, vec_r); 1.87 + result = _mm_mul_ps(vec_r, scale); 1.88 + 1.89 + /* store calc'd output tables indices */ 1.90 + *((__m64 *)&output[0]) = _mm_cvtps_pi32(result); 1.91 + result = _mm_movehl_ps(result, result); 1.92 + *((__m64 *)&output[2]) = _mm_cvtps_pi32(result) ; 1.93 + 1.94 + /* load for next loop while store completes */ 1.95 + vec_r = _mm_load_ss(&igtbl_r[src[0]]); 1.96 + vec_g = _mm_load_ss(&igtbl_g[src[1]]); 1.97 + vec_b = _mm_load_ss(&igtbl_b[src[2]]); 1.98 + src += 3; 1.99 + 1.100 + /* use calc'd indices to output RGB values */ 1.101 + dest[OUTPUT_R_INDEX] = otdata_r[output[0]]; 1.102 + dest[OUTPUT_G_INDEX] = otdata_g[output[1]]; 1.103 + dest[OUTPUT_B_INDEX] = otdata_b[output[2]]; 1.104 + dest += RGB_OUTPUT_COMPONENTS; 1.105 + } 1.106 + 1.107 + /* handle final (maybe only) pixel */ 1.108 + 1.109 + vec_r = _mm_shuffle_ps(vec_r, vec_r, 0); 1.110 + vec_g = _mm_shuffle_ps(vec_g, vec_g, 0); 1.111 + vec_b = _mm_shuffle_ps(vec_b, vec_b, 0); 1.112 + 1.113 + vec_r = _mm_mul_ps(vec_r, mat0); 1.114 + vec_g = _mm_mul_ps(vec_g, mat1); 1.115 + vec_b = _mm_mul_ps(vec_b, mat2); 1.116 + 1.117 + vec_r = _mm_add_ps(vec_r, _mm_add_ps(vec_g, vec_b)); 1.118 + vec_r = _mm_max_ps(min, vec_r); 1.119 + vec_r = _mm_min_ps(max, vec_r); 1.120 + result = _mm_mul_ps(vec_r, scale); 1.121 + 1.122 + *((__m64 *)&output[0]) = _mm_cvtps_pi32(result); 1.123 + result = _mm_movehl_ps(result, result); 1.124 + *((__m64 *)&output[2]) = _mm_cvtps_pi32(result); 1.125 + 1.126 + dest[OUTPUT_R_INDEX] = otdata_r[output[0]]; 1.127 + dest[OUTPUT_G_INDEX] = otdata_g[output[1]]; 1.128 + dest[OUTPUT_B_INDEX] = otdata_b[output[2]]; 1.129 + 1.130 + _mm_empty(); 1.131 +} 1.132 + 1.133 +void qcms_transform_data_rgba_out_lut_sse1(qcms_transform *transform, 1.134 + unsigned char *src, 1.135 + unsigned char *dest, 1.136 + size_t length) 1.137 +{ 1.138 + unsigned int i; 1.139 + float (*mat)[4] = transform->matrix; 1.140 + char input_back[32]; 1.141 + /* Ensure we have a buffer that's 16 byte aligned regardless of the original 1.142 + * stack alignment. We can't use __attribute__((aligned(16))) or __declspec(align(32)) 1.143 + * because they don't work on stack variables. gcc 4.4 does do the right thing 1.144 + * on x86 but that's too new for us right now. For more info: gcc bug #16660 */ 1.145 + float const * input = (float*)(((uintptr_t)&input_back[16]) & ~0xf); 1.146 + /* share input and output locations to save having to keep the 1.147 + * locations in separate registers */ 1.148 + uint32_t const * output = (uint32_t*)input; 1.149 + 1.150 + /* deref *transform now to avoid it in loop */ 1.151 + const float *igtbl_r = transform->input_gamma_table_r; 1.152 + const float *igtbl_g = transform->input_gamma_table_g; 1.153 + const float *igtbl_b = transform->input_gamma_table_b; 1.154 + 1.155 + /* deref *transform now to avoid it in loop */ 1.156 + const uint8_t *otdata_r = &transform->output_table_r->data[0]; 1.157 + const uint8_t *otdata_g = &transform->output_table_g->data[0]; 1.158 + const uint8_t *otdata_b = &transform->output_table_b->data[0]; 1.159 + 1.160 + /* input matrix values never change */ 1.161 + const __m128 mat0 = _mm_load_ps(mat[0]); 1.162 + const __m128 mat1 = _mm_load_ps(mat[1]); 1.163 + const __m128 mat2 = _mm_load_ps(mat[2]); 1.164 + 1.165 + /* these values don't change, either */ 1.166 + const __m128 max = _mm_load_ps(clampMaxValueX4); 1.167 + const __m128 min = _mm_setzero_ps(); 1.168 + const __m128 scale = _mm_load_ps(floatScaleX4); 1.169 + 1.170 + /* working variables */ 1.171 + __m128 vec_r, vec_g, vec_b, result; 1.172 + unsigned char alpha; 1.173 + 1.174 + /* CYA */ 1.175 + if (!length) 1.176 + return; 1.177 + 1.178 + /* one pixel is handled outside of the loop */ 1.179 + length--; 1.180 + 1.181 + /* setup for transforming 1st pixel */ 1.182 + vec_r = _mm_load_ss(&igtbl_r[src[0]]); 1.183 + vec_g = _mm_load_ss(&igtbl_g[src[1]]); 1.184 + vec_b = _mm_load_ss(&igtbl_b[src[2]]); 1.185 + alpha = src[3]; 1.186 + src += 4; 1.187 + 1.188 + /* transform all but final pixel */ 1.189 + 1.190 + for (i=0; i<length; i++) 1.191 + { 1.192 + /* position values from gamma tables */ 1.193 + vec_r = _mm_shuffle_ps(vec_r, vec_r, 0); 1.194 + vec_g = _mm_shuffle_ps(vec_g, vec_g, 0); 1.195 + vec_b = _mm_shuffle_ps(vec_b, vec_b, 0); 1.196 + 1.197 + /* gamma * matrix */ 1.198 + vec_r = _mm_mul_ps(vec_r, mat0); 1.199 + vec_g = _mm_mul_ps(vec_g, mat1); 1.200 + vec_b = _mm_mul_ps(vec_b, mat2); 1.201 + 1.202 + /* store alpha for this pixel; load alpha for next */ 1.203 + dest[OUTPUT_A_INDEX] = alpha; 1.204 + alpha = src[3]; 1.205 + 1.206 + /* crunch, crunch, crunch */ 1.207 + vec_r = _mm_add_ps(vec_r, _mm_add_ps(vec_g, vec_b)); 1.208 + vec_r = _mm_max_ps(min, vec_r); 1.209 + vec_r = _mm_min_ps(max, vec_r); 1.210 + result = _mm_mul_ps(vec_r, scale); 1.211 + 1.212 + /* store calc'd output tables indices */ 1.213 + *((__m64 *)&output[0]) = _mm_cvtps_pi32(result); 1.214 + result = _mm_movehl_ps(result, result); 1.215 + *((__m64 *)&output[2]) = _mm_cvtps_pi32(result); 1.216 + 1.217 + /* load gamma values for next loop while store completes */ 1.218 + vec_r = _mm_load_ss(&igtbl_r[src[0]]); 1.219 + vec_g = _mm_load_ss(&igtbl_g[src[1]]); 1.220 + vec_b = _mm_load_ss(&igtbl_b[src[2]]); 1.221 + src += 4; 1.222 + 1.223 + /* use calc'd indices to output RGB values */ 1.224 + dest[OUTPUT_R_INDEX] = otdata_r[output[0]]; 1.225 + dest[OUTPUT_G_INDEX] = otdata_g[output[1]]; 1.226 + dest[OUTPUT_B_INDEX] = otdata_b[output[2]]; 1.227 + dest += 4; 1.228 + } 1.229 + 1.230 + /* handle final (maybe only) pixel */ 1.231 + 1.232 + vec_r = _mm_shuffle_ps(vec_r, vec_r, 0); 1.233 + vec_g = _mm_shuffle_ps(vec_g, vec_g, 0); 1.234 + vec_b = _mm_shuffle_ps(vec_b, vec_b, 0); 1.235 + 1.236 + vec_r = _mm_mul_ps(vec_r, mat0); 1.237 + vec_g = _mm_mul_ps(vec_g, mat1); 1.238 + vec_b = _mm_mul_ps(vec_b, mat2); 1.239 + 1.240 + dest[OUTPUT_A_INDEX] = alpha; 1.241 + 1.242 + vec_r = _mm_add_ps(vec_r, _mm_add_ps(vec_g, vec_b)); 1.243 + vec_r = _mm_max_ps(min, vec_r); 1.244 + vec_r = _mm_min_ps(max, vec_r); 1.245 + result = _mm_mul_ps(vec_r, scale); 1.246 + 1.247 + *((__m64 *)&output[0]) = _mm_cvtps_pi32(result); 1.248 + result = _mm_movehl_ps(result, result); 1.249 + *((__m64 *)&output[2]) = _mm_cvtps_pi32(result); 1.250 + 1.251 + dest[OUTPUT_R_INDEX] = otdata_r[output[0]]; 1.252 + dest[OUTPUT_G_INDEX] = otdata_g[output[1]]; 1.253 + dest[OUTPUT_B_INDEX] = otdata_b[output[2]]; 1.254 + 1.255 + _mm_empty(); 1.256 +}