Commit 8fdb9614d8 for aom

commit 8fdb9614d8a5d9372781d69642ad414d45e7ea05
Author: Frank Galligan <fgalligan@google.com>
Date:   Thu Nov 26 20:30:50 2026 -0800

    x86: dual-lane AVX2 4x4 fwd txfm

    Replace the 128-bit SSE2 TX_4X4 dispatch with dual-lane 256-bit AVX2
    lowbd_fwd_txfm2d_4x4_avx2 (and outlined non-DCT helper) across all 4x4
    transform types.

    Expand AV1FwdTxfm2dMatchTest to cover signed [-255, +255] residuals,
    minimal stride = cols, and prefix output buffer poisoning.

    Speedup vs origin/main (AMD EPYC 7B13, --cpu-used=6, bit-exact):
      -4.14% 1080p allintra (1T), -1.03% 720p allintra (8T),
      -1.16% (1T) / -1.50% (8T) 720p good;
      test_libaom 4x4 DCT_DCT 619->317 us (21x->42x),
      MixedTxTypeSpeed 20.0->9.3 ns/call (10.46x->21.86x).
    Binary size (x86_64 -O3): libaom.a .text +0.9 KiB (+0.01%),
      .rodata +0.3 KiB (+0.03%); stripped aomenc +0.0 KiB (+0.00%).
    Memory: stateless leaf kernel (0 heap/static allocations; peak RSS
      delta within run variance).

    TAG=agy
    CONV=b929a630-a6cd-45a0-979c-1767c30178ee
    Change-Id: Ifc22ae4a6d585e07931c6d8a73274f76ce8ded3d

diff --git a/av1/encoder/x86/av1_fwd_txfm2d_avx2.c b/av1/encoder/x86/av1_fwd_txfm2d_avx2.c
index 3ad02f300f..50e222d851 100644
--- a/av1/encoder/x86/av1_fwd_txfm2d_avx2.c
+++ b/av1/encoder/x86/av1_fwd_txfm2d_avx2.c
@@ -10,9 +10,12 @@
  */

 #include <assert.h>
+#include <stddef.h>
+#include <stdint.h>

 #include "config/av1_rtcd.h"

+#include "aom_dsp/aom_dsp_common.h"
 #include "av1/common/enums.h"
 #include "av1/common/av1_txfm.h"
 #include "av1/encoder/x86/av1_fwd_txfm_avx2.h"
@@ -3944,8 +3947,217 @@ static void lowbd_fwd_txfm2d_16x8_avx2(const int16_t *input, int32_t *output,
   store_rect_row_pair_16bit_to_32bit_avx2(q_odd[3], output, 11, 3);
 }

+// 4-point 1-D DCT on unpacked pairs u0=(x0,x1), u1=(x3,x2); returns 32-bit
+// u01=[out0|out1] and u23=[out2|out3]. For bd == 8, col input |x| <= 255 << 2
+// = 1020 and row input |u| <= 4 * 1020 * cospi[32] >> 13 = 2885, so
+// non-saturating add/sub (<= 5770) cannot overflow int16_t.
+static inline void fdct4_256_avx2(__m128i u0, __m128i u1, __m256i *u01,
+                                  __m256i *u23) {
+  const int32_t *cospi = cospi_arr(13);
+  const __m256i r256 = _mm256_set1_epi32(1 << 12);
+  // stage 1
+  const __m128i v0 = _mm_add_epi16(u0, u1);
+  const __m128i v1 = _mm_sub_epi16(u0, u1);
+  const __m256i v01 =
+      _mm256_inserti128_si256(_mm256_castsi128_si256(v0), v1, 1);
+  // stage 2 and stage 3
+  const __m256i w01 =
+      pair_set_w16x2_epi16(cospi[32], cospi[32], cospi[16], cospi[48]);
+  const __m256i w23 =
+      pair_set_w16x2_epi16(cospi[32], -cospi[32], cospi[48], -cospi[16]);
+  *u01 = _mm256_srai_epi32(_mm256_add_epi32(_mm256_madd_epi16(v01, w01), r256),
+                           13);
+  *u23 = _mm256_srai_epi32(_mm256_add_epi32(_mm256_madd_epi16(v01, w23), r256),
+                           13);
+}
+
+// 4-point 1-D ADST on 4x int16 rows r0..r3 (low 64 bits); returns 32-bit
+// u01=[out0|out1] and u23=[out2|out3]. r3 is passed by pointer because Win32
+// __cdecl caps by-value __m128i parameters at 3 (MSVC C2719).
+static inline void fadst4_256_avx2(__m128i r0, __m128i r1, __m128i r2,
+                                   const __m128i *r3, __m256i *u01,
+                                   __m256i *u23) {
+  const int32_t *sinpi = sinpi_arr(13);
+  const __m256i r256 = _mm256_set1_epi32(1 << 12);
+  // Non-saturating add is safe for bd == 8: |r0|, |r1| <= 2885 -> <= 5770.
+  const __m128i in7 = _mm_add_epi16(r0, r1);
+  const __m128i u0 = _mm_unpacklo_epi16(r0, r1);
+  const __m128i u1 = _mm_unpacklo_epi16(r2, *r3);
+  const __m128i u2 = _mm_unpacklo_epi16(in7, *r3);
+
+  const __m256i u00 =
+      _mm256_permute4x64_epi64(_mm256_castsi128_si256(u0), 0x44);
+  const __m256i u11 =
+      _mm256_permute4x64_epi64(_mm256_castsi128_si256(u1), 0x44);
+  const __m256i u21 =
+      _mm256_inserti128_si256(_mm256_castsi128_si256(u2), u1, 1);
+
+  // stages 1..4: v03=[s0+s2 | s1-s3], v14=[s4+s5 | -s4+s6],
+  // s1_s4x3=[sinpi[3]*(r0+r1-r3) | 3*sinpi[3]*r2] (3*6689=20067 fits int16).
+  const __m256i v03 = _mm256_madd_epi16(
+      u00, pair_set_w16x2_epi16(sinpi[1], sinpi[2], sinpi[4], -sinpi[1]));
+  const __m256i v14 = _mm256_madd_epi16(
+      u11, pair_set_w16x2_epi16(sinpi[3], sinpi[4], -sinpi[3], sinpi[2]));
+  const __m256i s1_s4x3 = _mm256_madd_epi16(
+      u21, pair_set_w16x2_epi16(sinpi[3], -sinpi[3], 3 * sinpi[3], 0));
+
+  // stages 5..6
+  const __m256i s0_s2 = _mm256_add_epi32(v03, v14);
+  const __m128i s0 = _mm256_castsi256_si128(s0_s2);
+  const __m128i s2 = _mm256_extracti128_si256(s0_s2, 1);
+  const __m128i s4x3 = _mm256_extracti128_si256(s1_s4x3, 1);
+  const __m128i s6 = _mm_add_epi32(_mm_sub_epi32(s2, s0), s4x3);
+  const __m256i s1_s6 = _mm256_inserti128_si256(s1_s4x3, s6, 1);
+
+  const __m256i m01 = _mm256_permute2x128_si256(s0_s2, s1_s6, 0x20);
+  const __m256i m23 = _mm256_permute2x128_si256(s0_s2, s1_s6, 0x31);
+  *u01 = _mm256_srai_epi32(_mm256_add_epi32(m01, r256), 13);
+  *u23 = _mm256_srai_epi32(_mm256_add_epi32(m23, r256), 13);
+}
+
+// Outlined so non-DCT branches, ADST constants, and stack/register pressure
+// stay out of the hot DCT_DCT fast path.
+static AOM_NOINLINE void lowbd_fwd_txfm2d_4x4_non_dct_avx2(const int16_t *input,
+                                                           int32_t *output,
+                                                           int stride,
+                                                           TX_TYPE tx_type) {
+  assert(tx_type < TX_TYPES);
+  const int vtx = vtx_tab[tx_type];
+  const int htx = htx_tab[tx_type];
+  const ptrdiff_t s_in = stride;
+  const int16_t *in_ptr = (vtx == FLIPADST_1D) ? (input + 3 * s_in) : input;
+  const ptrdiff_t s = (vtx == FLIPADST_1D) ? -s_in : s_in;
+  const __m128i r0 =
+      _mm_slli_epi16(_mm_loadl_epi64((const __m128i *)(in_ptr + 0 * s)), 2);
+  const __m128i r1 =
+      _mm_slli_epi16(_mm_loadl_epi64((const __m128i *)(in_ptr + 1 * s)), 2);
+  const __m128i r2 =
+      _mm_slli_epi16(_mm_loadl_epi64((const __m128i *)(in_ptr + 2 * s)), 2);
+  const __m128i r3 =
+      _mm_slli_epi16(_mm_loadl_epi64((const __m128i *)(in_ptr + 3 * s)), 2);
+
+  // p02 = [c0, c2], p13 = [c1, c3] (each c_i = 4x int16_t).
+  __m128i p02, p13;
+  switch (vtx) {
+    case DCT_1D: {
+      __m256i u01, u23;
+      fdct4_256_avx2(_mm_unpacklo_epi16(r0, r1), _mm_unpacklo_epi16(r3, r2),
+                     &u01, &u23);
+      const __m256i p = _mm256_packs_epi32(u01, u23);
+      p02 = _mm256_castsi256_si128(p);
+      p13 = _mm256_extracti128_si256(p, 1);
+      break;
+    }
+    case ADST_1D:
+    case FLIPADST_1D: {
+      __m256i u01, u23;
+      fadst4_256_avx2(r0, r1, r2, &r3, &u01, &u23);
+      const __m256i p = _mm256_packs_epi32(u01, u23);
+      p02 = _mm256_castsi256_si128(p);
+      p13 = _mm256_extracti128_si256(p, 1);
+      break;
+    }
+    default: {
+      assert(vtx == IDTX_1D);
+      // 4-pt IDTX scales by NewSqrt2 via Q15 identity:
+      //   a + mulhrs(a, NewSqrt2*8 - 32768) == (a*NewSqrt2 + 2048) >> 12.
+      const __m128i r02 = _mm_unpacklo_epi64(r0, r2);
+      const __m128i r13 = _mm_unpacklo_epi64(r1, r3);
+      const __m128i scale = _mm_set1_epi16((int16_t)(NewSqrt2 * 8 - 32768));
+      p02 = _mm_add_epi16(r02, _mm_mulhrs_epi16(r02, scale));
+      p13 = _mm_add_epi16(r13, _mm_mulhrs_epi16(r13, scale));
+      break;
+    }
+  }
+
+  // Transpose 4x4: p02=[c0,c2], p13=[c1,c3] -> t01=[t0,t1], t23=[t2,t3].
+  const __m128i a0 = _mm_unpacklo_epi16(p02, p13);
+  const __m128i a1 = _mm_unpackhi_epi16(p02, p13);
+  const __m128i t01 = _mm_unpacklo_epi32(a0, a1);
+  const __m128i t23 = _mm_unpackhi_epi32(a0, a1);
+
+  switch (htx) {
+    case DCT_1D: {
+      const __m128i t1 = _mm_srli_si128(t01, 8);
+      const __m128i t3 = _mm_srli_si128(t23, 8);
+      __m256i u01, u23;
+      fdct4_256_avx2(_mm_unpacklo_epi16(t01, t1), _mm_unpacklo_epi16(t3, t23),
+                     &u01, &u23);
+      _mm256_storeu_si256((__m256i *)output, u01);
+      _mm256_storeu_si256((__m256i *)(output + 8), u23);
+      break;
+    }
+    case ADST_1D:
+    case FLIPADST_1D: {
+      __m128i a_0 = t01;
+      __m128i a_1 = _mm_srli_si128(t01, 8);
+      __m128i a_2 = t23;
+      __m128i a_3 = _mm_srli_si128(t23, 8);
+      if (htx == FLIPADST_1D) {
+        __m128i tmp = a_0;
+        a_0 = a_3;
+        a_3 = tmp;
+        tmp = a_1;
+        a_1 = a_2;
+        a_2 = tmp;
+      }
+      __m256i u01, u23;
+      fadst4_256_avx2(a_0, a_1, a_2, &a_3, &u01, &u23);
+      _mm256_storeu_si256((__m256i *)output, u01);
+      _mm256_storeu_si256((__m256i *)(output + 8), u23);
+      break;
+    }
+    default: {
+      assert(htx == IDTX_1D);
+      // 4-pt IDTX scales by NewSqrt2 via Q15 identity; for bd == 8, |t| <=
+      // 2885 (well within [-23168, 23168] so int16 add cannot overflow).
+      const __m256i t =
+          _mm256_inserti128_si256(_mm256_castsi128_si256(t01), t23, 1);
+      const __m256i scale = _mm256_set1_epi16((int16_t)(NewSqrt2 * 8 - 32768));
+      const __m256i o = _mm256_add_epi16(t, _mm256_mulhrs_epi16(t, scale));
+      _mm256_storeu_si256((__m256i *)output,
+                          _mm256_cvtepi16_epi32(_mm256_castsi256_si128(o)));
+      _mm256_storeu_si256(
+          (__m256i *)(output + 8),
+          _mm256_cvtepi16_epi32(_mm256_extracti128_si256(o, 1)));
+      break;
+    }
+  }
+}
+
+static void lowbd_fwd_txfm2d_4x4_avx2(const int16_t *input, int32_t *output,
+                                      int stride, TX_TYPE tx_type, int bd) {
+  (void)bd;
+  assert(bd == 8);
+  assert(tx_type < TX_TYPES);
+  if (tx_type != DCT_DCT) {
+    lowbd_fwd_txfm2d_4x4_non_dct_avx2(input, output, stride, tx_type);
+    return;
+  }
+  const ptrdiff_t s_in = stride;
+  const __m128i r0 = _mm_loadl_epi64((const __m128i *)(input + 0 * s_in));
+  const __m128i r1 = _mm_loadl_epi64((const __m128i *)(input + 1 * s_in));
+  const __m128i r2 = _mm_loadl_epi64((const __m128i *)(input + 2 * s_in));
+  const __m128i r3 = _mm_loadl_epi64((const __m128i *)(input + 3 * s_in));
+  const __m128i cu0 = _mm_slli_epi16(_mm_unpacklo_epi16(r0, r1), 2);
+  const __m128i cu1 = _mm_slli_epi16(_mm_unpacklo_epi16(r3, r2), 2);
+  __m256i u01, u23;
+  fdct4_256_avx2(cu0, cu1, &u01, &u23);
+  const __m256i p = _mm256_packs_epi32(u01, u23);
+  // Transpose [c0,c2|c1,c3] -> tu0=(x0,x1), tu1=(x3,x2) without a .rodata mask.
+  const __m256i sh =
+      _mm256_shufflehi_epi16(_mm256_shuffle_epi32(p, 0xd8), 0xb1);
+  const __m128i s0 = _mm256_castsi256_si128(sh);
+  const __m128i s1 = _mm256_extracti128_si256(sh, 1);
+  const __m128i tu0 = _mm_unpacklo_epi32(s0, s1);
+  const __m128i tu1 = _mm_unpackhi_epi32(s0, s1);
+  fdct4_256_avx2(tu0, tu1, &u01, &u23);
+  _mm256_storeu_si256((__m256i *)output, u01);
+  _mm256_storeu_si256((__m256i *)(output + 8), u23);
+}
+
 static FwdTxfm2dFunc fwd_txfm2d_func_ls[TX_SIZES_ALL] = {
-  av1_lowbd_fwd_txfm2d_4x4_sse2,   // 4x4 transform
+  lowbd_fwd_txfm2d_4x4_avx2,       // 4x4 transform
   av1_lowbd_fwd_txfm2d_8x8_avx2,   // 8x8 transform
   lowbd_fwd_txfm2d_16x16_avx2,     // 16x16 transform
   lowbd_fwd_txfm2d_32x32_avx2,     // 32x32 transform
diff --git a/test/av1_fwd_txfm2d_test.cc b/test/av1_fwd_txfm2d_test.cc
index 909070eda0..c67884e377 100644
--- a/test/av1_fwd_txfm2d_test.cc
+++ b/test/av1_fwd_txfm2d_test.cc
@@ -245,10 +245,13 @@ using lowbd_fwd_txfm_func = void (*)(const int16_t *src_diff, tran_low_t *coeff,

 void AV1FwdTxfm2dMatchTest(TX_SIZE tx_size, lowbd_fwd_txfm_func target_func) {
   const int bd = 8;
+  const int max_val = (1 << bd) - 1;
   TxfmParam param;
   memset(&param, 0, sizeof(param));
   const int rows = tx_size_high[tx_size];
   const int cols = tx_size_wide[tx_size];
+  const int strides[2] = { 64, cols };
+  const int num_strides = (cols == 64) ? 1 : 2;
   for (int tx_type = 0; tx_type < TX_TYPES; ++tx_type) {
     if (libaom_test::IsTxSizeTypeValid(
             tx_size, static_cast<TX_TYPE>(tx_type)) == false) {
@@ -257,40 +260,76 @@ void AV1FwdTxfm2dMatchTest(TX_SIZE tx_size, lowbd_fwd_txfm_func target_func) {

     FwdTxfm2dFunc ref_func = libaom_test::fwd_txfm_func_ls[tx_size];
     if (ref_func != nullptr) {
-      DECLARE_ALIGNED(32, int16_t, input[64 * 64]) = { 0 };
+      DECLARE_ALIGNED(32, int16_t, input[64 * 64]);
       DECLARE_ALIGNED(32, int32_t, output[64 * 64]);
       DECLARE_ALIGNED(32, int32_t, ref_output[64 * 64]);
-      int input_stride = 64;
       ACMRandom rnd(ACMRandom::DeterministicSeed());
-      for (int cnt = 0; cnt < 500; ++cnt) {
-        if (cnt == 0) {
-          for (int c = 0; c < cols; ++c) {
+      for (int s_idx = 0; s_idx < num_strides; ++s_idx) {
+        const int input_stride = strides[s_idx];
+        memset(input, 0x5a, sizeof(input));
+        for (int cnt = 0; cnt < 500; ++cnt) {
+          if (cnt == 0) {
+            for (int c = 0; c < cols; ++c) {
+              for (int r = 0; r < rows; ++r) {
+                input[r * input_stride + c] = max_val;
+              }
+            }
+          } else if (cnt == 1) {
             for (int r = 0; r < rows; ++r) {
-              input[r * input_stride + c] = (1 << bd) - 1;
+              for (int c = 0; c < cols; ++c) {
+                input[r * input_stride + c] = -max_val;
+              }
             }
-          }
-        } else {
-          for (int r = 0; r < rows; ++r) {
-            for (int c = 0; c < cols; ++c) {
-              input[r * input_stride + c] = rnd.Rand16() % (1 << bd);
+          } else if (cnt == 2) {
+            for (int r = 0; r < rows; ++r) {
+              for (int c = 0; c < cols; ++c) {
+                input[r * input_stride + c] =
+                    ((r ^ c) & 1) ? -max_val : max_val;
+              }
+            }
+          } else if (cnt == 3) {
+            for (int r = 0; r < rows; ++r) {
+              for (int c = 0; c < cols; ++c) {
+                input[r * input_stride + c] = (r & 1) ? -max_val : max_val;
+              }
+            }
+          } else if (cnt == 4) {
+            for (int r = 0; r < rows; ++r) {
+              for (int c = 0; c < cols; ++c) {
+                input[r * input_stride + c] = (c & 1) ? -max_val : max_val;
+              }
+            }
+          } else if (cnt & 1) {
+            for (int r = 0; r < rows; ++r) {
+              for (int c = 0; c < cols; ++c) {
+                input[r * input_stride + c] = rnd.Rand16() % (1 << bd);
+              }
+            }
+          } else {
+            for (int r = 0; r < rows; ++r) {
+              for (int c = 0; c < cols; ++c) {
+                input[r * input_stride + c] = static_cast<int16_t>(
+                    (rnd.Rand16() % (2 * max_val + 1)) - max_val);
+              }
             }
           }
-        }
-        param.tx_type = (TX_TYPE)tx_type;
-        param.tx_size = (TX_SIZE)tx_size;
-        param.tx_set_type = EXT_TX_SET_ALL16;
-        param.bd = bd;
-        ref_func(input, ref_output, input_stride, (TX_TYPE)tx_type, bd);
-        target_func(input, output, input_stride, &param);
-        const int check_cols = AOMMIN(32, cols);
-        const int check_rows = AOMMIN(32, rows * cols / check_cols);
-        for (int r = 0; r < check_rows; ++r) {
-          for (int c = 0; c < check_cols; ++c) {
-            ASSERT_EQ(ref_output[r * check_cols + c],
-                      output[r * check_cols + c])
-                << "[" << r << "," << c << "] cnt:" << cnt
-                << " tx_size: " << cols << "x" << rows
-                << " tx_type: " << tx_type_name[tx_type];
+          param.tx_type = (TX_TYPE)tx_type;
+          param.tx_size = (TX_SIZE)tx_size;
+          param.tx_set_type = EXT_TX_SET_ALL16;
+          param.bd = bd;
+          const int check_cols = AOMMIN(32, cols);
+          const int check_rows = AOMMIN(32, rows * cols / check_cols);
+          memset(output, 0x5a, sizeof(output[0]) * check_rows * check_cols);
+          ref_func(input, ref_output, input_stride, (TX_TYPE)tx_type, bd);
+          target_func(input, output, input_stride, &param);
+          for (int r = 0; r < check_rows; ++r) {
+            for (int c = 0; c < check_cols; ++c) {
+              ASSERT_EQ(ref_output[r * check_cols + c],
+                        output[r * check_cols + c])
+                  << "[" << r << "," << c << "] cnt:" << cnt
+                  << " stride: " << input_stride << " tx_size: " << cols << "x"
+                  << rows << " tx_type: " << tx_type_name[tx_type];
+            }
           }
         }
       }