aboutsummaryrefslogtreecommitdiff
path: root/vpx_dsp/x86/fwd_txfm_avx2.c
blob: c8f54a49cba6732f4d67d2eb6f9ee9a901de5176 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/*
 *  Copyright (c) 2012 The WebM project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#include <immintrin.h>  // AVX2
#include "./vpx_config.h"
#include "./vpx_dsp_rtcd.h"

#include "vpx_dsp/txfm_common.h"
#define ADD256_EPI16 _mm256_add_epi16
#define SUB256_EPI16 _mm256_sub_epi16

static INLINE void load_buffer_16bit_to_16bit_avx2(const int16_t *in,
                                                   int stride, __m256i *out,
                                                   int out_size, int pass) {
  int i;
  const __m256i kOne = _mm256_set1_epi16(1);
  if (pass == 0) {
    for (i = 0; i < out_size; i++) {
      out[i] = _mm256_loadu_si256((const __m256i *)(in + i * stride));
      // x = x << 2
      out[i] = _mm256_slli_epi16(out[i], 2);
    }
  } else {
    for (i = 0; i < out_size; i++) {
      out[i] = _mm256_loadu_si256((const __m256i *)(in + i * 16));
      // x = (x + 1) >> 2
      out[i] = _mm256_add_epi16(out[i], kOne);
      out[i] = _mm256_srai_epi16(out[i], 2);
    }
  }
}

static INLINE void transpose2_8x8_avx2(const __m256i *const in,
                                       __m256i *const out) {
  int i;
  __m256i t[16], u[16];
  // (1st, 2nd) ==> (lo, hi)
  //   (0, 1)   ==>  (0, 1)
  //   (2, 3)   ==>  (2, 3)
  //   (4, 5)   ==>  (4, 5)
  //   (6, 7)   ==>  (6, 7)
  for (i = 0; i < 4; i++) {
    t[2 * i] = _mm256_unpacklo_epi16(in[2 * i], in[2 * i + 1]);
    t[2 * i + 1] = _mm256_unpackhi_epi16(in[2 * i], in[2 * i + 1]);
  }

  // (1st, 2nd) ==> (lo, hi)
  //   (0, 2)   ==>  (0, 2)
  //   (1, 3)   ==>  (1, 3)
  //   (4, 6)   ==>  (4, 6)
  //   (5, 7)   ==>  (5, 7)
  for (i = 0; i < 2; i++) {
    u[i] = _mm256_unpacklo_epi32(t[i], t[i + 2]);
    u[i + 2] = _mm256_unpackhi_epi32(t[i], t[i + 2]);

    u[i + 4] = _mm256_unpacklo_epi32(t[i + 4], t[i + 6]);
    u[i + 6] = _mm256_unpackhi_epi32(t[i + 4], t[i + 6]);
  }

  // (1st, 2nd) ==> (lo, hi)
  //   (0, 4)   ==>  (0, 1)
  //   (1, 5)   ==>  (4, 5)
  //   (2, 6)   ==>  (2, 3)
  //   (3, 7)   ==>  (6, 7)
  for (i = 0; i < 2; i++) {
    out[2 * i] = _mm256_unpacklo_epi64(u[2 * i], u[2 * i + 4]);
    out[2 * i + 1] = _mm256_unpackhi_epi64(u[2 * i], u[2 * i + 4]);

    out[2 * i + 4] = _mm256_unpacklo_epi64(u[2 * i + 1], u[2 * i + 5]);
    out[2 * i + 5] = _mm256_unpackhi_epi64(u[2 * i + 1], u[2 * i + 5]);
  }
}

static INLINE void transpose_16bit_16x16_avx2(const __m256i *const in,
                                              __m256i *const out) {
  __m256i t[16];

#define LOADL(idx)                                                            \
  t[idx] = _mm256_castsi128_si256(_mm_load_si128((__m128i const *)&in[idx])); \
  t[idx] = _mm256_inserti128_si256(                                           \
      t[idx], _mm_load_si128((__m128i const *)&in[idx + 8]), 1);

#define LOADR(idx)                                                           \
  t[8 + idx] =                                                               \
      _mm256_castsi128_si256(_mm_load_si128((__m128i const *)&in[idx] + 1)); \
  t[8 + idx] = _mm256_inserti128_si256(                                      \
      t[8 + idx], _mm_load_si128((__m128i const *)&in[idx + 8] + 1), 1);

  // load left 8x16
  LOADL(0)
  LOADL(1)
  LOADL(2)
  LOADL(3)
  LOADL(4)
  LOADL(5)
  LOADL(6)
  LOADL(7)

  // load right 8x16
  LOADR(0)
  LOADR(1)
  LOADR(2)
  LOADR(3)
  LOADR(4)
  LOADR(5)
  LOADR(6)
  LOADR(7)

  // get the top 16x8 result
  transpose2_8x8_avx2(t, out);
  // get the bottom 16x8 result
  transpose2_8x8_avx2(&t[8], &out[8]);
}

// Store 8 16-bit values. Sign extend the values.
static INLINE void store_buffer_16bit_to_32bit_w16_avx2(const __m256i *const in,
                                                        tran_low_t *out,
                                                        const int stride,
                                                        const int out_size) {
  int i;
  for (i = 0; i < out_size; ++i) {
    _mm256_storeu_si256((__m256i *)(out), in[i]);
    out += stride;
  }
}

#define PAIR256_SET_EPI16(a, b)                                            \
  _mm256_set_epi16((int16_t)(b), (int16_t)(a), (int16_t)(b), (int16_t)(a), \
                   (int16_t)(b), (int16_t)(a), (int16_t)(b), (int16_t)(a), \
                   (int16_t)(b), (int16_t)(a), (int16_t)(b), (int16_t)(a), \
                   (int16_t)(b), (int16_t)(a), (int16_t)(b), (int16_t)(a))

static INLINE __m256i mult256_round_shift(const __m256i *pin0,
                                          const __m256i *pin1,
                                          const __m256i *pmultiplier,
                                          const __m256i *prounding,
                                          const int shift) {
  const __m256i u0 = _mm256_madd_epi16(*pin0, *pmultiplier);
  const __m256i u1 = _mm256_madd_epi16(*pin1, *pmultiplier);
  const __m256i v0 = _mm256_add_epi32(u0, *prounding);
  const __m256i v1 = _mm256_add_epi32(u1, *prounding);
  const __m256i w0 = _mm256_srai_epi32(v0, shift);
  const __m256i w1 = _mm256_srai_epi32(v1, shift);
  return _mm256_packs_epi32(w0, w1);
}

static INLINE void fdct16x16_1D_avx2(__m256i *input, __m256i *output) {
  int i;
  __m256i step2[4];
  __m256i in[8];
  __m256i step1[8];
  __m256i step3[8];

  const __m256i k__cospi_p16_p16 = _mm256_set1_epi16(cospi_16_64);
  const __m256i k__cospi_p16_m16 = PAIR256_SET_EPI16(cospi_16_64, -cospi_16_64);
  const __m256i k__cospi_p24_p08 = PAIR256_SET_EPI16(cospi_24_64, cospi_8_64);
  const __m256i k__cospi_p08_m24 = PAIR256_SET_EPI16(cospi_8_64, -cospi_24_64);
  const __m256i k__cospi_m08_p24 = PAIR256_SET_EPI16(-cospi_8_64, cospi_24_64);
  const __m256i k__cospi_p28_p04 = PAIR256_SET_EPI16(cospi_28_64, cospi_4_64);
  const __m256i k__cospi_m04_p28 = PAIR256_SET_EPI16(-cospi_4_64, cospi_28_64);
  const __m256i k__cospi_p12_p20 = PAIR256_SET_EPI16(cospi_12_64, cospi_20_64);
  const __m256i k__cospi_m20_p12 = PAIR256_SET_EPI16(-cospi_20_64, cospi_12_64);
  const __m256i k__cospi_p30_p02 = PAIR256_SET_EPI16(cospi_30_64, cospi_2_64);
  const __m256i k__cospi_p14_p18 = PAIR256_SET_EPI16(cospi_14_64, cospi_18_64);
  const __m256i k__cospi_m02_p30 = PAIR256_SET_EPI16(-cospi_2_64, cospi_30_64);
  const __m256i k__cospi_m18_p14 = PAIR256_SET_EPI16(-cospi_18_64, cospi_14_64);
  const __m256i k__cospi_p22_p10 = PAIR256_SET_EPI16(cospi_22_64, cospi_10_64);
  const __m256i k__cospi_p06_p26 = PAIR256_SET_EPI16(cospi_6_64, cospi_26_64);
  const __m256i k__cospi_m10_p22 = PAIR256_SET_EPI16(-cospi_10_64, cospi_22_64);
  const __m256i k__cospi_m26_p06 = PAIR256_SET_EPI16(-cospi_26_64, cospi_6_64);
  const __m256i k__DCT_CONST_ROUNDING = _mm256_set1_epi32(DCT_CONST_ROUNDING);

  // Calculate input for the first 8 results.
  for (i = 0; i < 8; i++) {
    in[i] = ADD256_EPI16(input[i], input[15 - i]);
  }

  // Calculate input for the next 8 results.
  for (i = 0; i < 8; i++) {
    step1[i] = SUB256_EPI16(input[7 - i], input[8 + i]);
  }

  // Work on the first eight values; fdct8(input, even_results);
  {
    // Add/subtract
    const __m256i q0 = ADD256_EPI16(in[0], in[7]);
    const __m256i q1 = ADD256_EPI16(in[1], in[6]);
    const __m256i q2 = ADD256_EPI16(in[2], in[5]);
    const __m256i q3 = ADD256_EPI16(in[3], in[4]);
    const __m256i q4 = SUB256_EPI16(in[3], in[4]);
    const __m256i q5 = SUB256_EPI16(in[2], in[5]);
    const __m256i q6 = SUB256_EPI16(in[1], in[6]);
    const __m256i q7 = SUB256_EPI16(in[0], in[7]);

    // Work on first four results
    {
      // Add/subtract
      const __m256i r0 = ADD256_EPI16(q0, q3);
      const __m256i r1 = ADD256_EPI16(q1, q2);
      const __m256i r2 = SUB256_EPI16(q1, q2);
      const __m256i r3 = SUB256_EPI16(q0, q3);

      // Interleave to do the multiply by constants which gets us
      // into 32 bits.
      {
        const __m256i t0 = _mm256_unpacklo_epi16(r0, r1);
        const __m256i t1 = _mm256_unpackhi_epi16(r0, r1);
        const __m256i t2 = _mm256_unpacklo_epi16(r2, r3);
        const __m256i t3 = _mm256_unpackhi_epi16(r2, r3);

        output[0] = mult256_round_shift(&t0, &t1, &k__cospi_p16_p16,
                                        &k__DCT_CONST_ROUNDING, DCT_CONST_BITS);
        output[8] = mult256_round_shift(&t0, &t1, &k__cospi_p16_m16,
                                        &k__DCT_CONST_ROUNDING, DCT_CONST_BITS);
        output[4] = mult256_round_shift(&t2, &t3, &k__cospi_p24_p08,
                                        &k__DCT_CONST_ROUNDING, DCT_CONST_BITS);
        output[12] =
            mult256_round_shift(&t2, &t3, &k__cospi_m08_p24,
                                &k__DCT_CONST_ROUNDING, DCT_CONST_BITS);
      }
    }

    // Work on next four results
    {
      // Interleave to do the multiply by constants which gets us
      // into 32 bits.
      const __m256i d0 = _mm256_unpacklo_epi16(q6, q5);
      const __m256i d1 = _mm256_unpackhi_epi16(q6, q5);
      const __m256i r0 = mult256_round_shift(
          &d0, &d1, &k__cospi_p16_m16, &k__DCT_CONST_ROUNDING, DCT_CONST_BITS);
      const __m256i r1 = mult256_round_shift(
          &d0, &d1, &k__cospi_p16_p16, &k__DCT_CONST_ROUNDING, DCT_CONST_BITS);

      {
        // Add/subtract
        const __m256i x0 = ADD256_EPI16(q4, r0);
        const __m256i x1 = SUB256_EPI16(q4, r0);
        const __m256i x2 = SUB256_EPI16(q7, r1);
        const __m256i x3 = ADD256_EPI16(q7, r1);

        // Interleave to do the multiply by constants which gets us
        // into 32 bits.
        {
          const __m256i t0 = _mm256_unpacklo_epi16(x0, x3);
          const __m256i t1 = _mm256_unpackhi_epi16(x0, x3);
          const __m256i t2 = _mm256_unpacklo_epi16(x1, x2);
          const __m256i t3 = _mm256_unpackhi_epi16(x1, x2);
          output[2] =
              mult256_round_shift(&t0, &t1, &k__cospi_p28_p04,
                                  &k__DCT_CONST_ROUNDING, DCT_CONST_BITS);
          output[14] =
              mult256_round_shift(&t0, &t1, &k__cospi_m04_p28,
                                  &k__DCT_CONST_ROUNDING, DCT_CONST_BITS);
          output[10] =
              mult256_round_shift(&t2, &t3, &k__cospi_p12_p20,
                                  &k__DCT_CONST_ROUNDING, DCT_CONST_BITS);
          output[6] =
              mult256_round_shift(&t2, &t3, &k__cospi_m20_p12,
                                  &k__DCT_CONST_ROUNDING, DCT_CONST_BITS);
        }
      }
    }
  }
  // Work on the next eight values; step1 -> odd_results
  {  // step 2
    {
      const __m256i t0 = _mm256_unpacklo_epi16(step1[5], step1[2]);
      const __m256i t1 = _mm256_unpackhi_epi16(step1[5], step1[2]);
      const __m256i t2 = _mm256_unpacklo_epi16(step1[4], step1[3]);
      const __m256i t3 = _mm256_unpackhi_epi16(step1[4], step1[3]);
      step2[0] = mult256_round_shift(&t0, &t1, &k__cospi_p16_m16,
                                     &k__DCT_CONST_ROUNDING, DCT_CONST_BITS);
      step2[1] = mult256_round_shift(&t2, &t3, &k__cospi_p16_m16,
                                     &k__DCT_CONST_ROUNDING, DCT_CONST_BITS);
      step2[2] = mult256_round_shift(&t0, &t1, &k__cospi_p16_p16,
                                     &k__DCT_CONST_ROUNDING, DCT_CONST_BITS);
      step2[3] = mult256_round_shift(&t2, &t3, &k__cospi_p16_p16,
                                     &k__DCT_CONST_ROUNDING, DCT_CONST_BITS);
    }
    // step 3
    {
      step3[0] = ADD256_EPI16(step1[0], step2[1]);
      step3[1] = ADD256_EPI16(step1[1], step2[0]);
      step3[2] = SUB256_EPI16(step1[1], step2[0]);
      step3[3] = SUB256_EPI16(step1[0], step2[1]);
      step3[4] = SUB256_EPI16(step1[7], step2[3]);
      step3[5] = SUB256_EPI16(step1[6], step2[2]);
      step3[6] = ADD256_EPI16(step1[6], step2[2]);
      step3[7] = ADD256_EPI16(step1[7], step2[3]);
    }
    // step 4
    {
      const __m256i t0 = _mm256_unpacklo_epi16(step3[1], step3[6]);
      const __m256i t1 = _mm256_unpackhi_epi16(step3[1], step3[6]);
      const __m256i t2 = _mm256_unpacklo_epi16(step3[2], step3[5]);
      const __m256i t3 = _mm256_unpackhi_epi16(step3[2], step3[5]);
      step2[0] = mult256_round_shift(&t0, &t1, &k__cospi_m08_p24,
                                     &k__DCT_CONST_ROUNDING, DCT_CONST_BITS);
      step2[1] = mult256_round_shift(&t2, &t3, &k__cospi_p24_p08,
                                     &k__DCT_CONST_ROUNDING, DCT_CONST_BITS);
      step2[2] = mult256_round_shift(&t0, &t1, &k__cospi_p24_p08,
                                     &k__DCT_CONST_ROUNDING, DCT_CONST_BITS);
      step2[3] = mult256_round_shift(&t2, &t3, &k__cospi_p08_m24,
                                     &k__DCT_CONST_ROUNDING, DCT_CONST_BITS);
    }
    // step 5
    {
      step1[0] = ADD256_EPI16(step3[0], step2[0]);
      step1[1] = SUB256_EPI16(step3[0], step2[0]);
      step1[2] = ADD256_EPI16(step3[3], step2[1]);
      step1[3] = SUB256_EPI16(step3[3], step2[1]);
      step1[4] = SUB256_EPI16(step3[4], step2[3]);
      step1[5] = ADD256_EPI16(step3[4], step2[3]);
      step1[6] = SUB256_EPI16(step3[7], step2[2]);
      step1[7] = ADD256_EPI16(step3[7], step2[2]);
    }
    // step 6
    {
      const __m256i t0 = _mm256_unpacklo_epi16(step1[0], step1[7]);
      const __m256i t1 = _mm256_unpackhi_epi16(step1[0], step1[7]);
      const __m256i t2 = _mm256_unpacklo_epi16(step1[1], step1[6]);
      const __m256i t3 = _mm256_unpackhi_epi16(step1[1], step1[6]);
      output[1] = mult256_round_shift(&t0, &t1, &k__cospi_p30_p02,
                                      &k__DCT_CONST_ROUNDING, DCT_CONST_BITS);
      output[9] = mult256_round_shift(&t2, &t3, &k__cospi_p14_p18,
                                      &k__DCT_CONST_ROUNDING, DCT_CONST_BITS);
      output[15] = mult256_round_shift(&t0, &t1, &k__cospi_m02_p30,
                                       &k__DCT_CONST_ROUNDING, DCT_CONST_BITS);
      output[7] = mult256_round_shift(&t2, &t3, &k__cospi_m18_p14,
                                      &k__DCT_CONST_ROUNDING, DCT_CONST_BITS);
    }
    {
      const __m256i t0 = _mm256_unpacklo_epi16(step1[2], step1[5]);
      const __m256i t1 = _mm256_unpackhi_epi16(step1[2], step1[5]);
      const __m256i t2 = _mm256_unpacklo_epi16(step1[3], step1[4]);
      const __m256i t3 = _mm256_unpackhi_epi16(step1[3], step1[4]);
      output[5] = mult256_round_shift(&t0, &t1, &k__cospi_p22_p10,
                                      &k__DCT_CONST_ROUNDING, DCT_CONST_BITS);
      output[13] = mult256_round_shift(&t2, &t3, &k__cospi_p06_p26,
                                       &k__DCT_CONST_ROUNDING, DCT_CONST_BITS);
      output[11] = mult256_round_shift(&t0, &t1, &k__cospi_m10_p22,
                                       &k__DCT_CONST_ROUNDING, DCT_CONST_BITS);
      output[3] = mult256_round_shift(&t2, &t3, &k__cospi_m26_p06,
                                      &k__DCT_CONST_ROUNDING, DCT_CONST_BITS);
    }
  }
}

void vpx_fdct16x16_avx2(const int16_t *input, tran_low_t *output, int stride) {
  int pass;
  DECLARE_ALIGNED(32, int16_t, intermediate[256]);
  int16_t *out0 = intermediate;
  tran_low_t *out1 = output;
  const int width = 16;
  const int height = 16;
  __m256i buf0[16], buf1[16];

  // Two transform and transpose passes
  // Process 16 columns (transposed rows in second pass) at a time.
  for (pass = 0; pass < 2; ++pass) {
    // Load and pre-condition input.
    load_buffer_16bit_to_16bit_avx2(input, stride, buf1, height, pass);

    // Calculate dct for 16x16 values
    fdct16x16_1D_avx2(buf1, buf0);

    // Transpose the results.
    transpose_16bit_16x16_avx2(buf0, buf1);

    if (pass == 0) {
      store_buffer_16bit_to_32bit_w16_avx2(buf1, out0, width, height);
    } else {
      store_buffer_16bit_to_32bit_w16_avx2(buf1, out1, width, height);
    }
    // Setup in/out for next pass.
    input = intermediate;
  }
}

#if !CONFIG_VP9_HIGHBITDEPTH
#define FDCT32x32_2D_AVX2 vpx_fdct32x32_rd_avx2
#define FDCT32x32_HIGH_PRECISION 0
#include "vpx_dsp/x86/fwd_dct32x32_impl_avx2.h"
#undef FDCT32x32_2D_AVX2
#undef FDCT32x32_HIGH_PRECISION

#define FDCT32x32_2D_AVX2 vpx_fdct32x32_avx2
#define FDCT32x32_HIGH_PRECISION 1
#include "vpx_dsp/x86/fwd_dct32x32_impl_avx2.h"  // NOLINT
#undef FDCT32x32_2D_AVX2
#undef FDCT32x32_HIGH_PRECISION
#endif  // !CONFIG_VP9_HIGHBITDEPTH