From ed38ca51794af4732357654eeefd1bfa6d768c8c Mon Sep 17 00:00:00 2001
From: Seon-Wook Park <seon.wook@swook.net>
Date: Tue, 30 Jun 2015 16:08:15 +0900
Subject: [PATCH] spatialGradient: Remove 4 loads in inner loop

---
 modules/imgproc/src/spatialgradient.cpp | 20 +++++++++++++++-----
 1 file changed, 15 insertions(+), 5 deletions(-)

diff --git a/modules/imgproc/src/spatialgradient.cpp b/modules/imgproc/src/spatialgradient.cpp
index c00ec1d67e..fee4ab270b 100644
--- a/modules/imgproc/src/spatialgradient.cpp
+++ b/modules/imgproc/src/spatialgradient.cpp
@@ -157,13 +157,18 @@ void spatialGradient( InputArray _src, OutputArray _dx, OutputArray _dy,
         n_dx = dx.ptr<short>(i+1);
         n_dy = dy.ptr<short>(i+1);
 
+        v_uint8x16 v_select_m = v_uint8x16(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+                                           0, 0, 0, 0xFF);
+
         // Process rest of columns 16-column chunks at a time
         for ( j = 1; j < W - 16; j += 16 )
         {
             // Load top row for 3x3 Sobel filter
             v_uint8x16 v_um = v_load(&p_src[j-1]);
-            v_uint8x16 v_un = v_load(&p_src[j]);
             v_uint8x16 v_up = v_load(&p_src[j+1]);
+            // TODO: Replace _mm_slli_si128 with hal method
+            v_uint8x16 v_un = v_select(v_select_m, v_uint8x16(_mm_slli_si128(v_up.val, 1)),
+                                                   v_uint8x16(_mm_srli_si128(v_um.val, 1)));
             v_uint16x8 v_um1, v_um2, v_un1, v_un2, v_up1, v_up2;
             v_expand(v_um, v_um1, v_um2);
             v_expand(v_un, v_un1, v_un2);
@@ -177,8 +182,10 @@ void spatialGradient( InputArray _src, OutputArray _dx, OutputArray _dy,
 
             // Load second row for 3x3 Sobel filter
             v_um = v_load(&c_src[j-1]);
-            v_un = v_load(&c_src[j]);
             v_up = v_load(&c_src[j+1]);
+            // TODO: Replace _mm_slli_si128 with hal method
+            v_un = v_select(v_select_m, v_uint8x16(_mm_slli_si128(v_up.val, 1)),
+                                        v_uint8x16(_mm_srli_si128(v_um.val, 1)));
             v_expand(v_um, v_um1, v_um2);
             v_expand(v_un, v_un1, v_un2);
             v_expand(v_up, v_up1, v_up2);
@@ -191,8 +198,10 @@ void spatialGradient( InputArray _src, OutputArray _dx, OutputArray _dy,
 
             // Load third row for 3x3 Sobel filter
             v_um = v_load(&n_src[j-1]);
-            v_un = v_load(&n_src[j]);
             v_up = v_load(&n_src[j+1]);
+            // TODO: Replace _mm_slli_si128 with hal method
+            v_un = v_select(v_select_m, v_uint8x16(_mm_slli_si128(v_up.val, 1)),
+                                        v_uint8x16(_mm_srli_si128(v_um.val, 1)));
             v_expand(v_um, v_um1, v_um2);
             v_expand(v_un, v_un1, v_un2);
             v_expand(v_up, v_up1, v_up2);
@@ -224,9 +233,10 @@ void spatialGradient( InputArray _src, OutputArray _dx, OutputArray _dy,
 
             // Load fourth row for 3x3 Sobel filter
             v_um = v_load(&m_src[j-1]);
-            v_un = v_load(&m_src[j]);
             v_up = v_load(&m_src[j+1]);
-
+            // TODO: Replace _mm_slli_si128 with hal method
+            v_un = v_select(v_select_m, v_uint8x16(_mm_slli_si128(v_up.val, 1)),
+                                        v_uint8x16(_mm_srli_si128(v_um.val, 1)));
             v_expand(v_um, v_um1, v_um2);
             v_expand(v_un, v_un1, v_un2);
             v_expand(v_up, v_up1, v_up2);