inv_txfm_sse2.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  1. /*
  2. * Copyright (c) 2015 The WebM project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #ifndef VPX_VPX_DSP_X86_INV_TXFM_SSE2_H_
  11. #define VPX_VPX_DSP_X86_INV_TXFM_SSE2_H_
  12. #include <emmintrin.h> // SSE2
  13. #include "./vpx_config.h"
  14. #include "vpx/vpx_integer.h"
  15. #include "vpx_dsp/inv_txfm.h"
  16. #include "vpx_dsp/x86/transpose_sse2.h"
  17. #include "vpx_dsp/x86/txfm_common_sse2.h"
  18. static INLINE void idct8x8_12_transpose_16bit_4x8(const __m128i *const in,
  19. __m128i *const out) {
  20. // Unpack 16 bit elements. Goes from:
  21. // in[0]: 30 31 32 33 00 01 02 03
  22. // in[1]: 20 21 22 23 10 11 12 13
  23. // in[2]: 40 41 42 43 70 71 72 73
  24. // in[3]: 50 51 52 53 60 61 62 63
  25. // to:
  26. // tr0_0: 00 10 01 11 02 12 03 13
  27. // tr0_1: 20 30 21 31 22 32 23 33
  28. // tr0_2: 40 50 41 51 42 52 43 53
  29. // tr0_3: 60 70 61 71 62 72 63 73
  30. const __m128i tr0_0 = _mm_unpackhi_epi16(in[0], in[1]);
  31. const __m128i tr0_1 = _mm_unpacklo_epi16(in[1], in[0]);
  32. const __m128i tr0_2 = _mm_unpacklo_epi16(in[2], in[3]);
  33. const __m128i tr0_3 = _mm_unpackhi_epi16(in[3], in[2]);
  34. // Unpack 32 bit elements resulting in:
  35. // tr1_0: 00 10 20 30 01 11 21 31
  36. // tr1_1: 02 12 22 32 03 13 23 33
  37. // tr1_2: 40 50 60 70 41 51 61 71
  38. // tr1_3: 42 52 62 72 43 53 63 73
  39. const __m128i tr1_0 = _mm_unpacklo_epi32(tr0_0, tr0_1);
  40. const __m128i tr1_1 = _mm_unpacklo_epi32(tr0_2, tr0_3);
  41. const __m128i tr1_2 = _mm_unpackhi_epi32(tr0_0, tr0_1);
  42. const __m128i tr1_3 = _mm_unpackhi_epi32(tr0_2, tr0_3);
  43. // Unpack 64 bit elements resulting in:
  44. // out[0]: 00 10 20 30 40 50 60 70
  45. // out[1]: 01 11 21 31 41 51 61 71
  46. // out[2]: 02 12 22 32 42 52 62 72
  47. // out[3]: 03 13 23 33 43 53 63 73
  48. out[0] = _mm_unpacklo_epi64(tr1_0, tr1_1);
  49. out[1] = _mm_unpackhi_epi64(tr1_0, tr1_1);
  50. out[2] = _mm_unpacklo_epi64(tr1_2, tr1_3);
  51. out[3] = _mm_unpackhi_epi64(tr1_2, tr1_3);
  52. }
  53. static INLINE __m128i dct_const_round_shift_sse2(const __m128i in) {
  54. const __m128i t = _mm_add_epi32(in, _mm_set1_epi32(DCT_CONST_ROUNDING));
  55. return _mm_srai_epi32(t, DCT_CONST_BITS);
  56. }
  57. static INLINE __m128i idct_madd_round_shift_sse2(const __m128i in,
  58. const __m128i cospi) {
  59. const __m128i t = _mm_madd_epi16(in, cospi);
  60. return dct_const_round_shift_sse2(t);
  61. }
  62. // Calculate the dot product between in0/1 and x and wrap to short.
  63. static INLINE __m128i idct_calc_wraplow_sse2(const __m128i in0,
  64. const __m128i in1,
  65. const __m128i x) {
  66. const __m128i t0 = idct_madd_round_shift_sse2(in0, x);
  67. const __m128i t1 = idct_madd_round_shift_sse2(in1, x);
  68. return _mm_packs_epi32(t0, t1);
  69. }
  70. // Multiply elements by constants and add them together.
  71. static INLINE void butterfly(const __m128i in0, const __m128i in1, const int c0,
  72. const int c1, __m128i *const out0,
  73. __m128i *const out1) {
  74. const __m128i cst0 = pair_set_epi16(c0, -c1);
  75. const __m128i cst1 = pair_set_epi16(c1, c0);
  76. const __m128i lo = _mm_unpacklo_epi16(in0, in1);
  77. const __m128i hi = _mm_unpackhi_epi16(in0, in1);
  78. *out0 = idct_calc_wraplow_sse2(lo, hi, cst0);
  79. *out1 = idct_calc_wraplow_sse2(lo, hi, cst1);
  80. }
  81. static INLINE __m128i butterfly_cospi16(const __m128i in) {
  82. const __m128i cst = pair_set_epi16(cospi_16_64, cospi_16_64);
  83. const __m128i lo = _mm_unpacklo_epi16(in, _mm_setzero_si128());
  84. const __m128i hi = _mm_unpackhi_epi16(in, _mm_setzero_si128());
  85. return idct_calc_wraplow_sse2(lo, hi, cst);
  86. }
  87. // Functions to allow 8 bit optimisations to be used when profile 0 is used with
  88. // highbitdepth enabled
  89. static INLINE __m128i load_input_data4(const tran_low_t *data) {
  90. #if CONFIG_VP9_HIGHBITDEPTH
  91. const __m128i zero = _mm_setzero_si128();
  92. const __m128i in = _mm_load_si128((const __m128i *)data);
  93. return _mm_packs_epi32(in, zero);
  94. #else
  95. return _mm_loadl_epi64((const __m128i *)data);
  96. #endif
  97. }
  98. static INLINE __m128i load_input_data8(const tran_low_t *data) {
  99. #if CONFIG_VP9_HIGHBITDEPTH
  100. const __m128i in0 = _mm_load_si128((const __m128i *)data);
  101. const __m128i in1 = _mm_load_si128((const __m128i *)(data + 4));
  102. return _mm_packs_epi32(in0, in1);
  103. #else
  104. return _mm_load_si128((const __m128i *)data);
  105. #endif
  106. }
  107. static INLINE void load_transpose_16bit_8x8(const tran_low_t *input,
  108. const int stride,
  109. __m128i *const in) {
  110. in[0] = load_input_data8(input + 0 * stride);
  111. in[1] = load_input_data8(input + 1 * stride);
  112. in[2] = load_input_data8(input + 2 * stride);
  113. in[3] = load_input_data8(input + 3 * stride);
  114. in[4] = load_input_data8(input + 4 * stride);
  115. in[5] = load_input_data8(input + 5 * stride);
  116. in[6] = load_input_data8(input + 6 * stride);
  117. in[7] = load_input_data8(input + 7 * stride);
  118. transpose_16bit_8x8(in, in);
  119. }
  120. static INLINE void recon_and_store(uint8_t *const dest, const __m128i in_x) {
  121. const __m128i zero = _mm_setzero_si128();
  122. __m128i d0 = _mm_loadl_epi64((__m128i *)(dest));
  123. d0 = _mm_unpacklo_epi8(d0, zero);
  124. d0 = _mm_add_epi16(in_x, d0);
  125. d0 = _mm_packus_epi16(d0, d0);
  126. _mm_storel_epi64((__m128i *)(dest), d0);
  127. }
  128. static INLINE void round_shift_8x8(const __m128i *const in,
  129. __m128i *const out) {
  130. const __m128i final_rounding = _mm_set1_epi16(1 << 4);
  131. out[0] = _mm_add_epi16(in[0], final_rounding);
  132. out[1] = _mm_add_epi16(in[1], final_rounding);
  133. out[2] = _mm_add_epi16(in[2], final_rounding);
  134. out[3] = _mm_add_epi16(in[3], final_rounding);
  135. out[4] = _mm_add_epi16(in[4], final_rounding);
  136. out[5] = _mm_add_epi16(in[5], final_rounding);
  137. out[6] = _mm_add_epi16(in[6], final_rounding);
  138. out[7] = _mm_add_epi16(in[7], final_rounding);
  139. out[0] = _mm_srai_epi16(out[0], 5);
  140. out[1] = _mm_srai_epi16(out[1], 5);
  141. out[2] = _mm_srai_epi16(out[2], 5);
  142. out[3] = _mm_srai_epi16(out[3], 5);
  143. out[4] = _mm_srai_epi16(out[4], 5);
  144. out[5] = _mm_srai_epi16(out[5], 5);
  145. out[6] = _mm_srai_epi16(out[6], 5);
  146. out[7] = _mm_srai_epi16(out[7], 5);
  147. }
  148. static INLINE void write_buffer_8x8(const __m128i *const in,
  149. uint8_t *const dest, const int stride) {
  150. __m128i t[8];
  151. round_shift_8x8(in, t);
  152. recon_and_store(dest + 0 * stride, t[0]);
  153. recon_and_store(dest + 1 * stride, t[1]);
  154. recon_and_store(dest + 2 * stride, t[2]);
  155. recon_and_store(dest + 3 * stride, t[3]);
  156. recon_and_store(dest + 4 * stride, t[4]);
  157. recon_and_store(dest + 5 * stride, t[5]);
  158. recon_and_store(dest + 6 * stride, t[6]);
  159. recon_and_store(dest + 7 * stride, t[7]);
  160. }
  161. static INLINE void recon_and_store4x4_sse2(const __m128i *const in,
  162. uint8_t *const dest,
  163. const int stride) {
  164. const __m128i zero = _mm_setzero_si128();
  165. __m128i d[2];
  166. // Reconstruction and Store
  167. d[0] = _mm_cvtsi32_si128(*(const int *)(dest));
  168. d[1] = _mm_cvtsi32_si128(*(const int *)(dest + stride * 3));
  169. d[0] = _mm_unpacklo_epi32(d[0],
  170. _mm_cvtsi32_si128(*(const int *)(dest + stride)));
  171. d[1] = _mm_unpacklo_epi32(
  172. _mm_cvtsi32_si128(*(const int *)(dest + stride * 2)), d[1]);
  173. d[0] = _mm_unpacklo_epi8(d[0], zero);
  174. d[1] = _mm_unpacklo_epi8(d[1], zero);
  175. d[0] = _mm_add_epi16(d[0], in[0]);
  176. d[1] = _mm_add_epi16(d[1], in[1]);
  177. d[0] = _mm_packus_epi16(d[0], d[1]);
  178. *(int *)dest = _mm_cvtsi128_si32(d[0]);
  179. d[0] = _mm_srli_si128(d[0], 4);
  180. *(int *)(dest + stride) = _mm_cvtsi128_si32(d[0]);
  181. d[0] = _mm_srli_si128(d[0], 4);
  182. *(int *)(dest + stride * 2) = _mm_cvtsi128_si32(d[0]);
  183. d[0] = _mm_srli_si128(d[0], 4);
  184. *(int *)(dest + stride * 3) = _mm_cvtsi128_si32(d[0]);
  185. }
  186. static INLINE void store_buffer_8x32(__m128i *in, uint8_t *dst, int stride) {
  187. const __m128i final_rounding = _mm_set1_epi16(1 << 5);
  188. int j = 0;
  189. while (j < 32) {
  190. in[j] = _mm_adds_epi16(in[j], final_rounding);
  191. in[j + 1] = _mm_adds_epi16(in[j + 1], final_rounding);
  192. in[j] = _mm_srai_epi16(in[j], 6);
  193. in[j + 1] = _mm_srai_epi16(in[j + 1], 6);
  194. recon_and_store(dst, in[j]);
  195. dst += stride;
  196. recon_and_store(dst, in[j + 1]);
  197. dst += stride;
  198. j += 2;
  199. }
  200. }
  201. static INLINE void write_buffer_8x1(uint8_t *const dest, const __m128i in) {
  202. const __m128i final_rounding = _mm_set1_epi16(1 << 5);
  203. __m128i out;
  204. out = _mm_adds_epi16(in, final_rounding);
  205. out = _mm_srai_epi16(out, 6);
  206. recon_and_store(dest, out);
  207. }
  208. // Only do addition and subtraction butterfly, size = 16, 32
  209. static INLINE void add_sub_butterfly(const __m128i *in, __m128i *out,
  210. int size) {
  211. int i = 0;
  212. const int num = size >> 1;
  213. const int bound = size - 1;
  214. while (i < num) {
  215. out[i] = _mm_add_epi16(in[i], in[bound - i]);
  216. out[bound - i] = _mm_sub_epi16(in[i], in[bound - i]);
  217. i++;
  218. }
  219. }
  220. static INLINE void idct8(const __m128i *const in /*in[8]*/,
  221. __m128i *const out /*out[8]*/) {
  222. __m128i step1[8], step2[8];
  223. // stage 1
  224. butterfly(in[1], in[7], cospi_28_64, cospi_4_64, &step1[4], &step1[7]);
  225. butterfly(in[5], in[3], cospi_12_64, cospi_20_64, &step1[5], &step1[6]);
  226. // stage 2
  227. butterfly(in[0], in[4], cospi_16_64, cospi_16_64, &step2[1], &step2[0]);
  228. butterfly(in[2], in[6], cospi_24_64, cospi_8_64, &step2[2], &step2[3]);
  229. step2[4] = _mm_add_epi16(step1[4], step1[5]);
  230. step2[5] = _mm_sub_epi16(step1[4], step1[5]);
  231. step2[6] = _mm_sub_epi16(step1[7], step1[6]);
  232. step2[7] = _mm_add_epi16(step1[7], step1[6]);
  233. // stage 3
  234. step1[0] = _mm_add_epi16(step2[0], step2[3]);
  235. step1[1] = _mm_add_epi16(step2[1], step2[2]);
  236. step1[2] = _mm_sub_epi16(step2[1], step2[2]);
  237. step1[3] = _mm_sub_epi16(step2[0], step2[3]);
  238. butterfly(step2[6], step2[5], cospi_16_64, cospi_16_64, &step1[5], &step1[6]);
  239. // stage 4
  240. out[0] = _mm_add_epi16(step1[0], step2[7]);
  241. out[1] = _mm_add_epi16(step1[1], step1[6]);
  242. out[2] = _mm_add_epi16(step1[2], step1[5]);
  243. out[3] = _mm_add_epi16(step1[3], step2[4]);
  244. out[4] = _mm_sub_epi16(step1[3], step2[4]);
  245. out[5] = _mm_sub_epi16(step1[2], step1[5]);
  246. out[6] = _mm_sub_epi16(step1[1], step1[6]);
  247. out[7] = _mm_sub_epi16(step1[0], step2[7]);
  248. }
  249. static INLINE void idct8x8_12_add_kernel_sse2(__m128i *const io /*io[8]*/) {
  250. const __m128i zero = _mm_setzero_si128();
  251. const __m128i cp_16_16 = pair_set_epi16(cospi_16_64, cospi_16_64);
  252. const __m128i cp_16_n16 = pair_set_epi16(cospi_16_64, -cospi_16_64);
  253. __m128i step1[8], step2[8], tmp[4];
  254. transpose_16bit_4x4(io, io);
  255. // io[0]: 00 10 20 30 01 11 21 31
  256. // io[1]: 02 12 22 32 03 13 23 33
  257. // stage 1
  258. {
  259. const __m128i cp_28_n4 = pair_set_epi16(cospi_28_64, -cospi_4_64);
  260. const __m128i cp_4_28 = pair_set_epi16(cospi_4_64, cospi_28_64);
  261. const __m128i cp_n20_12 = pair_set_epi16(-cospi_20_64, cospi_12_64);
  262. const __m128i cp_12_20 = pair_set_epi16(cospi_12_64, cospi_20_64);
  263. const __m128i lo_1 = _mm_unpackhi_epi16(io[0], zero);
  264. const __m128i lo_3 = _mm_unpackhi_epi16(io[1], zero);
  265. step1[4] = idct_calc_wraplow_sse2(cp_28_n4, cp_4_28, lo_1); // step1 4&7
  266. step1[5] = idct_calc_wraplow_sse2(cp_n20_12, cp_12_20, lo_3); // step1 5&6
  267. }
  268. // stage 2
  269. {
  270. const __m128i cp_24_n8 = pair_set_epi16(cospi_24_64, -cospi_8_64);
  271. const __m128i cp_8_24 = pair_set_epi16(cospi_8_64, cospi_24_64);
  272. const __m128i lo_0 = _mm_unpacklo_epi16(io[0], zero);
  273. const __m128i lo_2 = _mm_unpacklo_epi16(io[1], zero);
  274. const __m128i t = idct_madd_round_shift_sse2(cp_16_16, lo_0);
  275. step2[0] = _mm_packs_epi32(t, t); // step2 0&1
  276. step2[2] = idct_calc_wraplow_sse2(cp_8_24, cp_24_n8, lo_2); // step2 3&2
  277. step2[4] = _mm_add_epi16(step1[4], step1[5]); // step2 4&7
  278. step2[5] = _mm_sub_epi16(step1[4], step1[5]); // step2 5&6
  279. step2[6] = _mm_unpackhi_epi64(step2[5], zero); // step2 6
  280. }
  281. // stage 3
  282. {
  283. const __m128i lo_65 = _mm_unpacklo_epi16(step2[6], step2[5]);
  284. tmp[0] = _mm_add_epi16(step2[0], step2[2]); // step1 0&1
  285. tmp[1] = _mm_sub_epi16(step2[0], step2[2]); // step1 3&2
  286. step1[2] = _mm_unpackhi_epi64(tmp[1], tmp[0]); // step1 2&1
  287. step1[3] = _mm_unpacklo_epi64(tmp[1], tmp[0]); // step1 3&0
  288. step1[5] = idct_calc_wraplow_sse2(cp_16_n16, cp_16_16, lo_65); // step1 5&6
  289. }
  290. // stage 4
  291. tmp[0] = _mm_add_epi16(step1[3], step2[4]); // output 3&0
  292. tmp[1] = _mm_add_epi16(step1[2], step1[5]); // output 2&1
  293. tmp[2] = _mm_sub_epi16(step1[3], step2[4]); // output 4&7
  294. tmp[3] = _mm_sub_epi16(step1[2], step1[5]); // output 5&6
  295. idct8x8_12_transpose_16bit_4x8(tmp, io);
  296. io[4] = io[5] = io[6] = io[7] = zero;
  297. idct8(io, io);
  298. }
  299. static INLINE void idct16_8col(const __m128i *const in /*in[16]*/,
  300. __m128i *const out /*out[16]*/) {
  301. __m128i step1[16], step2[16];
  302. // stage 2
  303. butterfly(in[1], in[15], cospi_30_64, cospi_2_64, &step2[8], &step2[15]);
  304. butterfly(in[9], in[7], cospi_14_64, cospi_18_64, &step2[9], &step2[14]);
  305. butterfly(in[5], in[11], cospi_22_64, cospi_10_64, &step2[10], &step2[13]);
  306. butterfly(in[13], in[3], cospi_6_64, cospi_26_64, &step2[11], &step2[12]);
  307. // stage 3
  308. butterfly(in[2], in[14], cospi_28_64, cospi_4_64, &step1[4], &step1[7]);
  309. butterfly(in[10], in[6], cospi_12_64, cospi_20_64, &step1[5], &step1[6]);
  310. step1[8] = _mm_add_epi16(step2[8], step2[9]);
  311. step1[9] = _mm_sub_epi16(step2[8], step2[9]);
  312. step1[10] = _mm_sub_epi16(step2[11], step2[10]);
  313. step1[11] = _mm_add_epi16(step2[10], step2[11]);
  314. step1[12] = _mm_add_epi16(step2[12], step2[13]);
  315. step1[13] = _mm_sub_epi16(step2[12], step2[13]);
  316. step1[14] = _mm_sub_epi16(step2[15], step2[14]);
  317. step1[15] = _mm_add_epi16(step2[14], step2[15]);
  318. // stage 4
  319. butterfly(in[0], in[8], cospi_16_64, cospi_16_64, &step2[1], &step2[0]);
  320. butterfly(in[4], in[12], cospi_24_64, cospi_8_64, &step2[2], &step2[3]);
  321. butterfly(step1[14], step1[9], cospi_24_64, cospi_8_64, &step2[9],
  322. &step2[14]);
  323. butterfly(step1[10], step1[13], -cospi_8_64, -cospi_24_64, &step2[13],
  324. &step2[10]);
  325. step2[5] = _mm_sub_epi16(step1[4], step1[5]);
  326. step1[4] = _mm_add_epi16(step1[4], step1[5]);
  327. step2[6] = _mm_sub_epi16(step1[7], step1[6]);
  328. step1[7] = _mm_add_epi16(step1[6], step1[7]);
  329. step2[8] = step1[8];
  330. step2[11] = step1[11];
  331. step2[12] = step1[12];
  332. step2[15] = step1[15];
  333. // stage 5
  334. step1[0] = _mm_add_epi16(step2[0], step2[3]);
  335. step1[1] = _mm_add_epi16(step2[1], step2[2]);
  336. step1[2] = _mm_sub_epi16(step2[1], step2[2]);
  337. step1[3] = _mm_sub_epi16(step2[0], step2[3]);
  338. butterfly(step2[6], step2[5], cospi_16_64, cospi_16_64, &step1[5], &step1[6]);
  339. step1[8] = _mm_add_epi16(step2[8], step2[11]);
  340. step1[9] = _mm_add_epi16(step2[9], step2[10]);
  341. step1[10] = _mm_sub_epi16(step2[9], step2[10]);
  342. step1[11] = _mm_sub_epi16(step2[8], step2[11]);
  343. step1[12] = _mm_sub_epi16(step2[15], step2[12]);
  344. step1[13] = _mm_sub_epi16(step2[14], step2[13]);
  345. step1[14] = _mm_add_epi16(step2[14], step2[13]);
  346. step1[15] = _mm_add_epi16(step2[15], step2[12]);
  347. // stage 6
  348. step2[0] = _mm_add_epi16(step1[0], step1[7]);
  349. step2[1] = _mm_add_epi16(step1[1], step1[6]);
  350. step2[2] = _mm_add_epi16(step1[2], step1[5]);
  351. step2[3] = _mm_add_epi16(step1[3], step1[4]);
  352. step2[4] = _mm_sub_epi16(step1[3], step1[4]);
  353. step2[5] = _mm_sub_epi16(step1[2], step1[5]);
  354. step2[6] = _mm_sub_epi16(step1[1], step1[6]);
  355. step2[7] = _mm_sub_epi16(step1[0], step1[7]);
  356. butterfly(step1[13], step1[10], cospi_16_64, cospi_16_64, &step2[10],
  357. &step2[13]);
  358. butterfly(step1[12], step1[11], cospi_16_64, cospi_16_64, &step2[11],
  359. &step2[12]);
  360. // stage 7
  361. out[0] = _mm_add_epi16(step2[0], step1[15]);
  362. out[1] = _mm_add_epi16(step2[1], step1[14]);
  363. out[2] = _mm_add_epi16(step2[2], step2[13]);
  364. out[3] = _mm_add_epi16(step2[3], step2[12]);
  365. out[4] = _mm_add_epi16(step2[4], step2[11]);
  366. out[5] = _mm_add_epi16(step2[5], step2[10]);
  367. out[6] = _mm_add_epi16(step2[6], step1[9]);
  368. out[7] = _mm_add_epi16(step2[7], step1[8]);
  369. out[8] = _mm_sub_epi16(step2[7], step1[8]);
  370. out[9] = _mm_sub_epi16(step2[6], step1[9]);
  371. out[10] = _mm_sub_epi16(step2[5], step2[10]);
  372. out[11] = _mm_sub_epi16(step2[4], step2[11]);
  373. out[12] = _mm_sub_epi16(step2[3], step2[12]);
  374. out[13] = _mm_sub_epi16(step2[2], step2[13]);
  375. out[14] = _mm_sub_epi16(step2[1], step1[14]);
  376. out[15] = _mm_sub_epi16(step2[0], step1[15]);
  377. }
  378. static INLINE void idct16x16_10_pass1(const __m128i *const input /*input[4]*/,
  379. __m128i *const output /*output[16]*/) {
  380. const __m128i zero = _mm_setzero_si128();
  381. const __m128i k__cospi_p16_p16 = pair_set_epi16(cospi_16_64, cospi_16_64);
  382. const __m128i k__cospi_m16_p16 = pair_set_epi16(-cospi_16_64, cospi_16_64);
  383. __m128i step1[16], step2[16];
  384. transpose_16bit_4x4(input, output);
  385. // stage 2
  386. {
  387. const __m128i k__cospi_p30_m02 = pair_set_epi16(cospi_30_64, -cospi_2_64);
  388. const __m128i k__cospi_p02_p30 = pair_set_epi16(cospi_2_64, cospi_30_64);
  389. const __m128i k__cospi_p06_m26 = pair_set_epi16(cospi_6_64, -cospi_26_64);
  390. const __m128i k__cospi_p26_p06 = pair_set_epi16(cospi_26_64, cospi_6_64);
  391. const __m128i lo_1_15 = _mm_unpackhi_epi16(output[0], zero);
  392. const __m128i lo_13_3 = _mm_unpackhi_epi16(zero, output[1]);
  393. step2[8] = idct_calc_wraplow_sse2(k__cospi_p30_m02, k__cospi_p02_p30,
  394. lo_1_15); // step2 8&15
  395. step2[11] = idct_calc_wraplow_sse2(k__cospi_p06_m26, k__cospi_p26_p06,
  396. lo_13_3); // step2 11&12
  397. }
  398. // stage 3
  399. {
  400. const __m128i k__cospi_p28_m04 = pair_set_epi16(cospi_28_64, -cospi_4_64);
  401. const __m128i k__cospi_p04_p28 = pair_set_epi16(cospi_4_64, cospi_28_64);
  402. const __m128i lo_2_14 = _mm_unpacklo_epi16(output[1], zero);
  403. step1[4] = idct_calc_wraplow_sse2(k__cospi_p28_m04, k__cospi_p04_p28,
  404. lo_2_14); // step1 4&7
  405. step1[13] = _mm_unpackhi_epi64(step2[11], zero);
  406. step1[14] = _mm_unpackhi_epi64(step2[8], zero);
  407. }
  408. // stage 4
  409. {
  410. const __m128i k__cospi_m08_p24 = pair_set_epi16(-cospi_8_64, cospi_24_64);
  411. const __m128i k__cospi_p24_p08 = pair_set_epi16(cospi_24_64, cospi_8_64);
  412. const __m128i k__cospi_m24_m08 = pair_set_epi16(-cospi_24_64, -cospi_8_64);
  413. const __m128i lo_0_8 = _mm_unpacklo_epi16(output[0], zero);
  414. const __m128i lo_9_14 = _mm_unpacklo_epi16(step2[8], step1[14]);
  415. const __m128i lo_10_13 = _mm_unpacklo_epi16(step2[11], step1[13]);
  416. const __m128i t = idct_madd_round_shift_sse2(lo_0_8, k__cospi_p16_p16);
  417. step1[0] = _mm_packs_epi32(t, t); // step2 0&1
  418. step2[9] = idct_calc_wraplow_sse2(k__cospi_m08_p24, k__cospi_p24_p08,
  419. lo_9_14); // step2 9&14
  420. step2[10] = idct_calc_wraplow_sse2(k__cospi_m24_m08, k__cospi_m08_p24,
  421. lo_10_13); // step2 10&13
  422. step2[6] = _mm_unpackhi_epi64(step1[4], zero);
  423. }
  424. // stage 5
  425. {
  426. const __m128i lo_5_6 = _mm_unpacklo_epi16(step1[4], step2[6]);
  427. step1[6] = idct_calc_wraplow_sse2(k__cospi_p16_p16, k__cospi_m16_p16,
  428. lo_5_6); // step1 6&5
  429. step1[8] = _mm_add_epi16(step2[8], step2[11]);
  430. step1[9] = _mm_add_epi16(step2[9], step2[10]);
  431. step1[10] = _mm_sub_epi16(step2[9], step2[10]);
  432. step1[11] = _mm_sub_epi16(step2[8], step2[11]);
  433. step1[12] = _mm_unpackhi_epi64(step1[11], zero);
  434. step1[13] = _mm_unpackhi_epi64(step1[10], zero);
  435. step1[14] = _mm_unpackhi_epi64(step1[9], zero);
  436. step1[15] = _mm_unpackhi_epi64(step1[8], zero);
  437. }
  438. // stage 6
  439. {
  440. const __m128i lo_10_13 = _mm_unpacklo_epi16(step1[10], step1[13]);
  441. const __m128i lo_11_12 = _mm_unpacklo_epi16(step1[11], step1[12]);
  442. step2[10] = idct_calc_wraplow_sse2(k__cospi_m16_p16, k__cospi_p16_p16,
  443. lo_10_13); // step2 10&13
  444. step2[11] = idct_calc_wraplow_sse2(k__cospi_m16_p16, k__cospi_p16_p16,
  445. lo_11_12); // step2 11&12
  446. step2[13] = _mm_unpackhi_epi64(step2[10], zero);
  447. step2[12] = _mm_unpackhi_epi64(step2[11], zero);
  448. step2[3] = _mm_add_epi16(step1[0], step1[4]);
  449. step2[1] = _mm_add_epi16(step1[0], step1[6]);
  450. step2[6] = _mm_sub_epi16(step1[0], step1[6]);
  451. step2[4] = _mm_sub_epi16(step1[0], step1[4]);
  452. step2[0] = _mm_unpackhi_epi64(step2[3], zero);
  453. step2[2] = _mm_unpackhi_epi64(step2[1], zero);
  454. step2[5] = _mm_unpackhi_epi64(step2[6], zero);
  455. step2[7] = _mm_unpackhi_epi64(step2[4], zero);
  456. }
  457. // stage 7. Left 8x16 only.
  458. output[0] = _mm_add_epi16(step2[0], step1[15]);
  459. output[1] = _mm_add_epi16(step2[1], step1[14]);
  460. output[2] = _mm_add_epi16(step2[2], step2[13]);
  461. output[3] = _mm_add_epi16(step2[3], step2[12]);
  462. output[4] = _mm_add_epi16(step2[4], step2[11]);
  463. output[5] = _mm_add_epi16(step2[5], step2[10]);
  464. output[6] = _mm_add_epi16(step2[6], step1[9]);
  465. output[7] = _mm_add_epi16(step2[7], step1[8]);
  466. output[8] = _mm_sub_epi16(step2[7], step1[8]);
  467. output[9] = _mm_sub_epi16(step2[6], step1[9]);
  468. output[10] = _mm_sub_epi16(step2[5], step2[10]);
  469. output[11] = _mm_sub_epi16(step2[4], step2[11]);
  470. output[12] = _mm_sub_epi16(step2[3], step2[12]);
  471. output[13] = _mm_sub_epi16(step2[2], step2[13]);
  472. output[14] = _mm_sub_epi16(step2[1], step1[14]);
  473. output[15] = _mm_sub_epi16(step2[0], step1[15]);
  474. }
  475. static INLINE void idct16x16_10_pass2(__m128i *const l /*l[8]*/,
  476. __m128i *const io /*io[16]*/) {
  477. const __m128i zero = _mm_setzero_si128();
  478. __m128i step1[16], step2[16];
  479. transpose_16bit_4x8(l, io);
  480. // stage 2
  481. butterfly(io[1], zero, cospi_30_64, cospi_2_64, &step2[8], &step2[15]);
  482. butterfly(zero, io[3], cospi_6_64, cospi_26_64, &step2[11], &step2[12]);
  483. // stage 3
  484. butterfly(io[2], zero, cospi_28_64, cospi_4_64, &step1[4], &step1[7]);
  485. // stage 4
  486. step1[0] = butterfly_cospi16(io[0]);
  487. butterfly(step2[15], step2[8], cospi_24_64, cospi_8_64, &step2[9],
  488. &step2[14]);
  489. butterfly(step2[11], step2[12], -cospi_8_64, -cospi_24_64, &step2[13],
  490. &step2[10]);
  491. // stage 5
  492. butterfly(step1[7], step1[4], cospi_16_64, cospi_16_64, &step1[5], &step1[6]);
  493. step1[8] = _mm_add_epi16(step2[8], step2[11]);
  494. step1[9] = _mm_add_epi16(step2[9], step2[10]);
  495. step1[10] = _mm_sub_epi16(step2[9], step2[10]);
  496. step1[11] = _mm_sub_epi16(step2[8], step2[11]);
  497. step1[12] = _mm_sub_epi16(step2[15], step2[12]);
  498. step1[13] = _mm_sub_epi16(step2[14], step2[13]);
  499. step1[14] = _mm_add_epi16(step2[14], step2[13]);
  500. step1[15] = _mm_add_epi16(step2[15], step2[12]);
  501. // stage 6
  502. step2[0] = _mm_add_epi16(step1[0], step1[7]);
  503. step2[1] = _mm_add_epi16(step1[0], step1[6]);
  504. step2[2] = _mm_add_epi16(step1[0], step1[5]);
  505. step2[3] = _mm_add_epi16(step1[0], step1[4]);
  506. step2[4] = _mm_sub_epi16(step1[0], step1[4]);
  507. step2[5] = _mm_sub_epi16(step1[0], step1[5]);
  508. step2[6] = _mm_sub_epi16(step1[0], step1[6]);
  509. step2[7] = _mm_sub_epi16(step1[0], step1[7]);
  510. butterfly(step1[13], step1[10], cospi_16_64, cospi_16_64, &step2[10],
  511. &step2[13]);
  512. butterfly(step1[12], step1[11], cospi_16_64, cospi_16_64, &step2[11],
  513. &step2[12]);
  514. // stage 7
  515. io[0] = _mm_add_epi16(step2[0], step1[15]);
  516. io[1] = _mm_add_epi16(step2[1], step1[14]);
  517. io[2] = _mm_add_epi16(step2[2], step2[13]);
  518. io[3] = _mm_add_epi16(step2[3], step2[12]);
  519. io[4] = _mm_add_epi16(step2[4], step2[11]);
  520. io[5] = _mm_add_epi16(step2[5], step2[10]);
  521. io[6] = _mm_add_epi16(step2[6], step1[9]);
  522. io[7] = _mm_add_epi16(step2[7], step1[8]);
  523. io[8] = _mm_sub_epi16(step2[7], step1[8]);
  524. io[9] = _mm_sub_epi16(step2[6], step1[9]);
  525. io[10] = _mm_sub_epi16(step2[5], step2[10]);
  526. io[11] = _mm_sub_epi16(step2[4], step2[11]);
  527. io[12] = _mm_sub_epi16(step2[3], step2[12]);
  528. io[13] = _mm_sub_epi16(step2[2], step2[13]);
  529. io[14] = _mm_sub_epi16(step2[1], step1[14]);
  530. io[15] = _mm_sub_epi16(step2[0], step1[15]);
  531. }
  532. static INLINE void idct32_8x32_quarter_2_stage_4_to_6(
  533. __m128i *const step1 /*step1[16]*/, __m128i *const out /*out[16]*/) {
  534. __m128i step2[32];
  535. // stage 4
  536. step2[8] = step1[8];
  537. step2[15] = step1[15];
  538. butterfly(step1[14], step1[9], cospi_24_64, cospi_8_64, &step2[9],
  539. &step2[14]);
  540. butterfly(step1[13], step1[10], -cospi_8_64, cospi_24_64, &step2[10],
  541. &step2[13]);
  542. step2[11] = step1[11];
  543. step2[12] = step1[12];
  544. // stage 5
  545. step1[8] = _mm_add_epi16(step2[8], step2[11]);
  546. step1[9] = _mm_add_epi16(step2[9], step2[10]);
  547. step1[10] = _mm_sub_epi16(step2[9], step2[10]);
  548. step1[11] = _mm_sub_epi16(step2[8], step2[11]);
  549. step1[12] = _mm_sub_epi16(step2[15], step2[12]);
  550. step1[13] = _mm_sub_epi16(step2[14], step2[13]);
  551. step1[14] = _mm_add_epi16(step2[14], step2[13]);
  552. step1[15] = _mm_add_epi16(step2[15], step2[12]);
  553. // stage 6
  554. out[8] = step1[8];
  555. out[9] = step1[9];
  556. butterfly(step1[13], step1[10], cospi_16_64, cospi_16_64, &out[10], &out[13]);
  557. butterfly(step1[12], step1[11], cospi_16_64, cospi_16_64, &out[11], &out[12]);
  558. out[14] = step1[14];
  559. out[15] = step1[15];
  560. }
  561. static INLINE void idct32_8x32_quarter_3_4_stage_4_to_7(
  562. __m128i *const step1 /*step1[32]*/, __m128i *const out /*out[32]*/) {
  563. __m128i step2[32];
  564. // stage 4
  565. step2[16] = _mm_add_epi16(step1[16], step1[19]);
  566. step2[17] = _mm_add_epi16(step1[17], step1[18]);
  567. step2[18] = _mm_sub_epi16(step1[17], step1[18]);
  568. step2[19] = _mm_sub_epi16(step1[16], step1[19]);
  569. step2[20] = _mm_sub_epi16(step1[23], step1[20]);
  570. step2[21] = _mm_sub_epi16(step1[22], step1[21]);
  571. step2[22] = _mm_add_epi16(step1[22], step1[21]);
  572. step2[23] = _mm_add_epi16(step1[23], step1[20]);
  573. step2[24] = _mm_add_epi16(step1[24], step1[27]);
  574. step2[25] = _mm_add_epi16(step1[25], step1[26]);
  575. step2[26] = _mm_sub_epi16(step1[25], step1[26]);
  576. step2[27] = _mm_sub_epi16(step1[24], step1[27]);
  577. step2[28] = _mm_sub_epi16(step1[31], step1[28]);
  578. step2[29] = _mm_sub_epi16(step1[30], step1[29]);
  579. step2[30] = _mm_add_epi16(step1[29], step1[30]);
  580. step2[31] = _mm_add_epi16(step1[28], step1[31]);
  581. // stage 5
  582. step1[16] = step2[16];
  583. step1[17] = step2[17];
  584. butterfly(step2[29], step2[18], cospi_24_64, cospi_8_64, &step1[18],
  585. &step1[29]);
  586. butterfly(step2[28], step2[19], cospi_24_64, cospi_8_64, &step1[19],
  587. &step1[28]);
  588. butterfly(step2[27], step2[20], -cospi_8_64, cospi_24_64, &step1[20],
  589. &step1[27]);
  590. butterfly(step2[26], step2[21], -cospi_8_64, cospi_24_64, &step1[21],
  591. &step1[26]);
  592. step1[22] = step2[22];
  593. step1[23] = step2[23];
  594. step1[24] = step2[24];
  595. step1[25] = step2[25];
  596. step1[30] = step2[30];
  597. step1[31] = step2[31];
  598. // stage 6
  599. out[16] = _mm_add_epi16(step1[16], step1[23]);
  600. out[17] = _mm_add_epi16(step1[17], step1[22]);
  601. out[18] = _mm_add_epi16(step1[18], step1[21]);
  602. out[19] = _mm_add_epi16(step1[19], step1[20]);
  603. step2[20] = _mm_sub_epi16(step1[19], step1[20]);
  604. step2[21] = _mm_sub_epi16(step1[18], step1[21]);
  605. step2[22] = _mm_sub_epi16(step1[17], step1[22]);
  606. step2[23] = _mm_sub_epi16(step1[16], step1[23]);
  607. step2[24] = _mm_sub_epi16(step1[31], step1[24]);
  608. step2[25] = _mm_sub_epi16(step1[30], step1[25]);
  609. step2[26] = _mm_sub_epi16(step1[29], step1[26]);
  610. step2[27] = _mm_sub_epi16(step1[28], step1[27]);
  611. out[28] = _mm_add_epi16(step1[27], step1[28]);
  612. out[29] = _mm_add_epi16(step1[26], step1[29]);
  613. out[30] = _mm_add_epi16(step1[25], step1[30]);
  614. out[31] = _mm_add_epi16(step1[24], step1[31]);
  615. // stage 7
  616. butterfly(step2[27], step2[20], cospi_16_64, cospi_16_64, &out[20], &out[27]);
  617. butterfly(step2[26], step2[21], cospi_16_64, cospi_16_64, &out[21], &out[26]);
  618. butterfly(step2[25], step2[22], cospi_16_64, cospi_16_64, &out[22], &out[25]);
  619. butterfly(step2[24], step2[23], cospi_16_64, cospi_16_64, &out[23], &out[24]);
  620. }
  621. void idct4_sse2(__m128i *const in);
  622. void vpx_idct8_sse2(__m128i *const in);
  623. void idct16_sse2(__m128i *const in0, __m128i *const in1);
  624. void iadst4_sse2(__m128i *const in);
  625. void iadst8_sse2(__m128i *const in);
  626. void vpx_iadst16_8col_sse2(__m128i *const in);
  627. void iadst16_sse2(__m128i *const in0, __m128i *const in1);
  628. void idct32_1024_8x32(const __m128i *const in, __m128i *const out);
  629. void idct32_34_8x32_sse2(const __m128i *const in, __m128i *const out);
  630. void idct32_34_8x32_ssse3(const __m128i *const in, __m128i *const out);
  631. #endif // VPX_VPX_DSP_X86_INV_TXFM_SSE2_H_