Commit 6068d5baf9 for aom

commit 6068d5baf98647fa2b10f6b8e7760c75ea7499a3
Author: Frank Galligan <fgalligan@google.com>
Date:   Wed Nov 4 13:06:27 2026 -0800

    cfl: optimize SSSE3/AVX2 cfl_predict

    Optimize lowbd 4xN/8xN SSSE3 and 16xN AVX2 cfl_predict kernels:
    - Add 16xN AVX2 cfl_predict_lbd kernels (previously falling back to
      SSSE3) with paired-row _mm256_packus_epi16 +
      _mm256_permute4x64_epi64 output packing.
    - Pair rows in 4xN and 8xN SSSE3 cfl_predict_lbd loops (packing two
      rows per iteration via _mm_unpacklo_epi64 / _mm_packus_epi16).
    - Remove the unreachable width < 16 check in cfl_predict_lbd_ssse3 and
      unused 16xN prototypes in cfl_simd.h, and broadcast *dst via
      _mm256_set1_epi16 in cfl_predict_hbd_avx2 (matching
      cfl_predict_hbd_ssse3).

    Speedup vs origin/main (AMD EPYC 7B13, --cpu-used=6, bit-exact):
      -0.35% 1080p allintra (1T), -0.22% 720p allintra (1T),
      -1.22% 720p good (8T).
    Binary size (x86_64 -O3): libaom.a .text +1.3 KiB (+0.02%),
      .rodata +0.1 KiB (+0.01%); stripped aomenc +0.0 KiB (+0.00%).
    Memory (peak RSS, 1080p allintra): 1T -96 KiB (-0.21%),
      8T +1180 KiB (+2.20%).

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

diff --git a/av1/common/x86/cfl_avx2.c b/av1/common/x86/cfl_avx2.c
index 4ee0b220ca..62a2ef343c 100644
--- a/av1/common/x86/cfl_avx2.c
+++ b/av1/common/x86/cfl_avx2.c
@@ -8,7 +8,9 @@
  * Media Patent License 1.0 was not distributed with this source code in the
  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
  */
+#include <assert.h>
 #include <immintrin.h>
+#include <stddef.h>

 #include "config/av1_rtcd.h"

@@ -254,13 +256,29 @@ static inline __m256i predict_unclipped(const __m256i *input, __m256i alpha_q12,
 static inline void cfl_predict_lbd_avx2(const int16_t *pred_buf_q3,
                                         uint8_t *dst, int dst_stride,
                                         int alpha_q3, int width, int height) {
-  (void)width;
+  assert(width == 16 || width == 32);
+  assert(height >= 2 && (height & 1) == 0);
   const __m256i alpha_sign = _mm256_set1_epi16(alpha_q3);
   const __m256i alpha_q12 = _mm256_slli_epi16(_mm256_abs_epi16(alpha_sign), 9);
   const __m256i dc_q0 = _mm256_set1_epi16(*dst);
   __m256i *row = (__m256i *)pred_buf_q3;
   const __m256i *row_end = row + height * CFL_BUF_LINE_I256;

+  if (width == 16) {
+    do {
+      const __m256i res0 = predict_unclipped(row, alpha_q12, alpha_sign, dc_q0);
+      const __m256i res1 = predict_unclipped(row + CFL_BUF_LINE_I256, alpha_q12,
+                                             alpha_sign, dc_q0);
+      __m256i res = _mm256_packus_epi16(res0, res1);
+      res = _mm256_permute4x64_epi64(res, _MM_SHUFFLE(3, 1, 2, 0));
+      _mm_storeu_si128((__m128i *)dst, _mm256_castsi256_si128(res));
+      _mm_storeu_si128((__m128i *)(dst + dst_stride),
+                       _mm256_extracti128_si256(res, 1));
+      dst += (ptrdiff_t)dst_stride * 2;
+    } while ((row += CFL_BUF_LINE_I256 << 1) != row_end);
+    return;
+  }
+
   do {
     __m256i res = predict_unclipped(row, alpha_q12, alpha_sign, dc_q0);
     __m256i next = predict_unclipped(row + 1, alpha_q12, alpha_sign, dc_q0);
@@ -271,31 +289,35 @@ static inline void cfl_predict_lbd_avx2(const int16_t *pred_buf_q3,
   } while ((row += CFL_BUF_LINE_I256) < row_end);
 }

+CFL_PREDICT_X(avx2, 16, 4, lbd)
+CFL_PREDICT_X(avx2, 16, 8, lbd)
+CFL_PREDICT_X(avx2, 16, 16, lbd)
+CFL_PREDICT_X(avx2, 16, 32, lbd)
 CFL_PREDICT_X(avx2, 32, 8, lbd)
 CFL_PREDICT_X(avx2, 32, 16, lbd)
 CFL_PREDICT_X(avx2, 32, 32, lbd)

 cfl_predict_lbd_fn cfl_get_predict_lbd_fn_avx2(TX_SIZE tx_size) {
   static const cfl_predict_lbd_fn pred[TX_SIZES_ALL] = {
-    cfl_predict_lbd_4x4_ssse3,   /* 4x4 */
-    cfl_predict_lbd_8x8_ssse3,   /* 8x8 */
-    cfl_predict_lbd_16x16_ssse3, /* 16x16 */
-    cfl_predict_lbd_32x32_avx2,  /* 32x32 */
-    NULL,                        /* 64x64 (invalid CFL size) */
-    cfl_predict_lbd_4x8_ssse3,   /* 4x8 */
-    cfl_predict_lbd_8x4_ssse3,   /* 8x4 */
-    cfl_predict_lbd_8x16_ssse3,  /* 8x16 */
-    cfl_predict_lbd_16x8_ssse3,  /* 16x8 */
-    cfl_predict_lbd_16x32_ssse3, /* 16x32 */
-    cfl_predict_lbd_32x16_avx2,  /* 32x16 */
-    NULL,                        /* 32x64 (invalid CFL size) */
-    NULL,                        /* 64x32 (invalid CFL size) */
-    cfl_predict_lbd_4x16_ssse3,  /* 4x16  */
-    cfl_predict_lbd_16x4_ssse3,  /* 16x4  */
-    cfl_predict_lbd_8x32_ssse3,  /* 8x32  */
-    cfl_predict_lbd_32x8_avx2,   /* 32x8  */
-    NULL,                        /* 16x64 (invalid CFL size) */
-    NULL,                        /* 64x16 (invalid CFL size) */
+    cfl_predict_lbd_4x4_ssse3,  /* 4x4 */
+    cfl_predict_lbd_8x8_ssse3,  /* 8x8 */
+    cfl_predict_lbd_16x16_avx2, /* 16x16 */
+    cfl_predict_lbd_32x32_avx2, /* 32x32 */
+    NULL,                       /* 64x64 (invalid CFL size) */
+    cfl_predict_lbd_4x8_ssse3,  /* 4x8 */
+    cfl_predict_lbd_8x4_ssse3,  /* 8x4 */
+    cfl_predict_lbd_8x16_ssse3, /* 8x16 */
+    cfl_predict_lbd_16x8_avx2,  /* 16x8 */
+    cfl_predict_lbd_16x32_avx2, /* 16x32 */
+    cfl_predict_lbd_32x16_avx2, /* 32x16 */
+    NULL,                       /* 32x64 (invalid CFL size) */
+    NULL,                       /* 64x32 (invalid CFL size) */
+    cfl_predict_lbd_4x16_ssse3, /* 4x16  */
+    cfl_predict_lbd_16x4_avx2,  /* 16x4  */
+    cfl_predict_lbd_8x32_ssse3, /* 8x32  */
+    cfl_predict_lbd_32x8_avx2,  /* 32x8  */
+    NULL,                       /* 16x64 (invalid CFL size) */
+    NULL,                       /* 64x16 (invalid CFL size) */
   };
   // Modulo TX_SIZES_ALL to ensure that an attacker won't be able to index the
   // function pointer array out of bounds.
@@ -321,7 +343,7 @@ static inline void cfl_predict_hbd_avx2(const int16_t *pred_buf_q3,
   assert(width == 16 || width == 32);
   const __m256i alpha_sign = _mm256_set1_epi16(alpha_q3);
   const __m256i alpha_q12 = _mm256_slli_epi16(_mm256_abs_epi16(alpha_sign), 9);
-  const __m256i dc_q0 = _mm256_loadu_si256((__m256i *)dst);
+  const __m256i dc_q0 = _mm256_set1_epi16(*dst);
   const __m256i max = highbd_max_epi16(bd);

   __m256i *row = (__m256i *)pred_buf_q3;
diff --git a/av1/common/x86/cfl_simd.h b/av1/common/x86/cfl_simd.h
index 9f46ab5781..3ec3c66db2 100644
--- a/av1/common/x86/cfl_simd.h
+++ b/av1/common/x86/cfl_simd.h
@@ -208,15 +208,6 @@ void cfl_predict_lbd_8x16_ssse3(const int16_t *pred_buf_q3, uint8_t *dst,
 void cfl_predict_lbd_8x32_ssse3(const int16_t *pred_buf_q3, uint8_t *dst,
                                 int dst_stride, int alpha_q3);

-void cfl_predict_lbd_16x4_ssse3(const int16_t *pred_buf_q3, uint8_t *dst,
-                                int dst_stride, int alpha_q3);
-void cfl_predict_lbd_16x8_ssse3(const int16_t *pred_buf_q3, uint8_t *dst,
-                                int dst_stride, int alpha_q3);
-void cfl_predict_lbd_16x16_ssse3(const int16_t *pred_buf_q3, uint8_t *dst,
-                                 int dst_stride, int alpha_q3);
-void cfl_predict_lbd_16x32_ssse3(const int16_t *pred_buf_q3, uint8_t *dst,
-                                 int dst_stride, int alpha_q3);
-
 #if CONFIG_AV1_HIGHBITDEPTH
 void cfl_predict_hbd_4x4_ssse3(const int16_t *pred_buf_q3, uint16_t *dst,
                                int dst_stride, int alpha_q3, int bd);
diff --git a/av1/common/x86/cfl_ssse3.c b/av1/common/x86/cfl_ssse3.c
index 5441441beb..008780a213 100644
--- a/av1/common/x86/cfl_ssse3.c
+++ b/av1/common/x86/cfl_ssse3.c
@@ -9,10 +9,14 @@
  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
  */

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

 #include "config/av1_rtcd.h"

+#include "aom_dsp/x86/mem_sse2.h"
+#include "aom_dsp/x86/synonyms.h"
 #include "av1/common/cfl.h"

 #include "av1/common/x86/cfl_simd.h"
@@ -301,41 +305,63 @@ static inline void cfl_luma_subsampling_444_hbd_ssse3(const uint16_t *input,

 CFL_GET_SUBSAMPLE_FUNCTION(ssse3)

-static inline __m128i predict_unclipped(const __m128i *input, __m128i alpha_q12,
-                                        __m128i alpha_sign, __m128i dc_q0) {
-  __m128i ac_q3 = _mm_loadu_si128(input);
+static inline __m128i predict_unclipped_vec(__m128i ac_q3, __m128i alpha_q12,
+                                            __m128i alpha_sign, __m128i dc_q0) {
   __m128i ac_sign = _mm_sign_epi16(alpha_sign, ac_q3);
   __m128i scaled_luma_q0 = _mm_mulhrs_epi16(_mm_abs_epi16(ac_q3), alpha_q12);
   scaled_luma_q0 = _mm_sign_epi16(scaled_luma_q0, ac_sign);
   return _mm_add_epi16(scaled_luma_q0, dc_q0);
 }

+static inline __m128i predict_unclipped(const __m128i *input, __m128i alpha_q12,
+                                        __m128i alpha_sign, __m128i dc_q0) {
+  return predict_unclipped_vec(_mm_loadu_si128(input), alpha_q12, alpha_sign,
+                               dc_q0);
+}
+
 static inline void cfl_predict_lbd_ssse3(const int16_t *pred_buf_q3,
                                          uint8_t *dst, int dst_stride,
                                          int alpha_q3, int width, int height) {
+  assert(width == 4 || width == 8 || width == 16 || width == 32);
+  assert(height >= 2 && (height & 1) == 0);
   const __m128i alpha_sign = _mm_set1_epi16(alpha_q3);
   const __m128i alpha_q12 = _mm_slli_epi16(_mm_abs_epi16(alpha_sign), 9);
   const __m128i dc_q0 = _mm_set1_epi16(*dst);
   __m128i *row = (__m128i *)pred_buf_q3;
   const __m128i *row_end = row + height * CFL_BUF_LINE_I128;
+  if (width == 4) {
+    do {
+      const __m128i ac_q3 = xx_loadu_2x64(row + CFL_BUF_LINE_I128, row);
+      __m128i res = predict_unclipped_vec(ac_q3, alpha_q12, alpha_sign, dc_q0);
+      res = _mm_packus_epi16(res, res);
+      xx_storel_32(dst, res);
+      xx_storel_32(dst + dst_stride, _mm_srli_si128(res, 4));
+      dst += (ptrdiff_t)dst_stride * 2;
+    } while ((row += CFL_BUF_LINE_I128 << 1) != row_end);
+    return;
+  }
+  if (width == 8) {
+    do {
+      const __m128i res0 = predict_unclipped(row, alpha_q12, alpha_sign, dc_q0);
+      const __m128i res1 = predict_unclipped(row + CFL_BUF_LINE_I128, alpha_q12,
+                                             alpha_sign, dc_q0);
+      const __m128i res = _mm_packus_epi16(res0, res1);
+      _mm_storel_epi64((__m128i *)dst, res);
+      _mm_storeh_epi64((__m128i *)(dst + dst_stride), res);
+      dst += (ptrdiff_t)dst_stride * 2;
+    } while ((row += CFL_BUF_LINE_I128 << 1) != row_end);
+    return;
+  }
   do {
     __m128i res = predict_unclipped(row, alpha_q12, alpha_sign, dc_q0);
-    if (width < 16) {
-      res = _mm_packus_epi16(res, res);
-      if (width == 4)
-        _mm_storeh_epi32((__m128i *)dst, res);
-      else
-        _mm_storel_epi64((__m128i *)dst, res);
-    } else {
-      __m128i next = predict_unclipped(row + 1, alpha_q12, alpha_sign, dc_q0);
+    __m128i next = predict_unclipped(row + 1, alpha_q12, alpha_sign, dc_q0);
+    res = _mm_packus_epi16(res, next);
+    _mm_storeu_si128((__m128i *)dst, res);
+    if (width == 32) {
+      res = predict_unclipped(row + 2, alpha_q12, alpha_sign, dc_q0);
+      next = predict_unclipped(row + 3, alpha_q12, alpha_sign, dc_q0);
       res = _mm_packus_epi16(res, next);
-      _mm_storeu_si128((__m128i *)dst, res);
-      if (width == 32) {
-        res = predict_unclipped(row + 2, alpha_q12, alpha_sign, dc_q0);
-        next = predict_unclipped(row + 3, alpha_q12, alpha_sign, dc_q0);
-        res = _mm_packus_epi16(res, next);
-        _mm_storeu_si128((__m128i *)(dst + 16), res);
-      }
+      _mm_storeu_si128((__m128i *)(dst + 16), res);
     }
     dst += dst_stride;
   } while ((row += CFL_BUF_LINE_I128) < row_end);