From 1f8aa4f7d3262c49ea4a9f3dbd1ad3bdbe0e5219 Mon Sep 17 00:00:00 2001 From: k-shinotsuka Date: Sun, 18 Sep 2016 14:48:40 +0900 Subject: [PATCH] improve Lab2RGB_b() --- modules/imgproc/src/color.cpp | 62 ++++++++++++++--------------------- 1 file changed, 25 insertions(+), 37 deletions(-) diff --git a/modules/imgproc/src/color.cpp b/modules/imgproc/src/color.cpp index 746b303292..5ee0e3d9d8 100644 --- a/modules/imgproc/src/color.cpp +++ b/modules/imgproc/src/color.cpp @@ -5505,9 +5505,7 @@ struct Lab2RGB_b v_alpha = vdup_n_u8(ColorChannel::max()); v_128 = vdupq_n_f32(128.0f); #elif CV_SSE2 - v_scale_inv = _mm_set1_ps(100.f/255.f); v_scale = _mm_set1_ps(255.f); - v_128 = _mm_set1_ps(128.0f); v_zero = _mm_setzero_si128(); haveSIMD = checkHardwareSupport(CV_CPU_SSE2); #endif @@ -5516,6 +5514,7 @@ struct Lab2RGB_b #if CV_SSE2 // 16s x 8 void process(__m128i v_r, __m128i v_g, __m128i v_b, + __m128 v_coeffs, __m128 v_res, float * buf) const { __m128 v_r0 = _mm_cvtepi32_ps(_mm_unpacklo_epi16(v_r, v_zero)); @@ -5526,15 +5525,20 @@ struct Lab2RGB_b __m128 v_g1 = _mm_cvtepi32_ps(_mm_unpackhi_epi16(v_g, v_zero)); __m128 v_b1 = _mm_cvtepi32_ps(_mm_unpackhi_epi16(v_b, v_zero)); - v_r0 = _mm_mul_ps(v_r0, v_scale_inv); - v_r1 = _mm_mul_ps(v_r1, v_scale_inv); + v_r0 = _mm_sub_ps(_mm_mul_ps(v_r0, v_coeffs), v_res); + v_g1 = _mm_sub_ps(_mm_mul_ps(v_g1, v_coeffs), v_res); - v_g0 = _mm_sub_ps(v_g0, v_128); - v_g1 = _mm_sub_ps(v_g1, v_128); - v_b0 = _mm_sub_ps(v_b0, v_128); - v_b1 = _mm_sub_ps(v_b1, v_128); + v_coeffs = _mm_castsi128_ps(_mm_shuffle_epi32(_mm_castps_si128(v_coeffs), 0x49)); + v_res = _mm_castsi128_ps(_mm_shuffle_epi32(_mm_castps_si128(v_res), 0x49)); - _mm_interleave_ps(v_r0, v_r1, v_g0, v_g1, v_b0, v_b1); + v_r1 = _mm_sub_ps(_mm_mul_ps(v_r1, v_coeffs), v_res); + v_b0 = _mm_sub_ps(_mm_mul_ps(v_b0, v_coeffs), v_res); + + v_coeffs = _mm_castsi128_ps(_mm_shuffle_epi32(_mm_castps_si128(v_coeffs), 0x49)); + v_res = _mm_castsi128_ps(_mm_shuffle_epi32(_mm_castps_si128(v_res), 0x49)); + + v_g0 = _mm_sub_ps(_mm_mul_ps(v_g0, v_coeffs), v_res); + v_b1 = _mm_sub_ps(_mm_mul_ps(v_b1, v_coeffs), v_res); _mm_store_ps(buf, v_r0); _mm_store_ps(buf + 4, v_r1); @@ -5550,6 +5554,10 @@ struct Lab2RGB_b int i, j, dcn = dstcn; uchar alpha = ColorChannel::max(); float CV_DECL_ALIGNED(16) buf[3*BLOCK_SIZE]; + #if CV_SSE2 + __m128 v_coeffs = _mm_set_ps(100.f/255.f, 1.f, 1.f, 100.f/255.f); + __m128 v_res = _mm_set_ps(0.f, 128.f, 128.f, 0.f); + #endif for( i = 0; i < n; i += BLOCK_SIZE, src += BLOCK_SIZE*3 ) { @@ -5578,36 +5586,16 @@ struct Lab2RGB_b #elif CV_SSE2 if (haveSIMD) { - for ( ; j <= (dn - 32) * 3; j += 96) + for ( ; j <= (dn - 8) * 3; j += 24) { - __m128i v_r0 = _mm_loadu_si128((__m128i const *)(src + j)); - __m128i v_r1 = _mm_loadu_si128((__m128i const *)(src + j + 16)); - __m128i v_g0 = _mm_loadu_si128((__m128i const *)(src + j + 32)); - __m128i v_g1 = _mm_loadu_si128((__m128i const *)(src + j + 48)); - __m128i v_b0 = _mm_loadu_si128((__m128i const *)(src + j + 64)); - __m128i v_b1 = _mm_loadu_si128((__m128i const *)(src + j + 80)); + __m128i v_src0 = _mm_loadu_si128((__m128i const *)(src + j)); + __m128i v_src1 = _mm_loadl_epi64((__m128i const *)(src + j + 16)); - _mm_deinterleave_epi8(v_r0, v_r1, v_g0, v_g1, v_b0, v_b1); - - process(_mm_unpacklo_epi8(v_r0, v_zero), - _mm_unpacklo_epi8(v_g0, v_zero), - _mm_unpacklo_epi8(v_b0, v_zero), + process(_mm_unpacklo_epi8(v_src0, v_zero), + _mm_unpackhi_epi8(v_src0, v_zero), + _mm_unpacklo_epi8(v_src1, v_zero), + v_coeffs, v_res, buf + j); - - process(_mm_unpackhi_epi8(v_r0, v_zero), - _mm_unpackhi_epi8(v_g0, v_zero), - _mm_unpackhi_epi8(v_b0, v_zero), - buf + j + 24); - - process(_mm_unpacklo_epi8(v_r1, v_zero), - _mm_unpacklo_epi8(v_g1, v_zero), - _mm_unpacklo_epi8(v_b1, v_zero), - buf + j + 48); - - process(_mm_unpackhi_epi8(v_r1, v_zero), - _mm_unpackhi_epi8(v_g1, v_zero), - _mm_unpackhi_epi8(v_b1, v_zero), - buf + j + 72); } } #endif @@ -5692,7 +5680,7 @@ struct Lab2RGB_b float32x4_t v_scale, v_scale_inv, v_128; uint8x8_t v_alpha; #elif CV_SSE2 - __m128 v_scale, v_scale_inv, v_128; + __m128 v_scale; __m128i v_zero; bool haveSIMD; #endif