[x265] [PATCH] sad[16xN]:Intrinsic

praveen at multicorewareinc.com praveen at multicorewareinc.com
Fri Aug 23 14:01:06 CEST 2013


# HG changeset patch
# User praveentiwari
# Date 1377259254 -19800
# Node ID 7e5c568e480489d74c50673d538ddd323218cf97
# Parent  2892cde2c9c6ae9b2be004d9ae3420c210f4b6e8
sad[16xN]:Intrinsic

diff -r 2892cde2c9c6 -r 7e5c568e4804 source/common/vec/pixel8.inc
--- a/source/common/vec/pixel8.inc	Fri Aug 23 17:19:33 2013 +0530
+++ b/source/common/vec/pixel8.inc	Fri Aug 23 17:30:54 2013 +0530
@@ -876,53 +876,238 @@
     return horizontal_add(sum);
 }
 
-template<int size>
-ALWAYSINLINE void unrollFunc_16(pixel *fenc, intptr_t fencstride, pixel *fref, intptr_t frefstride, Vec8us& sad)
-{
-    unrollFunc_16<1>(fenc, fencstride, fref, frefstride, sad);
-    unrollFunc_16<size - 1>(fenc + fencstride, fencstride, fref + frefstride, frefstride, sad);
-}
-
-template<>
-ALWAYSINLINE void unrollFunc_16<1>(pixel *fenc, intptr_t, pixel *fref, intptr_t, Vec8us& sad)
-{
-    Vec16uc m1, n1;
-
-    m1.load_a(fenc);
-    n1.load(fref);
-    sad.addSumAbsDiff(m1, n1);
-}
-
 template<int ly>
 int sad_16(pixel * fenc, intptr_t fencstride, pixel * fref, intptr_t frefstride)
 {
-    Vec4i sum(0);
-    Vec8us sad(0);
-    int max_iterators = (ly >> 4) << 4;
-    int row = 0;
-
-    if (ly < 16)
+    assert((ly % 4) == 0);
+
+    __m128i sum0 = _mm_setzero_si128();
+    __m128i sum1 = _mm_setzero_si128();
+    __m128i T00, T01, T02, T03;
+    __m128i T10, T11, T12, T13;
+    __m128i T20, T21, T22, T23;
+
+    if (ly == 4)
     {
-        unrollFunc_16<ly>(fenc, fencstride, fref, frefstride, sad);
-        sum += extend_low(sad) + extend_high(sad);
-        return horizontal_add(sum);
+        T00 = _mm_load_si128((__m128i*)(fenc + (0) * fencstride));
+        T01 = _mm_load_si128((__m128i*)(fenc + (1) * fencstride));
+        T02 = _mm_load_si128((__m128i*)(fenc + (2) * fencstride));
+        T03 = _mm_load_si128((__m128i*)(fenc + (3) * fencstride));
+
+        T10 = _mm_load_si128((__m128i*)(fref + (0) * frefstride));
+        T11 = _mm_load_si128((__m128i*)(fref + (1) * frefstride));
+        T12 = _mm_load_si128((__m128i*)(fref + (2) * frefstride));
+        T13 = _mm_load_si128((__m128i*)(fref + (3) * frefstride));
+
+        T20 = _mm_sad_epu8(T00, T10);
+        T21 = _mm_sad_epu8(T01, T11);
+        T22 = _mm_sad_epu8(T02, T12);
+        T23 = _mm_sad_epu8(T03, T13);
+
+        sum0 = _mm_add_epi16(sum0, T20);
+        sum0 = _mm_add_epi16(sum0, T21);
+        sum0 = _mm_add_epi16(sum0, T22);
+        sum0 = _mm_add_epi16(sum0, T23);
     }
-    for (row = 0; row < max_iterators; row += 16)
+    else if (ly == 8)
     {
-        unrollFunc_16<16>(fenc, fencstride, fref, frefstride, sad);
-        sum += extend_low(sad) + extend_high(sad);
-        sad = 0;
-        fenc += fencstride * 16;
-        fref += frefstride * 16;
+        T00 = _mm_load_si128((__m128i*)(fenc + (0) * fencstride));
+        T01 = _mm_load_si128((__m128i*)(fenc + (1) * fencstride));
+        T02 = _mm_load_si128((__m128i*)(fenc + (2) * fencstride));
+        T03 = _mm_load_si128((__m128i*)(fenc + (3) * fencstride));
+
+        T10 = _mm_load_si128((__m128i*)(fref + (0) * frefstride));
+        T11 = _mm_load_si128((__m128i*)(fref + (1) * frefstride));
+        T12 = _mm_load_si128((__m128i*)(fref + (2) * frefstride));
+        T13 = _mm_load_si128((__m128i*)(fref + (3) * frefstride));
+
+        T20 = _mm_sad_epu8(T00, T10);
+        T21 = _mm_sad_epu8(T01, T11);
+        T22 = _mm_sad_epu8(T02, T12);
+        T23 = _mm_sad_epu8(T03, T13);
+
+        sum0 = _mm_add_epi16(sum0, T20);
+        sum0 = _mm_add_epi16(sum0, T21);
+        sum0 = _mm_add_epi16(sum0, T22);
+        sum0 = _mm_add_epi16(sum0, T23);
+
+        T00 = _mm_load_si128((__m128i*)(fenc + (4) * fencstride));
+        T01 = _mm_load_si128((__m128i*)(fenc + (5) * fencstride));
+        T02 = _mm_load_si128((__m128i*)(fenc + (6) * fencstride));
+        T03 = _mm_load_si128((__m128i*)(fenc + (7) * fencstride));
+
+        T10 = _mm_load_si128((__m128i*)(fref + (4) * frefstride));
+        T11 = _mm_load_si128((__m128i*)(fref + (5) * frefstride));
+        T12 = _mm_load_si128((__m128i*)(fref + (6) * frefstride));
+        T13 = _mm_load_si128((__m128i*)(fref + (7) * frefstride));
+
+        T20 = _mm_sad_epu8(T00, T10);
+        T21 = _mm_sad_epu8(T01, T11);
+        T22 = _mm_sad_epu8(T02, T12);
+        T23 = _mm_sad_epu8(T03, T13);
+
+        sum0 = _mm_add_epi16(sum0, T20);
+        sum0 = _mm_add_epi16(sum0, T21);
+        sum0 = _mm_add_epi16(sum0, T22);
+        sum0 = _mm_add_epi16(sum0, T23);
     }
-
-    if (ly & 8)
+    else if (ly == 16)
     {
-        unrollFunc_16<8>(fenc, fencstride, fref, frefstride, sad);
-        sum += extend_low(sad) + extend_high(sad);
-        return horizontal_add(sum);
+        T00 = _mm_load_si128((__m128i*)(fenc + (0) * fencstride));
+        T01 = _mm_load_si128((__m128i*)(fenc + (1) * fencstride));
+        T02 = _mm_load_si128((__m128i*)(fenc + (2) * fencstride));
+        T03 = _mm_load_si128((__m128i*)(fenc + (3) * fencstride));
+
+        T10 = _mm_load_si128((__m128i*)(fref + (0) * frefstride));
+        T11 = _mm_load_si128((__m128i*)(fref + (1) * frefstride));
+        T12 = _mm_load_si128((__m128i*)(fref + (2) * frefstride));
+        T13 = _mm_load_si128((__m128i*)(fref + (3) * frefstride));
+
+        T20 = _mm_sad_epu8(T00, T10);
+        T21 = _mm_sad_epu8(T01, T11);
+        T22 = _mm_sad_epu8(T02, T12);
+        T23 = _mm_sad_epu8(T03, T13);
+
+        sum0 = _mm_add_epi16(sum0, T20);
+        sum0 = _mm_add_epi16(sum0, T21);
+        sum0 = _mm_add_epi16(sum0, T22);
+        sum0 = _mm_add_epi16(sum0, T23);
+
+        T00 = _mm_load_si128((__m128i*)(fenc + (4) * fencstride));
+        T01 = _mm_load_si128((__m128i*)(fenc + (5) * fencstride));
+        T02 = _mm_load_si128((__m128i*)(fenc + (6) * fencstride));
+        T03 = _mm_load_si128((__m128i*)(fenc + (7) * fencstride));
+
+        T10 = _mm_load_si128((__m128i*)(fref + (4) * frefstride));
+        T11 = _mm_load_si128((__m128i*)(fref + (5) * frefstride));
+        T12 = _mm_load_si128((__m128i*)(fref + (6) * frefstride));
+        T13 = _mm_load_si128((__m128i*)(fref + (7) * frefstride));
+
+        T20 = _mm_sad_epu8(T00, T10);
+        T21 = _mm_sad_epu8(T01, T11);
+        T22 = _mm_sad_epu8(T02, T12);
+        T23 = _mm_sad_epu8(T03, T13);
+
+        sum0 = _mm_add_epi16(sum0, T20);
+        sum0 = _mm_add_epi16(sum0, T21);
+        sum0 = _mm_add_epi16(sum0, T22);
+        sum0 = _mm_add_epi16(sum0, T23);
+
+        T00 = _mm_load_si128((__m128i*)(fenc + (8) * fencstride));
+        T01 = _mm_load_si128((__m128i*)(fenc + (9) * fencstride));
+        T02 = _mm_load_si128((__m128i*)(fenc + (10) * fencstride));
+        T03 = _mm_load_si128((__m128i*)(fenc + (11) * fencstride));
+
+        T10 = _mm_load_si128((__m128i*)(fref + (8) * frefstride));
+        T11 = _mm_load_si128((__m128i*)(fref + (9) * frefstride));
+        T12 = _mm_load_si128((__m128i*)(fref + (10) * frefstride));
+        T13 = _mm_load_si128((__m128i*)(fref + (11) * frefstride));
+
+        T20 = _mm_sad_epu8(T00, T10);
+        T21 = _mm_sad_epu8(T01, T11);
+        T22 = _mm_sad_epu8(T02, T12);
+        T23 = _mm_sad_epu8(T03, T13);
+
+        sum0 = _mm_add_epi16(sum0, T20);
+        sum0 = _mm_add_epi16(sum0, T21);
+        sum0 = _mm_add_epi16(sum0, T22);
+        sum0 = _mm_add_epi16(sum0, T23);
+
+        T00 = _mm_load_si128((__m128i*)(fenc + (12) * fencstride));
+        T01 = _mm_load_si128((__m128i*)(fenc + (13) * fencstride));
+        T02 = _mm_load_si128((__m128i*)(fenc + (14) * fencstride));
+        T03 = _mm_load_si128((__m128i*)(fenc + (15) * fencstride));
+
+        T10 = _mm_load_si128((__m128i*)(fref + (12) * frefstride));
+        T11 = _mm_load_si128((__m128i*)(fref + (13) * frefstride));
+        T12 = _mm_load_si128((__m128i*)(fref + (14) * frefstride));
+        T13 = _mm_load_si128((__m128i*)(fref + (15) * frefstride));
+
+        T20 = _mm_sad_epu8(T00, T10);
+        T21 = _mm_sad_epu8(T01, T11);
+        T22 = _mm_sad_epu8(T02, T12);
+        T23 = _mm_sad_epu8(T03, T13);
+
+        sum0 = _mm_add_epi16(sum0, T20);
+        sum0 = _mm_add_epi16(sum0, T21);
+        sum0 = _mm_add_epi16(sum0, T22);
+        sum0 = _mm_add_epi16(sum0, T23);
     }
-    return horizontal_add(sum);
+    else if ((ly % 8) == 0)
+    {
+        for (int i = 0; i < ly; i += 8)
+        {
+            T00 = _mm_load_si128((__m128i*)(fenc + (i + 0) * fencstride));
+            T01 = _mm_load_si128((__m128i*)(fenc + (i + 1) * fencstride));
+            T02 = _mm_load_si128((__m128i*)(fenc + (i + 2) * fencstride));
+            T03 = _mm_load_si128((__m128i*)(fenc + (i + 3) * fencstride));
+
+            T10 = _mm_load_si128((__m128i*)(fref + (i + 0) * frefstride));
+            T11 = _mm_load_si128((__m128i*)(fref + (i + 1) * frefstride));
+            T12 = _mm_load_si128((__m128i*)(fref + (i + 2) * frefstride));
+            T13 = _mm_load_si128((__m128i*)(fref + (i + 3) * frefstride));
+
+            T20 = _mm_sad_epu8(T00, T10);
+            T21 = _mm_sad_epu8(T01, T11);
+            T22 = _mm_sad_epu8(T02, T12);
+            T23 = _mm_sad_epu8(T03, T13);
+
+            sum0 = _mm_add_epi16(sum0, T20);
+            sum0 = _mm_add_epi16(sum0, T21);
+            sum0 = _mm_add_epi16(sum0, T22);
+            sum0 = _mm_add_epi16(sum0, T23);
+
+            T00 = _mm_load_si128((__m128i*)(fenc + (i + 4) * fencstride));
+            T01 = _mm_load_si128((__m128i*)(fenc + (i + 5) * fencstride));
+            T02 = _mm_load_si128((__m128i*)(fenc + (i + 6) * fencstride));
+            T03 = _mm_load_si128((__m128i*)(fenc + (i + 7) * fencstride));
+
+            T10 = _mm_load_si128((__m128i*)(fref + (i + 4) * frefstride));
+            T11 = _mm_load_si128((__m128i*)(fref + (i + 5) * frefstride));
+            T12 = _mm_load_si128((__m128i*)(fref + (i + 6) * frefstride));
+            T13 = _mm_load_si128((__m128i*)(fref + (i + 7) * frefstride));
+
+            T20 = _mm_sad_epu8(T00, T10);
+            T21 = _mm_sad_epu8(T01, T11);
+            T22 = _mm_sad_epu8(T02, T12);
+            T23 = _mm_sad_epu8(T03, T13);
+
+            sum0 = _mm_add_epi16(sum0, T20);
+            sum0 = _mm_add_epi16(sum0, T21);
+            sum0 = _mm_add_epi16(sum0, T22);
+            sum0 = _mm_add_epi16(sum0, T23);
+        }
+    }
+    else
+    {
+        for (int i = 0; i < ly; i += 4)
+        {
+            T00 = _mm_load_si128((__m128i*)(fenc + (i + 0) * fencstride));
+            T01 = _mm_load_si128((__m128i*)(fenc + (i + 1) * fencstride));
+            T02 = _mm_load_si128((__m128i*)(fenc + (i + 2) * fencstride));
+            T03 = _mm_load_si128((__m128i*)(fenc + (i + 3) * fencstride));
+
+            T10 = _mm_load_si128((__m128i*)(fref + (i + 0) * frefstride));
+            T11 = _mm_load_si128((__m128i*)(fref + (i + 1) * frefstride));
+            T12 = _mm_load_si128((__m128i*)(fref + (i + 2) * frefstride));
+            T13 = _mm_load_si128((__m128i*)(fref + (i + 3) * frefstride));
+
+            T20 = _mm_sad_epu8(T00, T10);
+            T21 = _mm_sad_epu8(T01, T11);
+            T22 = _mm_sad_epu8(T02, T12);
+            T23 = _mm_sad_epu8(T03, T13);
+
+            sum0 = _mm_add_epi16(sum0, T20);
+            sum0 = _mm_add_epi16(sum0, T21);
+            sum0 = _mm_add_epi16(sum0, T22);
+            sum0 = _mm_add_epi16(sum0, T23);
+        }
+    }
+
+    sum1 = _mm_shuffle_epi32(sum0, 2);
+    sum0 = _mm_add_epi32(sum0, sum1);
+
+    return _mm_cvtsi128_si32(sum0);
 }
 
 template<int ly>


More information about the x265-devel mailing list