Commit 33cfaa9c19 for aom

commit 33cfaa9c19284727af97d27e82d7ac4faaee6ced
Author: Frank Galligan <fgalligan@google.com>
Date:   Tue Nov 24 16:18:20 2026 -0800

    tx_search: add SSE2/AVX2 RTCD av1_sub_sum_sse_lbd

    Add an RTCD-dispatched fused 8-bit subtract + sum + SSE helper
    av1_sub_sum_sse_lbd() (C fallback in av1/encoder/tx_search.c, SSE2
    kernel in av1/encoder/x86/tx_search_sse2.c, AVX2 kernel in
    av1/encoder/x86/tx_search_avx2.c, unit tests in test/subtract_test.cc)
    and wire it into intra block_rd_txfm() / search_tx_type() under
    #if HAVE_SSE2 for fully visible 8-bit blocks up to 32x32, leaving
    inter paths and non-x86 builds (ARM64/NEON, generic C) on
    aom_subtract_block + pixel_diff_stats().

    Adding the SSE2 specialization ensures pre-AVX2 x86 CPUs under runtime
    CPU detect (e.g., AOM_SIMD_CAPS_MASK=0x7) and static SSE2-SSE4.2 builds
    execute a fused SSE2 kernel instead of scalar C av1_sub_sum_sse_lbd_c()
    (runtime detect) or the 2-pass SSE2 path (static pre-AVX2).

    SSE2 kernel speedups (4x4..32x32, stride 384, AOM_SIMD_CAPS_MASK=0x7,
    AMD EPYC 7B13, Clang 21, 3-run median): 1.26x-1.85x over
    aom_subtract_block_sse2 + aom_sum_sse_2d_i16_sse2 (4x4: 1.26x,
    16x16: 1.43x, 32x32: 1.30x), 3.71x-11.21x over av1_sub_sum_sse_lbd_c,
    and 1.61x vs base / 8.36x vs C on 4096 mixed sizes.

    AVX2 kernel speedups (4x4..32x32, stride 384, AMD EPYC 7B13, Clang 21):
    1.23x-1.99x over aom_subtract_block + aom_sum_sse_2d_i16 (4x4: 1.23x,
    32x32: 1.39x), 3.13x-20.57x over av1_sub_sum_sse_lbd_c (4x4: 3.13x,
    32x32: 20.57x), and 2.07x vs base / 10.80x vs C on 4096 mixed sizes.

    Placed in dedicated av1/encoder/x86/tx_search_{sse2,avx2}.c units so
    they stay enabled under CONFIG_HIGHWAY=1 and keep generic tx_search.c
    free of target-specific intrinsics.

    Speedup: -0.94% (1T) / -2.60% (8T) CPU time (allintra cpu6 720p),
    +0.06% (good cpu6 480p), -0.02% (rt cpu8 VGA) on AMD EPYC 7B13
    (Clang 21); bit-exact vs main across 1T, 8T, and
    AOM_SIMD_CAPS_MASK=0x0/0x7/0x1f/0xff.
    Binary size (x86_64 -O3): libaom.a .text +1.8 KiB (+0.02%),
      .rodata +0.0 KiB (+0.00%); stripped aomenc +0.0 KiB (+0.00%).
    Memory (peak RSS, 1080p allintra): 0 heap allocations; 1T -100 KiB
      (-0.21%), +652 KiB 8T allocator variance.

    TAG=agy
    CONV=4b696004-0036-42b2-b3fa-d1c7d63a973c
    Change-Id: Ia0edb050c9f96aeb75d884f905df707a779e3b50

diff --git a/av1/av1.cmake b/av1/av1.cmake
index 732fd2d564..349ebeaeb8 100644
--- a/av1/av1.cmake
+++ b/av1/av1.cmake
@@ -357,6 +357,7 @@ list(APPEND AOM_AV1_ENCODER_INTRIN_SSE2
             "${AOM_ROOT}/av1/encoder/x86/model_rd_sse2.c"
             "${AOM_ROOT}/av1/encoder/x86/reconinter_enc_sse2.c"
             "${AOM_ROOT}/av1/encoder/x86/temporal_filter_sse2.c"
+            "${AOM_ROOT}/av1/encoder/x86/tx_search_sse2.c"
             "${AOM_ROOT}/av1/encoder/x86/wedge_utils_sse2.c")

 # The functions defined in these files are removed from rtcd when
@@ -390,6 +391,7 @@ list(APPEND AOM_AV1_ENCODER_INTRIN_AVX2
             "${AOM_ROOT}/av1/encoder/x86/rdopt_avx2.c"
             "${AOM_ROOT}/av1/encoder/x86/av1_k_means_avx2.c"
             "${AOM_ROOT}/av1/encoder/x86/temporal_filter_avx2.c"
+            "${AOM_ROOT}/av1/encoder/x86/tx_search_avx2.c"
             "${AOM_ROOT}/av1/encoder/x86/pickrst_avx2.c")

 # The functions defined in these files are removed from rtcd when
diff --git a/av1/common/av1_rtcd_defs.pl b/av1/common/av1_rtcd_defs.pl
index dd70ffca44..1bb08ffe2a 100644
--- a/av1/common/av1_rtcd_defs.pl
+++ b/av1/common/av1_rtcd_defs.pl
@@ -307,6 +307,9 @@ if (aom_config("CONFIG_AV1_ENCODER") eq "yes") {
   add_proto qw/int64_t av1_block_error_lp/, "const int16_t *coeff, const int16_t *dqcoeff, intptr_t block_size";
   specialize qw/av1_block_error_lp sse2 avx2 neon sve/;

+  add_proto qw/uint64_t av1_sub_sum_sse_lbd/, "const uint8_t *src, int src_stride, const uint8_t *dst, int dst_stride, int16_t *diff, int diff_stride, int width, int height, int *sum";
+  specialize qw/av1_sub_sum_sse_lbd sse2 avx2/;
+
   add_proto qw/void av1_cost_tokens_from_cdf_2_batch/, "int *costs, int costs_stride, const uint16_t *cdf, int cdf_stride, int n";
   specialize qw/av1_cost_tokens_from_cdf_2_batch avx2/;

diff --git a/av1/encoder/tx_search.c b/av1/encoder/tx_search.c
index fdb1349678..d12281c72a 100644
--- a/av1/encoder/tx_search.c
+++ b/av1/encoder/tx_search.c
@@ -11,7 +11,9 @@

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

+#include "config/aom_config.h"
 #include "config/av1_rtcd.h"

 #include "av1/common/cfl.h"
@@ -125,6 +127,31 @@ static inline void fetch_mb_rd_info(int n4, const MB_RD_INFO *const mb_rd_info,
   *rd_stats = mb_rd_info->rd_stats;
 }

+uint64_t av1_sub_sum_sse_lbd_c(const uint8_t *src, int src_stride,
+                               const uint8_t *dst, int dst_stride,
+                               int16_t *diff, int diff_stride, int width,
+                               int height, int *sum) {
+  assert(width == 4 || width == 8 || width == 16 || width == 32);
+  assert(height == 4 || height == 8 || height == 16 || height == 32);
+  assert(diff_stride >= width);
+  assert(sum != NULL);
+  int32_t acc_sum = 0;
+  uint64_t sse = 0;
+  for (int r = 0; r < height; ++r) {
+    const uint8_t *const sr = src + (ptrdiff_t)r * src_stride;
+    const uint8_t *const dr = dst + (ptrdiff_t)r * dst_stride;
+    int16_t *const fr = diff + (ptrdiff_t)r * diff_stride;
+    for (int c = 0; c < width; ++c) {
+      const int d = (int)sr[c] - (int)dr[c];
+      fr[c] = (int16_t)d;
+      acc_sum += d;
+      sse += (uint64_t)(d * d);
+    }
+  }
+  *sum = acc_sum;
+  return sse;
+}
+
 int64_t av1_pixel_diff_dist(const MACROBLOCK *x, int plane, int blk_row,
                             int blk_col, const BLOCK_SIZE plane_bsize,
                             const BLOCK_SIZE tx_bsize,
@@ -2029,23 +2056,18 @@ static int skip_trellis_opt_based_on_satd(MACROBLOCK *x,
   return skip_block_trellis;
 }

-// Predict DC only blocks if the residual variance is below a qstep based
-// threshold.For such blocks, transform type search is bypassed.
-static inline void predict_dc_only_block(
+// Shared DC-only / skip decision helper given precomputed block variance and
+// signed per-pixel mean (Q7), updating best_rd_stats (if skipped) or
+// *dc_only_blk.
+static inline void predict_dc_only_from_stats(
     MACROBLOCK *x, int plane, BLOCK_SIZE plane_bsize, TX_SIZE tx_size,
-    int block, int blk_row, int blk_col, RD_STATS *best_rd_stats,
-    int64_t *block_sse, unsigned int *block_mse_q8, int64_t *per_px_mean,
-    int *dc_only_blk) {
+    int block, uint64_t block_var, int64_t per_px_mean, RD_STATS *best_rd_stats,
+    int64_t *block_sse, int *dc_only_blk) {
   MACROBLOCKD *xd = &x->e_mbd;
   MB_MODE_INFO *mbmi = xd->mi[0];
   const int dequant_shift = (is_cur_buf_hbd(xd)) ? xd->bd - 5 : 3;
   const int qstep = x->plane[plane].dequant_QTX[1] >> dequant_shift;
-  uint64_t block_var = UINT64_MAX;
   const int dc_qstep = x->plane[plane].dequant_QTX[0] >> 3;
-  *block_sse = pixel_diff_stats(x, plane, blk_row, blk_col, plane_bsize,
-                                txsize_to_bsize[tx_size], block_mse_q8,
-                                per_px_mean, &block_var);
-  assert((*block_mse_q8) != UINT_MAX);
   uint64_t var_threshold = (uint64_t)(1.8 * qstep * qstep);
   if (is_cur_buf_hbd(xd))
     block_var = ROUND_POWER_OF_TWO(block_var, (xd->bd - 8) * 2);
@@ -2056,7 +2078,7 @@ static inline void predict_dc_only_block(

   // Prediction of skip block if residual mean and variance are less
   // than qstep based threshold
-  if ((llabs(*per_px_mean) * dc_coeff_scale[tx_size]) < (dc_qstep << 12)) {
+  if ((llabs(per_px_mean) * dc_coeff_scale[tx_size]) < (dc_qstep << 12)) {
     // If the normalized mean of residual block is less than the dc qstep and
     // the  normalized block variance is less than ac qstep, then the block is
     // assumed to be a skip block and its rdcost is updated accordingly.
@@ -2108,11 +2130,15 @@ static inline bool palette_tries_idtx(const AV1_COMP *cpi, const MACROBLOCK *x,

 // Search for the best transform type for a given transform block.
 // This function can be used for both inter and intra, both luma and chroma.
+// When precalc_sse >= 0, precalc_sse and precalc_sum hold the SSE and
+// residual sum from av1_sub_sum_sse_lbd() for a fully visible 8-bit intra block
+// (txw, txh <= 32, !cpi->do_border_pad), avoiding a second pass over src_diff.
 static void search_tx_type(const AV1_COMP *cpi, MACROBLOCK *x, int plane,
                            int block, int blk_row, int blk_col,
                            BLOCK_SIZE plane_bsize, TX_SIZE tx_size,
                            const TXB_CTX *const txb_ctx,
                            FAST_TX_SEARCH_MODE ftxs_mode, int64_t ref_best_rd,
+                           int64_t precalc_sse, int precalc_sum,
                            RD_STATS *best_rd_stats) {
   const AV1_COMMON *cm = &cpi->common;
   MACROBLOCKD *xd = &x->e_mbd;
@@ -2149,30 +2175,52 @@ static void search_tx_type(const AV1_COMP *cpi, MACROBLOCK *x, int plane,
   const bool predict_dc_block =
       txfm_params->predict_dc_level >= 1 && txw != 64 && txh != 64;
   int64_t per_px_mean = INT64_MAX;
+  uint64_t block_var = UINT64_MAX;

   int is_border_block = 0;
-  if (cpi->do_border_pad) {
-    is_border_block =
-        get_visible_dimensions(x, plane, plane_bsize, blk_col, blk_row, txw,
-                               txh, /*clip_dims=*/true, NULL, NULL);
-    if (is_border_block)
-      av1_subtract_txb(x, plane, plane_bsize, blk_col, blk_row, tx_size,
-                       best_tx_type, cpi->do_border_pad);
+  if (precalc_sse >= 0) {
+    assert(!is_cur_buf_hbd(xd) && !cpi->do_border_pad);
+    const uint64_t raw_sse = (uint64_t)precalc_sse;
+    const int log2_n = num_pels_log2_lookup[txsize_to_bsize[tx_size]];
+    block_sse = precalc_sse;
+    block_mse_q8 = (unsigned int)((raw_sse << 8) >> log2_n);
+    if (predict_dc_block) {
+      block_var = raw_sse -
+                  (((uint64_t)((int64_t)precalc_sum * precalc_sum)) >> log2_n);
+      const int64_t abs_mean = (int64_t)(abs(precalc_sum) >> log2_n) << 7;
+      per_px_mean = (precalc_sum > 0) ? abs_mean : -abs_mean;
+    }
+  } else {
+    if (cpi->do_border_pad) {
+      is_border_block =
+          get_visible_dimensions(x, plane, plane_bsize, blk_col, blk_row, txw,
+                                 txh, /*clip_dims=*/true, NULL, NULL);
+      if (is_border_block)
+        av1_subtract_txb(x, plane, plane_bsize, blk_col, blk_row, tx_size,
+                         best_tx_type, cpi->do_border_pad);
+    }
+
+    if (predict_dc_block) {
+      block_sse = pixel_diff_stats(x, plane, blk_row, blk_col, plane_bsize,
+                                   txsize_to_bsize[tx_size], &block_mse_q8,
+                                   &per_px_mean, &block_var);
+      assert(block_mse_q8 != UINT_MAX);
+    } else {
+      block_sse = av1_pixel_diff_dist(x, plane, blk_row, blk_col, plane_bsize,
+                                      txsize_to_bsize[tx_size], &block_mse_q8);
+      assert(block_mse_q8 != UINT_MAX);
+    }
   }

   if (predict_dc_block) {
-    predict_dc_only_block(x, plane, plane_bsize, tx_size, block, blk_row,
-                          blk_col, best_rd_stats, &block_sse, &block_mse_q8,
-                          &per_px_mean, &dc_only_blk);
+    predict_dc_only_from_stats(x, plane, plane_bsize, tx_size, block, block_var,
+                               per_px_mean, best_rd_stats, &block_sse,
+                               &dc_only_blk);
     if (best_rd_stats->skip_txfm == 1) {
       const TX_TYPE tx_type = DCT_DCT;
       if (plane == 0) xd->tx_type_map[tx_type_map_idx] = tx_type;
       return;
     }
-  } else {
-    block_sse = av1_pixel_diff_dist(x, plane, blk_row, blk_col, plane_bsize,
-                                    txsize_to_bsize[tx_size], &block_mse_q8);
-    assert(block_mse_q8 != UINT_MAX);
   }

   // Bit mask to indicate which transform types are allowed in the RD search.
@@ -2512,7 +2560,8 @@ static inline void tx_type_rd(const AV1_COMP *cpi, MACROBLOCK *x,
   assert(is_inter_block(x->e_mbd.mi[0]));
   RD_STATS this_rd_stats;
   search_tx_type(cpi, x, 0, block, blk_row, blk_col, plane_bsize, tx_size,
-                 txb_ctx, ftxs_mode, ref_rdcost, &this_rd_stats);
+                 txb_ctx, ftxs_mode, ref_rdcost, /*precalc_sse=*/-1,
+                 /*precalc_sum=*/0, &this_rd_stats);

   av1_merge_rd_stats(rd_stats, &this_rd_stats);
 }
@@ -3209,10 +3258,34 @@ static inline void block_rd_txfm(int plane, int block, int blk_row, int blk_col,
   RD_STATS this_rd_stats;
   av1_init_rd_stats(&this_rd_stats);

+  int64_t precalc_sse = -1;
+  int precalc_sum = 0;
   if (!is_inter) {
     av1_predict_intra_block_facade(cm, xd, plane, blk_col, blk_row, tx_size);
-    av1_subtract_txb(x, plane, plane_bsize, blk_col, blk_row, tx_size, DCT_DCT,
-                     false);
+#if HAVE_SSE2
+    // Only x86 has SIMD av1_sub_sum_sse_lbd(); elsewhere the C version is
+    // slower than aom_subtract_block() + aom_sum_sse_2d_i16().
+    const int txw = tx_size_wide[tx_size];
+    const int txh = tx_size_high[tx_size];
+    if (!is_cur_buf_hbd(xd) && x->pix_to_bottom_edge >= 0 &&
+        x->pix_to_right_edge >= 0 && ((txw | txh) & 64) == 0 &&
+        !cpi->do_border_pad) {
+      struct macroblock_plane *const p = &x->plane[plane];
+      const struct macroblockd_plane *const pd = &xd->plane[plane];
+      const int diff_stride = block_size_wide[plane_bsize];
+      precalc_sse = (int64_t)av1_sub_sum_sse_lbd(
+          &p->src.buf[(blk_row * p->src.stride + blk_col) << MI_SIZE_LOG2],
+          p->src.stride,
+          &pd->dst.buf[(blk_row * pd->dst.stride + blk_col) << MI_SIZE_LOG2],
+          pd->dst.stride,
+          &p->src_diff[(blk_row * diff_stride + blk_col) << MI_SIZE_LOG2],
+          diff_stride, txw, txh, &precalc_sum);
+    } else
+#endif  // HAVE_SSE2
+    {
+      av1_subtract_txb(x, plane, plane_bsize, blk_col, blk_row, tx_size,
+                       DCT_DCT, false);
+    }
 #if !CONFIG_REALTIME_ONLY
     const TxfmSearchParams *const txfm_params = &x->txfm_search_params;
     if (txfm_params->enable_nn_prune_intra_tx_depths) {
@@ -3231,7 +3304,7 @@ static inline void block_rd_txfm(int plane, int block, int blk_row, int blk_col,
   get_txb_ctx(plane_bsize, tx_size, plane, a, l, &txb_ctx);
   search_tx_type(cpi, x, plane, block, blk_row, blk_col, plane_bsize, tx_size,
                  &txb_ctx, args->ftxs_mode, args->best_rd - args->current_rd,
-                 &this_rd_stats);
+                 precalc_sse, precalc_sum, &this_rd_stats);

 #if !CONFIG_REALTIME_ONLY
   if (plane == AOM_PLANE_Y && xd->cfl.store_y) {
diff --git a/av1/encoder/x86/tx_search_avx2.c b/av1/encoder/x86/tx_search_avx2.c
new file mode 100644
index 0000000000..02eade56a3
--- /dev/null
+++ b/av1/encoder/x86/tx_search_avx2.c
@@ -0,0 +1,175 @@
+/*
+ * Copyright (c) 2026, Alliance for Open Media. All rights reserved.
+ *
+ * This source code is subject to the terms of the BSD 2 Clause License and
+ * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
+ * was not distributed with this source code in the LICENSE file, you can
+ * obtain it at www.aomedia.org/license/software. If the Alliance for Open
+ * 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 <stdint.h>
+
+#include "config/av1_rtcd.h"
+
+#include "aom_dsp/x86/synonyms.h"
+
+static inline __m128i sub_4x2_avx2(const uint8_t *src, ptrdiff_t sstride,
+                                   const uint8_t *dst, ptrdiff_t dstride,
+                                   int16_t *diff, ptrdiff_t fstride) {
+  const __m128i s0 = xx_loadl_32(src);
+  const __m128i s1 = xx_loadl_32(src + sstride);
+  const __m128i d0 = xx_loadl_32(dst);
+  const __m128i d1 = xx_loadl_32(dst + dstride);
+  const __m128i r01 =
+      _mm_sub_epi16(_mm_cvtepu8_epi16(_mm_unpacklo_epi32(s0, s1)),
+                    _mm_cvtepu8_epi16(_mm_unpacklo_epi32(d0, d1)));
+  _mm_storel_epi64((__m128i *)diff, r01);
+  _mm_storeh_pi((__m64 *)(diff + fstride), _mm_castsi128_ps(r01));
+  return r01;
+}
+
+static inline __m256i sub_8x2_avx2(const uint8_t *src, ptrdiff_t sstride,
+                                   const uint8_t *dst, ptrdiff_t dstride,
+                                   int16_t *diff, ptrdiff_t fstride) {
+  const __m128i s01 =
+      _mm_unpacklo_epi64(_mm_loadl_epi64((const __m128i *)src),
+                         _mm_loadl_epi64((const __m128i *)(src + sstride)));
+  const __m128i d01 =
+      _mm_unpacklo_epi64(_mm_loadl_epi64((const __m128i *)dst),
+                         _mm_loadl_epi64((const __m128i *)(dst + dstride)));
+  const __m256i r01 =
+      _mm256_sub_epi16(_mm256_cvtepu8_epi16(s01), _mm256_cvtepu8_epi16(d01));
+  _mm_storeu_si128((__m128i *)diff, _mm256_castsi256_si128(r01));
+  _mm_storeu_si128((__m128i *)(diff + fstride),
+                   _mm256_extracti128_si256(r01, 1));
+  return r01;
+}
+
+static inline void sub_16x2_avx2(const uint8_t *src, ptrdiff_t sstride,
+                                 const uint8_t *dst, ptrdiff_t dstride,
+                                 int16_t *diff, ptrdiff_t fstride,
+                                 __m256i *sum16, __m256i *sse) {
+  const __m256i r0 = _mm256_sub_epi16(
+      _mm256_cvtepu8_epi16(_mm_loadu_si128((const __m128i *)src)),
+      _mm256_cvtepu8_epi16(_mm_loadu_si128((const __m128i *)dst)));
+  const __m256i r1 = _mm256_sub_epi16(
+      _mm256_cvtepu8_epi16(_mm_loadu_si128((const __m128i *)(src + sstride))),
+      _mm256_cvtepu8_epi16(_mm_loadu_si128((const __m128i *)(dst + dstride))));
+  _mm256_storeu_si256((__m256i *)diff, r0);
+  _mm256_storeu_si256((__m256i *)(diff + fstride), r1);
+  *sum16 = _mm256_add_epi16(r0, r1);
+  *sse = _mm256_add_epi32(_mm256_madd_epi16(r0, r0), _mm256_madd_epi16(r1, r1));
+}
+
+static inline void sub_32x1_avx2(const uint8_t *src, const uint8_t *dst,
+                                 int16_t *diff, __m256i *sum16, __m256i *sse) {
+  const __m256i r0 = _mm256_sub_epi16(
+      _mm256_cvtepu8_epi16(_mm_loadu_si128((const __m128i *)src)),
+      _mm256_cvtepu8_epi16(_mm_loadu_si128((const __m128i *)dst)));
+  const __m256i r1 = _mm256_sub_epi16(
+      _mm256_cvtepu8_epi16(_mm_loadu_si128((const __m128i *)(src + 16))),
+      _mm256_cvtepu8_epi16(_mm_loadu_si128((const __m128i *)(dst + 16))));
+  _mm256_storeu_si256((__m256i *)diff, r0);
+  _mm256_storeu_si256((__m256i *)(diff + 16), r1);
+  *sum16 = _mm256_add_epi16(r0, r1);
+  *sse = _mm256_add_epi32(_mm256_madd_epi16(r0, r0), _mm256_madd_epi16(r1, r1));
+}
+
+uint64_t av1_sub_sum_sse_lbd_avx2(const uint8_t *src, int src_stride,
+                                  const uint8_t *dst, int dst_stride,
+                                  int16_t *diff, int diff_stride, int width,
+                                  int height, int *sum) {
+  const ptrdiff_t sstride = src_stride;
+  const ptrdiff_t dstride = dst_stride;
+  const ptrdiff_t fstride = diff_stride;
+  assert(width == 4 || width == 8 || width == 16 || width == 32);
+  assert(height == 4 || height == 8 || height == 16 || height == 32);
+  assert(diff_stride >= width);
+  assert(sum != NULL);
+  int h = height;
+  if (width == 4) {
+    // Max 16 iterations * 255 = 4080 <= INT16_MAX per lane.
+    __m128i r01 = sub_4x2_avx2(src, sstride, dst, dstride, diff, fstride);
+    __m128i v_sum16 = r01;
+    __m128i v_sse = _mm_madd_epi16(r01, r01);
+    h -= 2;
+    do {
+      src += 2 * sstride;
+      dst += 2 * dstride;
+      diff += 2 * fstride;
+      r01 = sub_4x2_avx2(src, sstride, dst, dstride, diff, fstride);
+      v_sum16 = _mm_add_epi16(v_sum16, r01);
+      v_sse = _mm_add_epi32(v_sse, _mm_madd_epi16(r01, r01));
+      h -= 2;
+    } while (h != 0);
+    __m128i v_sum = _mm_madd_epi16(v_sum16, _mm_set1_epi16(1));
+    v_sum = _mm_add_epi32(v_sum, _mm_srli_si128(v_sum, 8));
+    v_sum = _mm_add_epi32(v_sum, _mm_srli_si128(v_sum, 4));
+    *sum = _mm_cvtsi128_si32(v_sum);
+    v_sse = _mm_add_epi32(v_sse, _mm_srli_si128(v_sse, 8));
+    v_sse = _mm_add_epi32(v_sse, _mm_srli_si128(v_sse, 4));
+    return (uint64_t)(uint32_t)_mm_cvtsi128_si32(v_sse);
+  }
+
+  // Max per-lane sum across 32x32 is 64 * 255 = 16320 <= INT16_MAX.
+  __m256i v_sum16;
+  __m256i v_sse;
+  if (width == 8) {
+    __m256i r01 = sub_8x2_avx2(src, sstride, dst, dstride, diff, fstride);
+    v_sum16 = r01;
+    v_sse = _mm256_madd_epi16(r01, r01);
+    h -= 2;
+    do {
+      src += 2 * sstride;
+      dst += 2 * dstride;
+      diff += 2 * fstride;
+      r01 = sub_8x2_avx2(src, sstride, dst, dstride, diff, fstride);
+      v_sum16 = _mm256_add_epi16(v_sum16, r01);
+      v_sse = _mm256_add_epi32(v_sse, _mm256_madd_epi16(r01, r01));
+      h -= 2;
+    } while (h != 0);
+  } else if (width == 16) {
+    sub_16x2_avx2(src, sstride, dst, dstride, diff, fstride, &v_sum16, &v_sse);
+    h -= 2;
+    do {
+      src += 2 * sstride;
+      dst += 2 * dstride;
+      diff += 2 * fstride;
+      __m256i s01, sq01;
+      sub_16x2_avx2(src, sstride, dst, dstride, diff, fstride, &s01, &sq01);
+      v_sum16 = _mm256_add_epi16(v_sum16, s01);
+      v_sse = _mm256_add_epi32(v_sse, sq01);
+      h -= 2;
+    } while (h != 0);
+  } else {
+    assert(width == 32);
+    sub_32x1_avx2(src, dst, diff, &v_sum16, &v_sse);
+    --h;
+    do {
+      src += sstride;
+      dst += dstride;
+      diff += fstride;
+      __m256i s01, sq01;
+      sub_32x1_avx2(src, dst, diff, &s01, &sq01);
+      v_sum16 = _mm256_add_epi16(v_sum16, s01);
+      v_sse = _mm256_add_epi32(v_sse, sq01);
+      --h;
+    } while (h != 0);
+  }
+  const __m256i v_sum = _mm256_madd_epi16(v_sum16, _mm256_set1_epi16(1));
+  __m128i sum128 = _mm_add_epi32(_mm256_castsi256_si128(v_sum),
+                                 _mm256_extracti128_si256(v_sum, 1));
+  __m128i sse128 = _mm_add_epi32(_mm256_castsi256_si128(v_sse),
+                                 _mm256_extracti128_si256(v_sse, 1));
+  sum128 = _mm_add_epi32(sum128, _mm_srli_si128(sum128, 8));
+  sum128 = _mm_add_epi32(sum128, _mm_srli_si128(sum128, 4));
+  *sum = _mm_cvtsi128_si32(sum128);
+  sse128 = _mm_add_epi32(sse128, _mm_srli_si128(sse128, 8));
+  sse128 = _mm_add_epi32(sse128, _mm_srli_si128(sse128, 4));
+  return (uint64_t)(uint32_t)_mm_cvtsi128_si32(sse128);
+}
diff --git a/av1/encoder/x86/tx_search_sse2.c b/av1/encoder/x86/tx_search_sse2.c
new file mode 100644
index 0000000000..a961d15fd8
--- /dev/null
+++ b/av1/encoder/x86/tx_search_sse2.c
@@ -0,0 +1,171 @@
+/*
+ * Copyright (c) 2026, Alliance for Open Media. All rights reserved.
+ *
+ * This source code is subject to the terms of the BSD 2 Clause License and
+ * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
+ * was not distributed with this source code in the LICENSE file, you can
+ * obtain it at www.aomedia.org/license/software. If the Alliance for Open
+ * 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 <emmintrin.h>
+#include <stddef.h>
+#include <stdint.h>
+
+#include "config/av1_rtcd.h"
+
+#include "aom_dsp/x86/synonyms.h"
+
+static inline __m128i sub_4x2_sse2(const uint8_t *src, ptrdiff_t sstride,
+                                   const uint8_t *dst, ptrdiff_t dstride,
+                                   int16_t *diff, ptrdiff_t fstride,
+                                   __m128i zero) {
+  const __m128i s0 = xx_loadl_32(src);
+  const __m128i s1 = xx_loadl_32(src + sstride);
+  const __m128i d0 = xx_loadl_32(dst);
+  const __m128i d1 = xx_loadl_32(dst + dstride);
+  const __m128i s01 = _mm_unpacklo_epi8(_mm_unpacklo_epi32(s0, s1), zero);
+  const __m128i d01 = _mm_unpacklo_epi8(_mm_unpacklo_epi32(d0, d1), zero);
+  const __m128i r01 = _mm_sub_epi16(s01, d01);
+  _mm_storel_epi64((__m128i *)diff, r01);
+  _mm_storel_epi64((__m128i *)(diff + fstride), _mm_srli_si128(r01, 8));
+  return r01;
+}
+
+static inline void sub_8x2_sse2(const uint8_t *src, ptrdiff_t sstride,
+                                const uint8_t *dst, ptrdiff_t dstride,
+                                int16_t *diff, ptrdiff_t fstride, __m128i zero,
+                                __m128i *sum16, __m128i *sse) {
+  const __m128i s0 = _mm_unpacklo_epi8(xx_loadl_64(src), zero);
+  const __m128i d0 = _mm_unpacklo_epi8(xx_loadl_64(dst), zero);
+  const __m128i s1 = _mm_unpacklo_epi8(xx_loadl_64(src + sstride), zero);
+  const __m128i d1 = _mm_unpacklo_epi8(xx_loadl_64(dst + dstride), zero);
+  const __m128i r0 = _mm_sub_epi16(s0, d0);
+  const __m128i r1 = _mm_sub_epi16(s1, d1);
+  _mm_storeu_si128((__m128i *)diff, r0);
+  _mm_storeu_si128((__m128i *)(diff + fstride), r1);
+  *sum16 = _mm_add_epi16(r0, r1);
+  *sse = _mm_add_epi32(_mm_madd_epi16(r0, r0), _mm_madd_epi16(r1, r1));
+}
+
+static inline void sub_16x1_sse2(const uint8_t *src, const uint8_t *dst,
+                                 int16_t *diff, __m128i zero, __m128i *sum16,
+                                 __m128i *sse) {
+  const __m128i s = _mm_loadu_si128((const __m128i *)src);
+  const __m128i d = _mm_loadu_si128((const __m128i *)dst);
+  const __m128i r0 =
+      _mm_sub_epi16(_mm_unpacklo_epi8(s, zero), _mm_unpacklo_epi8(d, zero));
+  const __m128i r1 =
+      _mm_sub_epi16(_mm_unpackhi_epi8(s, zero), _mm_unpackhi_epi8(d, zero));
+  _mm_storeu_si128((__m128i *)diff, r0);
+  _mm_storeu_si128((__m128i *)(diff + 8), r1);
+  *sum16 = _mm_add_epi16(r0, r1);
+  *sse = _mm_add_epi32(_mm_madd_epi16(r0, r0), _mm_madd_epi16(r1, r1));
+}
+
+static inline void sub_32x1_sse2(const uint8_t *src, const uint8_t *dst,
+                                 int16_t *diff, __m128i zero, __m128i *sum16,
+                                 __m128i *sse) {
+  __m128i sum0, sse0, sum1, sse1;
+  sub_16x1_sse2(src, dst, diff, zero, &sum0, &sse0);
+  sub_16x1_sse2(src + 16, dst + 16, diff + 16, zero, &sum1, &sse1);
+  *sum16 = _mm_add_epi16(sum0, sum1);
+  *sse = _mm_add_epi32(sse0, sse1);
+}
+
+uint64_t av1_sub_sum_sse_lbd_sse2(const uint8_t *src, int src_stride,
+                                  const uint8_t *dst, int dst_stride,
+                                  int16_t *diff, int diff_stride, int width,
+                                  int height, int *sum) {
+  const ptrdiff_t sstride = src_stride;
+  const ptrdiff_t dstride = dst_stride;
+  const ptrdiff_t fstride = diff_stride;
+  assert(width == 4 || width == 8 || width == 16 || width == 32);
+  assert(height == 4 || height == 8 || height == 16 || height == 32);
+  assert(diff_stride >= width);
+  assert(sum != NULL);
+
+  const __m128i zero = _mm_setzero_si128();
+  int h = height;
+  __m128i v_sum16;
+  __m128i v_sse;
+  if (width == 4) {
+    // Max 32 pixels per lane * 255 = 8160 <= INT16_MAX (actual 4x32 has 16
+    // pixels per lane -> 4080).
+    __m128i r01 = sub_4x2_sse2(src, sstride, dst, dstride, diff, fstride, zero);
+    v_sum16 = r01;
+    v_sse = _mm_madd_epi16(r01, r01);
+    h -= 2;
+    do {
+      src += 2 * sstride;
+      dst += 2 * dstride;
+      diff += 2 * fstride;
+      r01 = sub_4x2_sse2(src, sstride, dst, dstride, diff, fstride, zero);
+      v_sum16 = _mm_add_epi16(v_sum16, r01);
+      v_sse = _mm_add_epi32(v_sse, _mm_madd_epi16(r01, r01));
+      h -= 2;
+    } while (h != 0);
+  } else if (width == 8) {
+    // Max 32 pixels per lane * 255 = 8160 <= INT16_MAX.
+    sub_8x2_sse2(src, sstride, dst, dstride, diff, fstride, zero, &v_sum16,
+                 &v_sse);
+    h -= 2;
+    do {
+      src += 2 * sstride;
+      dst += 2 * dstride;
+      diff += 2 * fstride;
+      __m128i s01, sq01;
+      sub_8x2_sse2(src, sstride, dst, dstride, diff, fstride, zero, &s01,
+                   &sq01);
+      v_sum16 = _mm_add_epi16(v_sum16, s01);
+      v_sse = _mm_add_epi32(v_sse, sq01);
+      h -= 2;
+    } while (h != 0);
+  } else if (width == 16) {
+    // Max 64 pixels per lane * 255 = 16320 <= INT16_MAX.
+    __m128i s0, sq0, s1, sq1;
+    sub_16x1_sse2(src, dst, diff, zero, &s0, &sq0);
+    sub_16x1_sse2(src + sstride, dst + dstride, diff + fstride, zero, &s1,
+                  &sq1);
+    v_sum16 = _mm_add_epi16(s0, s1);
+    v_sse = _mm_add_epi32(sq0, sq1);
+    h -= 2;
+    do {
+      src += 2 * sstride;
+      dst += 2 * dstride;
+      diff += 2 * fstride;
+      sub_16x1_sse2(src, dst, diff, zero, &s0, &sq0);
+      sub_16x1_sse2(src + sstride, dst + dstride, diff + fstride, zero, &s1,
+                    &sq1);
+      v_sum16 = _mm_add_epi16(v_sum16, _mm_add_epi16(s0, s1));
+      v_sse = _mm_add_epi32(v_sse, _mm_add_epi32(sq0, sq1));
+      h -= 2;
+    } while (h != 0);
+  } else {
+    assert(width == 32);
+    // Max 128 pixels per lane (32x32) * 255 = 32640 <= INT16_MAX.
+    // Max total SSE = 1024 * 255^2 = 66585600 <= INT32_MAX.
+    sub_32x1_sse2(src, dst, diff, zero, &v_sum16, &v_sse);
+    --h;
+    do {
+      src += sstride;
+      dst += dstride;
+      diff += fstride;
+      __m128i s01, sq01;
+      sub_32x1_sse2(src, dst, diff, zero, &s01, &sq01);
+      v_sum16 = _mm_add_epi16(v_sum16, s01);
+      v_sse = _mm_add_epi32(v_sse, sq01);
+      --h;
+    } while (h != 0);
+  }
+
+  __m128i v_sum = _mm_madd_epi16(v_sum16, _mm_set1_epi16(1));
+  v_sum = _mm_add_epi32(v_sum, _mm_srli_si128(v_sum, 8));
+  v_sum = _mm_add_epi32(v_sum, _mm_srli_si128(v_sum, 4));
+  *sum = _mm_cvtsi128_si32(v_sum);
+  v_sse = _mm_add_epi32(v_sse, _mm_srli_si128(v_sse, 8));
+  v_sse = _mm_add_epi32(v_sse, _mm_srli_si128(v_sse, 4));
+  return (uint64_t)(uint32_t)_mm_cvtsi128_si32(v_sse);
+}
diff --git a/test/subtract_test.cc b/test/subtract_test.cc
index 18f05bd5cd..e351a09202 100644
--- a/test/subtract_test.cc
+++ b/test/subtract_test.cc
@@ -9,13 +9,18 @@
  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
  */

+#include <algorithm>
 #include <cstdint>
+#include <memory>
 #include <tuple>
+#include <utility>
+#include <vector>

 #include "gtest/gtest.h"

 #include "config/aom_config.h"
 #include "config/aom_dsp_rtcd.h"
+#include "config/av1_rtcd.h"

 #include "test/acm_random.h"
 #include "test/register_state_check.h"
@@ -366,4 +371,314 @@ INSTANTIATE_TEST_SUITE_P(
                        ::testing::Values(&aom_highbd_subtract_block_c)));
 #endif
 #endif  // CONFIG_AV1_HIGHBITDEPTH
+
+#if CONFIG_AV1_ENCODER
+using SubSumSseLbdFunc = uint64_t (*)(const uint8_t *src, int src_stride,
+                                      const uint8_t *dst, int dst_stride,
+                                      int16_t *diff, int diff_stride, int width,
+                                      int height, int *sum);
+
+class AV1SubSumSseLbdTest : public ::testing::TestWithParam<SubSumSseLbdFunc> {
+};
+
+TEST_P(AV1SubSumSseLbdTest, MatchReference) {
+  const SubSumSseLbdFunc func = GetParam();
+  ACMRandom rnd(ACMRandom::DeterministicSeed());
+  static constexpr int kDims[4] = { 4, 8, 16, 32 };
+  static constexpr int16_t kCanary = static_cast<int16_t>(0x5a5a);
+  std::vector<uint8_t> src_buf(128 * 40);
+  std::vector<uint8_t> dst_buf(128 * 40);
+  std::vector<int16_t> diff_buf(128 * 40);
+
+  for (int wi = 0; wi < 4; ++wi) {
+    const int bsw = kDims[wi];
+    for (int hi = 0; hi < 4; ++hi) {
+      const int bsh = kDims[hi];
+      const int sstrides[4] = { bsw, bsw + 1, 67, 99 };
+      const int dstrides[4] = { bsw, bsw + 3, 73, 64 };
+      const int fstrides[4] = { bsw, bsw + 5, bsw + 16, 71 };
+
+      for (int si = 0; si < 4; ++si) {
+        const int sstride = sstrides[si];
+        const int dstride = dstrides[si];
+        const int fstride = fstrides[si];
+        static constexpr size_t kGuard = 37;
+        uint8_t *const src = src_buf.data() + ((si & 1) ? 3 : 16);
+        uint8_t *const dst = dst_buf.data() + ((si & 1) ? 7 : 16);
+        int16_t *const diff = diff_buf.data() + kGuard;
+
+        for (int pat = 0; pat < 10; ++pat) {
+          for (size_t i = 0; i < src_buf.size(); ++i) {
+            src_buf[i] = rnd.Rand8();
+            dst_buf[i] = rnd.Rand8();
+          }
+          std::fill(diff_buf.begin(), diff_buf.end(), kCanary);
+
+          int32_t ref_sum = 0;
+          uint64_t ref_sse = 0;
+          for (int r = 0; r < bsh; ++r) {
+            for (int c = 0; c < bsw; ++c) {
+              uint8_t sv = 0;
+              uint8_t dv = 0;
+              if (pat == 0) {
+                sv = 0;
+                dv = 255;
+              } else if (pat == 1) {
+                sv = 255;
+                dv = 0;
+              } else if (pat == 2) {
+                sv = 128;
+                dv = 128;
+              } else if (pat == 3) {
+                sv = ((r + c) & 1) ? 255 : 0;
+                dv = ((r + c) & 1) ? 0 : 255;
+              } else if (pat == 4) {
+                sv = (r & 1) ? 255 : 0;
+                dv = (c & 1) ? 255 : 0;
+              } else if (pat < 7) {
+                sv = rnd.Rand8Extremes();
+                dv = rnd.Rand8Extremes();
+              } else {
+                sv = rnd.Rand8();
+                dv = rnd.Rand8();
+              }
+              src[r * sstride + c] = sv;
+              dst[r * dstride + c] = dv;
+              const int32_t d = static_cast<int32_t>(sv) - dv;
+              ref_sum += d;
+              ref_sse += static_cast<uint64_t>(d * d);
+            }
+          }
+
+          int got_sum = 12345;
+          uint64_t got_sse = 0;
+          API_REGISTER_STATE_CHECK(got_sse =
+                                       func(src, sstride, dst, dstride, diff,
+                                            fstride, bsw, bsh, &got_sum));
+          ASSERT_EQ(ref_sum, got_sum)
+              << bsw << "x" << bsh << " si=" << si << " pat=" << pat;
+          ASSERT_EQ(ref_sse, got_sse)
+              << bsw << "x" << bsh << " si=" << si << " pat=" << pat;
+
+          for (size_t i = 0; i < kGuard; ++i) {
+            ASSERT_EQ(kCanary, diff_buf[i]);
+          }
+          for (int r = 0; r < bsh; ++r) {
+            for (int c = 0; c < fstride; ++c) {
+              const int16_t got_d = diff[r * fstride + c];
+              if (c < bsw) {
+                const int16_t exp_d =
+                    static_cast<int16_t>(src[r * sstride + c]) -
+                    static_cast<int16_t>(dst[r * dstride + c]);
+                ASSERT_EQ(exp_d, got_d)
+                    << bsw << "x" << bsh << " r=" << r << " c=" << c;
+              } else {
+                ASSERT_EQ(kCanary, got_d)
+                    << "clobbered stride padding " << bsw << "x" << bsh
+                    << " r=" << r << " c=" << c;
+              }
+            }
+          }
+          for (size_t i = kGuard + static_cast<size_t>(bsh) * fstride;
+               i < diff_buf.size(); ++i) {
+            ASSERT_EQ(kCanary, diff_buf[i]);
+          }
+        }
+      }
+    }
+  }
+}
+
+TEST_P(AV1SubSumSseLbdTest, TightBufferExactBounds) {
+  const SubSumSseLbdFunc func = GetParam();
+  ACMRandom rnd(ACMRandom::DeterministicSeed());
+  static constexpr int kDims[4] = { 4, 8, 16, 32 };
+  for (int bsw : kDims) {
+    for (int bsh : kDims) {
+      const int sstride = bsw + 7;
+      const int dstride = bsw + 5;
+      const int fstride = bsw + 3;
+      // Exact minimum allocations (no trailing slack past the final row) so
+      // ASan catches any out-of-bounds read or write.
+      std::vector<uint8_t> src(static_cast<size_t>((bsh - 1) * sstride + bsw));
+      std::vector<uint8_t> dst(static_cast<size_t>((bsh - 1) * dstride + bsw));
+      std::vector<int16_t> diff(static_cast<size_t>((bsh - 1) * fstride + bsw));
+      std::vector<int16_t> ref_diff(diff.size());
+      for (auto &v : src) v = rnd.Rand8();
+      for (auto &v : dst) v = rnd.Rand8();
+      int ref_sum = -1;
+      int got_sum = 1;
+      const uint64_t ref_sse =
+          av1_sub_sum_sse_lbd_c(src.data(), sstride, dst.data(), dstride,
+                                ref_diff.data(), fstride, bsw, bsh, &ref_sum);
+      uint64_t got_sse = 0;
+      API_REGISTER_STATE_CHECK(got_sse = func(src.data(), sstride, dst.data(),
+                                              dstride, diff.data(), fstride,
+                                              bsw, bsh, &got_sum));
+      ASSERT_EQ(ref_sse, got_sse) << bsw << "x" << bsh;
+      ASSERT_EQ(ref_sum, got_sum) << bsw << "x" << bsh;
+      for (int r = 0; r < bsh; ++r) {
+        for (int c = 0; c < bsw; ++c) {
+          ASSERT_EQ(ref_diff[r * fstride + c], diff[r * fstride + c]);
+        }
+      }
+    }
+  }
+}
+
+TEST_P(AV1SubSumSseLbdTest, DISABLED_Speed) {
+  const SubSumSseLbdFunc func = GetParam();
+  if (func == &av1_sub_sum_sse_lbd_c) GTEST_SKIP();
+  ACMRandom rnd(ACMRandom::DeterministicSeed());
+  static constexpr int kDims[4] = { 4, 8, 16, 32 };
+  static constexpr int kStride = 384;
+  static constexpr int kRuns = 200000;
+  static constexpr size_t kBufElems = 32 * kStride;
+  std::unique_ptr<uint8_t, decltype(&aom_free)> src(
+      reinterpret_cast<uint8_t *>(aom_memalign(32, kBufElems)), &aom_free);
+  std::unique_ptr<uint8_t, decltype(&aom_free)> dst(
+      reinterpret_cast<uint8_t *>(aom_memalign(32, kBufElems)), &aom_free);
+  std::unique_ptr<int16_t, decltype(&aom_free)> diff(
+      reinterpret_cast<int16_t *>(
+          aom_memalign(32, kBufElems * sizeof(int16_t))),
+      &aom_free);
+  ASSERT_NE(src.get(), nullptr);
+  ASSERT_NE(dst.get(), nullptr);
+  ASSERT_NE(diff.get(), nullptr);
+  for (size_t i = 0; i < kBufElems; ++i) {
+    src.get()[i] = rnd.Rand8();
+    dst.get()[i] = rnd.Rand8();
+  }
+  using SumSseFunc = uint64_t (*)(const int16_t *src, int src_stride, int width,
+                                  int height, int *sum);
+  SubtractFunc base_sub = aom_subtract_block;
+  SumSseFunc base_sum_sse = aom_sum_sse_2d_i16;
+#if HAVE_SSE2
+  if (func == &av1_sub_sum_sse_lbd_sse2) {
+#if HAVE_X86_ASM
+    base_sub = &aom_subtract_block_sse2;
+#else
+    base_sub = &aom_subtract_block_c;
+#endif
+    base_sum_sse = &aom_sum_sse_2d_i16_sse2;
+  }
+#endif  // HAVE_SSE2
+
+  volatile uint64_t sink = 0;
+  for (int bsw : kDims) {
+    for (int bsh : kDims) {
+      uint64_t acc = 0;
+      aom_usec_timer timer;
+      aom_usec_timer_start(&timer);
+      for (int i = 0; i < kRuns; ++i) {
+        int sum = 0;
+        acc += av1_sub_sum_sse_lbd_c(src.get(), kStride, dst.get(), kStride,
+                                     diff.get(), kStride, bsw, bsh, &sum);
+        acc += static_cast<uint32_t>(sum);
+      }
+      aom_usec_timer_mark(&timer);
+      const double c_us = static_cast<double>(aom_usec_timer_elapsed(&timer));
+
+      aom_usec_timer_start(&timer);
+      for (int i = 0; i < kRuns; ++i) {
+        int sum = 0;
+        base_sub(bsh, bsw, diff.get(), kStride, src.get(), kStride, dst.get(),
+                 kStride);
+        acc += base_sum_sse(diff.get(), kStride, bsw, bsh, &sum);
+        acc += static_cast<uint32_t>(sum);
+      }
+      aom_usec_timer_mark(&timer);
+      const double base_us =
+          static_cast<double>(aom_usec_timer_elapsed(&timer));
+
+      aom_usec_timer_start(&timer);
+      for (int i = 0; i < kRuns; ++i) {
+        int sum = 0;
+        acc += func(src.get(), kStride, dst.get(), kStride, diff.get(), kStride,
+                    bsw, bsh, &sum);
+        acc += static_cast<uint32_t>(sum);
+      }
+      aom_usec_timer_mark(&timer);
+      const double opt_us = static_cast<double>(aom_usec_timer_elapsed(&timer));
+      sink = acc;
+      printf(
+          "SubSumSseLbd %2dx%-2d: C=%8.1f us base=%8.1f us opt=%8.1f us "
+          "(vs C: %.2fx, vs base: %.2fx)\n",
+          bsw, bsh, c_us, base_us, opt_us, c_us / opt_us, base_us / opt_us);
+    }
+  }
+  static constexpr size_t kScheduleLen = 4096;
+  static constexpr int kMixedRepeats = 50;
+  std::vector<std::pair<int, int>> schedule;
+  schedule.reserve(kScheduleLen);
+  for (size_t i = 0; i < kScheduleLen; ++i) {
+    schedule.emplace_back(kDims[i & 3], kDims[(i >> 2) & 3]);
+  }
+  for (size_t i = kScheduleLen - 1; i > 0; --i) {
+    const size_t j = static_cast<size_t>(rnd.Rand16()) % (i + 1);
+    std::swap(schedule[i], schedule[j]);
+  }
+  {
+    uint64_t acc = 0;
+    aom_usec_timer timer;
+    aom_usec_timer_start(&timer);
+    for (int rep = 0; rep < kMixedRepeats; ++rep) {
+      for (const auto &dim : schedule) {
+        int sum = 0;
+        acc += av1_sub_sum_sse_lbd_c(src.get(), kStride, dst.get(), kStride,
+                                     diff.get(), kStride, dim.first, dim.second,
+                                     &sum);
+        acc += static_cast<uint32_t>(sum);
+      }
+    }
+    aom_usec_timer_mark(&timer);
+    const double c_us = static_cast<double>(aom_usec_timer_elapsed(&timer));
+
+    aom_usec_timer_start(&timer);
+    for (int rep = 0; rep < kMixedRepeats; ++rep) {
+      for (const auto &dim : schedule) {
+        int sum = 0;
+        base_sub(dim.second, dim.first, diff.get(), kStride, src.get(), kStride,
+                 dst.get(), kStride);
+        acc += base_sum_sse(diff.get(), kStride, dim.first, dim.second, &sum);
+        acc += static_cast<uint32_t>(sum);
+      }
+    }
+    aom_usec_timer_mark(&timer);
+    const double base_us = static_cast<double>(aom_usec_timer_elapsed(&timer));
+
+    aom_usec_timer_start(&timer);
+    for (int rep = 0; rep < kMixedRepeats; ++rep) {
+      for (const auto &dim : schedule) {
+        int sum = 0;
+        acc += func(src.get(), kStride, dst.get(), kStride, diff.get(), kStride,
+                    dim.first, dim.second, &sum);
+        acc += static_cast<uint32_t>(sum);
+      }
+    }
+    aom_usec_timer_mark(&timer);
+    const double opt_us = static_cast<double>(aom_usec_timer_elapsed(&timer));
+    sink = acc;
+    printf(
+        "SubSumSseLbd mixed(%zu x %d): C=%8.1f us base=%8.1f us opt=%8.1f us "
+        "(vs C: %.2fx, vs base: %.2fx)\n",
+        kScheduleLen, kMixedRepeats, c_us, base_us, opt_us, c_us / opt_us,
+        base_us / opt_us);
+  }
+  (void)sink;
+}
+
+INSTANTIATE_TEST_SUITE_P(C, AV1SubSumSseLbdTest,
+                         ::testing::Values(&av1_sub_sum_sse_lbd_c));
+
+#if HAVE_SSE2
+INSTANTIATE_TEST_SUITE_P(SSE2, AV1SubSumSseLbdTest,
+                         ::testing::Values(&av1_sub_sum_sse_lbd_sse2));
+#endif  // HAVE_SSE2
+
+#if HAVE_AVX2
+INSTANTIATE_TEST_SUITE_P(AVX2, AV1SubSumSseLbdTest,
+                         ::testing::Values(&av1_sub_sum_sse_lbd_avx2));
+#endif  // HAVE_AVX2
+#endif  // CONFIG_AV1_ENCODER
 }  // namespace