/*
* Copyright(c) 2019 Intel Corporation
* Copyright (c) 2016, Alliance for Open Media. All rights reserved
*
* This source code is subject to the terms of the BSD 2 Clause License and
* the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
* was not distributed with this source code in the LICENSE file, you can
* obtain it at https://www.aomedia.org/license/software-license. If the Alliance for Open
* Media Patent License 1.0 was not distributed with this source code in the
* PATENTS file, you can obtain it at https://www.aomedia.org/license/patent-license.
*/

#include <assert.h>
#include <smmintrin.h> /* SSE4.1 */

#include "EbDefinitions.h"
#include "common_dsp_rtcd.h"

#include "highbd_txfm_utility_sse4.h"
#include "EbInvTransforms.h"

#include "av1_inv_txfm_ssse3.h"

void svt_av1_highbd_inv_txfm2d_add_universe_sse4_1(const int32_t *input, uint16_t *output_r,
                                                   int32_t stride_r, uint16_t *output_w,
                                                   int32_t stride_w, TxType tx_type, TxSize tx_size,
                                                   int32_t eob, const int32_t bd);

typedef void (*Transform1dSse41)(__m128i *in, __m128i *out, int32_t bit, int32_t do_cols,
                                 int32_t bd, int32_t out_shift);

static INLINE void load_buffer_4x4(const int32_t *coeff, __m128i *in) {
    in[0] = _mm_loadu_si128((const __m128i *)(coeff + 0));
    in[1] = _mm_loadu_si128((const __m128i *)(coeff + 4));
    in[2] = _mm_loadu_si128((const __m128i *)(coeff + 8));
    in[3] = _mm_loadu_si128((const __m128i *)(coeff + 12));
}

static void addsub_sse4_1(const __m128i in0, const __m128i in1, __m128i *out0, __m128i *out1,
                          const __m128i *clamp_lo, const __m128i *clamp_hi) {
    __m128i a0 = _mm_add_epi32(in0, in1);
    __m128i a1 = _mm_sub_epi32(in0, in1);

    a0 = _mm_max_epi32(a0, *clamp_lo);
    a0 = _mm_min_epi32(a0, *clamp_hi);
    a1 = _mm_max_epi32(a1, *clamp_lo);
    a1 = _mm_min_epi32(a1, *clamp_hi);

    *out0 = a0;
    *out1 = a1;
}

static void shift_and_clamp_sse4_1(__m128i *in0, __m128i *in1, const __m128i *clamp_lo,
                                   const __m128i *clamp_hi, int shift) {
    __m128i offset       = _mm_set1_epi32((1 << shift) >> 1);
    __m128i in0_w_offset = _mm_add_epi32(*in0, offset);
    __m128i in1_w_offset = _mm_add_epi32(*in1, offset);

    in0_w_offset = _mm_sra_epi32(in0_w_offset, _mm_cvtsi32_si128(shift));
    in1_w_offset = _mm_sra_epi32(in1_w_offset, _mm_cvtsi32_si128(shift));

    in0_w_offset = _mm_max_epi32(in0_w_offset, *clamp_lo);
    in0_w_offset = _mm_min_epi32(in0_w_offset, *clamp_hi);
    in1_w_offset = _mm_max_epi32(in1_w_offset, *clamp_lo);
    in1_w_offset = _mm_min_epi32(in1_w_offset, *clamp_hi);

    *in0 = in0_w_offset;
    *in1 = in1_w_offset;
}

static void idct4x4_sse4_1(__m128i *in, __m128i *out, int32_t bit, int32_t do_cols, int32_t bd,
                           int32_t out_shift) {
    (void)*out;
    (void)bd;
    (void)out_shift;
    const int32_t *cospi     = cospi_arr(bit);
    const __m128i  cospi32   = _mm_set1_epi32(cospi[32]);
    const __m128i  cospi48   = _mm_set1_epi32(cospi[48]);
    const __m128i  cospi16   = _mm_set1_epi32(cospi[16]);
    const __m128i  cospim16  = _mm_set1_epi32(-cospi[16]);
    const __m128i  rnding    = _mm_set1_epi32(1 << (bit - 1));
    int            log_range = AOMMAX(16, bd + (do_cols ? 6 : 8));
    __m128i        clamp_lo  = _mm_set1_epi32(-(1 << (log_range - 1)));
    __m128i        clamp_hi  = _mm_set1_epi32((1 << (log_range - 1)) - 1);
    __m128i        u0, u1, u2, u3;
    __m128i        v0, v1, v2, v3, x, y;

    v0 = _mm_unpacklo_epi32(in[0], in[1]);
    v1 = _mm_unpackhi_epi32(in[0], in[1]);
    v2 = _mm_unpacklo_epi32(in[2], in[3]);
    v3 = _mm_unpackhi_epi32(in[2], in[3]);

    u0 = _mm_unpacklo_epi64(v0, v2);
    u1 = _mm_unpackhi_epi64(v0, v2);
    u2 = _mm_unpacklo_epi64(v1, v3);
    u3 = _mm_unpackhi_epi64(v1, v3);

    x  = _mm_mullo_epi32(u0, cospi32);
    y  = _mm_mullo_epi32(u2, cospi32);
    v0 = _mm_add_epi32(x, y);
    v0 = _mm_add_epi32(v0, rnding);
    v0 = _mm_srai_epi32(v0, bit);

    v1 = _mm_sub_epi32(x, y);
    v1 = _mm_add_epi32(v1, rnding);
    v1 = _mm_srai_epi32(v1, bit);

    x  = _mm_mullo_epi32(u1, cospi48);
    y  = _mm_mullo_epi32(u3, cospim16);
    v2 = _mm_add_epi32(x, y);
    v2 = _mm_add_epi32(v2, rnding);
    v2 = _mm_srai_epi32(v2, bit);

    x  = _mm_mullo_epi32(u1, cospi16);
    y  = _mm_mullo_epi32(u3, cospi48);
    v3 = _mm_add_epi32(x, y);
    v3 = _mm_add_epi32(v3, rnding);
    v3 = _mm_srai_epi32(v3, bit);

    addsub_sse4_1(v0, v3, out + 0, out + 3, &clamp_lo, &clamp_hi);
    addsub_sse4_1(v1, v2, out + 1, out + 2, &clamp_lo, &clamp_hi);

    if (!do_cols) {
        log_range = AOMMAX(16, bd + 6);
        clamp_lo  = _mm_set1_epi32(-(1 << (log_range - 1)));
        clamp_hi  = _mm_set1_epi32((1 << (log_range - 1)) - 1);

        shift_and_clamp_sse4_1(out + 0, out + 3, &clamp_lo, &clamp_hi, out_shift);
        shift_and_clamp_sse4_1(out + 1, out + 2, &clamp_lo, &clamp_hi, out_shift);
    }
}

static void highbd_clamp_epi32_sse4_1(const __m128i *in, __m128i *out, const __m128i *clamp_lo,
                                      const __m128i *clamp_hi, int32_t size) {
    __m128i a0, a1;
    for (int32_t i = 0; i < size; i += 4) {
        a0     = _mm_max_epi32(in[i], *clamp_lo);
        out[i] = _mm_min_epi32(a0, *clamp_hi);

        a1         = _mm_max_epi32(in[i + 1], *clamp_lo);
        out[i + 1] = _mm_min_epi32(a1, *clamp_hi);

        a0         = _mm_max_epi32(in[i + 2], *clamp_lo);
        out[i + 2] = _mm_min_epi32(a0, *clamp_hi);

        a1         = _mm_max_epi32(in[i + 3], *clamp_lo);
        out[i + 3] = _mm_min_epi32(a1, *clamp_hi);
    }
}

static INLINE void round_shift_4x4(__m128i *in, int32_t shift) {
    if (shift != 0) {
        __m128i rnding = _mm_set1_epi32(1 << (shift - 1));

        in[0] = _mm_add_epi32(in[0], rnding);
        in[1] = _mm_add_epi32(in[1], rnding);
        in[2] = _mm_add_epi32(in[2], rnding);
        in[3] = _mm_add_epi32(in[3], rnding);

        in[0] = _mm_srai_epi32(in[0], shift);
        in[1] = _mm_srai_epi32(in[1], shift);
        in[2] = _mm_srai_epi32(in[2], shift);
        in[3] = _mm_srai_epi32(in[3], shift);
    }
}

static void iadst4x4_sse4_1(__m128i *in, __m128i *out, int32_t bit, int32_t do_cols, int32_t bd,
                            int32_t out_shift) {
    (void)out_shift;
    const int32_t *sinpi  = sinpi_arr(bit);
    const __m128i  zero   = _mm_set1_epi32(0);
    __m128i        rnding = _mm_set1_epi32(1 << (bit + 4 - 1));
    rnding                = _mm_unpacklo_epi32(rnding, zero);
    const __m128i mul     = _mm_set1_epi32(1 << 4);
    const __m128i sinpi1  = _mm_set1_epi32((int)sinpi[1]);
    const __m128i sinpi2  = _mm_set1_epi32((int)sinpi[2]);
    const __m128i sinpi3  = _mm_set1_epi32((int)sinpi[3]);
    const __m128i sinpi4  = _mm_set1_epi32((int)sinpi[4]);
    __m128i       t;
    __m128i       s0, s1, s2, s3, s4, s5, s6, s7;
    __m128i       x0, x1, x2, x3;
    __m128i       u0, u1, u2, u3;
    __m128i       v0, v1, v2, v3;
    __m128i       u0_low, u1_low, u2_low, u3_low;
    __m128i       u0_high, u1_high, u2_high, u3_high;

    v0 = _mm_unpacklo_epi32(in[0], in[1]);
    v1 = _mm_unpackhi_epi32(in[0], in[1]);
    v2 = _mm_unpacklo_epi32(in[2], in[3]);
    v3 = _mm_unpackhi_epi32(in[2], in[3]);

    x0 = _mm_unpacklo_epi64(v0, v2);
    x1 = _mm_unpackhi_epi64(v0, v2);
    x2 = _mm_unpacklo_epi64(v1, v3);
    x3 = _mm_unpackhi_epi64(v1, v3);

    s0 = _mm_mullo_epi32(x0, sinpi1);
    s1 = _mm_mullo_epi32(x0, sinpi2);
    s2 = _mm_mullo_epi32(x1, sinpi3);
    s3 = _mm_mullo_epi32(x2, sinpi4);
    s4 = _mm_mullo_epi32(x2, sinpi1);
    s5 = _mm_mullo_epi32(x3, sinpi2);
    s6 = _mm_mullo_epi32(x3, sinpi4);
    t  = _mm_sub_epi32(x0, x2);
    s7 = _mm_add_epi32(t, x3);

    t  = _mm_add_epi32(s0, s3);
    s0 = _mm_add_epi32(t, s5);
    t  = _mm_sub_epi32(s1, s4);
    s1 = _mm_sub_epi32(t, s6);
    s3 = s2;
    s2 = _mm_mullo_epi32(s7, sinpi3);

    u0 = _mm_add_epi32(s0, s3);
    u1 = _mm_add_epi32(s1, s3);
    u2 = s2;
    t  = _mm_add_epi32(s0, s1);
    u3 = _mm_sub_epi32(t, s3);

    // u0
    u0_low = _mm_mul_epi32(u0, mul);
    u0_low = _mm_add_epi64(u0_low, rnding);

    u0      = _mm_srli_si128(u0, 4);
    u0_high = _mm_mul_epi32(u0, mul);
    u0_high = _mm_add_epi64(u0_high, rnding);

    u0_low  = _mm_srli_si128(u0_low, 2);
    u0_high = _mm_srli_si128(u0_high, 2);

    u0      = _mm_unpacklo_epi32(u0_low, u0_high);
    u0_high = _mm_unpackhi_epi32(u0_low, u0_high);
    u0      = _mm_unpacklo_epi64(u0, u0_high);

    // u1
    u1_low = _mm_mul_epi32(u1, mul);
    u1_low = _mm_add_epi64(u1_low, rnding);

    u1      = _mm_srli_si128(u1, 4);
    u1_high = _mm_mul_epi32(u1, mul);
    u1_high = _mm_add_epi64(u1_high, rnding);

    u1_low  = _mm_srli_si128(u1_low, 2);
    u1_high = _mm_srli_si128(u1_high, 2);

    u1      = _mm_unpacklo_epi32(u1_low, u1_high);
    u1_high = _mm_unpackhi_epi32(u1_low, u1_high);
    u1      = _mm_unpacklo_epi64(u1, u1_high);

    // u2
    u2_low = _mm_mul_epi32(u2, mul);
    u2_low = _mm_add_epi64(u2_low, rnding);

    u2      = _mm_srli_si128(u2, 4);
    u2_high = _mm_mul_epi32(u2, mul);
    u2_high = _mm_add_epi64(u2_high, rnding);

    u2_low  = _mm_srli_si128(u2_low, 2);
    u2_high = _mm_srli_si128(u2_high, 2);

    u2      = _mm_unpacklo_epi32(u2_low, u2_high);
    u2_high = _mm_unpackhi_epi32(u2_low, u2_high);
    u2      = _mm_unpacklo_epi64(u2, u2_high);

    // u3
    u3_low = _mm_mul_epi32(u3, mul);
    u3_low = _mm_add_epi64(u3_low, rnding);

    u3      = _mm_srli_si128(u3, 4);
    u3_high = _mm_mul_epi32(u3, mul);
    u3_high = _mm_add_epi64(u3_high, rnding);

    u3_low  = _mm_srli_si128(u3_low, 2);
    u3_high = _mm_srli_si128(u3_high, 2);

    u3      = _mm_unpacklo_epi32(u3_low, u3_high);
    u3_high = _mm_unpackhi_epi32(u3_low, u3_high);
    u3      = _mm_unpacklo_epi64(u3, u3_high);

    out[0] = u0;
    out[1] = u1;
    out[2] = u2;
    out[3] = u3;

    if (!do_cols) {
        const int     log_range = AOMMAX(16, bd + 6);
        const __m128i clamp_lo  = _mm_set1_epi32(-(1 << (log_range - 1)));
        const __m128i clamp_hi  = _mm_set1_epi32((1 << (log_range - 1)) - 1);
        round_shift_4x4(out, out_shift);
        highbd_clamp_epi32_sse4_1(out, out, &clamp_lo, &clamp_hi, 4);
    }
}

static INLINE __m128i highbd_clamp_epi16(__m128i u, int32_t bd) {
    const __m128i zero = _mm_setzero_si128();
    const __m128i one  = _mm_set1_epi16(1);
    const __m128i max  = _mm_sub_epi16(_mm_slli_epi16(one, bd), one);
    __m128i       clamped, mask;

    mask    = _mm_cmpgt_epi16(u, max);
    clamped = _mm_andnot_si128(mask, u);
    mask    = _mm_and_si128(mask, max);
    clamped = _mm_or_si128(mask, clamped);
    mask    = _mm_cmpgt_epi16(clamped, zero);
    clamped = _mm_and_si128(clamped, mask);

    return clamped;
}

static void write_buffer_4x4(__m128i *in, uint16_t *output_r, int32_t stride_r, uint16_t *output_w,
                             int32_t stride_w, int32_t fliplr, int32_t flipud, int32_t shift,
                             int32_t bd) {
    const __m128i zero = _mm_setzero_si128();
    __m128i       u0, u1, u2, u3;
    __m128i       v0, v1, v2, v3;

    round_shift_4x4(in, shift);

    v0 = _mm_loadl_epi64((__m128i const *)(output_r + 0 * stride_r));
    v1 = _mm_loadl_epi64((__m128i const *)(output_r + 1 * stride_r));
    v2 = _mm_loadl_epi64((__m128i const *)(output_r + 2 * stride_r));
    v3 = _mm_loadl_epi64((__m128i const *)(output_r + 3 * stride_r));

    v0 = _mm_unpacklo_epi16(v0, zero);
    v1 = _mm_unpacklo_epi16(v1, zero);
    v2 = _mm_unpacklo_epi16(v2, zero);
    v3 = _mm_unpacklo_epi16(v3, zero);

    if (fliplr) {
        in[0] = _mm_shuffle_epi32(in[0], 0x1B);
        in[1] = _mm_shuffle_epi32(in[1], 0x1B);
        in[2] = _mm_shuffle_epi32(in[2], 0x1B);
        in[3] = _mm_shuffle_epi32(in[3], 0x1B);
    }

    if (flipud) {
        u0 = _mm_add_epi32(in[3], v0);
        u1 = _mm_add_epi32(in[2], v1);
        u2 = _mm_add_epi32(in[1], v2);
        u3 = _mm_add_epi32(in[0], v3);
    } else {
        u0 = _mm_add_epi32(in[0], v0);
        u1 = _mm_add_epi32(in[1], v1);
        u2 = _mm_add_epi32(in[2], v2);
        u3 = _mm_add_epi32(in[3], v3);
    }

    v0 = _mm_packus_epi32(u0, u1);
    v2 = _mm_packus_epi32(u2, u3);

    u0 = highbd_clamp_epi16(v0, bd);
    u2 = highbd_clamp_epi16(v2, bd);

    v0 = _mm_unpacklo_epi64(u0, u0);
    v1 = _mm_unpackhi_epi64(u0, u0);
    v2 = _mm_unpacklo_epi64(u2, u2);
    v3 = _mm_unpackhi_epi64(u2, u2);

    _mm_storel_epi64((__m128i *)(output_w + 0 * stride_w), v0);
    _mm_storel_epi64((__m128i *)(output_w + 1 * stride_w), v1);
    _mm_storel_epi64((__m128i *)(output_w + 2 * stride_w), v2);
    _mm_storel_epi64((__m128i *)(output_w + 3 * stride_w), v3);
}

void svt_av1_inv_txfm2d_add_4x4_sse4_1(const int32_t *input, uint16_t *output_r, int32_t stride_r,
                                       uint16_t *output_w, int32_t stride_w, TxType tx_type,
                                       int32_t bd) {
    __m128i       in[4];
    const int8_t *shift   = eb_inv_txfm_shift_ls[TX_4X4];
    const int32_t txw_idx = get_txw_idx(TX_4X4);
    const int32_t txh_idx = get_txh_idx(TX_4X4);

    switch (tx_type) {
    case DCT_DCT:
        load_buffer_4x4(input, in);
        idct4x4_sse4_1(in, in, inv_cos_bit_row[txw_idx][txh_idx], 0, bd, 0);
        idct4x4_sse4_1(in, in, inv_cos_bit_col[txw_idx][txh_idx], 1, bd, 0);
        write_buffer_4x4(in, output_r, stride_r, output_w, stride_w, 0, 0, -shift[1], bd);
        break;
    case ADST_DCT:
        load_buffer_4x4(input, in);
        idct4x4_sse4_1(in, in, inv_cos_bit_row[txw_idx][txh_idx], 0, bd, 0);
        iadst4x4_sse4_1(in, in, inv_cos_bit_col[txw_idx][txh_idx], 1, bd, 0);
        write_buffer_4x4(in, output_r, stride_r, output_w, stride_w, 0, 0, -shift[1], bd);
        break;
    case DCT_ADST:
        load_buffer_4x4(input, in);
        iadst4x4_sse4_1(in, in, inv_cos_bit_row[txw_idx][txh_idx], 0, bd, 0);
        idct4x4_sse4_1(in, in, inv_cos_bit_col[txw_idx][txh_idx], 1, bd, 0);
        write_buffer_4x4(in, output_r, stride_r, output_w, stride_w, 0, 0, -shift[1], bd);
        break;
    case ADST_ADST:
        load_buffer_4x4(input, in);
        iadst4x4_sse4_1(in, in, inv_cos_bit_row[txw_idx][txh_idx], 0, bd, 0);
        iadst4x4_sse4_1(in, in, inv_cos_bit_col[txw_idx][txh_idx], 1, bd, 0);
        write_buffer_4x4(in, output_r, stride_r, output_w, stride_w, 0, 0, -shift[1], bd);
        break;
    case FLIPADST_DCT:
        load_buffer_4x4(input, in);
        idct4x4_sse4_1(in, in, inv_cos_bit_row[txw_idx][txh_idx], 0, bd, 0);
        iadst4x4_sse4_1(in, in, inv_cos_bit_col[txw_idx][txh_idx], 1, bd, 0);
        write_buffer_4x4(in, output_r, stride_r, output_w, stride_w, 0, 1, -shift[1], bd);
        break;
    case DCT_FLIPADST:
        load_buffer_4x4(input, in);
        iadst4x4_sse4_1(in, in, inv_cos_bit_row[txw_idx][txh_idx], 0, bd, 0);
        idct4x4_sse4_1(in, in, inv_cos_bit_col[txw_idx][txh_idx], 1, bd, 0);
        write_buffer_4x4(in, output_r, stride_r, output_w, stride_w, 1, 0, -shift[1], bd);
        break;
    case FLIPADST_FLIPADST:
        load_buffer_4x4(input, in);
        iadst4x4_sse4_1(in, in, inv_cos_bit_row[txw_idx][txh_idx], 0, bd, 0);
        iadst4x4_sse4_1(in, in, inv_cos_bit_col[txw_idx][txh_idx], 1, bd, 0);
        write_buffer_4x4(in, output_r, stride_r, output_w, stride_w, 1, 1, -shift[1], bd);
        break;
    case ADST_FLIPADST:
        load_buffer_4x4(input, in);
        iadst4x4_sse4_1(in, in, inv_cos_bit_row[txw_idx][txh_idx], 0, bd, 0);
        iadst4x4_sse4_1(in, in, inv_cos_bit_col[txw_idx][txh_idx], 1, bd, 0);
        write_buffer_4x4(in, output_r, stride_r, output_w, stride_w, 1, 0, -shift[1], bd);
        break;
    case FLIPADST_ADST:
        load_buffer_4x4(input, in);
        iadst4x4_sse4_1(in, in, inv_cos_bit_row[txw_idx][txh_idx], 0, bd, 0);
        iadst4x4_sse4_1(in, in, inv_cos_bit_col[txw_idx][txh_idx], 1, bd, 0);
        write_buffer_4x4(in, output_r, stride_r, output_w, stride_w, 0, 1, -shift[1], bd);
        break;
    default:
        svt_av1_inv_txfm2d_add_4x4_c(input, output_r, stride_r, output_w, stride_w, tx_type, bd);
        break;
    }
}

// 8x8
static void load_buffer_8x8(const int32_t *coeff, __m128i *in) {
    in[0]  = _mm_loadu_si128((const __m128i *)(coeff + 0));
    in[1]  = _mm_loadu_si128((const __m128i *)(coeff + 4));
    in[2]  = _mm_loadu_si128((const __m128i *)(coeff + 8));
    in[3]  = _mm_loadu_si128((const __m128i *)(coeff + 12));
    in[4]  = _mm_loadu_si128((const __m128i *)(coeff + 16));
    in[5]  = _mm_loadu_si128((const __m128i *)(coeff + 20));
    in[6]  = _mm_loadu_si128((const __m128i *)(coeff + 24));
    in[7]  = _mm_loadu_si128((const __m128i *)(coeff + 28));
    in[8]  = _mm_loadu_si128((const __m128i *)(coeff + 32));
    in[9]  = _mm_loadu_si128((const __m128i *)(coeff + 36));
    in[10] = _mm_loadu_si128((const __m128i *)(coeff + 40));
    in[11] = _mm_loadu_si128((const __m128i *)(coeff + 44));
    in[12] = _mm_loadu_si128((const __m128i *)(coeff + 48));
    in[13] = _mm_loadu_si128((const __m128i *)(coeff + 52));
    in[14] = _mm_loadu_si128((const __m128i *)(coeff + 56));
    in[15] = _mm_loadu_si128((const __m128i *)(coeff + 60));
}

static void idct8x8_sse4_1(__m128i *in, __m128i *out, int32_t bit) {
    const int32_t *cospi    = cospi_arr(bit);
    const __m128i  cospi56  = _mm_set1_epi32(cospi[56]);
    const __m128i  cospim8  = _mm_set1_epi32(-cospi[8]);
    const __m128i  cospi24  = _mm_set1_epi32(cospi[24]);
    const __m128i  cospim40 = _mm_set1_epi32(-cospi[40]);
    const __m128i  cospi40  = _mm_set1_epi32(cospi[40]);
    const __m128i  cospi8   = _mm_set1_epi32(cospi[8]);
    const __m128i  cospi32  = _mm_set1_epi32(cospi[32]);
    const __m128i  cospi48  = _mm_set1_epi32(cospi[48]);
    const __m128i  cospim16 = _mm_set1_epi32(-cospi[16]);
    const __m128i  cospi16  = _mm_set1_epi32(cospi[16]);
    const __m128i  rnding   = _mm_set1_epi32(1 << (bit - 1));
    __m128i        u0, u1, u2, u3, u4, u5, u6, u7;
    __m128i        v0, v1, v2, v3, v4, v5, v6, v7;
    __m128i        x, y;
    int32_t        col;

    // Note:
    //  Even column: 0, 2, ..., 14
    //  Odd column: 1, 3, ..., 15
    //  one even column plus one odd column constructs one row (8 coeffs)
    //  total we have 8 rows (8x8).
    for (col = 0; col < 2; ++col) {
        // stage 0
        // stage 1
        // stage 2
        u0 = in[0 * 2 + col];
        u1 = in[4 * 2 + col];
        u2 = in[2 * 2 + col];
        u3 = in[6 * 2 + col];

        x  = _mm_mullo_epi32(in[1 * 2 + col], cospi56);
        y  = _mm_mullo_epi32(in[7 * 2 + col], cospim8);
        u4 = _mm_add_epi32(x, y);
        u4 = _mm_add_epi32(u4, rnding);
        u4 = _mm_srai_epi32(u4, bit);

        x  = _mm_mullo_epi32(in[1 * 2 + col], cospi8);
        y  = _mm_mullo_epi32(in[7 * 2 + col], cospi56);
        u7 = _mm_add_epi32(x, y);
        u7 = _mm_add_epi32(u7, rnding);
        u7 = _mm_srai_epi32(u7, bit);

        x  = _mm_mullo_epi32(in[5 * 2 + col], cospi24);
        y  = _mm_mullo_epi32(in[3 * 2 + col], cospim40);
        u5 = _mm_add_epi32(x, y);
        u5 = _mm_add_epi32(u5, rnding);
        u5 = _mm_srai_epi32(u5, bit);

        x  = _mm_mullo_epi32(in[5 * 2 + col], cospi40);
        y  = _mm_mullo_epi32(in[3 * 2 + col], cospi24);
        u6 = _mm_add_epi32(x, y);
        u6 = _mm_add_epi32(u6, rnding);
        u6 = _mm_srai_epi32(u6, bit);

        // stage 3
        x  = _mm_mullo_epi32(u0, cospi32);
        y  = _mm_mullo_epi32(u1, cospi32);
        v0 = _mm_add_epi32(x, y);
        v0 = _mm_add_epi32(v0, rnding);
        v0 = _mm_srai_epi32(v0, bit);

        v1 = _mm_sub_epi32(x, y);
        v1 = _mm_add_epi32(v1, rnding);
        v1 = _mm_srai_epi32(v1, bit);

        x  = _mm_mullo_epi32(u2, cospi48);
        y  = _mm_mullo_epi32(u3, cospim16);
        v2 = _mm_add_epi32(x, y);
        v2 = _mm_add_epi32(v2, rnding);
        v2 = _mm_srai_epi32(v2, bit);

        x  = _mm_mullo_epi32(u2, cospi16);
        y  = _mm_mullo_epi32(u3, cospi48);
        v3 = _mm_add_epi32(x, y);
        v3 = _mm_add_epi32(v3, rnding);
        v3 = _mm_srai_epi32(v3, bit);

        v4 = _mm_add_epi32(u4, u5);
        v5 = _mm_sub_epi32(u4, u5);
        v6 = _mm_sub_epi32(u7, u6);
        v7 = _mm_add_epi32(u6, u7);

        // stage 4
        u0 = _mm_add_epi32(v0, v3);
        u1 = _mm_add_epi32(v1, v2);
        u2 = _mm_sub_epi32(v1, v2);
        u3 = _mm_sub_epi32(v0, v3);
        u4 = v4;
        u7 = v7;

        x  = _mm_mullo_epi32(v5, cospi32);
        y  = _mm_mullo_epi32(v6, cospi32);
        u6 = _mm_add_epi32(y, x);
        u6 = _mm_add_epi32(u6, rnding);
        u6 = _mm_srai_epi32(u6, bit);

        u5 = _mm_sub_epi32(y, x);
        u5 = _mm_add_epi32(u5, rnding);
        u5 = _mm_srai_epi32(u5, bit);

        // stage 5
        out[0 * 2 + col] = _mm_add_epi32(u0, u7);
        out[1 * 2 + col] = _mm_add_epi32(u1, u6);
        out[2 * 2 + col] = _mm_add_epi32(u2, u5);
        out[3 * 2 + col] = _mm_add_epi32(u3, u4);
        out[4 * 2 + col] = _mm_sub_epi32(u3, u4);
        out[5 * 2 + col] = _mm_sub_epi32(u2, u5);
        out[6 * 2 + col] = _mm_sub_epi32(u1, u6);
        out[7 * 2 + col] = _mm_sub_epi32(u0, u7);
    }
}

static void iadst8x8_sse4_1(__m128i *in, __m128i *out, int32_t bit) {
    const int32_t *cospi    = cospi_arr(bit);
    const __m128i  cospi4   = _mm_set1_epi32(cospi[4]);
    const __m128i  cospi60  = _mm_set1_epi32(cospi[60]);
    const __m128i  cospi20  = _mm_set1_epi32(cospi[20]);
    const __m128i  cospi44  = _mm_set1_epi32(cospi[44]);
    const __m128i  cospi36  = _mm_set1_epi32(cospi[36]);
    const __m128i  cospi28  = _mm_set1_epi32(cospi[28]);
    const __m128i  cospi52  = _mm_set1_epi32(cospi[52]);
    const __m128i  cospi12  = _mm_set1_epi32(cospi[12]);
    const __m128i  cospi16  = _mm_set1_epi32(cospi[16]);
    const __m128i  cospi48  = _mm_set1_epi32(cospi[48]);
    const __m128i  cospim48 = _mm_set1_epi32(-cospi[48]);
    const __m128i  cospi32  = _mm_set1_epi32(cospi[32]);
    const __m128i  rnding   = _mm_set1_epi32(1 << (bit - 1));
    const __m128i  k_zero   = _mm_setzero_si128();
    __m128i        u[8], v[8], x;

    // Even 8 points: 0, 2, ..., 14
    // stage 0
    // stage 1
    // stage 2
    // (1)
    u[0] = _mm_mullo_epi32(in[14], cospi4);
    x    = _mm_mullo_epi32(in[0], cospi60);
    u[0] = _mm_add_epi32(u[0], x);
    u[0] = _mm_add_epi32(u[0], rnding);
    u[0] = _mm_srai_epi32(u[0], bit);

    u[1] = _mm_mullo_epi32(in[14], cospi60);
    x    = _mm_mullo_epi32(in[0], cospi4);
    u[1] = _mm_sub_epi32(u[1], x);
    u[1] = _mm_add_epi32(u[1], rnding);
    u[1] = _mm_srai_epi32(u[1], bit);

    // (2)
    u[2] = _mm_mullo_epi32(in[10], cospi20);
    x    = _mm_mullo_epi32(in[4], cospi44);
    u[2] = _mm_add_epi32(u[2], x);
    u[2] = _mm_add_epi32(u[2], rnding);
    u[2] = _mm_srai_epi32(u[2], bit);

    u[3] = _mm_mullo_epi32(in[10], cospi44);
    x    = _mm_mullo_epi32(in[4], cospi20);
    u[3] = _mm_sub_epi32(u[3], x);
    u[3] = _mm_add_epi32(u[3], rnding);
    u[3] = _mm_srai_epi32(u[3], bit);

    // (3)
    u[4] = _mm_mullo_epi32(in[6], cospi36);
    x    = _mm_mullo_epi32(in[8], cospi28);
    u[4] = _mm_add_epi32(u[4], x);
    u[4] = _mm_add_epi32(u[4], rnding);
    u[4] = _mm_srai_epi32(u[4], bit);

    u[5] = _mm_mullo_epi32(in[6], cospi28);
    x    = _mm_mullo_epi32(in[8], cospi36);
    u[5] = _mm_sub_epi32(u[5], x);
    u[5] = _mm_add_epi32(u[5], rnding);
    u[5] = _mm_srai_epi32(u[5], bit);

    // (4)
    u[6] = _mm_mullo_epi32(in[2], cospi52);
    x    = _mm_mullo_epi32(in[12], cospi12);
    u[6] = _mm_add_epi32(u[6], x);
    u[6] = _mm_add_epi32(u[6], rnding);
    u[6] = _mm_srai_epi32(u[6], bit);

    u[7] = _mm_mullo_epi32(in[2], cospi12);
    x    = _mm_mullo_epi32(in[12], cospi52);
    u[7] = _mm_sub_epi32(u[7], x);
    u[7] = _mm_add_epi32(u[7], rnding);
    u[7] = _mm_srai_epi32(u[7], bit);

    // stage 3
    v[0] = _mm_add_epi32(u[0], u[4]);
    v[4] = _mm_sub_epi32(u[0], u[4]);
    v[1] = _mm_add_epi32(u[1], u[5]);
    v[5] = _mm_sub_epi32(u[1], u[5]);
    v[2] = _mm_add_epi32(u[2], u[6]);
    v[6] = _mm_sub_epi32(u[2], u[6]);
    v[3] = _mm_add_epi32(u[3], u[7]);
    v[7] = _mm_sub_epi32(u[3], u[7]);

    // stage 4
    u[0] = v[0];
    u[1] = v[1];
    u[2] = v[2];
    u[3] = v[3];

    u[4] = _mm_mullo_epi32(v[4], cospi16);
    x    = _mm_mullo_epi32(v[5], cospi48);
    u[4] = _mm_add_epi32(u[4], x);
    u[4] = _mm_add_epi32(u[4], rnding);
    u[4] = _mm_srai_epi32(u[4], bit);

    u[5] = _mm_mullo_epi32(v[4], cospi48);
    x    = _mm_mullo_epi32(v[5], cospi16);
    u[5] = _mm_sub_epi32(u[5], x);
    u[5] = _mm_add_epi32(u[5], rnding);
    u[5] = _mm_srai_epi32(u[5], bit);

    u[6] = _mm_mullo_epi32(v[6], cospim48);
    x    = _mm_mullo_epi32(v[7], cospi16);
    u[6] = _mm_add_epi32(u[6], x);
    u[6] = _mm_add_epi32(u[6], rnding);
    u[6] = _mm_srai_epi32(u[6], bit);

    u[7] = _mm_mullo_epi32(v[6], cospi16);
    x    = _mm_mullo_epi32(v[7], cospim48);
    u[7] = _mm_sub_epi32(u[7], x);
    u[7] = _mm_add_epi32(u[7], rnding);
    u[7] = _mm_srai_epi32(u[7], bit);

    // stage 5
    v[0] = _mm_add_epi32(u[0], u[2]);
    v[2] = _mm_sub_epi32(u[0], u[2]);
    v[1] = _mm_add_epi32(u[1], u[3]);
    v[3] = _mm_sub_epi32(u[1], u[3]);
    v[4] = _mm_add_epi32(u[4], u[6]);
    v[6] = _mm_sub_epi32(u[4], u[6]);
    v[5] = _mm_add_epi32(u[5], u[7]);
    v[7] = _mm_sub_epi32(u[5], u[7]);

    // stage 6
    u[0] = v[0];
    u[1] = v[1];
    u[4] = v[4];
    u[5] = v[5];

    v[0] = _mm_mullo_epi32(v[2], cospi32);
    x    = _mm_mullo_epi32(v[3], cospi32);
    u[2] = _mm_add_epi32(v[0], x);
    u[2] = _mm_add_epi32(u[2], rnding);
    u[2] = _mm_srai_epi32(u[2], bit);

    u[3] = _mm_sub_epi32(v[0], x);
    u[3] = _mm_add_epi32(u[3], rnding);
    u[3] = _mm_srai_epi32(u[3], bit);

    v[0] = _mm_mullo_epi32(v[6], cospi32);
    x    = _mm_mullo_epi32(v[7], cospi32);
    u[6] = _mm_add_epi32(v[0], x);
    u[6] = _mm_add_epi32(u[6], rnding);
    u[6] = _mm_srai_epi32(u[6], bit);

    u[7] = _mm_sub_epi32(v[0], x);
    u[7] = _mm_add_epi32(u[7], rnding);
    u[7] = _mm_srai_epi32(u[7], bit);

    // stage 7
    out[0]  = u[0];
    out[2]  = _mm_sub_epi32(k_zero, u[4]);
    out[4]  = u[6];
    out[6]  = _mm_sub_epi32(k_zero, u[2]);
    out[8]  = u[3];
    out[10] = _mm_sub_epi32(k_zero, u[7]);
    out[12] = u[5];
    out[14] = _mm_sub_epi32(k_zero, u[1]);

    // Odd 8 points: 1, 3, ..., 15
    // stage 0
    // stage 1
    // stage 2
    // (1)
    u[0] = _mm_mullo_epi32(in[15], cospi4);
    x    = _mm_mullo_epi32(in[1], cospi60);
    u[0] = _mm_add_epi32(u[0], x);
    u[0] = _mm_add_epi32(u[0], rnding);
    u[0] = _mm_srai_epi32(u[0], bit);

    u[1] = _mm_mullo_epi32(in[15], cospi60);
    x    = _mm_mullo_epi32(in[1], cospi4);
    u[1] = _mm_sub_epi32(u[1], x);
    u[1] = _mm_add_epi32(u[1], rnding);
    u[1] = _mm_srai_epi32(u[1], bit);

    // (2)
    u[2] = _mm_mullo_epi32(in[11], cospi20);
    x    = _mm_mullo_epi32(in[5], cospi44);
    u[2] = _mm_add_epi32(u[2], x);
    u[2] = _mm_add_epi32(u[2], rnding);
    u[2] = _mm_srai_epi32(u[2], bit);

    u[3] = _mm_mullo_epi32(in[11], cospi44);
    x    = _mm_mullo_epi32(in[5], cospi20);
    u[3] = _mm_sub_epi32(u[3], x);
    u[3] = _mm_add_epi32(u[3], rnding);
    u[3] = _mm_srai_epi32(u[3], bit);

    // (3)
    u[4] = _mm_mullo_epi32(in[7], cospi36);
    x    = _mm_mullo_epi32(in[9], cospi28);
    u[4] = _mm_add_epi32(u[4], x);
    u[4] = _mm_add_epi32(u[4], rnding);
    u[4] = _mm_srai_epi32(u[4], bit);

    u[5] = _mm_mullo_epi32(in[7], cospi28);
    x    = _mm_mullo_epi32(in[9], cospi36);
    u[5] = _mm_sub_epi32(u[5], x);
    u[5] = _mm_add_epi32(u[5], rnding);
    u[5] = _mm_srai_epi32(u[5], bit);

    // (4)
    u[6] = _mm_mullo_epi32(in[3], cospi52);
    x    = _mm_mullo_epi32(in[13], cospi12);
    u[6] = _mm_add_epi32(u[6], x);
    u[6] = _mm_add_epi32(u[6], rnding);
    u[6] = _mm_srai_epi32(u[6], bit);

    u[7] = _mm_mullo_epi32(in[3], cospi12);
    x    = _mm_mullo_epi32(in[13], cospi52);
    u[7] = _mm_sub_epi32(u[7], x);
    u[7] = _mm_add_epi32(u[7], rnding);
    u[7] = _mm_srai_epi32(u[7], bit);

    // stage 3
    v[0] = _mm_add_epi32(u[0], u[4]);
    v[4] = _mm_sub_epi32(u[0], u[4]);
    v[1] = _mm_add_epi32(u[1], u[5]);
    v[5] = _mm_sub_epi32(u[1], u[5]);
    v[2] = _mm_add_epi32(u[2], u[6]);
    v[6] = _mm_sub_epi32(u[2], u[6]);
    v[3] = _mm_add_epi32(u[3], u[7]);
    v[7] = _mm_sub_epi32(u[3], u[7]);

    // stage 4
    u[0] = v[0];
    u[1] = v[1];
    u[2] = v[2];
    u[3] = v[3];

    u[4] = _mm_mullo_epi32(v[4], cospi16);
    x    = _mm_mullo_epi32(v[5], cospi48);
    u[4] = _mm_add_epi32(u[4], x);
    u[4] = _mm_add_epi32(u[4], rnding);
    u[4] = _mm_srai_epi32(u[4], bit);

    u[5] = _mm_mullo_epi32(v[4], cospi48);
    x    = _mm_mullo_epi32(v[5], cospi16);
    u[5] = _mm_sub_epi32(u[5], x);
    u[5] = _mm_add_epi32(u[5], rnding);
    u[5] = _mm_srai_epi32(u[5], bit);

    u[6] = _mm_mullo_epi32(v[6], cospim48);
    x    = _mm_mullo_epi32(v[7], cospi16);
    u[6] = _mm_add_epi32(u[6], x);
    u[6] = _mm_add_epi32(u[6], rnding);
    u[6] = _mm_srai_epi32(u[6], bit);

    u[7] = _mm_mullo_epi32(v[6], cospi16);
    x    = _mm_mullo_epi32(v[7], cospim48);
    u[7] = _mm_sub_epi32(u[7], x);
    u[7] = _mm_add_epi32(u[7], rnding);
    u[7] = _mm_srai_epi32(u[7], bit);

    // stage 5
    v[0] = _mm_add_epi32(u[0], u[2]);
    v[2] = _mm_sub_epi32(u[0], u[2]);
    v[1] = _mm_add_epi32(u[1], u[3]);
    v[3] = _mm_sub_epi32(u[1], u[3]);
    v[4] = _mm_add_epi32(u[4], u[6]);
    v[6] = _mm_sub_epi32(u[4], u[6]);
    v[5] = _mm_add_epi32(u[5], u[7]);
    v[7] = _mm_sub_epi32(u[5], u[7]);

    // stage 6
    u[0] = v[0];
    u[1] = v[1];
    u[4] = v[4];
    u[5] = v[5];

    v[0] = _mm_mullo_epi32(v[2], cospi32);
    x    = _mm_mullo_epi32(v[3], cospi32);
    u[2] = _mm_add_epi32(v[0], x);
    u[2] = _mm_add_epi32(u[2], rnding);
    u[2] = _mm_srai_epi32(u[2], bit);

    u[3] = _mm_sub_epi32(v[0], x);
    u[3] = _mm_add_epi32(u[3], rnding);
    u[3] = _mm_srai_epi32(u[3], bit);

    v[0] = _mm_mullo_epi32(v[6], cospi32);
    x    = _mm_mullo_epi32(v[7], cospi32);
    u[6] = _mm_add_epi32(v[0], x);
    u[6] = _mm_add_epi32(u[6], rnding);
    u[6] = _mm_srai_epi32(u[6], bit);

    u[7] = _mm_sub_epi32(v[0], x);
    u[7] = _mm_add_epi32(u[7], rnding);
    u[7] = _mm_srai_epi32(u[7], bit);

    // stage 7
    out[1]  = u[0];
    out[3]  = _mm_sub_epi32(k_zero, u[4]);
    out[5]  = u[6];
    out[7]  = _mm_sub_epi32(k_zero, u[2]);
    out[9]  = u[3];
    out[11] = _mm_sub_epi32(k_zero, u[7]);
    out[13] = u[5];
    out[15] = _mm_sub_epi32(k_zero, u[1]);
}

static void round_shift_8x8(__m128i *in, int32_t shift) {
    round_shift_4x4(&in[0], shift);
    round_shift_4x4(&in[4], shift);
    round_shift_4x4(&in[8], shift);
    round_shift_4x4(&in[12], shift);
}

static __m128i get_recon_8x8(const __m128i pred, __m128i res_lo, __m128i res_hi, int32_t fliplr,
                             int32_t bd) {
    __m128i       x0, x1;
    const __m128i zero = _mm_setzero_si128();

    x0 = _mm_unpacklo_epi16(pred, zero);
    x1 = _mm_unpackhi_epi16(pred, zero);

    if (fliplr) {
        res_lo = _mm_shuffle_epi32(res_lo, 0x1B);
        res_hi = _mm_shuffle_epi32(res_hi, 0x1B);
        x0     = _mm_add_epi32(res_hi, x0);
        x1     = _mm_add_epi32(res_lo, x1);
    } else {
        x0 = _mm_add_epi32(res_lo, x0);
        x1 = _mm_add_epi32(res_hi, x1);
    }

    x0 = _mm_packus_epi32(x0, x1);
    return highbd_clamp_epi16(x0, bd);
}

static void write_buffer_8x8(__m128i *in, uint16_t *output_r, int32_t stride_r, uint16_t *output_w,
                             int32_t stride_w, int32_t fliplr, int32_t flipud, int32_t shift,
                             int32_t bd) {
    __m128i u0, u1, u2, u3, u4, u5, u6, u7;
    __m128i v0, v1, v2, v3, v4, v5, v6, v7;

    round_shift_8x8(in, shift);

    v0 = _mm_loadu_si128((__m128i const *)(output_r + 0 * stride_r));
    v1 = _mm_loadu_si128((__m128i const *)(output_r + 1 * stride_r));
    v2 = _mm_loadu_si128((__m128i const *)(output_r + 2 * stride_r));
    v3 = _mm_loadu_si128((__m128i const *)(output_r + 3 * stride_r));
    v4 = _mm_loadu_si128((__m128i const *)(output_r + 4 * stride_r));
    v5 = _mm_loadu_si128((__m128i const *)(output_r + 5 * stride_r));
    v6 = _mm_loadu_si128((__m128i const *)(output_r + 6 * stride_r));
    v7 = _mm_loadu_si128((__m128i const *)(output_r + 7 * stride_r));

    if (flipud) {
        u0 = get_recon_8x8(v0, in[14], in[15], fliplr, bd);
        u1 = get_recon_8x8(v1, in[12], in[13], fliplr, bd);
        u2 = get_recon_8x8(v2, in[10], in[11], fliplr, bd);
        u3 = get_recon_8x8(v3, in[8], in[9], fliplr, bd);
        u4 = get_recon_8x8(v4, in[6], in[7], fliplr, bd);
        u5 = get_recon_8x8(v5, in[4], in[5], fliplr, bd);
        u6 = get_recon_8x8(v6, in[2], in[3], fliplr, bd);
        u7 = get_recon_8x8(v7, in[0], in[1], fliplr, bd);
    } else {
        u0 = get_recon_8x8(v0, in[0], in[1], fliplr, bd);
        u1 = get_recon_8x8(v1, in[2], in[3], fliplr, bd);
        u2 = get_recon_8x8(v2, in[4], in[5], fliplr, bd);
        u3 = get_recon_8x8(v3, in[6], in[7], fliplr, bd);
        u4 = get_recon_8x8(v4, in[8], in[9], fliplr, bd);
        u5 = get_recon_8x8(v5, in[10], in[11], fliplr, bd);
        u6 = get_recon_8x8(v6, in[12], in[13], fliplr, bd);
        u7 = get_recon_8x8(v7, in[14], in[15], fliplr, bd);
    }

    _mm_storeu_si128((__m128i *)(output_w + 0 * stride_w), u0);
    _mm_storeu_si128((__m128i *)(output_w + 1 * stride_w), u1);
    _mm_storeu_si128((__m128i *)(output_w + 2 * stride_w), u2);
    _mm_storeu_si128((__m128i *)(output_w + 3 * stride_w), u3);
    _mm_storeu_si128((__m128i *)(output_w + 4 * stride_w), u4);
    _mm_storeu_si128((__m128i *)(output_w + 5 * stride_w), u5);
    _mm_storeu_si128((__m128i *)(output_w + 6 * stride_w), u6);
    _mm_storeu_si128((__m128i *)(output_w + 7 * stride_w), u7);
}

void svt_av1_inv_txfm2d_add_8x8_sse4_1(const int32_t *input, uint16_t *output_r, int32_t stride_r,
                                       uint16_t *output_w, int32_t stride_w, TxType tx_type,
                                       int32_t bd) {
    __m128i       in[16], out[16];
    const int8_t *shift   = eb_inv_txfm_shift_ls[TX_8X8];
    const int32_t txw_idx = get_txw_idx(TX_8X8);
    const int32_t txh_idx = get_txh_idx(TX_8X8);

    switch (tx_type) {
    case DCT_DCT:
        load_buffer_8x8(input, in);
        transpose_8x8(in, out);
        idct8x8_sse4_1(out, in, inv_cos_bit_row[txw_idx][txh_idx]);
        transpose_8x8(in, out);
        round_shift_8x8(out, -shift[0]);
        idct8x8_sse4_1(out, in, inv_cos_bit_col[txw_idx][txh_idx]);
        write_buffer_8x8(in, output_r, stride_r, output_w, stride_w, 0, 0, -shift[1], bd);
        break;
    case DCT_ADST:
        load_buffer_8x8(input, in);
        transpose_8x8(in, out);
        iadst8x8_sse4_1(out, in, inv_cos_bit_row[txw_idx][txh_idx]);
        transpose_8x8(in, out);
        round_shift_8x8(out, -shift[0]);
        idct8x8_sse4_1(out, in, inv_cos_bit_col[txw_idx][txh_idx]);
        write_buffer_8x8(in, output_r, stride_r, output_w, stride_w, 0, 0, -shift[1], bd);
        break;
    case ADST_DCT:
        load_buffer_8x8(input, in);
        transpose_8x8(in, out);
        idct8x8_sse4_1(out, in, inv_cos_bit_row[txw_idx][txh_idx]);
        transpose_8x8(in, out);
        round_shift_8x8(out, -shift[0]);
        iadst8x8_sse4_1(out, in, inv_cos_bit_col[txw_idx][txh_idx]);
        write_buffer_8x8(in, output_r, stride_r, output_w, stride_w, 0, 0, -shift[1], bd);
        break;
    case ADST_ADST:
        load_buffer_8x8(input, in);
        transpose_8x8(in, out);
        iadst8x8_sse4_1(out, in, inv_cos_bit_row[txw_idx][txh_idx]);
        transpose_8x8(in, out);
        round_shift_8x8(out, -shift[0]);
        iadst8x8_sse4_1(out, in, inv_cos_bit_col[txw_idx][txh_idx]);
        write_buffer_8x8(in, output_r, stride_r, output_w, stride_w, 0, 0, -shift[1], bd);
        break;
    case FLIPADST_DCT:
        load_buffer_8x8(input, in);
        transpose_8x8(in, out);
        idct8x8_sse4_1(out, in, inv_cos_bit_row[txw_idx][txh_idx]);
        transpose_8x8(in, out);
        round_shift_8x8(out, -shift[0]);
        iadst8x8_sse4_1(out, in, inv_cos_bit_col[txw_idx][txh_idx]);
        write_buffer_8x8(in, output_r, stride_r, output_w, stride_w, 0, 1, -shift[1], bd);
        break;
    case DCT_FLIPADST:
        load_buffer_8x8(input, in);
        transpose_8x8(in, out);
        iadst8x8_sse4_1(out, in, inv_cos_bit_row[txw_idx][txh_idx]);
        transpose_8x8(in, out);
        round_shift_8x8(out, -shift[0]);
        idct8x8_sse4_1(out, in, inv_cos_bit_col[txw_idx][txh_idx]);
        write_buffer_8x8(in, output_r, stride_r, output_w, stride_w, 1, 0, -shift[1], bd);
        break;
    case ADST_FLIPADST:
        load_buffer_8x8(input, in);
        transpose_8x8(in, out);
        iadst8x8_sse4_1(out, in, inv_cos_bit_row[txw_idx][txh_idx]);
        transpose_8x8(in, out);
        round_shift_8x8(out, -shift[0]);
        iadst8x8_sse4_1(out, in, inv_cos_bit_col[txw_idx][txh_idx]);
        write_buffer_8x8(in, output_r, stride_r, output_w, stride_w, 1, 0, -shift[1], bd);
        break;
    case FLIPADST_FLIPADST:
        load_buffer_8x8(input, in);
        transpose_8x8(in, out);
        iadst8x8_sse4_1(out, in, inv_cos_bit_row[txw_idx][txh_idx]);
        transpose_8x8(in, out);
        round_shift_8x8(out, -shift[0]);
        iadst8x8_sse4_1(out, in, inv_cos_bit_col[txw_idx][txh_idx]);
        write_buffer_8x8(in, output_r, stride_r, output_w, stride_w, 1, 1, -shift[1], bd);
        break;
    case FLIPADST_ADST:
        load_buffer_8x8(input, in);
        transpose_8x8(in, out);
        iadst8x8_sse4_1(out, in, inv_cos_bit_row[txw_idx][txh_idx]);
        transpose_8x8(in, out);
        round_shift_8x8(out, -shift[0]);
        iadst8x8_sse4_1(out, in, inv_cos_bit_col[txw_idx][txh_idx]);
        write_buffer_8x8(in, output_r, stride_r, output_w, stride_w, 0, 1, -shift[1], bd);
        break;
    case IDTX:
        load_buffer_8x8(input, in);
        // Operations can be joined together without losing precision
        // svt_av1_iidentity8_c() shift left 1 bits
        // round_shift_8x8(, -shift[0]) shift right 1 bits
        // svt_av1_iidentity8_c() shift left 1 bits
        // round_shift_8x8(, -shift[1]) shift right 4 bits with complement
        write_buffer_8x8(
            in, output_r, stride_r, output_w, stride_w, 0, 0, -shift[0] - shift[1] - 2, bd);
        break;
    case V_DCT:
        load_buffer_8x8(input, in);
        // svt_av1_iidentity8_c() shift left 1 bits
        // round_shift_8x8(, -shift[0]) shift right 1 bits
        idct8x8_sse4_1(in, out, inv_cos_bit_row[txw_idx][txh_idx]);
        write_buffer_8x8(out, output_r, stride_r, output_w, stride_w, 0, 0, -shift[1], bd);
        break;
    case H_DCT:
        load_buffer_8x8(input, in);
        transpose_8x8(in, out);
        idct8x8_sse4_1(out, in, inv_cos_bit_row[txw_idx][txh_idx]);
        transpose_8x8(in, out);
        // svt_av1_iidentity8_c() shift left 1 bits
        // round_shift_8x8(, -shift[1]) shift right 4 bits with complement
        round_shift_8x8(out, -shift[0]);
        write_buffer_8x8(out, output_r, stride_r, output_w, stride_w, 0, 0, -shift[1] - 1, bd);
        break;
    case V_ADST:
        load_buffer_8x8(input, in);
        // svt_av1_iidentity8_c() shift left 1 bits
        // round_shift_8x8(, -shift[0]) shift right 1 bits
        iadst8x8_sse4_1(in, out, inv_cos_bit_col[txw_idx][txh_idx]);
        write_buffer_8x8(out, output_r, stride_r, output_w, stride_w, 0, 0, -shift[1], bd);
        break;
    case H_ADST:
        load_buffer_8x8(input, in);
        transpose_8x8(in, out);
        iadst8x8_sse4_1(out, in, inv_cos_bit_row[txw_idx][txh_idx]);
        transpose_8x8(in, out);
        // svt_av1_iidentity8_c() shift left 1 bits
        // round_shift_8x8(, -shift[1]) shift right 4 bits with complement
        round_shift_8x8(out, -shift[0]);
        write_buffer_8x8(out, output_r, stride_r, output_w, stride_w, 0, 0, -shift[1] - 1, bd);
        break;
    case V_FLIPADST:
        load_buffer_8x8(input, in);
        // svt_av1_iidentity8_c() shift left 1 bits
        // round_shift_8x8(, -shift[0]) shift right 1 bits
        iadst8x8_sse4_1(in, out, inv_cos_bit_col[txw_idx][txh_idx]);
        write_buffer_8x8(out, output_r, stride_r, output_w, stride_w, 0, 1, -shift[1], bd);
        break;
    case H_FLIPADST:
        load_buffer_8x8(input, in);
        transpose_8x8(in, out);
        iadst8x8_sse4_1(out, in, inv_cos_bit_row[txw_idx][txh_idx]);
        transpose_8x8(in, out);
        // svt_av1_iidentity8_c() shift left 1 bits
        // round_shift_8x8(, -shift[1]) shift right 4 bits with complement
        round_shift_8x8(out, -shift[0]);
        write_buffer_8x8(out, output_r, stride_r, output_w, stride_w, 1, 0, -shift[1] - 1, bd);
        break;
    default:
        svt_av1_inv_txfm2d_add_8x8_c(input, output_r, stride_r, output_w, stride_w, tx_type, bd);
    }
}

// 16x16
static void load_buffer_16x16(const int32_t *coeff, __m128i *in) {
    int32_t i;
    for (i = 0; i < 64; ++i) in[i] = _mm_loadu_si128((const __m128i *)(coeff + (i << 2)));
}

static void assign_8x8_input_from_16x16(const __m128i *in, __m128i *in8x8, int32_t col) {
    int32_t i;
    for (i = 0; i < 16; i += 2) {
        in8x8[i]     = in[col];
        in8x8[i + 1] = in[col + 1];
        col += 4;
    }
}

static void swap_addr(uint16_t **output1, uint16_t **output2) {
    uint16_t *tmp;
    tmp      = *output1;
    *output1 = *output2;
    *output2 = tmp;
}

static void write_buffer_16x16(__m128i *in, uint16_t *output_r, int32_t stride_r,
                               uint16_t *output_w, int32_t stride_w, int32_t fliplr, int32_t flipud,
                               int32_t shift, int32_t bd) {
    __m128i   in8x8[16];
    uint16_t *left_up_r    = &output_r[0];
    uint16_t *right_up_r   = &output_r[8];
    uint16_t *left_down_r  = &output_r[8 * stride_r];
    uint16_t *right_down_r = &output_r[8 * stride_r + 8];
    uint16_t *left_up_w    = &output_w[0];
    uint16_t *right_up_w   = &output_w[8];
    uint16_t *left_down_w  = &output_w[8 * stride_w];
    uint16_t *right_down_w = &output_w[8 * stride_w + 8];

    if (fliplr) {
        swap_addr(&left_up_r, &right_up_r);
        swap_addr(&left_down_r, &right_down_r);
        swap_addr(&left_up_w, &right_up_w);
        swap_addr(&left_down_w, &right_down_w);
    }

    if (flipud) {
        swap_addr(&left_up_r, &left_down_r);
        swap_addr(&right_up_r, &right_down_r);
        swap_addr(&left_up_w, &left_down_w);
        swap_addr(&right_up_w, &right_down_w);
    }

    // Left-up quarter
    assign_8x8_input_from_16x16(in, in8x8, 0);
    write_buffer_8x8(in8x8, left_up_r, stride_r, left_up_w, stride_w, fliplr, flipud, shift, bd);

    // Right-up quarter
    assign_8x8_input_from_16x16(in, in8x8, 2);
    write_buffer_8x8(in8x8, right_up_r, stride_r, right_up_w, stride_w, fliplr, flipud, shift, bd);

    // Left-down quarter
    assign_8x8_input_from_16x16(in, in8x8, 32);
    write_buffer_8x8(
        in8x8, left_down_r, stride_r, left_down_w, stride_w, fliplr, flipud, shift, bd);

    // Right-down quarter
    assign_8x8_input_from_16x16(in, in8x8, 34);
    write_buffer_8x8(
        in8x8, right_down_r, stride_r, right_down_w, stride_w, fliplr, flipud, shift, bd);
}

static void idct16x16_sse4_1(__m128i *in, __m128i *out, int32_t bit) {
    const int32_t *cospi    = cospi_arr(bit);
    const __m128i  cospi60  = _mm_set1_epi32(cospi[60]);
    const __m128i  cospim4  = _mm_set1_epi32(-cospi[4]);
    const __m128i  cospi28  = _mm_set1_epi32(cospi[28]);
    const __m128i  cospim36 = _mm_set1_epi32(-cospi[36]);
    const __m128i  cospi44  = _mm_set1_epi32(cospi[44]);
    const __m128i  cospi20  = _mm_set1_epi32(cospi[20]);
    const __m128i  cospim20 = _mm_set1_epi32(-cospi[20]);
    const __m128i  cospi12  = _mm_set1_epi32(cospi[12]);
    const __m128i  cospim52 = _mm_set1_epi32(-cospi[52]);
    const __m128i  cospi52  = _mm_set1_epi32(cospi[52]);
    const __m128i  cospi36  = _mm_set1_epi32(cospi[36]);
    const __m128i  cospi4   = _mm_set1_epi32(cospi[4]);
    const __m128i  cospi56  = _mm_set1_epi32(cospi[56]);
    const __m128i  cospim8  = _mm_set1_epi32(-cospi[8]);
    const __m128i  cospi24  = _mm_set1_epi32(cospi[24]);
    const __m128i  cospim40 = _mm_set1_epi32(-cospi[40]);
    const __m128i  cospi40  = _mm_set1_epi32(cospi[40]);
    const __m128i  cospi8   = _mm_set1_epi32(cospi[8]);
    const __m128i  cospi32  = _mm_set1_epi32(cospi[32]);
    const __m128i  cospi48  = _mm_set1_epi32(cospi[48]);
    const __m128i  cospi16  = _mm_set1_epi32(cospi[16]);
    const __m128i  cospim16 = _mm_set1_epi32(-cospi[16]);
    const __m128i  cospim48 = _mm_set1_epi32(-cospi[48]);
    const __m128i  rnding   = _mm_set1_epi32(1 << (bit - 1));
    __m128i        u[16], v[16], x, y;
    int32_t        col;

    for (col = 0; col < 4; ++col) {
        // stage 0
        // stage 1
        u[0]  = in[0 * 4 + col];
        u[1]  = in[8 * 4 + col];
        u[2]  = in[4 * 4 + col];
        u[3]  = in[12 * 4 + col];
        u[4]  = in[2 * 4 + col];
        u[5]  = in[10 * 4 + col];
        u[6]  = in[6 * 4 + col];
        u[7]  = in[14 * 4 + col];
        u[8]  = in[1 * 4 + col];
        u[9]  = in[9 * 4 + col];
        u[10] = in[5 * 4 + col];
        u[11] = in[13 * 4 + col];
        u[12] = in[3 * 4 + col];
        u[13] = in[11 * 4 + col];
        u[14] = in[7 * 4 + col];
        u[15] = in[15 * 4 + col];

        // stage 2
        v[0] = u[0];
        v[1] = u[1];
        v[2] = u[2];
        v[3] = u[3];
        v[4] = u[4];
        v[5] = u[5];
        v[6] = u[6];
        v[7] = u[7];

        v[8]  = half_btf_sse4_1(&cospi60, &u[8], &cospim4, &u[15], &rnding, bit);
        v[9]  = half_btf_sse4_1(&cospi28, &u[9], &cospim36, &u[14], &rnding, bit);
        v[10] = half_btf_sse4_1(&cospi44, &u[10], &cospim20, &u[13], &rnding, bit);
        v[11] = half_btf_sse4_1(&cospi12, &u[11], &cospim52, &u[12], &rnding, bit);
        v[12] = half_btf_sse4_1(&cospi52, &u[11], &cospi12, &u[12], &rnding, bit);
        v[13] = half_btf_sse4_1(&cospi20, &u[10], &cospi44, &u[13], &rnding, bit);
        v[14] = half_btf_sse4_1(&cospi36, &u[9], &cospi28, &u[14], &rnding, bit);
        v[15] = half_btf_sse4_1(&cospi4, &u[8], &cospi60, &u[15], &rnding, bit);

        // stage 3
        u[0]  = v[0];
        u[1]  = v[1];
        u[2]  = v[2];
        u[3]  = v[3];
        u[4]  = half_btf_sse4_1(&cospi56, &v[4], &cospim8, &v[7], &rnding, bit);
        u[5]  = half_btf_sse4_1(&cospi24, &v[5], &cospim40, &v[6], &rnding, bit);
        u[6]  = half_btf_sse4_1(&cospi40, &v[5], &cospi24, &v[6], &rnding, bit);
        u[7]  = half_btf_sse4_1(&cospi8, &v[4], &cospi56, &v[7], &rnding, bit);
        u[8]  = _mm_add_epi32(v[8], v[9]);
        u[9]  = _mm_sub_epi32(v[8], v[9]);
        u[10] = _mm_sub_epi32(v[11], v[10]);
        u[11] = _mm_add_epi32(v[10], v[11]);
        u[12] = _mm_add_epi32(v[12], v[13]);
        u[13] = _mm_sub_epi32(v[12], v[13]);
        u[14] = _mm_sub_epi32(v[15], v[14]);
        u[15] = _mm_add_epi32(v[14], v[15]);

        // stage 4
        x    = _mm_mullo_epi32(u[0], cospi32);
        y    = _mm_mullo_epi32(u[1], cospi32);
        v[0] = _mm_add_epi32(x, y);
        v[0] = _mm_add_epi32(v[0], rnding);
        v[0] = _mm_srai_epi32(v[0], bit);

        v[1] = _mm_sub_epi32(x, y);
        v[1] = _mm_add_epi32(v[1], rnding);
        v[1] = _mm_srai_epi32(v[1], bit);

        v[2]  = half_btf_sse4_1(&cospi48, &u[2], &cospim16, &u[3], &rnding, bit);
        v[3]  = half_btf_sse4_1(&cospi16, &u[2], &cospi48, &u[3], &rnding, bit);
        v[4]  = _mm_add_epi32(u[4], u[5]);
        v[5]  = _mm_sub_epi32(u[4], u[5]);
        v[6]  = _mm_sub_epi32(u[7], u[6]);
        v[7]  = _mm_add_epi32(u[6], u[7]);
        v[8]  = u[8];
        v[9]  = half_btf_sse4_1(&cospim16, &u[9], &cospi48, &u[14], &rnding, bit);
        v[10] = half_btf_sse4_1(&cospim48, &u[10], &cospim16, &u[13], &rnding, bit);
        v[11] = u[11];
        v[12] = u[12];
        v[13] = half_btf_sse4_1(&cospim16, &u[10], &cospi48, &u[13], &rnding, bit);
        v[14] = half_btf_sse4_1(&cospi48, &u[9], &cospi16, &u[14], &rnding, bit);
        v[15] = u[15];

        // stage 5
        u[0] = _mm_add_epi32(v[0], v[3]);
        u[1] = _mm_add_epi32(v[1], v[2]);
        u[2] = _mm_sub_epi32(v[1], v[2]);
        u[3] = _mm_sub_epi32(v[0], v[3]);
        u[4] = v[4];

        x    = _mm_mullo_epi32(v[5], cospi32);
        y    = _mm_mullo_epi32(v[6], cospi32);
        u[5] = _mm_sub_epi32(y, x);
        u[5] = _mm_add_epi32(u[5], rnding);
        u[5] = _mm_srai_epi32(u[5], bit);

        u[6] = _mm_add_epi32(y, x);
        u[6] = _mm_add_epi32(u[6], rnding);
        u[6] = _mm_srai_epi32(u[6], bit);

        u[7]  = v[7];
        u[8]  = _mm_add_epi32(v[8], v[11]);
        u[9]  = _mm_add_epi32(v[9], v[10]);
        u[10] = _mm_sub_epi32(v[9], v[10]);
        u[11] = _mm_sub_epi32(v[8], v[11]);
        u[12] = _mm_sub_epi32(v[15], v[12]);
        u[13] = _mm_sub_epi32(v[14], v[13]);
        u[14] = _mm_add_epi32(v[13], v[14]);
        u[15] = _mm_add_epi32(v[12], v[15]);

        // stage 6
        v[0] = _mm_add_epi32(u[0], u[7]);
        v[1] = _mm_add_epi32(u[1], u[6]);
        v[2] = _mm_add_epi32(u[2], u[5]);
        v[3] = _mm_add_epi32(u[3], u[4]);
        v[4] = _mm_sub_epi32(u[3], u[4]);
        v[5] = _mm_sub_epi32(u[2], u[5]);
        v[6] = _mm_sub_epi32(u[1], u[6]);
        v[7] = _mm_sub_epi32(u[0], u[7]);
        v[8] = u[8];
        v[9] = u[9];

        x     = _mm_mullo_epi32(u[10], cospi32);
        y     = _mm_mullo_epi32(u[13], cospi32);
        v[10] = _mm_sub_epi32(y, x);
        v[10] = _mm_add_epi32(v[10], rnding);
        v[10] = _mm_srai_epi32(v[10], bit);

        v[13] = _mm_add_epi32(x, y);
        v[13] = _mm_add_epi32(v[13], rnding);
        v[13] = _mm_srai_epi32(v[13], bit);

        x     = _mm_mullo_epi32(u[11], cospi32);
        y     = _mm_mullo_epi32(u[12], cospi32);
        v[11] = _mm_sub_epi32(y, x);
        v[11] = _mm_add_epi32(v[11], rnding);
        v[11] = _mm_srai_epi32(v[11], bit);

        v[12] = _mm_add_epi32(x, y);
        v[12] = _mm_add_epi32(v[12], rnding);
        v[12] = _mm_srai_epi32(v[12], bit);

        v[14] = u[14];
        v[15] = u[15];

        // stage 7
        out[0 * 4 + col]  = _mm_add_epi32(v[0], v[15]);
        out[1 * 4 + col]  = _mm_add_epi32(v[1], v[14]);
        out[2 * 4 + col]  = _mm_add_epi32(v[2], v[13]);
        out[3 * 4 + col]  = _mm_add_epi32(v[3], v[12]);
        out[4 * 4 + col]  = _mm_add_epi32(v[4], v[11]);
        out[5 * 4 + col]  = _mm_add_epi32(v[5], v[10]);
        out[6 * 4 + col]  = _mm_add_epi32(v[6], v[9]);
        out[7 * 4 + col]  = _mm_add_epi32(v[7], v[8]);
        out[8 * 4 + col]  = _mm_sub_epi32(v[7], v[8]);
        out[9 * 4 + col]  = _mm_sub_epi32(v[6], v[9]);
        out[10 * 4 + col] = _mm_sub_epi32(v[5], v[10]);
        out[11 * 4 + col] = _mm_sub_epi32(v[4], v[11]);
        out[12 * 4 + col] = _mm_sub_epi32(v[3], v[12]);
        out[13 * 4 + col] = _mm_sub_epi32(v[2], v[13]);
        out[14 * 4 + col] = _mm_sub_epi32(v[1], v[14]);
        out[15 * 4 + col] = _mm_sub_epi32(v[0], v[15]);
    }
}

static void iadst16x16_sse4_1(__m128i *in, __m128i *out, int32_t bit) {
    const int32_t *cospi    = cospi_arr(bit);
    const __m128i  cospi2   = _mm_set1_epi32(cospi[2]);
    const __m128i  cospi62  = _mm_set1_epi32(cospi[62]);
    const __m128i  cospi10  = _mm_set1_epi32(cospi[10]);
    const __m128i  cospi54  = _mm_set1_epi32(cospi[54]);
    const __m128i  cospi18  = _mm_set1_epi32(cospi[18]);
    const __m128i  cospi46  = _mm_set1_epi32(cospi[46]);
    const __m128i  cospi26  = _mm_set1_epi32(cospi[26]);
    const __m128i  cospi38  = _mm_set1_epi32(cospi[38]);
    const __m128i  cospi34  = _mm_set1_epi32(cospi[34]);
    const __m128i  cospi30  = _mm_set1_epi32(cospi[30]);
    const __m128i  cospi42  = _mm_set1_epi32(cospi[42]);
    const __m128i  cospi22  = _mm_set1_epi32(cospi[22]);
    const __m128i  cospi50  = _mm_set1_epi32(cospi[50]);
    const __m128i  cospi14  = _mm_set1_epi32(cospi[14]);
    const __m128i  cospi58  = _mm_set1_epi32(cospi[58]);
    const __m128i  cospi6   = _mm_set1_epi32(cospi[6]);
    const __m128i  cospi8   = _mm_set1_epi32(cospi[8]);
    const __m128i  cospi56  = _mm_set1_epi32(cospi[56]);
    const __m128i  cospi40  = _mm_set1_epi32(cospi[40]);
    const __m128i  cospi24  = _mm_set1_epi32(cospi[24]);
    const __m128i  cospim56 = _mm_set1_epi32(-cospi[56]);
    const __m128i  cospim24 = _mm_set1_epi32(-cospi[24]);
    const __m128i  cospi48  = _mm_set1_epi32(cospi[48]);
    const __m128i  cospi16  = _mm_set1_epi32(cospi[16]);
    const __m128i  cospim48 = _mm_set1_epi32(-cospi[48]);
    const __m128i  cospi32  = _mm_set1_epi32(cospi[32]);
    const __m128i  rnding   = _mm_set1_epi32(1 << (bit - 1));
    __m128i        u[16], v[16], x, y;
    const int32_t  col_num = 4;
    int32_t        col;

    // Calculate the column 0, 1, 2, 3
    for (col = 0; col < col_num; ++col) {
        // stage 0
        // stage 1
        // stage 2
        v[0] = _mm_mullo_epi32(in[15 * col_num + col], cospi2);
        x    = _mm_mullo_epi32(in[0 * col_num + col], cospi62);
        v[0] = _mm_add_epi32(v[0], x);
        v[0] = _mm_add_epi32(v[0], rnding);
        v[0] = _mm_srai_epi32(v[0], bit);

        v[1] = _mm_mullo_epi32(in[15 * col_num + col], cospi62);
        x    = _mm_mullo_epi32(in[0 * col_num + col], cospi2);
        v[1] = _mm_sub_epi32(v[1], x);
        v[1] = _mm_add_epi32(v[1], rnding);
        v[1] = _mm_srai_epi32(v[1], bit);

        v[2] = _mm_mullo_epi32(in[13 * col_num + col], cospi10);
        x    = _mm_mullo_epi32(in[2 * col_num + col], cospi54);
        v[2] = _mm_add_epi32(v[2], x);
        v[2] = _mm_add_epi32(v[2], rnding);
        v[2] = _mm_srai_epi32(v[2], bit);

        v[3] = _mm_mullo_epi32(in[13 * col_num + col], cospi54);
        x    = _mm_mullo_epi32(in[2 * col_num + col], cospi10);
        v[3] = _mm_sub_epi32(v[3], x);
        v[3] = _mm_add_epi32(v[3], rnding);
        v[3] = _mm_srai_epi32(v[3], bit);

        v[4] = _mm_mullo_epi32(in[11 * col_num + col], cospi18);
        x    = _mm_mullo_epi32(in[4 * col_num + col], cospi46);
        v[4] = _mm_add_epi32(v[4], x);
        v[4] = _mm_add_epi32(v[4], rnding);
        v[4] = _mm_srai_epi32(v[4], bit);

        v[5] = _mm_mullo_epi32(in[11 * col_num + col], cospi46);
        x    = _mm_mullo_epi32(in[4 * col_num + col], cospi18);
        v[5] = _mm_sub_epi32(v[5], x);
        v[5] = _mm_add_epi32(v[5], rnding);
        v[5] = _mm_srai_epi32(v[5], bit);

        v[6] = _mm_mullo_epi32(in[9 * col_num + col], cospi26);
        x    = _mm_mullo_epi32(in[6 * col_num + col], cospi38);
        v[6] = _mm_add_epi32(v[6], x);
        v[6] = _mm_add_epi32(v[6], rnding);
        v[6] = _mm_srai_epi32(v[6], bit);

        v[7] = _mm_mullo_epi32(in[9 * col_num + col], cospi38);
        x    = _mm_mullo_epi32(in[6 * col_num + col], cospi26);
        v[7] = _mm_sub_epi32(v[7], x);
        v[7] = _mm_add_epi32(v[7], rnding);
        v[7] = _mm_srai_epi32(v[7], bit);

        v[8] = _mm_mullo_epi32(in[7 * col_num + col], cospi34);
        x    = _mm_mullo_epi32(in[8 * col_num + col], cospi30);
        v[8] = _mm_add_epi32(v[8], x);
        v[8] = _mm_add_epi32(v[8], rnding);
        v[8] = _mm_srai_epi32(v[8], bit);

        v[9] = _mm_mullo_epi32(in[7 * col_num + col], cospi30);
        x    = _mm_mullo_epi32(in[8 * col_num + col], cospi34);
        v[9] = _mm_sub_epi32(v[9], x);
        v[9] = _mm_add_epi32(v[9], rnding);
        v[9] = _mm_srai_epi32(v[9], bit);

        v[10] = _mm_mullo_epi32(in[5 * col_num + col], cospi42);
        x     = _mm_mullo_epi32(in[10 * col_num + col], cospi22);
        v[10] = _mm_add_epi32(v[10], x);
        v[10] = _mm_add_epi32(v[10], rnding);
        v[10] = _mm_srai_epi32(v[10], bit);

        v[11] = _mm_mullo_epi32(in[5 * col_num + col], cospi22);
        x     = _mm_mullo_epi32(in[10 * col_num + col], cospi42);
        v[11] = _mm_sub_epi32(v[11], x);
        v[11] = _mm_add_epi32(v[11], rnding);
        v[11] = _mm_srai_epi32(v[11], bit);

        v[12] = _mm_mullo_epi32(in[3 * col_num + col], cospi50);
        x     = _mm_mullo_epi32(in[12 * col_num + col], cospi14);
        v[12] = _mm_add_epi32(v[12], x);
        v[12] = _mm_add_epi32(v[12], rnding);
        v[12] = _mm_srai_epi32(v[12], bit);

        v[13] = _mm_mullo_epi32(in[3 * col_num + col], cospi14);
        x     = _mm_mullo_epi32(in[12 * col_num + col], cospi50);
        v[13] = _mm_sub_epi32(v[13], x);
        v[13] = _mm_add_epi32(v[13], rnding);
        v[13] = _mm_srai_epi32(v[13], bit);

        v[14] = _mm_mullo_epi32(in[1 * col_num + col], cospi58);
        x     = _mm_mullo_epi32(in[14 * col_num + col], cospi6);
        v[14] = _mm_add_epi32(v[14], x);
        v[14] = _mm_add_epi32(v[14], rnding);
        v[14] = _mm_srai_epi32(v[14], bit);

        v[15] = _mm_mullo_epi32(in[1 * col_num + col], cospi6);
        x     = _mm_mullo_epi32(in[14 * col_num + col], cospi58);
        v[15] = _mm_sub_epi32(v[15], x);
        v[15] = _mm_add_epi32(v[15], rnding);
        v[15] = _mm_srai_epi32(v[15], bit);

        // stage 3
        u[0]  = _mm_add_epi32(v[0], v[8]);
        u[8]  = _mm_sub_epi32(v[0], v[8]);
        u[1]  = _mm_add_epi32(v[1], v[9]);
        u[9]  = _mm_sub_epi32(v[1], v[9]);
        u[2]  = _mm_add_epi32(v[2], v[10]);
        u[10] = _mm_sub_epi32(v[2], v[10]);
        u[3]  = _mm_add_epi32(v[3], v[11]);
        u[11] = _mm_sub_epi32(v[3], v[11]);
        u[4]  = _mm_add_epi32(v[4], v[12]);
        u[12] = _mm_sub_epi32(v[4], v[12]);
        u[5]  = _mm_add_epi32(v[5], v[13]);
        u[13] = _mm_sub_epi32(v[5], v[13]);
        u[6]  = _mm_add_epi32(v[6], v[14]);
        u[14] = _mm_sub_epi32(v[6], v[14]);
        u[7]  = _mm_add_epi32(v[7], v[15]);
        u[15] = _mm_sub_epi32(v[7], v[15]);

        // stage 4
        v[0] = u[0];
        v[1] = u[1];
        v[2] = u[2];
        v[3] = u[3];
        v[4] = u[4];
        v[5] = u[5];
        v[6] = u[6];
        v[7] = u[7];

        v[8] = _mm_mullo_epi32(u[8], cospi8);
        x    = _mm_mullo_epi32(u[9], cospi56);
        v[8] = _mm_add_epi32(v[8], x);
        v[8] = _mm_add_epi32(v[8], rnding);
        v[8] = _mm_srai_epi32(v[8], bit);

        v[9] = _mm_mullo_epi32(u[8], cospi56);
        x    = _mm_mullo_epi32(u[9], cospi8);
        v[9] = _mm_sub_epi32(v[9], x);
        v[9] = _mm_add_epi32(v[9], rnding);
        v[9] = _mm_srai_epi32(v[9], bit);

        v[10] = _mm_mullo_epi32(u[10], cospi40);
        x     = _mm_mullo_epi32(u[11], cospi24);
        v[10] = _mm_add_epi32(v[10], x);
        v[10] = _mm_add_epi32(v[10], rnding);
        v[10] = _mm_srai_epi32(v[10], bit);

        v[11] = _mm_mullo_epi32(u[10], cospi24);
        x     = _mm_mullo_epi32(u[11], cospi40);
        v[11] = _mm_sub_epi32(v[11], x);
        v[11] = _mm_add_epi32(v[11], rnding);
        v[11] = _mm_srai_epi32(v[11], bit);

        v[12] = _mm_mullo_epi32(u[12], cospim56);
        x     = _mm_mullo_epi32(u[13], cospi8);
        v[12] = _mm_add_epi32(v[12], x);
        v[12] = _mm_add_epi32(v[12], rnding);
        v[12] = _mm_srai_epi32(v[12], bit);

        v[13] = _mm_mullo_epi32(u[12], cospi8);
        x     = _mm_mullo_epi32(u[13], cospim56);
        v[13] = _mm_sub_epi32(v[13], x);
        v[13] = _mm_add_epi32(v[13], rnding);
        v[13] = _mm_srai_epi32(v[13], bit);

        v[14] = _mm_mullo_epi32(u[14], cospim24);
        x     = _mm_mullo_epi32(u[15], cospi40);
        v[14] = _mm_add_epi32(v[14], x);
        v[14] = _mm_add_epi32(v[14], rnding);
        v[14] = _mm_srai_epi32(v[14], bit);

        v[15] = _mm_mullo_epi32(u[14], cospi40);
        x     = _mm_mullo_epi32(u[15], cospim24);
        v[15] = _mm_sub_epi32(v[15], x);
        v[15] = _mm_add_epi32(v[15], rnding);
        v[15] = _mm_srai_epi32(v[15], bit);

        // stage 5
        u[0]  = _mm_add_epi32(v[0], v[4]);
        u[4]  = _mm_sub_epi32(v[0], v[4]);
        u[1]  = _mm_add_epi32(v[1], v[5]);
        u[5]  = _mm_sub_epi32(v[1], v[5]);
        u[2]  = _mm_add_epi32(v[2], v[6]);
        u[6]  = _mm_sub_epi32(v[2], v[6]);
        u[3]  = _mm_add_epi32(v[3], v[7]);
        u[7]  = _mm_sub_epi32(v[3], v[7]);
        u[8]  = _mm_add_epi32(v[8], v[12]);
        u[12] = _mm_sub_epi32(v[8], v[12]);
        u[9]  = _mm_add_epi32(v[9], v[13]);
        u[13] = _mm_sub_epi32(v[9], v[13]);
        u[10] = _mm_add_epi32(v[10], v[14]);
        u[14] = _mm_sub_epi32(v[10], v[14]);
        u[11] = _mm_add_epi32(v[11], v[15]);
        u[15] = _mm_sub_epi32(v[11], v[15]);

        // stage 6
        v[0] = u[0];
        v[1] = u[1];
        v[2] = u[2];
        v[3] = u[3];

        v[4] = _mm_mullo_epi32(u[4], cospi16);
        x    = _mm_mullo_epi32(u[5], cospi48);
        v[4] = _mm_add_epi32(v[4], x);
        v[4] = _mm_add_epi32(v[4], rnding);
        v[4] = _mm_srai_epi32(v[4], bit);

        v[5] = _mm_mullo_epi32(u[4], cospi48);
        x    = _mm_mullo_epi32(u[5], cospi16);
        v[5] = _mm_sub_epi32(v[5], x);
        v[5] = _mm_add_epi32(v[5], rnding);
        v[5] = _mm_srai_epi32(v[5], bit);

        v[6] = _mm_mullo_epi32(u[6], cospim48);
        x    = _mm_mullo_epi32(u[7], cospi16);
        v[6] = _mm_add_epi32(v[6], x);
        v[6] = _mm_add_epi32(v[6], rnding);
        v[6] = _mm_srai_epi32(v[6], bit);

        v[7] = _mm_mullo_epi32(u[6], cospi16);
        x    = _mm_mullo_epi32(u[7], cospim48);
        v[7] = _mm_sub_epi32(v[7], x);
        v[7] = _mm_add_epi32(v[7], rnding);
        v[7] = _mm_srai_epi32(v[7], bit);

        v[8]  = u[8];
        v[9]  = u[9];
        v[10] = u[10];
        v[11] = u[11];

        v[12] = _mm_mullo_epi32(u[12], cospi16);
        x     = _mm_mullo_epi32(u[13], cospi48);
        v[12] = _mm_add_epi32(v[12], x);
        v[12] = _mm_add_epi32(v[12], rnding);
        v[12] = _mm_srai_epi32(v[12], bit);

        v[13] = _mm_mullo_epi32(u[12], cospi48);
        x     = _mm_mullo_epi32(u[13], cospi16);
        v[13] = _mm_sub_epi32(v[13], x);
        v[13] = _mm_add_epi32(v[13], rnding);
        v[13] = _mm_srai_epi32(v[13], bit);

        v[14] = _mm_mullo_epi32(u[14], cospim48);
        x     = _mm_mullo_epi32(u[15], cospi16);
        v[14] = _mm_add_epi32(v[14], x);
        v[14] = _mm_add_epi32(v[14], rnding);
        v[14] = _mm_srai_epi32(v[14], bit);

        v[15] = _mm_mullo_epi32(u[14], cospi16);
        x     = _mm_mullo_epi32(u[15], cospim48);
        v[15] = _mm_sub_epi32(v[15], x);
        v[15] = _mm_add_epi32(v[15], rnding);
        v[15] = _mm_srai_epi32(v[15], bit);

        // stage 7
        u[0]  = _mm_add_epi32(v[0], v[2]);
        u[2]  = _mm_sub_epi32(v[0], v[2]);
        u[1]  = _mm_add_epi32(v[1], v[3]);
        u[3]  = _mm_sub_epi32(v[1], v[3]);
        u[4]  = _mm_add_epi32(v[4], v[6]);
        u[6]  = _mm_sub_epi32(v[4], v[6]);
        u[5]  = _mm_add_epi32(v[5], v[7]);
        u[7]  = _mm_sub_epi32(v[5], v[7]);
        u[8]  = _mm_add_epi32(v[8], v[10]);
        u[10] = _mm_sub_epi32(v[8], v[10]);
        u[9]  = _mm_add_epi32(v[9], v[11]);
        u[11] = _mm_sub_epi32(v[9], v[11]);
        u[12] = _mm_add_epi32(v[12], v[14]);
        u[14] = _mm_sub_epi32(v[12], v[14]);
        u[13] = _mm_add_epi32(v[13], v[15]);
        u[15] = _mm_sub_epi32(v[13], v[15]);

        // stage 8
        v[0] = u[0];
        v[1] = u[1];

        y    = _mm_mullo_epi32(u[2], cospi32);
        x    = _mm_mullo_epi32(u[3], cospi32);
        v[2] = _mm_add_epi32(y, x);
        v[2] = _mm_add_epi32(v[2], rnding);
        v[2] = _mm_srai_epi32(v[2], bit);

        v[3] = _mm_sub_epi32(y, x);
        v[3] = _mm_add_epi32(v[3], rnding);
        v[3] = _mm_srai_epi32(v[3], bit);

        v[4] = u[4];
        v[5] = u[5];

        y    = _mm_mullo_epi32(u[6], cospi32);
        x    = _mm_mullo_epi32(u[7], cospi32);
        v[6] = _mm_add_epi32(y, x);
        v[6] = _mm_add_epi32(v[6], rnding);
        v[6] = _mm_srai_epi32(v[6], bit);

        v[7] = _mm_sub_epi32(y, x);
        v[7] = _mm_add_epi32(v[7], rnding);
        v[7] = _mm_srai_epi32(v[7], bit);

        v[8] = u[8];
        v[9] = u[9];

        y     = _mm_mullo_epi32(u[10], cospi32);
        x     = _mm_mullo_epi32(u[11], cospi32);
        v[10] = _mm_add_epi32(y, x);
        v[10] = _mm_add_epi32(v[10], rnding);
        v[10] = _mm_srai_epi32(v[10], bit);

        v[11] = _mm_sub_epi32(y, x);
        v[11] = _mm_add_epi32(v[11], rnding);
        v[11] = _mm_srai_epi32(v[11], bit);

        v[12] = u[12];
        v[13] = u[13];

        y     = _mm_mullo_epi32(u[14], cospi32);
        x     = _mm_mullo_epi32(u[15], cospi32);
        v[14] = _mm_add_epi32(y, x);
        v[14] = _mm_add_epi32(v[14], rnding);
        v[14] = _mm_srai_epi32(v[14], bit);

        v[15] = _mm_sub_epi32(y, x);
        v[15] = _mm_add_epi32(v[15], rnding);
        v[15] = _mm_srai_epi32(v[15], bit);

        // stage 9
        out[0 * col_num + col]  = v[0];
        out[1 * col_num + col]  = _mm_sub_epi32(_mm_setzero_si128(), v[8]);
        out[2 * col_num + col]  = v[12];
        out[3 * col_num + col]  = _mm_sub_epi32(_mm_setzero_si128(), v[4]);
        out[4 * col_num + col]  = v[6];
        out[5 * col_num + col]  = _mm_sub_epi32(_mm_setzero_si128(), v[14]);
        out[6 * col_num + col]  = v[10];
        out[7 * col_num + col]  = _mm_sub_epi32(_mm_setzero_si128(), v[2]);
        out[8 * col_num + col]  = v[3];
        out[9 * col_num + col]  = _mm_sub_epi32(_mm_setzero_si128(), v[11]);
        out[10 * col_num + col] = v[15];
        out[11 * col_num + col] = _mm_sub_epi32(_mm_setzero_si128(), v[7]);
        out[12 * col_num + col] = v[5];
        out[13 * col_num + col] = _mm_sub_epi32(_mm_setzero_si128(), v[13]);
        out[14 * col_num + col] = v[9];
        out[15 * col_num + col] = _mm_sub_epi32(_mm_setzero_si128(), v[1]);
    }
}

static void round_shift_16x16(__m128i *in, int32_t shift) {
    round_shift_8x8(&in[0], shift);
    round_shift_8x8(&in[16], shift);
    round_shift_8x8(&in[32], shift);
    round_shift_8x8(&in[48], shift);
}

static INLINE void iidentity16_and_round_shift_sse4_1(__m128i *input, int32_t shift) {
    // Input takes 18 bits, can be multiplied with new_sqrt2 in 32 bits space.
    // Multiplied by half value new_sqrt2, instead (2*new_sqrt2),
    // and round_shift() by one bit less (new_sqrt2_bits-1).
    // round_shift(new_sqrt2_bits-1) and next round_shift(shift) in one pass.
    const __m128i scalar = _mm_set1_epi32(new_sqrt2);
    const __m128i rnding = _mm_set1_epi32((1 << (new_sqrt2_bits - 2)) +
                                          (!!(shift) << (shift + new_sqrt2_bits - 2)));

    for (int32_t i = 0; i < 64; i++) {
        input[i] = _mm_mullo_epi32(input[i], scalar);
        input[i] = _mm_add_epi32(input[i], rnding);
        input[i] = _mm_srai_epi32(input[i], new_sqrt2_bits - 1 + shift);
    }
}

void svt_av1_inv_txfm2d_add_16x16_sse4_1(const int32_t *input, uint16_t *output_r, int32_t stride_r,
                                         uint16_t *output_w, int32_t stride_w, TxType tx_type,
                                         int32_t bd) {
    __m128i       in[64], out[64];
    const int8_t *shift   = eb_inv_txfm_shift_ls[TX_16X16];
    const int32_t txw_idx = get_txw_idx(TX_16X16);
    const int32_t txh_idx = get_txh_idx(TX_16X16);

    switch (tx_type) {
    case DCT_DCT:
        load_buffer_16x16(input, in);
        transpose_16x16(in, out);
        idct16x16_sse4_1(out, in, inv_cos_bit_row[txw_idx][txh_idx]);
        round_shift_16x16(in, -shift[0]);
        transpose_16x16(in, out);
        idct16x16_sse4_1(out, in, inv_cos_bit_col[txw_idx][txh_idx]);
        write_buffer_16x16(in, output_r, stride_r, output_w, stride_w, 0, 0, -shift[1], bd);
        break;
    case DCT_ADST:
        load_buffer_16x16(input, in);
        transpose_16x16(in, out);
        iadst16x16_sse4_1(out, in, inv_cos_bit_row[txw_idx][txh_idx]);
        round_shift_16x16(in, -shift[0]);
        transpose_16x16(in, out);
        idct16x16_sse4_1(out, in, inv_cos_bit_col[txw_idx][txh_idx]);
        write_buffer_16x16(in, output_r, stride_r, output_w, stride_w, 0, 0, -shift[1], bd);
        break;
    case ADST_DCT:
        load_buffer_16x16(input, in);
        transpose_16x16(in, out);
        idct16x16_sse4_1(out, in, inv_cos_bit_row[txw_idx][txh_idx]);
        round_shift_16x16(in, -shift[0]);
        transpose_16x16(in, out);
        iadst16x16_sse4_1(out, in, inv_cos_bit_col[txw_idx][txh_idx]);
        write_buffer_16x16(in, output_r, stride_r, output_w, stride_w, 0, 0, -shift[1], bd);
        break;
    case ADST_ADST:
        load_buffer_16x16(input, in);
        transpose_16x16(in, out);
        iadst16x16_sse4_1(out, in, inv_cos_bit_row[txw_idx][txh_idx]);
        round_shift_16x16(in, -shift[0]);
        transpose_16x16(in, out);
        iadst16x16_sse4_1(out, in, inv_cos_bit_col[txw_idx][txh_idx]);
        write_buffer_16x16(in, output_r, stride_r, output_w, stride_w, 0, 0, -shift[1], bd);
        break;
    case FLIPADST_DCT:
        load_buffer_16x16(input, in);
        transpose_16x16(in, out);
        idct16x16_sse4_1(out, in, inv_cos_bit_row[txw_idx][txh_idx]);
        round_shift_16x16(in, -shift[0]);
        transpose_16x16(in, out);
        iadst16x16_sse4_1(out, in, inv_cos_bit_col[txw_idx][txh_idx]);
        write_buffer_16x16(in, output_r, stride_r, output_w, stride_w, 0, 1, -shift[1], bd);
        break;
    case DCT_FLIPADST:
        load_buffer_16x16(input, in);
        transpose_16x16(in, out);
        iadst16x16_sse4_1(out, in, inv_cos_bit_row[txw_idx][txh_idx]);
        round_shift_16x16(in, -shift[0]);
        transpose_16x16(in, out);
        idct16x16_sse4_1(out, in, inv_cos_bit_col[txw_idx][txh_idx]);
        write_buffer_16x16(in, output_r, stride_r, output_w, stride_w, 1, 0, -shift[1], bd);
        break;
    case ADST_FLIPADST:
        load_buffer_16x16(input, in);
        transpose_16x16(in, out);
        iadst16x16_sse4_1(out, in, inv_cos_bit_row[txw_idx][txh_idx]);
        round_shift_16x16(in, -shift[0]);
        transpose_16x16(in, out);
        iadst16x16_sse4_1(out, in, inv_cos_bit_col[txw_idx][txh_idx]);
        write_buffer_16x16(in, output_r, stride_r, output_w, stride_w, 1, 0, -shift[1], bd);
        break;
    case FLIPADST_FLIPADST:
        load_buffer_16x16(input, in);
        transpose_16x16(in, out);
        iadst16x16_sse4_1(out, in, inv_cos_bit_row[txw_idx][txh_idx]);
        round_shift_16x16(in, -shift[0]);
        transpose_16x16(in, out);
        iadst16x16_sse4_1(out, in, inv_cos_bit_col[txw_idx][txh_idx]);
        write_buffer_16x16(in, output_r, stride_r, output_w, stride_w, 1, 1, -shift[1], bd);
        break;
    case FLIPADST_ADST:
        load_buffer_16x16(input, in);
        transpose_16x16(in, out);
        iadst16x16_sse4_1(out, in, inv_cos_bit_row[txw_idx][txh_idx]);
        round_shift_16x16(in, -shift[0]);
        transpose_16x16(in, out);
        iadst16x16_sse4_1(out, in, inv_cos_bit_col[txw_idx][txh_idx]);
        write_buffer_16x16(in, output_r, stride_r, output_w, stride_w, 0, 1, -shift[1], bd);
        break;
    case IDTX:
        load_buffer_16x16(input, in);
        iidentity16_and_round_shift_sse4_1(in, -shift[0]);
        iidentity16_and_round_shift_sse4_1(in, -shift[1]);
        write_buffer_16x16(in, output_r, stride_r, output_w, stride_w, 0, 0, 0, bd);
        break;
    case V_DCT:
        load_buffer_16x16(input, in);
        iidentity16_and_round_shift_sse4_1(in, -shift[0]);
        idct16x16_sse4_1(in, out, inv_cos_bit_col[txw_idx][txh_idx]);
        write_buffer_16x16(out, output_r, stride_r, output_w, stride_w, 0, 0, -shift[1], bd);
        break;
    case H_DCT:
        load_buffer_16x16(input, in);
        transpose_16x16(in, out);
        idct16x16_sse4_1(out, in, inv_cos_bit_row[txw_idx][txh_idx]);
        transpose_16x16(in, out);
        round_shift_16x16(out, -shift[0]);
        iidentity16_and_round_shift_sse4_1(out, -shift[1]);
        write_buffer_16x16(out, output_r, stride_r, output_w, stride_w, 0, 0, 0, bd);
        break;
    case V_ADST:
        load_buffer_16x16(input, in);
        iidentity16_and_round_shift_sse4_1(in, -shift[0]);
        iadst16x16_sse4_1(in, out, inv_cos_bit_col[txw_idx][txh_idx]);
        write_buffer_16x16(out, output_r, stride_r, output_w, stride_w, 0, 0, -shift[1], bd);
        break;
    case H_ADST:
        load_buffer_16x16(input, in);
        transpose_16x16(in, out);
        iadst16x16_sse4_1(out, in, inv_cos_bit_row[txw_idx][txh_idx]);
        transpose_16x16(in, out);
        round_shift_16x16(out, -shift[0]);
        iidentity16_and_round_shift_sse4_1(out, -shift[1]);
        write_buffer_16x16(out, output_r, stride_r, output_w, stride_w, 0, 0, 0, bd);
        break;
    case V_FLIPADST:
        load_buffer_16x16(input, in);
        iidentity16_and_round_shift_sse4_1(in, -shift[0]);
        iadst16x16_sse4_1(in, out, inv_cos_bit_col[txw_idx][txh_idx]);
        write_buffer_16x16(out, output_r, stride_r, output_w, stride_w, 0, 1, -shift[1], bd);
        break;
    case H_FLIPADST:
        load_buffer_16x16(input, in);
        transpose_16x16(in, out);
        iadst16x16_sse4_1(out, in, inv_cos_bit_row[txw_idx][txh_idx]);
        transpose_16x16(in, out);
        round_shift_16x16(out, -shift[0]);
        iidentity16_and_round_shift_sse4_1(out, -shift[1]);
        write_buffer_16x16(out, output_r, stride_r, output_w, stride_w, 1, 0, 0, bd);
        break;
    default:
        svt_av1_inv_txfm2d_add_16x16_c(input, output_r, stride_r, output_w, stride_w, tx_type, bd);
    }
}

static void load_buffer_64x64_lower_32x32(const int32_t *coeff, __m128i *in) {
    int32_t i, j;

    __m128i zero = _mm_setzero_si128();

    for (i = 0; i < 32; ++i) {
        for (j = 0; j < 8; ++j) {
            in[16 * i + j]     = _mm_loadu_si128((const __m128i *)(coeff + 32 * i + 4 * j));
            in[16 * i + j + 8] = zero;
        }
    }

    for (i = 0; i < 512; ++i) in[512 + i] = zero;
}

static void transpose_64x64(__m128i *in, __m128i *out, int32_t do_cols) {
    int32_t i, j;
    for (i = 0; i < (do_cols ? 16 : 8); ++i) {
        for (j = 0; j < 8; ++j) {
            TRANSPOSE_4X4(in[(4 * i + 0) * 16 + j],
                          in[(4 * i + 1) * 16 + j],
                          in[(4 * i + 2) * 16 + j],
                          in[(4 * i + 3) * 16 + j],
                          out[(4 * j + 0) * 16 + i],
                          out[(4 * j + 1) * 16 + i],
                          out[(4 * j + 2) * 16 + i],
                          out[(4 * j + 3) * 16 + i]);
        }
    }
}

static void round_shift_32x32(__m128i *in, int32_t shift) {
    round_shift_16x16(&in[0], shift);
    round_shift_16x16(&in[64], shift);
    round_shift_16x16(&in[128], shift);
    round_shift_16x16(&in[192], shift);
}

static void round_shift_64x64(__m128i *in, int32_t shift) {
    round_shift_32x32(&in[0], shift);
    round_shift_32x32(&in[256], shift);
}

static void assign_16x16_input_from_32x32(const __m128i *in, __m128i *in16x16, int32_t col) {
    int32_t i;
    for (i = 0; i < 16 * 16 / 4; i += 4) {
        in16x16[i]     = in[col];
        in16x16[i + 1] = in[col + 1];
        in16x16[i + 2] = in[col + 2];
        in16x16[i + 3] = in[col + 3];
        col += 8;
    }
}

static void write_buffer_32x32(__m128i *in, uint16_t *output_r, int32_t stride_r,
                               uint16_t *output_w, int32_t stride_w, int32_t fliplr, int32_t flipud,
                               int32_t shift, int32_t bd) {
    __m128i   in16x16[16 * 16 / 4];
    uint16_t *left_up_r    = &output_r[0];
    uint16_t *right_up_r   = &output_r[16];
    uint16_t *left_down_r  = &output_r[16 * stride_r];
    uint16_t *right_down_r = &output_r[16 * stride_r + 16];
    uint16_t *left_up_w    = &output_w[0];
    uint16_t *right_up_w   = &output_w[16];
    uint16_t *left_down_w  = &output_w[16 * stride_w];
    uint16_t *right_down_w = &output_w[16 * stride_w + 16];

    if (fliplr) {
        swap_addr(&left_up_r, &right_up_r);
        swap_addr(&left_down_r, &right_down_r);
        swap_addr(&left_up_w, &right_up_w);
        swap_addr(&left_down_w, &right_down_w);
    }

    if (flipud) {
        swap_addr(&left_up_r, &left_down_r);
        swap_addr(&right_up_r, &right_down_r);
        swap_addr(&left_up_w, &left_down_w);
        swap_addr(&right_up_w, &right_down_w);
    }

    // Left-up quarter
    assign_16x16_input_from_32x32(in, in16x16, 0);
    write_buffer_16x16(
        in16x16, left_up_r, stride_r, left_up_w, stride_w, fliplr, flipud, shift, bd);

    // Right-up quarter
    assign_16x16_input_from_32x32(in, in16x16, 32 / 2 / 4);
    write_buffer_16x16(
        in16x16, right_up_r, stride_r, right_up_w, stride_w, fliplr, flipud, shift, bd);

    // Left-down quarter
    assign_16x16_input_from_32x32(in, in16x16, 32 * 32 / 2 / 4);
    write_buffer_16x16(
        in16x16, left_down_r, stride_r, left_down_w, stride_w, fliplr, flipud, shift, bd);

    // Right-down quarter
    assign_16x16_input_from_32x32(in, in16x16, 32 * 32 / 2 / 4 + 32 / 2 / 4);
    write_buffer_16x16(
        in16x16, right_down_r, stride_r, right_down_w, stride_w, fliplr, flipud, shift, bd);
}

static void assign_32x32_input_from_64x64(const __m128i *in, __m128i *in32x32, int32_t col) {
    int32_t i;
    for (i = 0; i < 32 * 32 / 4; i += 8) {
        in32x32[i]     = in[col];
        in32x32[i + 1] = in[col + 1];
        in32x32[i + 2] = in[col + 2];
        in32x32[i + 3] = in[col + 3];
        in32x32[i + 4] = in[col + 4];
        in32x32[i + 5] = in[col + 5];
        in32x32[i + 6] = in[col + 6];
        in32x32[i + 7] = in[col + 7];
        col += 16;
    }
}

static void write_buffer_64x64(__m128i *in, uint16_t *output_r, int32_t stride_r,
                               uint16_t *output_w, int32_t stride_w, int32_t fliplr, int32_t flipud,
                               int32_t shift, int32_t bd) {
    __m128i   in32x32[32 * 32 / 4];
    uint16_t *left_up_r    = &output_r[0];
    uint16_t *right_up_r   = &output_r[32];
    uint16_t *left_down_r  = &output_r[32 * stride_r];
    uint16_t *right_down_r = &output_r[32 * stride_r + 32];
    uint16_t *left_up_w    = &output_w[0];
    uint16_t *right_up_w   = &output_w[32];
    uint16_t *left_down_w  = &output_w[32 * stride_w];
    uint16_t *right_down_w = &output_w[32 * stride_w + 32];

    if (fliplr) {
        swap_addr(&left_up_r, &right_up_r);
        swap_addr(&left_down_r, &right_down_r);
        swap_addr(&left_up_w, &right_up_w);
        swap_addr(&left_down_w, &right_down_w);
    }

    if (flipud) {
        swap_addr(&left_up_r, &left_down_r);
        swap_addr(&right_up_r, &right_down_r);
        swap_addr(&left_up_w, &left_down_w);
        swap_addr(&right_up_w, &right_down_w);
    }

    // Left-up quarter
    assign_32x32_input_from_64x64(in, in32x32, 0);
    write_buffer_32x32(
        in32x32, left_up_r, stride_r, left_up_w, stride_w, fliplr, flipud, shift, bd);

    // Right-up quarter
    assign_32x32_input_from_64x64(in, in32x32, 64 / 2 / 4);
    write_buffer_32x32(
        in32x32, right_up_r, stride_r, right_up_w, stride_w, fliplr, flipud, shift, bd);

    // Left-down quarter
    assign_32x32_input_from_64x64(in, in32x32, 64 * 64 / 2 / 4);
    write_buffer_32x32(
        in32x32, left_down_r, stride_r, left_down_w, stride_w, fliplr, flipud, shift, bd);

    // Right-down quarter
    assign_32x32_input_from_64x64(in, in32x32, 64 * 64 / 2 / 4 + 64 / 2 / 4);
    write_buffer_32x32(
        in32x32, right_down_r, stride_r, right_down_w, stride_w, fliplr, flipud, shift, bd);
}

static void idct64x64_sse4_1(__m128i *in, __m128i *out, int32_t bit, int32_t do_cols, int32_t bd) {
    int32_t        i, j;
    const int32_t *cospi     = cospi_arr(bit);
    const __m128i  rnding    = _mm_set1_epi32(1 << (bit - 1));
    const int32_t  log_range = AOMMAX(16, bd + (do_cols ? 6 : 8));
    const __m128i  clamp_lo  = _mm_set1_epi32(-(1 << (log_range - 1)));
    const __m128i  clamp_hi  = _mm_set1_epi32((1 << (log_range - 1)) - 1);
    int32_t        col;

    const __m128i cospi1  = _mm_set1_epi32(cospi[1]);
    const __m128i cospi2  = _mm_set1_epi32(cospi[2]);
    const __m128i cospi3  = _mm_set1_epi32(cospi[3]);
    const __m128i cospi4  = _mm_set1_epi32(cospi[4]);
    const __m128i cospi5  = _mm_set1_epi32(cospi[5]);
    const __m128i cospi6  = _mm_set1_epi32(cospi[6]);
    const __m128i cospi7  = _mm_set1_epi32(cospi[7]);
    const __m128i cospi8  = _mm_set1_epi32(cospi[8]);
    const __m128i cospi9  = _mm_set1_epi32(cospi[9]);
    const __m128i cospi10 = _mm_set1_epi32(cospi[10]);
    const __m128i cospi11 = _mm_set1_epi32(cospi[11]);
    const __m128i cospi12 = _mm_set1_epi32(cospi[12]);
    const __m128i cospi13 = _mm_set1_epi32(cospi[13]);
    const __m128i cospi14 = _mm_set1_epi32(cospi[14]);
    const __m128i cospi15 = _mm_set1_epi32(cospi[15]);
    const __m128i cospi16 = _mm_set1_epi32(cospi[16]);
    const __m128i cospi17 = _mm_set1_epi32(cospi[17]);
    const __m128i cospi18 = _mm_set1_epi32(cospi[18]);
    const __m128i cospi19 = _mm_set1_epi32(cospi[19]);
    const __m128i cospi20 = _mm_set1_epi32(cospi[20]);
    const __m128i cospi21 = _mm_set1_epi32(cospi[21]);
    const __m128i cospi22 = _mm_set1_epi32(cospi[22]);
    const __m128i cospi23 = _mm_set1_epi32(cospi[23]);
    const __m128i cospi24 = _mm_set1_epi32(cospi[24]);
    const __m128i cospi25 = _mm_set1_epi32(cospi[25]);
    const __m128i cospi26 = _mm_set1_epi32(cospi[26]);
    const __m128i cospi27 = _mm_set1_epi32(cospi[27]);
    const __m128i cospi28 = _mm_set1_epi32(cospi[28]);
    const __m128i cospi29 = _mm_set1_epi32(cospi[29]);
    const __m128i cospi30 = _mm_set1_epi32(cospi[30]);
    const __m128i cospi31 = _mm_set1_epi32(cospi[31]);
    const __m128i cospi32 = _mm_set1_epi32(cospi[32]);
    const __m128i cospi35 = _mm_set1_epi32(cospi[35]);
    const __m128i cospi36 = _mm_set1_epi32(cospi[36]);
    const __m128i cospi38 = _mm_set1_epi32(cospi[38]);
    const __m128i cospi39 = _mm_set1_epi32(cospi[39]);
    const __m128i cospi40 = _mm_set1_epi32(cospi[40]);
    const __m128i cospi43 = _mm_set1_epi32(cospi[43]);
    const __m128i cospi44 = _mm_set1_epi32(cospi[44]);
    const __m128i cospi46 = _mm_set1_epi32(cospi[46]);
    const __m128i cospi47 = _mm_set1_epi32(cospi[47]);
    const __m128i cospi48 = _mm_set1_epi32(cospi[48]);
    const __m128i cospi51 = _mm_set1_epi32(cospi[51]);
    const __m128i cospi52 = _mm_set1_epi32(cospi[52]);
    const __m128i cospi54 = _mm_set1_epi32(cospi[54]);
    const __m128i cospi55 = _mm_set1_epi32(cospi[55]);
    const __m128i cospi56 = _mm_set1_epi32(cospi[56]);
    const __m128i cospi59 = _mm_set1_epi32(cospi[59]);
    const __m128i cospi60 = _mm_set1_epi32(cospi[60]);
    const __m128i cospi62 = _mm_set1_epi32(cospi[62]);
    const __m128i cospi63 = _mm_set1_epi32(cospi[63]);

    const __m128i cospim4  = _mm_set1_epi32(-cospi[4]);
    const __m128i cospim8  = _mm_set1_epi32(-cospi[8]);
    const __m128i cospim12 = _mm_set1_epi32(-cospi[12]);
    const __m128i cospim16 = _mm_set1_epi32(-cospi[16]);
    const __m128i cospim20 = _mm_set1_epi32(-cospi[20]);
    const __m128i cospim24 = _mm_set1_epi32(-cospi[24]);
    const __m128i cospim28 = _mm_set1_epi32(-cospi[28]);
    const __m128i cospim32 = _mm_set1_epi32(-cospi[32]);
    const __m128i cospim33 = _mm_set1_epi32(-cospi[33]);
    const __m128i cospim34 = _mm_set1_epi32(-cospi[34]);
    const __m128i cospim36 = _mm_set1_epi32(-cospi[36]);
    const __m128i cospim37 = _mm_set1_epi32(-cospi[37]);
    const __m128i cospim40 = _mm_set1_epi32(-cospi[40]);
    const __m128i cospim41 = _mm_set1_epi32(-cospi[41]);
    const __m128i cospim42 = _mm_set1_epi32(-cospi[42]);
    const __m128i cospim44 = _mm_set1_epi32(-cospi[44]);
    const __m128i cospim45 = _mm_set1_epi32(-cospi[45]);
    const __m128i cospim48 = _mm_set1_epi32(-cospi[48]);
    const __m128i cospim49 = _mm_set1_epi32(-cospi[49]);
    const __m128i cospim50 = _mm_set1_epi32(-cospi[50]);
    const __m128i cospim52 = _mm_set1_epi32(-cospi[52]);
    const __m128i cospim53 = _mm_set1_epi32(-cospi[53]);
    const __m128i cospim56 = _mm_set1_epi32(-cospi[56]);
    const __m128i cospim57 = _mm_set1_epi32(-cospi[57]);
    const __m128i cospim58 = _mm_set1_epi32(-cospi[58]);
    const __m128i cospim60 = _mm_set1_epi32(-cospi[60]);
    const __m128i cospim61 = _mm_set1_epi32(-cospi[61]);

    for (col = 0; col < (do_cols ? 64 / 4 : 32 / 4); ++col) {
        __m128i u[64], v[64];

        // stage 1
        u[32] = in[1 * 16 + col];
        u[34] = in[17 * 16 + col];
        u[36] = in[9 * 16 + col];
        u[38] = in[25 * 16 + col];
        u[40] = in[5 * 16 + col];
        u[42] = in[21 * 16 + col];
        u[44] = in[13 * 16 + col];
        u[46] = in[29 * 16 + col];
        u[48] = in[3 * 16 + col];
        u[50] = in[19 * 16 + col];
        u[52] = in[11 * 16 + col];
        u[54] = in[27 * 16 + col];
        u[56] = in[7 * 16 + col];
        u[58] = in[23 * 16 + col];
        u[60] = in[15 * 16 + col];
        u[62] = in[31 * 16 + col];

        v[16] = in[2 * 16 + col];
        v[18] = in[18 * 16 + col];
        v[20] = in[10 * 16 + col];
        v[22] = in[26 * 16 + col];
        v[24] = in[6 * 16 + col];
        v[26] = in[22 * 16 + col];
        v[28] = in[14 * 16 + col];
        v[30] = in[30 * 16 + col];

        u[8]  = in[4 * 16 + col];
        u[10] = in[20 * 16 + col];
        u[12] = in[12 * 16 + col];
        u[14] = in[28 * 16 + col];

        v[4] = in[8 * 16 + col];
        v[6] = in[24 * 16 + col];

        u[0] = in[0 * 16 + col];
        u[2] = in[16 * 16 + col];

        // stage 2
        v[32] = half_btf_0_sse4_1(&cospi63, &u[32], &rnding, bit);
        v[33] = half_btf_0_sse4_1(&cospim33, &u[62], &rnding, bit);
        v[34] = half_btf_0_sse4_1(&cospi47, &u[34], &rnding, bit);
        v[35] = half_btf_0_sse4_1(&cospim49, &u[60], &rnding, bit);
        v[36] = half_btf_0_sse4_1(&cospi55, &u[36], &rnding, bit);
        v[37] = half_btf_0_sse4_1(&cospim41, &u[58], &rnding, bit);
        v[38] = half_btf_0_sse4_1(&cospi39, &u[38], &rnding, bit);
        v[39] = half_btf_0_sse4_1(&cospim57, &u[56], &rnding, bit);
        v[40] = half_btf_0_sse4_1(&cospi59, &u[40], &rnding, bit);
        v[41] = half_btf_0_sse4_1(&cospim37, &u[54], &rnding, bit);
        v[42] = half_btf_0_sse4_1(&cospi43, &u[42], &rnding, bit);
        v[43] = half_btf_0_sse4_1(&cospim53, &u[52], &rnding, bit);
        v[44] = half_btf_0_sse4_1(&cospi51, &u[44], &rnding, bit);
        v[45] = half_btf_0_sse4_1(&cospim45, &u[50], &rnding, bit);
        v[46] = half_btf_0_sse4_1(&cospi35, &u[46], &rnding, bit);
        v[47] = half_btf_0_sse4_1(&cospim61, &u[48], &rnding, bit);
        v[48] = half_btf_0_sse4_1(&cospi3, &u[48], &rnding, bit);
        v[49] = half_btf_0_sse4_1(&cospi29, &u[46], &rnding, bit);
        v[50] = half_btf_0_sse4_1(&cospi19, &u[50], &rnding, bit);
        v[51] = half_btf_0_sse4_1(&cospi13, &u[44], &rnding, bit);
        v[52] = half_btf_0_sse4_1(&cospi11, &u[52], &rnding, bit);
        v[53] = half_btf_0_sse4_1(&cospi21, &u[42], &rnding, bit);
        v[54] = half_btf_0_sse4_1(&cospi27, &u[54], &rnding, bit);
        v[55] = half_btf_0_sse4_1(&cospi5, &u[40], &rnding, bit);
        v[56] = half_btf_0_sse4_1(&cospi7, &u[56], &rnding, bit);
        v[57] = half_btf_0_sse4_1(&cospi25, &u[38], &rnding, bit);
        v[58] = half_btf_0_sse4_1(&cospi23, &u[58], &rnding, bit);
        v[59] = half_btf_0_sse4_1(&cospi9, &u[36], &rnding, bit);
        v[60] = half_btf_0_sse4_1(&cospi15, &u[60], &rnding, bit);
        v[61] = half_btf_0_sse4_1(&cospi17, &u[34], &rnding, bit);
        v[62] = half_btf_0_sse4_1(&cospi31, &u[62], &rnding, bit);
        v[63] = half_btf_0_sse4_1(&cospi1, &u[32], &rnding, bit);

        // stage 3
        u[16] = half_btf_0_sse4_1(&cospi62, &v[16], &rnding, bit);
        u[17] = half_btf_0_sse4_1(&cospim34, &v[30], &rnding, bit);
        u[18] = half_btf_0_sse4_1(&cospi46, &v[18], &rnding, bit);
        u[19] = half_btf_0_sse4_1(&cospim50, &v[28], &rnding, bit);
        u[20] = half_btf_0_sse4_1(&cospi54, &v[20], &rnding, bit);
        u[21] = half_btf_0_sse4_1(&cospim42, &v[26], &rnding, bit);
        u[22] = half_btf_0_sse4_1(&cospi38, &v[22], &rnding, bit);
        u[23] = half_btf_0_sse4_1(&cospim58, &v[24], &rnding, bit);
        u[24] = half_btf_0_sse4_1(&cospi6, &v[24], &rnding, bit);
        u[25] = half_btf_0_sse4_1(&cospi26, &v[22], &rnding, bit);
        u[26] = half_btf_0_sse4_1(&cospi22, &v[26], &rnding, bit);
        u[27] = half_btf_0_sse4_1(&cospi10, &v[20], &rnding, bit);
        u[28] = half_btf_0_sse4_1(&cospi14, &v[28], &rnding, bit);
        u[29] = half_btf_0_sse4_1(&cospi18, &v[18], &rnding, bit);
        u[30] = half_btf_0_sse4_1(&cospi30, &v[30], &rnding, bit);
        u[31] = half_btf_0_sse4_1(&cospi2, &v[16], &rnding, bit);

        for (i = 32; i < 64; i += 4) {
            addsub_sse4_1(v[i + 0], v[i + 1], &u[i + 0], &u[i + 1], &clamp_lo, &clamp_hi);
            addsub_sse4_1(v[i + 3], v[i + 2], &u[i + 3], &u[i + 2], &clamp_lo, &clamp_hi);
        }

        // stage 4
        v[8]  = half_btf_0_sse4_1(&cospi60, &u[8], &rnding, bit);
        v[9]  = half_btf_0_sse4_1(&cospim36, &u[14], &rnding, bit);
        v[10] = half_btf_0_sse4_1(&cospi44, &u[10], &rnding, bit);
        v[11] = half_btf_0_sse4_1(&cospim52, &u[12], &rnding, bit);
        v[12] = half_btf_0_sse4_1(&cospi12, &u[12], &rnding, bit);
        v[13] = half_btf_0_sse4_1(&cospi20, &u[10], &rnding, bit);
        v[14] = half_btf_0_sse4_1(&cospi28, &u[14], &rnding, bit);
        v[15] = half_btf_0_sse4_1(&cospi4, &u[8], &rnding, bit);

        for (i = 16; i < 32; i += 4) {
            addsub_sse4_1(u[i + 0], u[i + 1], &v[i + 0], &v[i + 1], &clamp_lo, &clamp_hi);
            addsub_sse4_1(u[i + 3], u[i + 2], &v[i + 3], &v[i + 2], &clamp_lo, &clamp_hi);
        }

        for (i = 32; i < 64; i += 4) {
            v[i + 0] = u[i + 0];
            v[i + 3] = u[i + 3];
        }

        v[33] = half_btf_sse4_1(&cospim4, &u[33], &cospi60, &u[62], &rnding, bit);
        v[34] = half_btf_sse4_1(&cospim60, &u[34], &cospim4, &u[61], &rnding, bit);
        v[37] = half_btf_sse4_1(&cospim36, &u[37], &cospi28, &u[58], &rnding, bit);
        v[38] = half_btf_sse4_1(&cospim28, &u[38], &cospim36, &u[57], &rnding, bit);
        v[41] = half_btf_sse4_1(&cospim20, &u[41], &cospi44, &u[54], &rnding, bit);
        v[42] = half_btf_sse4_1(&cospim44, &u[42], &cospim20, &u[53], &rnding, bit);
        v[45] = half_btf_sse4_1(&cospim52, &u[45], &cospi12, &u[50], &rnding, bit);
        v[46] = half_btf_sse4_1(&cospim12, &u[46], &cospim52, &u[49], &rnding, bit);
        v[49] = half_btf_sse4_1(&cospim52, &u[46], &cospi12, &u[49], &rnding, bit);
        v[50] = half_btf_sse4_1(&cospi12, &u[45], &cospi52, &u[50], &rnding, bit);
        v[53] = half_btf_sse4_1(&cospim20, &u[42], &cospi44, &u[53], &rnding, bit);
        v[54] = half_btf_sse4_1(&cospi44, &u[41], &cospi20, &u[54], &rnding, bit);
        v[57] = half_btf_sse4_1(&cospim36, &u[38], &cospi28, &u[57], &rnding, bit);
        v[58] = half_btf_sse4_1(&cospi28, &u[37], &cospi36, &u[58], &rnding, bit);
        v[61] = half_btf_sse4_1(&cospim4, &u[34], &cospi60, &u[61], &rnding, bit);
        v[62] = half_btf_sse4_1(&cospi60, &u[33], &cospi4, &u[62], &rnding, bit);

        // stage 5
        u[4] = half_btf_0_sse4_1(&cospi56, &v[4], &rnding, bit);
        u[5] = half_btf_0_sse4_1(&cospim40, &v[6], &rnding, bit);
        u[6] = half_btf_0_sse4_1(&cospi24, &v[6], &rnding, bit);
        u[7] = half_btf_0_sse4_1(&cospi8, &v[4], &rnding, bit);

        for (i = 8; i < 16; i += 4) {
            addsub_sse4_1(v[i + 0], v[i + 1], &u[i + 0], &u[i + 1], &clamp_lo, &clamp_hi);
            addsub_sse4_1(v[i + 3], v[i + 2], &u[i + 3], &u[i + 2], &clamp_lo, &clamp_hi);
        }

        for (i = 16; i < 32; i += 4) {
            u[i + 0] = v[i + 0];
            u[i + 3] = v[i + 3];
        }

        u[17] = half_btf_sse4_1(&cospim8, &v[17], &cospi56, &v[30], &rnding, bit);
        u[18] = half_btf_sse4_1(&cospim56, &v[18], &cospim8, &v[29], &rnding, bit);
        u[21] = half_btf_sse4_1(&cospim40, &v[21], &cospi24, &v[26], &rnding, bit);
        u[22] = half_btf_sse4_1(&cospim24, &v[22], &cospim40, &v[25], &rnding, bit);
        u[25] = half_btf_sse4_1(&cospim40, &v[22], &cospi24, &v[25], &rnding, bit);
        u[26] = half_btf_sse4_1(&cospi24, &v[21], &cospi40, &v[26], &rnding, bit);
        u[29] = half_btf_sse4_1(&cospim8, &v[18], &cospi56, &v[29], &rnding, bit);
        u[30] = half_btf_sse4_1(&cospi56, &v[17], &cospi8, &v[30], &rnding, bit);

        for (i = 32; i < 64; i += 8) {
            addsub_sse4_1(v[i + 0], v[i + 3], &u[i + 0], &u[i + 3], &clamp_lo, &clamp_hi);
            addsub_sse4_1(v[i + 1], v[i + 2], &u[i + 1], &u[i + 2], &clamp_lo, &clamp_hi);

            addsub_sse4_1(v[i + 7], v[i + 4], &u[i + 7], &u[i + 4], &clamp_lo, &clamp_hi);
            addsub_sse4_1(v[i + 6], v[i + 5], &u[i + 6], &u[i + 5], &clamp_lo, &clamp_hi);
        }

        // stage 6
        v[0] = half_btf_0_sse4_1(&cospi32, &u[0], &rnding, bit);
        v[1] = half_btf_0_sse4_1(&cospi32, &u[0], &rnding, bit);
        v[2] = half_btf_0_sse4_1(&cospi48, &u[2], &rnding, bit);
        v[3] = half_btf_0_sse4_1(&cospi16, &u[2], &rnding, bit);

        addsub_sse4_1(u[4], u[5], &v[4], &v[5], &clamp_lo, &clamp_hi);
        addsub_sse4_1(u[7], u[6], &v[7], &v[6], &clamp_lo, &clamp_hi);

        for (i = 8; i < 16; i += 4) {
            v[i + 0] = u[i + 0];
            v[i + 3] = u[i + 3];
        }

        v[9]  = half_btf_sse4_1(&cospim16, &u[9], &cospi48, &u[14], &rnding, bit);
        v[10] = half_btf_sse4_1(&cospim48, &u[10], &cospim16, &u[13], &rnding, bit);
        v[13] = half_btf_sse4_1(&cospim16, &u[10], &cospi48, &u[13], &rnding, bit);
        v[14] = half_btf_sse4_1(&cospi48, &u[9], &cospi16, &u[14], &rnding, bit);

        for (i = 16; i < 32; i += 8) {
            addsub_sse4_1(u[i + 0], u[i + 3], &v[i + 0], &v[i + 3], &clamp_lo, &clamp_hi);
            addsub_sse4_1(u[i + 1], u[i + 2], &v[i + 1], &v[i + 2], &clamp_lo, &clamp_hi);

            addsub_sse4_1(u[i + 7], u[i + 4], &v[i + 7], &v[i + 4], &clamp_lo, &clamp_hi);
            addsub_sse4_1(u[i + 6], u[i + 5], &v[i + 6], &v[i + 5], &clamp_lo, &clamp_hi);
        }

        for (i = 32; i < 64; i += 8) {
            v[i + 0] = u[i + 0];
            v[i + 1] = u[i + 1];
            v[i + 6] = u[i + 6];
            v[i + 7] = u[i + 7];
        }

        v[34] = half_btf_sse4_1(&cospim8, &u[34], &cospi56, &u[61], &rnding, bit);
        v[35] = half_btf_sse4_1(&cospim8, &u[35], &cospi56, &u[60], &rnding, bit);
        v[36] = half_btf_sse4_1(&cospim56, &u[36], &cospim8, &u[59], &rnding, bit);
        v[37] = half_btf_sse4_1(&cospim56, &u[37], &cospim8, &u[58], &rnding, bit);
        v[42] = half_btf_sse4_1(&cospim40, &u[42], &cospi24, &u[53], &rnding, bit);
        v[43] = half_btf_sse4_1(&cospim40, &u[43], &cospi24, &u[52], &rnding, bit);
        v[44] = half_btf_sse4_1(&cospim24, &u[44], &cospim40, &u[51], &rnding, bit);
        v[45] = half_btf_sse4_1(&cospim24, &u[45], &cospim40, &u[50], &rnding, bit);
        v[50] = half_btf_sse4_1(&cospim40, &u[45], &cospi24, &u[50], &rnding, bit);
        v[51] = half_btf_sse4_1(&cospim40, &u[44], &cospi24, &u[51], &rnding, bit);
        v[52] = half_btf_sse4_1(&cospi24, &u[43], &cospi40, &u[52], &rnding, bit);
        v[53] = half_btf_sse4_1(&cospi24, &u[42], &cospi40, &u[53], &rnding, bit);
        v[58] = half_btf_sse4_1(&cospim8, &u[37], &cospi56, &u[58], &rnding, bit);
        v[59] = half_btf_sse4_1(&cospim8, &u[36], &cospi56, &u[59], &rnding, bit);
        v[60] = half_btf_sse4_1(&cospi56, &u[35], &cospi8, &u[60], &rnding, bit);
        v[61] = half_btf_sse4_1(&cospi56, &u[34], &cospi8, &u[61], &rnding, bit);

        // stage 7
        addsub_sse4_1(v[0], v[3], &u[0], &u[3], &clamp_lo, &clamp_hi);
        addsub_sse4_1(v[1], v[2], &u[1], &u[2], &clamp_lo, &clamp_hi);

        u[4] = v[4];
        u[7] = v[7];
        u[5] = half_btf_sse4_1(&cospim32, &v[5], &cospi32, &v[6], &rnding, bit);
        u[6] = half_btf_sse4_1(&cospi32, &v[5], &cospi32, &v[6], &rnding, bit);

        addsub_sse4_1(v[8], v[11], &u[8], &u[11], &clamp_lo, &clamp_hi);
        addsub_sse4_1(v[9], v[10], &u[9], &u[10], &clamp_lo, &clamp_hi);
        addsub_sse4_1(v[15], v[12], &u[15], &u[12], &clamp_lo, &clamp_hi);
        addsub_sse4_1(v[14], v[13], &u[14], &u[13], &clamp_lo, &clamp_hi);

        for (i = 16; i < 32; i += 8) {
            u[i + 0] = v[i + 0];
            u[i + 1] = v[i + 1];
            u[i + 6] = v[i + 6];
            u[i + 7] = v[i + 7];
        }

        u[18] = half_btf_sse4_1(&cospim16, &v[18], &cospi48, &v[29], &rnding, bit);
        u[19] = half_btf_sse4_1(&cospim16, &v[19], &cospi48, &v[28], &rnding, bit);
        u[20] = half_btf_sse4_1(&cospim48, &v[20], &cospim16, &v[27], &rnding, bit);
        u[21] = half_btf_sse4_1(&cospim48, &v[21], &cospim16, &v[26], &rnding, bit);
        u[26] = half_btf_sse4_1(&cospim16, &v[21], &cospi48, &v[26], &rnding, bit);
        u[27] = half_btf_sse4_1(&cospim16, &v[20], &cospi48, &v[27], &rnding, bit);
        u[28] = half_btf_sse4_1(&cospi48, &v[19], &cospi16, &v[28], &rnding, bit);
        u[29] = half_btf_sse4_1(&cospi48, &v[18], &cospi16, &v[29], &rnding, bit);

        for (i = 32; i < 64; i += 16) {
            for (j = i; j < i + 4; j++) {
                addsub_sse4_1(v[j], v[j ^ 7], &u[j], &u[j ^ 7], &clamp_lo, &clamp_hi);
                addsub_sse4_1(v[j ^ 15], v[j ^ 8], &u[j ^ 15], &u[j ^ 8], &clamp_lo, &clamp_hi);
            }
        }

        // stage 8
        for (i = 0; i < 4; ++i)
            addsub_sse4_1(u[i], u[7 - i], &v[i], &v[7 - i], &clamp_lo, &clamp_hi);
        v[8]  = u[8];
        v[9]  = u[9];
        v[14] = u[14];
        v[15] = u[15];

        v[10] = half_btf_sse4_1(&cospim32, &u[10], &cospi32, &u[13], &rnding, bit);
        v[11] = half_btf_sse4_1(&cospim32, &u[11], &cospi32, &u[12], &rnding, bit);
        v[12] = half_btf_sse4_1(&cospi32, &u[11], &cospi32, &u[12], &rnding, bit);
        v[13] = half_btf_sse4_1(&cospi32, &u[10], &cospi32, &u[13], &rnding, bit);

        for (i = 16; i < 20; ++i) {
            addsub_sse4_1(u[i], u[i ^ 7], &v[i], &v[i ^ 7], &clamp_lo, &clamp_hi);
            addsub_sse4_1(u[i ^ 15], u[i ^ 8], &v[i ^ 15], &v[i ^ 8], &clamp_lo, &clamp_hi);
        }

        for (i = 32; i < 36; ++i) {
            v[i]      = u[i];
            v[i + 12] = u[i + 12];
            v[i + 16] = u[i + 16];
            v[i + 28] = u[i + 28];
        }

        v[36] = half_btf_sse4_1(&cospim16, &u[36], &cospi48, &u[59], &rnding, bit);
        v[37] = half_btf_sse4_1(&cospim16, &u[37], &cospi48, &u[58], &rnding, bit);
        v[38] = half_btf_sse4_1(&cospim16, &u[38], &cospi48, &u[57], &rnding, bit);
        v[39] = half_btf_sse4_1(&cospim16, &u[39], &cospi48, &u[56], &rnding, bit);
        v[40] = half_btf_sse4_1(&cospim48, &u[40], &cospim16, &u[55], &rnding, bit);
        v[41] = half_btf_sse4_1(&cospim48, &u[41], &cospim16, &u[54], &rnding, bit);
        v[42] = half_btf_sse4_1(&cospim48, &u[42], &cospim16, &u[53], &rnding, bit);
        v[43] = half_btf_sse4_1(&cospim48, &u[43], &cospim16, &u[52], &rnding, bit);
        v[52] = half_btf_sse4_1(&cospim16, &u[43], &cospi48, &u[52], &rnding, bit);
        v[53] = half_btf_sse4_1(&cospim16, &u[42], &cospi48, &u[53], &rnding, bit);
        v[54] = half_btf_sse4_1(&cospim16, &u[41], &cospi48, &u[54], &rnding, bit);
        v[55] = half_btf_sse4_1(&cospim16, &u[40], &cospi48, &u[55], &rnding, bit);
        v[56] = half_btf_sse4_1(&cospi48, &u[39], &cospi16, &u[56], &rnding, bit);
        v[57] = half_btf_sse4_1(&cospi48, &u[38], &cospi16, &u[57], &rnding, bit);
        v[58] = half_btf_sse4_1(&cospi48, &u[37], &cospi16, &u[58], &rnding, bit);
        v[59] = half_btf_sse4_1(&cospi48, &u[36], &cospi16, &u[59], &rnding, bit);

        // stage 9
        for (i = 0; i < 8; ++i)
            addsub_sse4_1(v[i], v[15 - i], &u[i], &u[15 - i], &clamp_lo, &clamp_hi);
        for (i = 16; i < 20; ++i) {
            u[i]      = v[i];
            u[i + 12] = v[i + 12];
        }

        u[20] = half_btf_sse4_1(&cospim32, &v[20], &cospi32, &v[27], &rnding, bit);
        u[21] = half_btf_sse4_1(&cospim32, &v[21], &cospi32, &v[26], &rnding, bit);
        u[22] = half_btf_sse4_1(&cospim32, &v[22], &cospi32, &v[25], &rnding, bit);
        u[23] = half_btf_sse4_1(&cospim32, &v[23], &cospi32, &v[24], &rnding, bit);
        u[24] = half_btf_sse4_1(&cospi32, &v[23], &cospi32, &v[24], &rnding, bit);
        u[25] = half_btf_sse4_1(&cospi32, &v[22], &cospi32, &v[25], &rnding, bit);
        u[26] = half_btf_sse4_1(&cospi32, &v[21], &cospi32, &v[26], &rnding, bit);
        u[27] = half_btf_sse4_1(&cospi32, &v[20], &cospi32, &v[27], &rnding, bit);

        for (i = 32; i < 40; i++)
            addsub_sse4_1(v[i], v[i ^ 15], &u[i], &u[i ^ 15], &clamp_lo, &clamp_hi);
        for (i = 48; i < 56; i++)
            addsub_sse4_1(v[i ^ 15], v[i], &u[i ^ 15], &u[i], &clamp_lo, &clamp_hi);
        // stage 10
        for (i = 0; i < 16; i++)
            addsub_sse4_1(u[i], u[31 - i], &v[i], &v[31 - i], &clamp_lo, &clamp_hi);
        for (i = 32; i < 40; i++) v[i] = u[i];

        v[40] = half_btf_sse4_1(&cospim32, &u[40], &cospi32, &u[55], &rnding, bit);
        v[41] = half_btf_sse4_1(&cospim32, &u[41], &cospi32, &u[54], &rnding, bit);
        v[42] = half_btf_sse4_1(&cospim32, &u[42], &cospi32, &u[53], &rnding, bit);
        v[43] = half_btf_sse4_1(&cospim32, &u[43], &cospi32, &u[52], &rnding, bit);
        v[44] = half_btf_sse4_1(&cospim32, &u[44], &cospi32, &u[51], &rnding, bit);
        v[45] = half_btf_sse4_1(&cospim32, &u[45], &cospi32, &u[50], &rnding, bit);
        v[46] = half_btf_sse4_1(&cospim32, &u[46], &cospi32, &u[49], &rnding, bit);
        v[47] = half_btf_sse4_1(&cospim32, &u[47], &cospi32, &u[48], &rnding, bit);
        v[48] = half_btf_sse4_1(&cospi32, &u[47], &cospi32, &u[48], &rnding, bit);
        v[49] = half_btf_sse4_1(&cospi32, &u[46], &cospi32, &u[49], &rnding, bit);
        v[50] = half_btf_sse4_1(&cospi32, &u[45], &cospi32, &u[50], &rnding, bit);
        v[51] = half_btf_sse4_1(&cospi32, &u[44], &cospi32, &u[51], &rnding, bit);
        v[52] = half_btf_sse4_1(&cospi32, &u[43], &cospi32, &u[52], &rnding, bit);
        v[53] = half_btf_sse4_1(&cospi32, &u[42], &cospi32, &u[53], &rnding, bit);
        v[54] = half_btf_sse4_1(&cospi32, &u[41], &cospi32, &u[54], &rnding, bit);
        v[55] = half_btf_sse4_1(&cospi32, &u[40], &cospi32, &u[55], &rnding, bit);

        for (i = 56; i < 64; i++) v[i] = u[i];

        // stage 11
        for (i = 0; i < 32; i++) {
            addsub_sse4_1(v[i],
                          v[63 - i],
                          &out[16 * (i) + col],
                          &out[16 * (63 - i) + col],
                          &clamp_lo,
                          &clamp_hi);
        }
    }
}

void svt_av1_inv_txfm2d_add_64x64_sse4_1(const int32_t *input, uint16_t *output_r, int32_t stride_r,
                                         uint16_t *output_w, int32_t stride_w, TxType tx_type,
                                         int32_t bd) {
    __m128i       in[64 * 64 / 4], out[64 * 64 / 4];
    const int8_t *shift   = eb_inv_txfm_shift_ls[TX_64X64];
    const int32_t txw_idx = tx_size_wide_log2[TX_64X64] - tx_size_wide_log2[0];
    const int32_t txh_idx = tx_size_high_log2[TX_64X64] - tx_size_high_log2[0];

    switch (tx_type) {
    case DCT_DCT:
        load_buffer_64x64_lower_32x32(input, in);
        transpose_64x64(in, out, 0);
        idct64x64_sse4_1(out, in, inv_cos_bit_row[txw_idx][txh_idx], 0, bd);
        // transpose before shift, so shift can apply to 512 contiguous values
        transpose_64x64(in, out, 1);
        round_shift_64x64(out, -shift[0]);
        idct64x64_sse4_1(out, in, inv_cos_bit_col[txw_idx][txh_idx], 1, bd);
        write_buffer_64x64(in, output_r, stride_r, output_w, stride_w, 0, 0, -shift[1], bd);
        break;

    default:
        svt_av1_inv_txfm2d_add_64x64_c(input, output_r, stride_r, output_w, stride_w, tx_type, bd);
        break;
    }
}

//4x8
static INLINE void load_buffer_32bit_input(const int32_t *in, int32_t stride, __m128i *out,
                                           int32_t out_size) {
    for (int32_t i = 0; i < out_size; ++i)
        out[i] = _mm_loadu_si128((const __m128i *)(in + i * stride));
}

static void neg_shift_sse4_1(const __m128i in0, const __m128i in1, __m128i *out0, __m128i *out1,
                             const __m128i *clamp_lo, const __m128i *clamp_hi, int32_t shift) {
    __m128i offset = _mm_set1_epi32((1 << shift) >> 1);
    __m128i a0     = _mm_add_epi32(offset, in0);
    __m128i a1     = _mm_sub_epi32(offset, in1);

    a0 = _mm_sra_epi32(a0, _mm_cvtsi32_si128(shift));
    a1 = _mm_sra_epi32(a1, _mm_cvtsi32_si128(shift));

    a0 = _mm_max_epi32(a0, *clamp_lo);
    a0 = _mm_min_epi32(a0, *clamp_hi);
    a1 = _mm_max_epi32(a1, *clamp_lo);
    a1 = _mm_min_epi32(a1, *clamp_hi);

    *out0 = a0;
    *out1 = a1;
}

static INLINE __m128i av1_round_shift_32_sse4_1(__m128i vec, int32_t bit) {
    __m128i tmp, round;
    round = _mm_set1_epi32(1 << (bit - 1));
    tmp   = _mm_add_epi32(vec, round);
    return _mm_srai_epi32(tmp, bit);
}

static INLINE void av1_round_shift_array_32_sse4_1(__m128i *input, __m128i *output,
                                                   const int32_t size, const int32_t bit) {
    if (bit > 0) {
        int32_t i;
        for (i = 0; i < size; i++) output[i] = av1_round_shift_32_sse4_1(input[i], bit);
    } else {
        int32_t i;
        for (i = 0; i < size; i++) output[i] = _mm_slli_epi32(input[i], -bit);
    }
}

static INLINE void av1_round_shift_rect_array_32_sse4_1(__m128i *input, __m128i *output,
                                                        const int32_t size, const int32_t bit,
                                                        const int32_t val) {
    const __m128i sqrt2 = _mm_set1_epi32(val);
    if (bit > 0) {
        int32_t i;
        for (i = 0; i < size; i++) {
            const __m128i r0 = av1_round_shift_32_sse4_1(input[i], bit);
            const __m128i r1 = _mm_mullo_epi32(sqrt2, r0);
            output[i]        = av1_round_shift_32_sse4_1(r1, new_sqrt2_bits);
        }
    } else {
        int32_t i;
        for (i = 0; i < size; i++) {
            const __m128i r0 = _mm_slli_epi32(input[i], -bit);
            const __m128i r1 = _mm_mullo_epi32(sqrt2, r0);
            output[i]        = av1_round_shift_32_sse4_1(r1, new_sqrt2_bits);
        }
    }
}

static void iidentity4_sse4_1(__m128i *in, __m128i *out, int32_t bit, int32_t do_cols, int32_t bd,
                              int32_t out_shift) {
    (void)bit;
    __m128i v[4];
    __m128i zero   = _mm_set1_epi32(0);
    __m128i fact   = _mm_set1_epi32(new_sqrt2);
    __m128i offset = _mm_set1_epi32(1 << (new_sqrt2_bits - 1));
    __m128i a0_low, a1_low;
    __m128i a0_high, a1_high;

    offset = _mm_unpacklo_epi32(offset, zero);

    for (int i = 0; i < 4; i++) {
        a0_low = _mm_mul_epi32(in[i], fact);
        a0_low = _mm_add_epi32(a0_low, offset);
        a0_low = _mm_srli_epi64(a0_low, new_sqrt2_bits);

        a0_high = _mm_srli_si128(in[i], 4);
        a0_high = _mm_mul_epi32(a0_high, fact);
        a0_high = _mm_add_epi32(a0_high, offset);
        a0_high = _mm_srli_epi64(a0_high, new_sqrt2_bits);

        a1_low  = _mm_unpacklo_epi32(a0_low, a0_high);
        a1_high = _mm_unpackhi_epi32(a0_low, a0_high);
        out[i]  = _mm_unpacklo_epi64(a1_low, a1_high);
    }

    if (!do_cols) {
        const int32_t log_range = AOMMAX(16, bd + 6);
        const __m128i clamp_lo  = _mm_set1_epi32(-(1 << (log_range - 1)));
        const __m128i clamp_hi  = _mm_set1_epi32((1 << (log_range - 1)) - 1);
        round_shift_4x4(out, out_shift);
        highbd_clamp_epi32_sse4_1(out, out, &clamp_lo, &clamp_hi, 4);
    }

    // Transpose for 4x4
    v[0] = _mm_unpacklo_epi32(out[0], out[1]);
    v[1] = _mm_unpackhi_epi32(out[0], out[1]);
    v[2] = _mm_unpacklo_epi32(out[2], out[3]);
    v[3] = _mm_unpackhi_epi32(out[2], out[3]);

    out[0] = _mm_unpacklo_epi64(v[0], v[2]);
    out[1] = _mm_unpackhi_epi64(v[0], v[2]);
    out[2] = _mm_unpacklo_epi64(v[1], v[3]);
    out[3] = _mm_unpackhi_epi64(v[1], v[3]);
}

static void iidentity8_sse4_1(__m128i *in, __m128i *out, int32_t bit, int32_t do_cols, int32_t bd,
                              int32_t out_shift) {
    (void)bit;
    out[0] = _mm_add_epi32(in[0], in[0]);
    out[1] = _mm_add_epi32(in[1], in[1]);
    out[2] = _mm_add_epi32(in[2], in[2]);
    out[3] = _mm_add_epi32(in[3], in[3]);
    out[4] = _mm_add_epi32(in[4], in[4]);
    out[5] = _mm_add_epi32(in[5], in[5]);
    out[6] = _mm_add_epi32(in[6], in[6]);
    out[7] = _mm_add_epi32(in[7], in[7]);

    if (!do_cols) {
        const int     log_range = AOMMAX(16, bd + 6);
        const __m128i clamp_lo  = _mm_set1_epi32(-(1 << (log_range - 1)));
        const __m128i clamp_hi  = _mm_set1_epi32((1 << (log_range - 1)) - 1);
        round_shift_4x4(out, out_shift);
        round_shift_4x4(out + 4, out_shift);
        highbd_clamp_epi32_sse4_1(out, out, &clamp_lo, &clamp_hi, 8);
    }
}

static void iidentity16_sse4_1(__m128i *in, __m128i *out, int32_t bit, int32_t do_cols, int32_t bd,
                               int32_t out_shift) {
    (void)bit;
    __m128i fact   = _mm_set1_epi32(2 * new_sqrt2);
    __m128i offset = _mm_set1_epi32(1 << (new_sqrt2_bits - 1));
    __m128i a0_low, a0_high, a1_low, a1_high;
    __m128i zero = _mm_set1_epi32(0);
    offset       = _mm_unpacklo_epi32(offset, zero);

    for (int i = 0; i < 16; i++) {
        a0_low = _mm_mul_epi32(in[i], fact);
        a0_low = _mm_add_epi32(a0_low, offset);
        a0_low = _mm_srli_epi64(a0_low, new_sqrt2_bits);

        a0_high = _mm_srli_si128(in[i], 4);
        a0_high = _mm_mul_epi32(a0_high, fact);
        a0_high = _mm_add_epi32(a0_high, offset);
        a0_high = _mm_srli_epi64(a0_high, new_sqrt2_bits);

        a1_low  = _mm_unpacklo_epi32(a0_low, a0_high);
        a1_high = _mm_unpackhi_epi32(a0_low, a0_high);
        out[i]  = _mm_unpacklo_epi64(a1_low, a1_high);
    }

    if (!do_cols) {
        const int     log_range = AOMMAX(16, bd + 6);
        const __m128i clamp_lo  = _mm_set1_epi32(-(1 << (log_range - 1)));
        const __m128i clamp_hi  = _mm_set1_epi32((1 << (log_range - 1)) - 1);
        round_shift_8x8(out, out_shift);
        highbd_clamp_epi32_sse4_1(out, out, &clamp_lo, &clamp_hi, 16);
    }
}

static INLINE __m128i highbd_get_recon_4xn_sse4_1(const __m128i pred, __m128i res0,
                                                  const int32_t bd) {
    __m128i x0 = _mm_cvtepi16_epi32(pred);

    x0 = _mm_add_epi32(res0, x0);
    x0 = _mm_packus_epi32(x0, x0);
    x0 = highbd_clamp_epi16(x0, bd);
    return x0;
}

static void idct8x8_new_sse4_1(__m128i *in, __m128i *out, int32_t bit, int32_t do_cols, int32_t bd,
                               int32_t out_shift) {
    const int32_t *cospi     = cospi_arr(bit);
    const __m128i  cospi56   = _mm_set1_epi32(cospi[56]);
    const __m128i  cospim8   = _mm_set1_epi32(-cospi[8]);
    const __m128i  cospi24   = _mm_set1_epi32(cospi[24]);
    const __m128i  cospim40  = _mm_set1_epi32(-cospi[40]);
    const __m128i  cospi40   = _mm_set1_epi32(cospi[40]);
    const __m128i  cospi8    = _mm_set1_epi32(cospi[8]);
    const __m128i  cospi32   = _mm_set1_epi32(cospi[32]);
    const __m128i  cospi48   = _mm_set1_epi32(cospi[48]);
    const __m128i  cospim16  = _mm_set1_epi32(-cospi[16]);
    const __m128i  cospi16   = _mm_set1_epi32(cospi[16]);
    const __m128i  rnding    = _mm_set1_epi32(1 << (bit - 1));
    const int32_t  log_range = AOMMAX(16, bd + (do_cols ? 6 : 8));
    const __m128i  clamp_lo  = _mm_set1_epi32(-(1 << (log_range - 1)));
    const __m128i  clamp_hi  = _mm_set1_epi32((1 << (log_range - 1)) - 1);
    __m128i        u0, u1, u2, u3, u4, u5, u6, u7;
    __m128i        v0, v1, v2, v3, v4, v5, v6, v7;
    __m128i        x, y;

    // stage 0
    // stage 1
    // stage 2
    u0 = in[0];
    u1 = in[4];
    u2 = in[2];
    u3 = in[6];

    x  = _mm_mullo_epi32(in[1], cospi56);
    y  = _mm_mullo_epi32(in[7], cospim8);
    u4 = _mm_add_epi32(x, y);
    u4 = _mm_add_epi32(u4, rnding);
    u4 = _mm_srai_epi32(u4, bit);

    x  = _mm_mullo_epi32(in[1], cospi8);
    y  = _mm_mullo_epi32(in[7], cospi56);
    u7 = _mm_add_epi32(x, y);
    u7 = _mm_add_epi32(u7, rnding);
    u7 = _mm_srai_epi32(u7, bit);

    x  = _mm_mullo_epi32(in[5], cospi24);
    y  = _mm_mullo_epi32(in[3], cospim40);
    u5 = _mm_add_epi32(x, y);
    u5 = _mm_add_epi32(u5, rnding);
    u5 = _mm_srai_epi32(u5, bit);

    x  = _mm_mullo_epi32(in[5], cospi40);
    y  = _mm_mullo_epi32(in[3], cospi24);
    u6 = _mm_add_epi32(x, y);
    u6 = _mm_add_epi32(u6, rnding);
    u6 = _mm_srai_epi32(u6, bit);

    // stage 3
    x  = _mm_mullo_epi32(u0, cospi32);
    y  = _mm_mullo_epi32(u1, cospi32);
    v0 = _mm_add_epi32(x, y);
    v0 = _mm_add_epi32(v0, rnding);
    v0 = _mm_srai_epi32(v0, bit);

    v1 = _mm_sub_epi32(x, y);
    v1 = _mm_add_epi32(v1, rnding);
    v1 = _mm_srai_epi32(v1, bit);

    x  = _mm_mullo_epi32(u2, cospi48);
    y  = _mm_mullo_epi32(u3, cospim16);
    v2 = _mm_add_epi32(x, y);
    v2 = _mm_add_epi32(v2, rnding);
    v2 = _mm_srai_epi32(v2, bit);

    x  = _mm_mullo_epi32(u2, cospi16);
    y  = _mm_mullo_epi32(u3, cospi48);
    v3 = _mm_add_epi32(x, y);
    v3 = _mm_add_epi32(v3, rnding);
    v3 = _mm_srai_epi32(v3, bit);

    addsub_sse4_1(u4, u5, &v4, &v5, &clamp_lo, &clamp_hi);
    addsub_sse4_1(u7, u6, &v7, &v6, &clamp_lo, &clamp_hi);

    // stage 4
    addsub_sse4_1(v0, v3, &u0, &u3, &clamp_lo, &clamp_hi);
    addsub_sse4_1(v1, v2, &u1, &u2, &clamp_lo, &clamp_hi);
    u4 = v4;
    u7 = v7;

    x  = _mm_mullo_epi32(v5, cospi32);
    y  = _mm_mullo_epi32(v6, cospi32);
    u6 = _mm_add_epi32(y, x);
    u6 = _mm_add_epi32(u6, rnding);
    u6 = _mm_srai_epi32(u6, bit);

    u5 = _mm_sub_epi32(y, x);
    u5 = _mm_add_epi32(u5, rnding);
    u5 = _mm_srai_epi32(u5, bit);

    // stage 5
    addsub_sse4_1(u0, u7, out + 0, out + 7, &clamp_lo, &clamp_hi);
    addsub_sse4_1(u1, u6, out + 1, out + 6, &clamp_lo, &clamp_hi);
    addsub_sse4_1(u2, u5, out + 2, out + 5, &clamp_lo, &clamp_hi);
    addsub_sse4_1(u3, u4, out + 3, out + 4, &clamp_lo, &clamp_hi);

    if (!do_cols) {
        const int     log_range_out = AOMMAX(16, bd + 6);
        const __m128i clamp_lo_out  = _mm_set1_epi32(-(1 << (log_range_out - 1)));
        const __m128i clamp_hi_out  = _mm_set1_epi32((1 << (log_range_out - 1)) - 1);

        round_shift_4x4(out, out_shift);
        round_shift_4x4(out + 4, out_shift);
        highbd_clamp_epi32_sse4_1(out, out, &clamp_lo_out, &clamp_hi_out, 8);
    }
}

static void idct8x8_low1_sse4_1(__m128i *in, __m128i *out, int32_t bit, int32_t do_cols, int32_t bd,
                                int32_t out_shift) {
    const int32_t *cospi     = cospi_arr(bit);
    const __m128i  cospi32   = _mm_set1_epi32(cospi[32]);
    const __m128i  rnding    = _mm_set1_epi32(1 << (bit - 1));
    const int32_t  log_range = AOMMAX(16, bd + (do_cols ? 6 : 8));
    __m128i        clamp_lo  = _mm_set1_epi32(-(1 << (log_range - 1)));
    __m128i        clamp_hi  = _mm_set1_epi32((1 << (log_range - 1)) - 1);
    __m128i        x;

    // stage 0
    // stage 1
    // stage 2
    // stage 3
    x = _mm_mullo_epi32(in[0], cospi32);
    x = _mm_add_epi32(x, rnding);
    x = _mm_srai_epi32(x, bit);

    // stage 4
    // stage 5
    if (!do_cols) {
        const int log_range_out = AOMMAX(16, bd + 6);
        clamp_lo                = _mm_set1_epi32(-(1 << (log_range_out - 1)));
        clamp_hi                = _mm_set1_epi32((1 << (log_range_out - 1)) - 1);

        __m128i offset = _mm_set1_epi32((1 << out_shift) >> 1);
        x              = _mm_add_epi32(x, offset);
        x              = _mm_sra_epi32(x, _mm_cvtsi32_si128(out_shift));
    }

    x      = _mm_max_epi32(x, clamp_lo);
    x      = _mm_min_epi32(x, clamp_hi);
    out[0] = x;
    out[1] = x;
    out[2] = x;
    out[3] = x;
    out[4] = x;
    out[5] = x;
    out[6] = x;
    out[7] = x;
}

static void iadst8x8_new_sse4_1(__m128i *in, __m128i *out, int32_t bit, int32_t do_cols, int32_t bd,
                                int32_t out_shift) {
    const int32_t *cospi     = cospi_arr(bit);
    const __m128i  cospi4    = _mm_set1_epi32(cospi[4]);
    const __m128i  cospi60   = _mm_set1_epi32(cospi[60]);
    const __m128i  cospi20   = _mm_set1_epi32(cospi[20]);
    const __m128i  cospi44   = _mm_set1_epi32(cospi[44]);
    const __m128i  cospi36   = _mm_set1_epi32(cospi[36]);
    const __m128i  cospi28   = _mm_set1_epi32(cospi[28]);
    const __m128i  cospi52   = _mm_set1_epi32(cospi[52]);
    const __m128i  cospi12   = _mm_set1_epi32(cospi[12]);
    const __m128i  cospi16   = _mm_set1_epi32(cospi[16]);
    const __m128i  cospi48   = _mm_set1_epi32(cospi[48]);
    const __m128i  cospim48  = _mm_set1_epi32(-cospi[48]);
    const __m128i  cospi32   = _mm_set1_epi32(cospi[32]);
    const __m128i  rnding    = _mm_set1_epi32(1 << (bit - 1));
    const __m128i  k_zero    = _mm_setzero_si128();
    const int32_t  log_range = AOMMAX(16, bd + (do_cols ? 6 : 8));
    const __m128i  clamp_lo  = _mm_set1_epi32(-(1 << (log_range - 1)));
    const __m128i  clamp_hi  = _mm_set1_epi32((1 << (log_range - 1)) - 1);
    __m128i        u[8], v[8], x;

    // stage 0
    // stage 1
    // stage 2

    u[0] = _mm_mullo_epi32(in[7], cospi4);
    x    = _mm_mullo_epi32(in[0], cospi60);
    u[0] = _mm_add_epi32(u[0], x);
    u[0] = _mm_add_epi32(u[0], rnding);
    u[0] = _mm_srai_epi32(u[0], bit);

    u[1] = _mm_mullo_epi32(in[7], cospi60);
    x    = _mm_mullo_epi32(in[0], cospi4);
    u[1] = _mm_sub_epi32(u[1], x);
    u[1] = _mm_add_epi32(u[1], rnding);
    u[1] = _mm_srai_epi32(u[1], bit);

    // (2)
    u[2] = _mm_mullo_epi32(in[5], cospi20);
    x    = _mm_mullo_epi32(in[2], cospi44);
    u[2] = _mm_add_epi32(u[2], x);
    u[2] = _mm_add_epi32(u[2], rnding);
    u[2] = _mm_srai_epi32(u[2], bit);

    u[3] = _mm_mullo_epi32(in[5], cospi44);
    x    = _mm_mullo_epi32(in[2], cospi20);
    u[3] = _mm_sub_epi32(u[3], x);
    u[3] = _mm_add_epi32(u[3], rnding);
    u[3] = _mm_srai_epi32(u[3], bit);

    // (3)
    u[4] = _mm_mullo_epi32(in[3], cospi36);
    x    = _mm_mullo_epi32(in[4], cospi28);
    u[4] = _mm_add_epi32(u[4], x);
    u[4] = _mm_add_epi32(u[4], rnding);
    u[4] = _mm_srai_epi32(u[4], bit);

    u[5] = _mm_mullo_epi32(in[3], cospi28);
    x    = _mm_mullo_epi32(in[4], cospi36);
    u[5] = _mm_sub_epi32(u[5], x);
    u[5] = _mm_add_epi32(u[5], rnding);
    u[5] = _mm_srai_epi32(u[5], bit);

    // (4)
    u[6] = _mm_mullo_epi32(in[1], cospi52);
    x    = _mm_mullo_epi32(in[6], cospi12);
    u[6] = _mm_add_epi32(u[6], x);
    u[6] = _mm_add_epi32(u[6], rnding);
    u[6] = _mm_srai_epi32(u[6], bit);

    u[7] = _mm_mullo_epi32(in[1], cospi12);
    x    = _mm_mullo_epi32(in[6], cospi52);
    u[7] = _mm_sub_epi32(u[7], x);
    u[7] = _mm_add_epi32(u[7], rnding);
    u[7] = _mm_srai_epi32(u[7], bit);

    // stage 3
    addsub_sse4_1(u[0], u[4], &v[0], &v[4], &clamp_lo, &clamp_hi);
    addsub_sse4_1(u[1], u[5], &v[1], &v[5], &clamp_lo, &clamp_hi);
    addsub_sse4_1(u[2], u[6], &v[2], &v[6], &clamp_lo, &clamp_hi);
    addsub_sse4_1(u[3], u[7], &v[3], &v[7], &clamp_lo, &clamp_hi);

    // stage 4
    u[0] = v[0];
    u[1] = v[1];
    u[2] = v[2];
    u[3] = v[3];

    u[4] = _mm_mullo_epi32(v[4], cospi16);
    x    = _mm_mullo_epi32(v[5], cospi48);
    u[4] = _mm_add_epi32(u[4], x);
    u[4] = _mm_add_epi32(u[4], rnding);
    u[4] = _mm_srai_epi32(u[4], bit);

    u[5] = _mm_mullo_epi32(v[4], cospi48);
    x    = _mm_mullo_epi32(v[5], cospi16);
    u[5] = _mm_sub_epi32(u[5], x);
    u[5] = _mm_add_epi32(u[5], rnding);
    u[5] = _mm_srai_epi32(u[5], bit);

    u[6] = _mm_mullo_epi32(v[6], cospim48);
    x    = _mm_mullo_epi32(v[7], cospi16);
    u[6] = _mm_add_epi32(u[6], x);
    u[6] = _mm_add_epi32(u[6], rnding);
    u[6] = _mm_srai_epi32(u[6], bit);

    u[7] = _mm_mullo_epi32(v[6], cospi16);
    x    = _mm_mullo_epi32(v[7], cospim48);
    u[7] = _mm_sub_epi32(u[7], x);
    u[7] = _mm_add_epi32(u[7], rnding);
    u[7] = _mm_srai_epi32(u[7], bit);

    // stage 5
    addsub_sse4_1(u[0], u[2], &v[0], &v[2], &clamp_lo, &clamp_hi);
    addsub_sse4_1(u[1], u[3], &v[1], &v[3], &clamp_lo, &clamp_hi);
    addsub_sse4_1(u[4], u[6], &v[4], &v[6], &clamp_lo, &clamp_hi);
    addsub_sse4_1(u[5], u[7], &v[5], &v[7], &clamp_lo, &clamp_hi);

    // stage 6
    u[0] = v[0];
    u[1] = v[1];
    u[4] = v[4];
    u[5] = v[5];

    v[0] = _mm_mullo_epi32(v[2], cospi32);
    x    = _mm_mullo_epi32(v[3], cospi32);
    u[2] = _mm_add_epi32(v[0], x);
    u[2] = _mm_add_epi32(u[2], rnding);
    u[2] = _mm_srai_epi32(u[2], bit);

    u[3] = _mm_sub_epi32(v[0], x);
    u[3] = _mm_add_epi32(u[3], rnding);
    u[3] = _mm_srai_epi32(u[3], bit);

    v[0] = _mm_mullo_epi32(v[6], cospi32);
    x    = _mm_mullo_epi32(v[7], cospi32);
    u[6] = _mm_add_epi32(v[0], x);
    u[6] = _mm_add_epi32(u[6], rnding);
    u[6] = _mm_srai_epi32(u[6], bit);

    u[7] = _mm_sub_epi32(v[0], x);
    u[7] = _mm_add_epi32(u[7], rnding);
    u[7] = _mm_srai_epi32(u[7], bit);

    // stage 7
    if (do_cols) {
        out[0] = u[0];
        out[1] = _mm_sub_epi32(k_zero, u[4]);
        out[2] = u[6];
        out[3] = _mm_sub_epi32(k_zero, u[2]);
        out[4] = u[3];
        out[5] = _mm_sub_epi32(k_zero, u[7]);
        out[6] = u[5];
        out[7] = _mm_sub_epi32(k_zero, u[1]);
    } else {
        const int32_t log_range_out = AOMMAX(16, bd + 6);
        const __m128i clamp_lo_out  = _mm_set1_epi32(-(1 << (log_range_out - 1)));
        const __m128i clamp_hi_out  = _mm_set1_epi32((1 << (log_range_out - 1)) - 1);

        neg_shift_sse4_1(u[0], u[4], out + 0, out + 1, &clamp_lo_out, &clamp_hi_out, out_shift);
        neg_shift_sse4_1(u[6], u[2], out + 2, out + 3, &clamp_lo_out, &clamp_hi_out, out_shift);
        neg_shift_sse4_1(u[3], u[7], out + 4, out + 5, &clamp_lo_out, &clamp_hi_out, out_shift);
        neg_shift_sse4_1(u[5], u[1], out + 6, out + 7, &clamp_lo_out, &clamp_hi_out, out_shift);
    }
}

static void iadst8x8_low1_sse4_1(__m128i *in, __m128i *out, int32_t bit, int32_t do_cols,
                                 int32_t bd, int32_t out_shift) {
    const int32_t *cospi   = cospi_arr(bit);
    const __m128i  cospi4  = _mm_set1_epi32(cospi[4]);
    const __m128i  cospi60 = _mm_set1_epi32(cospi[60]);
    const __m128i  cospi16 = _mm_set1_epi32(cospi[16]);
    const __m128i  cospi48 = _mm_set1_epi32(cospi[48]);
    const __m128i  cospi32 = _mm_set1_epi32(cospi[32]);
    const __m128i  rnding  = _mm_set1_epi32(1 << (bit - 1));
    const __m128i  k_zero  = _mm_setzero_si128();
    __m128i        u[8], x;

    // stage 0
    // stage 1
    // stage 2

    x    = _mm_mullo_epi32(in[0], cospi60);
    u[0] = _mm_add_epi32(x, rnding);
    u[0] = _mm_srai_epi32(u[0], bit);

    x    = _mm_mullo_epi32(in[0], cospi4);
    u[1] = _mm_sub_epi32(k_zero, x);
    u[1] = _mm_add_epi32(u[1], rnding);
    u[1] = _mm_srai_epi32(u[1], bit);

    // stage 3
    // stage 4
    __m128i temp1, temp2;
    temp1 = _mm_mullo_epi32(u[0], cospi16);
    x     = _mm_mullo_epi32(u[1], cospi48);
    temp1 = _mm_add_epi32(temp1, x);
    temp1 = _mm_add_epi32(temp1, rnding);
    temp1 = _mm_srai_epi32(temp1, bit);
    u[4]  = temp1;

    temp2 = _mm_mullo_epi32(u[0], cospi48);
    x     = _mm_mullo_epi32(u[1], cospi16);
    u[5]  = _mm_sub_epi32(temp2, x);
    u[5]  = _mm_add_epi32(u[5], rnding);
    u[5]  = _mm_srai_epi32(u[5], bit);

    // stage 5
    // stage 6
    temp1 = _mm_mullo_epi32(u[0], cospi32);
    x     = _mm_mullo_epi32(u[1], cospi32);
    u[2]  = _mm_add_epi32(temp1, x);
    u[2]  = _mm_add_epi32(u[2], rnding);
    u[2]  = _mm_srai_epi32(u[2], bit);

    u[3] = _mm_sub_epi32(temp1, x);
    u[3] = _mm_add_epi32(u[3], rnding);
    u[3] = _mm_srai_epi32(u[3], bit);

    temp1 = _mm_mullo_epi32(u[4], cospi32);
    x     = _mm_mullo_epi32(u[5], cospi32);
    u[6]  = _mm_add_epi32(temp1, x);
    u[6]  = _mm_add_epi32(u[6], rnding);
    u[6]  = _mm_srai_epi32(u[6], bit);

    u[7] = _mm_sub_epi32(temp1, x);
    u[7] = _mm_add_epi32(u[7], rnding);
    u[7] = _mm_srai_epi32(u[7], bit);

    // stage 7
    if (do_cols) {
        out[0] = u[0];
        out[1] = _mm_sub_epi32(k_zero, u[4]);
        out[2] = u[6];
        out[3] = _mm_sub_epi32(k_zero, u[2]);
        out[4] = u[3];
        out[5] = _mm_sub_epi32(k_zero, u[7]);
        out[6] = u[5];
        out[7] = _mm_sub_epi32(k_zero, u[1]);
    } else {
        const int32_t log_range_out = AOMMAX(16, bd + 6);
        const __m128i clamp_lo_out  = _mm_set1_epi32(-(1 << (log_range_out - 1)));
        const __m128i clamp_hi_out  = _mm_set1_epi32((1 << (log_range_out - 1)) - 1);

        neg_shift_sse4_1(u[0], u[4], out + 0, out + 1, &clamp_lo_out, &clamp_hi_out, out_shift);
        neg_shift_sse4_1(u[6], u[2], out + 2, out + 3, &clamp_lo_out, &clamp_hi_out, out_shift);
        neg_shift_sse4_1(u[3], u[7], out + 4, out + 5, &clamp_lo_out, &clamp_hi_out, out_shift);
        neg_shift_sse4_1(u[5], u[1], out + 6, out + 7, &clamp_lo_out, &clamp_hi_out, out_shift);
    }
}

static void idct16x16_low1_sse4_1(__m128i *in, __m128i *out, int32_t bit, int32_t do_cols,
                                  int32_t bd, int32_t out_shift) {
    const int32_t *cospi     = cospi_arr(bit);
    const __m128i  cospi32   = _mm_set1_epi32(cospi[32]);
    const __m128i  rnding    = _mm_set1_epi32(1 << (bit - 1));
    int32_t        log_range = AOMMAX(16, bd + (do_cols ? 6 : 8));
    __m128i        clamp_lo  = _mm_set1_epi32(-(1 << (log_range - 1)));
    __m128i        clamp_hi  = _mm_set1_epi32((1 << (log_range - 1)) - 1);

    {
        // stage 0
        // stage 1
        // stage 2
        // stage 3
        // stage 4
        in[0] = _mm_mullo_epi32(in[0], cospi32);
        in[0] = _mm_add_epi32(in[0], rnding);
        in[0] = _mm_srai_epi32(in[0], bit);

        // stage 5
        // stage 6
        // stage 7
        if (!do_cols) {
            log_range = AOMMAX(16, bd + 6);
            clamp_lo  = _mm_set1_epi32(-(1 << (log_range - 1)));
            clamp_hi  = _mm_set1_epi32((1 << (log_range - 1)) - 1);
            if (out_shift != 0) {
                __m128i offset = _mm_set1_epi32((1 << out_shift) >> 1);
                in[0]          = _mm_add_epi32(in[0], offset);
                in[0]          = _mm_sra_epi32(in[0], _mm_cvtsi32_si128(out_shift));
            }
        }

        in[0]   = _mm_max_epi32(in[0], clamp_lo);
        in[0]   = _mm_min_epi32(in[0], clamp_hi);
        out[0]  = in[0];
        out[1]  = in[0];
        out[2]  = in[0];
        out[3]  = in[0];
        out[4]  = in[0];
        out[5]  = in[0];
        out[6]  = in[0];
        out[7]  = in[0];
        out[8]  = in[0];
        out[9]  = in[0];
        out[10] = in[0];
        out[11] = in[0];
        out[12] = in[0];
        out[13] = in[0];
        out[14] = in[0];
        out[15] = in[0];
    }
}

static void idct16x16_low8_sse4_1(__m128i *in, __m128i *out, int32_t bit, int32_t do_cols,
                                  int32_t bd, int32_t out_shift) {
    const int32_t *cospi     = cospi_arr(bit);
    const __m128i  cospi60   = _mm_set1_epi32(cospi[60]);
    const __m128i  cospi28   = _mm_set1_epi32(cospi[28]);
    const __m128i  cospi44   = _mm_set1_epi32(cospi[44]);
    const __m128i  cospi20   = _mm_set1_epi32(cospi[20]);
    const __m128i  cospi12   = _mm_set1_epi32(cospi[12]);
    const __m128i  cospi4    = _mm_set1_epi32(cospi[4]);
    const __m128i  cospi56   = _mm_set1_epi32(cospi[56]);
    const __m128i  cospi24   = _mm_set1_epi32(cospi[24]);
    const __m128i  cospim40  = _mm_set1_epi32(-cospi[40]);
    const __m128i  cospi8    = _mm_set1_epi32(cospi[8]);
    const __m128i  cospi32   = _mm_set1_epi32(cospi[32]);
    const __m128i  cospi48   = _mm_set1_epi32(cospi[48]);
    const __m128i  cospi16   = _mm_set1_epi32(cospi[16]);
    const __m128i  cospim16  = _mm_set1_epi32(-cospi[16]);
    const __m128i  cospim48  = _mm_set1_epi32(-cospi[48]);
    const __m128i  cospim36  = _mm_set1_epi32(-cospi[36]);
    const __m128i  cospim52  = _mm_set1_epi32(-cospi[52]);
    const __m128i  rnding    = _mm_set1_epi32(1 << (bit - 1));
    const int32_t  log_range = AOMMAX(16, bd + (do_cols ? 6 : 8));
    const __m128i  clamp_lo  = _mm_set1_epi32(-(1 << (log_range - 1)));
    const __m128i  clamp_hi  = _mm_set1_epi32((1 << (log_range - 1)) - 1);
    __m128i        u[16], x, y;

    {
        // stage 0
        // stage 1
        u[0]  = in[0];
        u[2]  = in[4];
        u[4]  = in[2];
        u[6]  = in[6];
        u[8]  = in[1];
        u[10] = in[5];
        u[12] = in[3];
        u[14] = in[7];

        // stage 2
        u[15] = half_btf_0_sse4_1(&cospi4, &u[8], &rnding, bit);
        u[8]  = half_btf_0_sse4_1(&cospi60, &u[8], &rnding, bit);

        u[9]  = half_btf_0_sse4_1(&cospim36, &u[14], &rnding, bit);
        u[14] = half_btf_0_sse4_1(&cospi28, &u[14], &rnding, bit);

        u[13] = half_btf_0_sse4_1(&cospi20, &u[10], &rnding, bit);
        u[10] = half_btf_0_sse4_1(&cospi44, &u[10], &rnding, bit);

        u[11] = half_btf_0_sse4_1(&cospim52, &u[12], &rnding, bit);
        u[12] = half_btf_0_sse4_1(&cospi12, &u[12], &rnding, bit);

        // stage 3
        u[7] = half_btf_0_sse4_1(&cospi8, &u[4], &rnding, bit);
        u[4] = half_btf_0_sse4_1(&cospi56, &u[4], &rnding, bit);
        u[5] = half_btf_0_sse4_1(&cospim40, &u[6], &rnding, bit);
        u[6] = half_btf_0_sse4_1(&cospi24, &u[6], &rnding, bit);

        addsub_sse4_1(u[8], u[9], &u[8], &u[9], &clamp_lo, &clamp_hi);
        addsub_sse4_1(u[11], u[10], &u[11], &u[10], &clamp_lo, &clamp_hi);
        addsub_sse4_1(u[12], u[13], &u[12], &u[13], &clamp_lo, &clamp_hi);
        addsub_sse4_1(u[15], u[14], &u[15], &u[14], &clamp_lo, &clamp_hi);

        // stage 4
        x    = _mm_mullo_epi32(u[0], cospi32);
        u[0] = _mm_add_epi32(x, rnding);
        u[0] = _mm_srai_epi32(u[0], bit);
        u[1] = u[0];

        u[3] = half_btf_0_sse4_1(&cospi16, &u[2], &rnding, bit);
        u[2] = half_btf_0_sse4_1(&cospi48, &u[2], &rnding, bit);

        addsub_sse4_1(u[4], u[5], &u[4], &u[5], &clamp_lo, &clamp_hi);
        addsub_sse4_1(u[7], u[6], &u[7], &u[6], &clamp_lo, &clamp_hi);

        x     = half_btf_sse4_1(&cospim16, &u[9], &cospi48, &u[14], &rnding, bit);
        u[14] = half_btf_sse4_1(&cospi48, &u[9], &cospi16, &u[14], &rnding, bit);
        u[9]  = x;
        y     = half_btf_sse4_1(&cospim48, &u[10], &cospim16, &u[13], &rnding, bit);
        u[13] = half_btf_sse4_1(&cospim16, &u[10], &cospi48, &u[13], &rnding, bit);
        u[10] = y;

        // stage 5
        addsub_sse4_1(u[0], u[3], &u[0], &u[3], &clamp_lo, &clamp_hi);
        addsub_sse4_1(u[1], u[2], &u[1], &u[2], &clamp_lo, &clamp_hi);

        x    = _mm_mullo_epi32(u[5], cospi32);
        y    = _mm_mullo_epi32(u[6], cospi32);
        u[5] = _mm_sub_epi32(y, x);
        u[5] = _mm_add_epi32(u[5], rnding);
        u[5] = _mm_srai_epi32(u[5], bit);

        u[6] = _mm_add_epi32(y, x);
        u[6] = _mm_add_epi32(u[6], rnding);
        u[6] = _mm_srai_epi32(u[6], bit);

        addsub_sse4_1(u[8], u[11], &u[8], &u[11], &clamp_lo, &clamp_hi);
        addsub_sse4_1(u[9], u[10], &u[9], &u[10], &clamp_lo, &clamp_hi);
        addsub_sse4_1(u[15], u[12], &u[15], &u[12], &clamp_lo, &clamp_hi);
        addsub_sse4_1(u[14], u[13], &u[14], &u[13], &clamp_lo, &clamp_hi);

        // stage 6
        addsub_sse4_1(u[0], u[7], &u[0], &u[7], &clamp_lo, &clamp_hi);
        addsub_sse4_1(u[1], u[6], &u[1], &u[6], &clamp_lo, &clamp_hi);
        addsub_sse4_1(u[2], u[5], &u[2], &u[5], &clamp_lo, &clamp_hi);
        addsub_sse4_1(u[3], u[4], &u[3], &u[4], &clamp_lo, &clamp_hi);

        x     = _mm_mullo_epi32(u[10], cospi32);
        y     = _mm_mullo_epi32(u[13], cospi32);
        u[10] = _mm_sub_epi32(y, x);
        u[10] = _mm_add_epi32(u[10], rnding);
        u[10] = _mm_srai_epi32(u[10], bit);

        u[13] = _mm_add_epi32(x, y);
        u[13] = _mm_add_epi32(u[13], rnding);
        u[13] = _mm_srai_epi32(u[13], bit);

        x     = _mm_mullo_epi32(u[11], cospi32);
        y     = _mm_mullo_epi32(u[12], cospi32);
        u[11] = _mm_sub_epi32(y, x);
        u[11] = _mm_add_epi32(u[11], rnding);
        u[11] = _mm_srai_epi32(u[11], bit);

        u[12] = _mm_add_epi32(x, y);
        u[12] = _mm_add_epi32(u[12], rnding);
        u[12] = _mm_srai_epi32(u[12], bit);
        // stage 7
        addsub_sse4_1(u[0], u[15], out + 0, out + 15, &clamp_lo, &clamp_hi);
        addsub_sse4_1(u[1], u[14], out + 1, out + 14, &clamp_lo, &clamp_hi);
        addsub_sse4_1(u[2], u[13], out + 2, out + 13, &clamp_lo, &clamp_hi);
        addsub_sse4_1(u[3], u[12], out + 3, out + 12, &clamp_lo, &clamp_hi);
        addsub_sse4_1(u[4], u[11], out + 4, out + 11, &clamp_lo, &clamp_hi);
        addsub_sse4_1(u[5], u[10], out + 5, out + 10, &clamp_lo, &clamp_hi);
        addsub_sse4_1(u[6], u[9], out + 6, out + 9, &clamp_lo, &clamp_hi);
        addsub_sse4_1(u[7], u[8], out + 7, out + 8, &clamp_lo, &clamp_hi);

        if (!do_cols) {
            const int     log_range_out = AOMMAX(16, bd + 6);
            const __m128i clamp_lo_out  = _mm_set1_epi32(-(1 << (log_range_out - 1)));
            const __m128i clamp_hi_out  = _mm_set1_epi32((1 << (log_range_out - 1)) - 1);
            round_shift_8x8(out, out_shift);
            highbd_clamp_epi32_sse4_1(out, out, &clamp_lo_out, &clamp_hi_out, 16);
        }
    }
}

static void idct16x16_new_sse4_1(__m128i *in, __m128i *out, int32_t bit, int32_t do_cols,
                                 int32_t bd, int32_t out_shift) {
    const int32_t *cospi     = cospi_arr(bit);
    const __m128i  cospi60   = _mm_set1_epi32(cospi[60]);
    const __m128i  cospim4   = _mm_set1_epi32(-cospi[4]);
    const __m128i  cospi28   = _mm_set1_epi32(cospi[28]);
    const __m128i  cospim36  = _mm_set1_epi32(-cospi[36]);
    const __m128i  cospi44   = _mm_set1_epi32(cospi[44]);
    const __m128i  cospi20   = _mm_set1_epi32(cospi[20]);
    const __m128i  cospim20  = _mm_set1_epi32(-cospi[20]);
    const __m128i  cospi12   = _mm_set1_epi32(cospi[12]);
    const __m128i  cospim52  = _mm_set1_epi32(-cospi[52]);
    const __m128i  cospi52   = _mm_set1_epi32(cospi[52]);
    const __m128i  cospi36   = _mm_set1_epi32(cospi[36]);
    const __m128i  cospi4    = _mm_set1_epi32(cospi[4]);
    const __m128i  cospi56   = _mm_set1_epi32(cospi[56]);
    const __m128i  cospim8   = _mm_set1_epi32(-cospi[8]);
    const __m128i  cospi24   = _mm_set1_epi32(cospi[24]);
    const __m128i  cospim40  = _mm_set1_epi32(-cospi[40]);
    const __m128i  cospi40   = _mm_set1_epi32(cospi[40]);
    const __m128i  cospi8    = _mm_set1_epi32(cospi[8]);
    const __m128i  cospi32   = _mm_set1_epi32(cospi[32]);
    const __m128i  cospi48   = _mm_set1_epi32(cospi[48]);
    const __m128i  cospi16   = _mm_set1_epi32(cospi[16]);
    const __m128i  cospim16  = _mm_set1_epi32(-cospi[16]);
    const __m128i  cospim48  = _mm_set1_epi32(-cospi[48]);
    const __m128i  rnding    = _mm_set1_epi32(1 << (bit - 1));
    const int32_t  log_range = AOMMAX(16, bd + (do_cols ? 6 : 8));
    const __m128i  clamp_lo  = _mm_set1_epi32(-(1 << (log_range - 1)));
    const __m128i  clamp_hi  = _mm_set1_epi32((1 << (log_range - 1)) - 1);
    __m128i        u[16], v[16], x, y;

    {
        // stage 0
        // stage 1
        u[0]  = in[0];
        u[1]  = in[8];
        u[2]  = in[4];
        u[3]  = in[12];
        u[4]  = in[2];
        u[5]  = in[10];
        u[6]  = in[6];
        u[7]  = in[14];
        u[8]  = in[1];
        u[9]  = in[9];
        u[10] = in[5];
        u[11] = in[13];
        u[12] = in[3];
        u[13] = in[11];
        u[14] = in[7];
        u[15] = in[15];

        // stage 2
        v[0] = u[0];
        v[1] = u[1];
        v[2] = u[2];
        v[3] = u[3];
        v[4] = u[4];
        v[5] = u[5];
        v[6] = u[6];
        v[7] = u[7];

        v[8]  = half_btf_sse4_1(&cospi60, &u[8], &cospim4, &u[15], &rnding, bit);
        v[9]  = half_btf_sse4_1(&cospi28, &u[9], &cospim36, &u[14], &rnding, bit);
        v[10] = half_btf_sse4_1(&cospi44, &u[10], &cospim20, &u[13], &rnding, bit);
        v[11] = half_btf_sse4_1(&cospi12, &u[11], &cospim52, &u[12], &rnding, bit);
        v[12] = half_btf_sse4_1(&cospi52, &u[11], &cospi12, &u[12], &rnding, bit);
        v[13] = half_btf_sse4_1(&cospi20, &u[10], &cospi44, &u[13], &rnding, bit);
        v[14] = half_btf_sse4_1(&cospi36, &u[9], &cospi28, &u[14], &rnding, bit);
        v[15] = half_btf_sse4_1(&cospi4, &u[8], &cospi60, &u[15], &rnding, bit);

        // stage 3
        u[0] = v[0];
        u[1] = v[1];
        u[2] = v[2];
        u[3] = v[3];
        u[4] = half_btf_sse4_1(&cospi56, &v[4], &cospim8, &v[7], &rnding, bit);
        u[5] = half_btf_sse4_1(&cospi24, &v[5], &cospim40, &v[6], &rnding, bit);
        u[6] = half_btf_sse4_1(&cospi40, &v[5], &cospi24, &v[6], &rnding, bit);
        u[7] = half_btf_sse4_1(&cospi8, &v[4], &cospi56, &v[7], &rnding, bit);
        addsub_sse4_1(v[8], v[9], &u[8], &u[9], &clamp_lo, &clamp_hi);
        addsub_sse4_1(v[11], v[10], &u[11], &u[10], &clamp_lo, &clamp_hi);
        addsub_sse4_1(v[12], v[13], &u[12], &u[13], &clamp_lo, &clamp_hi);
        addsub_sse4_1(v[15], v[14], &u[15], &u[14], &clamp_lo, &clamp_hi);

        // stage 4
        x    = _mm_mullo_epi32(u[0], cospi32);
        y    = _mm_mullo_epi32(u[1], cospi32);
        v[0] = _mm_add_epi32(x, y);
        v[0] = _mm_add_epi32(v[0], rnding);
        v[0] = _mm_srai_epi32(v[0], bit);

        v[1] = _mm_sub_epi32(x, y);
        v[1] = _mm_add_epi32(v[1], rnding);
        v[1] = _mm_srai_epi32(v[1], bit);

        v[2] = half_btf_sse4_1(&cospi48, &u[2], &cospim16, &u[3], &rnding, bit);
        v[3] = half_btf_sse4_1(&cospi16, &u[2], &cospi48, &u[3], &rnding, bit);
        addsub_sse4_1(u[4], u[5], &v[4], &v[5], &clamp_lo, &clamp_hi);
        addsub_sse4_1(u[7], u[6], &v[7], &v[6], &clamp_lo, &clamp_hi);
        v[8]  = u[8];
        v[9]  = half_btf_sse4_1(&cospim16, &u[9], &cospi48, &u[14], &rnding, bit);
        v[10] = half_btf_sse4_1(&cospim48, &u[10], &cospim16, &u[13], &rnding, bit);
        v[11] = u[11];
        v[12] = u[12];
        v[13] = half_btf_sse4_1(&cospim16, &u[10], &cospi48, &u[13], &rnding, bit);
        v[14] = half_btf_sse4_1(&cospi48, &u[9], &cospi16, &u[14], &rnding, bit);
        v[15] = u[15];

        // stage 5
        addsub_sse4_1(v[0], v[3], &u[0], &u[3], &clamp_lo, &clamp_hi);
        addsub_sse4_1(v[1], v[2], &u[1], &u[2], &clamp_lo, &clamp_hi);
        u[4] = v[4];

        x    = _mm_mullo_epi32(v[5], cospi32);
        y    = _mm_mullo_epi32(v[6], cospi32);
        u[5] = _mm_sub_epi32(y, x);
        u[5] = _mm_add_epi32(u[5], rnding);
        u[5] = _mm_srai_epi32(u[5], bit);

        u[6] = _mm_add_epi32(y, x);
        u[6] = _mm_add_epi32(u[6], rnding);
        u[6] = _mm_srai_epi32(u[6], bit);

        u[7] = v[7];
        addsub_sse4_1(v[8], v[11], &u[8], &u[11], &clamp_lo, &clamp_hi);
        addsub_sse4_1(v[9], v[10], &u[9], &u[10], &clamp_lo, &clamp_hi);
        addsub_sse4_1(v[15], v[12], &u[15], &u[12], &clamp_lo, &clamp_hi);
        addsub_sse4_1(v[14], v[13], &u[14], &u[13], &clamp_lo, &clamp_hi);

        // stage 6
        addsub_sse4_1(u[0], u[7], &v[0], &v[7], &clamp_lo, &clamp_hi);
        addsub_sse4_1(u[1], u[6], &v[1], &v[6], &clamp_lo, &clamp_hi);
        addsub_sse4_1(u[2], u[5], &v[2], &v[5], &clamp_lo, &clamp_hi);
        addsub_sse4_1(u[3], u[4], &v[3], &v[4], &clamp_lo, &clamp_hi);
        v[8] = u[8];
        v[9] = u[9];

        x     = _mm_mullo_epi32(u[10], cospi32);
        y     = _mm_mullo_epi32(u[13], cospi32);
        v[10] = _mm_sub_epi32(y, x);
        v[10] = _mm_add_epi32(v[10], rnding);
        v[10] = _mm_srai_epi32(v[10], bit);

        v[13] = _mm_add_epi32(x, y);
        v[13] = _mm_add_epi32(v[13], rnding);
        v[13] = _mm_srai_epi32(v[13], bit);

        x     = _mm_mullo_epi32(u[11], cospi32);
        y     = _mm_mullo_epi32(u[12], cospi32);
        v[11] = _mm_sub_epi32(y, x);
        v[11] = _mm_add_epi32(v[11], rnding);
        v[11] = _mm_srai_epi32(v[11], bit);

        v[12] = _mm_add_epi32(x, y);
        v[12] = _mm_add_epi32(v[12], rnding);
        v[12] = _mm_srai_epi32(v[12], bit);

        v[14] = u[14];
        v[15] = u[15];

        // stage 7
        addsub_sse4_1(v[0], v[15], out + 0, out + 15, &clamp_lo, &clamp_hi);
        addsub_sse4_1(v[1], v[14], out + 1, out + 14, &clamp_lo, &clamp_hi);
        addsub_sse4_1(v[2], v[13], out + 2, out + 13, &clamp_lo, &clamp_hi);
        addsub_sse4_1(v[3], v[12], out + 3, out + 12, &clamp_lo, &clamp_hi);
        addsub_sse4_1(v[4], v[11], out + 4, out + 11, &clamp_lo, &clamp_hi);
        addsub_sse4_1(v[5], v[10], out + 5, out + 10, &clamp_lo, &clamp_hi);
        addsub_sse4_1(v[6], v[9], out + 6, out + 9, &clamp_lo, &clamp_hi);
        addsub_sse4_1(v[7], v[8], out + 7, out + 8, &clamp_lo, &clamp_hi);

        if (!do_cols) {
            const int     log_range_out = AOMMAX(16, bd + 6);
            const __m128i clamp_lo_out  = _mm_set1_epi32(-(1 << (log_range_out - 1)));
            const __m128i clamp_hi_out  = _mm_set1_epi32((1 << (log_range_out - 1)) - 1);
            round_shift_8x8(out, out_shift);
            highbd_clamp_epi32_sse4_1(out, out, &clamp_lo_out, &clamp_hi_out, 16);
        }
    }
}

static void iadst16x16_new_sse4_1(__m128i *in, __m128i *out, int32_t bit, int32_t do_cols,
                                  int32_t bd, int32_t out_shift) {
    const int32_t *cospi     = cospi_arr(bit);
    const __m128i  cospi2    = _mm_set1_epi32(cospi[2]);
    const __m128i  cospi62   = _mm_set1_epi32(cospi[62]);
    const __m128i  cospi10   = _mm_set1_epi32(cospi[10]);
    const __m128i  cospi54   = _mm_set1_epi32(cospi[54]);
    const __m128i  cospi18   = _mm_set1_epi32(cospi[18]);
    const __m128i  cospi46   = _mm_set1_epi32(cospi[46]);
    const __m128i  cospi26   = _mm_set1_epi32(cospi[26]);
    const __m128i  cospi38   = _mm_set1_epi32(cospi[38]);
    const __m128i  cospi34   = _mm_set1_epi32(cospi[34]);
    const __m128i  cospi30   = _mm_set1_epi32(cospi[30]);
    const __m128i  cospi42   = _mm_set1_epi32(cospi[42]);
    const __m128i  cospi22   = _mm_set1_epi32(cospi[22]);
    const __m128i  cospi50   = _mm_set1_epi32(cospi[50]);
    const __m128i  cospi14   = _mm_set1_epi32(cospi[14]);
    const __m128i  cospi58   = _mm_set1_epi32(cospi[58]);
    const __m128i  cospi6    = _mm_set1_epi32(cospi[6]);
    const __m128i  cospi8    = _mm_set1_epi32(cospi[8]);
    const __m128i  cospi56   = _mm_set1_epi32(cospi[56]);
    const __m128i  cospi40   = _mm_set1_epi32(cospi[40]);
    const __m128i  cospi24   = _mm_set1_epi32(cospi[24]);
    const __m128i  cospim56  = _mm_set1_epi32(-cospi[56]);
    const __m128i  cospim24  = _mm_set1_epi32(-cospi[24]);
    const __m128i  cospi48   = _mm_set1_epi32(cospi[48]);
    const __m128i  cospi16   = _mm_set1_epi32(cospi[16]);
    const __m128i  cospim48  = _mm_set1_epi32(-cospi[48]);
    const __m128i  cospi32   = _mm_set1_epi32(cospi[32]);
    const __m128i  rnding    = _mm_set1_epi32(1 << (bit - 1));
    const int32_t  log_range = AOMMAX(16, bd + (do_cols ? 6 : 8));
    const __m128i  clamp_lo  = _mm_set1_epi32(-(1 << (log_range - 1)));
    const __m128i  clamp_hi  = _mm_set1_epi32((1 << (log_range - 1)) - 1);
    __m128i        u[16], v[16], x, y;

    // Calculate the column 0, 1, 2, 3
    {
        // stage 0
        // stage 1
        // stage 2
        v[0] = _mm_mullo_epi32(in[15], cospi2);
        x    = _mm_mullo_epi32(in[0], cospi62);
        v[0] = _mm_add_epi32(v[0], x);
        v[0] = _mm_add_epi32(v[0], rnding);
        v[0] = _mm_srai_epi32(v[0], bit);

        v[1] = _mm_mullo_epi32(in[15], cospi62);
        x    = _mm_mullo_epi32(in[0], cospi2);
        v[1] = _mm_sub_epi32(v[1], x);
        v[1] = _mm_add_epi32(v[1], rnding);
        v[1] = _mm_srai_epi32(v[1], bit);

        v[2] = _mm_mullo_epi32(in[13], cospi10);
        x    = _mm_mullo_epi32(in[2], cospi54);
        v[2] = _mm_add_epi32(v[2], x);
        v[2] = _mm_add_epi32(v[2], rnding);
        v[2] = _mm_srai_epi32(v[2], bit);

        v[3] = _mm_mullo_epi32(in[13], cospi54);
        x    = _mm_mullo_epi32(in[2], cospi10);
        v[3] = _mm_sub_epi32(v[3], x);
        v[3] = _mm_add_epi32(v[3], rnding);
        v[3] = _mm_srai_epi32(v[3], bit);

        v[4] = _mm_mullo_epi32(in[11], cospi18);
        x    = _mm_mullo_epi32(in[4], cospi46);
        v[4] = _mm_add_epi32(v[4], x);
        v[4] = _mm_add_epi32(v[4], rnding);
        v[4] = _mm_srai_epi32(v[4], bit);

        v[5] = _mm_mullo_epi32(in[11], cospi46);
        x    = _mm_mullo_epi32(in[4], cospi18);
        v[5] = _mm_sub_epi32(v[5], x);
        v[5] = _mm_add_epi32(v[5], rnding);
        v[5] = _mm_srai_epi32(v[5], bit);

        v[6] = _mm_mullo_epi32(in[9], cospi26);
        x    = _mm_mullo_epi32(in[6], cospi38);
        v[6] = _mm_add_epi32(v[6], x);
        v[6] = _mm_add_epi32(v[6], rnding);
        v[6] = _mm_srai_epi32(v[6], bit);

        v[7] = _mm_mullo_epi32(in[9], cospi38);
        x    = _mm_mullo_epi32(in[6], cospi26);
        v[7] = _mm_sub_epi32(v[7], x);
        v[7] = _mm_add_epi32(v[7], rnding);
        v[7] = _mm_srai_epi32(v[7], bit);

        v[8] = _mm_mullo_epi32(in[7], cospi34);
        x    = _mm_mullo_epi32(in[8], cospi30);
        v[8] = _mm_add_epi32(v[8], x);
        v[8] = _mm_add_epi32(v[8], rnding);
        v[8] = _mm_srai_epi32(v[8], bit);

        v[9] = _mm_mullo_epi32(in[7], cospi30);
        x    = _mm_mullo_epi32(in[8], cospi34);
        v[9] = _mm_sub_epi32(v[9], x);
        v[9] = _mm_add_epi32(v[9], rnding);
        v[9] = _mm_srai_epi32(v[9], bit);

        v[10] = _mm_mullo_epi32(in[5], cospi42);
        x     = _mm_mullo_epi32(in[10], cospi22);
        v[10] = _mm_add_epi32(v[10], x);
        v[10] = _mm_add_epi32(v[10], rnding);
        v[10] = _mm_srai_epi32(v[10], bit);

        v[11] = _mm_mullo_epi32(in[5], cospi22);
        x     = _mm_mullo_epi32(in[10], cospi42);
        v[11] = _mm_sub_epi32(v[11], x);
        v[11] = _mm_add_epi32(v[11], rnding);
        v[11] = _mm_srai_epi32(v[11], bit);

        v[12] = _mm_mullo_epi32(in[3], cospi50);
        x     = _mm_mullo_epi32(in[12], cospi14);
        v[12] = _mm_add_epi32(v[12], x);
        v[12] = _mm_add_epi32(v[12], rnding);
        v[12] = _mm_srai_epi32(v[12], bit);

        v[13] = _mm_mullo_epi32(in[3], cospi14);
        x     = _mm_mullo_epi32(in[12], cospi50);
        v[13] = _mm_sub_epi32(v[13], x);
        v[13] = _mm_add_epi32(v[13], rnding);
        v[13] = _mm_srai_epi32(v[13], bit);

        v[14] = _mm_mullo_epi32(in[1], cospi58);
        x     = _mm_mullo_epi32(in[14], cospi6);
        v[14] = _mm_add_epi32(v[14], x);
        v[14] = _mm_add_epi32(v[14], rnding);
        v[14] = _mm_srai_epi32(v[14], bit);

        v[15] = _mm_mullo_epi32(in[1], cospi6);
        x     = _mm_mullo_epi32(in[14], cospi58);
        v[15] = _mm_sub_epi32(v[15], x);
        v[15] = _mm_add_epi32(v[15], rnding);
        v[15] = _mm_srai_epi32(v[15], bit);

        // stage 3
        addsub_sse4_1(v[0], v[8], &u[0], &u[8], &clamp_lo, &clamp_hi);
        addsub_sse4_1(v[1], v[9], &u[1], &u[9], &clamp_lo, &clamp_hi);
        addsub_sse4_1(v[2], v[10], &u[2], &u[10], &clamp_lo, &clamp_hi);
        addsub_sse4_1(v[3], v[11], &u[3], &u[11], &clamp_lo, &clamp_hi);
        addsub_sse4_1(v[4], v[12], &u[4], &u[12], &clamp_lo, &clamp_hi);
        addsub_sse4_1(v[5], v[13], &u[5], &u[13], &clamp_lo, &clamp_hi);
        addsub_sse4_1(v[6], v[14], &u[6], &u[14], &clamp_lo, &clamp_hi);
        addsub_sse4_1(v[7], v[15], &u[7], &u[15], &clamp_lo, &clamp_hi);

        // stage 4
        v[0] = u[0];
        v[1] = u[1];
        v[2] = u[2];
        v[3] = u[3];
        v[4] = u[4];
        v[5] = u[5];
        v[6] = u[6];
        v[7] = u[7];

        v[8] = _mm_mullo_epi32(u[8], cospi8);
        x    = _mm_mullo_epi32(u[9], cospi56);
        v[8] = _mm_add_epi32(v[8], x);
        v[8] = _mm_add_epi32(v[8], rnding);
        v[8] = _mm_srai_epi32(v[8], bit);

        v[9] = _mm_mullo_epi32(u[8], cospi56);
        x    = _mm_mullo_epi32(u[9], cospi8);
        v[9] = _mm_sub_epi32(v[9], x);
        v[9] = _mm_add_epi32(v[9], rnding);
        v[9] = _mm_srai_epi32(v[9], bit);

        v[10] = _mm_mullo_epi32(u[10], cospi40);
        x     = _mm_mullo_epi32(u[11], cospi24);
        v[10] = _mm_add_epi32(v[10], x);
        v[10] = _mm_add_epi32(v[10], rnding);
        v[10] = _mm_srai_epi32(v[10], bit);

        v[11] = _mm_mullo_epi32(u[10], cospi24);
        x     = _mm_mullo_epi32(u[11], cospi40);
        v[11] = _mm_sub_epi32(v[11], x);
        v[11] = _mm_add_epi32(v[11], rnding);
        v[11] = _mm_srai_epi32(v[11], bit);

        v[12] = _mm_mullo_epi32(u[12], cospim56);
        x     = _mm_mullo_epi32(u[13], cospi8);
        v[12] = _mm_add_epi32(v[12], x);
        v[12] = _mm_add_epi32(v[12], rnding);
        v[12] = _mm_srai_epi32(v[12], bit);

        v[13] = _mm_mullo_epi32(u[12], cospi8);
        x     = _mm_mullo_epi32(u[13], cospim56);
        v[13] = _mm_sub_epi32(v[13], x);
        v[13] = _mm_add_epi32(v[13], rnding);
        v[13] = _mm_srai_epi32(v[13], bit);

        v[14] = _mm_mullo_epi32(u[14], cospim24);
        x     = _mm_mullo_epi32(u[15], cospi40);
        v[14] = _mm_add_epi32(v[14], x);
        v[14] = _mm_add_epi32(v[14], rnding);
        v[14] = _mm_srai_epi32(v[14], bit);

        v[15] = _mm_mullo_epi32(u[14], cospi40);
        x     = _mm_mullo_epi32(u[15], cospim24);
        v[15] = _mm_sub_epi32(v[15], x);
        v[15] = _mm_add_epi32(v[15], rnding);
        v[15] = _mm_srai_epi32(v[15], bit);

        // stage 5
        addsub_sse4_1(v[0], v[4], &u[0], &u[4], &clamp_lo, &clamp_hi);
        addsub_sse4_1(v[1], v[5], &u[1], &u[5], &clamp_lo, &clamp_hi);
        addsub_sse4_1(v[2], v[6], &u[2], &u[6], &clamp_lo, &clamp_hi);
        addsub_sse4_1(v[3], v[7], &u[3], &u[7], &clamp_lo, &clamp_hi);
        addsub_sse4_1(v[8], v[12], &u[8], &u[12], &clamp_lo, &clamp_hi);
        addsub_sse4_1(v[9], v[13], &u[9], &u[13], &clamp_lo, &clamp_hi);
        addsub_sse4_1(v[10], v[14], &u[10], &u[14], &clamp_lo, &clamp_hi);
        addsub_sse4_1(v[11], v[15], &u[11], &u[15], &clamp_lo, &clamp_hi);

        // stage 6
        v[0] = u[0];
        v[1] = u[1];
        v[2] = u[2];
        v[3] = u[3];

        v[4] = _mm_mullo_epi32(u[4], cospi16);
        x    = _mm_mullo_epi32(u[5], cospi48);
        v[4] = _mm_add_epi32(v[4], x);
        v[4] = _mm_add_epi32(v[4], rnding);
        v[4] = _mm_srai_epi32(v[4], bit);

        v[5] = _mm_mullo_epi32(u[4], cospi48);
        x    = _mm_mullo_epi32(u[5], cospi16);
        v[5] = _mm_sub_epi32(v[5], x);
        v[5] = _mm_add_epi32(v[5], rnding);
        v[5] = _mm_srai_epi32(v[5], bit);

        v[6] = _mm_mullo_epi32(u[6], cospim48);
        x    = _mm_mullo_epi32(u[7], cospi16);
        v[6] = _mm_add_epi32(v[6], x);
        v[6] = _mm_add_epi32(v[6], rnding);
        v[6] = _mm_srai_epi32(v[6], bit);

        v[7] = _mm_mullo_epi32(u[6], cospi16);
        x    = _mm_mullo_epi32(u[7], cospim48);
        v[7] = _mm_sub_epi32(v[7], x);
        v[7] = _mm_add_epi32(v[7], rnding);
        v[7] = _mm_srai_epi32(v[7], bit);

        v[8]  = u[8];
        v[9]  = u[9];
        v[10] = u[10];
        v[11] = u[11];

        v[12] = _mm_mullo_epi32(u[12], cospi16);
        x     = _mm_mullo_epi32(u[13], cospi48);
        v[12] = _mm_add_epi32(v[12], x);
        v[12] = _mm_add_epi32(v[12], rnding);
        v[12] = _mm_srai_epi32(v[12], bit);

        v[13] = _mm_mullo_epi32(u[12], cospi48);
        x     = _mm_mullo_epi32(u[13], cospi16);
        v[13] = _mm_sub_epi32(v[13], x);
        v[13] = _mm_add_epi32(v[13], rnding);
        v[13] = _mm_srai_epi32(v[13], bit);

        v[14] = _mm_mullo_epi32(u[14], cospim48);
        x     = _mm_mullo_epi32(u[15], cospi16);
        v[14] = _mm_add_epi32(v[14], x);
        v[14] = _mm_add_epi32(v[14], rnding);
        v[14] = _mm_srai_epi32(v[14], bit);

        v[15] = _mm_mullo_epi32(u[14], cospi16);
        x     = _mm_mullo_epi32(u[15], cospim48);
        v[15] = _mm_sub_epi32(v[15], x);
        v[15] = _mm_add_epi32(v[15], rnding);
        v[15] = _mm_srai_epi32(v[15], bit);

        // stage 7
        addsub_sse4_1(v[0], v[2], &u[0], &u[2], &clamp_lo, &clamp_hi);
        addsub_sse4_1(v[1], v[3], &u[1], &u[3], &clamp_lo, &clamp_hi);
        addsub_sse4_1(v[4], v[6], &u[4], &u[6], &clamp_lo, &clamp_hi);
        addsub_sse4_1(v[5], v[7], &u[5], &u[7], &clamp_lo, &clamp_hi);
        addsub_sse4_1(v[8], v[10], &u[8], &u[10], &clamp_lo, &clamp_hi);
        addsub_sse4_1(v[9], v[11], &u[9], &u[11], &clamp_lo, &clamp_hi);
        addsub_sse4_1(v[12], v[14], &u[12], &u[14], &clamp_lo, &clamp_hi);
        addsub_sse4_1(v[13], v[15], &u[13], &u[15], &clamp_lo, &clamp_hi);

        // stage 8
        v[0] = u[0];
        v[1] = u[1];

        y    = _mm_mullo_epi32(u[2], cospi32);
        x    = _mm_mullo_epi32(u[3], cospi32);
        v[2] = _mm_add_epi32(y, x);
        v[2] = _mm_add_epi32(v[2], rnding);
        v[2] = _mm_srai_epi32(v[2], bit);

        v[3] = _mm_sub_epi32(y, x);
        v[3] = _mm_add_epi32(v[3], rnding);
        v[3] = _mm_srai_epi32(v[3], bit);

        v[4] = u[4];
        v[5] = u[5];

        y    = _mm_mullo_epi32(u[6], cospi32);
        x    = _mm_mullo_epi32(u[7], cospi32);
        v[6] = _mm_add_epi32(y, x);
        v[6] = _mm_add_epi32(v[6], rnding);
        v[6] = _mm_srai_epi32(v[6], bit);

        v[7] = _mm_sub_epi32(y, x);
        v[7] = _mm_add_epi32(v[7], rnding);
        v[7] = _mm_srai_epi32(v[7], bit);

        v[8] = u[8];
        v[9] = u[9];

        y     = _mm_mullo_epi32(u[10], cospi32);
        x     = _mm_mullo_epi32(u[11], cospi32);
        v[10] = _mm_add_epi32(y, x);
        v[10] = _mm_add_epi32(v[10], rnding);
        v[10] = _mm_srai_epi32(v[10], bit);

        v[11] = _mm_sub_epi32(y, x);
        v[11] = _mm_add_epi32(v[11], rnding);
        v[11] = _mm_srai_epi32(v[11], bit);

        v[12] = u[12];
        v[13] = u[13];

        y     = _mm_mullo_epi32(u[14], cospi32);
        x     = _mm_mullo_epi32(u[15], cospi32);
        v[14] = _mm_add_epi32(y, x);
        v[14] = _mm_add_epi32(v[14], rnding);
        v[14] = _mm_srai_epi32(v[14], bit);

        v[15] = _mm_sub_epi32(y, x);
        v[15] = _mm_add_epi32(v[15], rnding);
        v[15] = _mm_srai_epi32(v[15], bit);

        // stage 9
        if (do_cols) {
            out[0]  = v[0];
            out[1]  = _mm_sub_epi32(_mm_setzero_si128(), v[8]);
            out[2]  = v[12];
            out[3]  = _mm_sub_epi32(_mm_setzero_si128(), v[4]);
            out[4]  = v[6];
            out[5]  = _mm_sub_epi32(_mm_setzero_si128(), v[14]);
            out[6]  = v[10];
            out[7]  = _mm_sub_epi32(_mm_setzero_si128(), v[2]);
            out[8]  = v[3];
            out[9]  = _mm_sub_epi32(_mm_setzero_si128(), v[11]);
            out[10] = v[15];
            out[11] = _mm_sub_epi32(_mm_setzero_si128(), v[7]);
            out[12] = v[5];
            out[13] = _mm_sub_epi32(_mm_setzero_si128(), v[13]);
            out[14] = v[9];
            out[15] = _mm_sub_epi32(_mm_setzero_si128(), v[1]);
        } else {
            const int32_t log_range_out = AOMMAX(16, bd + 6);
            const __m128i clamp_lo_out  = _mm_set1_epi32(-(1 << (log_range_out - 1)));
            const __m128i clamp_hi_out  = _mm_set1_epi32((1 << (log_range_out - 1)) - 1);

            neg_shift_sse4_1(v[0], v[8], out + 0, out + 1, &clamp_lo_out, &clamp_hi_out, out_shift);
            neg_shift_sse4_1(
                v[12], v[4], out + 2, out + 3, &clamp_lo_out, &clamp_hi_out, out_shift);
            neg_shift_sse4_1(
                v[6], v[14], out + 4, out + 5, &clamp_lo_out, &clamp_hi_out, out_shift);
            neg_shift_sse4_1(
                v[10], v[2], out + 6, out + 7, &clamp_lo_out, &clamp_hi_out, out_shift);
            neg_shift_sse4_1(
                v[3], v[11], out + 8, out + 9, &clamp_lo_out, &clamp_hi_out, out_shift);
            neg_shift_sse4_1(
                v[15], v[7], out + 10, out + 11, &clamp_lo_out, &clamp_hi_out, out_shift);
            neg_shift_sse4_1(
                v[5], v[13], out + 12, out + 13, &clamp_lo_out, &clamp_hi_out, out_shift);
            neg_shift_sse4_1(
                v[9], v[1], out + 14, out + 15, &clamp_lo_out, &clamp_hi_out, out_shift);
        }
    }
}

static void iadst16x16_low1_sse4_1(__m128i *in, __m128i *out, int32_t bit, int32_t do_cols,
                                   int32_t bd, int32_t out_shift) {
    const int32_t *cospi   = cospi_arr(bit);
    const __m128i  cospi2  = _mm_set1_epi32(cospi[2]);
    const __m128i  cospi62 = _mm_set1_epi32(cospi[62]);
    const __m128i  cospi8  = _mm_set1_epi32(cospi[8]);
    const __m128i  cospi56 = _mm_set1_epi32(cospi[56]);
    const __m128i  cospi48 = _mm_set1_epi32(cospi[48]);
    const __m128i  cospi16 = _mm_set1_epi32(cospi[16]);
    const __m128i  cospi32 = _mm_set1_epi32(cospi[32]);
    const __m128i  rnding  = _mm_set1_epi32(1 << (bit - 1));
    const __m128i  zero    = _mm_setzero_si128();
    __m128i        v[16], x, y, temp1, temp2;

    // Calculate the column 0, 1, 2, 3
    {
        // stage 0
        // stage 1
        // stage 2
        x    = _mm_mullo_epi32(in[0], cospi62);
        v[0] = _mm_add_epi32(x, rnding);
        v[0] = _mm_srai_epi32(v[0], bit);

        x    = _mm_mullo_epi32(in[0], cospi2);
        v[1] = _mm_sub_epi32(zero, x);
        v[1] = _mm_add_epi32(v[1], rnding);
        v[1] = _mm_srai_epi32(v[1], bit);

        // stage 3
        v[8] = v[0];
        v[9] = v[1];

        // stage 4
        temp1 = _mm_mullo_epi32(v[8], cospi8);
        x     = _mm_mullo_epi32(v[9], cospi56);
        temp1 = _mm_add_epi32(temp1, x);
        temp1 = _mm_add_epi32(temp1, rnding);
        temp1 = _mm_srai_epi32(temp1, bit);

        temp2 = _mm_mullo_epi32(v[8], cospi56);
        x     = _mm_mullo_epi32(v[9], cospi8);
        temp2 = _mm_sub_epi32(temp2, x);
        temp2 = _mm_add_epi32(temp2, rnding);
        temp2 = _mm_srai_epi32(temp2, bit);
        v[8]  = temp1;
        v[9]  = temp2;

        // stage 5
        v[4]  = v[0];
        v[5]  = v[1];
        v[12] = v[8];
        v[13] = v[9];

        // stage 6
        temp1 = _mm_mullo_epi32(v[4], cospi16);
        x     = _mm_mullo_epi32(v[5], cospi48);
        temp1 = _mm_add_epi32(temp1, x);
        temp1 = _mm_add_epi32(temp1, rnding);
        temp1 = _mm_srai_epi32(temp1, bit);

        temp2 = _mm_mullo_epi32(v[4], cospi48);
        x     = _mm_mullo_epi32(v[5], cospi16);
        temp2 = _mm_sub_epi32(temp2, x);
        temp2 = _mm_add_epi32(temp2, rnding);
        temp2 = _mm_srai_epi32(temp2, bit);
        v[4]  = temp1;
        v[5]  = temp2;

        temp1 = _mm_mullo_epi32(v[12], cospi16);
        x     = _mm_mullo_epi32(v[13], cospi48);
        temp1 = _mm_add_epi32(temp1, x);
        temp1 = _mm_add_epi32(temp1, rnding);
        temp1 = _mm_srai_epi32(temp1, bit);

        temp2 = _mm_mullo_epi32(v[12], cospi48);
        x     = _mm_mullo_epi32(v[13], cospi16);
        temp2 = _mm_sub_epi32(temp2, x);
        temp2 = _mm_add_epi32(temp2, rnding);
        temp2 = _mm_srai_epi32(temp2, bit);
        v[12] = temp1;
        v[13] = temp2;

        // stage 7
        v[2]  = v[0];
        v[3]  = v[1];
        v[6]  = v[4];
        v[7]  = v[5];
        v[10] = v[8];
        v[11] = v[9];
        v[14] = v[12];
        v[15] = v[13];

        // stage 8
        y    = _mm_mullo_epi32(v[2], cospi32);
        x    = _mm_mullo_epi32(v[3], cospi32);
        v[2] = _mm_add_epi32(y, x);
        v[2] = _mm_add_epi32(v[2], rnding);
        v[2] = _mm_srai_epi32(v[2], bit);

        v[3] = _mm_sub_epi32(y, x);
        v[3] = _mm_add_epi32(v[3], rnding);
        v[3] = _mm_srai_epi32(v[3], bit);

        y    = _mm_mullo_epi32(v[6], cospi32);
        x    = _mm_mullo_epi32(v[7], cospi32);
        v[6] = _mm_add_epi32(y, x);
        v[6] = _mm_add_epi32(v[6], rnding);
        v[6] = _mm_srai_epi32(v[6], bit);

        v[7] = _mm_sub_epi32(y, x);
        v[7] = _mm_add_epi32(v[7], rnding);
        v[7] = _mm_srai_epi32(v[7], bit);

        y     = _mm_mullo_epi32(v[10], cospi32);
        x     = _mm_mullo_epi32(v[11], cospi32);
        v[10] = _mm_add_epi32(y, x);
        v[10] = _mm_add_epi32(v[10], rnding);
        v[10] = _mm_srai_epi32(v[10], bit);

        v[11] = _mm_sub_epi32(y, x);
        v[11] = _mm_add_epi32(v[11], rnding);
        v[11] = _mm_srai_epi32(v[11], bit);

        y     = _mm_mullo_epi32(v[14], cospi32);
        x     = _mm_mullo_epi32(v[15], cospi32);
        v[14] = _mm_add_epi32(y, x);
        v[14] = _mm_add_epi32(v[14], rnding);
        v[14] = _mm_srai_epi32(v[14], bit);

        v[15] = _mm_sub_epi32(y, x);
        v[15] = _mm_add_epi32(v[15], rnding);
        v[15] = _mm_srai_epi32(v[15], bit);

        // stage 9
        if (do_cols) {
            out[0]  = v[0];
            out[1]  = _mm_sub_epi32(zero, v[8]);
            out[2]  = v[12];
            out[3]  = _mm_sub_epi32(zero, v[4]);
            out[4]  = v[6];
            out[5]  = _mm_sub_epi32(zero, v[14]);
            out[6]  = v[10];
            out[7]  = _mm_sub_epi32(zero, v[2]);
            out[8]  = v[3];
            out[9]  = _mm_sub_epi32(zero, v[11]);
            out[10] = v[15];
            out[11] = _mm_sub_epi32(zero, v[7]);
            out[12] = v[5];
            out[13] = _mm_sub_epi32(zero, v[13]);
            out[14] = v[9];
            out[15] = _mm_sub_epi32(zero, v[1]);
        } else {
            const int32_t log_range_out = AOMMAX(16, bd + 6);
            const __m128i clamp_lo_out  = _mm_set1_epi32(-(1 << (log_range_out - 1)));
            const __m128i clamp_hi_out  = _mm_set1_epi32((1 << (log_range_out - 1)) - 1);

            neg_shift_sse4_1(v[0], v[8], out + 0, out + 1, &clamp_lo_out, &clamp_hi_out, out_shift);
            neg_shift_sse4_1(
                v[12], v[4], out + 2, out + 3, &clamp_lo_out, &clamp_hi_out, out_shift);
            neg_shift_sse4_1(
                v[6], v[14], out + 4, out + 5, &clamp_lo_out, &clamp_hi_out, out_shift);
            neg_shift_sse4_1(
                v[10], v[2], out + 6, out + 7, &clamp_lo_out, &clamp_hi_out, out_shift);
            neg_shift_sse4_1(
                v[3], v[11], out + 8, out + 9, &clamp_lo_out, &clamp_hi_out, out_shift);
            neg_shift_sse4_1(
                v[15], v[7], out + 10, out + 11, &clamp_lo_out, &clamp_hi_out, out_shift);
            neg_shift_sse4_1(
                v[5], v[13], out + 12, out + 13, &clamp_lo_out, &clamp_hi_out, out_shift);
            neg_shift_sse4_1(
                v[9], v[1], out + 14, out + 15, &clamp_lo_out, &clamp_hi_out, out_shift);
        }
    }
}

static void iadst16x16_low8_sse4_1(__m128i *in, __m128i *out, int32_t bit, int32_t do_cols,
                                   int32_t bd, int32_t out_shift) {
    const int32_t *cospi     = cospi_arr(bit);
    const __m128i  cospi2    = _mm_set1_epi32(cospi[2]);
    const __m128i  cospi62   = _mm_set1_epi32(cospi[62]);
    const __m128i  cospi10   = _mm_set1_epi32(cospi[10]);
    const __m128i  cospi54   = _mm_set1_epi32(cospi[54]);
    const __m128i  cospi18   = _mm_set1_epi32(cospi[18]);
    const __m128i  cospi46   = _mm_set1_epi32(cospi[46]);
    const __m128i  cospi26   = _mm_set1_epi32(cospi[26]);
    const __m128i  cospi38   = _mm_set1_epi32(cospi[38]);
    const __m128i  cospi34   = _mm_set1_epi32(cospi[34]);
    const __m128i  cospi30   = _mm_set1_epi32(cospi[30]);
    const __m128i  cospi42   = _mm_set1_epi32(cospi[42]);
    const __m128i  cospi22   = _mm_set1_epi32(cospi[22]);
    const __m128i  cospi50   = _mm_set1_epi32(cospi[50]);
    const __m128i  cospi14   = _mm_set1_epi32(cospi[14]);
    const __m128i  cospi58   = _mm_set1_epi32(cospi[58]);
    const __m128i  cospi6    = _mm_set1_epi32(cospi[6]);
    const __m128i  cospi8    = _mm_set1_epi32(cospi[8]);
    const __m128i  cospi56   = _mm_set1_epi32(cospi[56]);
    const __m128i  cospi40   = _mm_set1_epi32(cospi[40]);
    const __m128i  cospi24   = _mm_set1_epi32(cospi[24]);
    const __m128i  cospim56  = _mm_set1_epi32(-cospi[56]);
    const __m128i  cospim24  = _mm_set1_epi32(-cospi[24]);
    const __m128i  cospi48   = _mm_set1_epi32(cospi[48]);
    const __m128i  cospi16   = _mm_set1_epi32(cospi[16]);
    const __m128i  cospim48  = _mm_set1_epi32(-cospi[48]);
    const __m128i  cospi32   = _mm_set1_epi32(cospi[32]);
    const __m128i  rnding    = _mm_set1_epi32(1 << (bit - 1));
    const int32_t  log_range = AOMMAX(16, bd + (do_cols ? 6 : 8));
    const __m128i  clamp_lo  = _mm_set1_epi32(-(1 << (log_range - 1)));
    const __m128i  clamp_hi  = _mm_set1_epi32((1 << (log_range - 1)) - 1);
    __m128i        u[16], x, y;

    // Calculate the column 0, 1, 2, 3
    {
        // stage 0
        // stage 1
        // stage 2
        __m128i zero = _mm_setzero_si128();
        x            = _mm_mullo_epi32(in[0], cospi62);
        u[0]         = _mm_add_epi32(x, rnding);
        u[0]         = _mm_srai_epi32(u[0], bit);

        x    = _mm_mullo_epi32(in[0], cospi2);
        u[1] = _mm_sub_epi32(zero, x);
        u[1] = _mm_add_epi32(u[1], rnding);
        u[1] = _mm_srai_epi32(u[1], bit);

        x    = _mm_mullo_epi32(in[2], cospi54);
        u[2] = _mm_add_epi32(x, rnding);
        u[2] = _mm_srai_epi32(u[2], bit);

        x    = _mm_mullo_epi32(in[2], cospi10);
        u[3] = _mm_sub_epi32(zero, x);
        u[3] = _mm_add_epi32(u[3], rnding);
        u[3] = _mm_srai_epi32(u[3], bit);

        x    = _mm_mullo_epi32(in[4], cospi46);
        u[4] = _mm_add_epi32(x, rnding);
        u[4] = _mm_srai_epi32(u[4], bit);

        x    = _mm_mullo_epi32(in[4], cospi18);
        u[5] = _mm_sub_epi32(zero, x);
        u[5] = _mm_add_epi32(u[5], rnding);
        u[5] = _mm_srai_epi32(u[5], bit);

        x    = _mm_mullo_epi32(in[6], cospi38);
        u[6] = _mm_add_epi32(x, rnding);
        u[6] = _mm_srai_epi32(u[6], bit);

        x    = _mm_mullo_epi32(in[6], cospi26);
        u[7] = _mm_sub_epi32(zero, x);
        u[7] = _mm_add_epi32(u[7], rnding);
        u[7] = _mm_srai_epi32(u[7], bit);

        u[8] = _mm_mullo_epi32(in[7], cospi34);
        u[8] = _mm_add_epi32(u[8], rnding);
        u[8] = _mm_srai_epi32(u[8], bit);

        u[9] = _mm_mullo_epi32(in[7], cospi30);
        u[9] = _mm_add_epi32(u[9], rnding);
        u[9] = _mm_srai_epi32(u[9], bit);

        u[10] = _mm_mullo_epi32(in[5], cospi42);
        u[10] = _mm_add_epi32(u[10], rnding);
        u[10] = _mm_srai_epi32(u[10], bit);

        u[11] = _mm_mullo_epi32(in[5], cospi22);
        u[11] = _mm_add_epi32(u[11], rnding);
        u[11] = _mm_srai_epi32(u[11], bit);

        u[12] = _mm_mullo_epi32(in[3], cospi50);
        u[12] = _mm_add_epi32(u[12], rnding);
        u[12] = _mm_srai_epi32(u[12], bit);

        u[13] = _mm_mullo_epi32(in[3], cospi14);
        u[13] = _mm_add_epi32(u[13], rnding);
        u[13] = _mm_srai_epi32(u[13], bit);

        u[14] = _mm_mullo_epi32(in[1], cospi58);
        u[14] = _mm_add_epi32(u[14], rnding);
        u[14] = _mm_srai_epi32(u[14], bit);

        u[15] = _mm_mullo_epi32(in[1], cospi6);
        u[15] = _mm_add_epi32(u[15], rnding);
        u[15] = _mm_srai_epi32(u[15], bit);

        // stage 3
        addsub_sse4_1(u[0], u[8], &u[0], &u[8], &clamp_lo, &clamp_hi);
        addsub_sse4_1(u[1], u[9], &u[1], &u[9], &clamp_lo, &clamp_hi);
        addsub_sse4_1(u[2], u[10], &u[2], &u[10], &clamp_lo, &clamp_hi);
        addsub_sse4_1(u[3], u[11], &u[3], &u[11], &clamp_lo, &clamp_hi);
        addsub_sse4_1(u[4], u[12], &u[4], &u[12], &clamp_lo, &clamp_hi);
        addsub_sse4_1(u[5], u[13], &u[5], &u[13], &clamp_lo, &clamp_hi);
        addsub_sse4_1(u[6], u[14], &u[6], &u[14], &clamp_lo, &clamp_hi);
        addsub_sse4_1(u[7], u[15], &u[7], &u[15], &clamp_lo, &clamp_hi);

        // stage 4
        y    = _mm_mullo_epi32(u[8], cospi56);
        x    = _mm_mullo_epi32(u[9], cospi56);
        u[8] = _mm_mullo_epi32(u[8], cospi8);
        u[8] = _mm_add_epi32(u[8], x);
        u[8] = _mm_add_epi32(u[8], rnding);
        u[8] = _mm_srai_epi32(u[8], bit);

        x    = _mm_mullo_epi32(u[9], cospi8);
        u[9] = _mm_sub_epi32(y, x);
        u[9] = _mm_add_epi32(u[9], rnding);
        u[9] = _mm_srai_epi32(u[9], bit);

        x     = _mm_mullo_epi32(u[11], cospi24);
        y     = _mm_mullo_epi32(u[10], cospi24);
        u[10] = _mm_mullo_epi32(u[10], cospi40);
        u[10] = _mm_add_epi32(u[10], x);
        u[10] = _mm_add_epi32(u[10], rnding);
        u[10] = _mm_srai_epi32(u[10], bit);

        x     = _mm_mullo_epi32(u[11], cospi40);
        u[11] = _mm_sub_epi32(y, x);
        u[11] = _mm_add_epi32(u[11], rnding);
        u[11] = _mm_srai_epi32(u[11], bit);

        x     = _mm_mullo_epi32(u[13], cospi8);
        y     = _mm_mullo_epi32(u[12], cospi8);
        u[12] = _mm_mullo_epi32(u[12], cospim56);
        u[12] = _mm_add_epi32(u[12], x);
        u[12] = _mm_add_epi32(u[12], rnding);
        u[12] = _mm_srai_epi32(u[12], bit);

        x     = _mm_mullo_epi32(u[13], cospim56);
        u[13] = _mm_sub_epi32(y, x);
        u[13] = _mm_add_epi32(u[13], rnding);
        u[13] = _mm_srai_epi32(u[13], bit);

        x     = _mm_mullo_epi32(u[15], cospi40);
        y     = _mm_mullo_epi32(u[14], cospi40);
        u[14] = _mm_mullo_epi32(u[14], cospim24);
        u[14] = _mm_add_epi32(u[14], x);
        u[14] = _mm_add_epi32(u[14], rnding);
        u[14] = _mm_srai_epi32(u[14], bit);

        x     = _mm_mullo_epi32(u[15], cospim24);
        u[15] = _mm_sub_epi32(y, x);
        u[15] = _mm_add_epi32(u[15], rnding);
        u[15] = _mm_srai_epi32(u[15], bit);

        // stage 5
        addsub_sse4_1(u[0], u[4], &u[0], &u[4], &clamp_lo, &clamp_hi);
        addsub_sse4_1(u[1], u[5], &u[1], &u[5], &clamp_lo, &clamp_hi);
        addsub_sse4_1(u[2], u[6], &u[2], &u[6], &clamp_lo, &clamp_hi);
        addsub_sse4_1(u[3], u[7], &u[3], &u[7], &clamp_lo, &clamp_hi);
        addsub_sse4_1(u[8], u[12], &u[8], &u[12], &clamp_lo, &clamp_hi);
        addsub_sse4_1(u[9], u[13], &u[9], &u[13], &clamp_lo, &clamp_hi);
        addsub_sse4_1(u[10], u[14], &u[10], &u[14], &clamp_lo, &clamp_hi);
        addsub_sse4_1(u[11], u[15], &u[11], &u[15], &clamp_lo, &clamp_hi);

        // stage 6
        x    = _mm_mullo_epi32(u[5], cospi48);
        y    = _mm_mullo_epi32(u[4], cospi48);
        u[4] = _mm_mullo_epi32(u[4], cospi16);
        u[4] = _mm_add_epi32(u[4], x);
        u[4] = _mm_add_epi32(u[4], rnding);
        u[4] = _mm_srai_epi32(u[4], bit);

        x    = _mm_mullo_epi32(u[5], cospi16);
        u[5] = _mm_sub_epi32(y, x);
        u[5] = _mm_add_epi32(u[5], rnding);
        u[5] = _mm_srai_epi32(u[5], bit);

        x    = _mm_mullo_epi32(u[7], cospi16);
        y    = _mm_mullo_epi32(u[6], cospi16);
        u[6] = _mm_mullo_epi32(u[6], cospim48);
        u[6] = _mm_add_epi32(u[6], x);
        u[6] = _mm_add_epi32(u[6], rnding);
        u[6] = _mm_srai_epi32(u[6], bit);

        x    = _mm_mullo_epi32(u[7], cospim48);
        u[7] = _mm_sub_epi32(y, x);
        u[7] = _mm_add_epi32(u[7], rnding);
        u[7] = _mm_srai_epi32(u[7], bit);

        x     = _mm_mullo_epi32(u[13], cospi48);
        y     = _mm_mullo_epi32(u[12], cospi48);
        u[12] = _mm_mullo_epi32(u[12], cospi16);
        u[12] = _mm_add_epi32(u[12], x);
        u[12] = _mm_add_epi32(u[12], rnding);
        u[12] = _mm_srai_epi32(u[12], bit);

        x     = _mm_mullo_epi32(u[13], cospi16);
        u[13] = _mm_sub_epi32(y, x);
        u[13] = _mm_add_epi32(u[13], rnding);
        u[13] = _mm_srai_epi32(u[13], bit);

        x     = _mm_mullo_epi32(u[15], cospi16);
        y     = _mm_mullo_epi32(u[14], cospi16);
        u[14] = _mm_mullo_epi32(u[14], cospim48);
        u[14] = _mm_add_epi32(u[14], x);
        u[14] = _mm_add_epi32(u[14], rnding);
        u[14] = _mm_srai_epi32(u[14], bit);

        x     = _mm_mullo_epi32(u[15], cospim48);
        u[15] = _mm_sub_epi32(y, x);
        u[15] = _mm_add_epi32(u[15], rnding);
        u[15] = _mm_srai_epi32(u[15], bit);

        // stage 7
        addsub_sse4_1(u[0], u[2], &u[0], &u[2], &clamp_lo, &clamp_hi);
        addsub_sse4_1(u[1], u[3], &u[1], &u[3], &clamp_lo, &clamp_hi);
        addsub_sse4_1(u[4], u[6], &u[4], &u[6], &clamp_lo, &clamp_hi);
        addsub_sse4_1(u[5], u[7], &u[5], &u[7], &clamp_lo, &clamp_hi);
        addsub_sse4_1(u[8], u[10], &u[8], &u[10], &clamp_lo, &clamp_hi);
        addsub_sse4_1(u[9], u[11], &u[9], &u[11], &clamp_lo, &clamp_hi);
        addsub_sse4_1(u[12], u[14], &u[12], &u[14], &clamp_lo, &clamp_hi);
        addsub_sse4_1(u[13], u[15], &u[13], &u[15], &clamp_lo, &clamp_hi);

        // stage 8
        y    = _mm_mullo_epi32(u[2], cospi32);
        x    = _mm_mullo_epi32(u[3], cospi32);
        u[2] = _mm_add_epi32(y, x);
        u[2] = _mm_add_epi32(u[2], rnding);
        u[2] = _mm_srai_epi32(u[2], bit);

        u[3] = _mm_sub_epi32(y, x);
        u[3] = _mm_add_epi32(u[3], rnding);
        u[3] = _mm_srai_epi32(u[3], bit);
        y    = _mm_mullo_epi32(u[6], cospi32);
        x    = _mm_mullo_epi32(u[7], cospi32);
        u[6] = _mm_add_epi32(y, x);
        u[6] = _mm_add_epi32(u[6], rnding);
        u[6] = _mm_srai_epi32(u[6], bit);

        u[7] = _mm_sub_epi32(y, x);
        u[7] = _mm_add_epi32(u[7], rnding);
        u[7] = _mm_srai_epi32(u[7], bit);

        y     = _mm_mullo_epi32(u[10], cospi32);
        x     = _mm_mullo_epi32(u[11], cospi32);
        u[10] = _mm_add_epi32(y, x);
        u[10] = _mm_add_epi32(u[10], rnding);
        u[10] = _mm_srai_epi32(u[10], bit);

        u[11] = _mm_sub_epi32(y, x);
        u[11] = _mm_add_epi32(u[11], rnding);
        u[11] = _mm_srai_epi32(u[11], bit);

        y     = _mm_mullo_epi32(u[14], cospi32);
        x     = _mm_mullo_epi32(u[15], cospi32);
        u[14] = _mm_add_epi32(y, x);
        u[14] = _mm_add_epi32(u[14], rnding);
        u[14] = _mm_srai_epi32(u[14], bit);

        u[15] = _mm_sub_epi32(y, x);
        u[15] = _mm_add_epi32(u[15], rnding);
        u[15] = _mm_srai_epi32(u[15], bit);

        // stage 9
        if (do_cols) {
            out[0]  = u[0];
            out[1]  = _mm_sub_epi32(zero, u[8]);
            out[2]  = u[12];
            out[3]  = _mm_sub_epi32(zero, u[4]);
            out[4]  = u[6];
            out[5]  = _mm_sub_epi32(zero, u[14]);
            out[6]  = u[10];
            out[7]  = _mm_sub_epi32(zero, u[2]);
            out[8]  = u[3];
            out[9]  = _mm_sub_epi32(zero, u[11]);
            out[10] = u[15];
            out[11] = _mm_sub_epi32(zero, u[7]);
            out[12] = u[5];
            out[13] = _mm_sub_epi32(zero, u[13]);
            out[14] = u[9];
            out[15] = _mm_sub_epi32(zero, u[1]);
        } else {
            const int32_t log_range_out = AOMMAX(16, bd + 6);
            const __m128i clamp_lo_out  = _mm_set1_epi32(-(1 << (log_range_out - 1)));
            const __m128i clamp_hi_out  = _mm_set1_epi32((1 << (log_range_out - 1)) - 1);

            neg_shift_sse4_1(u[0], u[8], out + 0, out + 1, &clamp_lo_out, &clamp_hi_out, out_shift);
            neg_shift_sse4_1(
                u[12], u[4], out + 2, out + 3, &clamp_lo_out, &clamp_hi_out, out_shift);
            neg_shift_sse4_1(
                u[6], u[14], out + 4, out + 5, &clamp_lo_out, &clamp_hi_out, out_shift);
            neg_shift_sse4_1(
                u[10], u[2], out + 6, out + 7, &clamp_lo_out, &clamp_hi_out, out_shift);
            neg_shift_sse4_1(
                u[3], u[11], out + 8, out + 9, &clamp_lo_out, &clamp_hi_out, out_shift);
            neg_shift_sse4_1(
                u[15], u[7], out + 10, out + 11, &clamp_lo_out, &clamp_hi_out, out_shift);
            neg_shift_sse4_1(
                u[5], u[13], out + 12, out + 13, &clamp_lo_out, &clamp_hi_out, out_shift);
            neg_shift_sse4_1(
                u[9], u[1], out + 14, out + 15, &clamp_lo_out, &clamp_hi_out, out_shift);
        }
    }
}

static INLINE void highbd_write_buffer_4xn_sse4_1(__m128i *in, uint16_t *output_r, int32_t stride_r,
                                                  uint16_t *output_w, int32_t stride_w,
                                                  int32_t flipud, int32_t height,
                                                  const int32_t bd) {
    int32_t       j    = flipud ? (height - 1) : 0;
    const int32_t step = flipud ? -1 : 1;
    for (int32_t i = 0; i < height; ++i, j += step) {
        __m128i v = _mm_loadl_epi64((__m128i const *)(output_r + i * stride_r));
        __m128i u = highbd_get_recon_4xn_sse4_1(v, in[j], bd);

        _mm_storel_epi64((__m128i *)(output_w + i * stride_w), u);
    }
}

static INLINE void idct32_stage4_sse4_1(__m128i *bf1, const __m128i *cospim8,
                                        const __m128i *cospi56, const __m128i *cospi8,
                                        const __m128i *cospim56, const __m128i *cospim40,
                                        const __m128i *cospi24, const __m128i *cospi40,
                                        const __m128i *cospim24, const __m128i *rounding, int bit) {
    __m128i temp1, temp2;
    temp1   = half_btf_sse4_1(cospim8, &bf1[17], cospi56, &bf1[30], rounding, bit);
    bf1[30] = half_btf_sse4_1(cospi56, &bf1[17], cospi8, &bf1[30], rounding, bit);
    bf1[17] = temp1;

    temp2   = half_btf_sse4_1(cospim56, &bf1[18], cospim8, &bf1[29], rounding, bit);
    bf1[29] = half_btf_sse4_1(cospim8, &bf1[18], cospi56, &bf1[29], rounding, bit);
    bf1[18] = temp2;

    temp1   = half_btf_sse4_1(cospim40, &bf1[21], cospi24, &bf1[26], rounding, bit);
    bf1[26] = half_btf_sse4_1(cospi24, &bf1[21], cospi40, &bf1[26], rounding, bit);
    bf1[21] = temp1;

    temp2   = half_btf_sse4_1(cospim24, &bf1[22], cospim40, &bf1[25], rounding, bit);
    bf1[25] = half_btf_sse4_1(cospim40, &bf1[22], cospi24, &bf1[25], rounding, bit);
    bf1[22] = temp2;
}

static INLINE void idct32_stage5_sse4_1(__m128i *bf1, const __m128i *cospim16,
                                        const __m128i *cospi48, const __m128i *cospi16,
                                        const __m128i *cospim48, const __m128i *clamp_lo,
                                        const __m128i *clamp_hi, const __m128i *rounding, int bit) {
    __m128i temp1, temp2;
    temp1   = half_btf_sse4_1(cospim16, &bf1[9], cospi48, &bf1[14], rounding, bit);
    bf1[14] = half_btf_sse4_1(cospi48, &bf1[9], cospi16, &bf1[14], rounding, bit);
    bf1[9]  = temp1;

    temp2   = half_btf_sse4_1(cospim48, &bf1[10], cospim16, &bf1[13], rounding, bit);
    bf1[13] = half_btf_sse4_1(cospim16, &bf1[10], cospi48, &bf1[13], rounding, bit);
    bf1[10] = temp2;

    addsub_sse4_1(bf1[16], bf1[19], bf1 + 16, bf1 + 19, clamp_lo, clamp_hi);
    addsub_sse4_1(bf1[17], bf1[18], bf1 + 17, bf1 + 18, clamp_lo, clamp_hi);
    addsub_sse4_1(bf1[23], bf1[20], bf1 + 23, bf1 + 20, clamp_lo, clamp_hi);
    addsub_sse4_1(bf1[22], bf1[21], bf1 + 22, bf1 + 21, clamp_lo, clamp_hi);
    addsub_sse4_1(bf1[24], bf1[27], bf1 + 24, bf1 + 27, clamp_lo, clamp_hi);
    addsub_sse4_1(bf1[25], bf1[26], bf1 + 25, bf1 + 26, clamp_lo, clamp_hi);
    addsub_sse4_1(bf1[31], bf1[28], bf1 + 31, bf1 + 28, clamp_lo, clamp_hi);
    addsub_sse4_1(bf1[30], bf1[29], bf1 + 30, bf1 + 29, clamp_lo, clamp_hi);
}

static INLINE void idct32_stage6_sse4_1(__m128i *bf1, const __m128i *cospim32,
                                        const __m128i *cospi32, const __m128i *cospim16,
                                        const __m128i *cospi48, const __m128i *cospi16,
                                        const __m128i *cospim48, const __m128i *clamp_lo,
                                        const __m128i *clamp_hi, const __m128i *rounding, int bit) {
    __m128i temp1, temp2;
    temp1  = half_btf_sse4_1(cospim32, &bf1[5], cospi32, &bf1[6], rounding, bit);
    bf1[6] = half_btf_sse4_1(cospi32, &bf1[5], cospi32, &bf1[6], rounding, bit);
    bf1[5] = temp1;

    addsub_sse4_1(bf1[8], bf1[11], bf1 + 8, bf1 + 11, clamp_lo, clamp_hi);
    addsub_sse4_1(bf1[9], bf1[10], bf1 + 9, bf1 + 10, clamp_lo, clamp_hi);
    addsub_sse4_1(bf1[15], bf1[12], bf1 + 15, bf1 + 12, clamp_lo, clamp_hi);
    addsub_sse4_1(bf1[14], bf1[13], bf1 + 14, bf1 + 13, clamp_lo, clamp_hi);

    temp1   = half_btf_sse4_1(cospim16, &bf1[18], cospi48, &bf1[29], rounding, bit);
    bf1[29] = half_btf_sse4_1(cospi48, &bf1[18], cospi16, &bf1[29], rounding, bit);
    bf1[18] = temp1;
    temp2   = half_btf_sse4_1(cospim16, &bf1[19], cospi48, &bf1[28], rounding, bit);
    bf1[28] = half_btf_sse4_1(cospi48, &bf1[19], cospi16, &bf1[28], rounding, bit);
    bf1[19] = temp2;
    temp1   = half_btf_sse4_1(cospim48, &bf1[20], cospim16, &bf1[27], rounding, bit);
    bf1[27] = half_btf_sse4_1(cospim16, &bf1[20], cospi48, &bf1[27], rounding, bit);
    bf1[20] = temp1;
    temp2   = half_btf_sse4_1(cospim48, &bf1[21], cospim16, &bf1[26], rounding, bit);
    bf1[26] = half_btf_sse4_1(cospim16, &bf1[21], cospi48, &bf1[26], rounding, bit);
    bf1[21] = temp2;
}

static INLINE void idct32_stage7_sse4_1(__m128i *bf1, const __m128i *cospim32,
                                        const __m128i *cospi32, const __m128i *clamp_lo,
                                        const __m128i *clamp_hi, const __m128i *rounding, int bit) {
    __m128i temp1, temp2;
    addsub_sse4_1(bf1[0], bf1[7], bf1 + 0, bf1 + 7, clamp_lo, clamp_hi);
    addsub_sse4_1(bf1[1], bf1[6], bf1 + 1, bf1 + 6, clamp_lo, clamp_hi);
    addsub_sse4_1(bf1[2], bf1[5], bf1 + 2, bf1 + 5, clamp_lo, clamp_hi);
    addsub_sse4_1(bf1[3], bf1[4], bf1 + 3, bf1 + 4, clamp_lo, clamp_hi);

    temp1   = half_btf_sse4_1(cospim32, &bf1[10], cospi32, &bf1[13], rounding, bit);
    bf1[13] = half_btf_sse4_1(cospi32, &bf1[10], cospi32, &bf1[13], rounding, bit);
    bf1[10] = temp1;
    temp2   = half_btf_sse4_1(cospim32, &bf1[11], cospi32, &bf1[12], rounding, bit);
    bf1[12] = half_btf_sse4_1(cospi32, &bf1[11], cospi32, &bf1[12], rounding, bit);
    bf1[11] = temp2;

    addsub_sse4_1(bf1[16], bf1[23], bf1 + 16, bf1 + 23, clamp_lo, clamp_hi);
    addsub_sse4_1(bf1[17], bf1[22], bf1 + 17, bf1 + 22, clamp_lo, clamp_hi);
    addsub_sse4_1(bf1[18], bf1[21], bf1 + 18, bf1 + 21, clamp_lo, clamp_hi);
    addsub_sse4_1(bf1[19], bf1[20], bf1 + 19, bf1 + 20, clamp_lo, clamp_hi);
    addsub_sse4_1(bf1[31], bf1[24], bf1 + 31, bf1 + 24, clamp_lo, clamp_hi);
    addsub_sse4_1(bf1[30], bf1[25], bf1 + 30, bf1 + 25, clamp_lo, clamp_hi);
    addsub_sse4_1(bf1[29], bf1[26], bf1 + 29, bf1 + 26, clamp_lo, clamp_hi);
    addsub_sse4_1(bf1[28], bf1[27], bf1 + 28, bf1 + 27, clamp_lo, clamp_hi);
}

static INLINE void idct32_stage8_sse4_1(__m128i *bf1, const __m128i *cospim32,
                                        const __m128i *cospi32, const __m128i *clamp_lo,
                                        const __m128i *clamp_hi, const __m128i *rounding, int bit) {
    __m128i temp1, temp2;
    addsub_sse4_1(bf1[0], bf1[15], bf1 + 0, bf1 + 15, clamp_lo, clamp_hi);
    addsub_sse4_1(bf1[1], bf1[14], bf1 + 1, bf1 + 14, clamp_lo, clamp_hi);
    addsub_sse4_1(bf1[2], bf1[13], bf1 + 2, bf1 + 13, clamp_lo, clamp_hi);
    addsub_sse4_1(bf1[3], bf1[12], bf1 + 3, bf1 + 12, clamp_lo, clamp_hi);
    addsub_sse4_1(bf1[4], bf1[11], bf1 + 4, bf1 + 11, clamp_lo, clamp_hi);
    addsub_sse4_1(bf1[5], bf1[10], bf1 + 5, bf1 + 10, clamp_lo, clamp_hi);
    addsub_sse4_1(bf1[6], bf1[9], bf1 + 6, bf1 + 9, clamp_lo, clamp_hi);
    addsub_sse4_1(bf1[7], bf1[8], bf1 + 7, bf1 + 8, clamp_lo, clamp_hi);

    temp1   = half_btf_sse4_1(cospim32, &bf1[20], cospi32, &bf1[27], rounding, bit);
    bf1[27] = half_btf_sse4_1(cospi32, &bf1[20], cospi32, &bf1[27], rounding, bit);
    bf1[20] = temp1;
    temp2   = half_btf_sse4_1(cospim32, &bf1[21], cospi32, &bf1[26], rounding, bit);
    bf1[26] = half_btf_sse4_1(cospi32, &bf1[21], cospi32, &bf1[26], rounding, bit);
    bf1[21] = temp2;
    temp1   = half_btf_sse4_1(cospim32, &bf1[22], cospi32, &bf1[25], rounding, bit);
    bf1[25] = half_btf_sse4_1(cospi32, &bf1[22], cospi32, &bf1[25], rounding, bit);
    bf1[22] = temp1;
    temp2   = half_btf_sse4_1(cospim32, &bf1[23], cospi32, &bf1[24], rounding, bit);
    bf1[24] = half_btf_sse4_1(cospi32, &bf1[23], cospi32, &bf1[24], rounding, bit);
    bf1[23] = temp2;
}

static INLINE void idct32_stage9_sse4_1(__m128i *bf1, __m128i *out, const int do_cols, const int bd,
                                        const int out_shift, const __m128i *clamp_lo,
                                        const __m128i *clamp_hi) {
    addsub_sse4_1(bf1[0], bf1[31], out + 0, out + 31, clamp_lo, clamp_hi);
    addsub_sse4_1(bf1[1], bf1[30], out + 1, out + 30, clamp_lo, clamp_hi);
    addsub_sse4_1(bf1[2], bf1[29], out + 2, out + 29, clamp_lo, clamp_hi);
    addsub_sse4_1(bf1[3], bf1[28], out + 3, out + 28, clamp_lo, clamp_hi);
    addsub_sse4_1(bf1[4], bf1[27], out + 4, out + 27, clamp_lo, clamp_hi);
    addsub_sse4_1(bf1[5], bf1[26], out + 5, out + 26, clamp_lo, clamp_hi);
    addsub_sse4_1(bf1[6], bf1[25], out + 6, out + 25, clamp_lo, clamp_hi);
    addsub_sse4_1(bf1[7], bf1[24], out + 7, out + 24, clamp_lo, clamp_hi);
    addsub_sse4_1(bf1[8], bf1[23], out + 8, out + 23, clamp_lo, clamp_hi);
    addsub_sse4_1(bf1[9], bf1[22], out + 9, out + 22, clamp_lo, clamp_hi);
    addsub_sse4_1(bf1[10], bf1[21], out + 10, out + 21, clamp_lo, clamp_hi);
    addsub_sse4_1(bf1[11], bf1[20], out + 11, out + 20, clamp_lo, clamp_hi);
    addsub_sse4_1(bf1[12], bf1[19], out + 12, out + 19, clamp_lo, clamp_hi);
    addsub_sse4_1(bf1[13], bf1[18], out + 13, out + 18, clamp_lo, clamp_hi);
    addsub_sse4_1(bf1[14], bf1[17], out + 14, out + 17, clamp_lo, clamp_hi);
    addsub_sse4_1(bf1[15], bf1[16], out + 15, out + 16, clamp_lo, clamp_hi);

    if (!do_cols) {
        const int     log_range_out = AOMMAX(16, bd + 6);
        const __m128i clamp_lo_out  = _mm_set1_epi32(-(1 << (log_range_out - 1)));
        const __m128i clamp_hi_out  = _mm_set1_epi32((1 << (log_range_out - 1)) - 1);
        for (int i = 0; i < 32; i += 8) {
            round_shift_4x4(out + i, out_shift);
            round_shift_4x4(out + i + 4, out_shift);
        }
        highbd_clamp_epi32_sse4_1(out, out, &clamp_lo_out, &clamp_hi_out, 32);
    }
}

static INLINE void idct64_stage8_sse4_1(__m128i *u, const __m128i *cospim32, const __m128i *cospi32,
                                        const __m128i *cospim16, const __m128i *cospi48,
                                        const __m128i *cospi16, const __m128i *cospim48,
                                        const __m128i *clamp_lo, const __m128i *clamp_hi,
                                        const __m128i *rnding, int bit) {
    int     i;
    __m128i temp1, temp2, temp3, temp4;
    temp1 = half_btf_sse4_1(cospim32, &u[10], cospi32, &u[13], rnding, bit);
    u[13] = half_btf_sse4_1(cospi32, &u[10], cospi32, &u[13], rnding, bit);
    u[10] = temp1;
    temp2 = half_btf_sse4_1(cospim32, &u[11], cospi32, &u[12], rnding, bit);
    u[12] = half_btf_sse4_1(cospi32, &u[11], cospi32, &u[12], rnding, bit);
    u[11] = temp2;

    for (i = 16; i < 20; ++i) {
        addsub_sse4_1(u[i], u[i ^ 7], &u[i], &u[i ^ 7], clamp_lo, clamp_hi);
        addsub_sse4_1(u[i ^ 15], u[i ^ 8], &u[i ^ 15], &u[i ^ 8], clamp_lo, clamp_hi);
    }

    temp1 = half_btf_sse4_1(cospim16, &u[36], cospi48, &u[59], rnding, bit);
    temp2 = half_btf_sse4_1(cospim16, &u[37], cospi48, &u[58], rnding, bit);
    temp3 = half_btf_sse4_1(cospim16, &u[38], cospi48, &u[57], rnding, bit);
    temp4 = half_btf_sse4_1(cospim16, &u[39], cospi48, &u[56], rnding, bit);
    u[56] = half_btf_sse4_1(cospi48, &u[39], cospi16, &u[56], rnding, bit);
    u[57] = half_btf_sse4_1(cospi48, &u[38], cospi16, &u[57], rnding, bit);
    u[58] = half_btf_sse4_1(cospi48, &u[37], cospi16, &u[58], rnding, bit);
    u[59] = half_btf_sse4_1(cospi48, &u[36], cospi16, &u[59], rnding, bit);
    u[36] = temp1;
    u[37] = temp2;
    u[38] = temp3;
    u[39] = temp4;

    temp1 = half_btf_sse4_1(cospim48, &u[40], cospim16, &u[55], rnding, bit);
    temp2 = half_btf_sse4_1(cospim48, &u[41], cospim16, &u[54], rnding, bit);
    temp3 = half_btf_sse4_1(cospim48, &u[42], cospim16, &u[53], rnding, bit);
    temp4 = half_btf_sse4_1(cospim48, &u[43], cospim16, &u[52], rnding, bit);
    u[52] = half_btf_sse4_1(cospim16, &u[43], cospi48, &u[52], rnding, bit);
    u[53] = half_btf_sse4_1(cospim16, &u[42], cospi48, &u[53], rnding, bit);
    u[54] = half_btf_sse4_1(cospim16, &u[41], cospi48, &u[54], rnding, bit);
    u[55] = half_btf_sse4_1(cospim16, &u[40], cospi48, &u[55], rnding, bit);
    u[40] = temp1;
    u[41] = temp2;
    u[42] = temp3;
    u[43] = temp4;
}

static INLINE void idct64_stage9_sse4_1(__m128i *u, const __m128i *cospim32, const __m128i *cospi32,
                                        const __m128i *clamp_lo, const __m128i *clamp_hi,
                                        const __m128i *rnding, int bit) {
    int     i;
    __m128i temp1, temp2, temp3, temp4;
    for (i = 0; i < 8; ++i) {
        addsub_sse4_1(u[i], u[15 - i], &u[i], &u[15 - i], clamp_lo, clamp_hi);
    }

    temp1 = half_btf_sse4_1(cospim32, &u[20], cospi32, &u[27], rnding, bit);
    temp2 = half_btf_sse4_1(cospim32, &u[21], cospi32, &u[26], rnding, bit);
    temp3 = half_btf_sse4_1(cospim32, &u[22], cospi32, &u[25], rnding, bit);
    temp4 = half_btf_sse4_1(cospim32, &u[23], cospi32, &u[24], rnding, bit);
    u[24] = half_btf_sse4_1(cospi32, &u[23], cospi32, &u[24], rnding, bit);
    u[25] = half_btf_sse4_1(cospi32, &u[22], cospi32, &u[25], rnding, bit);
    u[26] = half_btf_sse4_1(cospi32, &u[21], cospi32, &u[26], rnding, bit);
    u[27] = half_btf_sse4_1(cospi32, &u[20], cospi32, &u[27], rnding, bit);
    u[20] = temp1;
    u[21] = temp2;
    u[22] = temp3;
    u[23] = temp4;
    for (i = 32; i < 40; i++) {
        addsub_sse4_1(u[i], u[i ^ 15], &u[i], &u[i ^ 15], clamp_lo, clamp_hi);
    }

    for (i = 48; i < 56; i++) {
        addsub_sse4_1(u[i ^ 15], u[i], &u[i ^ 15], &u[i], clamp_lo, clamp_hi);
    }
}

static INLINE void idct64_stage10_sse4_1(__m128i *u, const __m128i *cospim32,
                                         const __m128i *cospi32, const __m128i *clamp_lo,
                                         const __m128i *clamp_hi, const __m128i *rnding, int bit) {
    __m128i temp1, temp2, temp3, temp4;
    for (int i = 0; i < 16; i++) {
        addsub_sse4_1(u[i], u[31 - i], &u[i], &u[31 - i], clamp_lo, clamp_hi);
    }

    temp1 = half_btf_sse4_1(cospim32, &u[40], cospi32, &u[55], rnding, bit);
    temp2 = half_btf_sse4_1(cospim32, &u[41], cospi32, &u[54], rnding, bit);
    temp3 = half_btf_sse4_1(cospim32, &u[42], cospi32, &u[53], rnding, bit);
    temp4 = half_btf_sse4_1(cospim32, &u[43], cospi32, &u[52], rnding, bit);
    u[52] = half_btf_sse4_1(cospi32, &u[43], cospi32, &u[52], rnding, bit);
    u[53] = half_btf_sse4_1(cospi32, &u[42], cospi32, &u[53], rnding, bit);
    u[54] = half_btf_sse4_1(cospi32, &u[41], cospi32, &u[54], rnding, bit);
    u[55] = half_btf_sse4_1(cospi32, &u[40], cospi32, &u[55], rnding, bit);
    u[40] = temp1;
    u[41] = temp2;
    u[42] = temp3;
    u[43] = temp4;

    temp1 = half_btf_sse4_1(cospim32, &u[44], cospi32, &u[51], rnding, bit);
    temp2 = half_btf_sse4_1(cospim32, &u[45], cospi32, &u[50], rnding, bit);
    temp3 = half_btf_sse4_1(cospim32, &u[46], cospi32, &u[49], rnding, bit);
    temp4 = half_btf_sse4_1(cospim32, &u[47], cospi32, &u[48], rnding, bit);
    u[48] = half_btf_sse4_1(cospi32, &u[47], cospi32, &u[48], rnding, bit);
    u[49] = half_btf_sse4_1(cospi32, &u[46], cospi32, &u[49], rnding, bit);
    u[50] = half_btf_sse4_1(cospi32, &u[45], cospi32, &u[50], rnding, bit);
    u[51] = half_btf_sse4_1(cospi32, &u[44], cospi32, &u[51], rnding, bit);
    u[44] = temp1;
    u[45] = temp2;
    u[46] = temp3;
    u[47] = temp4;
}

static INLINE void idct64_stage11_sse4_1(__m128i *u, __m128i *out, int do_cols, int bd,
                                         int out_shift, const __m128i *clamp_lo,
                                         const __m128i *clamp_hi) {
    for (int i = 0; i < 32; i++) {
        addsub_sse4_1(u[i], u[63 - i], out + i, out + 63 - i, clamp_lo, clamp_hi);
    }

    if (!do_cols) {
        const int     log_range_out = AOMMAX(16, bd + 6);
        const __m128i clamp_lo_out  = _mm_set1_epi32(-(1 << (log_range_out - 1)));
        const __m128i clamp_hi_out  = _mm_set1_epi32((1 << (log_range_out - 1)) - 1);

        for (int i = 0; i < 64; i += 4) {
            round_shift_4x4(out + i, out_shift);
            highbd_clamp_epi32_sse4_1(out + i, out + i, &clamp_lo_out, &clamp_hi_out, 4);
        }
    }
}

static void idct32x32_low1_sse4_1(__m128i *in, __m128i *out, int bit, int do_cols, int bd,
                                  int out_shift) {
    const int32_t *cospi     = cospi_arr(bit);
    const __m128i  cospi32   = _mm_set1_epi32(cospi[32]);
    const __m128i  rounding  = _mm_set1_epi32(1 << (bit - 1));
    const int      log_range = AOMMAX(16, bd + (do_cols ? 6 : 8));
    __m128i        clamp_lo  = _mm_set1_epi32(-(1 << (log_range - 1)));
    __m128i        clamp_hi  = _mm_set1_epi32((1 << (log_range - 1)) - 1);
    __m128i        bf1;

    // stage 0
    // stage 1
    bf1 = in[0];

    // stage 2
    // stage 3
    // stage 4
    // stage 5
    bf1 = half_btf_0_sse4_1(&cospi32, &bf1, &rounding, bit);

    // stage 6
    // stage 7
    // stage 8
    // stage 9
    if (do_cols) {
        bf1 = _mm_max_epi32(bf1, clamp_lo);
        bf1 = _mm_min_epi32(bf1, clamp_hi);
    } else {
        const int log_range_out = AOMMAX(16, bd + 6);
        clamp_lo                = _mm_set1_epi32(-(1 << (log_range_out - 1)));
        clamp_hi                = _mm_set1_epi32((1 << (log_range_out - 1)) - 1);
        if (out_shift != 0) {
            __m128i offset = _mm_set1_epi32((1 << out_shift) >> 1);
            bf1            = _mm_add_epi32(bf1, offset);
            bf1            = _mm_sra_epi32(bf1, _mm_cvtsi32_si128(out_shift));
        }
    }

    bf1     = _mm_max_epi32(bf1, clamp_lo);
    bf1     = _mm_min_epi32(bf1, clamp_hi);
    out[0]  = bf1;
    out[1]  = bf1;
    out[2]  = bf1;
    out[3]  = bf1;
    out[4]  = bf1;
    out[5]  = bf1;
    out[6]  = bf1;
    out[7]  = bf1;
    out[8]  = bf1;
    out[9]  = bf1;
    out[10] = bf1;
    out[11] = bf1;
    out[12] = bf1;
    out[13] = bf1;
    out[14] = bf1;
    out[15] = bf1;
    out[16] = bf1;
    out[17] = bf1;
    out[18] = bf1;
    out[19] = bf1;
    out[20] = bf1;
    out[21] = bf1;
    out[22] = bf1;
    out[23] = bf1;
    out[24] = bf1;
    out[25] = bf1;
    out[26] = bf1;
    out[27] = bf1;
    out[28] = bf1;
    out[29] = bf1;
    out[30] = bf1;
    out[31] = bf1;
}

static void idct32x32_low8_sse4_1(__m128i *in, __m128i *out, int bit, int do_cols, int bd,
                                  int out_shift) {
    const int32_t *cospi     = cospi_arr(bit);
    const __m128i  cospi62   = _mm_set1_epi32(cospi[62]);
    const __m128i  cospi14   = _mm_set1_epi32(cospi[14]);
    const __m128i  cospi54   = _mm_set1_epi32(cospi[54]);
    const __m128i  cospi6    = _mm_set1_epi32(cospi[6]);
    const __m128i  cospi10   = _mm_set1_epi32(cospi[10]);
    const __m128i  cospi2    = _mm_set1_epi32(cospi[2]);
    const __m128i  cospim58  = _mm_set1_epi32(-cospi[58]);
    const __m128i  cospim50  = _mm_set1_epi32(-cospi[50]);
    const __m128i  cospi60   = _mm_set1_epi32(cospi[60]);
    const __m128i  cospi12   = _mm_set1_epi32(cospi[12]);
    const __m128i  cospi4    = _mm_set1_epi32(cospi[4]);
    const __m128i  cospim52  = _mm_set1_epi32(-cospi[52]);
    const __m128i  cospi56   = _mm_set1_epi32(cospi[56]);
    const __m128i  cospi24   = _mm_set1_epi32(cospi[24]);
    const __m128i  cospi40   = _mm_set1_epi32(cospi[40]);
    const __m128i  cospi8    = _mm_set1_epi32(cospi[8]);
    const __m128i  cospim40  = _mm_set1_epi32(-cospi[40]);
    const __m128i  cospim8   = _mm_set1_epi32(-cospi[8]);
    const __m128i  cospim56  = _mm_set1_epi32(-cospi[56]);
    const __m128i  cospim24  = _mm_set1_epi32(-cospi[24]);
    const __m128i  cospi32   = _mm_set1_epi32(cospi[32]);
    const __m128i  cospim32  = _mm_set1_epi32(-cospi[32]);
    const __m128i  cospi48   = _mm_set1_epi32(cospi[48]);
    const __m128i  cospim48  = _mm_set1_epi32(-cospi[48]);
    const __m128i  cospi16   = _mm_set1_epi32(cospi[16]);
    const __m128i  cospim16  = _mm_set1_epi32(-cospi[16]);
    const __m128i  rounding  = _mm_set1_epi32(1 << (bit - 1));
    const int      log_range = AOMMAX(16, bd + (do_cols ? 6 : 8));
    const __m128i  clamp_lo  = _mm_set1_epi32(-(1 << (log_range - 1)));
    const __m128i  clamp_hi  = _mm_set1_epi32((1 << (log_range - 1)) - 1);
    __m128i        bf1[32];

    // stage 0
    // stage 1
    bf1[0]  = in[0];
    bf1[4]  = in[4];
    bf1[8]  = in[2];
    bf1[12] = in[6];
    bf1[16] = in[1];
    bf1[20] = in[5];
    bf1[24] = in[3];
    bf1[28] = in[7];

    // stage 2
    bf1[31] = half_btf_0_sse4_1(&cospi2, &bf1[16], &rounding, bit);
    bf1[16] = half_btf_0_sse4_1(&cospi62, &bf1[16], &rounding, bit);
    bf1[19] = half_btf_0_sse4_1(&cospim50, &bf1[28], &rounding, bit);
    bf1[28] = half_btf_0_sse4_1(&cospi14, &bf1[28], &rounding, bit);
    bf1[27] = half_btf_0_sse4_1(&cospi10, &bf1[20], &rounding, bit);
    bf1[20] = half_btf_0_sse4_1(&cospi54, &bf1[20], &rounding, bit);
    bf1[23] = half_btf_0_sse4_1(&cospim58, &bf1[24], &rounding, bit);
    bf1[24] = half_btf_0_sse4_1(&cospi6, &bf1[24], &rounding, bit);

    // stage 3
    bf1[15] = half_btf_0_sse4_1(&cospi4, &bf1[8], &rounding, bit);
    bf1[8]  = half_btf_0_sse4_1(&cospi60, &bf1[8], &rounding, bit);

    bf1[11] = half_btf_0_sse4_1(&cospim52, &bf1[12], &rounding, bit);
    bf1[12] = half_btf_0_sse4_1(&cospi12, &bf1[12], &rounding, bit);
    bf1[17] = bf1[16];
    bf1[18] = bf1[19];
    bf1[21] = bf1[20];
    bf1[22] = bf1[23];
    bf1[25] = bf1[24];
    bf1[26] = bf1[27];
    bf1[29] = bf1[28];
    bf1[30] = bf1[31];

    // stage 4 :
    bf1[7] = half_btf_0_sse4_1(&cospi8, &bf1[4], &rounding, bit);
    bf1[4] = half_btf_0_sse4_1(&cospi56, &bf1[4], &rounding, bit);

    bf1[9]  = bf1[8];
    bf1[10] = bf1[11];
    bf1[13] = bf1[12];
    bf1[14] = bf1[15];

    idct32_stage4_sse4_1(bf1,
                         &cospim8,
                         &cospi56,
                         &cospi8,
                         &cospim56,
                         &cospim40,
                         &cospi24,
                         &cospi40,
                         &cospim24,
                         &rounding,
                         bit);

    // stage 5
    bf1[0] = half_btf_0_sse4_1(&cospi32, &bf1[0], &rounding, bit);
    bf1[1] = bf1[0];
    bf1[5] = bf1[4];
    bf1[6] = bf1[7];

    idct32_stage5_sse4_1(
        bf1, &cospim16, &cospi48, &cospi16, &cospim48, &clamp_lo, &clamp_hi, &rounding, bit);

    // stage 6
    bf1[3] = bf1[0];
    bf1[2] = bf1[1];

    idct32_stage6_sse4_1(bf1,
                         &cospim32,
                         &cospi32,
                         &cospim16,
                         &cospi48,
                         &cospi16,
                         &cospim48,
                         &clamp_lo,
                         &clamp_hi,
                         &rounding,
                         bit);

    // stage 7
    idct32_stage7_sse4_1(bf1, &cospim32, &cospi32, &clamp_lo, &clamp_hi, &rounding, bit);

    // stage 8
    idct32_stage8_sse4_1(bf1, &cospim32, &cospi32, &clamp_lo, &clamp_hi, &rounding, bit);

    // stage 9
    idct32_stage9_sse4_1(bf1, out, do_cols, bd, out_shift, &clamp_lo, &clamp_hi);
}

static void idct32x32_low16_sse4_1(__m128i *in, __m128i *out, int bit, int do_cols, int bd,
                                   int out_shift) {
    const int32_t *cospi     = cospi_arr(bit);
    const __m128i  cospi62   = _mm_set1_epi32(cospi[62]);
    const __m128i  cospi30   = _mm_set1_epi32(cospi[30]);
    const __m128i  cospi46   = _mm_set1_epi32(cospi[46]);
    const __m128i  cospi14   = _mm_set1_epi32(cospi[14]);
    const __m128i  cospi54   = _mm_set1_epi32(cospi[54]);
    const __m128i  cospi22   = _mm_set1_epi32(cospi[22]);
    const __m128i  cospi38   = _mm_set1_epi32(cospi[38]);
    const __m128i  cospi6    = _mm_set1_epi32(cospi[6]);
    const __m128i  cospi26   = _mm_set1_epi32(cospi[26]);
    const __m128i  cospi10   = _mm_set1_epi32(cospi[10]);
    const __m128i  cospi18   = _mm_set1_epi32(cospi[18]);
    const __m128i  cospi2    = _mm_set1_epi32(cospi[2]);
    const __m128i  cospim58  = _mm_set1_epi32(-cospi[58]);
    const __m128i  cospim42  = _mm_set1_epi32(-cospi[42]);
    const __m128i  cospim50  = _mm_set1_epi32(-cospi[50]);
    const __m128i  cospim34  = _mm_set1_epi32(-cospi[34]);
    const __m128i  cospi60   = _mm_set1_epi32(cospi[60]);
    const __m128i  cospi28   = _mm_set1_epi32(cospi[28]);
    const __m128i  cospi44   = _mm_set1_epi32(cospi[44]);
    const __m128i  cospi12   = _mm_set1_epi32(cospi[12]);
    const __m128i  cospi20   = _mm_set1_epi32(cospi[20]);
    const __m128i  cospi4    = _mm_set1_epi32(cospi[4]);
    const __m128i  cospim52  = _mm_set1_epi32(-cospi[52]);
    const __m128i  cospim36  = _mm_set1_epi32(-cospi[36]);
    const __m128i  cospi56   = _mm_set1_epi32(cospi[56]);
    const __m128i  cospi24   = _mm_set1_epi32(cospi[24]);
    const __m128i  cospi40   = _mm_set1_epi32(cospi[40]);
    const __m128i  cospi8    = _mm_set1_epi32(cospi[8]);
    const __m128i  cospim40  = _mm_set1_epi32(-cospi[40]);
    const __m128i  cospim8   = _mm_set1_epi32(-cospi[8]);
    const __m128i  cospim56  = _mm_set1_epi32(-cospi[56]);
    const __m128i  cospim24  = _mm_set1_epi32(-cospi[24]);
    const __m128i  cospi32   = _mm_set1_epi32(cospi[32]);
    const __m128i  cospim32  = _mm_set1_epi32(-cospi[32]);
    const __m128i  cospi48   = _mm_set1_epi32(cospi[48]);
    const __m128i  cospim48  = _mm_set1_epi32(-cospi[48]);
    const __m128i  cospi16   = _mm_set1_epi32(cospi[16]);
    const __m128i  cospim16  = _mm_set1_epi32(-cospi[16]);
    const __m128i  rounding  = _mm_set1_epi32(1 << (bit - 1));
    const int      log_range = AOMMAX(16, bd + (do_cols ? 6 : 8));
    const __m128i  clamp_lo  = _mm_set1_epi32(-(1 << (log_range - 1)));
    const __m128i  clamp_hi  = _mm_set1_epi32((1 << (log_range - 1)) - 1);
    __m128i        bf1[32];

    // stage 0
    // stage 1

    bf1[0]  = in[0];
    bf1[2]  = in[8];
    bf1[4]  = in[4];
    bf1[6]  = in[12];
    bf1[8]  = in[2];
    bf1[10] = in[10];
    bf1[12] = in[6];
    bf1[14] = in[14];
    bf1[16] = in[1];
    bf1[18] = in[9];
    bf1[20] = in[5];
    bf1[22] = in[13];
    bf1[24] = in[3];
    bf1[26] = in[11];
    bf1[28] = in[7];
    bf1[30] = in[15];

    // stage 2
    bf1[31] = half_btf_0_sse4_1(&cospi2, &bf1[16], &rounding, bit);
    bf1[16] = half_btf_0_sse4_1(&cospi62, &bf1[16], &rounding, bit);
    bf1[17] = half_btf_0_sse4_1(&cospim34, &bf1[30], &rounding, bit);
    bf1[30] = half_btf_0_sse4_1(&cospi30, &bf1[30], &rounding, bit);
    bf1[29] = half_btf_0_sse4_1(&cospi18, &bf1[18], &rounding, bit);
    bf1[18] = half_btf_0_sse4_1(&cospi46, &bf1[18], &rounding, bit);
    bf1[19] = half_btf_0_sse4_1(&cospim50, &bf1[28], &rounding, bit);
    bf1[28] = half_btf_0_sse4_1(&cospi14, &bf1[28], &rounding, bit);
    bf1[27] = half_btf_0_sse4_1(&cospi10, &bf1[20], &rounding, bit);
    bf1[20] = half_btf_0_sse4_1(&cospi54, &bf1[20], &rounding, bit);
    bf1[21] = half_btf_0_sse4_1(&cospim42, &bf1[26], &rounding, bit);
    bf1[26] = half_btf_0_sse4_1(&cospi22, &bf1[26], &rounding, bit);
    bf1[25] = half_btf_0_sse4_1(&cospi26, &bf1[22], &rounding, bit);
    bf1[22] = half_btf_0_sse4_1(&cospi38, &bf1[22], &rounding, bit);
    bf1[23] = half_btf_0_sse4_1(&cospim58, &bf1[24], &rounding, bit);
    bf1[24] = half_btf_0_sse4_1(&cospi6, &bf1[24], &rounding, bit);

    // stage 3
    bf1[15] = half_btf_0_sse4_1(&cospi4, &bf1[8], &rounding, bit);
    bf1[8]  = half_btf_0_sse4_1(&cospi60, &bf1[8], &rounding, bit);
    bf1[9]  = half_btf_0_sse4_1(&cospim36, &bf1[14], &rounding, bit);
    bf1[14] = half_btf_0_sse4_1(&cospi28, &bf1[14], &rounding, bit);
    bf1[13] = half_btf_0_sse4_1(&cospi20, &bf1[10], &rounding, bit);
    bf1[10] = half_btf_0_sse4_1(&cospi44, &bf1[10], &rounding, bit);
    bf1[11] = half_btf_0_sse4_1(&cospim52, &bf1[12], &rounding, bit);
    bf1[12] = half_btf_0_sse4_1(&cospi12, &bf1[12], &rounding, bit);

    addsub_sse4_1(bf1[16], bf1[17], bf1 + 16, bf1 + 17, &clamp_lo, &clamp_hi);
    addsub_sse4_1(bf1[19], bf1[18], bf1 + 19, bf1 + 18, &clamp_lo, &clamp_hi);
    addsub_sse4_1(bf1[20], bf1[21], bf1 + 20, bf1 + 21, &clamp_lo, &clamp_hi);
    addsub_sse4_1(bf1[23], bf1[22], bf1 + 23, bf1 + 22, &clamp_lo, &clamp_hi);
    addsub_sse4_1(bf1[24], bf1[25], bf1 + 24, bf1 + 25, &clamp_lo, &clamp_hi);
    addsub_sse4_1(bf1[27], bf1[26], bf1 + 27, bf1 + 26, &clamp_lo, &clamp_hi);
    addsub_sse4_1(bf1[28], bf1[29], bf1 + 28, bf1 + 29, &clamp_lo, &clamp_hi);
    addsub_sse4_1(bf1[31], bf1[30], bf1 + 31, bf1 + 30, &clamp_lo, &clamp_hi);
    // stage 4
    bf1[7] = half_btf_0_sse4_1(&cospi8, &bf1[4], &rounding, bit);
    bf1[4] = half_btf_0_sse4_1(&cospi56, &bf1[4], &rounding, bit);
    bf1[5] = half_btf_0_sse4_1(&cospim40, &bf1[6], &rounding, bit);
    bf1[6] = half_btf_0_sse4_1(&cospi24, &bf1[6], &rounding, bit);

    addsub_sse4_1(bf1[8], bf1[9], bf1 + 8, bf1 + 9, &clamp_lo, &clamp_hi);
    addsub_sse4_1(bf1[11], bf1[10], bf1 + 11, bf1 + 10, &clamp_lo, &clamp_hi);
    addsub_sse4_1(bf1[12], bf1[13], bf1 + 12, bf1 + 13, &clamp_lo, &clamp_hi);
    addsub_sse4_1(bf1[15], bf1[14], bf1 + 15, bf1 + 14, &clamp_lo, &clamp_hi);

    idct32_stage4_sse4_1(bf1,
                         &cospim8,
                         &cospi56,
                         &cospi8,
                         &cospim56,
                         &cospim40,
                         &cospi24,
                         &cospi40,
                         &cospim24,
                         &rounding,
                         bit);

    // stage 5
    bf1[0] = half_btf_0_sse4_1(&cospi32, &bf1[0], &rounding, bit);
    bf1[1] = bf1[0];
    bf1[3] = half_btf_0_sse4_1(&cospi16, &bf1[2], &rounding, bit);
    bf1[2] = half_btf_0_sse4_1(&cospi48, &bf1[2], &rounding, bit);

    addsub_sse4_1(bf1[4], bf1[5], bf1 + 4, bf1 + 5, &clamp_lo, &clamp_hi);
    addsub_sse4_1(bf1[7], bf1[6], bf1 + 7, bf1 + 6, &clamp_lo, &clamp_hi);

    idct32_stage5_sse4_1(
        bf1, &cospim16, &cospi48, &cospi16, &cospim48, &clamp_lo, &clamp_hi, &rounding, bit);

    // stage 6
    addsub_sse4_1(bf1[0], bf1[3], bf1 + 0, bf1 + 3, &clamp_lo, &clamp_hi);
    addsub_sse4_1(bf1[1], bf1[2], bf1 + 1, bf1 + 2, &clamp_lo, &clamp_hi);

    idct32_stage6_sse4_1(bf1,
                         &cospim32,
                         &cospi32,
                         &cospim16,
                         &cospi48,
                         &cospi16,
                         &cospim48,
                         &clamp_lo,
                         &clamp_hi,
                         &rounding,
                         bit);

    // stage 7
    idct32_stage7_sse4_1(bf1, &cospim32, &cospi32, &clamp_lo, &clamp_hi, &rounding, bit);

    // stage 8
    idct32_stage8_sse4_1(bf1, &cospim32, &cospi32, &clamp_lo, &clamp_hi, &rounding, bit);
    // stage 9
    idct32_stage9_sse4_1(bf1, out, do_cols, bd, out_shift, &clamp_lo, &clamp_hi);
}

static void idct32x32_sse4_1(__m128i *in, __m128i *out, int bit, int do_cols, int bd,
                             int out_shift) {
    const int32_t *cospi     = cospi_arr(bit);
    const __m128i  cospi62   = _mm_set1_epi32(cospi[62]);
    const __m128i  cospi30   = _mm_set1_epi32(cospi[30]);
    const __m128i  cospi46   = _mm_set1_epi32(cospi[46]);
    const __m128i  cospi14   = _mm_set1_epi32(cospi[14]);
    const __m128i  cospi54   = _mm_set1_epi32(cospi[54]);
    const __m128i  cospi22   = _mm_set1_epi32(cospi[22]);
    const __m128i  cospi38   = _mm_set1_epi32(cospi[38]);
    const __m128i  cospi6    = _mm_set1_epi32(cospi[6]);
    const __m128i  cospi58   = _mm_set1_epi32(cospi[58]);
    const __m128i  cospi26   = _mm_set1_epi32(cospi[26]);
    const __m128i  cospi42   = _mm_set1_epi32(cospi[42]);
    const __m128i  cospi10   = _mm_set1_epi32(cospi[10]);
    const __m128i  cospi50   = _mm_set1_epi32(cospi[50]);
    const __m128i  cospi18   = _mm_set1_epi32(cospi[18]);
    const __m128i  cospi34   = _mm_set1_epi32(cospi[34]);
    const __m128i  cospi2    = _mm_set1_epi32(cospi[2]);
    const __m128i  cospim58  = _mm_set1_epi32(-cospi[58]);
    const __m128i  cospim26  = _mm_set1_epi32(-cospi[26]);
    const __m128i  cospim42  = _mm_set1_epi32(-cospi[42]);
    const __m128i  cospim10  = _mm_set1_epi32(-cospi[10]);
    const __m128i  cospim50  = _mm_set1_epi32(-cospi[50]);
    const __m128i  cospim18  = _mm_set1_epi32(-cospi[18]);
    const __m128i  cospim34  = _mm_set1_epi32(-cospi[34]);
    const __m128i  cospim2   = _mm_set1_epi32(-cospi[2]);
    const __m128i  cospi60   = _mm_set1_epi32(cospi[60]);
    const __m128i  cospi28   = _mm_set1_epi32(cospi[28]);
    const __m128i  cospi44   = _mm_set1_epi32(cospi[44]);
    const __m128i  cospi12   = _mm_set1_epi32(cospi[12]);
    const __m128i  cospi52   = _mm_set1_epi32(cospi[52]);
    const __m128i  cospi20   = _mm_set1_epi32(cospi[20]);
    const __m128i  cospi36   = _mm_set1_epi32(cospi[36]);
    const __m128i  cospi4    = _mm_set1_epi32(cospi[4]);
    const __m128i  cospim52  = _mm_set1_epi32(-cospi[52]);
    const __m128i  cospim20  = _mm_set1_epi32(-cospi[20]);
    const __m128i  cospim36  = _mm_set1_epi32(-cospi[36]);
    const __m128i  cospim4   = _mm_set1_epi32(-cospi[4]);
    const __m128i  cospi56   = _mm_set1_epi32(cospi[56]);
    const __m128i  cospi24   = _mm_set1_epi32(cospi[24]);
    const __m128i  cospi40   = _mm_set1_epi32(cospi[40]);
    const __m128i  cospi8    = _mm_set1_epi32(cospi[8]);
    const __m128i  cospim40  = _mm_set1_epi32(-cospi[40]);
    const __m128i  cospim8   = _mm_set1_epi32(-cospi[8]);
    const __m128i  cospim56  = _mm_set1_epi32(-cospi[56]);
    const __m128i  cospim24  = _mm_set1_epi32(-cospi[24]);
    const __m128i  cospi32   = _mm_set1_epi32(cospi[32]);
    const __m128i  cospim32  = _mm_set1_epi32(-cospi[32]);
    const __m128i  cospi48   = _mm_set1_epi32(cospi[48]);
    const __m128i  cospim48  = _mm_set1_epi32(-cospi[48]);
    const __m128i  cospi16   = _mm_set1_epi32(cospi[16]);
    const __m128i  cospim16  = _mm_set1_epi32(-cospi[16]);
    const __m128i  rounding  = _mm_set1_epi32(1 << (bit - 1));
    const int      log_range = AOMMAX(16, bd + (do_cols ? 6 : 8));
    const __m128i  clamp_lo  = _mm_set1_epi32(-(1 << (log_range - 1)));
    const __m128i  clamp_hi  = _mm_set1_epi32((1 << (log_range - 1)) - 1);
    __m128i        bf1[32], bf0[32];

    // stage 0
    // stage 1
    bf1[0]  = in[0];
    bf1[1]  = in[16];
    bf1[2]  = in[8];
    bf1[3]  = in[24];
    bf1[4]  = in[4];
    bf1[5]  = in[20];
    bf1[6]  = in[12];
    bf1[7]  = in[28];
    bf1[8]  = in[2];
    bf1[9]  = in[18];
    bf1[10] = in[10];
    bf1[11] = in[26];
    bf1[12] = in[6];
    bf1[13] = in[22];
    bf1[14] = in[14];
    bf1[15] = in[30];
    bf1[16] = in[1];
    bf1[17] = in[17];
    bf1[18] = in[9];
    bf1[19] = in[25];
    bf1[20] = in[5];
    bf1[21] = in[21];
    bf1[22] = in[13];
    bf1[23] = in[29];
    bf1[24] = in[3];
    bf1[25] = in[19];
    bf1[26] = in[11];
    bf1[27] = in[27];
    bf1[28] = in[7];
    bf1[29] = in[23];
    bf1[30] = in[15];
    bf1[31] = in[31];

    // stage 2
    bf0[0]  = bf1[0];
    bf0[1]  = bf1[1];
    bf0[2]  = bf1[2];
    bf0[3]  = bf1[3];
    bf0[4]  = bf1[4];
    bf0[5]  = bf1[5];
    bf0[6]  = bf1[6];
    bf0[7]  = bf1[7];
    bf0[8]  = bf1[8];
    bf0[9]  = bf1[9];
    bf0[10] = bf1[10];
    bf0[11] = bf1[11];
    bf0[12] = bf1[12];
    bf0[13] = bf1[13];
    bf0[14] = bf1[14];
    bf0[15] = bf1[15];
    bf0[16] = half_btf_sse4_1(&cospi62, &bf1[16], &cospim2, &bf1[31], &rounding, bit);
    bf0[17] = half_btf_sse4_1(&cospi30, &bf1[17], &cospim34, &bf1[30], &rounding, bit);
    bf0[18] = half_btf_sse4_1(&cospi46, &bf1[18], &cospim18, &bf1[29], &rounding, bit);
    bf0[19] = half_btf_sse4_1(&cospi14, &bf1[19], &cospim50, &bf1[28], &rounding, bit);
    bf0[20] = half_btf_sse4_1(&cospi54, &bf1[20], &cospim10, &bf1[27], &rounding, bit);
    bf0[21] = half_btf_sse4_1(&cospi22, &bf1[21], &cospim42, &bf1[26], &rounding, bit);
    bf0[22] = half_btf_sse4_1(&cospi38, &bf1[22], &cospim26, &bf1[25], &rounding, bit);
    bf0[23] = half_btf_sse4_1(&cospi6, &bf1[23], &cospim58, &bf1[24], &rounding, bit);
    bf0[24] = half_btf_sse4_1(&cospi58, &bf1[23], &cospi6, &bf1[24], &rounding, bit);
    bf0[25] = half_btf_sse4_1(&cospi26, &bf1[22], &cospi38, &bf1[25], &rounding, bit);
    bf0[26] = half_btf_sse4_1(&cospi42, &bf1[21], &cospi22, &bf1[26], &rounding, bit);
    bf0[27] = half_btf_sse4_1(&cospi10, &bf1[20], &cospi54, &bf1[27], &rounding, bit);
    bf0[28] = half_btf_sse4_1(&cospi50, &bf1[19], &cospi14, &bf1[28], &rounding, bit);
    bf0[29] = half_btf_sse4_1(&cospi18, &bf1[18], &cospi46, &bf1[29], &rounding, bit);
    bf0[30] = half_btf_sse4_1(&cospi34, &bf1[17], &cospi30, &bf1[30], &rounding, bit);
    bf0[31] = half_btf_sse4_1(&cospi2, &bf1[16], &cospi62, &bf1[31], &rounding, bit);

    // stage 3
    bf1[0]  = bf0[0];
    bf1[1]  = bf0[1];
    bf1[2]  = bf0[2];
    bf1[3]  = bf0[3];
    bf1[4]  = bf0[4];
    bf1[5]  = bf0[5];
    bf1[6]  = bf0[6];
    bf1[7]  = bf0[7];
    bf1[8]  = half_btf_sse4_1(&cospi60, &bf0[8], &cospim4, &bf0[15], &rounding, bit);
    bf1[9]  = half_btf_sse4_1(&cospi28, &bf0[9], &cospim36, &bf0[14], &rounding, bit);
    bf1[10] = half_btf_sse4_1(&cospi44, &bf0[10], &cospim20, &bf0[13], &rounding, bit);
    bf1[11] = half_btf_sse4_1(&cospi12, &bf0[11], &cospim52, &bf0[12], &rounding, bit);
    bf1[12] = half_btf_sse4_1(&cospi52, &bf0[11], &cospi12, &bf0[12], &rounding, bit);
    bf1[13] = half_btf_sse4_1(&cospi20, &bf0[10], &cospi44, &bf0[13], &rounding, bit);
    bf1[14] = half_btf_sse4_1(&cospi36, &bf0[9], &cospi28, &bf0[14], &rounding, bit);
    bf1[15] = half_btf_sse4_1(&cospi4, &bf0[8], &cospi60, &bf0[15], &rounding, bit);

    addsub_sse4_1(bf0[16], bf0[17], bf1 + 16, bf1 + 17, &clamp_lo, &clamp_hi);
    addsub_sse4_1(bf0[19], bf0[18], bf1 + 19, bf1 + 18, &clamp_lo, &clamp_hi);
    addsub_sse4_1(bf0[20], bf0[21], bf1 + 20, bf1 + 21, &clamp_lo, &clamp_hi);
    addsub_sse4_1(bf0[23], bf0[22], bf1 + 23, bf1 + 22, &clamp_lo, &clamp_hi);
    addsub_sse4_1(bf0[24], bf0[25], bf1 + 24, bf1 + 25, &clamp_lo, &clamp_hi);
    addsub_sse4_1(bf0[27], bf0[26], bf1 + 27, bf1 + 26, &clamp_lo, &clamp_hi);
    addsub_sse4_1(bf0[28], bf0[29], bf1 + 28, bf1 + 29, &clamp_lo, &clamp_hi);
    addsub_sse4_1(bf0[31], bf0[30], bf1 + 31, bf1 + 30, &clamp_lo, &clamp_hi);

    // stage 4
    bf0[0] = bf1[0];
    bf0[1] = bf1[1];
    bf0[2] = bf1[2];
    bf0[3] = bf1[3];
    bf0[4] = half_btf_sse4_1(&cospi56, &bf1[4], &cospim8, &bf1[7], &rounding, bit);
    bf0[5] = half_btf_sse4_1(&cospi24, &bf1[5], &cospim40, &bf1[6], &rounding, bit);
    bf0[6] = half_btf_sse4_1(&cospi40, &bf1[5], &cospi24, &bf1[6], &rounding, bit);
    bf0[7] = half_btf_sse4_1(&cospi8, &bf1[4], &cospi56, &bf1[7], &rounding, bit);

    addsub_sse4_1(bf1[8], bf1[9], bf0 + 8, bf0 + 9, &clamp_lo, &clamp_hi);
    addsub_sse4_1(bf1[11], bf1[10], bf0 + 11, bf0 + 10, &clamp_lo, &clamp_hi);
    addsub_sse4_1(bf1[12], bf1[13], bf0 + 12, bf0 + 13, &clamp_lo, &clamp_hi);
    addsub_sse4_1(bf1[15], bf1[14], bf0 + 15, bf0 + 14, &clamp_lo, &clamp_hi);

    bf0[16] = bf1[16];
    bf0[17] = half_btf_sse4_1(&cospim8, &bf1[17], &cospi56, &bf1[30], &rounding, bit);
    bf0[18] = half_btf_sse4_1(&cospim56, &bf1[18], &cospim8, &bf1[29], &rounding, bit);
    bf0[19] = bf1[19];
    bf0[20] = bf1[20];
    bf0[21] = half_btf_sse4_1(&cospim40, &bf1[21], &cospi24, &bf1[26], &rounding, bit);
    bf0[22] = half_btf_sse4_1(&cospim24, &bf1[22], &cospim40, &bf1[25], &rounding, bit);
    bf0[23] = bf1[23];
    bf0[24] = bf1[24];
    bf0[25] = half_btf_sse4_1(&cospim40, &bf1[22], &cospi24, &bf1[25], &rounding, bit);
    bf0[26] = half_btf_sse4_1(&cospi24, &bf1[21], &cospi40, &bf1[26], &rounding, bit);
    bf0[27] = bf1[27];
    bf0[28] = bf1[28];
    bf0[29] = half_btf_sse4_1(&cospim8, &bf1[18], &cospi56, &bf1[29], &rounding, bit);
    bf0[30] = half_btf_sse4_1(&cospi56, &bf1[17], &cospi8, &bf1[30], &rounding, bit);
    bf0[31] = bf1[31];

    // stage 5
    bf1[0] = half_btf_sse4_1(&cospi32, &bf0[0], &cospi32, &bf0[1], &rounding, bit);
    bf1[1] = half_btf_sse4_1(&cospi32, &bf0[0], &cospim32, &bf0[1], &rounding, bit);
    bf1[2] = half_btf_sse4_1(&cospi48, &bf0[2], &cospim16, &bf0[3], &rounding, bit);
    bf1[3] = half_btf_sse4_1(&cospi16, &bf0[2], &cospi48, &bf0[3], &rounding, bit);
    addsub_sse4_1(bf0[4], bf0[5], bf1 + 4, bf1 + 5, &clamp_lo, &clamp_hi);
    addsub_sse4_1(bf0[7], bf0[6], bf1 + 7, bf1 + 6, &clamp_lo, &clamp_hi);
    bf1[8]  = bf0[8];
    bf1[9]  = half_btf_sse4_1(&cospim16, &bf0[9], &cospi48, &bf0[14], &rounding, bit);
    bf1[10] = half_btf_sse4_1(&cospim48, &bf0[10], &cospim16, &bf0[13], &rounding, bit);
    bf1[11] = bf0[11];
    bf1[12] = bf0[12];
    bf1[13] = half_btf_sse4_1(&cospim16, &bf0[10], &cospi48, &bf0[13], &rounding, bit);
    bf1[14] = half_btf_sse4_1(&cospi48, &bf0[9], &cospi16, &bf0[14], &rounding, bit);
    bf1[15] = bf0[15];
    addsub_sse4_1(bf0[16], bf0[19], bf1 + 16, bf1 + 19, &clamp_lo, &clamp_hi);
    addsub_sse4_1(bf0[17], bf0[18], bf1 + 17, bf1 + 18, &clamp_lo, &clamp_hi);
    addsub_sse4_1(bf0[23], bf0[20], bf1 + 23, bf1 + 20, &clamp_lo, &clamp_hi);
    addsub_sse4_1(bf0[22], bf0[21], bf1 + 22, bf1 + 21, &clamp_lo, &clamp_hi);
    addsub_sse4_1(bf0[24], bf0[27], bf1 + 24, bf1 + 27, &clamp_lo, &clamp_hi);
    addsub_sse4_1(bf0[25], bf0[26], bf1 + 25, bf1 + 26, &clamp_lo, &clamp_hi);
    addsub_sse4_1(bf0[31], bf0[28], bf1 + 31, bf1 + 28, &clamp_lo, &clamp_hi);
    addsub_sse4_1(bf0[30], bf0[29], bf1 + 30, bf1 + 29, &clamp_lo, &clamp_hi);

    // stage 6
    addsub_sse4_1(bf1[0], bf1[3], bf0 + 0, bf0 + 3, &clamp_lo, &clamp_hi);
    addsub_sse4_1(bf1[1], bf1[2], bf0 + 1, bf0 + 2, &clamp_lo, &clamp_hi);
    bf0[4] = bf1[4];
    bf0[5] = half_btf_sse4_1(&cospim32, &bf1[5], &cospi32, &bf1[6], &rounding, bit);
    bf0[6] = half_btf_sse4_1(&cospi32, &bf1[5], &cospi32, &bf1[6], &rounding, bit);
    bf0[7] = bf1[7];
    addsub_sse4_1(bf1[8], bf1[11], bf0 + 8, bf0 + 11, &clamp_lo, &clamp_hi);
    addsub_sse4_1(bf1[9], bf1[10], bf0 + 9, bf0 + 10, &clamp_lo, &clamp_hi);
    addsub_sse4_1(bf1[15], bf1[12], bf0 + 15, bf0 + 12, &clamp_lo, &clamp_hi);
    addsub_sse4_1(bf1[14], bf1[13], bf0 + 14, bf0 + 13, &clamp_lo, &clamp_hi);
    bf0[16] = bf1[16];
    bf0[17] = bf1[17];
    bf0[18] = half_btf_sse4_1(&cospim16, &bf1[18], &cospi48, &bf1[29], &rounding, bit);
    bf0[19] = half_btf_sse4_1(&cospim16, &bf1[19], &cospi48, &bf1[28], &rounding, bit);
    bf0[20] = half_btf_sse4_1(&cospim48, &bf1[20], &cospim16, &bf1[27], &rounding, bit);
    bf0[21] = half_btf_sse4_1(&cospim48, &bf1[21], &cospim16, &bf1[26], &rounding, bit);
    bf0[22] = bf1[22];
    bf0[23] = bf1[23];
    bf0[24] = bf1[24];
    bf0[25] = bf1[25];
    bf0[26] = half_btf_sse4_1(&cospim16, &bf1[21], &cospi48, &bf1[26], &rounding, bit);
    bf0[27] = half_btf_sse4_1(&cospim16, &bf1[20], &cospi48, &bf1[27], &rounding, bit);
    bf0[28] = half_btf_sse4_1(&cospi48, &bf1[19], &cospi16, &bf1[28], &rounding, bit);
    bf0[29] = half_btf_sse4_1(&cospi48, &bf1[18], &cospi16, &bf1[29], &rounding, bit);
    bf0[30] = bf1[30];
    bf0[31] = bf1[31];

    // stage 7
    addsub_sse4_1(bf0[0], bf0[7], bf1 + 0, bf1 + 7, &clamp_lo, &clamp_hi);
    addsub_sse4_1(bf0[1], bf0[6], bf1 + 1, bf1 + 6, &clamp_lo, &clamp_hi);
    addsub_sse4_1(bf0[2], bf0[5], bf1 + 2, bf1 + 5, &clamp_lo, &clamp_hi);
    addsub_sse4_1(bf0[3], bf0[4], bf1 + 3, bf1 + 4, &clamp_lo, &clamp_hi);
    bf1[8]  = bf0[8];
    bf1[9]  = bf0[9];
    bf1[10] = half_btf_sse4_1(&cospim32, &bf0[10], &cospi32, &bf0[13], &rounding, bit);
    bf1[11] = half_btf_sse4_1(&cospim32, &bf0[11], &cospi32, &bf0[12], &rounding, bit);
    bf1[12] = half_btf_sse4_1(&cospi32, &bf0[11], &cospi32, &bf0[12], &rounding, bit);
    bf1[13] = half_btf_sse4_1(&cospi32, &bf0[10], &cospi32, &bf0[13], &rounding, bit);
    bf1[14] = bf0[14];
    bf1[15] = bf0[15];
    addsub_sse4_1(bf0[16], bf0[23], bf1 + 16, bf1 + 23, &clamp_lo, &clamp_hi);
    addsub_sse4_1(bf0[17], bf0[22], bf1 + 17, bf1 + 22, &clamp_lo, &clamp_hi);
    addsub_sse4_1(bf0[18], bf0[21], bf1 + 18, bf1 + 21, &clamp_lo, &clamp_hi);
    addsub_sse4_1(bf0[19], bf0[20], bf1 + 19, bf1 + 20, &clamp_lo, &clamp_hi);
    addsub_sse4_1(bf0[31], bf0[24], bf1 + 31, bf1 + 24, &clamp_lo, &clamp_hi);
    addsub_sse4_1(bf0[30], bf0[25], bf1 + 30, bf1 + 25, &clamp_lo, &clamp_hi);
    addsub_sse4_1(bf0[29], bf0[26], bf1 + 29, bf1 + 26, &clamp_lo, &clamp_hi);
    addsub_sse4_1(bf0[28], bf0[27], bf1 + 28, bf1 + 27, &clamp_lo, &clamp_hi);

    // stage 8
    addsub_sse4_1(bf1[0], bf1[15], bf0 + 0, bf0 + 15, &clamp_lo, &clamp_hi);
    addsub_sse4_1(bf1[1], bf1[14], bf0 + 1, bf0 + 14, &clamp_lo, &clamp_hi);
    addsub_sse4_1(bf1[2], bf1[13], bf0 + 2, bf0 + 13, &clamp_lo, &clamp_hi);
    addsub_sse4_1(bf1[3], bf1[12], bf0 + 3, bf0 + 12, &clamp_lo, &clamp_hi);
    addsub_sse4_1(bf1[4], bf1[11], bf0 + 4, bf0 + 11, &clamp_lo, &clamp_hi);
    addsub_sse4_1(bf1[5], bf1[10], bf0 + 5, bf0 + 10, &clamp_lo, &clamp_hi);
    addsub_sse4_1(bf1[6], bf1[9], bf0 + 6, bf0 + 9, &clamp_lo, &clamp_hi);
    addsub_sse4_1(bf1[7], bf1[8], bf0 + 7, bf0 + 8, &clamp_lo, &clamp_hi);
    bf0[16] = bf1[16];
    bf0[17] = bf1[17];
    bf0[18] = bf1[18];
    bf0[19] = bf1[19];
    bf0[20] = half_btf_sse4_1(&cospim32, &bf1[20], &cospi32, &bf1[27], &rounding, bit);
    bf0[21] = half_btf_sse4_1(&cospim32, &bf1[21], &cospi32, &bf1[26], &rounding, bit);
    bf0[22] = half_btf_sse4_1(&cospim32, &bf1[22], &cospi32, &bf1[25], &rounding, bit);
    bf0[23] = half_btf_sse4_1(&cospim32, &bf1[23], &cospi32, &bf1[24], &rounding, bit);
    bf0[24] = half_btf_sse4_1(&cospi32, &bf1[23], &cospi32, &bf1[24], &rounding, bit);
    bf0[25] = half_btf_sse4_1(&cospi32, &bf1[22], &cospi32, &bf1[25], &rounding, bit);
    bf0[26] = half_btf_sse4_1(&cospi32, &bf1[21], &cospi32, &bf1[26], &rounding, bit);
    bf0[27] = half_btf_sse4_1(&cospi32, &bf1[20], &cospi32, &bf1[27], &rounding, bit);
    bf0[28] = bf1[28];
    bf0[29] = bf1[29];
    bf0[30] = bf1[30];
    bf0[31] = bf1[31];

    // stage 9
    addsub_sse4_1(bf0[0], bf0[31], out + 0, out + 31, &clamp_lo, &clamp_hi);
    addsub_sse4_1(bf0[1], bf0[30], out + 1, out + 30, &clamp_lo, &clamp_hi);
    addsub_sse4_1(bf0[2], bf0[29], out + 2, out + 29, &clamp_lo, &clamp_hi);
    addsub_sse4_1(bf0[3], bf0[28], out + 3, out + 28, &clamp_lo, &clamp_hi);
    addsub_sse4_1(bf0[4], bf0[27], out + 4, out + 27, &clamp_lo, &clamp_hi);
    addsub_sse4_1(bf0[5], bf0[26], out + 5, out + 26, &clamp_lo, &clamp_hi);
    addsub_sse4_1(bf0[6], bf0[25], out + 6, out + 25, &clamp_lo, &clamp_hi);
    addsub_sse4_1(bf0[7], bf0[24], out + 7, out + 24, &clamp_lo, &clamp_hi);
    addsub_sse4_1(bf0[8], bf0[23], out + 8, out + 23, &clamp_lo, &clamp_hi);
    addsub_sse4_1(bf0[9], bf0[22], out + 9, out + 22, &clamp_lo, &clamp_hi);
    addsub_sse4_1(bf0[10], bf0[21], out + 10, out + 21, &clamp_lo, &clamp_hi);
    addsub_sse4_1(bf0[11], bf0[20], out + 11, out + 20, &clamp_lo, &clamp_hi);
    addsub_sse4_1(bf0[12], bf0[19], out + 12, out + 19, &clamp_lo, &clamp_hi);
    addsub_sse4_1(bf0[13], bf0[18], out + 13, out + 18, &clamp_lo, &clamp_hi);
    addsub_sse4_1(bf0[14], bf0[17], out + 14, out + 17, &clamp_lo, &clamp_hi);
    addsub_sse4_1(bf0[15], bf0[16], out + 15, out + 16, &clamp_lo, &clamp_hi);

    if (!do_cols) {
        const int     log_range_out = AOMMAX(16, bd + 6);
        const __m128i clamp_lo_out  = _mm_set1_epi32(-(1 << (log_range_out - 1)));
        const __m128i clamp_hi_out  = _mm_set1_epi32((1 << (log_range_out - 1)) - 1);
        round_shift_8x8(out, out_shift);
        round_shift_8x8(out + 16, out_shift);
        highbd_clamp_epi32_sse4_1(out, out, &clamp_lo_out, &clamp_hi_out, 32);
    }
}

static void iidentity32_sse4_1(__m128i *in, __m128i *out, int bit, int do_cols, int bd,
                               int out_shift) {
    (void)bit;
    for (int i = 0; i < 32; i += 16) {
        out[i]      = _mm_slli_epi32(in[i], 2);
        out[i + 1]  = _mm_slli_epi32(in[i + 1], 2);
        out[i + 2]  = _mm_slli_epi32(in[i + 2], 2);
        out[i + 3]  = _mm_slli_epi32(in[i + 3], 2);
        out[i + 4]  = _mm_slli_epi32(in[i + 4], 2);
        out[i + 5]  = _mm_slli_epi32(in[i + 5], 2);
        out[i + 6]  = _mm_slli_epi32(in[i + 6], 2);
        out[i + 7]  = _mm_slli_epi32(in[i + 7], 2);
        out[i + 8]  = _mm_slli_epi32(in[i + 8], 2);
        out[i + 9]  = _mm_slli_epi32(in[i + 9], 2);
        out[i + 10] = _mm_slli_epi32(in[i + 10], 2);
        out[i + 11] = _mm_slli_epi32(in[i + 11], 2);
        out[i + 12] = _mm_slli_epi32(in[i + 12], 2);
        out[i + 13] = _mm_slli_epi32(in[i + 13], 2);
        out[i + 14] = _mm_slli_epi32(in[i + 14], 2);
        out[i + 15] = _mm_slli_epi32(in[i + 15], 2);
    }

    if (!do_cols) {
        const int     log_range_out = AOMMAX(16, bd + 6);
        const __m128i clamp_lo_out  = _mm_set1_epi32(-(1 << (log_range_out - 1)));
        const __m128i clamp_hi_out  = _mm_set1_epi32((1 << (log_range_out - 1)) - 1);
        round_shift_8x8(out, out_shift);
        round_shift_8x8(out + 16, out_shift);
        highbd_clamp_epi32_sse4_1(out, out, &clamp_lo_out, &clamp_hi_out, 32);
    }
}

static void idct64x64_low1_sse4_1(__m128i *in, __m128i *out, int bit, int do_cols, int bd,
                                  int out_shift) {
    const int32_t *cospi     = cospi_arr(bit);
    const __m128i  rnding    = _mm_set1_epi32(1 << (bit - 1));
    const int      log_range = AOMMAX(16, bd + (do_cols ? 6 : 8));
    __m128i        clamp_lo  = _mm_set1_epi32(-(1 << (log_range - 1)));
    __m128i        clamp_hi  = _mm_set1_epi32((1 << (log_range - 1)) - 1);

    const __m128i cospi32 = _mm_set1_epi32(cospi[32]);

    {
        __m128i x;

        // stage 1
        // stage 2
        // stage 3
        // stage 4
        // stage 5
        // stage 6
        x = half_btf_0_sse4_1(&cospi32, &in[0], &rnding, bit);

        // stage 8
        // stage 9
        // stage 10
        // stage 11
        if (!do_cols) {
            const int log_range_out = AOMMAX(16, bd + 6);
            clamp_lo                = _mm_set1_epi32(-(1 << (log_range_out - 1)));
            clamp_hi                = _mm_set1_epi32((1 << (log_range_out - 1)) - 1);
            if (out_shift != 0) {
                __m128i offset = _mm_set1_epi32((1 << out_shift) >> 1);
                x              = _mm_add_epi32(x, offset);
                x              = _mm_sra_epi32(x, _mm_cvtsi32_si128(out_shift));
            }
        }
        x       = _mm_max_epi32(x, clamp_lo);
        x       = _mm_min_epi32(x, clamp_hi);
        out[0]  = x;
        out[1]  = x;
        out[2]  = x;
        out[3]  = x;
        out[4]  = x;
        out[5]  = x;
        out[6]  = x;
        out[7]  = x;
        out[8]  = x;
        out[9]  = x;
        out[10] = x;
        out[11] = x;
        out[12] = x;
        out[13] = x;
        out[14] = x;
        out[15] = x;
        out[16] = x;
        out[17] = x;
        out[18] = x;
        out[19] = x;
        out[20] = x;
        out[21] = x;
        out[22] = x;
        out[23] = x;
        out[24] = x;
        out[25] = x;
        out[26] = x;
        out[27] = x;
        out[28] = x;
        out[29] = x;
        out[30] = x;
        out[31] = x;
        out[32] = x;
        out[33] = x;
        out[34] = x;
        out[35] = x;
        out[36] = x;
        out[37] = x;
        out[38] = x;
        out[39] = x;
        out[40] = x;
        out[41] = x;
        out[42] = x;
        out[43] = x;
        out[44] = x;
        out[45] = x;
        out[46] = x;
        out[47] = x;
        out[48] = x;
        out[49] = x;
        out[50] = x;
        out[51] = x;
        out[52] = x;
        out[53] = x;
        out[54] = x;
        out[55] = x;
        out[56] = x;
        out[57] = x;
        out[58] = x;
        out[59] = x;
        out[60] = x;
        out[61] = x;
        out[62] = x;
        out[63] = x;
    }
}

static void idct64x64_low8_sse4_1(__m128i *in, __m128i *out, int bit, int do_cols, int bd,
                                  int out_shift) {
    const int32_t *cospi     = cospi_arr(bit);
    const __m128i  rnding    = _mm_set1_epi32(1 << (bit - 1));
    const int      log_range = AOMMAX(16, bd + (do_cols ? 6 : 8));
    const __m128i  clamp_lo  = _mm_set1_epi32(-(1 << (log_range - 1)));
    const __m128i  clamp_hi  = _mm_set1_epi32((1 << (log_range - 1)) - 1);

    const __m128i cospi1   = _mm_set1_epi32(cospi[1]);
    const __m128i cospi2   = _mm_set1_epi32(cospi[2]);
    const __m128i cospi3   = _mm_set1_epi32(cospi[3]);
    const __m128i cospi4   = _mm_set1_epi32(cospi[4]);
    const __m128i cospi6   = _mm_set1_epi32(cospi[6]);
    const __m128i cospi8   = _mm_set1_epi32(cospi[8]);
    const __m128i cospi12  = _mm_set1_epi32(cospi[12]);
    const __m128i cospi16  = _mm_set1_epi32(cospi[16]);
    const __m128i cospi20  = _mm_set1_epi32(cospi[20]);
    const __m128i cospi24  = _mm_set1_epi32(cospi[24]);
    const __m128i cospi28  = _mm_set1_epi32(cospi[28]);
    const __m128i cospi32  = _mm_set1_epi32(cospi[32]);
    const __m128i cospi40  = _mm_set1_epi32(cospi[40]);
    const __m128i cospi44  = _mm_set1_epi32(cospi[44]);
    const __m128i cospi48  = _mm_set1_epi32(cospi[48]);
    const __m128i cospi56  = _mm_set1_epi32(cospi[56]);
    const __m128i cospi60  = _mm_set1_epi32(cospi[60]);
    const __m128i cospim4  = _mm_set1_epi32(-cospi[4]);
    const __m128i cospim8  = _mm_set1_epi32(-cospi[8]);
    const __m128i cospim12 = _mm_set1_epi32(-cospi[12]);
    const __m128i cospim16 = _mm_set1_epi32(-cospi[16]);
    const __m128i cospim20 = _mm_set1_epi32(-cospi[20]);
    const __m128i cospim24 = _mm_set1_epi32(-cospi[24]);
    const __m128i cospim28 = _mm_set1_epi32(-cospi[28]);
    const __m128i cospim32 = _mm_set1_epi32(-cospi[32]);
    const __m128i cospim36 = _mm_set1_epi32(-cospi[36]);
    const __m128i cospim40 = _mm_set1_epi32(-cospi[40]);
    const __m128i cospim48 = _mm_set1_epi32(-cospi[48]);
    const __m128i cospim52 = _mm_set1_epi32(-cospi[52]);
    const __m128i cospim56 = _mm_set1_epi32(-cospi[56]);
    const __m128i cospi63  = _mm_set1_epi32(cospi[63]);
    const __m128i cospim57 = _mm_set1_epi32(-cospi[57]);
    const __m128i cospi7   = _mm_set1_epi32(cospi[7]);
    const __m128i cospi5   = _mm_set1_epi32(cospi[5]);
    const __m128i cospi59  = _mm_set1_epi32(cospi[59]);
    const __m128i cospim61 = _mm_set1_epi32(-cospi[61]);
    const __m128i cospim58 = _mm_set1_epi32(-cospi[58]);
    const __m128i cospi62  = _mm_set1_epi32(cospi[62]);

    {
        __m128i u[64];

        // stage 1
        u[0]  = in[0];
        u[8]  = in[4];
        u[16] = in[2];
        u[24] = in[6];
        u[32] = in[1];
        u[40] = in[5];
        u[48] = in[3];
        u[56] = in[7];

        // stage 2
        u[63] = half_btf_0_sse4_1(&cospi1, &u[32], &rnding, bit);
        u[32] = half_btf_0_sse4_1(&cospi63, &u[32], &rnding, bit);
        u[39] = half_btf_0_sse4_1(&cospim57, &u[56], &rnding, bit);
        u[56] = half_btf_0_sse4_1(&cospi7, &u[56], &rnding, bit);
        u[55] = half_btf_0_sse4_1(&cospi5, &u[40], &rnding, bit);
        u[40] = half_btf_0_sse4_1(&cospi59, &u[40], &rnding, bit);
        u[47] = half_btf_0_sse4_1(&cospim61, &u[48], &rnding, bit);
        u[48] = half_btf_0_sse4_1(&cospi3, &u[48], &rnding, bit);

        // stage 3
        u[31] = half_btf_0_sse4_1(&cospi2, &u[16], &rnding, bit);
        u[16] = half_btf_0_sse4_1(&cospi62, &u[16], &rnding, bit);
        u[23] = half_btf_0_sse4_1(&cospim58, &u[24], &rnding, bit);
        u[24] = half_btf_0_sse4_1(&cospi6, &u[24], &rnding, bit);
        u[33] = u[32];
        u[38] = u[39];
        u[41] = u[40];
        u[46] = u[47];
        u[49] = u[48];
        u[54] = u[55];
        u[57] = u[56];
        u[62] = u[63];

        // stage 4
        __m128i temp1, temp2;
        u[15] = half_btf_0_sse4_1(&cospi4, &u[8], &rnding, bit);
        u[8]  = half_btf_0_sse4_1(&cospi60, &u[8], &rnding, bit);
        u[17] = u[16];
        u[22] = u[23];
        u[25] = u[24];
        u[30] = u[31];

        temp1 = half_btf_sse4_1(&cospim4, &u[33], &cospi60, &u[62], &rnding, bit);
        u[62] = half_btf_sse4_1(&cospi60, &u[33], &cospi4, &u[62], &rnding, bit);
        u[33] = temp1;

        temp2 = half_btf_sse4_1(&cospim36, &u[38], &cospi28, &u[57], &rnding, bit);
        u[38] = half_btf_sse4_1(&cospim28, &u[38], &cospim36, &u[57], &rnding, bit);
        u[57] = temp2;

        temp1 = half_btf_sse4_1(&cospim20, &u[41], &cospi44, &u[54], &rnding, bit);
        u[54] = half_btf_sse4_1(&cospi44, &u[41], &cospi20, &u[54], &rnding, bit);
        u[41] = temp1;

        temp2 = half_btf_sse4_1(&cospim12, &u[46], &cospim52, &u[49], &rnding, bit);
        u[49] = half_btf_sse4_1(&cospim52, &u[46], &cospi12, &u[49], &rnding, bit);
        u[46] = temp2;

        // stage 5
        u[9]  = u[8];
        u[14] = u[15];

        temp1 = half_btf_sse4_1(&cospim8, &u[17], &cospi56, &u[30], &rnding, bit);
        u[30] = half_btf_sse4_1(&cospi56, &u[17], &cospi8, &u[30], &rnding, bit);
        u[17] = temp1;

        temp2 = half_btf_sse4_1(&cospim24, &u[22], &cospim40, &u[25], &rnding, bit);
        u[25] = half_btf_sse4_1(&cospim40, &u[22], &cospi24, &u[25], &rnding, bit);
        u[22] = temp2;

        u[35] = u[32];
        u[34] = u[33];
        u[36] = u[39];
        u[37] = u[38];
        u[43] = u[40];
        u[42] = u[41];
        u[44] = u[47];
        u[45] = u[46];
        u[51] = u[48];
        u[50] = u[49];
        u[52] = u[55];
        u[53] = u[54];
        u[59] = u[56];
        u[58] = u[57];
        u[60] = u[63];
        u[61] = u[62];

        // stage 6
        temp1 = half_btf_0_sse4_1(&cospi32, &u[0], &rnding, bit);
        u[1]  = half_btf_0_sse4_1(&cospi32, &u[0], &rnding, bit);
        u[0]  = temp1;

        temp2 = half_btf_sse4_1(&cospim16, &u[9], &cospi48, &u[14], &rnding, bit);
        u[14] = half_btf_sse4_1(&cospi48, &u[9], &cospi16, &u[14], &rnding, bit);
        u[9]  = temp2;
        u[19] = u[16];
        u[18] = u[17];
        u[20] = u[23];
        u[21] = u[22];
        u[27] = u[24];
        u[26] = u[25];
        u[28] = u[31];
        u[29] = u[30];

        temp1 = half_btf_sse4_1(&cospim8, &u[34], &cospi56, &u[61], &rnding, bit);
        u[61] = half_btf_sse4_1(&cospi56, &u[34], &cospi8, &u[61], &rnding, bit);
        u[34] = temp1;
        temp2 = half_btf_sse4_1(&cospim8, &u[35], &cospi56, &u[60], &rnding, bit);
        u[60] = half_btf_sse4_1(&cospi56, &u[35], &cospi8, &u[60], &rnding, bit);
        u[35] = temp2;
        temp1 = half_btf_sse4_1(&cospim56, &u[36], &cospim8, &u[59], &rnding, bit);
        u[59] = half_btf_sse4_1(&cospim8, &u[36], &cospi56, &u[59], &rnding, bit);
        u[36] = temp1;
        temp2 = half_btf_sse4_1(&cospim56, &u[37], &cospim8, &u[58], &rnding, bit);
        u[58] = half_btf_sse4_1(&cospim8, &u[37], &cospi56, &u[58], &rnding, bit);
        u[37] = temp2;
        temp1 = half_btf_sse4_1(&cospim40, &u[42], &cospi24, &u[53], &rnding, bit);
        u[53] = half_btf_sse4_1(&cospi24, &u[42], &cospi40, &u[53], &rnding, bit);
        u[42] = temp1;
        temp2 = half_btf_sse4_1(&cospim40, &u[43], &cospi24, &u[52], &rnding, bit);
        u[52] = half_btf_sse4_1(&cospi24, &u[43], &cospi40, &u[52], &rnding, bit);
        u[43] = temp2;
        temp1 = half_btf_sse4_1(&cospim24, &u[44], &cospim40, &u[51], &rnding, bit);
        u[51] = half_btf_sse4_1(&cospim40, &u[44], &cospi24, &u[51], &rnding, bit);
        u[44] = temp1;
        temp2 = half_btf_sse4_1(&cospim24, &u[45], &cospim40, &u[50], &rnding, bit);
        u[50] = half_btf_sse4_1(&cospim40, &u[45], &cospi24, &u[50], &rnding, bit);
        u[45] = temp2;

        // stage 7
        u[3]  = u[0];
        u[2]  = u[1];
        u[11] = u[8];
        u[10] = u[9];
        u[12] = u[15];
        u[13] = u[14];

        temp1 = half_btf_sse4_1(&cospim16, &u[18], &cospi48, &u[29], &rnding, bit);
        u[29] = half_btf_sse4_1(&cospi48, &u[18], &cospi16, &u[29], &rnding, bit);
        u[18] = temp1;
        temp2 = half_btf_sse4_1(&cospim16, &u[19], &cospi48, &u[28], &rnding, bit);
        u[28] = half_btf_sse4_1(&cospi48, &u[19], &cospi16, &u[28], &rnding, bit);
        u[19] = temp2;
        temp1 = half_btf_sse4_1(&cospim48, &u[20], &cospim16, &u[27], &rnding, bit);
        u[27] = half_btf_sse4_1(&cospim16, &u[20], &cospi48, &u[27], &rnding, bit);
        u[20] = temp1;
        temp2 = half_btf_sse4_1(&cospim48, &u[21], &cospim16, &u[26], &rnding, bit);
        u[26] = half_btf_sse4_1(&cospim16, &u[21], &cospi48, &u[26], &rnding, bit);
        u[21] = temp2;
        for (int i = 32; i < 64; i += 16) {
            for (int j = i; j < i + 4; j++) {
                addsub_sse4_1(u[j], u[j ^ 7], &u[j], &u[j ^ 7], &clamp_lo, &clamp_hi);
                addsub_sse4_1(u[j ^ 15], u[j ^ 8], &u[j ^ 15], &u[j ^ 8], &clamp_lo, &clamp_hi);
            }
        }

        // stage 8
        u[7] = u[0];
        u[6] = u[1];
        u[5] = u[2];
        u[4] = u[3];

        idct64_stage8_sse4_1(u,
                             &cospim32,
                             &cospi32,
                             &cospim16,
                             &cospi48,
                             &cospi16,
                             &cospim48,
                             &clamp_lo,
                             &clamp_hi,
                             &rnding,
                             bit);

        // stage 9
        idct64_stage9_sse4_1(u, &cospim32, &cospi32, &clamp_lo, &clamp_hi, &rnding, bit);

        // stage 10
        idct64_stage10_sse4_1(u, &cospim32, &cospi32, &clamp_lo, &clamp_hi, &rnding, bit);

        // stage 11
        idct64_stage11_sse4_1(u, out, do_cols, bd, out_shift, &clamp_lo, &clamp_hi);
    }
}

static void idct64x64_low16_sse4_1(__m128i *in, __m128i *out, int bit, int do_cols, int bd,
                                   int out_shift) {
    const int32_t *cospi     = cospi_arr(bit);
    const __m128i  rnding    = _mm_set1_epi32(1 << (bit - 1));
    const int      log_range = AOMMAX(16, bd + (do_cols ? 6 : 8));
    const __m128i  clamp_lo  = _mm_set1_epi32(-(1 << (log_range - 1)));
    const __m128i  clamp_hi  = _mm_set1_epi32((1 << (log_range - 1)) - 1);

    const __m128i cospi1  = _mm_set1_epi32(cospi[1]);
    const __m128i cospi2  = _mm_set1_epi32(cospi[2]);
    const __m128i cospi3  = _mm_set1_epi32(cospi[3]);
    const __m128i cospi4  = _mm_set1_epi32(cospi[4]);
    const __m128i cospi5  = _mm_set1_epi32(cospi[5]);
    const __m128i cospi6  = _mm_set1_epi32(cospi[6]);
    const __m128i cospi7  = _mm_set1_epi32(cospi[7]);
    const __m128i cospi8  = _mm_set1_epi32(cospi[8]);
    const __m128i cospi9  = _mm_set1_epi32(cospi[9]);
    const __m128i cospi10 = _mm_set1_epi32(cospi[10]);
    const __m128i cospi11 = _mm_set1_epi32(cospi[11]);
    const __m128i cospi12 = _mm_set1_epi32(cospi[12]);
    const __m128i cospi13 = _mm_set1_epi32(cospi[13]);
    const __m128i cospi14 = _mm_set1_epi32(cospi[14]);
    const __m128i cospi15 = _mm_set1_epi32(cospi[15]);
    const __m128i cospi16 = _mm_set1_epi32(cospi[16]);
    const __m128i cospi20 = _mm_set1_epi32(cospi[20]);
    const __m128i cospi24 = _mm_set1_epi32(cospi[24]);
    const __m128i cospi28 = _mm_set1_epi32(cospi[28]);
    const __m128i cospi32 = _mm_set1_epi32(cospi[32]);
    const __m128i cospi36 = _mm_set1_epi32(cospi[36]);
    const __m128i cospi40 = _mm_set1_epi32(cospi[40]);
    const __m128i cospi44 = _mm_set1_epi32(cospi[44]);
    const __m128i cospi48 = _mm_set1_epi32(cospi[48]);
    const __m128i cospi51 = _mm_set1_epi32(cospi[51]);
    const __m128i cospi52 = _mm_set1_epi32(cospi[52]);
    const __m128i cospi54 = _mm_set1_epi32(cospi[54]);
    const __m128i cospi55 = _mm_set1_epi32(cospi[55]);
    const __m128i cospi56 = _mm_set1_epi32(cospi[56]);
    const __m128i cospi59 = _mm_set1_epi32(cospi[59]);
    const __m128i cospi60 = _mm_set1_epi32(cospi[60]);
    const __m128i cospi62 = _mm_set1_epi32(cospi[62]);
    const __m128i cospi63 = _mm_set1_epi32(cospi[63]);

    const __m128i cospim4  = _mm_set1_epi32(-cospi[4]);
    const __m128i cospim8  = _mm_set1_epi32(-cospi[8]);
    const __m128i cospim12 = _mm_set1_epi32(-cospi[12]);
    const __m128i cospim16 = _mm_set1_epi32(-cospi[16]);
    const __m128i cospim20 = _mm_set1_epi32(-cospi[20]);
    const __m128i cospim24 = _mm_set1_epi32(-cospi[24]);
    const __m128i cospim28 = _mm_set1_epi32(-cospi[28]);
    const __m128i cospim32 = _mm_set1_epi32(-cospi[32]);
    const __m128i cospim36 = _mm_set1_epi32(-cospi[36]);
    const __m128i cospim40 = _mm_set1_epi32(-cospi[40]);
    const __m128i cospim44 = _mm_set1_epi32(-cospi[44]);
    const __m128i cospim48 = _mm_set1_epi32(-cospi[48]);
    const __m128i cospim49 = _mm_set1_epi32(-cospi[49]);
    const __m128i cospim50 = _mm_set1_epi32(-cospi[50]);
    const __m128i cospim52 = _mm_set1_epi32(-cospi[52]);
    const __m128i cospim53 = _mm_set1_epi32(-cospi[53]);
    const __m128i cospim56 = _mm_set1_epi32(-cospi[56]);
    const __m128i cospim57 = _mm_set1_epi32(-cospi[57]);
    const __m128i cospim58 = _mm_set1_epi32(-cospi[58]);
    const __m128i cospim60 = _mm_set1_epi32(-cospi[60]);
    const __m128i cospim61 = _mm_set1_epi32(-cospi[61]);

    {
        __m128i u[64];
        __m128i tmp1, tmp2, tmp3, tmp4;
        // stage 1
        u[0]  = in[0];
        u[32] = in[1];
        u[36] = in[9];
        u[40] = in[5];
        u[44] = in[13];
        u[48] = in[3];
        u[52] = in[11];
        u[56] = in[7];
        u[60] = in[15];
        u[16] = in[2];
        u[20] = in[10];
        u[24] = in[6];
        u[28] = in[14];
        u[4]  = in[8];
        u[8]  = in[4];
        u[12] = in[12];

        // stage 2
        u[63] = half_btf_0_sse4_1(&cospi1, &u[32], &rnding, bit);
        u[32] = half_btf_0_sse4_1(&cospi63, &u[32], &rnding, bit);
        u[35] = half_btf_0_sse4_1(&cospim49, &u[60], &rnding, bit);
        u[60] = half_btf_0_sse4_1(&cospi15, &u[60], &rnding, bit);
        u[59] = half_btf_0_sse4_1(&cospi9, &u[36], &rnding, bit);
        u[36] = half_btf_0_sse4_1(&cospi55, &u[36], &rnding, bit);
        u[39] = half_btf_0_sse4_1(&cospim57, &u[56], &rnding, bit);
        u[56] = half_btf_0_sse4_1(&cospi7, &u[56], &rnding, bit);
        u[55] = half_btf_0_sse4_1(&cospi5, &u[40], &rnding, bit);
        u[40] = half_btf_0_sse4_1(&cospi59, &u[40], &rnding, bit);
        u[43] = half_btf_0_sse4_1(&cospim53, &u[52], &rnding, bit);
        u[52] = half_btf_0_sse4_1(&cospi11, &u[52], &rnding, bit);
        u[47] = half_btf_0_sse4_1(&cospim61, &u[48], &rnding, bit);
        u[48] = half_btf_0_sse4_1(&cospi3, &u[48], &rnding, bit);
        u[51] = half_btf_0_sse4_1(&cospi13, &u[44], &rnding, bit);
        u[44] = half_btf_0_sse4_1(&cospi51, &u[44], &rnding, bit);

        // stage 3
        u[31] = half_btf_0_sse4_1(&cospi2, &u[16], &rnding, bit);
        u[16] = half_btf_0_sse4_1(&cospi62, &u[16], &rnding, bit);
        u[19] = half_btf_0_sse4_1(&cospim50, &u[28], &rnding, bit);
        u[28] = half_btf_0_sse4_1(&cospi14, &u[28], &rnding, bit);
        u[27] = half_btf_0_sse4_1(&cospi10, &u[20], &rnding, bit);
        u[20] = half_btf_0_sse4_1(&cospi54, &u[20], &rnding, bit);
        u[23] = half_btf_0_sse4_1(&cospim58, &u[24], &rnding, bit);
        u[24] = half_btf_0_sse4_1(&cospi6, &u[24], &rnding, bit);
        u[33] = u[32];
        u[34] = u[35];
        u[37] = u[36];
        u[38] = u[39];
        u[41] = u[40];
        u[42] = u[43];
        u[45] = u[44];
        u[46] = u[47];
        u[49] = u[48];
        u[50] = u[51];
        u[53] = u[52];
        u[54] = u[55];
        u[57] = u[56];
        u[58] = u[59];
        u[61] = u[60];
        u[62] = u[63];

        // stage 4
        u[15] = half_btf_0_sse4_1(&cospi4, &u[8], &rnding, bit);
        u[8]  = half_btf_0_sse4_1(&cospi60, &u[8], &rnding, bit);
        u[11] = half_btf_0_sse4_1(&cospim52, &u[12], &rnding, bit);
        u[12] = half_btf_0_sse4_1(&cospi12, &u[12], &rnding, bit);

        u[17] = u[16];
        u[18] = u[19];
        u[21] = u[20];
        u[22] = u[23];
        u[25] = u[24];
        u[26] = u[27];
        u[29] = u[28];
        u[30] = u[31];

        tmp1  = half_btf_sse4_1(&cospim4, &u[33], &cospi60, &u[62], &rnding, bit);
        tmp2  = half_btf_sse4_1(&cospim60, &u[34], &cospim4, &u[61], &rnding, bit);
        tmp3  = half_btf_sse4_1(&cospim36, &u[37], &cospi28, &u[58], &rnding, bit);
        tmp4  = half_btf_sse4_1(&cospim28, &u[38], &cospim36, &u[57], &rnding, bit);
        u[57] = half_btf_sse4_1(&cospim36, &u[38], &cospi28, &u[57], &rnding, bit);
        u[58] = half_btf_sse4_1(&cospi28, &u[37], &cospi36, &u[58], &rnding, bit);
        u[61] = half_btf_sse4_1(&cospim4, &u[34], &cospi60, &u[61], &rnding, bit);
        u[62] = half_btf_sse4_1(&cospi60, &u[33], &cospi4, &u[62], &rnding, bit);
        u[33] = tmp1;
        u[34] = tmp2;
        u[37] = tmp3;
        u[38] = tmp4;

        tmp1  = half_btf_sse4_1(&cospim20, &u[41], &cospi44, &u[54], &rnding, bit);
        tmp2  = half_btf_sse4_1(&cospim44, &u[42], &cospim20, &u[53], &rnding, bit);
        tmp3  = half_btf_sse4_1(&cospim52, &u[45], &cospi12, &u[50], &rnding, bit);
        tmp4  = half_btf_sse4_1(&cospim12, &u[46], &cospim52, &u[49], &rnding, bit);
        u[49] = half_btf_sse4_1(&cospim52, &u[46], &cospi12, &u[49], &rnding, bit);
        u[50] = half_btf_sse4_1(&cospi12, &u[45], &cospi52, &u[50], &rnding, bit);
        u[53] = half_btf_sse4_1(&cospim20, &u[42], &cospi44, &u[53], &rnding, bit);
        u[54] = half_btf_sse4_1(&cospi44, &u[41], &cospi20, &u[54], &rnding, bit);
        u[41] = tmp1;
        u[42] = tmp2;
        u[45] = tmp3;
        u[46] = tmp4;

        // stage 5
        u[7] = half_btf_0_sse4_1(&cospi8, &u[4], &rnding, bit);
        u[4] = half_btf_0_sse4_1(&cospi56, &u[4], &rnding, bit);

        u[9]  = u[8];
        u[10] = u[11];
        u[13] = u[12];
        u[14] = u[15];

        tmp1  = half_btf_sse4_1(&cospim8, &u[17], &cospi56, &u[30], &rnding, bit);
        tmp2  = half_btf_sse4_1(&cospim56, &u[18], &cospim8, &u[29], &rnding, bit);
        tmp3  = half_btf_sse4_1(&cospim40, &u[21], &cospi24, &u[26], &rnding, bit);
        tmp4  = half_btf_sse4_1(&cospim24, &u[22], &cospim40, &u[25], &rnding, bit);
        u[25] = half_btf_sse4_1(&cospim40, &u[22], &cospi24, &u[25], &rnding, bit);
        u[26] = half_btf_sse4_1(&cospi24, &u[21], &cospi40, &u[26], &rnding, bit);
        u[29] = half_btf_sse4_1(&cospim8, &u[18], &cospi56, &u[29], &rnding, bit);
        u[30] = half_btf_sse4_1(&cospi56, &u[17], &cospi8, &u[30], &rnding, bit);
        u[17] = tmp1;
        u[18] = tmp2;
        u[21] = tmp3;
        u[22] = tmp4;

        for (int i = 32; i < 64; i += 8) {
            addsub_sse4_1(u[i + 0], u[i + 3], &u[i + 0], &u[i + 3], &clamp_lo, &clamp_hi);
            addsub_sse4_1(u[i + 1], u[i + 2], &u[i + 1], &u[i + 2], &clamp_lo, &clamp_hi);

            addsub_sse4_1(u[i + 7], u[i + 4], &u[i + 7], &u[i + 4], &clamp_lo, &clamp_hi);
            addsub_sse4_1(u[i + 6], u[i + 5], &u[i + 6], &u[i + 5], &clamp_lo, &clamp_hi);
        }

        // stage 6
        tmp1 = half_btf_0_sse4_1(&cospi32, &u[0], &rnding, bit);
        u[1] = half_btf_0_sse4_1(&cospi32, &u[0], &rnding, bit);
        u[0] = tmp1;
        u[5] = u[4];
        u[6] = u[7];

        tmp1  = half_btf_sse4_1(&cospim16, &u[9], &cospi48, &u[14], &rnding, bit);
        u[14] = half_btf_sse4_1(&cospi48, &u[9], &cospi16, &u[14], &rnding, bit);
        u[9]  = tmp1;
        tmp2  = half_btf_sse4_1(&cospim48, &u[10], &cospim16, &u[13], &rnding, bit);
        u[13] = half_btf_sse4_1(&cospim16, &u[10], &cospi48, &u[13], &rnding, bit);
        u[10] = tmp2;

        for (int i = 16; i < 32; i += 8) {
            addsub_sse4_1(u[i + 0], u[i + 3], &u[i + 0], &u[i + 3], &clamp_lo, &clamp_hi);
            addsub_sse4_1(u[i + 1], u[i + 2], &u[i + 1], &u[i + 2], &clamp_lo, &clamp_hi);

            addsub_sse4_1(u[i + 7], u[i + 4], &u[i + 7], &u[i + 4], &clamp_lo, &clamp_hi);
            addsub_sse4_1(u[i + 6], u[i + 5], &u[i + 6], &u[i + 5], &clamp_lo, &clamp_hi);
        }

        tmp1  = half_btf_sse4_1(&cospim8, &u[34], &cospi56, &u[61], &rnding, bit);
        tmp2  = half_btf_sse4_1(&cospim8, &u[35], &cospi56, &u[60], &rnding, bit);
        tmp3  = half_btf_sse4_1(&cospim56, &u[36], &cospim8, &u[59], &rnding, bit);
        tmp4  = half_btf_sse4_1(&cospim56, &u[37], &cospim8, &u[58], &rnding, bit);
        u[58] = half_btf_sse4_1(&cospim8, &u[37], &cospi56, &u[58], &rnding, bit);
        u[59] = half_btf_sse4_1(&cospim8, &u[36], &cospi56, &u[59], &rnding, bit);
        u[60] = half_btf_sse4_1(&cospi56, &u[35], &cospi8, &u[60], &rnding, bit);
        u[61] = half_btf_sse4_1(&cospi56, &u[34], &cospi8, &u[61], &rnding, bit);
        u[34] = tmp1;
        u[35] = tmp2;
        u[36] = tmp3;
        u[37] = tmp4;

        tmp1  = half_btf_sse4_1(&cospim40, &u[42], &cospi24, &u[53], &rnding, bit);
        tmp2  = half_btf_sse4_1(&cospim40, &u[43], &cospi24, &u[52], &rnding, bit);
        tmp3  = half_btf_sse4_1(&cospim24, &u[44], &cospim40, &u[51], &rnding, bit);
        tmp4  = half_btf_sse4_1(&cospim24, &u[45], &cospim40, &u[50], &rnding, bit);
        u[50] = half_btf_sse4_1(&cospim40, &u[45], &cospi24, &u[50], &rnding, bit);
        u[51] = half_btf_sse4_1(&cospim40, &u[44], &cospi24, &u[51], &rnding, bit);
        u[52] = half_btf_sse4_1(&cospi24, &u[43], &cospi40, &u[52], &rnding, bit);
        u[53] = half_btf_sse4_1(&cospi24, &u[42], &cospi40, &u[53], &rnding, bit);
        u[42] = tmp1;
        u[43] = tmp2;
        u[44] = tmp3;
        u[45] = tmp4;

        // stage 7
        u[3] = u[0];
        u[2] = u[1];
        tmp1 = half_btf_sse4_1(&cospim32, &u[5], &cospi32, &u[6], &rnding, bit);
        u[6] = half_btf_sse4_1(&cospi32, &u[5], &cospi32, &u[6], &rnding, bit);
        u[5] = tmp1;
        addsub_sse4_1(u[8], u[11], &u[8], &u[11], &clamp_lo, &clamp_hi);
        addsub_sse4_1(u[9], u[10], &u[9], &u[10], &clamp_lo, &clamp_hi);
        addsub_sse4_1(u[15], u[12], &u[15], &u[12], &clamp_lo, &clamp_hi);
        addsub_sse4_1(u[14], u[13], &u[14], &u[13], &clamp_lo, &clamp_hi);

        tmp1  = half_btf_sse4_1(&cospim16, &u[18], &cospi48, &u[29], &rnding, bit);
        tmp2  = half_btf_sse4_1(&cospim16, &u[19], &cospi48, &u[28], &rnding, bit);
        tmp3  = half_btf_sse4_1(&cospim48, &u[20], &cospim16, &u[27], &rnding, bit);
        tmp4  = half_btf_sse4_1(&cospim48, &u[21], &cospim16, &u[26], &rnding, bit);
        u[26] = half_btf_sse4_1(&cospim16, &u[21], &cospi48, &u[26], &rnding, bit);
        u[27] = half_btf_sse4_1(&cospim16, &u[20], &cospi48, &u[27], &rnding, bit);
        u[28] = half_btf_sse4_1(&cospi48, &u[19], &cospi16, &u[28], &rnding, bit);
        u[29] = half_btf_sse4_1(&cospi48, &u[18], &cospi16, &u[29], &rnding, bit);
        u[18] = tmp1;
        u[19] = tmp2;
        u[20] = tmp3;
        u[21] = tmp4;

        for (int i = 32; i < 64; i += 16) {
            for (int j = i; j < i + 4; j++) {
                addsub_sse4_1(u[j], u[j ^ 7], &u[j], &u[j ^ 7], &clamp_lo, &clamp_hi);
                addsub_sse4_1(u[j ^ 15], u[j ^ 8], &u[j ^ 15], &u[j ^ 8], &clamp_lo, &clamp_hi);
            }
        }

        // stage 8
        for (int i = 0; i < 4; ++i) {
            addsub_sse4_1(u[i], u[7 - i], &u[i], &u[7 - i], &clamp_lo, &clamp_hi);
        }

        idct64_stage8_sse4_1(u,
                             &cospim32,
                             &cospi32,
                             &cospim16,
                             &cospi48,
                             &cospi16,
                             &cospim48,
                             &clamp_lo,
                             &clamp_hi,
                             &rnding,
                             bit);

        // stage 9
        idct64_stage9_sse4_1(u, &cospim32, &cospi32, &clamp_lo, &clamp_hi, &rnding, bit);

        // stage 10
        idct64_stage10_sse4_1(u, &cospim32, &cospi32, &clamp_lo, &clamp_hi, &rnding, bit);

        // stage 11
        idct64_stage11_sse4_1(u, out, do_cols, bd, out_shift, &clamp_lo, &clamp_hi);
    }
}

static void idct64x64_general_sse4_1(__m128i *in, __m128i *out, int bit, int do_cols, int bd,
                                     int out_shift) {
    const int32_t *cospi     = cospi_arr(bit);
    const __m128i  rnding    = _mm_set1_epi32(1 << (bit - 1));
    const int      log_range = AOMMAX(16, bd + (do_cols ? 6 : 8));
    const __m128i  clamp_lo  = _mm_set1_epi32(-(1 << (log_range - 1)));
    const __m128i  clamp_hi  = _mm_set1_epi32((1 << (log_range - 1)) - 1);

    const __m128i cospi1  = _mm_set1_epi32(cospi[1]);
    const __m128i cospi2  = _mm_set1_epi32(cospi[2]);
    const __m128i cospi3  = _mm_set1_epi32(cospi[3]);
    const __m128i cospi4  = _mm_set1_epi32(cospi[4]);
    const __m128i cospi5  = _mm_set1_epi32(cospi[5]);
    const __m128i cospi6  = _mm_set1_epi32(cospi[6]);
    const __m128i cospi7  = _mm_set1_epi32(cospi[7]);
    const __m128i cospi8  = _mm_set1_epi32(cospi[8]);
    const __m128i cospi9  = _mm_set1_epi32(cospi[9]);
    const __m128i cospi10 = _mm_set1_epi32(cospi[10]);
    const __m128i cospi11 = _mm_set1_epi32(cospi[11]);
    const __m128i cospi12 = _mm_set1_epi32(cospi[12]);
    const __m128i cospi13 = _mm_set1_epi32(cospi[13]);
    const __m128i cospi14 = _mm_set1_epi32(cospi[14]);
    const __m128i cospi15 = _mm_set1_epi32(cospi[15]);
    const __m128i cospi16 = _mm_set1_epi32(cospi[16]);
    const __m128i cospi17 = _mm_set1_epi32(cospi[17]);
    const __m128i cospi18 = _mm_set1_epi32(cospi[18]);
    const __m128i cospi19 = _mm_set1_epi32(cospi[19]);
    const __m128i cospi20 = _mm_set1_epi32(cospi[20]);
    const __m128i cospi21 = _mm_set1_epi32(cospi[21]);
    const __m128i cospi22 = _mm_set1_epi32(cospi[22]);
    const __m128i cospi23 = _mm_set1_epi32(cospi[23]);
    const __m128i cospi24 = _mm_set1_epi32(cospi[24]);
    const __m128i cospi25 = _mm_set1_epi32(cospi[25]);
    const __m128i cospi26 = _mm_set1_epi32(cospi[26]);
    const __m128i cospi27 = _mm_set1_epi32(cospi[27]);
    const __m128i cospi28 = _mm_set1_epi32(cospi[28]);
    const __m128i cospi29 = _mm_set1_epi32(cospi[29]);
    const __m128i cospi30 = _mm_set1_epi32(cospi[30]);
    const __m128i cospi31 = _mm_set1_epi32(cospi[31]);
    const __m128i cospi32 = _mm_set1_epi32(cospi[32]);
    const __m128i cospi35 = _mm_set1_epi32(cospi[35]);
    const __m128i cospi36 = _mm_set1_epi32(cospi[36]);
    const __m128i cospi38 = _mm_set1_epi32(cospi[38]);
    const __m128i cospi39 = _mm_set1_epi32(cospi[39]);
    const __m128i cospi40 = _mm_set1_epi32(cospi[40]);
    const __m128i cospi43 = _mm_set1_epi32(cospi[43]);
    const __m128i cospi44 = _mm_set1_epi32(cospi[44]);
    const __m128i cospi46 = _mm_set1_epi32(cospi[46]);
    const __m128i cospi47 = _mm_set1_epi32(cospi[47]);
    const __m128i cospi48 = _mm_set1_epi32(cospi[48]);
    const __m128i cospi51 = _mm_set1_epi32(cospi[51]);
    const __m128i cospi52 = _mm_set1_epi32(cospi[52]);
    const __m128i cospi54 = _mm_set1_epi32(cospi[54]);
    const __m128i cospi55 = _mm_set1_epi32(cospi[55]);
    const __m128i cospi56 = _mm_set1_epi32(cospi[56]);
    const __m128i cospi59 = _mm_set1_epi32(cospi[59]);
    const __m128i cospi60 = _mm_set1_epi32(cospi[60]);
    const __m128i cospi62 = _mm_set1_epi32(cospi[62]);
    const __m128i cospi63 = _mm_set1_epi32(cospi[63]);

    const __m128i cospim4  = _mm_set1_epi32(-cospi[4]);
    const __m128i cospim8  = _mm_set1_epi32(-cospi[8]);
    const __m128i cospim12 = _mm_set1_epi32(-cospi[12]);
    const __m128i cospim16 = _mm_set1_epi32(-cospi[16]);
    const __m128i cospim20 = _mm_set1_epi32(-cospi[20]);
    const __m128i cospim24 = _mm_set1_epi32(-cospi[24]);
    const __m128i cospim28 = _mm_set1_epi32(-cospi[28]);
    const __m128i cospim32 = _mm_set1_epi32(-cospi[32]);
    const __m128i cospim33 = _mm_set1_epi32(-cospi[33]);
    const __m128i cospim34 = _mm_set1_epi32(-cospi[34]);
    const __m128i cospim36 = _mm_set1_epi32(-cospi[36]);
    const __m128i cospim37 = _mm_set1_epi32(-cospi[37]);
    const __m128i cospim40 = _mm_set1_epi32(-cospi[40]);
    const __m128i cospim41 = _mm_set1_epi32(-cospi[41]);
    const __m128i cospim42 = _mm_set1_epi32(-cospi[42]);
    const __m128i cospim44 = _mm_set1_epi32(-cospi[44]);
    const __m128i cospim45 = _mm_set1_epi32(-cospi[45]);
    const __m128i cospim48 = _mm_set1_epi32(-cospi[48]);
    const __m128i cospim49 = _mm_set1_epi32(-cospi[49]);
    const __m128i cospim50 = _mm_set1_epi32(-cospi[50]);
    const __m128i cospim52 = _mm_set1_epi32(-cospi[52]);
    const __m128i cospim53 = _mm_set1_epi32(-cospi[53]);
    const __m128i cospim56 = _mm_set1_epi32(-cospi[56]);
    const __m128i cospim57 = _mm_set1_epi32(-cospi[57]);
    const __m128i cospim58 = _mm_set1_epi32(-cospi[58]);
    const __m128i cospim60 = _mm_set1_epi32(-cospi[60]);
    const __m128i cospim61 = _mm_set1_epi32(-cospi[61]);

    {
        __m128i u[64], v[64];

        // stage 1
        u[32] = in[1];
        u[34] = in[17];
        u[36] = in[9];
        u[38] = in[25];
        u[40] = in[5];
        u[42] = in[21];
        u[44] = in[13];
        u[46] = in[29];
        u[48] = in[3];
        u[50] = in[19];
        u[52] = in[11];
        u[54] = in[27];
        u[56] = in[7];
        u[58] = in[23];
        u[60] = in[15];
        u[62] = in[31];

        v[16] = in[2];
        v[18] = in[18];
        v[20] = in[10];
        v[22] = in[26];
        v[24] = in[6];
        v[26] = in[22];
        v[28] = in[14];
        v[30] = in[30];

        u[8]  = in[4];
        u[10] = in[20];
        u[12] = in[12];
        u[14] = in[28];

        v[4] = in[8];
        v[6] = in[24];

        u[0] = in[0];
        u[2] = in[16];

        // stage 2
        v[32] = half_btf_0_sse4_1(&cospi63, &u[32], &rnding, bit);
        v[33] = half_btf_0_sse4_1(&cospim33, &u[62], &rnding, bit);
        v[34] = half_btf_0_sse4_1(&cospi47, &u[34], &rnding, bit);
        v[35] = half_btf_0_sse4_1(&cospim49, &u[60], &rnding, bit);
        v[36] = half_btf_0_sse4_1(&cospi55, &u[36], &rnding, bit);
        v[37] = half_btf_0_sse4_1(&cospim41, &u[58], &rnding, bit);
        v[38] = half_btf_0_sse4_1(&cospi39, &u[38], &rnding, bit);
        v[39] = half_btf_0_sse4_1(&cospim57, &u[56], &rnding, bit);
        v[40] = half_btf_0_sse4_1(&cospi59, &u[40], &rnding, bit);
        v[41] = half_btf_0_sse4_1(&cospim37, &u[54], &rnding, bit);
        v[42] = half_btf_0_sse4_1(&cospi43, &u[42], &rnding, bit);
        v[43] = half_btf_0_sse4_1(&cospim53, &u[52], &rnding, bit);
        v[44] = half_btf_0_sse4_1(&cospi51, &u[44], &rnding, bit);
        v[45] = half_btf_0_sse4_1(&cospim45, &u[50], &rnding, bit);
        v[46] = half_btf_0_sse4_1(&cospi35, &u[46], &rnding, bit);
        v[47] = half_btf_0_sse4_1(&cospim61, &u[48], &rnding, bit);
        v[48] = half_btf_0_sse4_1(&cospi3, &u[48], &rnding, bit);
        v[49] = half_btf_0_sse4_1(&cospi29, &u[46], &rnding, bit);
        v[50] = half_btf_0_sse4_1(&cospi19, &u[50], &rnding, bit);
        v[51] = half_btf_0_sse4_1(&cospi13, &u[44], &rnding, bit);
        v[52] = half_btf_0_sse4_1(&cospi11, &u[52], &rnding, bit);
        v[53] = half_btf_0_sse4_1(&cospi21, &u[42], &rnding, bit);
        v[54] = half_btf_0_sse4_1(&cospi27, &u[54], &rnding, bit);
        v[55] = half_btf_0_sse4_1(&cospi5, &u[40], &rnding, bit);
        v[56] = half_btf_0_sse4_1(&cospi7, &u[56], &rnding, bit);
        v[57] = half_btf_0_sse4_1(&cospi25, &u[38], &rnding, bit);
        v[58] = half_btf_0_sse4_1(&cospi23, &u[58], &rnding, bit);
        v[59] = half_btf_0_sse4_1(&cospi9, &u[36], &rnding, bit);
        v[60] = half_btf_0_sse4_1(&cospi15, &u[60], &rnding, bit);
        v[61] = half_btf_0_sse4_1(&cospi17, &u[34], &rnding, bit);
        v[62] = half_btf_0_sse4_1(&cospi31, &u[62], &rnding, bit);
        v[63] = half_btf_0_sse4_1(&cospi1, &u[32], &rnding, bit);

        // stage 3
        u[16] = half_btf_0_sse4_1(&cospi62, &v[16], &rnding, bit);
        u[17] = half_btf_0_sse4_1(&cospim34, &v[30], &rnding, bit);
        u[18] = half_btf_0_sse4_1(&cospi46, &v[18], &rnding, bit);
        u[19] = half_btf_0_sse4_1(&cospim50, &v[28], &rnding, bit);
        u[20] = half_btf_0_sse4_1(&cospi54, &v[20], &rnding, bit);
        u[21] = half_btf_0_sse4_1(&cospim42, &v[26], &rnding, bit);
        u[22] = half_btf_0_sse4_1(&cospi38, &v[22], &rnding, bit);
        u[23] = half_btf_0_sse4_1(&cospim58, &v[24], &rnding, bit);
        u[24] = half_btf_0_sse4_1(&cospi6, &v[24], &rnding, bit);
        u[25] = half_btf_0_sse4_1(&cospi26, &v[22], &rnding, bit);
        u[26] = half_btf_0_sse4_1(&cospi22, &v[26], &rnding, bit);
        u[27] = half_btf_0_sse4_1(&cospi10, &v[20], &rnding, bit);
        u[28] = half_btf_0_sse4_1(&cospi14, &v[28], &rnding, bit);
        u[29] = half_btf_0_sse4_1(&cospi18, &v[18], &rnding, bit);
        u[30] = half_btf_0_sse4_1(&cospi30, &v[30], &rnding, bit);
        u[31] = half_btf_0_sse4_1(&cospi2, &v[16], &rnding, bit);

        for (int i = 32; i < 64; i += 4) {
            addsub_sse4_1(v[i + 0], v[i + 1], &u[i + 0], &u[i + 1], &clamp_lo, &clamp_hi);
            addsub_sse4_1(v[i + 3], v[i + 2], &u[i + 3], &u[i + 2], &clamp_lo, &clamp_hi);
        }

        // stage 4
        v[8]  = half_btf_0_sse4_1(&cospi60, &u[8], &rnding, bit);
        v[9]  = half_btf_0_sse4_1(&cospim36, &u[14], &rnding, bit);
        v[10] = half_btf_0_sse4_1(&cospi44, &u[10], &rnding, bit);
        v[11] = half_btf_0_sse4_1(&cospim52, &u[12], &rnding, bit);
        v[12] = half_btf_0_sse4_1(&cospi12, &u[12], &rnding, bit);
        v[13] = half_btf_0_sse4_1(&cospi20, &u[10], &rnding, bit);
        v[14] = half_btf_0_sse4_1(&cospi28, &u[14], &rnding, bit);
        v[15] = half_btf_0_sse4_1(&cospi4, &u[8], &rnding, bit);

        for (int i = 16; i < 32; i += 4) {
            addsub_sse4_1(u[i + 0], u[i + 1], &v[i + 0], &v[i + 1], &clamp_lo, &clamp_hi);
            addsub_sse4_1(u[i + 3], u[i + 2], &v[i + 3], &v[i + 2], &clamp_lo, &clamp_hi);
        }

        for (int i = 32; i < 64; i += 4) {
            v[i + 0] = u[i + 0];
            v[i + 3] = u[i + 3];
        }

        v[33] = half_btf_sse4_1(&cospim4, &u[33], &cospi60, &u[62], &rnding, bit);
        v[34] = half_btf_sse4_1(&cospim60, &u[34], &cospim4, &u[61], &rnding, bit);
        v[37] = half_btf_sse4_1(&cospim36, &u[37], &cospi28, &u[58], &rnding, bit);
        v[38] = half_btf_sse4_1(&cospim28, &u[38], &cospim36, &u[57], &rnding, bit);
        v[41] = half_btf_sse4_1(&cospim20, &u[41], &cospi44, &u[54], &rnding, bit);
        v[42] = half_btf_sse4_1(&cospim44, &u[42], &cospim20, &u[53], &rnding, bit);
        v[45] = half_btf_sse4_1(&cospim52, &u[45], &cospi12, &u[50], &rnding, bit);
        v[46] = half_btf_sse4_1(&cospim12, &u[46], &cospim52, &u[49], &rnding, bit);
        v[49] = half_btf_sse4_1(&cospim52, &u[46], &cospi12, &u[49], &rnding, bit);
        v[50] = half_btf_sse4_1(&cospi12, &u[45], &cospi52, &u[50], &rnding, bit);
        v[53] = half_btf_sse4_1(&cospim20, &u[42], &cospi44, &u[53], &rnding, bit);
        v[54] = half_btf_sse4_1(&cospi44, &u[41], &cospi20, &u[54], &rnding, bit);
        v[57] = half_btf_sse4_1(&cospim36, &u[38], &cospi28, &u[57], &rnding, bit);
        v[58] = half_btf_sse4_1(&cospi28, &u[37], &cospi36, &u[58], &rnding, bit);
        v[61] = half_btf_sse4_1(&cospim4, &u[34], &cospi60, &u[61], &rnding, bit);
        v[62] = half_btf_sse4_1(&cospi60, &u[33], &cospi4, &u[62], &rnding, bit);

        // stage 5
        u[4] = half_btf_0_sse4_1(&cospi56, &v[4], &rnding, bit);
        u[5] = half_btf_0_sse4_1(&cospim40, &v[6], &rnding, bit);
        u[6] = half_btf_0_sse4_1(&cospi24, &v[6], &rnding, bit);
        u[7] = half_btf_0_sse4_1(&cospi8, &v[4], &rnding, bit);

        for (int i = 8; i < 16; i += 4) {
            addsub_sse4_1(v[i + 0], v[i + 1], &u[i + 0], &u[i + 1], &clamp_lo, &clamp_hi);
            addsub_sse4_1(v[i + 3], v[i + 2], &u[i + 3], &u[i + 2], &clamp_lo, &clamp_hi);
        }

        for (int i = 16; i < 32; i += 4) {
            u[i + 0] = v[i + 0];
            u[i + 3] = v[i + 3];
        }

        u[17] = half_btf_sse4_1(&cospim8, &v[17], &cospi56, &v[30], &rnding, bit);
        u[18] = half_btf_sse4_1(&cospim56, &v[18], &cospim8, &v[29], &rnding, bit);
        u[21] = half_btf_sse4_1(&cospim40, &v[21], &cospi24, &v[26], &rnding, bit);
        u[22] = half_btf_sse4_1(&cospim24, &v[22], &cospim40, &v[25], &rnding, bit);
        u[25] = half_btf_sse4_1(&cospim40, &v[22], &cospi24, &v[25], &rnding, bit);
        u[26] = half_btf_sse4_1(&cospi24, &v[21], &cospi40, &v[26], &rnding, bit);
        u[29] = half_btf_sse4_1(&cospim8, &v[18], &cospi56, &v[29], &rnding, bit);
        u[30] = half_btf_sse4_1(&cospi56, &v[17], &cospi8, &v[30], &rnding, bit);

        for (int i = 32; i < 64; i += 8) {
            addsub_sse4_1(v[i + 0], v[i + 3], &u[i + 0], &u[i + 3], &clamp_lo, &clamp_hi);
            addsub_sse4_1(v[i + 1], v[i + 2], &u[i + 1], &u[i + 2], &clamp_lo, &clamp_hi);

            addsub_sse4_1(v[i + 7], v[i + 4], &u[i + 7], &u[i + 4], &clamp_lo, &clamp_hi);
            addsub_sse4_1(v[i + 6], v[i + 5], &u[i + 6], &u[i + 5], &clamp_lo, &clamp_hi);
        }

        // stage 6
        v[0] = half_btf_0_sse4_1(&cospi32, &u[0], &rnding, bit);
        v[1] = half_btf_0_sse4_1(&cospi32, &u[0], &rnding, bit);
        v[2] = half_btf_0_sse4_1(&cospi48, &u[2], &rnding, bit);
        v[3] = half_btf_0_sse4_1(&cospi16, &u[2], &rnding, bit);

        addsub_sse4_1(u[4], u[5], &v[4], &v[5], &clamp_lo, &clamp_hi);
        addsub_sse4_1(u[7], u[6], &v[7], &v[6], &clamp_lo, &clamp_hi);

        for (int i = 8; i < 16; i += 4) {
            v[i + 0] = u[i + 0];
            v[i + 3] = u[i + 3];
        }

        v[9]  = half_btf_sse4_1(&cospim16, &u[9], &cospi48, &u[14], &rnding, bit);
        v[10] = half_btf_sse4_1(&cospim48, &u[10], &cospim16, &u[13], &rnding, bit);
        v[13] = half_btf_sse4_1(&cospim16, &u[10], &cospi48, &u[13], &rnding, bit);
        v[14] = half_btf_sse4_1(&cospi48, &u[9], &cospi16, &u[14], &rnding, bit);

        for (int i = 16; i < 32; i += 8) {
            addsub_sse4_1(u[i + 0], u[i + 3], &v[i + 0], &v[i + 3], &clamp_lo, &clamp_hi);
            addsub_sse4_1(u[i + 1], u[i + 2], &v[i + 1], &v[i + 2], &clamp_lo, &clamp_hi);

            addsub_sse4_1(u[i + 7], u[i + 4], &v[i + 7], &v[i + 4], &clamp_lo, &clamp_hi);
            addsub_sse4_1(u[i + 6], u[i + 5], &v[i + 6], &v[i + 5], &clamp_lo, &clamp_hi);
        }

        for (int i = 32; i < 64; i += 8) {
            v[i + 0] = u[i + 0];
            v[i + 1] = u[i + 1];
            v[i + 6] = u[i + 6];
            v[i + 7] = u[i + 7];
        }

        v[34] = half_btf_sse4_1(&cospim8, &u[34], &cospi56, &u[61], &rnding, bit);
        v[35] = half_btf_sse4_1(&cospim8, &u[35], &cospi56, &u[60], &rnding, bit);
        v[36] = half_btf_sse4_1(&cospim56, &u[36], &cospim8, &u[59], &rnding, bit);
        v[37] = half_btf_sse4_1(&cospim56, &u[37], &cospim8, &u[58], &rnding, bit);
        v[42] = half_btf_sse4_1(&cospim40, &u[42], &cospi24, &u[53], &rnding, bit);
        v[43] = half_btf_sse4_1(&cospim40, &u[43], &cospi24, &u[52], &rnding, bit);
        v[44] = half_btf_sse4_1(&cospim24, &u[44], &cospim40, &u[51], &rnding, bit);
        v[45] = half_btf_sse4_1(&cospim24, &u[45], &cospim40, &u[50], &rnding, bit);
        v[50] = half_btf_sse4_1(&cospim40, &u[45], &cospi24, &u[50], &rnding, bit);
        v[51] = half_btf_sse4_1(&cospim40, &u[44], &cospi24, &u[51], &rnding, bit);
        v[52] = half_btf_sse4_1(&cospi24, &u[43], &cospi40, &u[52], &rnding, bit);
        v[53] = half_btf_sse4_1(&cospi24, &u[42], &cospi40, &u[53], &rnding, bit);
        v[58] = half_btf_sse4_1(&cospim8, &u[37], &cospi56, &u[58], &rnding, bit);
        v[59] = half_btf_sse4_1(&cospim8, &u[36], &cospi56, &u[59], &rnding, bit);
        v[60] = half_btf_sse4_1(&cospi56, &u[35], &cospi8, &u[60], &rnding, bit);
        v[61] = half_btf_sse4_1(&cospi56, &u[34], &cospi8, &u[61], &rnding, bit);

        // stage 7
        addsub_sse4_1(v[0], v[3], &u[0], &u[3], &clamp_lo, &clamp_hi);
        addsub_sse4_1(v[1], v[2], &u[1], &u[2], &clamp_lo, &clamp_hi);

        u[4] = v[4];
        u[7] = v[7];
        u[5] = half_btf_sse4_1(&cospim32, &v[5], &cospi32, &v[6], &rnding, bit);
        u[6] = half_btf_sse4_1(&cospi32, &v[5], &cospi32, &v[6], &rnding, bit);

        addsub_sse4_1(v[8], v[11], &u[8], &u[11], &clamp_lo, &clamp_hi);
        addsub_sse4_1(v[9], v[10], &u[9], &u[10], &clamp_lo, &clamp_hi);
        addsub_sse4_1(v[15], v[12], &u[15], &u[12], &clamp_lo, &clamp_hi);
        addsub_sse4_1(v[14], v[13], &u[14], &u[13], &clamp_lo, &clamp_hi);

        for (int i = 16; i < 32; i += 8) {
            u[i + 0] = v[i + 0];
            u[i + 1] = v[i + 1];
            u[i + 6] = v[i + 6];
            u[i + 7] = v[i + 7];
        }

        u[18] = half_btf_sse4_1(&cospim16, &v[18], &cospi48, &v[29], &rnding, bit);
        u[19] = half_btf_sse4_1(&cospim16, &v[19], &cospi48, &v[28], &rnding, bit);
        u[20] = half_btf_sse4_1(&cospim48, &v[20], &cospim16, &v[27], &rnding, bit);
        u[21] = half_btf_sse4_1(&cospim48, &v[21], &cospim16, &v[26], &rnding, bit);
        u[26] = half_btf_sse4_1(&cospim16, &v[21], &cospi48, &v[26], &rnding, bit);
        u[27] = half_btf_sse4_1(&cospim16, &v[20], &cospi48, &v[27], &rnding, bit);
        u[28] = half_btf_sse4_1(&cospi48, &v[19], &cospi16, &v[28], &rnding, bit);
        u[29] = half_btf_sse4_1(&cospi48, &v[18], &cospi16, &v[29], &rnding, bit);

        for (int i = 32; i < 64; i += 16) {
            for (int j = i; j < i + 4; j++) {
                addsub_sse4_1(v[j], v[j ^ 7], &u[j], &u[j ^ 7], &clamp_lo, &clamp_hi);
                addsub_sse4_1(v[j ^ 15], v[j ^ 8], &u[j ^ 15], &u[j ^ 8], &clamp_lo, &clamp_hi);
            }
        }

        // stage 8
        for (int i = 0; i < 4; ++i) {
            addsub_sse4_1(u[i], u[7 - i], &v[i], &v[7 - i], &clamp_lo, &clamp_hi);
        }

        v[8]  = u[8];
        v[9]  = u[9];
        v[14] = u[14];
        v[15] = u[15];

        v[10] = half_btf_sse4_1(&cospim32, &u[10], &cospi32, &u[13], &rnding, bit);
        v[11] = half_btf_sse4_1(&cospim32, &u[11], &cospi32, &u[12], &rnding, bit);
        v[12] = half_btf_sse4_1(&cospi32, &u[11], &cospi32, &u[12], &rnding, bit);
        v[13] = half_btf_sse4_1(&cospi32, &u[10], &cospi32, &u[13], &rnding, bit);

        for (int i = 16; i < 20; ++i) {
            addsub_sse4_1(u[i], u[i ^ 7], &v[i], &v[i ^ 7], &clamp_lo, &clamp_hi);
            addsub_sse4_1(u[i ^ 15], u[i ^ 8], &v[i ^ 15], &v[i ^ 8], &clamp_lo, &clamp_hi);
        }

        for (int i = 32; i < 36; ++i) {
            v[i]      = u[i];
            v[i + 12] = u[i + 12];
            v[i + 16] = u[i + 16];
            v[i + 28] = u[i + 28];
        }

        v[36] = half_btf_sse4_1(&cospim16, &u[36], &cospi48, &u[59], &rnding, bit);
        v[37] = half_btf_sse4_1(&cospim16, &u[37], &cospi48, &u[58], &rnding, bit);
        v[38] = half_btf_sse4_1(&cospim16, &u[38], &cospi48, &u[57], &rnding, bit);
        v[39] = half_btf_sse4_1(&cospim16, &u[39], &cospi48, &u[56], &rnding, bit);
        v[40] = half_btf_sse4_1(&cospim48, &u[40], &cospim16, &u[55], &rnding, bit);
        v[41] = half_btf_sse4_1(&cospim48, &u[41], &cospim16, &u[54], &rnding, bit);
        v[42] = half_btf_sse4_1(&cospim48, &u[42], &cospim16, &u[53], &rnding, bit);
        v[43] = half_btf_sse4_1(&cospim48, &u[43], &cospim16, &u[52], &rnding, bit);
        v[52] = half_btf_sse4_1(&cospim16, &u[43], &cospi48, &u[52], &rnding, bit);
        v[53] = half_btf_sse4_1(&cospim16, &u[42], &cospi48, &u[53], &rnding, bit);
        v[54] = half_btf_sse4_1(&cospim16, &u[41], &cospi48, &u[54], &rnding, bit);
        v[55] = half_btf_sse4_1(&cospim16, &u[40], &cospi48, &u[55], &rnding, bit);
        v[56] = half_btf_sse4_1(&cospi48, &u[39], &cospi16, &u[56], &rnding, bit);
        v[57] = half_btf_sse4_1(&cospi48, &u[38], &cospi16, &u[57], &rnding, bit);
        v[58] = half_btf_sse4_1(&cospi48, &u[37], &cospi16, &u[58], &rnding, bit);
        v[59] = half_btf_sse4_1(&cospi48, &u[36], &cospi16, &u[59], &rnding, bit);

        // stage 9
        for (int i = 0; i < 8; ++i) {
            addsub_sse4_1(v[i], v[15 - i], &u[i], &u[15 - i], &clamp_lo, &clamp_hi);
        }

        for (int i = 16; i < 20; ++i) {
            u[i]      = v[i];
            u[i + 12] = v[i + 12];
        }

        u[20] = half_btf_sse4_1(&cospim32, &v[20], &cospi32, &v[27], &rnding, bit);
        u[21] = half_btf_sse4_1(&cospim32, &v[21], &cospi32, &v[26], &rnding, bit);
        u[22] = half_btf_sse4_1(&cospim32, &v[22], &cospi32, &v[25], &rnding, bit);
        u[23] = half_btf_sse4_1(&cospim32, &v[23], &cospi32, &v[24], &rnding, bit);
        u[24] = half_btf_sse4_1(&cospi32, &v[23], &cospi32, &v[24], &rnding, bit);
        u[25] = half_btf_sse4_1(&cospi32, &v[22], &cospi32, &v[25], &rnding, bit);
        u[26] = half_btf_sse4_1(&cospi32, &v[21], &cospi32, &v[26], &rnding, bit);
        u[27] = half_btf_sse4_1(&cospi32, &v[20], &cospi32, &v[27], &rnding, bit);

        for (int i = 32; i < 40; i++) {
            addsub_sse4_1(v[i], v[i ^ 15], &u[i], &u[i ^ 15], &clamp_lo, &clamp_hi);
        }

        for (int i = 48; i < 56; i++) {
            addsub_sse4_1(v[i ^ 15], v[i], &u[i ^ 15], &u[i], &clamp_lo, &clamp_hi);
        }

        // stage 10
        for (int i = 0; i < 16; i++) {
            addsub_sse4_1(u[i], u[31 - i], &v[i], &v[31 - i], &clamp_lo, &clamp_hi);
        }

        for (int i = 32; i < 40; i++) v[i] = u[i];

        v[40] = half_btf_sse4_1(&cospim32, &u[40], &cospi32, &u[55], &rnding, bit);
        v[41] = half_btf_sse4_1(&cospim32, &u[41], &cospi32, &u[54], &rnding, bit);
        v[42] = half_btf_sse4_1(&cospim32, &u[42], &cospi32, &u[53], &rnding, bit);
        v[43] = half_btf_sse4_1(&cospim32, &u[43], &cospi32, &u[52], &rnding, bit);
        v[44] = half_btf_sse4_1(&cospim32, &u[44], &cospi32, &u[51], &rnding, bit);
        v[45] = half_btf_sse4_1(&cospim32, &u[45], &cospi32, &u[50], &rnding, bit);
        v[46] = half_btf_sse4_1(&cospim32, &u[46], &cospi32, &u[49], &rnding, bit);
        v[47] = half_btf_sse4_1(&cospim32, &u[47], &cospi32, &u[48], &rnding, bit);
        v[48] = half_btf_sse4_1(&cospi32, &u[47], &cospi32, &u[48], &rnding, bit);
        v[49] = half_btf_sse4_1(&cospi32, &u[46], &cospi32, &u[49], &rnding, bit);
        v[50] = half_btf_sse4_1(&cospi32, &u[45], &cospi32, &u[50], &rnding, bit);
        v[51] = half_btf_sse4_1(&cospi32, &u[44], &cospi32, &u[51], &rnding, bit);
        v[52] = half_btf_sse4_1(&cospi32, &u[43], &cospi32, &u[52], &rnding, bit);
        v[53] = half_btf_sse4_1(&cospi32, &u[42], &cospi32, &u[53], &rnding, bit);
        v[54] = half_btf_sse4_1(&cospi32, &u[41], &cospi32, &u[54], &rnding, bit);
        v[55] = half_btf_sse4_1(&cospi32, &u[40], &cospi32, &u[55], &rnding, bit);

        for (int i = 56; i < 64; i++) v[i] = u[i];

        // stage 11
        for (int i = 0; i < 32; i++) {
            addsub_sse4_1(v[i], v[63 - i], &out[(i)], &out[(63 - i)], &clamp_lo, &clamp_hi);
        }

        if (!do_cols) {
            const int     log_range_out = AOMMAX(16, bd + 6);
            const __m128i clamp_lo_out  = _mm_set1_epi32(-(1 << (log_range_out - 1)));
            const __m128i clamp_hi_out  = _mm_set1_epi32((1 << (log_range_out - 1)) - 1);
            for (int i = 0; i < 64; i += 4) {
                round_shift_4x4(out + i, out_shift);
                highbd_clamp_epi32_sse4_1(out + i, out + i, &clamp_lo_out, &clamp_hi_out, 4);
            }
        }
    }
}

static void idct32_sse4_1(__m128i *in, __m128i *out, int32_t bit) {
    const int32_t *cospi    = cospi_arr(bit);
    const __m128i  cospi62  = _mm_set1_epi32(cospi[62]);
    const __m128i  cospi30  = _mm_set1_epi32(cospi[30]);
    const __m128i  cospi46  = _mm_set1_epi32(cospi[46]);
    const __m128i  cospi14  = _mm_set1_epi32(cospi[14]);
    const __m128i  cospi54  = _mm_set1_epi32(cospi[54]);
    const __m128i  cospi22  = _mm_set1_epi32(cospi[22]);
    const __m128i  cospi38  = _mm_set1_epi32(cospi[38]);
    const __m128i  cospi6   = _mm_set1_epi32(cospi[6]);
    const __m128i  cospi58  = _mm_set1_epi32(cospi[58]);
    const __m128i  cospi26  = _mm_set1_epi32(cospi[26]);
    const __m128i  cospi42  = _mm_set1_epi32(cospi[42]);
    const __m128i  cospi10  = _mm_set1_epi32(cospi[10]);
    const __m128i  cospi50  = _mm_set1_epi32(cospi[50]);
    const __m128i  cospi18  = _mm_set1_epi32(cospi[18]);
    const __m128i  cospi34  = _mm_set1_epi32(cospi[34]);
    const __m128i  cospi2   = _mm_set1_epi32(cospi[2]);
    const __m128i  cospim58 = _mm_set1_epi32(-cospi[58]);
    const __m128i  cospim26 = _mm_set1_epi32(-cospi[26]);
    const __m128i  cospim42 = _mm_set1_epi32(-cospi[42]);
    const __m128i  cospim10 = _mm_set1_epi32(-cospi[10]);
    const __m128i  cospim50 = _mm_set1_epi32(-cospi[50]);
    const __m128i  cospim18 = _mm_set1_epi32(-cospi[18]);
    const __m128i  cospim34 = _mm_set1_epi32(-cospi[34]);
    const __m128i  cospim2  = _mm_set1_epi32(-cospi[2]);
    const __m128i  cospi60  = _mm_set1_epi32(cospi[60]);
    const __m128i  cospi28  = _mm_set1_epi32(cospi[28]);
    const __m128i  cospi44  = _mm_set1_epi32(cospi[44]);
    const __m128i  cospi12  = _mm_set1_epi32(cospi[12]);
    const __m128i  cospi52  = _mm_set1_epi32(cospi[52]);
    const __m128i  cospi20  = _mm_set1_epi32(cospi[20]);
    const __m128i  cospi36  = _mm_set1_epi32(cospi[36]);
    const __m128i  cospi4   = _mm_set1_epi32(cospi[4]);
    const __m128i  cospim52 = _mm_set1_epi32(-cospi[52]);
    const __m128i  cospim20 = _mm_set1_epi32(-cospi[20]);
    const __m128i  cospim36 = _mm_set1_epi32(-cospi[36]);
    const __m128i  cospim4  = _mm_set1_epi32(-cospi[4]);
    const __m128i  cospi56  = _mm_set1_epi32(cospi[56]);
    const __m128i  cospi24  = _mm_set1_epi32(cospi[24]);
    const __m128i  cospi40  = _mm_set1_epi32(cospi[40]);
    const __m128i  cospi8   = _mm_set1_epi32(cospi[8]);
    const __m128i  cospim40 = _mm_set1_epi32(-cospi[40]);
    const __m128i  cospim8  = _mm_set1_epi32(-cospi[8]);
    const __m128i  cospim56 = _mm_set1_epi32(-cospi[56]);
    const __m128i  cospim24 = _mm_set1_epi32(-cospi[24]);
    const __m128i  cospi32  = _mm_set1_epi32(cospi[32]);
    const __m128i  cospim32 = _mm_set1_epi32(-cospi[32]);
    const __m128i  cospi48  = _mm_set1_epi32(cospi[48]);
    const __m128i  cospim48 = _mm_set1_epi32(-cospi[48]);
    const __m128i  cospi16  = _mm_set1_epi32(cospi[16]);
    const __m128i  cospim16 = _mm_set1_epi32(-cospi[16]);
    const __m128i  rounding = _mm_set1_epi32(1 << (bit - 1));
    __m128i        bf1[32], bf0[32];
    int32_t        col;

    for (col = 0; col < 8; ++col) {
        // stage 0
        // stage 1
        bf1[0]  = in[0 * 8 + col];
        bf1[1]  = in[16 * 8 + col];
        bf1[2]  = in[8 * 8 + col];
        bf1[3]  = in[24 * 8 + col];
        bf1[4]  = in[4 * 8 + col];
        bf1[5]  = in[20 * 8 + col];
        bf1[6]  = in[12 * 8 + col];
        bf1[7]  = in[28 * 8 + col];
        bf1[8]  = in[2 * 8 + col];
        bf1[9]  = in[18 * 8 + col];
        bf1[10] = in[10 * 8 + col];
        bf1[11] = in[26 * 8 + col];
        bf1[12] = in[6 * 8 + col];
        bf1[13] = in[22 * 8 + col];
        bf1[14] = in[14 * 8 + col];
        bf1[15] = in[30 * 8 + col];
        bf1[16] = in[1 * 8 + col];
        bf1[17] = in[17 * 8 + col];
        bf1[18] = in[9 * 8 + col];
        bf1[19] = in[25 * 8 + col];
        bf1[20] = in[5 * 8 + col];
        bf1[21] = in[21 * 8 + col];
        bf1[22] = in[13 * 8 + col];
        bf1[23] = in[29 * 8 + col];
        bf1[24] = in[3 * 8 + col];
        bf1[25] = in[19 * 8 + col];
        bf1[26] = in[11 * 8 + col];
        bf1[27] = in[27 * 8 + col];
        bf1[28] = in[7 * 8 + col];
        bf1[29] = in[23 * 8 + col];
        bf1[30] = in[15 * 8 + col];
        bf1[31] = in[31 * 8 + col];

        // stage 2
        bf0[0]  = bf1[0];
        bf0[1]  = bf1[1];
        bf0[2]  = bf1[2];
        bf0[3]  = bf1[3];
        bf0[4]  = bf1[4];
        bf0[5]  = bf1[5];
        bf0[6]  = bf1[6];
        bf0[7]  = bf1[7];
        bf0[8]  = bf1[8];
        bf0[9]  = bf1[9];
        bf0[10] = bf1[10];
        bf0[11] = bf1[11];
        bf0[12] = bf1[12];
        bf0[13] = bf1[13];
        bf0[14] = bf1[14];
        bf0[15] = bf1[15];
        bf0[16] = half_btf_sse4_1(&cospi62, &bf1[16], &cospim2, &bf1[31], &rounding, bit);
        bf0[17] = half_btf_sse4_1(&cospi30, &bf1[17], &cospim34, &bf1[30], &rounding, bit);
        bf0[18] = half_btf_sse4_1(&cospi46, &bf1[18], &cospim18, &bf1[29], &rounding, bit);
        bf0[19] = half_btf_sse4_1(&cospi14, &bf1[19], &cospim50, &bf1[28], &rounding, bit);
        bf0[20] = half_btf_sse4_1(&cospi54, &bf1[20], &cospim10, &bf1[27], &rounding, bit);
        bf0[21] = half_btf_sse4_1(&cospi22, &bf1[21], &cospim42, &bf1[26], &rounding, bit);
        bf0[22] = half_btf_sse4_1(&cospi38, &bf1[22], &cospim26, &bf1[25], &rounding, bit);
        bf0[23] = half_btf_sse4_1(&cospi6, &bf1[23], &cospim58, &bf1[24], &rounding, bit);
        bf0[24] = half_btf_sse4_1(&cospi58, &bf1[23], &cospi6, &bf1[24], &rounding, bit);
        bf0[25] = half_btf_sse4_1(&cospi26, &bf1[22], &cospi38, &bf1[25], &rounding, bit);
        bf0[26] = half_btf_sse4_1(&cospi42, &bf1[21], &cospi22, &bf1[26], &rounding, bit);
        bf0[27] = half_btf_sse4_1(&cospi10, &bf1[20], &cospi54, &bf1[27], &rounding, bit);
        bf0[28] = half_btf_sse4_1(&cospi50, &bf1[19], &cospi14, &bf1[28], &rounding, bit);
        bf0[29] = half_btf_sse4_1(&cospi18, &bf1[18], &cospi46, &bf1[29], &rounding, bit);
        bf0[30] = half_btf_sse4_1(&cospi34, &bf1[17], &cospi30, &bf1[30], &rounding, bit);
        bf0[31] = half_btf_sse4_1(&cospi2, &bf1[16], &cospi62, &bf1[31], &rounding, bit);

        // stage 3
        bf1[0]  = bf0[0];
        bf1[1]  = bf0[1];
        bf1[2]  = bf0[2];
        bf1[3]  = bf0[3];
        bf1[4]  = bf0[4];
        bf1[5]  = bf0[5];
        bf1[6]  = bf0[6];
        bf1[7]  = bf0[7];
        bf1[8]  = half_btf_sse4_1(&cospi60, &bf0[8], &cospim4, &bf0[15], &rounding, bit);
        bf1[9]  = half_btf_sse4_1(&cospi28, &bf0[9], &cospim36, &bf0[14], &rounding, bit);
        bf1[10] = half_btf_sse4_1(&cospi44, &bf0[10], &cospim20, &bf0[13], &rounding, bit);
        bf1[11] = half_btf_sse4_1(&cospi12, &bf0[11], &cospim52, &bf0[12], &rounding, bit);
        bf1[12] = half_btf_sse4_1(&cospi52, &bf0[11], &cospi12, &bf0[12], &rounding, bit);
        bf1[13] = half_btf_sse4_1(&cospi20, &bf0[10], &cospi44, &bf0[13], &rounding, bit);
        bf1[14] = half_btf_sse4_1(&cospi36, &bf0[9], &cospi28, &bf0[14], &rounding, bit);
        bf1[15] = half_btf_sse4_1(&cospi4, &bf0[8], &cospi60, &bf0[15], &rounding, bit);
        bf1[16] = _mm_add_epi32(bf0[16], bf0[17]);
        bf1[17] = _mm_sub_epi32(bf0[16], bf0[17]);
        bf1[18] = _mm_sub_epi32(bf0[19], bf0[18]);
        bf1[19] = _mm_add_epi32(bf0[18], bf0[19]);
        bf1[20] = _mm_add_epi32(bf0[20], bf0[21]);
        bf1[21] = _mm_sub_epi32(bf0[20], bf0[21]);
        bf1[22] = _mm_sub_epi32(bf0[23], bf0[22]);
        bf1[23] = _mm_add_epi32(bf0[22], bf0[23]);
        bf1[24] = _mm_add_epi32(bf0[24], bf0[25]);
        bf1[25] = _mm_sub_epi32(bf0[24], bf0[25]);
        bf1[26] = _mm_sub_epi32(bf0[27], bf0[26]);
        bf1[27] = _mm_add_epi32(bf0[26], bf0[27]);
        bf1[28] = _mm_add_epi32(bf0[28], bf0[29]);
        bf1[29] = _mm_sub_epi32(bf0[28], bf0[29]);
        bf1[30] = _mm_sub_epi32(bf0[31], bf0[30]);
        bf1[31] = _mm_add_epi32(bf0[30], bf0[31]);

        // stage 4
        bf0[0]  = bf1[0];
        bf0[1]  = bf1[1];
        bf0[2]  = bf1[2];
        bf0[3]  = bf1[3];
        bf0[4]  = half_btf_sse4_1(&cospi56, &bf1[4], &cospim8, &bf1[7], &rounding, bit);
        bf0[5]  = half_btf_sse4_1(&cospi24, &bf1[5], &cospim40, &bf1[6], &rounding, bit);
        bf0[6]  = half_btf_sse4_1(&cospi40, &bf1[5], &cospi24, &bf1[6], &rounding, bit);
        bf0[7]  = half_btf_sse4_1(&cospi8, &bf1[4], &cospi56, &bf1[7], &rounding, bit);
        bf0[8]  = _mm_add_epi32(bf1[8], bf1[9]);
        bf0[9]  = _mm_sub_epi32(bf1[8], bf1[9]);
        bf0[10] = _mm_sub_epi32(bf1[11], bf1[10]);
        bf0[11] = _mm_add_epi32(bf1[10], bf1[11]);
        bf0[12] = _mm_add_epi32(bf1[12], bf1[13]);
        bf0[13] = _mm_sub_epi32(bf1[12], bf1[13]);
        bf0[14] = _mm_sub_epi32(bf1[15], bf1[14]);
        bf0[15] = _mm_add_epi32(bf1[14], bf1[15]);
        bf0[16] = bf1[16];
        bf0[17] = half_btf_sse4_1(&cospim8, &bf1[17], &cospi56, &bf1[30], &rounding, bit);
        bf0[18] = half_btf_sse4_1(&cospim56, &bf1[18], &cospim8, &bf1[29], &rounding, bit);
        bf0[19] = bf1[19];
        bf0[20] = bf1[20];
        bf0[21] = half_btf_sse4_1(&cospim40, &bf1[21], &cospi24, &bf1[26], &rounding, bit);
        bf0[22] = half_btf_sse4_1(&cospim24, &bf1[22], &cospim40, &bf1[25], &rounding, bit);
        bf0[23] = bf1[23];
        bf0[24] = bf1[24];
        bf0[25] = half_btf_sse4_1(&cospim40, &bf1[22], &cospi24, &bf1[25], &rounding, bit);
        bf0[26] = half_btf_sse4_1(&cospi24, &bf1[21], &cospi40, &bf1[26], &rounding, bit);
        bf0[27] = bf1[27];
        bf0[28] = bf1[28];
        bf0[29] = half_btf_sse4_1(&cospim8, &bf1[18], &cospi56, &bf1[29], &rounding, bit);
        bf0[30] = half_btf_sse4_1(&cospi56, &bf1[17], &cospi8, &bf1[30], &rounding, bit);
        bf0[31] = bf1[31];

        // stage 5
        bf1[0]  = half_btf_sse4_1(&cospi32, &bf0[0], &cospi32, &bf0[1], &rounding, bit);
        bf1[1]  = half_btf_sse4_1(&cospi32, &bf0[0], &cospim32, &bf0[1], &rounding, bit);
        bf1[2]  = half_btf_sse4_1(&cospi48, &bf0[2], &cospim16, &bf0[3], &rounding, bit);
        bf1[3]  = half_btf_sse4_1(&cospi16, &bf0[2], &cospi48, &bf0[3], &rounding, bit);
        bf1[4]  = _mm_add_epi32(bf0[4], bf0[5]);
        bf1[5]  = _mm_sub_epi32(bf0[4], bf0[5]);
        bf1[6]  = _mm_sub_epi32(bf0[7], bf0[6]);
        bf1[7]  = _mm_add_epi32(bf0[6], bf0[7]);
        bf1[8]  = bf0[8];
        bf1[9]  = half_btf_sse4_1(&cospim16, &bf0[9], &cospi48, &bf0[14], &rounding, bit);
        bf1[10] = half_btf_sse4_1(&cospim48, &bf0[10], &cospim16, &bf0[13], &rounding, bit);
        bf1[11] = bf0[11];
        bf1[12] = bf0[12];
        bf1[13] = half_btf_sse4_1(&cospim16, &bf0[10], &cospi48, &bf0[13], &rounding, bit);
        bf1[14] = half_btf_sse4_1(&cospi48, &bf0[9], &cospi16, &bf0[14], &rounding, bit);
        bf1[15] = bf0[15];
        bf1[16] = _mm_add_epi32(bf0[16], bf0[19]);
        bf1[17] = _mm_add_epi32(bf0[17], bf0[18]);
        bf1[18] = _mm_sub_epi32(bf0[17], bf0[18]);
        bf1[19] = _mm_sub_epi32(bf0[16], bf0[19]);
        bf1[20] = _mm_sub_epi32(bf0[23], bf0[20]);
        bf1[21] = _mm_sub_epi32(bf0[22], bf0[21]);
        bf1[22] = _mm_add_epi32(bf0[21], bf0[22]);
        bf1[23] = _mm_add_epi32(bf0[20], bf0[23]);
        bf1[24] = _mm_add_epi32(bf0[24], bf0[27]);
        bf1[25] = _mm_add_epi32(bf0[25], bf0[26]);
        bf1[26] = _mm_sub_epi32(bf0[25], bf0[26]);
        bf1[27] = _mm_sub_epi32(bf0[24], bf0[27]);
        bf1[28] = _mm_sub_epi32(bf0[31], bf0[28]);
        bf1[29] = _mm_sub_epi32(bf0[30], bf0[29]);
        bf1[30] = _mm_add_epi32(bf0[29], bf0[30]);
        bf1[31] = _mm_add_epi32(bf0[28], bf0[31]);

        // stage 6
        bf0[0]  = _mm_add_epi32(bf1[0], bf1[3]);
        bf0[1]  = _mm_add_epi32(bf1[1], bf1[2]);
        bf0[2]  = _mm_sub_epi32(bf1[1], bf1[2]);
        bf0[3]  = _mm_sub_epi32(bf1[0], bf1[3]);
        bf0[4]  = bf1[4];
        bf0[5]  = half_btf_sse4_1(&cospim32, &bf1[5], &cospi32, &bf1[6], &rounding, bit);
        bf0[6]  = half_btf_sse4_1(&cospi32, &bf1[5], &cospi32, &bf1[6], &rounding, bit);
        bf0[7]  = bf1[7];
        bf0[8]  = _mm_add_epi32(bf1[8], bf1[11]);
        bf0[9]  = _mm_add_epi32(bf1[9], bf1[10]);
        bf0[10] = _mm_sub_epi32(bf1[9], bf1[10]);
        bf0[11] = _mm_sub_epi32(bf1[8], bf1[11]);
        bf0[12] = _mm_sub_epi32(bf1[15], bf1[12]);
        bf0[13] = _mm_sub_epi32(bf1[14], bf1[13]);
        bf0[14] = _mm_add_epi32(bf1[13], bf1[14]);
        bf0[15] = _mm_add_epi32(bf1[12], bf1[15]);
        bf0[16] = bf1[16];
        bf0[17] = bf1[17];
        bf0[18] = half_btf_sse4_1(&cospim16, &bf1[18], &cospi48, &bf1[29], &rounding, bit);
        bf0[19] = half_btf_sse4_1(&cospim16, &bf1[19], &cospi48, &bf1[28], &rounding, bit);
        bf0[20] = half_btf_sse4_1(&cospim48, &bf1[20], &cospim16, &bf1[27], &rounding, bit);
        bf0[21] = half_btf_sse4_1(&cospim48, &bf1[21], &cospim16, &bf1[26], &rounding, bit);
        bf0[22] = bf1[22];
        bf0[23] = bf1[23];
        bf0[24] = bf1[24];
        bf0[25] = bf1[25];
        bf0[26] = half_btf_sse4_1(&cospim16, &bf1[21], &cospi48, &bf1[26], &rounding, bit);
        bf0[27] = half_btf_sse4_1(&cospim16, &bf1[20], &cospi48, &bf1[27], &rounding, bit);
        bf0[28] = half_btf_sse4_1(&cospi48, &bf1[19], &cospi16, &bf1[28], &rounding, bit);
        bf0[29] = half_btf_sse4_1(&cospi48, &bf1[18], &cospi16, &bf1[29], &rounding, bit);
        bf0[30] = bf1[30];
        bf0[31] = bf1[31];

        // stage 7
        bf1[0]  = _mm_add_epi32(bf0[0], bf0[7]);
        bf1[1]  = _mm_add_epi32(bf0[1], bf0[6]);
        bf1[2]  = _mm_add_epi32(bf0[2], bf0[5]);
        bf1[3]  = _mm_add_epi32(bf0[3], bf0[4]);
        bf1[4]  = _mm_sub_epi32(bf0[3], bf0[4]);
        bf1[5]  = _mm_sub_epi32(bf0[2], bf0[5]);
        bf1[6]  = _mm_sub_epi32(bf0[1], bf0[6]);
        bf1[7]  = _mm_sub_epi32(bf0[0], bf0[7]);
        bf1[8]  = bf0[8];
        bf1[9]  = bf0[9];
        bf1[10] = half_btf_sse4_1(&cospim32, &bf0[10], &cospi32, &bf0[13], &rounding, bit);
        bf1[11] = half_btf_sse4_1(&cospim32, &bf0[11], &cospi32, &bf0[12], &rounding, bit);
        bf1[12] = half_btf_sse4_1(&cospi32, &bf0[11], &cospi32, &bf0[12], &rounding, bit);
        bf1[13] = half_btf_sse4_1(&cospi32, &bf0[10], &cospi32, &bf0[13], &rounding, bit);
        bf1[14] = bf0[14];
        bf1[15] = bf0[15];
        bf1[16] = _mm_add_epi32(bf0[16], bf0[23]);
        bf1[17] = _mm_add_epi32(bf0[17], bf0[22]);
        bf1[18] = _mm_add_epi32(bf0[18], bf0[21]);
        bf1[19] = _mm_add_epi32(bf0[19], bf0[20]);
        bf1[20] = _mm_sub_epi32(bf0[19], bf0[20]);
        bf1[21] = _mm_sub_epi32(bf0[18], bf0[21]);
        bf1[22] = _mm_sub_epi32(bf0[17], bf0[22]);
        bf1[23] = _mm_sub_epi32(bf0[16], bf0[23]);
        bf1[24] = _mm_sub_epi32(bf0[31], bf0[24]);
        bf1[25] = _mm_sub_epi32(bf0[30], bf0[25]);
        bf1[26] = _mm_sub_epi32(bf0[29], bf0[26]);
        bf1[27] = _mm_sub_epi32(bf0[28], bf0[27]);
        bf1[28] = _mm_add_epi32(bf0[27], bf0[28]);
        bf1[29] = _mm_add_epi32(bf0[26], bf0[29]);
        bf1[30] = _mm_add_epi32(bf0[25], bf0[30]);
        bf1[31] = _mm_add_epi32(bf0[24], bf0[31]);

        // stage 8
        bf0[0]  = _mm_add_epi32(bf1[0], bf1[15]);
        bf0[1]  = _mm_add_epi32(bf1[1], bf1[14]);
        bf0[2]  = _mm_add_epi32(bf1[2], bf1[13]);
        bf0[3]  = _mm_add_epi32(bf1[3], bf1[12]);
        bf0[4]  = _mm_add_epi32(bf1[4], bf1[11]);
        bf0[5]  = _mm_add_epi32(bf1[5], bf1[10]);
        bf0[6]  = _mm_add_epi32(bf1[6], bf1[9]);
        bf0[7]  = _mm_add_epi32(bf1[7], bf1[8]);
        bf0[8]  = _mm_sub_epi32(bf1[7], bf1[8]);
        bf0[9]  = _mm_sub_epi32(bf1[6], bf1[9]);
        bf0[10] = _mm_sub_epi32(bf1[5], bf1[10]);
        bf0[11] = _mm_sub_epi32(bf1[4], bf1[11]);
        bf0[12] = _mm_sub_epi32(bf1[3], bf1[12]);
        bf0[13] = _mm_sub_epi32(bf1[2], bf1[13]);
        bf0[14] = _mm_sub_epi32(bf1[1], bf1[14]);
        bf0[15] = _mm_sub_epi32(bf1[0], bf1[15]);
        bf0[16] = bf1[16];
        bf0[17] = bf1[17];
        bf0[18] = bf1[18];
        bf0[19] = bf1[19];
        bf0[20] = half_btf_sse4_1(&cospim32, &bf1[20], &cospi32, &bf1[27], &rounding, bit);
        bf0[21] = half_btf_sse4_1(&cospim32, &bf1[21], &cospi32, &bf1[26], &rounding, bit);
        bf0[22] = half_btf_sse4_1(&cospim32, &bf1[22], &cospi32, &bf1[25], &rounding, bit);
        bf0[23] = half_btf_sse4_1(&cospim32, &bf1[23], &cospi32, &bf1[24], &rounding, bit);
        bf0[24] = half_btf_sse4_1(&cospi32, &bf1[23], &cospi32, &bf1[24], &rounding, bit);
        bf0[25] = half_btf_sse4_1(&cospi32, &bf1[22], &cospi32, &bf1[25], &rounding, bit);
        bf0[26] = half_btf_sse4_1(&cospi32, &bf1[21], &cospi32, &bf1[26], &rounding, bit);
        bf0[27] = half_btf_sse4_1(&cospi32, &bf1[20], &cospi32, &bf1[27], &rounding, bit);
        bf0[28] = bf1[28];
        bf0[29] = bf1[29];
        bf0[30] = bf1[30];
        bf0[31] = bf1[31];

        // stage 9
        out[0 * 8 + col]  = _mm_add_epi32(bf0[0], bf0[31]);
        out[1 * 8 + col]  = _mm_add_epi32(bf0[1], bf0[30]);
        out[2 * 8 + col]  = _mm_add_epi32(bf0[2], bf0[29]);
        out[3 * 8 + col]  = _mm_add_epi32(bf0[3], bf0[28]);
        out[4 * 8 + col]  = _mm_add_epi32(bf0[4], bf0[27]);
        out[5 * 8 + col]  = _mm_add_epi32(bf0[5], bf0[26]);
        out[6 * 8 + col]  = _mm_add_epi32(bf0[6], bf0[25]);
        out[7 * 8 + col]  = _mm_add_epi32(bf0[7], bf0[24]);
        out[8 * 8 + col]  = _mm_add_epi32(bf0[8], bf0[23]);
        out[9 * 8 + col]  = _mm_add_epi32(bf0[9], bf0[22]);
        out[10 * 8 + col] = _mm_add_epi32(bf0[10], bf0[21]);
        out[11 * 8 + col] = _mm_add_epi32(bf0[11], bf0[20]);
        out[12 * 8 + col] = _mm_add_epi32(bf0[12], bf0[19]);
        out[13 * 8 + col] = _mm_add_epi32(bf0[13], bf0[18]);
        out[14 * 8 + col] = _mm_add_epi32(bf0[14], bf0[17]);
        out[15 * 8 + col] = _mm_add_epi32(bf0[15], bf0[16]);
        out[16 * 8 + col] = _mm_sub_epi32(bf0[15], bf0[16]);
        out[17 * 8 + col] = _mm_sub_epi32(bf0[14], bf0[17]);
        out[18 * 8 + col] = _mm_sub_epi32(bf0[13], bf0[18]);
        out[19 * 8 + col] = _mm_sub_epi32(bf0[12], bf0[19]);
        out[20 * 8 + col] = _mm_sub_epi32(bf0[11], bf0[20]);
        out[21 * 8 + col] = _mm_sub_epi32(bf0[10], bf0[21]);
        out[22 * 8 + col] = _mm_sub_epi32(bf0[9], bf0[22]);
        out[23 * 8 + col] = _mm_sub_epi32(bf0[8], bf0[23]);
        out[24 * 8 + col] = _mm_sub_epi32(bf0[7], bf0[24]);
        out[25 * 8 + col] = _mm_sub_epi32(bf0[6], bf0[25]);
        out[26 * 8 + col] = _mm_sub_epi32(bf0[5], bf0[26]);
        out[27 * 8 + col] = _mm_sub_epi32(bf0[4], bf0[27]);
        out[28 * 8 + col] = _mm_sub_epi32(bf0[3], bf0[28]);
        out[29 * 8 + col] = _mm_sub_epi32(bf0[2], bf0[29]);
        out[30 * 8 + col] = _mm_sub_epi32(bf0[1], bf0[30]);
        out[31 * 8 + col] = _mm_sub_epi32(bf0[0], bf0[31]);
    }
}

static const Transform1dSse41 highbd_txfm_all_1d_zeros_w8_arr[TX_SIZES][ITX_TYPES_1D][4] = {
    {
        {idct4x4_sse4_1, NULL, NULL, NULL},
        {iadst4x4_sse4_1, NULL, NULL, NULL},
        {iidentity4_sse4_1, iidentity4_sse4_1, iidentity4_sse4_1, NULL},
    },
    {{idct8x8_low1_sse4_1, idct8x8_new_sse4_1, NULL, NULL},
     {iadst8x8_low1_sse4_1, iadst8x8_new_sse4_1, NULL, NULL},
     {iidentity8_sse4_1, iidentity8_sse4_1, NULL, NULL}},
    {
        {idct16x16_low1_sse4_1, idct16x16_low8_sse4_1, idct16x16_new_sse4_1, NULL},
        {iadst16x16_low1_sse4_1, iadst16x16_low8_sse4_1, iadst16x16_new_sse4_1, NULL},
        {iidentity16_sse4_1, NULL, iidentity16_sse4_1, NULL},
    },
    {{idct32x32_low1_sse4_1, idct32x32_low8_sse4_1, idct32x32_low16_sse4_1, idct32x32_sse4_1},
     {NULL, NULL, NULL, NULL},
     {iidentity32_sse4_1, NULL, NULL, NULL}},
    {{idct64x64_low1_sse4_1,
      idct64x64_low8_sse4_1,
      idct64x64_low16_sse4_1,
      idct64x64_general_sse4_1},
     {NULL, NULL, NULL, NULL},
     {NULL, NULL, NULL, NULL}}};

void svt_av1_inv_txfm2d_add_4x8_sse4_1(const int32_t *input, uint16_t *output_r, int32_t stride_r,
                                       uint16_t *output_w, int32_t stride_w, TxType tx_type,
                                       TxSize tx_size, int32_t bd) {
    __m128i                buf1[8];
    const int8_t          *shift         = eb_inv_txfm_shift_ls[tx_size];
    const int32_t          txw_idx       = get_txw_idx(tx_size);
    const int32_t          txh_idx       = get_txh_idx(tx_size);
    const int32_t          txfm_size_col = tx_size_wide[tx_size];
    const int32_t          txfm_size_row = tx_size_high[tx_size];
    const Transform1dSse41 row_txfm =
        highbd_txfm_all_1d_zeros_w8_arr[txw_idx][hitx_1d_tab[tx_type]][0];
    const Transform1dSse41 col_txfm =
        highbd_txfm_all_1d_zeros_w8_arr[txh_idx][vitx_1d_tab[tx_type]][1];
    const int32_t input_stride = AOMMIN(32, txfm_size_col);

    assert(col_txfm != NULL);
    assert(row_txfm != NULL);
    int32_t ud_flip, lr_flip;
    get_flip_cfg(tx_type, &ud_flip, &lr_flip);

    // 1st stage: column transform
    __m128i        buf0[8];
    const int32_t *input_row = input;
    __m128i       *buf0_cur  = buf0;
    load_buffer_32bit_input(input_row, input_stride, buf0_cur, txfm_size_row);
    av1_round_shift_rect_array_32_sse4_1(buf0, buf0, txfm_size_row, 0, new_inv_sqrt2);
    row_txfm(buf0, buf0, inv_cos_bit_row[txw_idx][txh_idx], 0, bd, -shift[0]);
    row_txfm(buf0 + 4, buf0 + 4, inv_cos_bit_row[txw_idx][txh_idx], 0, bd, -shift[0]);

    if (lr_flip) {
        TRANSPOSE_4X4(buf0[3], buf0[2], buf0[1], buf0[0], buf1[0], buf1[1], buf1[2], buf1[3]);

        TRANSPOSE_4X4(buf0[7], buf0[6], buf0[5], buf0[4], buf1[4], buf1[5], buf1[6], buf1[7]);
    } else {
        TRANSPOSE_4X4(buf0[0], buf0[1], buf0[2], buf0[3], buf1[0], buf1[1], buf1[2], buf1[3]);

        TRANSPOSE_4X4(buf0[4], buf0[5], buf0[6], buf0[7], buf1[4], buf1[5], buf1[6], buf1[7]);
    }

    // 2nd stage: column transform
    col_txfm(buf1, buf1, inv_cos_bit_col[txw_idx][txh_idx], 1, bd, 0);

    av1_round_shift_array_32_sse4_1(buf1, buf1, txfm_size_row, -shift[1]);

    // write to buffer
    highbd_write_buffer_4xn_sse4_1(
        buf1, output_r, stride_r, output_w, stride_w, ud_flip, txfm_size_row, bd);
}

//8x4
static INLINE void flip_buf_sse2(__m128i *in, __m128i *out, int32_t size) {
    for (int32_t i = 0; i < size; ++i) out[size - i - 1] = in[i];
}

static INLINE __m128i highbd_get_recon_8x8_sse4_1(const __m128i pred, __m128i res0, __m128i res1,
                                                  const int32_t bd) {
    __m128i x0           = _mm_cvtepi16_epi32(pred);
    __m128i x1           = _mm_cvtepi16_epi32(_mm_srli_si128(pred, 8));
    __m128i min_clip_val = _mm_setzero_si128();
    __m128i max_clip_val = _mm_set1_epi32((1 << bd) - 1);
    x0                   = _mm_add_epi32(res0, x0);
    x1                   = _mm_add_epi32(res1, x1);
    x0                   = _mm_max_epi32(x0, min_clip_val);
    x0                   = _mm_min_epi32(x0, max_clip_val);
    x1                   = _mm_max_epi32(x1, min_clip_val);
    x1                   = _mm_min_epi32(x1, max_clip_val);
    x0                   = _mm_packus_epi32(x0, x1);
    return x0;
}

static INLINE void highbd_write_buffer_8xn_sse4_1(__m128i *in, uint16_t *output_r, int32_t stride_r,
                                                  uint16_t *output_w, int32_t stride_w,
                                                  int32_t flipud, int32_t height,
                                                  const int32_t bd) {
    int32_t       j    = flipud ? (height - 1) : 0;
    const int32_t step = flipud ? -1 : 1;
    for (int32_t i = 0; i < height; ++i, j += step) {
        __m128i v = _mm_loadu_si128((__m128i const *)(output_r + i * stride_r));
        __m128i u = highbd_get_recon_8x8_sse4_1(v, in[j], in[j + height], bd);

        _mm_storeu_si128((__m128i *)(output_w + i * stride_w), u);
    }
}
void svt_av1_inv_txfm2d_add_8x4_sse4_1(const int32_t *input, uint16_t *output_r, int32_t stride_r,
                                       uint16_t *output_w, int32_t stride_w, TxType tx_type,
                                       TxSize tx_size, int32_t bd) {
    __m128i                buf1[8];
    const int8_t          *shift         = eb_inv_txfm_shift_ls[tx_size];
    const int32_t          txw_idx       = get_txw_idx(tx_size);
    const int32_t          txh_idx       = get_txh_idx(tx_size);
    const int32_t          txfm_size_col = tx_size_wide[tx_size];
    const int32_t          txfm_size_row = tx_size_high[tx_size];
    const Transform1dSse41 row_txfm =
        highbd_txfm_all_1d_zeros_w8_arr[txw_idx][hitx_1d_tab[tx_type]][1];
    const Transform1dSse41 col_txfm =
        highbd_txfm_all_1d_zeros_w8_arr[txh_idx][vitx_1d_tab[tx_type]][0];

    assert(col_txfm != NULL);
    assert(row_txfm != NULL);
    int32_t ud_flip, lr_flip;
    get_flip_cfg(tx_type, &ud_flip, &lr_flip);

    // 1st stage: column transform
    __m128i        buf0[8];
    const int32_t *input_row = input;
    load_buffer_32bit_input(input_row, 4, buf0, txfm_size_col);

    TRANSPOSE_4X4(buf0[0], buf0[2], buf0[4], buf0[6], buf1[0], buf1[1], buf1[2], buf1[3]);
    TRANSPOSE_4X4(buf0[1], buf0[3], buf0[5], buf0[7], buf1[4], buf1[5], buf1[6], buf1[7]);

    av1_round_shift_rect_array_32_sse4_1(buf1, buf0, txfm_size_col, 0, new_inv_sqrt2);
    row_txfm(buf0, buf0, inv_cos_bit_row[txw_idx][txh_idx], 0, bd, -shift[0]);

    __m128i *buf1_ptr;
    if (lr_flip) {
        flip_buf_sse2(buf0, buf1, txfm_size_col);
        buf1_ptr = buf1;
    } else
        buf1_ptr = buf0;
    // 2nd stage: column transform
    for (int32_t i = 0; i < 2; i++) {
        col_txfm(buf1_ptr + i * txfm_size_row,
                 buf1_ptr + i * txfm_size_row,
                 inv_cos_bit_col[txw_idx][txh_idx],
                 1,
                 bd,
                 0);
    }
    av1_round_shift_array_32_sse4_1(buf1_ptr, buf1_ptr, txfm_size_col, -shift[1]);
    // write to buffer
    highbd_write_buffer_8xn_sse4_1(
        buf1_ptr, output_r, stride_r, output_w, stride_w, ud_flip, txfm_size_row, bd);
}

//4x16
void svt_av1_inv_txfm2d_add_4x16_sse4_1(const int32_t *input, uint16_t *output_r, int32_t stride_r,
                                        uint16_t *output_w, int32_t stride_w, TxType tx_type,
                                        TxSize tx_size, int32_t bd) {
    __m128i                buf1[16];
    const int8_t          *shift           = eb_inv_txfm_shift_ls[tx_size];
    const int32_t          txw_idx         = get_txw_idx(tx_size);
    const int32_t          txh_idx         = get_txh_idx(tx_size);
    const int32_t          txfm_size_col   = tx_size_wide[tx_size];
    const int32_t          txfm_size_row   = tx_size_high[tx_size];
    const int32_t          buf_size_h_div8 = txfm_size_row >> 2;
    const Transform1dSse41 row_txfm =
        highbd_txfm_all_1d_zeros_w8_arr[txw_idx][hitx_1d_tab[tx_type]][0];
    const Transform1dSse41 col_txfm =
        highbd_txfm_all_1d_zeros_w8_arr[txh_idx][vitx_1d_tab[tx_type]][2];
    const int32_t input_stride = AOMMIN(32, txfm_size_col);

    assert(col_txfm != NULL);
    assert(row_txfm != NULL);
    int32_t ud_flip, lr_flip;
    get_flip_cfg(tx_type, &ud_flip, &lr_flip);

    // 1st stage: column transform
    __m128i        buf0[16];
    const int32_t *input_row = input;
    __m128i       *buf0_cur  = buf0;
    load_buffer_32bit_input(input_row, input_stride, buf0_cur, txfm_size_row);
    for (int32_t i = 0; i < (txfm_size_row >> 2); i++) {
        row_txfm(
            buf0 + (i << 2), buf0 + (i << 2), inv_cos_bit_row[txw_idx][txh_idx], 0, bd, -shift[0]);
    }

    if (lr_flip) {
        for (int32_t j = 0; j < buf_size_h_div8; ++j) {
            TRANSPOSE_4X4(buf0[4 * j + 3],
                          buf0[4 * j + 2],
                          buf0[4 * j + 1],
                          buf0[4 * j],
                          buf1[4 * j],
                          buf1[4 * j + 1],
                          buf1[4 * j + 2],
                          buf1[4 * j + 3]);
        }
    } else {
        for (int32_t j = 0; j < buf_size_h_div8; ++j) {
            TRANSPOSE_4X4(buf0[4 * j],
                          buf0[4 * j + 1],
                          buf0[4 * j + 2],
                          buf0[4 * j + 3],
                          buf1[4 * j],
                          buf1[4 * j + 1],
                          buf1[4 * j + 2],
                          buf1[4 * j + 3]);
        }
    }

    // 2nd stage: column transform
    col_txfm(buf1, buf1, inv_cos_bit_col[txw_idx][txh_idx], 1, bd, 0);

    av1_round_shift_array_32_sse4_1(buf1, buf1, txfm_size_row, -shift[1]);

    // write to buffer
    highbd_write_buffer_4xn_sse4_1(
        buf1, output_r, stride_r, output_w, stride_w, ud_flip, txfm_size_row, bd);
}

//16x4
void svt_av1_inv_txfm2d_add_16x4_sse4_1(const int32_t *input, uint16_t *output_r, int32_t stride_r,
                                        uint16_t *output_w, int32_t stride_w, TxType tx_type,
                                        TxSize tx_size, int32_t bd) {
    __m128i                buf1[16];
    const int8_t          *shift           = eb_inv_txfm_shift_ls[tx_size];
    const int32_t          txw_idx         = get_txw_idx(tx_size);
    const int32_t          txh_idx         = get_txh_idx(tx_size);
    const int32_t          txfm_size_col   = tx_size_wide[tx_size];
    const int32_t          txfm_size_row   = tx_size_high[tx_size];
    const int32_t          buf_size_w_div8 = txfm_size_col >> 2;
    const Transform1dSse41 row_txfm =
        highbd_txfm_all_1d_zeros_w8_arr[txw_idx][hitx_1d_tab[tx_type]][2];
    const Transform1dSse41 col_txfm =
        highbd_txfm_all_1d_zeros_w8_arr[txh_idx][vitx_1d_tab[tx_type]][0];

    assert(col_txfm != NULL);
    assert(row_txfm != NULL);
    int32_t ud_flip, lr_flip;
    get_flip_cfg(tx_type, &ud_flip, &lr_flip);

    // 1st stage: column transform
    __m128i        buf0[16];
    const int32_t *input_row = input;
    load_buffer_32bit_input(input_row, 4, buf0, txfm_size_col);

    for (int32_t j = 0; j < buf_size_w_div8; j++) {
        TRANSPOSE_4X4(buf0[j],
                      buf0[j + 4],
                      buf0[j + 8],
                      buf0[j + 12],
                      buf1[4 * j],
                      buf1[4 * j + 1],
                      buf1[4 * j + 2],
                      buf1[4 * j + 3]);
    }
    row_txfm(buf1, buf0, inv_cos_bit_row[txw_idx][txh_idx], 0, bd, -shift[0]);

    __m128i *buf1_ptr;
    if (lr_flip) {
        flip_buf_sse2(buf0, buf1, txfm_size_col);
        buf1_ptr = buf1;
    } else
        buf1_ptr = buf0;
    // 2nd stage: column transform
    for (int32_t i = 0; i < buf_size_w_div8; i++) {
        col_txfm(buf1_ptr + i * txfm_size_row,
                 buf1_ptr + i * txfm_size_row,
                 inv_cos_bit_col[txw_idx][txh_idx],
                 1,
                 bd,
                 0);
    }
    av1_round_shift_array_32_sse4_1(buf1_ptr, buf1_ptr, txfm_size_col, -shift[1]);

    // write to buffer
    for (int32_t i = 0; i < (txfm_size_col >> 3); i++) {
        highbd_write_buffer_8xn_sse4_1(buf1_ptr + i * txfm_size_row * 2,
                                       output_r + 8 * i,
                                       stride_r,
                                       output_w + 8 * i,
                                       stride_w,
                                       ud_flip,
                                       txfm_size_row,
                                       bd);
    }
}

const int8_t *svt_av1_inv_txfm_shift_ls[TX_SIZES_ALL] = {
    inv_shift_4x4,   inv_shift_8x8,   inv_shift_16x16, inv_shift_32x32, inv_shift_64x64,
    inv_shift_4x8,   inv_shift_8x4,   inv_shift_8x16,  inv_shift_16x8,  inv_shift_16x32,
    inv_shift_32x16, inv_shift_32x64, inv_shift_64x32, inv_shift_4x16,  inv_shift_16x4,
    inv_shift_8x32,  inv_shift_32x8,  inv_shift_16x64, inv_shift_64x16,
};

typedef void (*transform_1d_sse4_1)(__m128i *in, __m128i *out, int bit, int do_cols, int bd,
                                    int out_shift);

static void svt_highbd_inv_txfm2d_add_no_identity_sse41(const int32_t *input, uint16_t *output_r,
                                                        int32_t stride_r, uint16_t *output_w,
                                                        int32_t stride_w, TxType tx_type,
                                                        TxSize tx_size, int32_t eob,
                                                        const int32_t bd) {
    __m128i buf1[64 * 16];
    int     eobx, eoby;
    get_eobx_eoby_scan_default(&eobx, &eoby, tx_size, eob);
    const int8_t *shift                   = svt_av1_inv_txfm_shift_ls[tx_size];
    const int     txw_idx                 = get_txw_idx(tx_size);
    const int     txh_idx                 = get_txh_idx(tx_size);
    const int     txfm_size_col           = tx_size_wide[tx_size];
    const int     txfm_size_row           = tx_size_high[tx_size];
    const int     buf_size_w_div8         = txfm_size_col >> 2;
    const int     buf_size_nonzero_w_div8 = (eobx + 8) >> 3;
    const int     buf_size_nonzero_h_div8 = (eoby + 8) >> 3;
    const int     input_stride            = AOMMIN(32, txfm_size_col);
    const int     rect_type               = get_rect_tx_log_ratio(txfm_size_col, txfm_size_row);

    const int                 fun_idx_x = lowbd_txfm_all_1d_zeros_idx[eobx];
    const int                 fun_idx_y = lowbd_txfm_all_1d_zeros_idx[eoby];
    const transform_1d_sse4_1 row_txfm =
        highbd_txfm_all_1d_zeros_w8_arr[txw_idx][hitx_1d_tab[tx_type]][fun_idx_x];
    const transform_1d_sse4_1 col_txfm =
        highbd_txfm_all_1d_zeros_w8_arr[txh_idx][vitx_1d_tab[tx_type]][fun_idx_y];

    assert(col_txfm != NULL);
    assert(row_txfm != NULL);
    int ud_flip, lr_flip;
    get_flip_cfg(tx_type, &ud_flip, &lr_flip);

    // 1st stage: column transform
    for (int i = 0; i < buf_size_nonzero_h_div8 << 1; i++) {
        __m128i        buf0[64];
        const int32_t *input_row = input + i * input_stride * 4;
        for (int j = 0; j < buf_size_nonzero_w_div8 << 1; ++j) {
            __m128i *buf0_cur = buf0 + j * 4;
            load_buffer_32bit_input(input_row + j * 4, input_stride, buf0_cur, 4);

            TRANSPOSE_4X4(buf0_cur[0],
                          buf0_cur[1],
                          buf0_cur[2],
                          buf0_cur[3],
                          buf0_cur[0],
                          buf0_cur[1],
                          buf0_cur[2],
                          buf0_cur[3]);
        }
        if (rect_type == 1 || rect_type == -1) {
            av1_round_shift_rect_array_32_sse4_1(
                buf0, buf0, buf_size_nonzero_w_div8 << 3, 0, new_inv_sqrt2);
        }
        row_txfm(buf0, buf0, inv_cos_bit_row[txw_idx][txh_idx], 0, bd, -shift[0]);

        __m128i *_buf1 = buf1 + i * 4;
        if (lr_flip) {
            for (int j = 0; j < buf_size_w_div8; ++j) {
                TRANSPOSE_4X4(buf0[4 * j + 3],
                              buf0[4 * j + 2],
                              buf0[4 * j + 1],
                              buf0[4 * j],
                              _buf1[txfm_size_row * (buf_size_w_div8 - 1 - j) + 0],
                              _buf1[txfm_size_row * (buf_size_w_div8 - 1 - j) + 1],
                              _buf1[txfm_size_row * (buf_size_w_div8 - 1 - j) + 2],
                              _buf1[txfm_size_row * (buf_size_w_div8 - 1 - j) + 3]);
            }
        } else {
            for (int j = 0; j < buf_size_w_div8; ++j) {
                TRANSPOSE_4X4(buf0[j * 4 + 0],
                              buf0[j * 4 + 1],
                              buf0[j * 4 + 2],
                              buf0[j * 4 + 3],
                              _buf1[j * txfm_size_row + 0],
                              _buf1[j * txfm_size_row + 1],
                              _buf1[j * txfm_size_row + 2],
                              _buf1[j * txfm_size_row + 3]);
            }
        }
    }
    // 2nd stage: column transform
    for (int i = 0; i < buf_size_w_div8; i++) {
        col_txfm(buf1 + i * txfm_size_row,
                 buf1 + i * txfm_size_row,
                 inv_cos_bit_col[txw_idx][txh_idx],
                 1,
                 bd,
                 0);

        av1_round_shift_array_32_sse4_1(
            buf1 + i * txfm_size_row, buf1 + i * txfm_size_row, txfm_size_row, -shift[1]);
    }

    // write to buffer
    for (int i = 0; i < (txfm_size_col >> 3); i++) {
        highbd_write_buffer_8xn_sse4_1(buf1 + i * txfm_size_row * 2,
                                       output_r + 8 * i,
                                       stride_r,
                                       output_w + 8 * i,
                                       stride_w,
                                       ud_flip,
                                       txfm_size_row,
                                       bd);
    }
}

static void svt_highbd_inv_txfm2d_add_v_identity_ssse41(const int32_t *input, uint16_t *output_r,
                                                        int32_t stride_r, uint16_t *output_w,
                                                        int32_t stride_w, TxType tx_type,
                                                        TxSize tx_size, int32_t eob,
                                                        const int32_t bd) {
    __m128i buf1[64];
    int     eobx, eoby;
    get_eobx_eoby_scan_v_identity(&eobx, &eoby, tx_size, eob);
    const int8_t             *shift           = svt_av1_inv_txfm_shift_ls[tx_size];
    const int                 txw_idx         = get_txw_idx(tx_size);
    const int                 txh_idx         = get_txh_idx(tx_size);
    const int                 txfm_size_col   = tx_size_wide[tx_size];
    const int                 txfm_size_row   = tx_size_high[tx_size];
    const int                 input_stride    = AOMMIN(32, txfm_size_col);
    const int                 buf_size_w_div4 = input_stride >> 2;
    const int                 buf_size_h_div8 = (eoby + 8) >> 3;
    const int                 rect_type       = get_rect_tx_log_ratio(txfm_size_col, txfm_size_row);
    const int                 fun_idx         = lowbd_txfm_all_1d_zeros_idx[eoby];
    const transform_1d_sse4_1 row_txfm =
        highbd_txfm_all_1d_zeros_w8_arr[txw_idx][hitx_1d_tab[tx_type]][0];
    const transform_1d_sse4_1 col_txfm =
        highbd_txfm_all_1d_zeros_w8_arr[txh_idx][vitx_1d_tab[tx_type]][fun_idx];
    int ud_flip, lr_flip;
    get_flip_cfg(tx_type, &ud_flip, &lr_flip);

    for (int i = 0; i < (buf_size_h_div8 << 1); ++i) {
        __m128i        buf0[16];
        const int32_t *input_row = input + i * input_stride * 4;
        for (int j = 0; j < buf_size_w_div4; ++j) {
            __m128i *buf0_cur = buf0 + j * 4;
            load_buffer_32bit_input(input_row + j * 4, input_stride, buf0_cur, 4);
        }
        if (rect_type == 1 || rect_type == -1) {
            av1_round_shift_rect_array_32_sse4_1(buf0, buf0, input_stride, 0, new_inv_sqrt2);
        }
        row_txfm(buf0, buf0, inv_cos_bit_row[txw_idx][txh_idx], 0, bd, -shift[0]);

        __m128i *_buf1 = buf1 + i * 4;

        for (int j = 0; j < buf_size_w_div4; ++j) {
            _buf1[j * txfm_size_row + 0] = buf0[j * 4 + 0];
            _buf1[j * txfm_size_row + 1] = buf0[j * 4 + 1];
            _buf1[j * txfm_size_row + 2] = buf0[j * 4 + 2];
            _buf1[j * txfm_size_row + 3] = buf0[j * 4 + 3];
        }
    }
    for (int i = 0; i < buf_size_w_div4; i++) {
        col_txfm(buf1 + i * txfm_size_row,
                 buf1 + i * txfm_size_row,
                 inv_cos_bit_col[txw_idx][txh_idx],
                 1,
                 bd,
                 0);

        av1_round_shift_array_32_sse4_1(
            buf1 + i * txfm_size_row, buf1 + i * txfm_size_row, txfm_size_row, -shift[1]);
    }

    // write to buffer
    for (int i = 0; i < (txfm_size_col >> 3); i++) {
        highbd_write_buffer_8xn_sse4_1(buf1 + i * txfm_size_row * 2,
                                       output_r + 8 * i,
                                       stride_r,
                                       output_w + 8 * i,
                                       stride_w,
                                       ud_flip,
                                       txfm_size_row,
                                       bd);
    }
}

static void svt_highbd_inv_txfm2d_add_h_identity_ssse41(const int32_t *input, uint16_t *output_r,
                                                        int32_t stride_r, uint16_t *output_w,
                                                        int32_t stride_w, TxType tx_type,
                                                        TxSize tx_size, int32_t eob,
                                                        const int8_t bd) {
    __m128i buf1[64];
    int     eobx, eoby;
    get_eobx_eoby_scan_h_identity(&eobx, &eoby, tx_size, eob);
    const int8_t             *shift                   = svt_av1_inv_txfm_shift_ls[tx_size];
    const int                 txw_idx                 = get_txw_idx(tx_size);
    const int                 txh_idx                 = get_txh_idx(tx_size);
    const int                 txfm_size_col           = tx_size_wide[tx_size];
    const int                 txfm_size_row           = tx_size_high[tx_size];
    const int                 input_stride            = AOMMIN(32, txfm_size_col);
    const int                 buf_size_w_div8         = input_stride >> 2;
    const int                 row_max                 = AOMMIN(32, txfm_size_row);
    const int                 buf_size_nonzero_w_div8 = (eobx + 8) >> 3;
    const int                 rect_type = get_rect_tx_log_ratio(txfm_size_col, txfm_size_row);
    const int                 fun_idx   = lowbd_txfm_all_1d_zeros_idx[eobx];
    const transform_1d_sse4_1 row_txfm =
        highbd_txfm_all_1d_zeros_w8_arr[txw_idx][hitx_1d_tab[tx_type]][fun_idx];
    const transform_1d_sse4_1 col_txfm =
        highbd_txfm_all_1d_zeros_w8_arr[txh_idx][vitx_1d_tab[tx_type]][0];
    int ud_flip, lr_flip;
    get_flip_cfg(tx_type, &ud_flip, &lr_flip);

    for (int i = 0; i < (row_max >> 2); ++i) {
        __m128i        buf0[16];
        const int32_t *input_row = input + i * input_stride * 4;
        for (int j = 0; j < (buf_size_nonzero_w_div8 << 1); ++j) {
            __m128i *buf0_cur = buf0 + j * 4;
            load_buffer_32bit_input(input_row + j * 4, input_stride, buf0_cur, 4);

            TRANSPOSE_4X4(buf0_cur[0],
                          buf0_cur[1],
                          buf0_cur[2],
                          buf0_cur[3],
                          buf0_cur[0],
                          buf0_cur[1],
                          buf0_cur[2],
                          buf0_cur[3]);
        }
        if (rect_type == 1 || rect_type == -1) {
            av1_round_shift_rect_array_32_sse4_1(
                buf0, buf0, (buf_size_nonzero_w_div8 << 3), 0, new_inv_sqrt2);
        }
        row_txfm(buf0, buf0, inv_cos_bit_row[txw_idx][txh_idx], 0, bd, -shift[0]);

        __m128i *_buf1 = buf1 + i * 4;
        if (lr_flip) {
            for (int j = 0; j < buf_size_w_div8; ++j) {
                TRANSPOSE_4X4(buf0[4 * j + 3],
                              buf0[4 * j + 2],
                              buf0[4 * j + 1],
                              buf0[4 * j],
                              _buf1[txfm_size_row * (buf_size_w_div8 - 1 - j) + 0],
                              _buf1[txfm_size_row * (buf_size_w_div8 - 1 - j) + 1],
                              _buf1[txfm_size_row * (buf_size_w_div8 - 1 - j) + 2],
                              _buf1[txfm_size_row * (buf_size_w_div8 - 1 - j) + 3]);
            }
        } else {
            for (int j = 0; j < buf_size_w_div8; ++j) {
                TRANSPOSE_4X4(buf0[j * 4 + 0],
                              buf0[j * 4 + 1],
                              buf0[j * 4 + 2],
                              buf0[j * 4 + 3],
                              _buf1[j * txfm_size_row + 0],
                              _buf1[j * txfm_size_row + 1],
                              _buf1[j * txfm_size_row + 2],
                              _buf1[j * txfm_size_row + 3]);
            }
        }
    }
    for (int i = 0; i < buf_size_w_div8; i++) {
        col_txfm(buf1 + i * txfm_size_row,
                 buf1 + i * txfm_size_row,
                 inv_cos_bit_col[txw_idx][txh_idx],
                 1,
                 bd,
                 0);

        av1_round_shift_array_32_sse4_1(
            buf1 + i * txfm_size_row, buf1 + i * txfm_size_row, txfm_size_row, -shift[1]);
    }

    // write to buffer
    {
        for (int i = 0; i < (txfm_size_col >> 3); i++) {
            highbd_write_buffer_8xn_sse4_1(buf1 + i * txfm_size_row * 2,
                                           output_r + 8 * i,
                                           stride_r,
                                           output_w + 8 * i,
                                           stride_w,
                                           ud_flip,
                                           txfm_size_row,
                                           bd);
        }
    }
}

static void svt_highbd_inv_txfm2d_add_idtx_ssse41(const int32_t *input, uint16_t *output_r,
                                                  int32_t stride_r, uint16_t *output_w,
                                                  int32_t stride_w, TxType tx_type, TxSize tx_size,
                                                  int32_t eob, const int8_t bd) {
    (void)eob;
    __m128i                   buf1[64 * 4];
    const int8_t             *shift         = svt_av1_inv_txfm_shift_ls[tx_size];
    const int                 txw_idx       = get_txw_idx(tx_size);
    const int                 txh_idx       = get_txh_idx(tx_size);
    const int                 txfm_size_col = tx_size_wide[tx_size];
    const int                 txfm_size_row = tx_size_high[tx_size];
    const int                 input_stride  = AOMMIN(32, txfm_size_col);
    const int                 row_max       = AOMMIN(32, txfm_size_row);
    const int                 rect_type     = get_rect_tx_log_ratio(txfm_size_col, txfm_size_row);
    const transform_1d_sse4_1 row_txfm =
        highbd_txfm_all_1d_zeros_w8_arr[txw_idx][hitx_1d_tab[tx_type]][0];
    const transform_1d_sse4_1 col_txfm =
        highbd_txfm_all_1d_zeros_w8_arr[txh_idx][vitx_1d_tab[tx_type]][0];
    int ud_flip, lr_flip;
    get_flip_cfg(tx_type, &ud_flip, &lr_flip);

    for (int i = 0; i < (row_max >> 2); ++i) {
        __m128i        buf0[32];
        const int32_t *input_row = input + i * input_stride * 4;
        for (int j = 0; j < (input_stride >> 2); ++j) {
            __m128i *buf0_cur = buf0 + j * 4;
            load_buffer_32bit_input(input_row + j * 4, input_stride, buf0_cur, 4);
        }
        if (rect_type == 1 || rect_type == -1) {
            av1_round_shift_rect_array_32_sse4_1(buf0, buf0, input_stride, 0, new_inv_sqrt2);
        }
        row_txfm(buf0, buf0, inv_cos_bit_row[txw_idx][txh_idx], 0, bd, -shift[0]);

        __m128i *_buf1 = buf1 + i * 4;
        for (int j = 0; j < (input_stride >> 2); ++j) {
            _buf1[j * txfm_size_row + 0] = buf0[j * 4 + 0];
            _buf1[j * txfm_size_row + 1] = buf0[j * 4 + 1];
            _buf1[j * txfm_size_row + 2] = buf0[j * 4 + 2];
            _buf1[j * txfm_size_row + 3] = buf0[j * 4 + 3];
        }
    }
    for (int i = 0; i < (input_stride >> 2); i++) {
        col_txfm(buf1 + i * txfm_size_row,
                 buf1 + i * txfm_size_row,
                 inv_cos_bit_col[txw_idx][txh_idx],
                 1,
                 bd,
                 0);

        av1_round_shift_array_32_sse4_1(
            buf1 + i * txfm_size_row, buf1 + i * txfm_size_row, txfm_size_row, -shift[1]);
    }

    // write to buffer
    {
        for (int i = 0; i < (txfm_size_col >> 3); i++) {
            highbd_write_buffer_8xn_sse4_1(buf1 + i * txfm_size_row * 2,
                                           output_r + 8 * i,
                                           stride_r,
                                           output_w + 8 * i,
                                           stride_w,
                                           ud_flip,
                                           txfm_size_row,
                                           bd);
        }
    }
}

void svt_av1_highbd_inv_txfm2d_add_universe_sse4_1(const int32_t *input, uint16_t *output_r,
                                                   int32_t stride_r, uint16_t *output_w,
                                                   int32_t stride_w, TxType tx_type, TxSize tx_size,
                                                   int32_t eob, const int32_t bd) {
    switch (tx_type) {
    case DCT_DCT:
    case ADST_DCT:
    case DCT_ADST:
    case ADST_ADST:
    case FLIPADST_DCT:
    case DCT_FLIPADST:
    case FLIPADST_FLIPADST:
    case ADST_FLIPADST:
    case FLIPADST_ADST:
        svt_highbd_inv_txfm2d_add_no_identity_sse41(
            input, output_r, stride_r, output_w, stride_w, tx_type, tx_size, eob, bd);
        break;
    case V_DCT:
    case V_ADST:
    case V_FLIPADST:
        svt_highbd_inv_txfm2d_add_v_identity_ssse41(
            input, output_r, stride_r, output_w, stride_w, tx_type, tx_size, eob, bd);
        break;
    case H_DCT:
    case H_ADST:
    case H_FLIPADST:
        svt_highbd_inv_txfm2d_add_h_identity_ssse41(
            input, output_r, stride_r, output_w, stride_w, tx_type, tx_size, eob, bd);
        break;
    case IDTX:
        svt_highbd_inv_txfm2d_add_idtx_ssse41(
            input, output_r, stride_r, output_w, stride_w, tx_type, tx_size, eob, bd);
        break;
    default: assert(0); break;
    }
}

void svt_av1_highbd_inv_txfm_add_sse4_1(const int32_t *input, uint16_t *output_r, int32_t stride_r,
                                        uint16_t *output_w, int32_t stride_w, TxType tx_type,
                                        TxSize tx_size, int32_t eob, int32_t bd) {
    svt_av1_highbd_inv_txfm2d_add_universe_sse4_1(
        input, output_r, stride_r, output_w, stride_w, tx_type, tx_size, eob, bd);
}

static void load_buffer_32x32(const int32_t *coeff, __m128i *in) {
    int32_t i;
    for (i = 0; i < 256; ++i) {
        in[i] = _mm_loadu_si128((const __m128i *)coeff);
        coeff += 4;
    }
}

static INLINE void transpose_8nx8n(const __m128i *input, __m128i *output, const int width,
                                   const int height) {
    const int numcol = height >> 2;
    const int numrow = width >> 2;
    for (int j = 0; j < numrow; j++) {
        for (int i = 0; i < numcol; i++) {
            TRANSPOSE_4X4(input[i * width + j + (numrow * 0)],
                          input[i * width + j + (numrow * 1)],
                          input[i * width + j + (numrow * 2)],
                          input[i * width + j + (numrow * 3)],
                          output[j * height + i + (numcol * 0)],
                          output[j * height + i + (numcol * 1)],
                          output[j * height + i + (numcol * 2)],
                          output[j * height + i + (numcol * 3)]);
        }
    }
}

void svt_av1_inv_txfm2d_add_32x32_sse4_1(const int32_t *input, uint16_t *output_r, int32_t stride_r,
                                         uint16_t *output_w, int32_t stride_w, TxType tx_type,
                                         int32_t bd) {
    __m128i       in[256], out[256];
    const int8_t *shift   = eb_inv_txfm_shift_ls[TX_32X32];
    const int32_t txw_idx = get_txw_idx(TX_32X32);
    const int32_t txh_idx = get_txh_idx(TX_32X32);

    switch (tx_type) {
    case DCT_DCT:
        load_buffer_32x32(input, in);
        transpose_8nx8n(in, out, 32, 32);
        idct32_sse4_1(out, in, inv_cos_bit_row[txw_idx][txh_idx]);
        round_shift_32x32(in, -shift[0]);
        transpose_8nx8n(in, out, 32, 32);
        idct32_sse4_1(out, in, inv_cos_bit_col[txw_idx][txh_idx]);
        write_buffer_32x32(in, output_r, stride_r, output_w, stride_w, 0, 0, -shift[1], bd);
        break;
    case IDTX:
        load_buffer_32x32(input, in);
        write_buffer_32x32(
            in, output_r, stride_r, output_w, stride_w, 0, 0, (-shift[0] - shift[1] - 4), bd);
        break;
    default: assert(0);
    }
}
