Commit 7c0bb43552 for aom

commit 7c0bb43552bd759d953d0575955948f37f7ffad8
Author: Frank Galligan <fgalligan@google.com>
Date:   Mon Nov 23 00:48:49 2026 -0800

    av1,hwy: Vector-local 8/16 fwd txfm and transpose

    Keep 8- and 16-point 1D forward DCT/ADST stages in SIMD vector locals,
    replace scatter stores in Transpose8/Transpose16 with hierarchical
    interleaves, use 256-bit paired-mul WholeVec butterflies, and disable
    inlining of 16- and 32-point 1D blocks with HWY_NOINLINE to bound
    I-cache and stack usage.

    Speedup on AMD EPYC 7B13 (bit-exact):
    - 2-pass VOD AV1 (6T): -1.79% weighted (-3.14% 720p s3, -2.51%
      360p s1, -1.94% 4K s5, -1.16% 1080p s2, -0.71% 720p HDR10 s3);
      -1.50% 1T 720p s3.
    - AVX2/AV1FwdTxfm2dTest.DISABLED_Speed (1T): 1.20x-1.41x faster on
      16x16 (1.23x on 16x8, 1.18x on 8x16).

    TAG=agy
    CONV=181013d1-8c6e-44c8-b7ad-7781bba16c12
    Change-Id: I9fc89e0a151497568f09bb442000cfcb008f3d29

diff --git a/av1/encoder/av1_fwd_txfm2d_hwy.h b/av1/encoder/av1_fwd_txfm2d_hwy.h
index 65a3668cd2..cbf46a7ca5 100644
--- a/av1/encoder/av1_fwd_txfm2d_hwy.h
+++ b/av1/encoder/av1_fwd_txfm2d_hwy.h
@@ -12,6 +12,8 @@
 #ifndef AOM_AV1_ENCODER_AV1_FWD_TXFM2D_HWY_H_
 #define AOM_AV1_ENCODER_AV1_FWD_TXFM2D_HWY_H_

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

 #include "config/aom_config.h"
@@ -57,6 +59,9 @@ namespace HWY_NAMESPACE {

 namespace hn = hwy::HWY_NAMESPACE;

+static_assert(!HWY_HAVE_SCALABLE,
+              "av1_fwd_txfm2d_hwy.h requires fixed-size SIMD vectors");
+
 constexpr int8_t kForwardTransformShift[TX_SIZES_ALL][3] = {
   { 2, 0, 0 },    //
   { 2, -1, 0 },   //
@@ -145,18 +150,15 @@ struct ButterflyTraits {};
 template <>
 struct ButterflyTraits<2> {
   template <typename D>
-  HWY_ATTR HWY_INLINE static void Whole(
-      D int_tag, int w0, int w1, const hn::TFromD<D> *HWY_RESTRICT in0,
-      const hn::TFromD<D> *HWY_RESTRICT in1, hn::TFromD<D> *HWY_RESTRICT out0,
-      hn::TFromD<D> *HWY_RESTRICT out1, int bit,
+  HWY_ATTR HWY_INLINE static void WholeVec(
+      D int_tag, int w0, int w1, hn::VFromD<D> in0, hn::VFromD<D> in1,
+      hn::VFromD<D> &out0, hn::VFromD<D> &out1, int bit,
       hn::VFromD<hn::Repartition<int32_t, D>> round) {
     constexpr hn::RepartitionToWide<D> int32_tag;
     const auto ww0 = SetPair(int_tag, w0, w1);
     const auto ww1 = SetPair(int_tag, w1, -w0);
-    const auto i0 = hn::Load(int_tag, in0);
-    const auto i1 = hn::Load(int_tag, in1);
-    const auto t0 = hn::InterleaveLower(int_tag, i0, i1);
-    const auto t1 = hn::InterleaveUpper(int_tag, i0, i1);
+    const auto t0 = hn::InterleaveLower(int_tag, in0, in1);
+    const auto t1 = hn::InterleaveUpper(int_tag, in0, in1);
     const auto u0 = hn::WidenMulPairwiseAdd(int32_tag, t0, ww0);
     const auto u1 = hn::WidenMulPairwiseAdd(int32_tag, t1, ww0);
     const auto v0 = hn::WidenMulPairwiseAdd(int32_tag, t0, ww1);
@@ -165,64 +167,64 @@ struct ButterflyTraits<2> {
     const auto c1 = hn::ShiftRightSame(hn::Add(u1, round), bit);
     const auto d0 = hn::ShiftRightSame(hn::Add(v0, round), bit);
     const auto d1 = hn::ShiftRightSame(hn::Add(v1, round), bit);
-    hn::Store(hn::ReorderDemote2To(int_tag, c0, c1), int_tag, out0);
-    hn::Store(hn::ReorderDemote2To(int_tag, d0, d1), int_tag, out1);
+    out0 = hn::ReorderDemote2To(int_tag, c0, c1);
+    out1 = hn::ReorderDemote2To(int_tag, d0, d1);
   }

   template <typename D>
-  HWY_ATTR HWY_INLINE static void Half(
+  HWY_ATTR HWY_INLINE static void Whole(
       D int_tag, int w0, int w1, const hn::TFromD<D> *HWY_RESTRICT in0,
-      const hn::TFromD<D> *HWY_RESTRICT in1, hn::TFromD<D> *HWY_RESTRICT out,
-      int bit, hn::VFromD<hn::Repartition<int32_t, D>> round) {
-    constexpr hn::RepartitionToWide<D> int32_tag;
-    const auto i0 = hn::Load(int_tag, in0);
-    const auto i1 = hn::Load(int_tag, in1);
-    const auto t0 = hn::InterleaveLower(int_tag, i0, i1);
-    const auto t1 = hn::InterleaveUpper(int_tag, i0, i1);
-    const auto ww0 = SetPair(int_tag, w0, w1);
-    const auto u0 = hn::WidenMulPairwiseAdd(int32_tag, t0, ww0);
-    const auto u1 = hn::WidenMulPairwiseAdd(int32_tag, t1, ww0);
-    const auto c0 = hn::ShiftRightSame(hn::Add(u0, round), bit);
-    const auto c1 = hn::ShiftRightSame(hn::Add(u1, round), bit);
-    hn::Store(hn::ReorderDemote2To(int_tag, c0, c1), int_tag, out);
+      const hn::TFromD<D> *HWY_RESTRICT in1, hn::TFromD<D> *HWY_RESTRICT out0,
+      hn::TFromD<D> *HWY_RESTRICT out1, int bit,
+      hn::VFromD<hn::Repartition<int32_t, D>> round) {
+    hn::VFromD<D> v0, v1;
+    WholeVec(int_tag, w0, w1, hn::Load(int_tag, in0), hn::Load(int_tag, in1),
+             v0, v1, bit, round);
+    hn::Store(v0, int_tag, out0);
+    hn::Store(v1, int_tag, out1);
   }
 };

 template <>
 struct ButterflyTraits<4> {
   template <typename D>
-  HWY_ATTR HWY_INLINE static void Whole(
-      D int_tag, int w0, int w1, const hn::TFromD<D> *HWY_RESTRICT in0,
-      const hn::TFromD<D> *HWY_RESTRICT in1, hn::TFromD<D> *HWY_RESTRICT out0,
-      hn::TFromD<D> *HWY_RESTRICT out1, int bit,
+  HWY_ATTR HWY_INLINE static void WholeVec(
+      D int_tag, int w0, int w1, hn::VFromD<D> in0, hn::VFromD<D> in1,
+      hn::VFromD<D> &out0, hn::VFromD<D> &out1, int bit,
       hn::VFromD<hn::Repartition<int32_t, D>> round) {
-    const auto i0 = hn::Load(int_tag, in0);
-    const auto i1 = hn::Load(int_tag, in1);
     const auto ww0 = hn::Set(int_tag, w0);
     const auto ww1 = hn::Set(int_tag, w1);
-    const auto in1_w1 = hn::Mul(i1, ww1);
-    const auto o0 = hn::MulAdd(i0, ww0, in1_w1);
-    hn::Store(hn::ShiftRightSame(hn::Add(o0, round), bit), int_tag, out0);
-    const auto in1_w0 = hn::Mul(i1, ww0);
-    const auto o1 = hn::MulSub(i0, ww1, in1_w0);
-    hn::Store(hn::ShiftRightSame(hn::Add(o1, round), bit), int_tag, out1);
+    const auto in1_w1 = hn::Mul(in1, ww1);
+    const auto o0 = hn::MulAdd(in0, ww0, in1_w1);
+    out0 = hn::ShiftRightSame(hn::Add(o0, round), bit);
+    const auto in1_w0 = hn::Mul(in1, ww0);
+    const auto o1 = hn::MulSub(in0, ww1, in1_w0);
+    out1 = hn::ShiftRightSame(hn::Add(o1, round), bit);
   }

   template <typename D>
-  HWY_ATTR HWY_INLINE static void Half(
+  HWY_ATTR HWY_INLINE static void Whole(
       D int_tag, int w0, int w1, const hn::TFromD<D> *HWY_RESTRICT in0,
-      const hn::TFromD<D> *HWY_RESTRICT in1, hn::TFromD<D> *HWY_RESTRICT out,
-      int bit, hn::VFromD<hn::Repartition<int32_t, D>> round) {
-    const auto i0 = hn::Load(int_tag, in0);
-    const auto i1 = hn::Load(int_tag, in1);
-    const auto ww0 = hn::Set(int_tag, w0);
-    const auto ww1 = hn::Set(int_tag, w1);
-    const auto in1_w1 = hn::Mul(i1, ww1);
-    const auto o0 = hn::MulAdd(i0, ww0, in1_w1);
-    hn::Store(hn::ShiftRightSame(hn::Add(o0, round), bit), int_tag, out);
+      const hn::TFromD<D> *HWY_RESTRICT in1, hn::TFromD<D> *HWY_RESTRICT out0,
+      hn::TFromD<D> *HWY_RESTRICT out1, int bit,
+      hn::VFromD<hn::Repartition<int32_t, D>> round) {
+    hn::VFromD<D> v0, v1;
+    WholeVec(int_tag, w0, w1, hn::Load(int_tag, in0), hn::Load(int_tag, in1),
+             v0, v1, bit, round);
+    hn::Store(v0, int_tag, out0);
+    hn::Store(v1, int_tag, out1);
   }
 };

+template <typename D>
+HWY_ATTR HWY_INLINE void ButterflyVec(
+    D int_tag, int w0, int w1, const hn::VFromD<D> &in0,
+    const hn::VFromD<D> &in1, hn::VFromD<D> &out0, hn::VFromD<D> &out1, int bit,
+    hn::VFromD<hn::Repartition<int32_t, D>> round) {
+  ButterflyTraits<sizeof(hn::TFromD<D>)>::WholeVec(int_tag, w0, w1, in0, in1,
+                                                   out0, out1, bit, round);
+}
+
 template <typename D>
 HWY_ATTR HWY_INLINE void Butterfly(
     D int_tag, int w0, int w1, const hn::TFromD<D> *HWY_RESTRICT in0,
@@ -234,12 +236,17 @@ HWY_ATTR HWY_INLINE void Butterfly(
 }

 template <typename D>
-HWY_ATTR HWY_INLINE void HalfButterfly(
-    D int_tag, int w0, int w1, const hn::TFromD<D> *HWY_RESTRICT in0,
-    const hn::TFromD<D> *HWY_RESTRICT in1, hn::TFromD<D> *HWY_RESTRICT out,
-    int bit, hn::VFromD<hn::Repartition<int32_t, D>> round) {
-  ButterflyTraits<sizeof(hn::TFromD<D>)>::Half(int_tag, w0, w1, in0, in1, out,
-                                               bit, round);
+HWY_ATTR HWY_INLINE void AddSubVec(D int_tag, hn::VFromD<D> in0,
+                                   hn::VFromD<D> in1, hn::VFromD<D> &out_add,
+                                   hn::VFromD<D> &out_sub) {
+  (void)int_tag;
+  if CONSTEXPR_IF (sizeof(hn::TFromD<D>) == 2) {
+    out_add = hn::SaturatedAdd(in0, in1);
+    out_sub = hn::SaturatedSub(in0, in1);
+  } else {
+    out_add = hn::Add(in0, in1);
+    out_sub = hn::Sub(in0, in1);
+  }
 }

 template <typename D>
@@ -247,15 +254,11 @@ HWY_ATTR HWY_INLINE void AddSub(D int_tag, const hn::TFromD<D> *in0,
                                 const hn::TFromD<D> *in1,
                                 hn::TFromD<D> *out_add,
                                 hn::TFromD<D> *out_sub) {
-  const auto i0 = hn::Load(int_tag, in0);
-  const auto i1 = hn::Load(int_tag, in1);
-  if CONSTEXPR_IF (sizeof(hn::TFromD<D>) == 2) {
-    hn::Store(hn::SaturatedAdd(i0, i1), int_tag, out_add);
-    hn::Store(hn::SaturatedSub(i0, i1), int_tag, out_sub);
-  } else {
-    hn::Store(hn::Add(i0, i1), int_tag, out_add);
-    hn::Store(hn::Sub(i0, i1), int_tag, out_sub);
-  }
+  hn::VFromD<D> add_v, sub_v;
+  AddSubVec(int_tag, hn::Load(int_tag, in0), hn::Load(int_tag, in1), add_v,
+            sub_v);
+  hn::Store(add_v, int_tag, out_add);
+  hn::Store(sub_v, int_tag, out_sub);
 }

 template <size_t LaneSize, size_t NumLanes>
@@ -334,164 +337,141 @@ HWY_ATTR HWY_INLINE void Fdct4(D int_tag, hn::TFromD<D> *HWY_RESTRICT in,
 template <typename D>
 HWY_ATTR HWY_INLINE void Fdct8(D int_tag, hn::TFromD<D> *HWY_RESTRICT in,
                                const int8_t cos_bit, size_t instride) {
-  constexpr size_t kNumLanes = hn::MaxLanes(int_tag);
-  HWY_ALIGN_MAX hn::TFromD<D> buf0[8 * kNumLanes];
-  HWY_ALIGN_MAX hn::TFromD<D> buf1[8 * kNumLanes];
   const int32_t *HWY_RESTRICT const cospi = cospi_arr(cos_bit);
   const auto round = hn::Set(hn::Repartition<int32_t, D>(), 1 << (cos_bit - 1));

-  // Even 8 points 0, 2, ..., 14
   // stage 0
   // stage 1
-  // buf0/buf1
-  AddSub(int_tag, &in[0 * instride], &in[7 * instride], &buf0[0 * kNumLanes],
-         &buf1[7 * kNumLanes]);
-  // buf0/buf0
-  AddSub(int_tag, &in[1 * instride], &in[6 * instride], &buf0[1 * kNumLanes],
-         &buf0[6 * kNumLanes]);
-  // buf0/buf0
-  AddSub(int_tag, &in[2 * instride], &in[5 * instride], &buf0[2 * kNumLanes],
-         &buf0[5 * kNumLanes]);
-  // buf0/buf1
-  AddSub(int_tag, &in[3 * instride], &in[4 * instride], &buf0[3 * kNumLanes],
-         &buf1[4 * kNumLanes]);
+  hn::VFromD<D> b0[8], b1[8];
+  AddSubVec(int_tag, hn::Load(int_tag, &in[0 * instride]),
+            hn::Load(int_tag, &in[7 * instride]), b0[0], b1[7]);
+  AddSubVec(int_tag, hn::Load(int_tag, &in[1 * instride]),
+            hn::Load(int_tag, &in[6 * instride]), b0[1], b0[6]);
+  AddSubVec(int_tag, hn::Load(int_tag, &in[2 * instride]),
+            hn::Load(int_tag, &in[5 * instride]), b0[2], b0[5]);
+  AddSubVec(int_tag, hn::Load(int_tag, &in[3 * instride]),
+            hn::Load(int_tag, &in[4 * instride]), b0[3], b1[4]);

   // stage 2
-  for (size_t i = 0; i < 2; ++i) {
-    AddSub(int_tag, &buf0[i * kNumLanes], &buf0[(3 - i) * kNumLanes],
-           &buf1[i * kNumLanes], &buf1[(3 - i) * kNumLanes]);
-  }
-
-  Butterfly(int_tag, -cospi[32], cospi[32], &buf0[5 * kNumLanes],
-            &buf0[6 * kNumLanes], &buf1[5 * kNumLanes], &buf1[6 * kNumLanes],
-            cos_bit, round);
+  AddSubVec(int_tag, b0[0], b0[3], b1[0], b1[3]);
+  AddSubVec(int_tag, b0[1], b0[2], b1[1], b1[2]);
+  ButterflyVec(int_tag, -cospi[32], cospi[32], b0[5], b0[6], b1[5], b1[6],
+               cos_bit, round);

   // stage 3
-  // type 0
-  Butterfly(int_tag, cospi[32], cospi[32], &buf1[0 * kNumLanes],
-            &buf1[1 * kNumLanes], &in[0 * instride], &in[4 * instride], cos_bit,
-            round);
-
-  // type 1
-  Butterfly(int_tag, cospi[16], cospi[48], &buf1[3 * kNumLanes],
-            &buf1[2 * kNumLanes], &in[2 * instride], &in[6 * instride], cos_bit,
-            round);
-
-  AddSub(int_tag, &buf1[4 * kNumLanes], &buf1[5 * kNumLanes],
-         &buf0[4 * kNumLanes], &buf0[5 * kNumLanes]);
-  AddSub(int_tag, &buf1[7 * kNumLanes], &buf1[6 * kNumLanes],
-         &buf0[7 * kNumLanes], &buf0[6 * kNumLanes]);
-
-  // stage 4
-  // stage 5
-  Butterfly(int_tag, cospi[8], cospi[56], &buf0[7 * kNumLanes],
-            &buf0[4 * kNumLanes], &in[1 * instride], &in[7 * instride], cos_bit,
-            round);
-  Butterfly(int_tag, cospi[40], cospi[24], &buf0[6 * kNumLanes],
-            &buf0[5 * kNumLanes], &in[5 * instride], &in[3 * instride], cos_bit,
-            round);
+  hn::VFromD<D> o0, o2, o4, o6;
+  ButterflyVec(int_tag, cospi[32], cospi[32], b1[0], b1[1], o0, o4, cos_bit,
+               round);
+  ButterflyVec(int_tag, cospi[16], cospi[48], b1[3], b1[2], o2, o6, cos_bit,
+               round);
+  hn::Store(o0, int_tag, &in[0 * instride]);
+  hn::Store(o4, int_tag, &in[4 * instride]);
+  hn::Store(o2, int_tag, &in[2 * instride]);
+  hn::Store(o6, int_tag, &in[6 * instride]);
+
+  AddSubVec(int_tag, b1[4], b1[5], b0[4], b0[5]);
+  AddSubVec(int_tag, b1[7], b1[6], b0[7], b0[6]);
+
+  // stage 4 & 5
+  hn::VFromD<D> o1, o3, o5, o7;
+  ButterflyVec(int_tag, cospi[8], cospi[56], b0[7], b0[4], o1, o7, cos_bit,
+               round);
+  ButterflyVec(int_tag, cospi[40], cospi[24], b0[6], b0[5], o5, o3, cos_bit,
+               round);
+  hn::Store(o1, int_tag, &in[1 * instride]);
+  hn::Store(o7, int_tag, &in[7 * instride]);
+  hn::Store(o5, int_tag, &in[5 * instride]);
+  hn::Store(o3, int_tag, &in[3 * instride]);
 }

 template <typename D>
 HWY_ATTR HWY_INLINE void Fdct16(D int_tag, hn::TFromD<D> *HWY_RESTRICT in,
                                 const int8_t cos_bit, size_t instride) {
-  constexpr size_t kNumLanes = hn::MaxLanes(int_tag);
-  HWY_ALIGN_MAX hn::TFromD<D> buf0[16 * kNumLanes];
-  HWY_ALIGN_MAX hn::TFromD<D> buf1[16 * kNumLanes];
   const int32_t *HWY_RESTRICT const cospi = cospi_arr(cos_bit);
   const auto round = hn::Set(hn::Repartition<int32_t, D>(), 1 << (cos_bit - 1));
+  hn::VFromD<D> b0[16], b1[16];

-  // Calculate the column 0, 1, 2, 3
   // stage 0
   // stage 1
   for (size_t i = 0; i < 8; ++i) {
-    AddSub(int_tag, &in[i * instride], &in[(15 - i) * instride],
-           &buf0[i * kNumLanes], &buf0[(15 - i) * kNumLanes]);
+    AddSubVec(int_tag, hn::Load(int_tag, &in[i * instride]),
+              hn::Load(int_tag, &in[(15 - i) * instride]), b0[i], b0[15 - i]);
   }

   // stage 2
   for (size_t i = 0; i < 4; ++i) {
-    AddSub(int_tag, &buf0[i * kNumLanes], &buf0[(7 - i) * kNumLanes],
-           &buf1[i * kNumLanes], &buf1[(7 - i) * kNumLanes]);
+    AddSubVec(int_tag, b0[i], b0[7 - i], b1[i], b1[7 - i]);
   }
-
-  Butterfly(int_tag, -cospi[32], cospi[32], &buf0[10 * kNumLanes],
-            &buf0[13 * kNumLanes], &buf1[10 * kNumLanes], &buf1[13 * kNumLanes],
-            cos_bit, round);
-  Butterfly(int_tag, -cospi[32], cospi[32], &buf0[11 * kNumLanes],
-            &buf0[12 * kNumLanes], &buf1[11 * kNumLanes], &buf1[12 * kNumLanes],
-            cos_bit, round);
+  ButterflyVec(int_tag, -cospi[32], cospi[32], b0[10], b0[13], b1[10], b1[13],
+               cos_bit, round);
+  ButterflyVec(int_tag, -cospi[32], cospi[32], b0[11], b0[12], b1[11], b1[12],
+               cos_bit, round);

   // stage 3
   for (size_t i = 0; i < 2; ++i) {
-    AddSub(int_tag, &buf1[i * kNumLanes], &buf1[(3 - i) * kNumLanes],
-           &buf0[i * kNumLanes], &buf0[(3 - i) * kNumLanes]);
+    AddSubVec(int_tag, b1[i], b1[3 - i], b0[i], b0[3 - i]);
   }
-
-  Butterfly(int_tag, -cospi[32], cospi[32], &buf1[5 * kNumLanes],
-            &buf1[6 * kNumLanes], &buf0[5 * kNumLanes], &buf0[6 * kNumLanes],
-            cos_bit, round);
-
+  ButterflyVec(int_tag, -cospi[32], cospi[32], b1[5], b1[6], b0[5], b0[6],
+               cos_bit, round);
   for (size_t i = 0; i < 2; ++i) {
-    AddSub(int_tag, &buf0[(8 + i) * kNumLanes], &buf1[(11 - i) * kNumLanes],
-           &buf0[(8 + i) * kNumLanes], &buf0[(11 - i) * kNumLanes]);
+    AddSubVec(int_tag, b0[8 + i], b1[11 - i], b0[8 + i], b0[11 - i]);
   }
   for (size_t i = 0; i < 2; ++i) {
-    AddSub(int_tag, &buf0[(15 - i) * kNumLanes], &buf1[(12 + i) * kNumLanes],
-           &buf0[(15 - i) * kNumLanes], &buf0[(12 + i) * kNumLanes]);
+    AddSubVec(int_tag, b0[15 - i], b1[12 + i], b0[15 - i], b0[12 + i]);
   }

   // stage 4
-  Butterfly(int_tag, cospi[32], cospi[32], &buf0[0 * kNumLanes],
-            &buf0[1 * kNumLanes], &in[0 * instride], &in[8 * instride], cos_bit,
-            round);
-
-  Butterfly(int_tag, cospi[16], cospi[48], &buf0[3 * kNumLanes],
-            &buf0[2 * kNumLanes], &in[4 * instride], &in[12 * instride],
-            cos_bit, round);
-
-  AddSub(int_tag, &buf1[4 * kNumLanes], &buf0[5 * kNumLanes],
-         &buf1[4 * kNumLanes], &buf1[5 * kNumLanes]);
-  AddSub(int_tag, &buf1[7 * kNumLanes], &buf0[6 * kNumLanes],
-         &buf1[7 * kNumLanes], &buf1[6 * kNumLanes]);
-
-  Butterfly(int_tag, -cospi[16], cospi[48], &buf0[9 * kNumLanes],
-            &buf0[14 * kNumLanes], &buf1[9 * kNumLanes], &buf1[14 * kNumLanes],
-            cos_bit, round);
-  Butterfly(int_tag, -cospi[48], -cospi[16], &buf0[10 * kNumLanes],
-            &buf0[13 * kNumLanes], &buf1[10 * kNumLanes], &buf1[13 * kNumLanes],
-            cos_bit, round);
+  hn::VFromD<D> o0, o8, o4, o12;
+  ButterflyVec(int_tag, cospi[32], cospi[32], b0[0], b0[1], o0, o8, cos_bit,
+               round);
+  ButterflyVec(int_tag, cospi[16], cospi[48], b0[3], b0[2], o4, o12, cos_bit,
+               round);
+  hn::Store(o0, int_tag, &in[0 * instride]);
+  hn::Store(o8, int_tag, &in[8 * instride]);
+  hn::Store(o4, int_tag, &in[4 * instride]);
+  hn::Store(o12, int_tag, &in[12 * instride]);
+
+  AddSubVec(int_tag, b1[4], b0[5], b1[4], b1[5]);
+  AddSubVec(int_tag, b1[7], b0[6], b1[7], b1[6]);
+  ButterflyVec(int_tag, -cospi[16], cospi[48], b0[9], b0[14], b1[9], b1[14],
+               cos_bit, round);
+  ButterflyVec(int_tag, -cospi[48], -cospi[16], b0[10], b0[13], b1[10], b1[13],
+               cos_bit, round);

   // stage 5
-  Butterfly(int_tag, cospi[8], cospi[56], &buf1[7 * kNumLanes],
-            &buf1[4 * kNumLanes], &in[2 * instride], &in[14 * instride],
-            cos_bit, round);
-  Butterfly(int_tag, cospi[40], cospi[24], &buf1[6 * kNumLanes],
-            &buf1[5 * kNumLanes], &in[10 * instride], &in[6 * instride],
-            cos_bit, round);
-
-  AddSub(int_tag, &buf0[8 * kNumLanes], &buf1[9 * kNumLanes],
-         &buf0[8 * kNumLanes], &buf0[9 * kNumLanes]);
-  AddSub(int_tag, &buf0[11 * kNumLanes], &buf1[10 * kNumLanes],
-         &buf0[11 * kNumLanes], &buf0[10 * kNumLanes]);
-  AddSub(int_tag, &buf0[12 * kNumLanes], &buf1[13 * kNumLanes],
-         &buf0[12 * kNumLanes], &buf0[13 * kNumLanes]);
-  AddSub(int_tag, &buf0[15 * kNumLanes], &buf1[14 * kNumLanes],
-         &buf0[15 * kNumLanes], &buf0[14 * kNumLanes]);
+  hn::VFromD<D> o2, o14, o10, o6;
+  ButterflyVec(int_tag, cospi[8], cospi[56], b1[7], b1[4], o2, o14, cos_bit,
+               round);
+  ButterflyVec(int_tag, cospi[40], cospi[24], b1[6], b1[5], o10, o6, cos_bit,
+               round);
+  hn::Store(o2, int_tag, &in[2 * instride]);
+  hn::Store(o14, int_tag, &in[14 * instride]);
+  hn::Store(o10, int_tag, &in[10 * instride]);
+  hn::Store(o6, int_tag, &in[6 * instride]);
+
+  AddSubVec(int_tag, b0[8], b1[9], b0[8], b0[9]);
+  AddSubVec(int_tag, b0[11], b1[10], b0[11], b0[10]);
+  AddSubVec(int_tag, b0[12], b1[13], b0[12], b0[13]);
+  AddSubVec(int_tag, b0[15], b1[14], b0[15], b0[14]);

   // stage 6
-  Butterfly(int_tag, cospi[4], cospi[60], &buf0[15 * kNumLanes],
-            &buf0[8 * kNumLanes], &in[1 * instride], &in[15 * instride],
-            cos_bit, round);
-  Butterfly(int_tag, cospi[36], cospi[28], &buf0[14 * kNumLanes],
-            &buf0[9 * kNumLanes], &in[9 * instride], &in[7 * instride], cos_bit,
-            round);
-  Butterfly(int_tag, cospi[20], cospi[44], &buf0[13 * kNumLanes],
-            &buf0[10 * kNumLanes], &in[5 * instride], &in[11 * instride],
-            cos_bit, round);
-  Butterfly(int_tag, cospi[52], cospi[12], &buf0[12 * kNumLanes],
-            &buf0[11 * kNumLanes], &in[13 * instride], &in[3 * instride],
-            cos_bit, round);
+  hn::VFromD<D> o1, o15, o9, o7, o5, o11, o13, o3;
+  ButterflyVec(int_tag, cospi[4], cospi[60], b0[15], b0[8], o1, o15, cos_bit,
+               round);
+  ButterflyVec(int_tag, cospi[36], cospi[28], b0[14], b0[9], o9, o7, cos_bit,
+               round);
+  ButterflyVec(int_tag, cospi[20], cospi[44], b0[13], b0[10], o5, o11, cos_bit,
+               round);
+  ButterflyVec(int_tag, cospi[52], cospi[12], b0[12], b0[11], o13, o3, cos_bit,
+               round);
+  hn::Store(o1, int_tag, &in[1 * instride]);
+  hn::Store(o15, int_tag, &in[15 * instride]);
+  hn::Store(o9, int_tag, &in[9 * instride]);
+  hn::Store(o7, int_tag, &in[7 * instride]);
+  hn::Store(o5, int_tag, &in[5 * instride]);
+  hn::Store(o11, int_tag, &in[11 * instride]);
+  hn::Store(o13, int_tag, &in[13 * instride]);
+  hn::Store(o3, int_tag, &in[3 * instride]);
 }

 template <typename D>
@@ -1374,277 +1354,194 @@ HWY_ATTR HWY_INLINE void Fadst4(D int_tag, hn::TFromD<D> *HWY_RESTRICT in,
                                                               instride);
 }

-template <size_t Width, typename D>
+template <typename D>
 HWY_ATTR HWY_INLINE void Fadst8(D int_tag, hn::TFromD<D> *HWY_RESTRICT in,
                                 const int8_t cos_bit, const size_t instride) {
-  constexpr size_t kNumLanes = hn::MaxLanes(int_tag);
-  constexpr size_t kNumBytes = kNumLanes * sizeof(hn::TFromD<D>);
-  HWY_ALIGN_MAX hn::TFromD<D> buf0[8 * kNumLanes];
-  HWY_ALIGN_MAX hn::TFromD<D> buf1[8 * kNumLanes];
   const int32_t *HWY_RESTRICT cospi = cospi_arr(cos_bit);
   const auto round = hn::Set(hn::Repartition<int32_t, D>(), 1 << (cos_bit - 1));
+  hn::VFromD<D> b0[8], b1[8];

   // stage 0
   // stage 1
-  hn::Store(hn::Load(int_tag, &in[0 * instride]), int_tag,
-            &buf0[0 * kNumLanes]);
-  hn::Store(hn::Neg(hn::Load(int_tag, &in[7 * instride])), int_tag,
-            &buf0[1 * kNumLanes]);
-  hn::Store(hn::Neg(hn::Load(int_tag, &in[3 * instride])), int_tag,
-            &buf0[2 * kNumLanes]);
-  hn::Store(hn::Load(int_tag, &in[4 * instride]), int_tag,
-            &buf0[3 * kNumLanes]);
-  hn::Store(hn::Neg(hn::Load(int_tag, &in[1 * instride])), int_tag,
-            &buf0[4 * kNumLanes]);
-  hn::Store(hn::Load(int_tag, &in[6 * instride]), int_tag,
-            &buf0[5 * kNumLanes]);
-  hn::Store(hn::Load(int_tag, &in[2 * instride]), int_tag,
-            &buf0[6 * kNumLanes]);
-  hn::Store(hn::Neg(hn::Load(int_tag, &in[5 * instride])), int_tag,
-            &buf0[7 * kNumLanes]);
+  b0[0] = hn::Load(int_tag, &in[0 * instride]);
+  b0[1] = hn::Neg(hn::Load(int_tag, &in[7 * instride]));
+  b0[2] = hn::Neg(hn::Load(int_tag, &in[3 * instride]));
+  b0[3] = hn::Load(int_tag, &in[4 * instride]);
+  b0[4] = hn::Neg(hn::Load(int_tag, &in[1 * instride]));
+  b0[5] = hn::Load(int_tag, &in[6 * instride]);
+  b0[6] = hn::Load(int_tag, &in[2 * instride]);
+  b0[7] = hn::Neg(hn::Load(int_tag, &in[5 * instride]));

   // stage 2
-  hwy::CopyBytes<2 * kNumBytes>(&buf0[0 * kNumLanes], &buf1[0 * kNumLanes]);
-  Butterfly(int_tag, cospi[32], cospi[32], &buf0[2 * kNumLanes],
-            &buf0[3 * kNumLanes], &buf1[2 * kNumLanes], &buf1[3 * kNumLanes],
-            cos_bit, round);
-  hwy::CopyBytes<2 * kNumBytes>(&buf0[4 * kNumLanes], &buf1[4 * kNumLanes]);
-  Butterfly(int_tag, cospi[32], cospi[32], &buf0[6 * kNumLanes],
-            &buf0[7 * kNumLanes], &buf1[6 * kNumLanes], &buf1[7 * kNumLanes],
-            cos_bit, round);
+  b1[0] = b0[0];
+  b1[1] = b0[1];
+  ButterflyVec(int_tag, cospi[32], cospi[32], b0[2], b0[3], b1[2], b1[3],
+               cos_bit, round);
+  b1[4] = b0[4];
+  b1[5] = b0[5];
+  ButterflyVec(int_tag, cospi[32], cospi[32], b0[6], b0[7], b1[6], b1[7],
+               cos_bit, round);

   // stage 3
   for (size_t j = 0; j < 8; j += 4) {
     for (size_t i = 0; i < 2; ++i) {
-      AddSub(int_tag, &buf1[(0 + i + j) * kNumLanes],
-             &buf1[(2 + i + j) * kNumLanes], &buf0[(0 + i + j) * kNumLanes],
-             &buf0[(2 + i + j) * kNumLanes]);
+      AddSubVec(int_tag, b1[0 + i + j], b1[2 + i + j], b0[0 + i + j],
+                b0[2 + i + j]);
     }
   }

   // stage 4
-  hwy::CopyBytes<4 * kNumBytes>(&buf0[0 * kNumLanes], &buf1[0 * kNumLanes]);
-  HalfButterfly(int_tag, cospi[16], cospi[48], &buf0[4 * kNumLanes],
-                &buf0[5 * kNumLanes], &buf1[4 * kNumLanes], cos_bit, round);
-  HalfButterfly(int_tag, cospi[48], -cospi[16], &buf0[4 * kNumLanes],
-                &buf0[5 * kNumLanes], &buf1[5 * kNumLanes], cos_bit, round);
-  HalfButterfly(int_tag, -cospi[48], cospi[16], &buf0[6 * kNumLanes],
-                &buf0[7 * kNumLanes], &buf1[6 * kNumLanes], cos_bit, round);
-  HalfButterfly(int_tag, cospi[16], cospi[48], &buf0[6 * kNumLanes],
-                &buf0[7 * kNumLanes], &buf1[7 * kNumLanes], cos_bit, round);
+  for (size_t i = 0; i < 4; ++i) b1[i] = b0[i];
+  ButterflyVec(int_tag, cospi[16], cospi[48], b0[4], b0[5], b1[4], b1[5],
+               cos_bit, round);
+  ButterflyVec(int_tag, -cospi[48], cospi[16], b0[6], b0[7], b1[6], b1[7],
+               cos_bit, round);

   // stage 5
   for (size_t i = 0; i < 4; ++i) {
-    AddSub(int_tag, &buf1[(0 + i) * kNumLanes], &buf1[(4 + i) * kNumLanes],
-           &buf0[(0 + i) * kNumLanes], &buf0[(4 + i) * kNumLanes]);
-  }
-
-  // stage 6
-  HalfButterfly(int_tag, cospi[4], cospi[60], &buf0[0 * kNumLanes],
-                &buf0[1 * kNumLanes], &buf1[0 * kNumLanes], cos_bit, round);
-  HalfButterfly(int_tag, cospi[60], -cospi[4], &buf0[0 * kNumLanes],
-                &buf0[1 * kNumLanes], &buf1[1 * kNumLanes], cos_bit, round);
-  HalfButterfly(int_tag, cospi[20], cospi[44], &buf0[2 * kNumLanes],
-                &buf0[3 * kNumLanes], &buf1[2 * kNumLanes], cos_bit, round);
-  HalfButterfly(int_tag, cospi[44], -cospi[20], &buf0[2 * kNumLanes],
-                &buf0[3 * kNumLanes], &buf1[3 * kNumLanes], cos_bit, round);
-  HalfButterfly(int_tag, cospi[36], cospi[28], &buf0[4 * kNumLanes],
-                &buf0[5 * kNumLanes], &buf1[4 * kNumLanes], cos_bit, round);
-  HalfButterfly(int_tag, cospi[28], -cospi[36], &buf0[4 * kNumLanes],
-                &buf0[5 * kNumLanes], &buf1[5 * kNumLanes], cos_bit, round);
-  HalfButterfly(int_tag, cospi[52], cospi[12], &buf0[6 * kNumLanes],
-                &buf0[7 * kNumLanes], &buf1[6 * kNumLanes], cos_bit, round);
-  HalfButterfly(int_tag, cospi[12], -cospi[52], &buf0[6 * kNumLanes],
-                &buf0[7 * kNumLanes], &buf1[7 * kNumLanes], cos_bit, round);
-
-  // stage 7
-  hwy::CopyBytes<kNumBytes>(&buf1[1 * kNumLanes], &in[0 * instride]);
-  hwy::CopyBytes<kNumBytes>(&buf1[6 * kNumLanes], &in[1 * instride]);
-  hwy::CopyBytes<kNumBytes>(&buf1[3 * kNumLanes], &in[2 * instride]);
-  hwy::CopyBytes<kNumBytes>(&buf1[4 * kNumLanes], &in[3 * instride]);
-  hwy::CopyBytes<kNumBytes>(&buf1[5 * kNumLanes], &in[4 * instride]);
-  hwy::CopyBytes<kNumBytes>(&buf1[2 * kNumLanes], &in[5 * instride]);
-  hwy::CopyBytes<kNumBytes>(&buf1[7 * kNumLanes], &in[6 * instride]);
-  hwy::CopyBytes<kNumBytes>(&buf1[0 * kNumLanes], &in[7 * instride]);
+    AddSubVec(int_tag, b1[i], b1[4 + i], b0[i], b0[4 + i]);
+  }
+
+  // stage 6 & 7
+  ButterflyVec(int_tag, cospi[4], cospi[60], b0[0], b0[1], b1[0], b1[1],
+               cos_bit, round);
+  ButterflyVec(int_tag, cospi[20], cospi[44], b0[2], b0[3], b1[2], b1[3],
+               cos_bit, round);
+  ButterflyVec(int_tag, cospi[36], cospi[28], b0[4], b0[5], b1[4], b1[5],
+               cos_bit, round);
+  ButterflyVec(int_tag, cospi[52], cospi[12], b0[6], b0[7], b1[6], b1[7],
+               cos_bit, round);
+  hn::Store(b1[1], int_tag, &in[0 * instride]);
+  hn::Store(b1[6], int_tag, &in[1 * instride]);
+  hn::Store(b1[3], int_tag, &in[2 * instride]);
+  hn::Store(b1[4], int_tag, &in[3 * instride]);
+  hn::Store(b1[5], int_tag, &in[4 * instride]);
+  hn::Store(b1[2], int_tag, &in[5 * instride]);
+  hn::Store(b1[7], int_tag, &in[6 * instride]);
+  hn::Store(b1[0], int_tag, &in[7 * instride]);
 }

-template <size_t Width, typename D>
+template <typename D>
 HWY_ATTR HWY_INLINE void Fadst16(D int_tag, hn::TFromD<D> *HWY_RESTRICT in,
                                  const int8_t cos_bit, const size_t instride) {
-  constexpr size_t kNumLanes = hn::MaxLanes(int_tag);
-  constexpr size_t kNumBytes = kNumLanes * sizeof(hn::TFromD<D>);
-  HWY_ALIGN_MAX hn::TFromD<D> buf0[16 * kNumLanes];
-  HWY_ALIGN_MAX hn::TFromD<D> buf1[16 * kNumLanes];
   const int32_t *HWY_RESTRICT const cospi = cospi_arr(cos_bit);
   const auto round = hn::Set(hn::Repartition<int32_t, D>(), 1 << (cos_bit - 1));
+  hn::VFromD<D> b0[16], b1[16];

   // stage 0
   // stage 1
-  hn::Store(hn::Load(int_tag, &in[0 * instride]), int_tag,
-            &buf0[0 * kNumLanes]);
-  hn::Store(hn::Neg(hn::Load(int_tag, &in[15 * instride])), int_tag,
-            &buf0[1 * kNumLanes]);
-  hn::Store(hn::Neg(hn::Load(int_tag, &in[7 * instride])), int_tag,
-            &buf0[2 * kNumLanes]);
-  hn::Store(hn::Load(int_tag, &in[8 * instride]), int_tag,
-            &buf0[3 * kNumLanes]);
-  hn::Store(hn::Neg(hn::Load(int_tag, &in[3 * instride])), int_tag,
-            &buf0[4 * kNumLanes]);
-  hn::Store(hn::Load(int_tag, &in[12 * instride]), int_tag,
-            &buf0[5 * kNumLanes]);
-  hn::Store(hn::Load(int_tag, &in[4 * instride]), int_tag,
-            &buf0[6 * kNumLanes]);
-  hn::Store(hn::Neg(hn::Load(int_tag, &in[11 * instride])), int_tag,
-            &buf0[7 * kNumLanes]);
-  hn::Store(hn::Neg(hn::Load(int_tag, &in[1 * instride])), int_tag,
-            &buf0[8 * kNumLanes]);
-  hn::Store(hn::Load(int_tag, &in[14 * instride]), int_tag,
-            &buf0[9 * kNumLanes]);
-  hn::Store(hn::Load(int_tag, &in[6 * instride]), int_tag,
-            &buf0[10 * kNumLanes]);
-  hn::Store(hn::Neg(hn::Load(int_tag, &in[9 * instride])), int_tag,
-            &buf0[11 * kNumLanes]);
-  hn::Store(hn::Load(int_tag, &in[2 * instride]), int_tag,
-            &buf0[12 * kNumLanes]);
-  hn::Store(hn::Neg(hn::Load(int_tag, &in[13 * instride])), int_tag,
-            &buf0[13 * kNumLanes]);
-  hn::Store(hn::Neg(hn::Load(int_tag, &in[5 * instride])), int_tag,
-            &buf0[14 * kNumLanes]);
-  hn::Store(hn::Load(int_tag, &in[10 * instride]), int_tag,
-            &buf0[15 * kNumLanes]);
+  b0[0] = hn::Load(int_tag, &in[0 * instride]);
+  b0[1] = hn::Neg(hn::Load(int_tag, &in[15 * instride]));
+  b0[2] = hn::Neg(hn::Load(int_tag, &in[7 * instride]));
+  b0[3] = hn::Load(int_tag, &in[8 * instride]);
+  b0[4] = hn::Neg(hn::Load(int_tag, &in[3 * instride]));
+  b0[5] = hn::Load(int_tag, &in[12 * instride]);
+  b0[6] = hn::Load(int_tag, &in[4 * instride]);
+  b0[7] = hn::Neg(hn::Load(int_tag, &in[11 * instride]));
+  b0[8] = hn::Neg(hn::Load(int_tag, &in[1 * instride]));
+  b0[9] = hn::Load(int_tag, &in[14 * instride]);
+  b0[10] = hn::Load(int_tag, &in[6 * instride]);
+  b0[11] = hn::Neg(hn::Load(int_tag, &in[9 * instride]));
+  b0[12] = hn::Load(int_tag, &in[2 * instride]);
+  b0[13] = hn::Neg(hn::Load(int_tag, &in[13 * instride]));
+  b0[14] = hn::Neg(hn::Load(int_tag, &in[5 * instride]));
+  b0[15] = hn::Load(int_tag, &in[10 * instride]);

   // stage 2
-  hwy::CopyBytes<kNumBytes * 2>(&buf0[0 * kNumLanes], &buf1[0 * kNumLanes]);
-  Butterfly(int_tag, cospi[32], cospi[32], &buf0[2 * kNumLanes],
-            &buf0[3 * kNumLanes], &buf1[2 * kNumLanes], &buf1[3 * kNumLanes],
-            cos_bit, round);
-  hwy::CopyBytes<kNumBytes * 2>(&buf0[4 * kNumLanes], &buf1[4 * kNumLanes]);
-  Butterfly(int_tag, cospi[32], cospi[32], &buf0[6 * kNumLanes],
-            &buf0[7 * kNumLanes], &buf1[6 * kNumLanes], &buf1[7 * kNumLanes],
-            cos_bit, round);
-  hwy::CopyBytes<kNumBytes * 2>(&buf0[8 * kNumLanes], &buf1[8 * kNumLanes]);
-  Butterfly(int_tag, cospi[32], cospi[32], &buf0[10 * kNumLanes],
-            &buf0[11 * kNumLanes], &buf1[10 * kNumLanes], &buf1[11 * kNumLanes],
-            cos_bit, round);
-  hwy::CopyBytes<kNumBytes * 2>(&buf0[12 * kNumLanes], &buf1[12 * kNumLanes]);
-  Butterfly(int_tag, cospi[32], cospi[32], &buf0[14 * kNumLanes],
-            &buf0[15 * kNumLanes], &buf1[14 * kNumLanes], &buf1[15 * kNumLanes],
-            cos_bit, round);
+  b1[0] = b0[0];
+  b1[1] = b0[1];
+  ButterflyVec(int_tag, cospi[32], cospi[32], b0[2], b0[3], b1[2], b1[3],
+               cos_bit, round);
+  b1[4] = b0[4];
+  b1[5] = b0[5];
+  ButterflyVec(int_tag, cospi[32], cospi[32], b0[6], b0[7], b1[6], b1[7],
+               cos_bit, round);
+  b1[8] = b0[8];
+  b1[9] = b0[9];
+  ButterflyVec(int_tag, cospi[32], cospi[32], b0[10], b0[11], b1[10], b1[11],
+               cos_bit, round);
+  b1[12] = b0[12];
+  b1[13] = b0[13];
+  ButterflyVec(int_tag, cospi[32], cospi[32], b0[14], b0[15], b1[14], b1[15],
+               cos_bit, round);

   // stage 3
   for (size_t j = 0; j < 16; j += 4) {
     for (size_t i = 0; i < 2; ++i) {
-      AddSub(int_tag, &buf1[(0 + i + j) * kNumLanes],
-             &buf1[(2 + i + j) * kNumLanes], &buf0[(0 + i + j) * kNumLanes],
-             &buf0[(2 + i + j) * kNumLanes]);
+      AddSubVec(int_tag, b1[0 + i + j], b1[2 + i + j], b0[0 + i + j],
+                b0[2 + i + j]);
     }
   }

   // stage 4
-  hwy::CopyBytes<kNumBytes * 4>(&buf0[0 * kNumLanes], &buf1[0 * kNumLanes]);
-  HalfButterfly(int_tag, cospi[16], cospi[48], &buf0[4 * kNumLanes],
-                &buf0[5 * kNumLanes], &buf1[4 * kNumLanes], cos_bit, round);
-  HalfButterfly(int_tag, cospi[48], -cospi[16], &buf0[4 * kNumLanes],
-                &buf0[5 * kNumLanes], &buf1[5 * kNumLanes], cos_bit, round);
-  HalfButterfly(int_tag, -cospi[48], cospi[16], &buf0[6 * kNumLanes],
-                &buf0[7 * kNumLanes], &buf1[6 * kNumLanes], cos_bit, round);
-  HalfButterfly(int_tag, cospi[16], cospi[48], &buf0[6 * kNumLanes],
-                &buf0[7 * kNumLanes], &buf1[7 * kNumLanes], cos_bit, round);
-  hwy::CopyBytes<kNumBytes * 4>(&buf0[8 * kNumLanes], &buf1[8 * kNumLanes]);
-  HalfButterfly(int_tag, cospi[16], cospi[48], &buf0[12 * kNumLanes],
-                &buf0[13 * kNumLanes], &buf1[12 * kNumLanes], cos_bit, round);
-  HalfButterfly(int_tag, cospi[48], -cospi[16], &buf0[12 * kNumLanes],
-                &buf0[13 * kNumLanes], &buf1[13 * kNumLanes], cos_bit, round);
-  HalfButterfly(int_tag, -cospi[48], cospi[16], &buf0[14 * kNumLanes],
-                &buf0[15 * kNumLanes], &buf1[14 * kNumLanes], cos_bit, round);
-  HalfButterfly(int_tag, cospi[16], cospi[48], &buf0[14 * kNumLanes],
-                &buf0[15 * kNumLanes], &buf1[15 * kNumLanes], cos_bit, round);
+  for (size_t i = 0; i < 4; ++i) b1[i] = b0[i];
+  ButterflyVec(int_tag, cospi[16], cospi[48], b0[4], b0[5], b1[4], b1[5],
+               cos_bit, round);
+  ButterflyVec(int_tag, -cospi[48], cospi[16], b0[6], b0[7], b1[6], b1[7],
+               cos_bit, round);
+  for (size_t i = 8; i < 12; ++i) b1[i] = b0[i];
+  ButterflyVec(int_tag, cospi[16], cospi[48], b0[12], b0[13], b1[12], b1[13],
+               cos_bit, round);
+  ButterflyVec(int_tag, -cospi[48], cospi[16], b0[14], b0[15], b1[14], b1[15],
+               cos_bit, round);

   // stage 5
   for (size_t j = 0; j < 16; j += 8) {
     for (size_t i = 0; i < 4; ++i) {
-      AddSub(int_tag, &buf1[(0 + i + j) * kNumLanes],
-             &buf1[(4 + i + j) * kNumLanes], &buf0[(0 + i + j) * kNumLanes],
-             &buf0[(4 + i + j) * kNumLanes]);
+      AddSubVec(int_tag, b1[0 + i + j], b1[4 + i + j], b0[0 + i + j],
+                b0[4 + i + j]);
     }
   }

   // stage 6
-  hwy::CopyBytes<kNumBytes * 8>(&buf0[0 * kNumLanes], &buf1[0 * kNumLanes]);
-  HalfButterfly(int_tag, cospi[8], cospi[56], &buf0[8 * kNumLanes],
-                &buf0[9 * kNumLanes], &buf1[8 * kNumLanes], cos_bit, round);
-  HalfButterfly(int_tag, cospi[56], -cospi[8], &buf0[8 * kNumLanes],
-                &buf0[9 * kNumLanes], &buf1[9 * kNumLanes], cos_bit, round);
-  HalfButterfly(int_tag, cospi[40], cospi[24], &buf0[10 * kNumLanes],
-                &buf0[11 * kNumLanes], &buf1[10 * kNumLanes], cos_bit, round);
-  HalfButterfly(int_tag, cospi[24], -cospi[40], &buf0[10 * kNumLanes],
-                &buf0[11 * kNumLanes], &buf1[11 * kNumLanes], cos_bit, round);
-  HalfButterfly(int_tag, -cospi[56], cospi[8], &buf0[12 * kNumLanes],
-                &buf0[13 * kNumLanes], &buf1[12 * kNumLanes], cos_bit, round);
-  HalfButterfly(int_tag, cospi[8], cospi[56], &buf0[12 * kNumLanes],
-                &buf0[13 * kNumLanes], &buf1[13 * kNumLanes], cos_bit, round);
-  HalfButterfly(int_tag, -cospi[24], cospi[40], &buf0[14 * kNumLanes],
-                &buf0[15 * kNumLanes], &buf1[14 * kNumLanes], cos_bit, round);
-  HalfButterfly(int_tag, cospi[40], cospi[24], &buf0[14 * kNumLanes],
-                &buf0[15 * kNumLanes], &buf1[15 * kNumLanes], cos_bit, round);
+  for (size_t i = 0; i < 8; ++i) b1[i] = b0[i];
+  ButterflyVec(int_tag, cospi[8], cospi[56], b0[8], b0[9], b1[8], b1[9],
+               cos_bit, round);
+  ButterflyVec(int_tag, cospi[40], cospi[24], b0[10], b0[11], b1[10], b1[11],
+               cos_bit, round);
+  ButterflyVec(int_tag, -cospi[56], cospi[8], b0[12], b0[13], b1[12], b1[13],
+               cos_bit, round);
+  ButterflyVec(int_tag, -cospi[24], cospi[40], b0[14], b0[15], b1[14], b1[15],
+               cos_bit, round);

   // stage 7
   for (size_t i = 0; i < 8; ++i) {
-    AddSub(int_tag, &buf1[(0 + i) * kNumLanes], &buf1[(8 + i) * kNumLanes],
-           &buf0[(0 + i) * kNumLanes], &buf0[(8 + i) * kNumLanes]);
+    AddSubVec(int_tag, b1[i], b1[8 + i], b0[i], b0[8 + i]);
   }

-  // stage 8
-  HalfButterfly(int_tag, cospi[2], cospi[62], &buf0[0 * kNumLanes],
-                &buf0[1 * kNumLanes], &buf1[0 * kNumLanes], cos_bit, round);
-  HalfButterfly(int_tag, cospi[62], -cospi[2], &buf0[0 * kNumLanes],
-                &buf0[1 * kNumLanes], &buf1[1 * kNumLanes], cos_bit, round);
-  HalfButterfly(int_tag, cospi[10], cospi[54], &buf0[2 * kNumLanes],
-                &buf0[3 * kNumLanes], &buf1[2 * kNumLanes], cos_bit, round);
-  HalfButterfly(int_tag, cospi[54], -cospi[10], &buf0[2 * kNumLanes],
-                &buf0[3 * kNumLanes], &buf1[3 * kNumLanes], cos_bit, round);
-  HalfButterfly(int_tag, cospi[18], cospi[46], &buf0[4 * kNumLanes],
-                &buf0[5 * kNumLanes], &buf1[4 * kNumLanes], cos_bit, round);
-  HalfButterfly(int_tag, cospi[46], -cospi[18], &buf0[4 * kNumLanes],
-                &buf0[5 * kNumLanes], &buf1[5 * kNumLanes], cos_bit, round);
-  HalfButterfly(int_tag, cospi[26], cospi[38], &buf0[6 * kNumLanes],
-                &buf0[7 * kNumLanes], &buf1[6 * kNumLanes], cos_bit, round);
-  HalfButterfly(int_tag, cospi[38], -cospi[26], &buf0[6 * kNumLanes],
-                &buf0[7 * kNumLanes], &buf1[7 * kNumLanes], cos_bit, round);
-  HalfButterfly(int_tag, cospi[34], cospi[30], &buf0[8 * kNumLanes],
-                &buf0[9 * kNumLanes], &buf1[8 * kNumLanes], cos_bit, round);
-  HalfButterfly(int_tag, cospi[30], -cospi[34], &buf0[8 * kNumLanes],
-                &buf0[9 * kNumLanes], &buf1[9 * kNumLanes], cos_bit, round);
-  HalfButterfly(int_tag, cospi[42], cospi[22], &buf0[10 * kNumLanes],
-                &buf0[11 * kNumLanes], &buf1[10 * kNumLanes], cos_bit, round);
-  HalfButterfly(int_tag, cospi[22], -cospi[42], &buf0[10 * kNumLanes],
-                &buf0[11 * kNumLanes], &buf1[11 * kNumLanes], cos_bit, round);
-  HalfButterfly(int_tag, cospi[50], cospi[14], &buf0[12 * kNumLanes],
-                &buf0[13 * kNumLanes], &buf1[12 * kNumLanes], cos_bit, round);
-  HalfButterfly(int_tag, cospi[14], -cospi[50], &buf0[12 * kNumLanes],
-                &buf0[13 * kNumLanes], &buf1[13 * kNumLanes], cos_bit, round);
-  HalfButterfly(int_tag, cospi[58], cospi[6], &buf0[14 * kNumLanes],
-                &buf0[15 * kNumLanes], &buf1[14 * kNumLanes], cos_bit, round);
-  HalfButterfly(int_tag, cospi[6], -cospi[58], &buf0[14 * kNumLanes],
-                &buf0[15 * kNumLanes], &buf1[15 * kNumLanes], cos_bit, round);
-
-  // stage 9
-  hwy::CopyBytes<kNumBytes>(&buf1[1 * kNumLanes], &in[0 * instride]);
-  hwy::CopyBytes<kNumBytes>(&buf1[14 * kNumLanes], &in[1 * instride]);
-  hwy::CopyBytes<kNumBytes>(&buf1[3 * kNumLanes], &in[2 * instride]);
-  hwy::CopyBytes<kNumBytes>(&buf1[12 * kNumLanes], &in[3 * instride]);
-  hwy::CopyBytes<kNumBytes>(&buf1[5 * kNumLanes], &in[4 * instride]);
-  hwy::CopyBytes<kNumBytes>(&buf1[10 * kNumLanes], &in[5 * instride]);
-  hwy::CopyBytes<kNumBytes>(&buf1[7 * kNumLanes], &in[6 * instride]);
-  hwy::CopyBytes<kNumBytes>(&buf1[8 * kNumLanes], &in[7 * instride]);
-  hwy::CopyBytes<kNumBytes>(&buf1[9 * kNumLanes], &in[8 * instride]);
-  hwy::CopyBytes<kNumBytes>(&buf1[6 * kNumLanes], &in[9 * instride]);
-  hwy::CopyBytes<kNumBytes>(&buf1[11 * kNumLanes], &in[10 * instride]);
-  hwy::CopyBytes<kNumBytes>(&buf1[4 * kNumLanes], &in[11 * instride]);
-  hwy::CopyBytes<kNumBytes>(&buf1[13 * kNumLanes], &in[12 * instride]);
-  hwy::CopyBytes<kNumBytes>(&buf1[2 * kNumLanes], &in[13 * instride]);
-  hwy::CopyBytes<kNumBytes>(&buf1[15 * kNumLanes], &in[14 * instride]);
-  hwy::CopyBytes<kNumBytes>(&buf1[0 * kNumLanes], &in[15 * instride]);
+  // stage 8 & 9
+  ButterflyVec(int_tag, cospi[2], cospi[62], b0[0], b0[1], b1[0], b1[1],
+               cos_bit, round);
+  ButterflyVec(int_tag, cospi[10], cospi[54], b0[2], b0[3], b1[2], b1[3],
+               cos_bit, round);
+  ButterflyVec(int_tag, cospi[18], cospi[46], b0[4], b0[5], b1[4], b1[5],
+               cos_bit, round);
+  ButterflyVec(int_tag, cospi[26], cospi[38], b0[6], b0[7], b1[6], b1[7],
+               cos_bit, round);
+  ButterflyVec(int_tag, cospi[34], cospi[30], b0[8], b0[9], b1[8], b1[9],
+               cos_bit, round);
+  ButterflyVec(int_tag, cospi[42], cospi[22], b0[10], b0[11], b1[10], b1[11],
+               cos_bit, round);
+  ButterflyVec(int_tag, cospi[50], cospi[14], b0[12], b0[13], b1[12], b1[13],
+               cos_bit, round);
+  ButterflyVec(int_tag, cospi[58], cospi[6], b0[14], b0[15], b1[14], b1[15],
+               cos_bit, round);
+  hn::Store(b1[1], int_tag, &in[0 * instride]);
+  hn::Store(b1[14], int_tag, &in[1 * instride]);
+  hn::Store(b1[3], int_tag, &in[2 * instride]);
+  hn::Store(b1[12], int_tag, &in[3 * instride]);
+  hn::Store(b1[5], int_tag, &in[4 * instride]);
+  hn::Store(b1[10], int_tag, &in[5 * instride]);
+  hn::Store(b1[7], int_tag, &in[6 * instride]);
+  hn::Store(b1[8], int_tag, &in[7 * instride]);
+  hn::Store(b1[9], int_tag, &in[8 * instride]);
+  hn::Store(b1[6], int_tag, &in[9 * instride]);
+  hn::Store(b1[11], int_tag, &in[10 * instride]);
+  hn::Store(b1[4], int_tag, &in[11 * instride]);
+  hn::Store(b1[13], int_tag, &in[12 * instride]);
+  hn::Store(b1[2], int_tag, &in[13 * instride]);
+  hn::Store(b1[15], int_tag, &in[14 * instride]);
+  hn::Store(b1[0], int_tag, &in[15 * instride]);
 }

 template <size_t Width, typename D>
@@ -1729,7 +1626,7 @@ HWY_ATTR HWY_INLINE void IdtxSqrt2(D tag, hn::TFromD<D> *HWY_RESTRICT in) {
 }

 template <size_t Width, size_t Stride, typename T>
-HWY_ATTR void FdctNx4Block(T *HWY_RESTRICT in, int8_t cos_bit) {
+HWY_ATTR HWY_INLINE void FdctNx4Block(T *HWY_RESTRICT in, int8_t cos_bit) {
   constexpr auto int_tag = hn::CappedTag<T, Width>();
   for (size_t i = 0; i < Width; i += hn::MaxLanes(int_tag)) {
     Fdct4(int_tag, &in[i], cos_bit, Stride);
@@ -1737,23 +1634,27 @@ HWY_ATTR void FdctNx4Block(T *HWY_RESTRICT in, int8_t cos_bit) {
 }

 template <size_t Width, size_t Stride, typename T>
-HWY_ATTR void FdctNx8Block(T *HWY_RESTRICT in, int8_t cos_bit) {
+HWY_ATTR HWY_INLINE void FdctNx8Block(T *HWY_RESTRICT in, int8_t cos_bit) {
   constexpr auto int_tag = hn::CappedTag<T, Stride>();
   for (size_t i = 0; i < Width; i += hn::MaxLanes(int_tag)) {
     Fdct8(int_tag, &in[i], cos_bit, Stride);
   }
 }

+// Disable inlining of 16-point 1D FDCT to avoid I-cache and stack frame
+// growth in 2D transform callers.
 template <size_t Width, size_t Stride, typename T>
-HWY_ATTR void FdctNx16Block(T *HWY_RESTRICT in, int8_t cos_bit) {
+HWY_ATTR HWY_NOINLINE void FdctNx16Block(T *HWY_RESTRICT in, int8_t cos_bit) {
   constexpr auto int_tag = hn::CappedTag<T, Stride>();
   for (size_t i = 0; i < Width; i += hn::MaxLanes(int_tag)) {
     Fdct16(int_tag, &in[i], cos_bit, Stride);
   }
 }

+// Disable inlining of 32-point 1D FDCT to avoid I-cache and stack frame
+// growth in 2D transform callers.
 template <size_t Width, size_t Stride, typename T>
-HWY_ATTR void FdctNx32Block(T *HWY_RESTRICT in, int8_t cos_bit) {
+HWY_ATTR HWY_NOINLINE void FdctNx32Block(T *HWY_RESTRICT in, int8_t cos_bit) {
   constexpr auto int_tag = hn::CappedTag<T, Stride>();
   for (size_t i = 0; i < Width; i += hn::MaxLanes(int_tag)) {
     Fdct32(int_tag, &in[i], cos_bit, Stride);
@@ -1778,23 +1679,25 @@ HWY_ATTR HWY_INLINE void FadstNx4Block(T *HWY_RESTRICT in, int8_t cos_bit) {
 }

 template <size_t Width, size_t Stride, typename T>
-HWY_ATTR void FadstNx8Block(T *HWY_RESTRICT in, int8_t cos_bit) {
+HWY_ATTR HWY_INLINE void FadstNx8Block(T *HWY_RESTRICT in, int8_t cos_bit) {
   constexpr auto int_tag = hn::CappedTag<T, Stride>();
   for (size_t i = 0; i < Width; i += hn::MaxLanes(int_tag)) {
-    Fadst8<Width>(int_tag, &in[i], cos_bit, Stride);
+    Fadst8(int_tag, &in[i], cos_bit, Stride);
   }
 }

+// Disable inlining of 16-point 1D FADST to avoid I-cache and stack frame
+// growth in 2D transform callers.
 template <size_t Width, size_t Stride, typename T>
-HWY_ATTR void FadstNx16Block(T *HWY_RESTRICT in, int8_t cos_bit) {
+HWY_ATTR HWY_NOINLINE void FadstNx16Block(T *HWY_RESTRICT in, int8_t cos_bit) {
   constexpr auto int_tag = hn::CappedTag<T, Stride>();
   for (size_t i = 0; i < Width; i += hn::MaxLanes(int_tag)) {
-    Fadst16<Width>(int_tag, &in[i], cos_bit, Stride);
+    Fadst16(int_tag, &in[i], cos_bit, Stride);
   }
 }

 template <size_t Width, size_t Stride, size_t BlockHeight, typename T>
-HWY_ATTR void IdtxAdd2Block(T *HWY_RESTRICT in, int8_t cos_bit) {
+HWY_ATTR HWY_INLINE void IdtxAdd2Block(T *HWY_RESTRICT in, int8_t cos_bit) {
   (void)cos_bit;
   constexpr auto int_tag = hn::CappedTag<T, Width>();
   for (size_t y = 0; y < BlockHeight; ++y) {
@@ -1804,7 +1707,7 @@ HWY_ATTR void IdtxAdd2Block(T *HWY_RESTRICT in, int8_t cos_bit) {

 template <size_t Width, size_t Stride, size_t BlockHeight, int Scale,
           typename T>
-HWY_ATTR void IdtxSqrt2Block(T *HWY_RESTRICT in, int8_t cos_bit) {
+HWY_ATTR HWY_INLINE void IdtxSqrt2Block(T *HWY_RESTRICT in, int8_t cos_bit) {
   (void)cos_bit;
   constexpr auto int_tag = hn::CappedTag<T, Width>();
   for (size_t y = 0; y < BlockHeight; ++y) {
@@ -1814,7 +1717,7 @@ HWY_ATTR void IdtxSqrt2Block(T *HWY_RESTRICT in, int8_t cos_bit) {

 template <size_t Width, size_t Stride, size_t BlockHeight, int Shift,
           typename T>
-HWY_ATTR void IdtxShiftBlock(T *HWY_RESTRICT in, int8_t cos_bit) {
+HWY_ATTR HWY_INLINE void IdtxShiftBlock(T *HWY_RESTRICT in, int8_t cos_bit) {
   (void)cos_bit;
   constexpr auto int_tag = hn::CappedTag<T, Width>();
   for (size_t y = 0; y < BlockHeight; ++y) {
@@ -1822,16 +1725,6 @@ HWY_ATTR void IdtxShiftBlock(T *HWY_RESTRICT in, int8_t cos_bit) {
   }
 }

-template <typename T>
-void TransformFail(T *in, int8_t cos_bit) {
-  (void)in;
-  (void)cos_bit;
-  assert(false && "Incorrect transform requested.");
-}
-
-template <typename T>
-using Transform1D = void (*)(T *in, int8_t cos_bit);
-
 template <bool PositiveOrZero>
 struct RoundShiftTraits {};

@@ -1939,78 +1832,169 @@ HWY_ATTR HWY_INLINE void Transpose4(const TIn *HWY_RESTRICT in,
   }
 }

+template <size_t LaneSize>
+struct Transpose8StoreTraits;
+
+template <>
+struct Transpose8StoreTraits<2> {
+  template <typename D, typename TOut>
+  HWY_ATTR HWY_INLINE static void Store(
+      D out_tag, const hn::VFromD<D> &t0, const hn::VFromD<D> &t1,
+      const hn::VFromD<D> &t2, const hn::VFromD<D> &t3, const hn::VFromD<D> &t4,
+      const hn::VFromD<D> &t5, const hn::VFromD<D> &t6, const hn::VFromD<D> &t7,
+      TOut *HWY_RESTRICT out, size_t outstride) {
+    constexpr hn::RepartitionToWide<D> int32_tag;
+    constexpr hn::RepartitionToWide<decltype(int32_tag)> int64_tag;
+    const auto u0 = hn::InterleaveLower(int32_tag, hn::BitCast(int32_tag, t0),
+                                        hn::BitCast(int32_tag, t2));
+    const auto u1 = hn::InterleaveUpper(int32_tag, hn::BitCast(int32_tag, t0),
+                                        hn::BitCast(int32_tag, t2));
+    const auto u2 = hn::InterleaveLower(int32_tag, hn::BitCast(int32_tag, t1),
+                                        hn::BitCast(int32_tag, t3));
+    const auto u3 = hn::InterleaveUpper(int32_tag, hn::BitCast(int32_tag, t1),
+                                        hn::BitCast(int32_tag, t3));
+    const auto u4 = hn::InterleaveLower(int32_tag, hn::BitCast(int32_tag, t4),
+                                        hn::BitCast(int32_tag, t6));
+    const auto u5 = hn::InterleaveUpper(int32_tag, hn::BitCast(int32_tag, t4),
+                                        hn::BitCast(int32_tag, t6));
+    const auto u6 = hn::InterleaveLower(int32_tag, hn::BitCast(int32_tag, t5),
+                                        hn::BitCast(int32_tag, t7));
+    const auto u7 = hn::InterleaveUpper(int32_tag, hn::BitCast(int32_tag, t5),
+                                        hn::BitCast(int32_tag, t7));
+    hn::StoreU(hn::BitCast(out_tag, hn::InterleaveLower(
+                                        int64_tag, hn::BitCast(int64_tag, u0),
+                                        hn::BitCast(int64_tag, u4))),
+               out_tag, &out[0 * outstride]);
+    hn::StoreU(hn::BitCast(out_tag, hn::InterleaveUpper(
+                                        int64_tag, hn::BitCast(int64_tag, u0),
+                                        hn::BitCast(int64_tag, u4))),
+               out_tag, &out[1 * outstride]);
+    hn::StoreU(hn::BitCast(out_tag, hn::InterleaveLower(
+                                        int64_tag, hn::BitCast(int64_tag, u1),
+                                        hn::BitCast(int64_tag, u5))),
+               out_tag, &out[2 * outstride]);
+    hn::StoreU(hn::BitCast(out_tag, hn::InterleaveUpper(
+                                        int64_tag, hn::BitCast(int64_tag, u1),
+                                        hn::BitCast(int64_tag, u5))),
+               out_tag, &out[3 * outstride]);
+    hn::StoreU(hn::BitCast(out_tag, hn::InterleaveLower(
+                                        int64_tag, hn::BitCast(int64_tag, u2),
+                                        hn::BitCast(int64_tag, u6))),
+               out_tag, &out[4 * outstride]);
+    hn::StoreU(hn::BitCast(out_tag, hn::InterleaveUpper(
+                                        int64_tag, hn::BitCast(int64_tag, u2),
+                                        hn::BitCast(int64_tag, u6))),
+               out_tag, &out[5 * outstride]);
+    hn::StoreU(hn::BitCast(out_tag, hn::InterleaveLower(
+                                        int64_tag, hn::BitCast(int64_tag, u3),
+                                        hn::BitCast(int64_tag, u7))),
+               out_tag, &out[6 * outstride]);
+    hn::StoreU(hn::BitCast(out_tag, hn::InterleaveUpper(
+                                        int64_tag, hn::BitCast(int64_tag, u3),
+                                        hn::BitCast(int64_tag, u7))),
+               out_tag, &out[7 * outstride]);
+  }
+};
+
+template <>
+struct Transpose8StoreTraits<4> {
+  template <typename D, typename TOut>
+  HWY_ATTR HWY_INLINE static void Store(
+      D out_tag, const hn::VFromD<D> &t0, const hn::VFromD<D> &t1,
+      const hn::VFromD<D> &t2, const hn::VFromD<D> &t3, const hn::VFromD<D> &t4,
+      const hn::VFromD<D> &t5, const hn::VFromD<D> &t6, const hn::VFromD<D> &t7,
+      TOut *HWY_RESTRICT out, size_t outstride) {
+    constexpr hn::RepartitionToWide<D> int64_tag;
+    const auto u0 = hn::InterleaveLower(int64_tag, hn::BitCast(int64_tag, t0),
+                                        hn::BitCast(int64_tag, t2));
+    const auto u1 = hn::InterleaveUpper(int64_tag, hn::BitCast(int64_tag, t0),
+                                        hn::BitCast(int64_tag, t2));
+    const auto u2 = hn::InterleaveLower(int64_tag, hn::BitCast(int64_tag, t1),
+                                        hn::BitCast(int64_tag, t3));
+    const auto u3 = hn::InterleaveUpper(int64_tag, hn::BitCast(int64_tag, t1),
+                                        hn::BitCast(int64_tag, t3));
+    const auto u4 = hn::InterleaveLower(int64_tag, hn::BitCast(int64_tag, t4),
+                                        hn::BitCast(int64_tag, t6));
+    const auto u5 = hn::InterleaveUpper(int64_tag, hn::BitCast(int64_tag, t4),
+                                        hn::BitCast(int64_tag, t6));
+    const auto u6 = hn::InterleaveLower(int64_tag, hn::BitCast(int64_tag, t5),
+                                        hn::BitCast(int64_tag, t7));
+    const auto u7 = hn::InterleaveUpper(int64_tag, hn::BitCast(int64_tag, t5),
+                                        hn::BitCast(int64_tag, t7));
+    hn::StoreU(hn::BitCast(out_tag, hn::ConcatLowerLower(int64_tag, u4, u0)),
+               out_tag, &out[0 * outstride]);
+    hn::StoreU(hn::BitCast(out_tag, hn::ConcatLowerLower(int64_tag, u5, u1)),
+               out_tag, &out[1 * outstride]);
+    hn::StoreU(hn::BitCast(out_tag, hn::ConcatLowerLower(int64_tag, u6, u2)),
+               out_tag, &out[2 * outstride]);
+    hn::StoreU(hn::BitCast(out_tag, hn::ConcatLowerLower(int64_tag, u7, u3)),
+               out_tag, &out[3 * outstride]);
+    hn::StoreU(hn::BitCast(out_tag, hn::ConcatUpperUpper(int64_tag, u4, u0)),
+               out_tag, &out[4 * outstride]);
+    hn::StoreU(hn::BitCast(out_tag, hn::ConcatUpperUpper(int64_tag, u5, u1)),
+               out_tag, &out[5 * outstride]);
+    hn::StoreU(hn::BitCast(out_tag, hn::ConcatUpperUpper(int64_tag, u6, u2)),
+               out_tag, &out[6 * outstride]);
+    hn::StoreU(hn::BitCast(out_tag, hn::ConcatUpperUpper(int64_tag, u7, u3)),
+               out_tag, &out[7 * outstride]);
+  }
+};
+
 template <int8_t Bit, bool ApplyRectScale, typename TIn, typename TOut>
 HWY_ATTR HWY_INLINE void Transpose8(const TIn *HWY_RESTRICT in,
                                     TOut *HWY_RESTRICT out, size_t instride,
                                     size_t outstride) {
   constexpr hn::FixedTag<TIn, 8> int_tag;
   constexpr hn::Rebind<TOut, decltype(int_tag)> out_tag;
-  // N.B. there isn't a StoreInterleaved8, so hand-code Transpose8.
-  constexpr hn::RepartitionToWide<decltype(out_tag)> wide_int_tag;
-  HWY_ALIGN_MAX hn::TFromD<decltype(wide_int_tag)> interleaved0[16];
-  HWY_ALIGN_MAX hn::TFromD<decltype(wide_int_tag)> interleaved1[16];
-  auto i0 = hn::Load(int_tag, &in[0 * instride]);
-  auto i1 = hn::Load(int_tag, &in[1 * instride]);
-  auto i2 = hn::Load(int_tag, &in[2 * instride]);
-  auto i3 = hn::Load(int_tag, &in[3 * instride]);
-  auto i4 = hn::Load(int_tag, &in[4 * instride]);
-  auto i5 = hn::Load(int_tag, &in[5 * instride]);
-  auto i6 = hn::Load(int_tag, &in[6 * instride]);
-  auto i7 = hn::Load(int_tag, &in[7 * instride]);
-  auto s0 = hn::Undefined(out_tag);
-  auto s1 = hn::Undefined(out_tag);
-  auto s2 = hn::Undefined(out_tag);
-  auto s3 = hn::Undefined(out_tag);
-  auto s4 = hn::Undefined(out_tag);
-  auto s5 = hn::Undefined(out_tag);
-  auto s6 = hn::Undefined(out_tag);
-  auto s7 = hn::Undefined(out_tag);
-  auto ip0 = MaybePromoteTo(out_tag, i0);
-  auto ip1 = MaybePromoteTo(out_tag, i1);
-  auto ip2 = MaybePromoteTo(out_tag, i2);
-  auto ip3 = MaybePromoteTo(out_tag, i3);
-  auto ip4 = MaybePromoteTo(out_tag, i4);
-  auto ip5 = MaybePromoteTo(out_tag, i5);
-  auto ip6 = MaybePromoteTo(out_tag, i6);
-  auto ip7 = MaybePromoteTo(out_tag, i7);
-  s0 = RectScale<ApplyRectScale>(out_tag, RoundShift<Bit>(out_tag, ip0));
-  s1 = RectScale<ApplyRectScale>(out_tag, RoundShift<Bit>(out_tag, ip1));
-  s2 = RectScale<ApplyRectScale>(out_tag, RoundShift<Bit>(out_tag, ip2));
-  s3 = RectScale<ApplyRectScale>(out_tag, RoundShift<Bit>(out_tag, ip3));
-  s4 = RectScale<ApplyRectScale>(out_tag, RoundShift<Bit>(out_tag, ip4));
-  s5 = RectScale<ApplyRectScale>(out_tag, RoundShift<Bit>(out_tag, ip5));
-  s6 = RectScale<ApplyRectScale>(out_tag, RoundShift<Bit>(out_tag, ip6));
-  s7 = RectScale<ApplyRectScale>(out_tag, RoundShift<Bit>(out_tag, ip7));
-  auto u0 = hn::ZipLower(wide_int_tag, s0, s1);
-  auto u1 = hn::ZipUpper(wide_int_tag, s0, s1);
-  auto u2 = hn::ZipLower(wide_int_tag, s2, s3);
-  auto u3 = hn::ZipUpper(wide_int_tag, s2, s3);
-  auto u4 = hn::ZipLower(wide_int_tag, s4, s5);
-  auto u5 = hn::ZipUpper(wide_int_tag, s4, s5);
-  auto u6 = hn::ZipLower(wide_int_tag, s6, s7);
-  auto u7 = hn::ZipUpper(wide_int_tag, s6, s7);
-  hn::StoreInterleaved4(u0, u2, u4, u6, wide_int_tag, interleaved0);
-  hn::StoreInterleaved4(u1, u3, u5, u7, wide_int_tag, interleaved1);
-  constexpr size_t kNumBytes = hn::MaxLanes(int_tag) * sizeof(*out);
-  if CONSTEXPR_IF (sizeof(TOut) == 2) {
-    hwy::CopyBytes<kNumBytes>(&interleaved0[0], &out[0 * outstride]);
-    hwy::CopyBytes<kNumBytes>(&interleaved0[4], &out[1 * outstride]);
-    hwy::CopyBytes<kNumBytes>(&interleaved0[8], &out[2 * outstride]);
-    hwy::CopyBytes<kNumBytes>(&interleaved0[12], &out[3 * outstride]);
-    hwy::CopyBytes<kNumBytes>(&interleaved1[0], &out[4 * outstride]);
-    hwy::CopyBytes<kNumBytes>(&interleaved1[4], &out[5 * outstride]);
-    hwy::CopyBytes<kNumBytes>(&interleaved1[8], &out[6 * outstride]);
-    hwy::CopyBytes<kNumBytes>(&interleaved1[12], &out[7 * outstride]);
-  } else {
-    hwy::CopyBytes<kNumBytes>(&interleaved0[0], &out[0 * outstride]);
-    hwy::CopyBytes<kNumBytes>(&interleaved0[4], &out[1 * outstride]);
-    hwy::CopyBytes<kNumBytes>(&interleaved1[0], &out[2 * outstride]);
-    hwy::CopyBytes<kNumBytes>(&interleaved1[4], &out[3 * outstride]);
-    hwy::CopyBytes<kNumBytes>(&interleaved0[8], &out[4 * outstride]);
-    hwy::CopyBytes<kNumBytes>(&interleaved0[12], &out[5 * outstride]);
-    hwy::CopyBytes<kNumBytes>(&interleaved1[8], &out[6 * outstride]);
-    hwy::CopyBytes<kNumBytes>(&interleaved1[12], &out[7 * outstride]);
-  }
+  const auto s0 = RectScale<ApplyRectScale>(
+      out_tag,
+      RoundShift<Bit>(out_tag,
+                      MaybePromoteTo(out_tag, hn::Load(int_tag, &in[0]))));
+  const auto s1 = RectScale<ApplyRectScale>(
+      out_tag,
+      RoundShift<Bit>(
+          out_tag,
+          MaybePromoteTo(out_tag, hn::Load(int_tag, &in[1 * instride]))));
+  const auto s2 = RectScale<ApplyRectScale>(
+      out_tag,
+      RoundShift<Bit>(
+          out_tag,
+          MaybePromoteTo(out_tag, hn::Load(int_tag, &in[2 * instride]))));
+  const auto s3 = RectScale<ApplyRectScale>(
+      out_tag,
+      RoundShift<Bit>(
+          out_tag,
+          MaybePromoteTo(out_tag, hn::Load(int_tag, &in[3 * instride]))));
+  const auto s4 = RectScale<ApplyRectScale>(
+      out_tag,
+      RoundShift<Bit>(
+          out_tag,
+          MaybePromoteTo(out_tag, hn::Load(int_tag, &in[4 * instride]))));
+  const auto s5 = RectScale<ApplyRectScale>(
+      out_tag,
+      RoundShift<Bit>(
+          out_tag,
+          MaybePromoteTo(out_tag, hn::Load(int_tag, &in[5 * instride]))));
+  const auto s6 = RectScale<ApplyRectScale>(
+      out_tag,
+      RoundShift<Bit>(
+          out_tag,
+          MaybePromoteTo(out_tag, hn::Load(int_tag, &in[6 * instride]))));
+  const auto s7 = RectScale<ApplyRectScale>(
+      out_tag,
+      RoundShift<Bit>(
+          out_tag,
+          MaybePromoteTo(out_tag, hn::Load(int_tag, &in[7 * instride]))));
+  const auto t0 = hn::InterleaveLower(out_tag, s0, s1);
+  const auto t1 = hn::InterleaveUpper(out_tag, s0, s1);
+  const auto t2 = hn::InterleaveLower(out_tag, s2, s3);
+  const auto t3 = hn::InterleaveUpper(out_tag, s2, s3);
+  const auto t4 = hn::InterleaveLower(out_tag, s4, s5);
+  const auto t5 = hn::InterleaveUpper(out_tag, s4, s5);
+  const auto t6 = hn::InterleaveLower(out_tag, s6, s7);
+  const auto t7 = hn::InterleaveUpper(out_tag, s6, s7);
+  Transpose8StoreTraits<sizeof(TOut)>::Store(out_tag, t0, t1, t2, t3, t4, t5,
+                                             t6, t7, out, outstride);
 }

 template <typename D>
@@ -2050,61 +2034,74 @@ struct Transpose16Traits<2> {
     constexpr hn::FixedTag<TIn, 16> int_tag;
     static_assert(hn::MaxLanes(int_tag) == 16,
                   "16-bit Transpose16 requires an 16-lane int_tag");
-    constexpr hn::RepartitionToWide<decltype(int_tag)> wide_int_tag;
-    constexpr hn::RepartitionToWide<decltype(wide_int_tag)> widex2_int_tag;
-    HWY_ALIGN_MAX hn::TFromD<decltype(wide_int_tag)>
-        y[16 * hn::MaxLanes(wide_int_tag)];
-    HWY_ALIGN_MAX hn::TFromD<decltype(widex2_int_tag)>
-        z[16 * hn::MaxLanes(widex2_int_tag)];
-    for (size_t i = 0; i < 16; i += 2) {
-      auto i0 = RectScale<ApplyRectScale>(
+    constexpr hn::Half<decltype(int_tag)> half_tag;
+    for (size_t half = 0; half < 16; half += 8) {
+      const auto v0 = RectScale<ApplyRectScale>(
           int_tag,
-          RoundShift<Bit>(int_tag, hn::Load(int_tag, &in[(i + 0) * instride])));
-      auto i1 = RectScale<ApplyRectScale>(
+          RoundShift<Bit>(
+              int_tag,
+              hn::Combine(int_tag,
+                          hn::LoadU(half_tag, &in[8 * instride + half]),
+                          hn::LoadU(half_tag, &in[0 * instride + half]))));
+      const auto v1 = RectScale<ApplyRectScale>(
           int_tag,
-          RoundShift<Bit>(int_tag, hn::Load(int_tag, &in[(i + 1) * instride])));
-      hn::Store(hn::ZipLower(wide_int_tag, i0, i1), wide_int_tag,
-                &y[(i + 0) * hn::MaxLanes(wide_int_tag)]);
-      hn::Store(hn::ZipUpper(wide_int_tag, i0, i1), wide_int_tag,
-                &y[(i + 1) * hn::MaxLanes(wide_int_tag)]);
-    }
-    for (size_t i = 0; i < 16; i += 4) {
-      for (size_t j = 0; j < 2; ++j) {
-        auto i0 = hn::Load(wide_int_tag,
-                           &y[(i + j + 0) * hn::MaxLanes(wide_int_tag)]);
-        auto i2 = hn::Load(wide_int_tag,
-                           &y[(i + j + 2) * hn::MaxLanes(wide_int_tag)]);
-        hn::Store(hn::ZipLower(widex2_int_tag, i0, i2), widex2_int_tag,
-                  &z[(i + j + 0) * hn::MaxLanes(widex2_int_tag)]);
-        hn::Store(hn::ZipUpper(widex2_int_tag, i0, i2), widex2_int_tag,
-                  &z[(i + j + 2) * hn::MaxLanes(widex2_int_tag)]);
-      }
-    }
-    for (size_t i = 0; i < 16; i += 8) {
-      for (size_t j = 0; j < 4; ++j) {
-        auto i0 = hn::Load(widex2_int_tag,
-                           &z[(i + j + 0) * hn::MaxLanes(widex2_int_tag)]);
-        auto i4 = hn::Load(widex2_int_tag,
-                           &z[(i + j + 4) * hn::MaxLanes(widex2_int_tag)]);
-        hn::Store(hn::InterleaveLower(widex2_int_tag, i0, i4), widex2_int_tag,
-                  &z[(i + j + 0) * hn::MaxLanes(widex2_int_tag)]);
-        hn::Store(hn::InterleaveUpper(widex2_int_tag, i0, i4), widex2_int_tag,
-                  &z[(i + j + 4) * hn::MaxLanes(widex2_int_tag)]);
-      }
-    }
-    static constexpr size_t kStoreIndex[] = { 0, 4,  2,  6,  1, 5,  3,  7,
-                                              8, 12, 10, 14, 9, 13, 11, 15 };
-    for (size_t j = 0; j < 8; ++j) {
-      auto i0 =
-          hn::Load(widex2_int_tag, &z[(j + 0) * hn::MaxLanes(widex2_int_tag)]);
-      auto i8 =
-          hn::Load(widex2_int_tag, &z[(j + 8) * hn::MaxLanes(widex2_int_tag)]);
-      hn::StoreU(
-          hn::BitCast(int_tag, hn::ConcatLowerLower(widex2_int_tag, i8, i0)),
-          int_tag, &out[kStoreIndex[j + 0] * outstride]);
-      hn::StoreU(
-          hn::BitCast(int_tag, hn::ConcatUpperUpper(widex2_int_tag, i8, i0)),
-          int_tag, &out[kStoreIndex[j + 8] * outstride]);
+          RoundShift<Bit>(
+              int_tag,
+              hn::Combine(int_tag,
+                          hn::LoadU(half_tag, &in[9 * instride + half]),
+                          hn::LoadU(half_tag, &in[1 * instride + half]))));
+      const auto v2 = RectScale<ApplyRectScale>(
+          int_tag,
+          RoundShift<Bit>(
+              int_tag,
+              hn::Combine(int_tag,
+                          hn::LoadU(half_tag, &in[10 * instride + half]),
+                          hn::LoadU(half_tag, &in[2 * instride + half]))));
+      const auto v3 = RectScale<ApplyRectScale>(
+          int_tag,
+          RoundShift<Bit>(
+              int_tag,
+              hn::Combine(int_tag,
+                          hn::LoadU(half_tag, &in[11 * instride + half]),
+                          hn::LoadU(half_tag, &in[3 * instride + half]))));
+      const auto v4 = RectScale<ApplyRectScale>(
+          int_tag,
+          RoundShift<Bit>(
+              int_tag,
+              hn::Combine(int_tag,
+                          hn::LoadU(half_tag, &in[12 * instride + half]),
+                          hn::LoadU(half_tag, &in[4 * instride + half]))));
+      const auto v5 = RectScale<ApplyRectScale>(
+          int_tag,
+          RoundShift<Bit>(
+              int_tag,
+              hn::Combine(int_tag,
+                          hn::LoadU(half_tag, &in[13 * instride + half]),
+                          hn::LoadU(half_tag, &in[5 * instride + half]))));
+      const auto v6 = RectScale<ApplyRectScale>(
+          int_tag,
+          RoundShift<Bit>(
+              int_tag,
+              hn::Combine(int_tag,
+                          hn::LoadU(half_tag, &in[14 * instride + half]),
+                          hn::LoadU(half_tag, &in[6 * instride + half]))));
+      const auto v7 = RectScale<ApplyRectScale>(
+          int_tag,
+          RoundShift<Bit>(
+              int_tag,
+              hn::Combine(int_tag,
+                          hn::LoadU(half_tag, &in[15 * instride + half]),
+                          hn::LoadU(half_tag, &in[7 * instride + half]))));
+      const auto t0 = hn::InterleaveLower(int_tag, v0, v1);
+      const auto t1 = hn::InterleaveUpper(int_tag, v0, v1);
+      const auto t2 = hn::InterleaveLower(int_tag, v2, v3);
+      const auto t3 = hn::InterleaveUpper(int_tag, v2, v3);
+      const auto t4 = hn::InterleaveLower(int_tag, v4, v5);
+      const auto t5 = hn::InterleaveUpper(int_tag, v4, v5);
+      const auto t6 = hn::InterleaveLower(int_tag, v6, v7);
+      const auto t7 = hn::InterleaveUpper(int_tag, v6, v7);
+      Transpose8StoreTraits<2>::Store(int_tag, t0, t1, t2, t3, t4, t5, t6, t7,
+                                      &out[half * outstride], outstride);
     }
   }
 };
@@ -2352,26 +2349,30 @@ template <size_t TransformWidth, size_t BlockWidth, size_t BlockHeight,
           typename T>
 HWY_ATTR HWY_INLINE void Transform16(TX_TYPE_1D tx_type, T *in,
                                      int8_t cos_bit) {
-  static const Transform1D<T> kTransform[] = {
-    FdctNx16Block<TransformWidth, BlockWidth, T>,   // DCT_1D
-    FadstNx16Block<TransformWidth, BlockWidth, T>,  // ADST_1D
-    FadstNx16Block<TransformWidth, BlockWidth, T>,  // FLIPADST_1D
-    IdtxSqrt2Block<TransformWidth, BlockWidth, BlockHeight, 2, T>,  // IDTX_1D
-  };
-  kTransform[tx_type](in, cos_bit);
+  switch (tx_type) {
+    case DCT_1D: FdctNx16Block<TransformWidth, BlockWidth>(in, cos_bit); break;
+    case IDTX_1D:
+      IdtxSqrt2Block<TransformWidth, BlockWidth, BlockHeight, 2>(in, cos_bit);
+      break;
+    case ADST_1D:
+    case FLIPADST_1D:
+    default:
+      assert(tx_type == ADST_1D || tx_type == FLIPADST_1D);
+      FadstNx16Block<TransformWidth, BlockWidth>(in, cos_bit);
+      break;
+  }
 }

 template <size_t TransformWidth, size_t BlockWidth, size_t BlockHeight,
           typename T>
 HWY_ATTR HWY_INLINE void Transform32(TX_TYPE_1D tx_type, T *in,
                                      int8_t cos_bit) {
-  static const Transform1D<T> kTransform[] = {
-    FdctNx32Block<TransformWidth, BlockWidth, T>,  // DCT_1D
-    TransformFail<T>,                              // ADST_1D
-    TransformFail<T>,                              // FLIPADST_1D
-    IdtxShiftBlock<TransformWidth, BlockWidth, BlockHeight, 2, T>,  // IDTX_1D
-  };
-  kTransform[tx_type](in, cos_bit);
+  if (tx_type == DCT_1D) {
+    FdctNx32Block<TransformWidth, BlockWidth>(in, cos_bit);
+  } else {
+    assert(tx_type == IDTX_1D);
+    IdtxShiftBlock<TransformWidth, BlockWidth, BlockHeight, 2>(in, cos_bit);
+  }
 }

 template <size_t TransformWidth, size_t BlockWidth, typename T>