Commit 2fc7fb24e2 for aom

commit 2fc7fb24e29305823545a9afb8780d307ae8018c
Author: Frank Galligan <fgalligan@google.com>
Date:   Tue Nov 10 19:41:49 2026 -0800

    cdef,avx2: optimize streaming dir & copy_rect8

    Optimize AVX2 CDEF direction search and 8-to-16 copy:
    - Streamline cdef_find_dir_dual_avx2() with split Pass 1/Pass 2 8x8
      transpose, streaming pair accumulators (load_dir_line_avx2,
      dir_accum_{01,23,45,67}_avx2, pointer-arg finish_dir_costs_avx2),
      _mm256_shuffle_epi32 max reduction, single 256-bit tie-break
      movemask, and skipped vpsraw shift when coeff_shift == 0.
    - Rewrite cdef_copy_rect8_8bit_to_16bit_avx2() with a 64-wide main
      loop, 2-row pointer-advancing strips, and xx_loadl_32 / xx_storel_32
      / loadu_int16 tails (removing unaligned int32 pointer casts).
    - Extend test/cdef_test.cc coverage for 8x8 block pairs at
      CDEF_BSTRIDE (with unaligned/tie-pattern blocks) and unaligned
      src/dst pointers across tail widths (including 128).

    Speedup (AMD EPYC 7B13, bit-exact):
    - CDEFFindDirDualSpeedTest.DISABLED_TestSpeed AVX2 vs C:
      GCC 15: 3.60x -> 3.99x (-9.1% AVX2 time); Clang 21: 5.46x -> 5.33x
      (SSE4.1 control: 4.23x -> 4.11x).
    - vs 433c8742ce (Clang 21, 3-rep median, --cpu-used=6): -1.62% (8T)
      720p allintra; prior 5381b9a7a6: -0.84% (1T) / -1.15% (8T) avifenc,
      -0.61% (1T) / -0.41% (8T) 720p allintra.

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

diff --git a/av1/common/x86/cdef_block_avx2.c b/av1/common/x86/cdef_block_avx2.c
index 4a8d249a3b..4ce4841633 100644
--- a/av1/common/x86/cdef_block_avx2.c
+++ b/av1/common/x86/cdef_block_avx2.c
@@ -9,7 +9,16 @@
  * 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 "aom_dsp/aom_simd.h"
+#include "aom_dsp/x86/mem_sse2.h"
+#include "aom_dsp/x86/synonyms.h"
+#include "aom_ports/bitops.h"
+#include "aom_ports/mem.h"
 #define SIMD_FUNC(name) name##_avx2
 #include "av1/common/cdef_block_simd.h"

@@ -63,294 +72,368 @@ static inline __m256i hsum4_avx2(__m256i *x0, __m256i *x1, __m256i *x2,
                           _mm256_add_epi32(*x2, *x3));
 }

-/* Computes cost for directions 0, 5, 6 and 7. We can call this function again
-to compute the remaining directions. */
-static inline __m256i compute_directions_avx2(__m256i *lines,
-                                              int32_t cost_frist_8x8[4],
-                                              int32_t cost_second_8x8[4]) {
-  __m256i partial4a, partial4b, partial5a, partial5b, partial7a, partial7b;
-  __m256i partial6;
-  __m256i tmp;
-  /* Partial sums for lines 0 and 1. */
-  partial4a = _mm256_slli_si256(lines[0], 14);
-  partial4b = _mm256_srli_si256(lines[0], 2);
-  partial4a = _mm256_add_epi16(partial4a, _mm256_slli_si256(lines[1], 12));
-  partial4b = _mm256_add_epi16(partial4b, _mm256_srli_si256(lines[1], 4));
-  tmp = _mm256_add_epi16(lines[0], lines[1]);
-  partial5a = _mm256_slli_si256(tmp, 10);
-  partial5b = _mm256_srli_si256(tmp, 6);
-  partial7a = _mm256_slli_si256(tmp, 4);
-  partial7b = _mm256_srli_si256(tmp, 12);
-  partial6 = tmp;
-
-  /* Partial sums for lines 2 and 3. */
-  partial4a = _mm256_add_epi16(partial4a, _mm256_slli_si256(lines[2], 10));
-  partial4b = _mm256_add_epi16(partial4b, _mm256_srli_si256(lines[2], 6));
-  partial4a = _mm256_add_epi16(partial4a, _mm256_slli_si256(lines[3], 8));
-  partial4b = _mm256_add_epi16(partial4b, _mm256_srli_si256(lines[3], 8));
-  tmp = _mm256_add_epi16(lines[2], lines[3]);
-  partial5a = _mm256_add_epi16(partial5a, _mm256_slli_si256(tmp, 8));
-  partial5b = _mm256_add_epi16(partial5b, _mm256_srli_si256(tmp, 8));
-  partial7a = _mm256_add_epi16(partial7a, _mm256_slli_si256(tmp, 6));
-  partial7b = _mm256_add_epi16(partial7b, _mm256_srli_si256(tmp, 10));
-  partial6 = _mm256_add_epi16(partial6, tmp);
-
-  /* Partial sums for lines 4 and 5. */
-  partial4a = _mm256_add_epi16(partial4a, _mm256_slli_si256(lines[4], 6));
-  partial4b = _mm256_add_epi16(partial4b, _mm256_srli_si256(lines[4], 10));
-  partial4a = _mm256_add_epi16(partial4a, _mm256_slli_si256(lines[5], 4));
-  partial4b = _mm256_add_epi16(partial4b, _mm256_srli_si256(lines[5], 12));
-  tmp = _mm256_add_epi16(lines[4], lines[5]);
-  partial5a = _mm256_add_epi16(partial5a, _mm256_slli_si256(tmp, 6));
-  partial5b = _mm256_add_epi16(partial5b, _mm256_srli_si256(tmp, 10));
-  partial7a = _mm256_add_epi16(partial7a, _mm256_slli_si256(tmp, 8));
-  partial7b = _mm256_add_epi16(partial7b, _mm256_srli_si256(tmp, 8));
-  partial6 = _mm256_add_epi16(partial6, tmp);
-
-  /* Partial sums for lines 6 and 7. */
-  partial4a = _mm256_add_epi16(partial4a, _mm256_slli_si256(lines[6], 2));
-  partial4b = _mm256_add_epi16(partial4b, _mm256_srli_si256(lines[6], 14));
-  partial4a = _mm256_add_epi16(partial4a, lines[7]);
-  tmp = _mm256_add_epi16(lines[6], lines[7]);
-  partial5a = _mm256_add_epi16(partial5a, _mm256_slli_si256(tmp, 4));
-  partial5b = _mm256_add_epi16(partial5b, _mm256_srli_si256(tmp, 12));
-  partial7a = _mm256_add_epi16(partial7a, _mm256_slli_si256(tmp, 10));
-  partial7b = _mm256_add_epi16(partial7b, _mm256_srli_si256(tmp, 6));
-  partial6 = _mm256_add_epi16(partial6, tmp);
-
+// Finalizes partial accumulators into 4 packed direction costs per 128-bit
+// lane.
+static inline __m256i finish_dir_costs_avx2(
+    __m256i *partial4a, __m256i *partial4b, __m256i *partial5a,
+    __m256i *partial5b, __m256i *partial6, __m256i *partial7a,
+    __m256i *partial7b) {
   const __m256i const_reg_1 =
       _mm256_set_epi32(210, 280, 420, 840, 210, 280, 420, 840);
   const __m256i const_reg_2 =
       _mm256_set_epi32(105, 120, 140, 168, 105, 120, 140, 168);
+  *partial4a =
+      fold_mul_and_sum_avx2(partial4a, partial4b, &const_reg_1, &const_reg_2);
   const __m256i const_reg_3 = _mm256_set_epi32(210, 420, 0, 0, 210, 420, 0, 0);
   const __m256i const_reg_4 =
       _mm256_set_epi32(105, 105, 105, 140, 105, 105, 105, 140);
+  *partial7a =
+      fold_mul_and_sum_avx2(partial7a, partial7b, &const_reg_3, &const_reg_4);
+  *partial5a =
+      fold_mul_and_sum_avx2(partial5a, partial5b, &const_reg_3, &const_reg_4);
+  *partial6 = _mm256_madd_epi16(*partial6, *partial6);
+  *partial6 = _mm256_mullo_epi32(*partial6, _mm256_set1_epi32(105));
+  return hsum4_avx2(partial4a, partial5a, partial6, partial7a);
+}
+
+static inline __m256i load_dir_line_avx2(const uint16_t *img1,
+                                         const uint16_t *img2, int idx,
+                                         int stride, int coeff_shift,
+                                         __m128i shift_reg, __m256i c128) {
+  const ptrdiff_t off = (ptrdiff_t)idx * stride;
+  const __m128i s1 = _mm_loadu_si128((const __m128i *)(img1 + off));
+  const __m128i s2 = _mm_loadu_si128((const __m128i *)(img2 + off));
+  __m256i v = _mm256_inserti128_si256(_mm256_castsi128_si256(s1), s2, 1);
+  if (coeff_shift != 0) {
+    v = _mm256_sra_epi16(v, shift_reg);
+  }
+  return _mm256_sub_epi16(v, c128);
+}
+
+static inline void dir_accum_01_avx2(__m256i l0, __m256i l1, __m256i *p4a,
+                                     __m256i *p4b, __m256i *p5a, __m256i *p5b,
+                                     __m256i *p6, __m256i *p7a, __m256i *p7b) {
+  *p4a = _mm256_add_epi16(_mm256_slli_si256(l0, 14), _mm256_slli_si256(l1, 12));
+  *p4b = _mm256_add_epi16(_mm256_srli_si256(l0, 2), _mm256_srli_si256(l1, 4));
+  const __m256i tmp = _mm256_add_epi16(l0, l1);
+  *p5a = _mm256_slli_si256(tmp, 10);
+  *p5b = _mm256_srli_si256(tmp, 6);
+  *p7a = _mm256_slli_si256(tmp, 4);
+  *p7b = _mm256_srli_si256(tmp, 12);
+  *p6 = tmp;
+}

-  /* Compute costs in terms of partial sums. */
-  partial4a =
-      fold_mul_and_sum_avx2(&partial4a, &partial4b, &const_reg_1, &const_reg_2);
-  partial7a =
-      fold_mul_and_sum_avx2(&partial7a, &partial7b, &const_reg_3, &const_reg_4);
-  partial5a =
-      fold_mul_and_sum_avx2(&partial5a, &partial5b, &const_reg_3, &const_reg_4);
-  partial6 = _mm256_madd_epi16(partial6, partial6);
-  partial6 = _mm256_mullo_epi32(partial6, _mm256_set1_epi32(105));
-
-  partial4a = hsum4_avx2(&partial4a, &partial5a, &partial6, &partial7a);
-  _mm_storeu_si128((__m128i *)cost_frist_8x8,
-                   _mm256_castsi256_si128(partial4a));
-  _mm_storeu_si128((__m128i *)cost_second_8x8,
-                   _mm256_extractf128_si256(partial4a, 1));
-
-  return partial4a;
+static inline void dir_accum_23_avx2(__m256i l2, __m256i l3, __m256i *p4a,
+                                     __m256i *p4b, __m256i *p5a, __m256i *p5b,
+                                     __m256i *p6, __m256i *p7a, __m256i *p7b) {
+  *p4a = _mm256_add_epi16(*p4a, _mm256_slli_si256(l2, 10));
+  *p4b = _mm256_add_epi16(*p4b, _mm256_srli_si256(l2, 6));
+  *p4a = _mm256_add_epi16(*p4a, _mm256_slli_si256(l3, 8));
+  *p4b = _mm256_add_epi16(*p4b, _mm256_srli_si256(l3, 8));
+  const __m256i tmp = _mm256_add_epi16(l2, l3);
+  *p5a = _mm256_add_epi16(*p5a, _mm256_slli_si256(tmp, 8));
+  *p5b = _mm256_add_epi16(*p5b, _mm256_srli_si256(tmp, 8));
+  *p7a = _mm256_add_epi16(*p7a, _mm256_slli_si256(tmp, 6));
+  *p7b = _mm256_add_epi16(*p7b, _mm256_srli_si256(tmp, 10));
+  *p6 = _mm256_add_epi16(*p6, tmp);
 }

-/* transpose and reverse the order of the lines -- equivalent to a 90-degree
-counter-clockwise rotation of the pixels. */
-static inline void array_reverse_transpose_8x8_avx2(__m256i *in, __m256i *res) {
-  const __m256i tr0_0 = _mm256_unpacklo_epi16(in[0], in[1]);
-  const __m256i tr0_1 = _mm256_unpacklo_epi16(in[2], in[3]);
-  const __m256i tr0_2 = _mm256_unpackhi_epi16(in[0], in[1]);
-  const __m256i tr0_3 = _mm256_unpackhi_epi16(in[2], in[3]);
-  const __m256i tr0_4 = _mm256_unpacklo_epi16(in[4], in[5]);
-  const __m256i tr0_5 = _mm256_unpacklo_epi16(in[6], in[7]);
-  const __m256i tr0_6 = _mm256_unpackhi_epi16(in[4], in[5]);
-  const __m256i tr0_7 = _mm256_unpackhi_epi16(in[6], in[7]);
-
-  const __m256i tr1_0 = _mm256_unpacklo_epi32(tr0_0, tr0_1);
-  const __m256i tr1_1 = _mm256_unpacklo_epi32(tr0_4, tr0_5);
-  const __m256i tr1_2 = _mm256_unpackhi_epi32(tr0_0, tr0_1);
-  const __m256i tr1_3 = _mm256_unpackhi_epi32(tr0_4, tr0_5);
-  const __m256i tr1_4 = _mm256_unpacklo_epi32(tr0_2, tr0_3);
-  const __m256i tr1_5 = _mm256_unpacklo_epi32(tr0_6, tr0_7);
-  const __m256i tr1_6 = _mm256_unpackhi_epi32(tr0_2, tr0_3);
-  const __m256i tr1_7 = _mm256_unpackhi_epi32(tr0_6, tr0_7);
-
-  res[7] = _mm256_unpacklo_epi64(tr1_0, tr1_1);
-  res[6] = _mm256_unpackhi_epi64(tr1_0, tr1_1);
-  res[5] = _mm256_unpacklo_epi64(tr1_2, tr1_3);
-  res[4] = _mm256_unpackhi_epi64(tr1_2, tr1_3);
-  res[3] = _mm256_unpacklo_epi64(tr1_4, tr1_5);
-  res[2] = _mm256_unpackhi_epi64(tr1_4, tr1_5);
-  res[1] = _mm256_unpacklo_epi64(tr1_6, tr1_7);
-  res[0] = _mm256_unpackhi_epi64(tr1_6, tr1_7);
+static inline void dir_accum_45_avx2(__m256i l4, __m256i l5, __m256i *p4a,
+                                     __m256i *p4b, __m256i *p5a, __m256i *p5b,
+                                     __m256i *p6, __m256i *p7a, __m256i *p7b) {
+  *p4a = _mm256_add_epi16(*p4a, _mm256_slli_si256(l4, 6));
+  *p4b = _mm256_add_epi16(*p4b, _mm256_srli_si256(l4, 10));
+  *p4a = _mm256_add_epi16(*p4a, _mm256_slli_si256(l5, 4));
+  *p4b = _mm256_add_epi16(*p4b, _mm256_srli_si256(l5, 12));
+  const __m256i tmp = _mm256_add_epi16(l4, l5);
+  *p5a = _mm256_add_epi16(*p5a, _mm256_slli_si256(tmp, 6));
+  *p5b = _mm256_add_epi16(*p5b, _mm256_srli_si256(tmp, 10));
+  *p7a = _mm256_add_epi16(*p7a, _mm256_slli_si256(tmp, 8));
+  *p7b = _mm256_add_epi16(*p7b, _mm256_srli_si256(tmp, 8));
+  *p6 = _mm256_add_epi16(*p6, tmp);
+}
+
+static inline void dir_accum_67_avx2(__m256i l6, __m256i l7, __m256i *p4a,
+                                     __m256i *p4b, __m256i *p5a, __m256i *p5b,
+                                     __m256i *p6, __m256i *p7a, __m256i *p7b) {
+  *p4a = _mm256_add_epi16(*p4a, _mm256_slli_si256(l6, 2));
+  *p4b = _mm256_add_epi16(*p4b, _mm256_srli_si256(l6, 14));
+  *p4a = _mm256_add_epi16(*p4a, l7);
+  const __m256i tmp = _mm256_add_epi16(l6, l7);
+  *p5a = _mm256_add_epi16(*p5a, _mm256_slli_si256(tmp, 4));
+  *p5b = _mm256_add_epi16(*p5b, _mm256_srli_si256(tmp, 12));
+  *p7a = _mm256_add_epi16(*p7a, _mm256_slli_si256(tmp, 10));
+  *p7b = _mm256_add_epi16(*p7b, _mm256_srli_si256(tmp, 6));
+  *p6 = _mm256_add_epi16(*p6, tmp);
 }

 void cdef_find_dir_dual_avx2(const uint16_t *img1, const uint16_t *img2,
                              int stride, int32_t *var_out_1st,
                              int32_t *var_out_2nd, int coeff_shift,
                              int *out_dir_1st_8x8, int *out_dir_2nd_8x8) {
-  int32_t cost_first_8x8[8];
-  int32_t cost_second_8x8[8];
-  // Used to store the best cost for 2 8x8's.
-  int32_t best_cost[2] = { 0 };
-  // Best direction for 2 8x8's.
-  int best_dir[2] = { 0 };
-
-  const __m128i const_coeff_shift_reg = _mm_cvtsi32_si128(coeff_shift);
-  const __m256i const_128_reg = _mm256_set1_epi16(128);
-  __m256i lines[8];
-  for (int i = 0; i < 8; i++) {
-    const __m128i src_1 = _mm_loadu_si128((const __m128i *)&img1[i * stride]);
-    const __m128i src_2 = _mm_loadu_si128((const __m128i *)&img2[i * stride]);
-
-    lines[i] = _mm256_insertf128_si256(_mm256_castsi128_si256(src_1), src_2, 1);
-    lines[i] = _mm256_sub_epi16(
-        _mm256_sra_epi16(lines[i], const_coeff_shift_reg), const_128_reg);
+  const __m256i c128 = _mm256_set1_epi16(128);
+  const __m128i sh = _mm_cvtsi32_si128(coeff_shift);
+  DECLARE_ALIGNED(32, uint8_t, t0_buf[8 * 32]);
+  DECLARE_ALIGNED(32, int32_t, costs[16]);
+
+  /* Pass 1 (mostly vertical): load 8 row pairs once, pre-interleave 16-bit
+     transpose stage into t0_buf (reversed 90-degree counter-clockwise order),
+     and accumulate dir47 costs sequentially. */
+  __m256i p4a, p4b, p5a, p5b, p6, p7a, p7b;
+  {
+    const __m256i a0 =
+        load_dir_line_avx2(img1, img2, 0, stride, coeff_shift, sh, c128);
+    const __m256i a1 =
+        load_dir_line_avx2(img1, img2, 1, stride, coeff_shift, sh, c128);
+    _mm256_store_si256((__m256i *)(t0_buf + 0 * 32),
+                       _mm256_unpacklo_epi16(a0, a1));
+    _mm256_store_si256((__m256i *)(t0_buf + 2 * 32),
+                       _mm256_unpackhi_epi16(a0, a1));
+    dir_accum_01_avx2(a0, a1, &p4a, &p4b, &p5a, &p5b, &p6, &p7a, &p7b);
+
+    const __m256i a2 =
+        load_dir_line_avx2(img1, img2, 2, stride, coeff_shift, sh, c128);
+    const __m256i a3 =
+        load_dir_line_avx2(img1, img2, 3, stride, coeff_shift, sh, c128);
+    _mm256_store_si256((__m256i *)(t0_buf + 1 * 32),
+                       _mm256_unpacklo_epi16(a2, a3));
+    _mm256_store_si256((__m256i *)(t0_buf + 3 * 32),
+                       _mm256_unpackhi_epi16(a2, a3));
+    dir_accum_23_avx2(a2, a3, &p4a, &p4b, &p5a, &p5b, &p6, &p7a, &p7b);
+
+    const __m256i a4 =
+        load_dir_line_avx2(img1, img2, 4, stride, coeff_shift, sh, c128);
+    const __m256i a5 =
+        load_dir_line_avx2(img1, img2, 5, stride, coeff_shift, sh, c128);
+    _mm256_store_si256((__m256i *)(t0_buf + 4 * 32),
+                       _mm256_unpacklo_epi16(a4, a5));
+    _mm256_store_si256((__m256i *)(t0_buf + 6 * 32),
+                       _mm256_unpackhi_epi16(a4, a5));
+    dir_accum_45_avx2(a4, a5, &p4a, &p4b, &p5a, &p5b, &p6, &p7a, &p7b);
+
+    const __m256i a6 =
+        load_dir_line_avx2(img1, img2, 6, stride, coeff_shift, sh, c128);
+    const __m256i a7 =
+        load_dir_line_avx2(img1, img2, 7, stride, coeff_shift, sh, c128);
+    _mm256_store_si256((__m256i *)(t0_buf + 5 * 32),
+                       _mm256_unpacklo_epi16(a6, a7));
+    _mm256_store_si256((__m256i *)(t0_buf + 7 * 32),
+                       _mm256_unpackhi_epi16(a6, a7));
+    dir_accum_67_avx2(a6, a7, &p4a, &p4b, &p5a, &p5b, &p6, &p7a, &p7b);
+  }
+  const __m256i v_dir47 =
+      finish_dir_costs_avx2(&p4a, &p4b, &p5a, &p5b, &p6, &p7a, &p7b);
+  _mm256_store_si256((__m256i *)(costs + 8), v_dir47);
+
+  /* Pass 2 (mostly horizontal): finish 32-bit/64-bit transpose in two 4-row
+     halves into dir03. */
+  {
+    const __m256i u2 = _mm256_load_si256((const __m256i *)(t0_buf + 2 * 32));
+    const __m256i u3 = _mm256_load_si256((const __m256i *)(t0_buf + 3 * 32));
+    const __m256i u6 = _mm256_load_si256((const __m256i *)(t0_buf + 6 * 32));
+    const __m256i u7 = _mm256_load_si256((const __m256i *)(t0_buf + 7 * 32));
+    const __m256i tr1_4 = _mm256_unpacklo_epi32(u2, u3);
+    const __m256i tr1_6 = _mm256_unpackhi_epi32(u2, u3);
+    const __m256i tr1_5 = _mm256_unpacklo_epi32(u6, u7);
+    const __m256i tr1_7 = _mm256_unpackhi_epi32(u6, u7);
+    const __m256i r0 = _mm256_unpackhi_epi64(tr1_6, tr1_7);
+    const __m256i r1 = _mm256_unpacklo_epi64(tr1_6, tr1_7);
+    const __m256i r2 = _mm256_unpackhi_epi64(tr1_4, tr1_5);
+    const __m256i r3 = _mm256_unpacklo_epi64(tr1_4, tr1_5);
+    dir_accum_01_avx2(r0, r1, &p4a, &p4b, &p5a, &p5b, &p6, &p7a, &p7b);
+    dir_accum_23_avx2(r2, r3, &p4a, &p4b, &p5a, &p5b, &p6, &p7a, &p7b);
+  }
+  {
+    const __m256i u0 = _mm256_load_si256((const __m256i *)(t0_buf + 0 * 32));
+    const __m256i u1 = _mm256_load_si256((const __m256i *)(t0_buf + 1 * 32));
+    const __m256i u4 = _mm256_load_si256((const __m256i *)(t0_buf + 4 * 32));
+    const __m256i u5 = _mm256_load_si256((const __m256i *)(t0_buf + 5 * 32));
+    const __m256i tr1_0 = _mm256_unpacklo_epi32(u0, u1);
+    const __m256i tr1_2 = _mm256_unpackhi_epi32(u0, u1);
+    const __m256i tr1_1 = _mm256_unpacklo_epi32(u4, u5);
+    const __m256i tr1_3 = _mm256_unpackhi_epi32(u4, u5);
+    const __m256i r4 = _mm256_unpackhi_epi64(tr1_2, tr1_3);
+    const __m256i r5 = _mm256_unpacklo_epi64(tr1_2, tr1_3);
+    const __m256i r6 = _mm256_unpackhi_epi64(tr1_0, tr1_1);
+    const __m256i r7 = _mm256_unpacklo_epi64(tr1_0, tr1_1);
+    dir_accum_45_avx2(r4, r5, &p4a, &p4b, &p5a, &p5b, &p6, &p7a, &p7b);
+    dir_accum_67_avx2(r6, r7, &p4a, &p4b, &p5a, &p5b, &p6, &p7a, &p7b);
   }
-
-  /* Compute "mostly vertical" directions. */
-  const __m256i dir47 =
-      compute_directions_avx2(lines, cost_first_8x8 + 4, cost_second_8x8 + 4);
-
-  /* Transpose and reverse the order of the lines. */
-  array_reverse_transpose_8x8_avx2(lines, lines);
-
-  /* Compute "mostly horizontal" directions. */
   const __m256i dir03 =
-      compute_directions_avx2(lines, cost_first_8x8, cost_second_8x8);
+      finish_dir_costs_avx2(&p4a, &p4b, &p5a, &p5b, &p6, &p7a, &p7b);
+  _mm256_store_si256((__m256i *)costs, dir03);

-  __m256i max = _mm256_max_epi32(dir03, dir47);
+  __m256i max = _mm256_max_epi32(dir03, v_dir47);
   max =
-      _mm256_max_epi32(max, _mm256_or_si256(_mm256_srli_si256(max, 8),
-                                            _mm256_slli_si256(max, 16 - (8))));
+      _mm256_max_epi32(max, _mm256_shuffle_epi32(max, _MM_SHUFFLE(1, 0, 3, 2)));
   max =
-      _mm256_max_epi32(max, _mm256_or_si256(_mm256_srli_si256(max, 4),
-                                            _mm256_slli_si256(max, 16 - (4))));
-
-  const __m128i first_8x8_output = _mm256_castsi256_si128(max);
-  const __m128i second_8x8_output = _mm256_extractf128_si256(max, 1);
-  const __m128i cmpeg_res_00 =
-      _mm_cmpeq_epi32(first_8x8_output, _mm256_castsi256_si128(dir47));
-  const __m128i cmpeg_res_01 =
-      _mm_cmpeq_epi32(first_8x8_output, _mm256_castsi256_si128(dir03));
-  const __m128i cmpeg_res_10 =
-      _mm_cmpeq_epi32(second_8x8_output, _mm256_extractf128_si256(dir47, 1));
-  const __m128i cmpeg_res_11 =
-      _mm_cmpeq_epi32(second_8x8_output, _mm256_extractf128_si256(dir03, 1));
-  const __m128i t_first_8x8 = _mm_packs_epi32(cmpeg_res_01, cmpeg_res_00);
-  const __m128i t_second_8x8 = _mm_packs_epi32(cmpeg_res_11, cmpeg_res_10);
-
-  best_cost[0] = _mm_cvtsi128_si32(_mm256_castsi256_si128(max));
-  best_cost[1] = _mm_cvtsi128_si32(second_8x8_output);
-  best_dir[0] = _mm_movemask_epi8(_mm_packs_epi16(t_first_8x8, t_first_8x8));
-  best_dir[0] =
-      get_msb(best_dir[0] ^ (best_dir[0] - 1));  // Count trailing zeros
-  best_dir[1] = _mm_movemask_epi8(_mm_packs_epi16(t_second_8x8, t_second_8x8));
-  best_dir[1] =
-      get_msb(best_dir[1] ^ (best_dir[1] - 1));  // Count trailing zeros
+      _mm256_max_epi32(max, _mm256_shuffle_epi32(max, _MM_SHUFFLE(2, 3, 0, 1)));
+
+  const __m256i eq03 = _mm256_cmpeq_epi32(max, dir03);
+  const __m256i eq47 = _mm256_cmpeq_epi32(max, v_dir47);
+  const __m256i packed16 = _mm256_packs_epi32(eq03, eq47);
+  const uint32_t eq_mask =
+      (uint32_t)_mm256_movemask_epi8(_mm256_packs_epi16(packed16, packed16));
+
+  // Count trailing zeros via get_msb(m ^ (m - 1)) to keep lowest tie-break dir.
+  const uint32_t m0 = eq_mask & 0xffU;
+  const uint32_t m1 = (eq_mask >> 16) & 0xffU;
+  assert(m0 != 0 && m1 != 0);
+  const int dir0 = get_msb(m0 ^ (m0 - 1));
+  const int dir1 = get_msb(m1 ^ (m1 - 1));
+
+  const int32_t best0 = _mm_cvtsi128_si32(_mm256_castsi256_si128(max));
+  const int32_t best1 = _mm_cvtsi128_si32(_mm256_extracti128_si256(max, 1));
+  const int32_t *orth_costs0 = (dir0 & 4) ? costs : costs + 8;
+  const int32_t *orth_costs1 = (dir1 & 4) ? costs + 4 : costs + 12;

   /* Difference between the optimal variance and the variance along the
-     orthogonal direction. Again, the sum(x^2) terms cancel out. */
-  *var_out_1st = best_cost[0] - cost_first_8x8[(best_dir[0] + 4) & 7];
-  *var_out_2nd = best_cost[1] - cost_second_8x8[(best_dir[1] + 4) & 7];
-
-  /* We'd normally divide by 840, but dividing by 1024 is close enough
-  for what we're going to do with this. */
-  *var_out_1st >>= 10;
-  *var_out_2nd >>= 10;
-  *out_dir_1st_8x8 = best_dir[0];
-  *out_dir_2nd_8x8 = best_dir[1];
+     orthogonal direction (>> 10 approximates / 840; sum(x^2) cancels out). */
+  *var_out_1st = (best0 - orth_costs0[dir0 & 3]) >> 10;
+  *var_out_2nd = (best1 - orth_costs1[dir1 & 3]) >> 10;
+  *out_dir_1st_8x8 = dir0;
+  *out_dir_2nd_8x8 = dir1;
 }

 void cdef_copy_rect8_8bit_to_16bit_avx2(uint16_t *dst, int dstride,
                                         const uint8_t *src, int sstride,
                                         int width, int height) {
-  int j = 0;
-  int remaining_width = width;
   assert(height % 2 == 0);
   assert(height > 0);
   assert(width > 0);

-  // Process multiple 32 pixels at a time.
-  if (remaining_width > 31) {
-    int i = 0;
+  const ptrdiff_t ss = sstride;
+  const ptrdiff_t ds = dstride;
+  const ptrdiff_t ss2 = 2 * ss;
+  const ptrdiff_t ds2 = 2 * ds;
+  int j = 0;
+
+  if (width >= 64) {
     do {
-      j = 0;
-      do {
-        __m128i row00 =
-            _mm_loadu_si128((const __m128i *)&src[(i + 0) * sstride + (j + 0)]);
-        __m128i row01 = _mm_loadu_si128(
-            (const __m128i *)&src[(i + 0) * sstride + (j + 16)]);
-        __m128i row10 =
-            _mm_loadu_si128((const __m128i *)&src[(i + 1) * sstride + (j + 0)]);
-        __m128i row11 = _mm_loadu_si128(
-            (const __m128i *)&src[(i + 1) * sstride + (j + 16)]);
-        _mm256_storeu_si256((__m256i *)&dst[(i + 0) * dstride + (j + 0)],
-                            _mm256_cvtepu8_epi16(row00));
-        _mm256_storeu_si256((__m256i *)&dst[(i + 0) * dstride + (j + 16)],
-                            _mm256_cvtepu8_epi16(row01));
-        _mm256_storeu_si256((__m256i *)&dst[(i + 1) * dstride + (j + 0)],
-                            _mm256_cvtepu8_epi16(row10));
-        _mm256_storeu_si256((__m256i *)&dst[(i + 1) * dstride + (j + 16)],
-                            _mm256_cvtepu8_epi16(row11));
-        j += 32;
-      } while (j <= width - 32);
-      i += 2;
-    } while (i < height);
-    remaining_width = width & 31;
+      const uint8_t *s = src + j;
+      uint16_t *d = dst + j;
+      int h = height;
+      for (;;) {
+        const __m128i r00 = _mm_loadu_si128((const __m128i *)(s + 0));
+        const __m128i r01 = _mm_loadu_si128((const __m128i *)(s + 16));
+        const __m128i r02 = _mm_loadu_si128((const __m128i *)(s + 32));
+        const __m128i r03 = _mm_loadu_si128((const __m128i *)(s + 48));
+        const __m128i r10 = _mm_loadu_si128((const __m128i *)(s + ss + 0));
+        const __m128i r11 = _mm_loadu_si128((const __m128i *)(s + ss + 16));
+        const __m128i r12 = _mm_loadu_si128((const __m128i *)(s + ss + 32));
+        const __m128i r13 = _mm_loadu_si128((const __m128i *)(s + ss + 48));
+        _mm256_storeu_si256((__m256i *)(d + 0), _mm256_cvtepu8_epi16(r00));
+        _mm256_storeu_si256((__m256i *)(d + 16), _mm256_cvtepu8_epi16(r01));
+        _mm256_storeu_si256((__m256i *)(d + 32), _mm256_cvtepu8_epi16(r02));
+        _mm256_storeu_si256((__m256i *)(d + 48), _mm256_cvtepu8_epi16(r03));
+        _mm256_storeu_si256((__m256i *)(d + ds + 0), _mm256_cvtepu8_epi16(r10));
+        _mm256_storeu_si256((__m256i *)(d + ds + 16),
+                            _mm256_cvtepu8_epi16(r11));
+        _mm256_storeu_si256((__m256i *)(d + ds + 32),
+                            _mm256_cvtepu8_epi16(r12));
+        _mm256_storeu_si256((__m256i *)(d + ds + 48),
+                            _mm256_cvtepu8_epi16(r13));
+        if ((h -= 2) <= 0) break;
+        s += ss2;
+        d += ds2;
+      }
+      j += 64;
+    } while (j <= width - 64);
   }

-  // Process 16 pixels at a time.
-  if (remaining_width > 15) {
-    int i = 0;
-    do {
-      __m128i row0 =
-          _mm_loadu_si128((const __m128i *)&src[(i + 0) * sstride + j]);
-      __m128i row1 =
-          _mm_loadu_si128((const __m128i *)&src[(i + 1) * sstride + j]);
-      _mm256_storeu_si256((__m256i *)&dst[(i + 0) * dstride + j],
-                          _mm256_cvtepu8_epi16(row0));
-      _mm256_storeu_si256((__m256i *)&dst[(i + 1) * dstride + j],
-                          _mm256_cvtepu8_epi16(row1));
-      i += 2;
-    } while (i < height);
-    remaining_width = width & 15;
+  if (width & 32) {
+    const uint8_t *s = src + j;
+    uint16_t *d = dst + j;
+    int h = height;
+    for (;;) {
+      const __m128i r00 = _mm_loadu_si128((const __m128i *)(s + 0));
+      const __m128i r01 = _mm_loadu_si128((const __m128i *)(s + 16));
+      const __m128i r10 = _mm_loadu_si128((const __m128i *)(s + ss + 0));
+      const __m128i r11 = _mm_loadu_si128((const __m128i *)(s + ss + 16));
+      _mm256_storeu_si256((__m256i *)(d + 0), _mm256_cvtepu8_epi16(r00));
+      _mm256_storeu_si256((__m256i *)(d + 16), _mm256_cvtepu8_epi16(r01));
+      _mm256_storeu_si256((__m256i *)(d + ds + 0), _mm256_cvtepu8_epi16(r10));
+      _mm256_storeu_si256((__m256i *)(d + ds + 16), _mm256_cvtepu8_epi16(r11));
+      if ((h -= 2) <= 0) break;
+      s += ss2;
+      d += ds2;
+    }
+    j += 32;
+  }
+
+  if (width & 16) {
+    const uint8_t *s = src + j;
+    uint16_t *d = dst + j;
+    int h = height;
+    for (;;) {
+      const __m128i r0 = _mm_loadu_si128((const __m128i *)s);
+      const __m128i r1 = _mm_loadu_si128((const __m128i *)(s + ss));
+      _mm256_storeu_si256((__m256i *)d, _mm256_cvtepu8_epi16(r0));
+      _mm256_storeu_si256((__m256i *)(d + ds), _mm256_cvtepu8_epi16(r1));
+      if ((h -= 2) <= 0) break;
+      s += ss2;
+      d += ds2;
+    }
     j += 16;
   }

-  // Process 8 pixels at a time.
-  if (remaining_width > 7) {
-    int i = 0;
-    do {
-      __m128i row0 =
-          _mm_loadl_epi64((const __m128i *)&src[(i + 0) * sstride + j]);
-      __m128i row1 =
-          _mm_loadl_epi64((const __m128i *)&src[(i + 1) * sstride + j]);
-      _mm_storeu_si128((__m128i *)&dst[(i + 0) * dstride + j],
-                       _mm_unpacklo_epi8(row0, _mm_setzero_si128()));
-      _mm_storeu_si128((__m128i *)&dst[(i + 1) * dstride + j],
-                       _mm_unpacklo_epi8(row1, _mm_setzero_si128()));
-      i += 2;
-    } while (i < height);
-    remaining_width = width & 7;
+  if (width & 8) {
+    const uint8_t *s = src + j;
+    uint16_t *d = dst + j;
+    int h = height;
+    for (;;) {
+      const __m128i r0 = _mm_loadl_epi64((const __m128i *)s);
+      const __m128i r1 = _mm_loadl_epi64((const __m128i *)(s + ss));
+      _mm_storeu_si128((__m128i *)d, _mm_cvtepu8_epi16(r0));
+      _mm_storeu_si128((__m128i *)(d + ds), _mm_cvtepu8_epi16(r1));
+      if ((h -= 2) <= 0) break;
+      s += ss2;
+      d += ds2;
+    }
     j += 8;
   }

-  // Process 4 pixels at a time.
-  if (remaining_width > 3) {
-    int i = 0;
-    do {
-      __m128i row0 =
-          _mm_cvtsi32_si128(*((const int32_t *)&src[(i + 0) * sstride + j]));
-      __m128i row1 =
-          _mm_cvtsi32_si128(*((const int32_t *)&src[(i + 1) * sstride + j]));
-      _mm_storel_epi64((__m128i *)&dst[(i + 0) * dstride + j],
-                       _mm_unpacklo_epi8(row0, _mm_setzero_si128()));
-      _mm_storel_epi64((__m128i *)&dst[(i + 1) * dstride + j],
-                       _mm_unpacklo_epi8(row1, _mm_setzero_si128()));
-      i += 2;
-    } while (i < height);
-    remaining_width = width & 3;
+  if (width & 4) {
+    const uint8_t *s = src + j;
+    uint16_t *d = dst + j;
+    int h = height;
+    for (;;) {
+      _mm_storel_epi64((__m128i *)d, _mm_cvtepu8_epi16(xx_loadl_32(s)));
+      _mm_storel_epi64((__m128i *)(d + ds),
+                       _mm_cvtepu8_epi16(xx_loadl_32(s + ss)));
+      if ((h -= 2) <= 0) break;
+      s += ss2;
+      d += ds2;
+    }
     j += 4;
   }

-  // Process the remaining pixels.
-  if (remaining_width) {
-    for (int i = 0; i < height; i++) {
-      for (int k = j; k < width; k++) {
-        dst[i * dstride + k] = src[i * sstride + k];
-      }
+  if (width & 2) {
+    const uint8_t *s = src + j;
+    uint16_t *d = dst + j;
+    int h = height;
+    for (;;) {
+      const int32_t v0 = (int32_t)(uint16_t)loadu_int16(s);
+      const int32_t v1 = (int32_t)(uint16_t)loadu_int16(s + ss);
+      xx_storel_32(d, _mm_cvtepu8_epi16(_mm_cvtsi32_si128(v0)));
+      xx_storel_32(d + ds, _mm_cvtepu8_epi16(_mm_cvtsi32_si128(v1)));
+      if ((h -= 2) <= 0) break;
+      s += ss2;
+      d += ds2;
+    }
+    j += 2;
+  }
+
+  if (width & 1) {
+    const uint8_t *s = src + j;
+    uint16_t *d = dst + j;
+    int h = height;
+    for (;;) {
+      d[0] = s[0];
+      d[ds] = s[ss];
+      if ((h -= 2) <= 0) break;
+      s += ss2;
+      d += ds2;
     }
   }
 }
diff --git a/test/cdef_test.cc b/test/cdef_test.cc
index df535abcdd..eefc441875 100644
--- a/test/cdef_test.cc
+++ b/test/cdef_test.cc
@@ -9,18 +9,22 @@
  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
  */

+#include <algorithm>
 #include <array>
 #include <cassert>
 #include <cstdlib>
 #include <iostream>
+#include <memory>
 #include <string>
 #include <tuple>
+#include <vector>

 #include "gtest/gtest.h"

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

+#include "aom_mem/aom_mem.h"
 #include "aom_ports/aom_timer.h"
 #include "av1/common/cdef_block.h"
 #include "test/acm_random.h"
@@ -604,6 +608,85 @@ TEST_P(CDEFFindDirDualTest, TestSIMDNoMismatch) {
   test_finddir_dual(finddir_, ref_finddir_);
 }

+TEST_P(CDEFFindDirDualTest, TestCdefBStride) {
+  ACMRandom rnd(ACMRandom::DeterministicSeed());
+  std::unique_ptr<uint16_t[], decltype(&aom_free)> inbuf_guard(
+      static_cast<uint16_t *>(
+          aom_memalign(32, CDEF_INBUF_SIZE * sizeof(uint16_t))),
+      &aom_free);
+  ASSERT_NE(inbuf_guard, nullptr);
+  uint16_t *const inbuf = inbuf_guard.get();
+  struct BlockPair {
+    int r1, c1, r2, c2;
+  };
+  static const BlockPair kPairs[] = {
+    { CDEF_VBORDER, CDEF_HBORDER, CDEF_VBORDER, CDEF_HBORDER + 16 },
+    { CDEF_VBORDER, CDEF_HBORDER + 1, CDEF_VBORDER + 16, CDEF_HBORDER + 25 },
+    { CDEF_VBORDER + 8, CDEF_HBORDER + 56, CDEF_VBORDER + 24,
+      CDEF_HBORDER + 8 },
+  };
+  for (int shift = 0; shift <= 4; shift += 2) {
+    const int depth = 8 + shift;
+    const uint16_t max_val = static_cast<uint16_t>((1 << depth) - 1);
+    for (size_t pair = 0; pair < sizeof(kPairs) / sizeof(kPairs[0]); ++pair) {
+      const BlockPair &bp = kPairs[pair];
+      for (int pat = 0; pat < 10; ++pat) {
+        for (size_t idx = 0; idx < static_cast<size_t>(CDEF_INBUF_SIZE);
+             ++idx) {
+          inbuf[idx] = CDEF_VERY_LARGE;
+        }
+        uint16_t *const img1 = inbuf + bp.r1 * CDEF_BSTRIDE + bp.c1;
+        uint16_t *const img2 = inbuf + bp.r2 * CDEF_BSTRIDE + bp.c2;
+        for (int r = 0; r < 8; ++r) {
+          for (int c = 0; c < 8; ++c) {
+            uint16_t v1 = 0;
+            uint16_t v2 = 0;
+            if (pat == 0) {
+              v1 = 0;
+              v2 = max_val;
+            } else if (pat == 1) {
+              v1 = max_val;
+              v2 = 0;
+            } else if (pat == 2) {
+              v1 = ((r ^ c) & 1) ? max_val : 0;
+              v2 = ((r ^ c) & 1) ? 0 : max_val;
+            } else if (pat == 3) {
+              const uint16_t lo = static_cast<uint16_t>((1 << shift) - 1);
+              v1 = (c < 4) ? lo : static_cast<uint16_t>(max_val - lo);
+              v2 = (r < 4) ? static_cast<uint16_t>(max_val - lo) : lo;
+            } else if (pat == 4) {
+              v1 = static_cast<uint16_t>(((r * 31 + c * 3) << shift) & max_val);
+              v2 = static_cast<uint16_t>(((c * 29 + r * 5) << shift) & max_val);
+            } else {
+              v1 = static_cast<uint16_t>(rnd.Rand16() & max_val);
+              v2 = static_cast<uint16_t>(rnd.Rand16() & max_val);
+            }
+            img1[r * CDEF_BSTRIDE + c] = v1;
+            img2[r * CDEF_BSTRIDE + c] = v2;
+          }
+        }
+        int32_t ref_var[2] = { -1, -1 };
+        int32_t tst_var[2] = { -2, -2 };
+        int ref_dir[2] = { -1, -1 };
+        int tst_dir[2] = { -2, -2 };
+        ref_finddir_(img1, img2, CDEF_BSTRIDE, &ref_var[0], &ref_var[1], shift,
+                     &ref_dir[0], &ref_dir[1]);
+        API_REGISTER_STATE_CHECK(finddir_(img1, img2, CDEF_BSTRIDE, &tst_var[0],
+                                          &tst_var[1], shift, &tst_dir[0],
+                                          &tst_dir[1]));
+        for (int j = 0; j < 2; ++j) {
+          ASSERT_EQ(ref_dir[j], tst_dir[j])
+              << "shift=" << shift << " pair=" << pair << " pat=" << pat
+              << " blk=" << j;
+          ASSERT_EQ(ref_var[j], tst_var[j])
+              << "shift=" << shift << " pair=" << pair << " pat=" << pat
+              << " blk=" << j;
+        }
+      }
+    }
+  }
+}
+
 TEST_P(CDEFFindDirDualSpeedTest, DISABLED_TestSpeed) {
   test_finddir_dual_speed(finddir_, ref_finddir_);
 }
@@ -761,6 +844,58 @@ INSTANTIATE_TEST_SUITE_P(
     ::testing::Values(make_tuple(&cdef_copy_rect8_8bit_to_16bit_c,
                                  &cdef_copy_rect8_8bit_to_16bit_avx2)));

+TEST(AVX2, CDEFCopyRect8to16WidthsAndAlignments) {
+  ACMRandom rnd(ACMRandom::DeterministicSeed());
+  static const int kWidths[] = { 1,  2,  3,  4,  5,  6,  7,  8,   12,  15,
+                                 16, 31, 32, 63, 64, 79, 95, 127, 128, 135 };
+  static const int kHeights[] = { 2, 4, 6, 8 };
+  constexpr int kSrcStride = 157;
+  constexpr int kDstStride = 163;
+  constexpr int kMaxRows = 12;
+  constexpr uint16_t kCanary = 0x5aa5;
+  std::vector<uint8_t> src_buf(static_cast<size_t>(kSrcStride) * kMaxRows + 32);
+  std::vector<uint16_t> ref_buf(static_cast<size_t>(kDstStride) * kMaxRows +
+                                32);
+  std::vector<uint16_t> tst_buf(static_cast<size_t>(kDstStride) * kMaxRows +
+                                32);
+
+  for (int src_off : { 0, 1, 3, 7 }) {
+    for (int dst_off : { 0, 1, 3 }) {
+      for (int height : kHeights) {
+        for (int width : kWidths) {
+          for (size_t idx = 0; idx < src_buf.size(); ++idx) {
+            src_buf[idx] = rnd.Rand8();
+          }
+          std::fill(ref_buf.begin(), ref_buf.end(), kCanary);
+          std::fill(tst_buf.begin(), tst_buf.end(), kCanary);
+          const uint8_t *const src_ptr = src_buf.data() + src_off;
+          uint16_t *const ref_ptr = ref_buf.data() + dst_off;
+          uint16_t *const tst_ptr = tst_buf.data() + dst_off;
+          cdef_copy_rect8_8bit_to_16bit_c(ref_ptr, kDstStride, src_ptr,
+                                          kSrcStride, width, height);
+          API_REGISTER_STATE_CHECK(cdef_copy_rect8_8bit_to_16bit_avx2(
+              tst_ptr, kDstStride, src_ptr, kSrcStride, width, height));
+          for (size_t idx = 0; idx < ref_buf.size(); ++idx) {
+            const bool in_dst = idx >= static_cast<size_t>(dst_off);
+            const size_t rel = in_dst ? idx - static_cast<size_t>(dst_off) : 0;
+            const size_t r = rel / static_cast<size_t>(kDstStride);
+            const size_t c = rel % static_cast<size_t>(kDstStride);
+            const bool in_rect = in_dst && r < static_cast<size_t>(height) &&
+                                 c < static_cast<size_t>(width);
+            ASSERT_EQ(ref_buf[idx], tst_buf[idx])
+                << (in_rect ? "mismatch"
+                            : (!in_dst ? "pre-dst canary overwrite"
+                                       : "canary overwrite"))
+                << " idx=" << idx << " r=" << r << " c=" << c
+                << " width=" << width << " height=" << height
+                << " src_off=" << src_off << " dst_off=" << dst_off;
+          }
+        }
+      }
+    }
+  }
+}
+
 #if CONFIG_AV1_HIGHBITDEPTH
 INSTANTIATE_TEST_SUITE_P(
     AVX2, CDEFCopyRect16to16Test,