vp9_dct_intrin_sse2.c 81 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045
  1. /*
  2. * Copyright (c) 2012 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. #include <assert.h>
  11. #include <emmintrin.h> // SSE2
  12. #include "./vp9_rtcd.h"
  13. #include "./vpx_dsp_rtcd.h"
  14. #include "vpx_dsp/txfm_common.h"
  15. #include "vpx_dsp/x86/fwd_txfm_sse2.h"
  16. #include "vpx_dsp/x86/txfm_common_sse2.h"
  17. #include "vpx_ports/mem.h"
  18. static INLINE void load_buffer_4x4(const int16_t *input, __m128i *in,
  19. int stride) {
  20. const __m128i k__nonzero_bias_a = _mm_setr_epi16(0, 1, 1, 1, 1, 1, 1, 1);
  21. const __m128i k__nonzero_bias_b = _mm_setr_epi16(1, 0, 0, 0, 0, 0, 0, 0);
  22. __m128i mask;
  23. in[0] = _mm_loadl_epi64((const __m128i *)(input + 0 * stride));
  24. in[1] = _mm_loadl_epi64((const __m128i *)(input + 1 * stride));
  25. in[2] = _mm_loadl_epi64((const __m128i *)(input + 2 * stride));
  26. in[3] = _mm_loadl_epi64((const __m128i *)(input + 3 * stride));
  27. in[0] = _mm_slli_epi16(in[0], 4);
  28. in[1] = _mm_slli_epi16(in[1], 4);
  29. in[2] = _mm_slli_epi16(in[2], 4);
  30. in[3] = _mm_slli_epi16(in[3], 4);
  31. mask = _mm_cmpeq_epi16(in[0], k__nonzero_bias_a);
  32. in[0] = _mm_add_epi16(in[0], mask);
  33. in[0] = _mm_add_epi16(in[0], k__nonzero_bias_b);
  34. }
  35. static INLINE void write_buffer_4x4(tran_low_t *output, __m128i *res) {
  36. const __m128i kOne = _mm_set1_epi16(1);
  37. __m128i in01 = _mm_unpacklo_epi64(res[0], res[1]);
  38. __m128i in23 = _mm_unpacklo_epi64(res[2], res[3]);
  39. __m128i out01 = _mm_add_epi16(in01, kOne);
  40. __m128i out23 = _mm_add_epi16(in23, kOne);
  41. out01 = _mm_srai_epi16(out01, 2);
  42. out23 = _mm_srai_epi16(out23, 2);
  43. store_output(&out01, (output + 0 * 8));
  44. store_output(&out23, (output + 1 * 8));
  45. }
  46. static INLINE void transpose_4x4(__m128i *res) {
  47. // Combine and transpose
  48. // 00 01 02 03 20 21 22 23
  49. // 10 11 12 13 30 31 32 33
  50. const __m128i tr0_0 = _mm_unpacklo_epi16(res[0], res[1]);
  51. const __m128i tr0_1 = _mm_unpackhi_epi16(res[0], res[1]);
  52. // 00 10 01 11 02 12 03 13
  53. // 20 30 21 31 22 32 23 33
  54. res[0] = _mm_unpacklo_epi32(tr0_0, tr0_1);
  55. res[2] = _mm_unpackhi_epi32(tr0_0, tr0_1);
  56. // 00 10 20 30 01 11 21 31
  57. // 02 12 22 32 03 13 23 33
  58. // only use the first 4 16-bit integers
  59. res[1] = _mm_unpackhi_epi64(res[0], res[0]);
  60. res[3] = _mm_unpackhi_epi64(res[2], res[2]);
  61. }
  62. static void fdct4_sse2(__m128i *in) {
  63. const __m128i k__cospi_p16_p16 = _mm_set1_epi16((int16_t)cospi_16_64);
  64. const __m128i k__cospi_p16_m16 = pair_set_epi16(cospi_16_64, -cospi_16_64);
  65. const __m128i k__cospi_p08_p24 = pair_set_epi16(cospi_8_64, cospi_24_64);
  66. const __m128i k__cospi_p24_m08 = pair_set_epi16(cospi_24_64, -cospi_8_64);
  67. const __m128i k__DCT_CONST_ROUNDING = _mm_set1_epi32(DCT_CONST_ROUNDING);
  68. __m128i u[4], v[4];
  69. u[0] = _mm_unpacklo_epi16(in[0], in[1]);
  70. u[1] = _mm_unpacklo_epi16(in[3], in[2]);
  71. v[0] = _mm_add_epi16(u[0], u[1]);
  72. v[1] = _mm_sub_epi16(u[0], u[1]);
  73. u[0] = _mm_madd_epi16(v[0], k__cospi_p16_p16); // 0
  74. u[1] = _mm_madd_epi16(v[0], k__cospi_p16_m16); // 2
  75. u[2] = _mm_madd_epi16(v[1], k__cospi_p08_p24); // 1
  76. u[3] = _mm_madd_epi16(v[1], k__cospi_p24_m08); // 3
  77. v[0] = _mm_add_epi32(u[0], k__DCT_CONST_ROUNDING);
  78. v[1] = _mm_add_epi32(u[1], k__DCT_CONST_ROUNDING);
  79. v[2] = _mm_add_epi32(u[2], k__DCT_CONST_ROUNDING);
  80. v[3] = _mm_add_epi32(u[3], k__DCT_CONST_ROUNDING);
  81. u[0] = _mm_srai_epi32(v[0], DCT_CONST_BITS);
  82. u[1] = _mm_srai_epi32(v[1], DCT_CONST_BITS);
  83. u[2] = _mm_srai_epi32(v[2], DCT_CONST_BITS);
  84. u[3] = _mm_srai_epi32(v[3], DCT_CONST_BITS);
  85. in[0] = _mm_packs_epi32(u[0], u[1]);
  86. in[1] = _mm_packs_epi32(u[2], u[3]);
  87. transpose_4x4(in);
  88. }
  89. static void fadst4_sse2(__m128i *in) {
  90. const __m128i k__sinpi_p01_p02 = pair_set_epi16(sinpi_1_9, sinpi_2_9);
  91. const __m128i k__sinpi_p04_m01 = pair_set_epi16(sinpi_4_9, -sinpi_1_9);
  92. const __m128i k__sinpi_p03_p04 = pair_set_epi16(sinpi_3_9, sinpi_4_9);
  93. const __m128i k__sinpi_m03_p02 = pair_set_epi16(-sinpi_3_9, sinpi_2_9);
  94. const __m128i k__sinpi_p03_p03 = _mm_set1_epi16((int16_t)sinpi_3_9);
  95. const __m128i kZero = _mm_set1_epi16(0);
  96. const __m128i k__DCT_CONST_ROUNDING = _mm_set1_epi32(DCT_CONST_ROUNDING);
  97. __m128i u[8], v[8];
  98. __m128i in7 = _mm_add_epi16(in[0], in[1]);
  99. u[0] = _mm_unpacklo_epi16(in[0], in[1]);
  100. u[1] = _mm_unpacklo_epi16(in[2], in[3]);
  101. u[2] = _mm_unpacklo_epi16(in7, kZero);
  102. u[3] = _mm_unpacklo_epi16(in[2], kZero);
  103. u[4] = _mm_unpacklo_epi16(in[3], kZero);
  104. v[0] = _mm_madd_epi16(u[0], k__sinpi_p01_p02); // s0 + s2
  105. v[1] = _mm_madd_epi16(u[1], k__sinpi_p03_p04); // s4 + s5
  106. v[2] = _mm_madd_epi16(u[2], k__sinpi_p03_p03); // x1
  107. v[3] = _mm_madd_epi16(u[0], k__sinpi_p04_m01); // s1 - s3
  108. v[4] = _mm_madd_epi16(u[1], k__sinpi_m03_p02); // -s4 + s6
  109. v[5] = _mm_madd_epi16(u[3], k__sinpi_p03_p03); // s4
  110. v[6] = _mm_madd_epi16(u[4], k__sinpi_p03_p03);
  111. u[0] = _mm_add_epi32(v[0], v[1]);
  112. u[1] = _mm_sub_epi32(v[2], v[6]);
  113. u[2] = _mm_add_epi32(v[3], v[4]);
  114. u[3] = _mm_sub_epi32(u[2], u[0]);
  115. u[4] = _mm_slli_epi32(v[5], 2);
  116. u[5] = _mm_sub_epi32(u[4], v[5]);
  117. u[6] = _mm_add_epi32(u[3], u[5]);
  118. v[0] = _mm_add_epi32(u[0], k__DCT_CONST_ROUNDING);
  119. v[1] = _mm_add_epi32(u[1], k__DCT_CONST_ROUNDING);
  120. v[2] = _mm_add_epi32(u[2], k__DCT_CONST_ROUNDING);
  121. v[3] = _mm_add_epi32(u[6], k__DCT_CONST_ROUNDING);
  122. u[0] = _mm_srai_epi32(v[0], DCT_CONST_BITS);
  123. u[1] = _mm_srai_epi32(v[1], DCT_CONST_BITS);
  124. u[2] = _mm_srai_epi32(v[2], DCT_CONST_BITS);
  125. u[3] = _mm_srai_epi32(v[3], DCT_CONST_BITS);
  126. in[0] = _mm_packs_epi32(u[0], u[2]);
  127. in[1] = _mm_packs_epi32(u[1], u[3]);
  128. transpose_4x4(in);
  129. }
  130. void vp9_fht4x4_sse2(const int16_t *input, tran_low_t *output, int stride,
  131. int tx_type) {
  132. __m128i in[4];
  133. switch (tx_type) {
  134. case DCT_DCT: vpx_fdct4x4_sse2(input, output, stride); break;
  135. case ADST_DCT:
  136. load_buffer_4x4(input, in, stride);
  137. fadst4_sse2(in);
  138. fdct4_sse2(in);
  139. write_buffer_4x4(output, in);
  140. break;
  141. case DCT_ADST:
  142. load_buffer_4x4(input, in, stride);
  143. fdct4_sse2(in);
  144. fadst4_sse2(in);
  145. write_buffer_4x4(output, in);
  146. break;
  147. case ADST_ADST:
  148. load_buffer_4x4(input, in, stride);
  149. fadst4_sse2(in);
  150. fadst4_sse2(in);
  151. write_buffer_4x4(output, in);
  152. break;
  153. default: assert(0); break;
  154. }
  155. }
  156. void vp9_fdct8x8_quant_sse2(const int16_t *input, int stride,
  157. int16_t *coeff_ptr, intptr_t n_coeffs,
  158. int skip_block, const int16_t *zbin_ptr,
  159. const int16_t *round_ptr, const int16_t *quant_ptr,
  160. const int16_t *quant_shift_ptr, int16_t *qcoeff_ptr,
  161. int16_t *dqcoeff_ptr, const int16_t *dequant_ptr,
  162. uint16_t *eob_ptr, const int16_t *scan_ptr,
  163. const int16_t *iscan_ptr) {
  164. __m128i zero;
  165. int pass;
  166. // Constants
  167. // When we use them, in one case, they are all the same. In all others
  168. // it's a pair of them that we need to repeat four times. This is done
  169. // by constructing the 32 bit constant corresponding to that pair.
  170. const __m128i k__cospi_p16_p16 = _mm_set1_epi16((int16_t)cospi_16_64);
  171. const __m128i k__cospi_p16_m16 = pair_set_epi16(cospi_16_64, -cospi_16_64);
  172. const __m128i k__cospi_p24_p08 = pair_set_epi16(cospi_24_64, cospi_8_64);
  173. const __m128i k__cospi_m08_p24 = pair_set_epi16(-cospi_8_64, cospi_24_64);
  174. const __m128i k__cospi_p28_p04 = pair_set_epi16(cospi_28_64, cospi_4_64);
  175. const __m128i k__cospi_m04_p28 = pair_set_epi16(-cospi_4_64, cospi_28_64);
  176. const __m128i k__cospi_p12_p20 = pair_set_epi16(cospi_12_64, cospi_20_64);
  177. const __m128i k__cospi_m20_p12 = pair_set_epi16(-cospi_20_64, cospi_12_64);
  178. const __m128i k__DCT_CONST_ROUNDING = _mm_set1_epi32(DCT_CONST_ROUNDING);
  179. // Load input
  180. __m128i in0 = _mm_load_si128((const __m128i *)(input + 0 * stride));
  181. __m128i in1 = _mm_load_si128((const __m128i *)(input + 1 * stride));
  182. __m128i in2 = _mm_load_si128((const __m128i *)(input + 2 * stride));
  183. __m128i in3 = _mm_load_si128((const __m128i *)(input + 3 * stride));
  184. __m128i in4 = _mm_load_si128((const __m128i *)(input + 4 * stride));
  185. __m128i in5 = _mm_load_si128((const __m128i *)(input + 5 * stride));
  186. __m128i in6 = _mm_load_si128((const __m128i *)(input + 6 * stride));
  187. __m128i in7 = _mm_load_si128((const __m128i *)(input + 7 * stride));
  188. __m128i *in[8];
  189. int index = 0;
  190. (void)scan_ptr;
  191. (void)zbin_ptr;
  192. (void)quant_shift_ptr;
  193. (void)coeff_ptr;
  194. // Pre-condition input (shift by two)
  195. in0 = _mm_slli_epi16(in0, 2);
  196. in1 = _mm_slli_epi16(in1, 2);
  197. in2 = _mm_slli_epi16(in2, 2);
  198. in3 = _mm_slli_epi16(in3, 2);
  199. in4 = _mm_slli_epi16(in4, 2);
  200. in5 = _mm_slli_epi16(in5, 2);
  201. in6 = _mm_slli_epi16(in6, 2);
  202. in7 = _mm_slli_epi16(in7, 2);
  203. in[0] = &in0;
  204. in[1] = &in1;
  205. in[2] = &in2;
  206. in[3] = &in3;
  207. in[4] = &in4;
  208. in[5] = &in5;
  209. in[6] = &in6;
  210. in[7] = &in7;
  211. // We do two passes, first the columns, then the rows. The results of the
  212. // first pass are transposed so that the same column code can be reused. The
  213. // results of the second pass are also transposed so that the rows (processed
  214. // as columns) are put back in row positions.
  215. for (pass = 0; pass < 2; pass++) {
  216. // To store results of each pass before the transpose.
  217. __m128i res0, res1, res2, res3, res4, res5, res6, res7;
  218. // Add/subtract
  219. const __m128i q0 = _mm_add_epi16(in0, in7);
  220. const __m128i q1 = _mm_add_epi16(in1, in6);
  221. const __m128i q2 = _mm_add_epi16(in2, in5);
  222. const __m128i q3 = _mm_add_epi16(in3, in4);
  223. const __m128i q4 = _mm_sub_epi16(in3, in4);
  224. const __m128i q5 = _mm_sub_epi16(in2, in5);
  225. const __m128i q6 = _mm_sub_epi16(in1, in6);
  226. const __m128i q7 = _mm_sub_epi16(in0, in7);
  227. // Work on first four results
  228. {
  229. // Add/subtract
  230. const __m128i r0 = _mm_add_epi16(q0, q3);
  231. const __m128i r1 = _mm_add_epi16(q1, q2);
  232. const __m128i r2 = _mm_sub_epi16(q1, q2);
  233. const __m128i r3 = _mm_sub_epi16(q0, q3);
  234. // Interleave to do the multiply by constants which gets us into 32bits
  235. const __m128i t0 = _mm_unpacklo_epi16(r0, r1);
  236. const __m128i t1 = _mm_unpackhi_epi16(r0, r1);
  237. const __m128i t2 = _mm_unpacklo_epi16(r2, r3);
  238. const __m128i t3 = _mm_unpackhi_epi16(r2, r3);
  239. const __m128i u0 = _mm_madd_epi16(t0, k__cospi_p16_p16);
  240. const __m128i u1 = _mm_madd_epi16(t1, k__cospi_p16_p16);
  241. const __m128i u2 = _mm_madd_epi16(t0, k__cospi_p16_m16);
  242. const __m128i u3 = _mm_madd_epi16(t1, k__cospi_p16_m16);
  243. const __m128i u4 = _mm_madd_epi16(t2, k__cospi_p24_p08);
  244. const __m128i u5 = _mm_madd_epi16(t3, k__cospi_p24_p08);
  245. const __m128i u6 = _mm_madd_epi16(t2, k__cospi_m08_p24);
  246. const __m128i u7 = _mm_madd_epi16(t3, k__cospi_m08_p24);
  247. // dct_const_round_shift
  248. const __m128i v0 = _mm_add_epi32(u0, k__DCT_CONST_ROUNDING);
  249. const __m128i v1 = _mm_add_epi32(u1, k__DCT_CONST_ROUNDING);
  250. const __m128i v2 = _mm_add_epi32(u2, k__DCT_CONST_ROUNDING);
  251. const __m128i v3 = _mm_add_epi32(u3, k__DCT_CONST_ROUNDING);
  252. const __m128i v4 = _mm_add_epi32(u4, k__DCT_CONST_ROUNDING);
  253. const __m128i v5 = _mm_add_epi32(u5, k__DCT_CONST_ROUNDING);
  254. const __m128i v6 = _mm_add_epi32(u6, k__DCT_CONST_ROUNDING);
  255. const __m128i v7 = _mm_add_epi32(u7, k__DCT_CONST_ROUNDING);
  256. const __m128i w0 = _mm_srai_epi32(v0, DCT_CONST_BITS);
  257. const __m128i w1 = _mm_srai_epi32(v1, DCT_CONST_BITS);
  258. const __m128i w2 = _mm_srai_epi32(v2, DCT_CONST_BITS);
  259. const __m128i w3 = _mm_srai_epi32(v3, DCT_CONST_BITS);
  260. const __m128i w4 = _mm_srai_epi32(v4, DCT_CONST_BITS);
  261. const __m128i w5 = _mm_srai_epi32(v5, DCT_CONST_BITS);
  262. const __m128i w6 = _mm_srai_epi32(v6, DCT_CONST_BITS);
  263. const __m128i w7 = _mm_srai_epi32(v7, DCT_CONST_BITS);
  264. // Combine
  265. res0 = _mm_packs_epi32(w0, w1);
  266. res4 = _mm_packs_epi32(w2, w3);
  267. res2 = _mm_packs_epi32(w4, w5);
  268. res6 = _mm_packs_epi32(w6, w7);
  269. }
  270. // Work on next four results
  271. {
  272. // Interleave to do the multiply by constants which gets us into 32bits
  273. const __m128i d0 = _mm_unpacklo_epi16(q6, q5);
  274. const __m128i d1 = _mm_unpackhi_epi16(q6, q5);
  275. const __m128i e0 = _mm_madd_epi16(d0, k__cospi_p16_m16);
  276. const __m128i e1 = _mm_madd_epi16(d1, k__cospi_p16_m16);
  277. const __m128i e2 = _mm_madd_epi16(d0, k__cospi_p16_p16);
  278. const __m128i e3 = _mm_madd_epi16(d1, k__cospi_p16_p16);
  279. // dct_const_round_shift
  280. const __m128i f0 = _mm_add_epi32(e0, k__DCT_CONST_ROUNDING);
  281. const __m128i f1 = _mm_add_epi32(e1, k__DCT_CONST_ROUNDING);
  282. const __m128i f2 = _mm_add_epi32(e2, k__DCT_CONST_ROUNDING);
  283. const __m128i f3 = _mm_add_epi32(e3, k__DCT_CONST_ROUNDING);
  284. const __m128i s0 = _mm_srai_epi32(f0, DCT_CONST_BITS);
  285. const __m128i s1 = _mm_srai_epi32(f1, DCT_CONST_BITS);
  286. const __m128i s2 = _mm_srai_epi32(f2, DCT_CONST_BITS);
  287. const __m128i s3 = _mm_srai_epi32(f3, DCT_CONST_BITS);
  288. // Combine
  289. const __m128i r0 = _mm_packs_epi32(s0, s1);
  290. const __m128i r1 = _mm_packs_epi32(s2, s3);
  291. // Add/subtract
  292. const __m128i x0 = _mm_add_epi16(q4, r0);
  293. const __m128i x1 = _mm_sub_epi16(q4, r0);
  294. const __m128i x2 = _mm_sub_epi16(q7, r1);
  295. const __m128i x3 = _mm_add_epi16(q7, r1);
  296. // Interleave to do the multiply by constants which gets us into 32bits
  297. const __m128i t0 = _mm_unpacklo_epi16(x0, x3);
  298. const __m128i t1 = _mm_unpackhi_epi16(x0, x3);
  299. const __m128i t2 = _mm_unpacklo_epi16(x1, x2);
  300. const __m128i t3 = _mm_unpackhi_epi16(x1, x2);
  301. const __m128i u0 = _mm_madd_epi16(t0, k__cospi_p28_p04);
  302. const __m128i u1 = _mm_madd_epi16(t1, k__cospi_p28_p04);
  303. const __m128i u2 = _mm_madd_epi16(t0, k__cospi_m04_p28);
  304. const __m128i u3 = _mm_madd_epi16(t1, k__cospi_m04_p28);
  305. const __m128i u4 = _mm_madd_epi16(t2, k__cospi_p12_p20);
  306. const __m128i u5 = _mm_madd_epi16(t3, k__cospi_p12_p20);
  307. const __m128i u6 = _mm_madd_epi16(t2, k__cospi_m20_p12);
  308. const __m128i u7 = _mm_madd_epi16(t3, k__cospi_m20_p12);
  309. // dct_const_round_shift
  310. const __m128i v0 = _mm_add_epi32(u0, k__DCT_CONST_ROUNDING);
  311. const __m128i v1 = _mm_add_epi32(u1, k__DCT_CONST_ROUNDING);
  312. const __m128i v2 = _mm_add_epi32(u2, k__DCT_CONST_ROUNDING);
  313. const __m128i v3 = _mm_add_epi32(u3, k__DCT_CONST_ROUNDING);
  314. const __m128i v4 = _mm_add_epi32(u4, k__DCT_CONST_ROUNDING);
  315. const __m128i v5 = _mm_add_epi32(u5, k__DCT_CONST_ROUNDING);
  316. const __m128i v6 = _mm_add_epi32(u6, k__DCT_CONST_ROUNDING);
  317. const __m128i v7 = _mm_add_epi32(u7, k__DCT_CONST_ROUNDING);
  318. const __m128i w0 = _mm_srai_epi32(v0, DCT_CONST_BITS);
  319. const __m128i w1 = _mm_srai_epi32(v1, DCT_CONST_BITS);
  320. const __m128i w2 = _mm_srai_epi32(v2, DCT_CONST_BITS);
  321. const __m128i w3 = _mm_srai_epi32(v3, DCT_CONST_BITS);
  322. const __m128i w4 = _mm_srai_epi32(v4, DCT_CONST_BITS);
  323. const __m128i w5 = _mm_srai_epi32(v5, DCT_CONST_BITS);
  324. const __m128i w6 = _mm_srai_epi32(v6, DCT_CONST_BITS);
  325. const __m128i w7 = _mm_srai_epi32(v7, DCT_CONST_BITS);
  326. // Combine
  327. res1 = _mm_packs_epi32(w0, w1);
  328. res7 = _mm_packs_epi32(w2, w3);
  329. res5 = _mm_packs_epi32(w4, w5);
  330. res3 = _mm_packs_epi32(w6, w7);
  331. }
  332. // Transpose the 8x8.
  333. {
  334. // 00 01 02 03 04 05 06 07
  335. // 10 11 12 13 14 15 16 17
  336. // 20 21 22 23 24 25 26 27
  337. // 30 31 32 33 34 35 36 37
  338. // 40 41 42 43 44 45 46 47
  339. // 50 51 52 53 54 55 56 57
  340. // 60 61 62 63 64 65 66 67
  341. // 70 71 72 73 74 75 76 77
  342. const __m128i tr0_0 = _mm_unpacklo_epi16(res0, res1);
  343. const __m128i tr0_1 = _mm_unpacklo_epi16(res2, res3);
  344. const __m128i tr0_2 = _mm_unpackhi_epi16(res0, res1);
  345. const __m128i tr0_3 = _mm_unpackhi_epi16(res2, res3);
  346. const __m128i tr0_4 = _mm_unpacklo_epi16(res4, res5);
  347. const __m128i tr0_5 = _mm_unpacklo_epi16(res6, res7);
  348. const __m128i tr0_6 = _mm_unpackhi_epi16(res4, res5);
  349. const __m128i tr0_7 = _mm_unpackhi_epi16(res6, res7);
  350. // 00 10 01 11 02 12 03 13
  351. // 20 30 21 31 22 32 23 33
  352. // 04 14 05 15 06 16 07 17
  353. // 24 34 25 35 26 36 27 37
  354. // 40 50 41 51 42 52 43 53
  355. // 60 70 61 71 62 72 63 73
  356. // 54 54 55 55 56 56 57 57
  357. // 64 74 65 75 66 76 67 77
  358. const __m128i tr1_0 = _mm_unpacklo_epi32(tr0_0, tr0_1);
  359. const __m128i tr1_1 = _mm_unpacklo_epi32(tr0_2, tr0_3);
  360. const __m128i tr1_2 = _mm_unpackhi_epi32(tr0_0, tr0_1);
  361. const __m128i tr1_3 = _mm_unpackhi_epi32(tr0_2, tr0_3);
  362. const __m128i tr1_4 = _mm_unpacklo_epi32(tr0_4, tr0_5);
  363. const __m128i tr1_5 = _mm_unpacklo_epi32(tr0_6, tr0_7);
  364. const __m128i tr1_6 = _mm_unpackhi_epi32(tr0_4, tr0_5);
  365. const __m128i tr1_7 = _mm_unpackhi_epi32(tr0_6, tr0_7);
  366. // 00 10 20 30 01 11 21 31
  367. // 40 50 60 70 41 51 61 71
  368. // 02 12 22 32 03 13 23 33
  369. // 42 52 62 72 43 53 63 73
  370. // 04 14 24 34 05 15 21 36
  371. // 44 54 64 74 45 55 61 76
  372. // 06 16 26 36 07 17 27 37
  373. // 46 56 66 76 47 57 67 77
  374. in0 = _mm_unpacklo_epi64(tr1_0, tr1_4);
  375. in1 = _mm_unpackhi_epi64(tr1_0, tr1_4);
  376. in2 = _mm_unpacklo_epi64(tr1_2, tr1_6);
  377. in3 = _mm_unpackhi_epi64(tr1_2, tr1_6);
  378. in4 = _mm_unpacklo_epi64(tr1_1, tr1_5);
  379. in5 = _mm_unpackhi_epi64(tr1_1, tr1_5);
  380. in6 = _mm_unpacklo_epi64(tr1_3, tr1_7);
  381. in7 = _mm_unpackhi_epi64(tr1_3, tr1_7);
  382. // 00 10 20 30 40 50 60 70
  383. // 01 11 21 31 41 51 61 71
  384. // 02 12 22 32 42 52 62 72
  385. // 03 13 23 33 43 53 63 73
  386. // 04 14 24 34 44 54 64 74
  387. // 05 15 25 35 45 55 65 75
  388. // 06 16 26 36 46 56 66 76
  389. // 07 17 27 37 47 57 67 77
  390. }
  391. }
  392. // Post-condition output and store it
  393. {
  394. // Post-condition (division by two)
  395. // division of two 16 bits signed numbers using shifts
  396. // n / 2 = (n - (n >> 15)) >> 1
  397. const __m128i sign_in0 = _mm_srai_epi16(in0, 15);
  398. const __m128i sign_in1 = _mm_srai_epi16(in1, 15);
  399. const __m128i sign_in2 = _mm_srai_epi16(in2, 15);
  400. const __m128i sign_in3 = _mm_srai_epi16(in3, 15);
  401. const __m128i sign_in4 = _mm_srai_epi16(in4, 15);
  402. const __m128i sign_in5 = _mm_srai_epi16(in5, 15);
  403. const __m128i sign_in6 = _mm_srai_epi16(in6, 15);
  404. const __m128i sign_in7 = _mm_srai_epi16(in7, 15);
  405. in0 = _mm_sub_epi16(in0, sign_in0);
  406. in1 = _mm_sub_epi16(in1, sign_in1);
  407. in2 = _mm_sub_epi16(in2, sign_in2);
  408. in3 = _mm_sub_epi16(in3, sign_in3);
  409. in4 = _mm_sub_epi16(in4, sign_in4);
  410. in5 = _mm_sub_epi16(in5, sign_in5);
  411. in6 = _mm_sub_epi16(in6, sign_in6);
  412. in7 = _mm_sub_epi16(in7, sign_in7);
  413. in0 = _mm_srai_epi16(in0, 1);
  414. in1 = _mm_srai_epi16(in1, 1);
  415. in2 = _mm_srai_epi16(in2, 1);
  416. in3 = _mm_srai_epi16(in3, 1);
  417. in4 = _mm_srai_epi16(in4, 1);
  418. in5 = _mm_srai_epi16(in5, 1);
  419. in6 = _mm_srai_epi16(in6, 1);
  420. in7 = _mm_srai_epi16(in7, 1);
  421. }
  422. iscan_ptr += n_coeffs;
  423. qcoeff_ptr += n_coeffs;
  424. dqcoeff_ptr += n_coeffs;
  425. n_coeffs = -n_coeffs;
  426. zero = _mm_setzero_si128();
  427. if (!skip_block) {
  428. __m128i eob;
  429. __m128i round, quant, dequant;
  430. {
  431. __m128i coeff0, coeff1;
  432. // Setup global values
  433. {
  434. round = _mm_load_si128((const __m128i *)round_ptr);
  435. quant = _mm_load_si128((const __m128i *)quant_ptr);
  436. dequant = _mm_load_si128((const __m128i *)dequant_ptr);
  437. }
  438. {
  439. __m128i coeff0_sign, coeff1_sign;
  440. __m128i qcoeff0, qcoeff1;
  441. __m128i qtmp0, qtmp1;
  442. // Do DC and first 15 AC
  443. coeff0 = *in[0];
  444. coeff1 = *in[1];
  445. // Poor man's sign extract
  446. coeff0_sign = _mm_srai_epi16(coeff0, 15);
  447. coeff1_sign = _mm_srai_epi16(coeff1, 15);
  448. qcoeff0 = _mm_xor_si128(coeff0, coeff0_sign);
  449. qcoeff1 = _mm_xor_si128(coeff1, coeff1_sign);
  450. qcoeff0 = _mm_sub_epi16(qcoeff0, coeff0_sign);
  451. qcoeff1 = _mm_sub_epi16(qcoeff1, coeff1_sign);
  452. qcoeff0 = _mm_adds_epi16(qcoeff0, round);
  453. round = _mm_unpackhi_epi64(round, round);
  454. qcoeff1 = _mm_adds_epi16(qcoeff1, round);
  455. qtmp0 = _mm_mulhi_epi16(qcoeff0, quant);
  456. quant = _mm_unpackhi_epi64(quant, quant);
  457. qtmp1 = _mm_mulhi_epi16(qcoeff1, quant);
  458. // Reinsert signs
  459. qcoeff0 = _mm_xor_si128(qtmp0, coeff0_sign);
  460. qcoeff1 = _mm_xor_si128(qtmp1, coeff1_sign);
  461. qcoeff0 = _mm_sub_epi16(qcoeff0, coeff0_sign);
  462. qcoeff1 = _mm_sub_epi16(qcoeff1, coeff1_sign);
  463. _mm_store_si128((__m128i *)(qcoeff_ptr + n_coeffs), qcoeff0);
  464. _mm_store_si128((__m128i *)(qcoeff_ptr + n_coeffs) + 1, qcoeff1);
  465. coeff0 = _mm_mullo_epi16(qcoeff0, dequant);
  466. dequant = _mm_unpackhi_epi64(dequant, dequant);
  467. coeff1 = _mm_mullo_epi16(qcoeff1, dequant);
  468. _mm_store_si128((__m128i *)(dqcoeff_ptr + n_coeffs), coeff0);
  469. _mm_store_si128((__m128i *)(dqcoeff_ptr + n_coeffs) + 1, coeff1);
  470. }
  471. {
  472. // Scan for eob
  473. __m128i zero_coeff0, zero_coeff1;
  474. __m128i nzero_coeff0, nzero_coeff1;
  475. __m128i iscan0, iscan1;
  476. __m128i eob1;
  477. zero_coeff0 = _mm_cmpeq_epi16(coeff0, zero);
  478. zero_coeff1 = _mm_cmpeq_epi16(coeff1, zero);
  479. nzero_coeff0 = _mm_cmpeq_epi16(zero_coeff0, zero);
  480. nzero_coeff1 = _mm_cmpeq_epi16(zero_coeff1, zero);
  481. iscan0 = _mm_load_si128((const __m128i *)(iscan_ptr + n_coeffs));
  482. iscan1 = _mm_load_si128((const __m128i *)(iscan_ptr + n_coeffs) + 1);
  483. // Add one to convert from indices to counts
  484. iscan0 = _mm_sub_epi16(iscan0, nzero_coeff0);
  485. iscan1 = _mm_sub_epi16(iscan1, nzero_coeff1);
  486. eob = _mm_and_si128(iscan0, nzero_coeff0);
  487. eob1 = _mm_and_si128(iscan1, nzero_coeff1);
  488. eob = _mm_max_epi16(eob, eob1);
  489. }
  490. n_coeffs += 8 * 2;
  491. }
  492. // AC only loop
  493. index = 2;
  494. while (n_coeffs < 0) {
  495. __m128i coeff0, coeff1;
  496. {
  497. __m128i coeff0_sign, coeff1_sign;
  498. __m128i qcoeff0, qcoeff1;
  499. __m128i qtmp0, qtmp1;
  500. assert(index < (int)(sizeof(in) / sizeof(in[0])) - 1);
  501. coeff0 = *in[index];
  502. coeff1 = *in[index + 1];
  503. // Poor man's sign extract
  504. coeff0_sign = _mm_srai_epi16(coeff0, 15);
  505. coeff1_sign = _mm_srai_epi16(coeff1, 15);
  506. qcoeff0 = _mm_xor_si128(coeff0, coeff0_sign);
  507. qcoeff1 = _mm_xor_si128(coeff1, coeff1_sign);
  508. qcoeff0 = _mm_sub_epi16(qcoeff0, coeff0_sign);
  509. qcoeff1 = _mm_sub_epi16(qcoeff1, coeff1_sign);
  510. qcoeff0 = _mm_adds_epi16(qcoeff0, round);
  511. qcoeff1 = _mm_adds_epi16(qcoeff1, round);
  512. qtmp0 = _mm_mulhi_epi16(qcoeff0, quant);
  513. qtmp1 = _mm_mulhi_epi16(qcoeff1, quant);
  514. // Reinsert signs
  515. qcoeff0 = _mm_xor_si128(qtmp0, coeff0_sign);
  516. qcoeff1 = _mm_xor_si128(qtmp1, coeff1_sign);
  517. qcoeff0 = _mm_sub_epi16(qcoeff0, coeff0_sign);
  518. qcoeff1 = _mm_sub_epi16(qcoeff1, coeff1_sign);
  519. _mm_store_si128((__m128i *)(qcoeff_ptr + n_coeffs), qcoeff0);
  520. _mm_store_si128((__m128i *)(qcoeff_ptr + n_coeffs) + 1, qcoeff1);
  521. coeff0 = _mm_mullo_epi16(qcoeff0, dequant);
  522. coeff1 = _mm_mullo_epi16(qcoeff1, dequant);
  523. _mm_store_si128((__m128i *)(dqcoeff_ptr + n_coeffs), coeff0);
  524. _mm_store_si128((__m128i *)(dqcoeff_ptr + n_coeffs) + 1, coeff1);
  525. }
  526. {
  527. // Scan for eob
  528. __m128i zero_coeff0, zero_coeff1;
  529. __m128i nzero_coeff0, nzero_coeff1;
  530. __m128i iscan0, iscan1;
  531. __m128i eob0, eob1;
  532. zero_coeff0 = _mm_cmpeq_epi16(coeff0, zero);
  533. zero_coeff1 = _mm_cmpeq_epi16(coeff1, zero);
  534. nzero_coeff0 = _mm_cmpeq_epi16(zero_coeff0, zero);
  535. nzero_coeff1 = _mm_cmpeq_epi16(zero_coeff1, zero);
  536. iscan0 = _mm_load_si128((const __m128i *)(iscan_ptr + n_coeffs));
  537. iscan1 = _mm_load_si128((const __m128i *)(iscan_ptr + n_coeffs) + 1);
  538. // Add one to convert from indices to counts
  539. iscan0 = _mm_sub_epi16(iscan0, nzero_coeff0);
  540. iscan1 = _mm_sub_epi16(iscan1, nzero_coeff1);
  541. eob0 = _mm_and_si128(iscan0, nzero_coeff0);
  542. eob1 = _mm_and_si128(iscan1, nzero_coeff1);
  543. eob0 = _mm_max_epi16(eob0, eob1);
  544. eob = _mm_max_epi16(eob, eob0);
  545. }
  546. n_coeffs += 8 * 2;
  547. index += 2;
  548. }
  549. // Accumulate EOB
  550. {
  551. __m128i eob_shuffled;
  552. eob_shuffled = _mm_shuffle_epi32(eob, 0xe);
  553. eob = _mm_max_epi16(eob, eob_shuffled);
  554. eob_shuffled = _mm_shufflelo_epi16(eob, 0xe);
  555. eob = _mm_max_epi16(eob, eob_shuffled);
  556. eob_shuffled = _mm_shufflelo_epi16(eob, 0x1);
  557. eob = _mm_max_epi16(eob, eob_shuffled);
  558. *eob_ptr = _mm_extract_epi16(eob, 1);
  559. }
  560. } else {
  561. do {
  562. _mm_store_si128((__m128i *)(dqcoeff_ptr + n_coeffs), zero);
  563. _mm_store_si128((__m128i *)(dqcoeff_ptr + n_coeffs) + 1, zero);
  564. _mm_store_si128((__m128i *)(qcoeff_ptr + n_coeffs), zero);
  565. _mm_store_si128((__m128i *)(qcoeff_ptr + n_coeffs) + 1, zero);
  566. n_coeffs += 8 * 2;
  567. } while (n_coeffs < 0);
  568. *eob_ptr = 0;
  569. }
  570. }
  571. // load 8x8 array
  572. static INLINE void load_buffer_8x8(const int16_t *input, __m128i *in,
  573. int stride) {
  574. in[0] = _mm_load_si128((const __m128i *)(input + 0 * stride));
  575. in[1] = _mm_load_si128((const __m128i *)(input + 1 * stride));
  576. in[2] = _mm_load_si128((const __m128i *)(input + 2 * stride));
  577. in[3] = _mm_load_si128((const __m128i *)(input + 3 * stride));
  578. in[4] = _mm_load_si128((const __m128i *)(input + 4 * stride));
  579. in[5] = _mm_load_si128((const __m128i *)(input + 5 * stride));
  580. in[6] = _mm_load_si128((const __m128i *)(input + 6 * stride));
  581. in[7] = _mm_load_si128((const __m128i *)(input + 7 * stride));
  582. in[0] = _mm_slli_epi16(in[0], 2);
  583. in[1] = _mm_slli_epi16(in[1], 2);
  584. in[2] = _mm_slli_epi16(in[2], 2);
  585. in[3] = _mm_slli_epi16(in[3], 2);
  586. in[4] = _mm_slli_epi16(in[4], 2);
  587. in[5] = _mm_slli_epi16(in[5], 2);
  588. in[6] = _mm_slli_epi16(in[6], 2);
  589. in[7] = _mm_slli_epi16(in[7], 2);
  590. }
  591. // right shift and rounding
  592. static INLINE void right_shift_8x8(__m128i *res, const int bit) {
  593. __m128i sign0 = _mm_srai_epi16(res[0], 15);
  594. __m128i sign1 = _mm_srai_epi16(res[1], 15);
  595. __m128i sign2 = _mm_srai_epi16(res[2], 15);
  596. __m128i sign3 = _mm_srai_epi16(res[3], 15);
  597. __m128i sign4 = _mm_srai_epi16(res[4], 15);
  598. __m128i sign5 = _mm_srai_epi16(res[5], 15);
  599. __m128i sign6 = _mm_srai_epi16(res[6], 15);
  600. __m128i sign7 = _mm_srai_epi16(res[7], 15);
  601. if (bit == 2) {
  602. const __m128i const_rounding = _mm_set1_epi16(1);
  603. res[0] = _mm_add_epi16(res[0], const_rounding);
  604. res[1] = _mm_add_epi16(res[1], const_rounding);
  605. res[2] = _mm_add_epi16(res[2], const_rounding);
  606. res[3] = _mm_add_epi16(res[3], const_rounding);
  607. res[4] = _mm_add_epi16(res[4], const_rounding);
  608. res[5] = _mm_add_epi16(res[5], const_rounding);
  609. res[6] = _mm_add_epi16(res[6], const_rounding);
  610. res[7] = _mm_add_epi16(res[7], const_rounding);
  611. }
  612. res[0] = _mm_sub_epi16(res[0], sign0);
  613. res[1] = _mm_sub_epi16(res[1], sign1);
  614. res[2] = _mm_sub_epi16(res[2], sign2);
  615. res[3] = _mm_sub_epi16(res[3], sign3);
  616. res[4] = _mm_sub_epi16(res[4], sign4);
  617. res[5] = _mm_sub_epi16(res[5], sign5);
  618. res[6] = _mm_sub_epi16(res[6], sign6);
  619. res[7] = _mm_sub_epi16(res[7], sign7);
  620. if (bit == 1) {
  621. res[0] = _mm_srai_epi16(res[0], 1);
  622. res[1] = _mm_srai_epi16(res[1], 1);
  623. res[2] = _mm_srai_epi16(res[2], 1);
  624. res[3] = _mm_srai_epi16(res[3], 1);
  625. res[4] = _mm_srai_epi16(res[4], 1);
  626. res[5] = _mm_srai_epi16(res[5], 1);
  627. res[6] = _mm_srai_epi16(res[6], 1);
  628. res[7] = _mm_srai_epi16(res[7], 1);
  629. } else {
  630. res[0] = _mm_srai_epi16(res[0], 2);
  631. res[1] = _mm_srai_epi16(res[1], 2);
  632. res[2] = _mm_srai_epi16(res[2], 2);
  633. res[3] = _mm_srai_epi16(res[3], 2);
  634. res[4] = _mm_srai_epi16(res[4], 2);
  635. res[5] = _mm_srai_epi16(res[5], 2);
  636. res[6] = _mm_srai_epi16(res[6], 2);
  637. res[7] = _mm_srai_epi16(res[7], 2);
  638. }
  639. }
  640. // write 8x8 array
  641. static INLINE void write_buffer_8x8(tran_low_t *output, __m128i *res,
  642. int stride) {
  643. store_output(&res[0], (output + 0 * stride));
  644. store_output(&res[1], (output + 1 * stride));
  645. store_output(&res[2], (output + 2 * stride));
  646. store_output(&res[3], (output + 3 * stride));
  647. store_output(&res[4], (output + 4 * stride));
  648. store_output(&res[5], (output + 5 * stride));
  649. store_output(&res[6], (output + 6 * stride));
  650. store_output(&res[7], (output + 7 * stride));
  651. }
  652. // perform in-place transpose
  653. static INLINE void array_transpose_8x8(__m128i *in, __m128i *res) {
  654. const __m128i tr0_0 = _mm_unpacklo_epi16(in[0], in[1]);
  655. const __m128i tr0_1 = _mm_unpacklo_epi16(in[2], in[3]);
  656. const __m128i tr0_2 = _mm_unpackhi_epi16(in[0], in[1]);
  657. const __m128i tr0_3 = _mm_unpackhi_epi16(in[2], in[3]);
  658. const __m128i tr0_4 = _mm_unpacklo_epi16(in[4], in[5]);
  659. const __m128i tr0_5 = _mm_unpacklo_epi16(in[6], in[7]);
  660. const __m128i tr0_6 = _mm_unpackhi_epi16(in[4], in[5]);
  661. const __m128i tr0_7 = _mm_unpackhi_epi16(in[6], in[7]);
  662. // 00 10 01 11 02 12 03 13
  663. // 20 30 21 31 22 32 23 33
  664. // 04 14 05 15 06 16 07 17
  665. // 24 34 25 35 26 36 27 37
  666. // 40 50 41 51 42 52 43 53
  667. // 60 70 61 71 62 72 63 73
  668. // 44 54 45 55 46 56 47 57
  669. // 64 74 65 75 66 76 67 77
  670. const __m128i tr1_0 = _mm_unpacklo_epi32(tr0_0, tr0_1);
  671. const __m128i tr1_1 = _mm_unpacklo_epi32(tr0_4, tr0_5);
  672. const __m128i tr1_2 = _mm_unpackhi_epi32(tr0_0, tr0_1);
  673. const __m128i tr1_3 = _mm_unpackhi_epi32(tr0_4, tr0_5);
  674. const __m128i tr1_4 = _mm_unpacklo_epi32(tr0_2, tr0_3);
  675. const __m128i tr1_5 = _mm_unpacklo_epi32(tr0_6, tr0_7);
  676. const __m128i tr1_6 = _mm_unpackhi_epi32(tr0_2, tr0_3);
  677. const __m128i tr1_7 = _mm_unpackhi_epi32(tr0_6, tr0_7);
  678. // 00 10 20 30 01 11 21 31
  679. // 40 50 60 70 41 51 61 71
  680. // 02 12 22 32 03 13 23 33
  681. // 42 52 62 72 43 53 63 73
  682. // 04 14 24 34 05 15 25 35
  683. // 44 54 64 74 45 55 65 75
  684. // 06 16 26 36 07 17 27 37
  685. // 46 56 66 76 47 57 67 77
  686. res[0] = _mm_unpacklo_epi64(tr1_0, tr1_1);
  687. res[1] = _mm_unpackhi_epi64(tr1_0, tr1_1);
  688. res[2] = _mm_unpacklo_epi64(tr1_2, tr1_3);
  689. res[3] = _mm_unpackhi_epi64(tr1_2, tr1_3);
  690. res[4] = _mm_unpacklo_epi64(tr1_4, tr1_5);
  691. res[5] = _mm_unpackhi_epi64(tr1_4, tr1_5);
  692. res[6] = _mm_unpacklo_epi64(tr1_6, tr1_7);
  693. res[7] = _mm_unpackhi_epi64(tr1_6, tr1_7);
  694. // 00 10 20 30 40 50 60 70
  695. // 01 11 21 31 41 51 61 71
  696. // 02 12 22 32 42 52 62 72
  697. // 03 13 23 33 43 53 63 73
  698. // 04 14 24 34 44 54 64 74
  699. // 05 15 25 35 45 55 65 75
  700. // 06 16 26 36 46 56 66 76
  701. // 07 17 27 37 47 57 67 77
  702. }
  703. static void fdct8_sse2(__m128i *in) {
  704. // constants
  705. const __m128i k__cospi_p16_p16 = _mm_set1_epi16((int16_t)cospi_16_64);
  706. const __m128i k__cospi_p16_m16 = pair_set_epi16(cospi_16_64, -cospi_16_64);
  707. const __m128i k__cospi_p24_p08 = pair_set_epi16(cospi_24_64, cospi_8_64);
  708. const __m128i k__cospi_m08_p24 = pair_set_epi16(-cospi_8_64, cospi_24_64);
  709. const __m128i k__cospi_p28_p04 = pair_set_epi16(cospi_28_64, cospi_4_64);
  710. const __m128i k__cospi_m04_p28 = pair_set_epi16(-cospi_4_64, cospi_28_64);
  711. const __m128i k__cospi_p12_p20 = pair_set_epi16(cospi_12_64, cospi_20_64);
  712. const __m128i k__cospi_m20_p12 = pair_set_epi16(-cospi_20_64, cospi_12_64);
  713. const __m128i k__DCT_CONST_ROUNDING = _mm_set1_epi32(DCT_CONST_ROUNDING);
  714. __m128i u0, u1, u2, u3, u4, u5, u6, u7;
  715. __m128i v0, v1, v2, v3, v4, v5, v6, v7;
  716. __m128i s0, s1, s2, s3, s4, s5, s6, s7;
  717. // stage 1
  718. s0 = _mm_add_epi16(in[0], in[7]);
  719. s1 = _mm_add_epi16(in[1], in[6]);
  720. s2 = _mm_add_epi16(in[2], in[5]);
  721. s3 = _mm_add_epi16(in[3], in[4]);
  722. s4 = _mm_sub_epi16(in[3], in[4]);
  723. s5 = _mm_sub_epi16(in[2], in[5]);
  724. s6 = _mm_sub_epi16(in[1], in[6]);
  725. s7 = _mm_sub_epi16(in[0], in[7]);
  726. u0 = _mm_add_epi16(s0, s3);
  727. u1 = _mm_add_epi16(s1, s2);
  728. u2 = _mm_sub_epi16(s1, s2);
  729. u3 = _mm_sub_epi16(s0, s3);
  730. // interleave and perform butterfly multiplication/addition
  731. v0 = _mm_unpacklo_epi16(u0, u1);
  732. v1 = _mm_unpackhi_epi16(u0, u1);
  733. v2 = _mm_unpacklo_epi16(u2, u3);
  734. v3 = _mm_unpackhi_epi16(u2, u3);
  735. u0 = _mm_madd_epi16(v0, k__cospi_p16_p16);
  736. u1 = _mm_madd_epi16(v1, k__cospi_p16_p16);
  737. u2 = _mm_madd_epi16(v0, k__cospi_p16_m16);
  738. u3 = _mm_madd_epi16(v1, k__cospi_p16_m16);
  739. u4 = _mm_madd_epi16(v2, k__cospi_p24_p08);
  740. u5 = _mm_madd_epi16(v3, k__cospi_p24_p08);
  741. u6 = _mm_madd_epi16(v2, k__cospi_m08_p24);
  742. u7 = _mm_madd_epi16(v3, k__cospi_m08_p24);
  743. // shift and rounding
  744. v0 = _mm_add_epi32(u0, k__DCT_CONST_ROUNDING);
  745. v1 = _mm_add_epi32(u1, k__DCT_CONST_ROUNDING);
  746. v2 = _mm_add_epi32(u2, k__DCT_CONST_ROUNDING);
  747. v3 = _mm_add_epi32(u3, k__DCT_CONST_ROUNDING);
  748. v4 = _mm_add_epi32(u4, k__DCT_CONST_ROUNDING);
  749. v5 = _mm_add_epi32(u5, k__DCT_CONST_ROUNDING);
  750. v6 = _mm_add_epi32(u6, k__DCT_CONST_ROUNDING);
  751. v7 = _mm_add_epi32(u7, k__DCT_CONST_ROUNDING);
  752. u0 = _mm_srai_epi32(v0, DCT_CONST_BITS);
  753. u1 = _mm_srai_epi32(v1, DCT_CONST_BITS);
  754. u2 = _mm_srai_epi32(v2, DCT_CONST_BITS);
  755. u3 = _mm_srai_epi32(v3, DCT_CONST_BITS);
  756. u4 = _mm_srai_epi32(v4, DCT_CONST_BITS);
  757. u5 = _mm_srai_epi32(v5, DCT_CONST_BITS);
  758. u6 = _mm_srai_epi32(v6, DCT_CONST_BITS);
  759. u7 = _mm_srai_epi32(v7, DCT_CONST_BITS);
  760. in[0] = _mm_packs_epi32(u0, u1);
  761. in[2] = _mm_packs_epi32(u4, u5);
  762. in[4] = _mm_packs_epi32(u2, u3);
  763. in[6] = _mm_packs_epi32(u6, u7);
  764. // stage 2
  765. // interleave and perform butterfly multiplication/addition
  766. u0 = _mm_unpacklo_epi16(s6, s5);
  767. u1 = _mm_unpackhi_epi16(s6, s5);
  768. v0 = _mm_madd_epi16(u0, k__cospi_p16_m16);
  769. v1 = _mm_madd_epi16(u1, k__cospi_p16_m16);
  770. v2 = _mm_madd_epi16(u0, k__cospi_p16_p16);
  771. v3 = _mm_madd_epi16(u1, k__cospi_p16_p16);
  772. // shift and rounding
  773. u0 = _mm_add_epi32(v0, k__DCT_CONST_ROUNDING);
  774. u1 = _mm_add_epi32(v1, k__DCT_CONST_ROUNDING);
  775. u2 = _mm_add_epi32(v2, k__DCT_CONST_ROUNDING);
  776. u3 = _mm_add_epi32(v3, k__DCT_CONST_ROUNDING);
  777. v0 = _mm_srai_epi32(u0, DCT_CONST_BITS);
  778. v1 = _mm_srai_epi32(u1, DCT_CONST_BITS);
  779. v2 = _mm_srai_epi32(u2, DCT_CONST_BITS);
  780. v3 = _mm_srai_epi32(u3, DCT_CONST_BITS);
  781. u0 = _mm_packs_epi32(v0, v1);
  782. u1 = _mm_packs_epi32(v2, v3);
  783. // stage 3
  784. s0 = _mm_add_epi16(s4, u0);
  785. s1 = _mm_sub_epi16(s4, u0);
  786. s2 = _mm_sub_epi16(s7, u1);
  787. s3 = _mm_add_epi16(s7, u1);
  788. // stage 4
  789. u0 = _mm_unpacklo_epi16(s0, s3);
  790. u1 = _mm_unpackhi_epi16(s0, s3);
  791. u2 = _mm_unpacklo_epi16(s1, s2);
  792. u3 = _mm_unpackhi_epi16(s1, s2);
  793. v0 = _mm_madd_epi16(u0, k__cospi_p28_p04);
  794. v1 = _mm_madd_epi16(u1, k__cospi_p28_p04);
  795. v2 = _mm_madd_epi16(u2, k__cospi_p12_p20);
  796. v3 = _mm_madd_epi16(u3, k__cospi_p12_p20);
  797. v4 = _mm_madd_epi16(u2, k__cospi_m20_p12);
  798. v5 = _mm_madd_epi16(u3, k__cospi_m20_p12);
  799. v6 = _mm_madd_epi16(u0, k__cospi_m04_p28);
  800. v7 = _mm_madd_epi16(u1, k__cospi_m04_p28);
  801. // shift and rounding
  802. u0 = _mm_add_epi32(v0, k__DCT_CONST_ROUNDING);
  803. u1 = _mm_add_epi32(v1, k__DCT_CONST_ROUNDING);
  804. u2 = _mm_add_epi32(v2, k__DCT_CONST_ROUNDING);
  805. u3 = _mm_add_epi32(v3, k__DCT_CONST_ROUNDING);
  806. u4 = _mm_add_epi32(v4, k__DCT_CONST_ROUNDING);
  807. u5 = _mm_add_epi32(v5, k__DCT_CONST_ROUNDING);
  808. u6 = _mm_add_epi32(v6, k__DCT_CONST_ROUNDING);
  809. u7 = _mm_add_epi32(v7, k__DCT_CONST_ROUNDING);
  810. v0 = _mm_srai_epi32(u0, DCT_CONST_BITS);
  811. v1 = _mm_srai_epi32(u1, DCT_CONST_BITS);
  812. v2 = _mm_srai_epi32(u2, DCT_CONST_BITS);
  813. v3 = _mm_srai_epi32(u3, DCT_CONST_BITS);
  814. v4 = _mm_srai_epi32(u4, DCT_CONST_BITS);
  815. v5 = _mm_srai_epi32(u5, DCT_CONST_BITS);
  816. v6 = _mm_srai_epi32(u6, DCT_CONST_BITS);
  817. v7 = _mm_srai_epi32(u7, DCT_CONST_BITS);
  818. in[1] = _mm_packs_epi32(v0, v1);
  819. in[3] = _mm_packs_epi32(v4, v5);
  820. in[5] = _mm_packs_epi32(v2, v3);
  821. in[7] = _mm_packs_epi32(v6, v7);
  822. // transpose
  823. array_transpose_8x8(in, in);
  824. }
  825. static void fadst8_sse2(__m128i *in) {
  826. // Constants
  827. const __m128i k__cospi_p02_p30 = pair_set_epi16(cospi_2_64, cospi_30_64);
  828. const __m128i k__cospi_p30_m02 = pair_set_epi16(cospi_30_64, -cospi_2_64);
  829. const __m128i k__cospi_p10_p22 = pair_set_epi16(cospi_10_64, cospi_22_64);
  830. const __m128i k__cospi_p22_m10 = pair_set_epi16(cospi_22_64, -cospi_10_64);
  831. const __m128i k__cospi_p18_p14 = pair_set_epi16(cospi_18_64, cospi_14_64);
  832. const __m128i k__cospi_p14_m18 = pair_set_epi16(cospi_14_64, -cospi_18_64);
  833. const __m128i k__cospi_p26_p06 = pair_set_epi16(cospi_26_64, cospi_6_64);
  834. const __m128i k__cospi_p06_m26 = pair_set_epi16(cospi_6_64, -cospi_26_64);
  835. const __m128i k__cospi_p08_p24 = pair_set_epi16(cospi_8_64, cospi_24_64);
  836. const __m128i k__cospi_p24_m08 = pair_set_epi16(cospi_24_64, -cospi_8_64);
  837. const __m128i k__cospi_m24_p08 = pair_set_epi16(-cospi_24_64, cospi_8_64);
  838. const __m128i k__cospi_p16_m16 = pair_set_epi16(cospi_16_64, -cospi_16_64);
  839. const __m128i k__cospi_p16_p16 = _mm_set1_epi16((int16_t)cospi_16_64);
  840. const __m128i k__const_0 = _mm_set1_epi16(0);
  841. const __m128i k__DCT_CONST_ROUNDING = _mm_set1_epi32(DCT_CONST_ROUNDING);
  842. __m128i u0, u1, u2, u3, u4, u5, u6, u7, u8, u9, u10, u11, u12, u13, u14, u15;
  843. __m128i v0, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15;
  844. __m128i w0, w1, w2, w3, w4, w5, w6, w7, w8, w9, w10, w11, w12, w13, w14, w15;
  845. __m128i s0, s1, s2, s3, s4, s5, s6, s7;
  846. __m128i in0, in1, in2, in3, in4, in5, in6, in7;
  847. // properly aligned for butterfly input
  848. in0 = in[7];
  849. in1 = in[0];
  850. in2 = in[5];
  851. in3 = in[2];
  852. in4 = in[3];
  853. in5 = in[4];
  854. in6 = in[1];
  855. in7 = in[6];
  856. // column transformation
  857. // stage 1
  858. // interleave and multiply/add into 32-bit integer
  859. s0 = _mm_unpacklo_epi16(in0, in1);
  860. s1 = _mm_unpackhi_epi16(in0, in1);
  861. s2 = _mm_unpacklo_epi16(in2, in3);
  862. s3 = _mm_unpackhi_epi16(in2, in3);
  863. s4 = _mm_unpacklo_epi16(in4, in5);
  864. s5 = _mm_unpackhi_epi16(in4, in5);
  865. s6 = _mm_unpacklo_epi16(in6, in7);
  866. s7 = _mm_unpackhi_epi16(in6, in7);
  867. u0 = _mm_madd_epi16(s0, k__cospi_p02_p30);
  868. u1 = _mm_madd_epi16(s1, k__cospi_p02_p30);
  869. u2 = _mm_madd_epi16(s0, k__cospi_p30_m02);
  870. u3 = _mm_madd_epi16(s1, k__cospi_p30_m02);
  871. u4 = _mm_madd_epi16(s2, k__cospi_p10_p22);
  872. u5 = _mm_madd_epi16(s3, k__cospi_p10_p22);
  873. u6 = _mm_madd_epi16(s2, k__cospi_p22_m10);
  874. u7 = _mm_madd_epi16(s3, k__cospi_p22_m10);
  875. u8 = _mm_madd_epi16(s4, k__cospi_p18_p14);
  876. u9 = _mm_madd_epi16(s5, k__cospi_p18_p14);
  877. u10 = _mm_madd_epi16(s4, k__cospi_p14_m18);
  878. u11 = _mm_madd_epi16(s5, k__cospi_p14_m18);
  879. u12 = _mm_madd_epi16(s6, k__cospi_p26_p06);
  880. u13 = _mm_madd_epi16(s7, k__cospi_p26_p06);
  881. u14 = _mm_madd_epi16(s6, k__cospi_p06_m26);
  882. u15 = _mm_madd_epi16(s7, k__cospi_p06_m26);
  883. // addition
  884. w0 = _mm_add_epi32(u0, u8);
  885. w1 = _mm_add_epi32(u1, u9);
  886. w2 = _mm_add_epi32(u2, u10);
  887. w3 = _mm_add_epi32(u3, u11);
  888. w4 = _mm_add_epi32(u4, u12);
  889. w5 = _mm_add_epi32(u5, u13);
  890. w6 = _mm_add_epi32(u6, u14);
  891. w7 = _mm_add_epi32(u7, u15);
  892. w8 = _mm_sub_epi32(u0, u8);
  893. w9 = _mm_sub_epi32(u1, u9);
  894. w10 = _mm_sub_epi32(u2, u10);
  895. w11 = _mm_sub_epi32(u3, u11);
  896. w12 = _mm_sub_epi32(u4, u12);
  897. w13 = _mm_sub_epi32(u5, u13);
  898. w14 = _mm_sub_epi32(u6, u14);
  899. w15 = _mm_sub_epi32(u7, u15);
  900. // shift and rounding
  901. v0 = _mm_add_epi32(w0, k__DCT_CONST_ROUNDING);
  902. v1 = _mm_add_epi32(w1, k__DCT_CONST_ROUNDING);
  903. v2 = _mm_add_epi32(w2, k__DCT_CONST_ROUNDING);
  904. v3 = _mm_add_epi32(w3, k__DCT_CONST_ROUNDING);
  905. v4 = _mm_add_epi32(w4, k__DCT_CONST_ROUNDING);
  906. v5 = _mm_add_epi32(w5, k__DCT_CONST_ROUNDING);
  907. v6 = _mm_add_epi32(w6, k__DCT_CONST_ROUNDING);
  908. v7 = _mm_add_epi32(w7, k__DCT_CONST_ROUNDING);
  909. v8 = _mm_add_epi32(w8, k__DCT_CONST_ROUNDING);
  910. v9 = _mm_add_epi32(w9, k__DCT_CONST_ROUNDING);
  911. v10 = _mm_add_epi32(w10, k__DCT_CONST_ROUNDING);
  912. v11 = _mm_add_epi32(w11, k__DCT_CONST_ROUNDING);
  913. v12 = _mm_add_epi32(w12, k__DCT_CONST_ROUNDING);
  914. v13 = _mm_add_epi32(w13, k__DCT_CONST_ROUNDING);
  915. v14 = _mm_add_epi32(w14, k__DCT_CONST_ROUNDING);
  916. v15 = _mm_add_epi32(w15, k__DCT_CONST_ROUNDING);
  917. u0 = _mm_srai_epi32(v0, DCT_CONST_BITS);
  918. u1 = _mm_srai_epi32(v1, DCT_CONST_BITS);
  919. u2 = _mm_srai_epi32(v2, DCT_CONST_BITS);
  920. u3 = _mm_srai_epi32(v3, DCT_CONST_BITS);
  921. u4 = _mm_srai_epi32(v4, DCT_CONST_BITS);
  922. u5 = _mm_srai_epi32(v5, DCT_CONST_BITS);
  923. u6 = _mm_srai_epi32(v6, DCT_CONST_BITS);
  924. u7 = _mm_srai_epi32(v7, DCT_CONST_BITS);
  925. u8 = _mm_srai_epi32(v8, DCT_CONST_BITS);
  926. u9 = _mm_srai_epi32(v9, DCT_CONST_BITS);
  927. u10 = _mm_srai_epi32(v10, DCT_CONST_BITS);
  928. u11 = _mm_srai_epi32(v11, DCT_CONST_BITS);
  929. u12 = _mm_srai_epi32(v12, DCT_CONST_BITS);
  930. u13 = _mm_srai_epi32(v13, DCT_CONST_BITS);
  931. u14 = _mm_srai_epi32(v14, DCT_CONST_BITS);
  932. u15 = _mm_srai_epi32(v15, DCT_CONST_BITS);
  933. // back to 16-bit and pack 8 integers into __m128i
  934. in[0] = _mm_packs_epi32(u0, u1);
  935. in[1] = _mm_packs_epi32(u2, u3);
  936. in[2] = _mm_packs_epi32(u4, u5);
  937. in[3] = _mm_packs_epi32(u6, u7);
  938. in[4] = _mm_packs_epi32(u8, u9);
  939. in[5] = _mm_packs_epi32(u10, u11);
  940. in[6] = _mm_packs_epi32(u12, u13);
  941. in[7] = _mm_packs_epi32(u14, u15);
  942. // stage 2
  943. s0 = _mm_add_epi16(in[0], in[2]);
  944. s1 = _mm_add_epi16(in[1], in[3]);
  945. s2 = _mm_sub_epi16(in[0], in[2]);
  946. s3 = _mm_sub_epi16(in[1], in[3]);
  947. u0 = _mm_unpacklo_epi16(in[4], in[5]);
  948. u1 = _mm_unpackhi_epi16(in[4], in[5]);
  949. u2 = _mm_unpacklo_epi16(in[6], in[7]);
  950. u3 = _mm_unpackhi_epi16(in[6], in[7]);
  951. v0 = _mm_madd_epi16(u0, k__cospi_p08_p24);
  952. v1 = _mm_madd_epi16(u1, k__cospi_p08_p24);
  953. v2 = _mm_madd_epi16(u0, k__cospi_p24_m08);
  954. v3 = _mm_madd_epi16(u1, k__cospi_p24_m08);
  955. v4 = _mm_madd_epi16(u2, k__cospi_m24_p08);
  956. v5 = _mm_madd_epi16(u3, k__cospi_m24_p08);
  957. v6 = _mm_madd_epi16(u2, k__cospi_p08_p24);
  958. v7 = _mm_madd_epi16(u3, k__cospi_p08_p24);
  959. w0 = _mm_add_epi32(v0, v4);
  960. w1 = _mm_add_epi32(v1, v5);
  961. w2 = _mm_add_epi32(v2, v6);
  962. w3 = _mm_add_epi32(v3, v7);
  963. w4 = _mm_sub_epi32(v0, v4);
  964. w5 = _mm_sub_epi32(v1, v5);
  965. w6 = _mm_sub_epi32(v2, v6);
  966. w7 = _mm_sub_epi32(v3, v7);
  967. v0 = _mm_add_epi32(w0, k__DCT_CONST_ROUNDING);
  968. v1 = _mm_add_epi32(w1, k__DCT_CONST_ROUNDING);
  969. v2 = _mm_add_epi32(w2, k__DCT_CONST_ROUNDING);
  970. v3 = _mm_add_epi32(w3, k__DCT_CONST_ROUNDING);
  971. v4 = _mm_add_epi32(w4, k__DCT_CONST_ROUNDING);
  972. v5 = _mm_add_epi32(w5, k__DCT_CONST_ROUNDING);
  973. v6 = _mm_add_epi32(w6, k__DCT_CONST_ROUNDING);
  974. v7 = _mm_add_epi32(w7, k__DCT_CONST_ROUNDING);
  975. u0 = _mm_srai_epi32(v0, DCT_CONST_BITS);
  976. u1 = _mm_srai_epi32(v1, DCT_CONST_BITS);
  977. u2 = _mm_srai_epi32(v2, DCT_CONST_BITS);
  978. u3 = _mm_srai_epi32(v3, DCT_CONST_BITS);
  979. u4 = _mm_srai_epi32(v4, DCT_CONST_BITS);
  980. u5 = _mm_srai_epi32(v5, DCT_CONST_BITS);
  981. u6 = _mm_srai_epi32(v6, DCT_CONST_BITS);
  982. u7 = _mm_srai_epi32(v7, DCT_CONST_BITS);
  983. // back to 16-bit intergers
  984. s4 = _mm_packs_epi32(u0, u1);
  985. s5 = _mm_packs_epi32(u2, u3);
  986. s6 = _mm_packs_epi32(u4, u5);
  987. s7 = _mm_packs_epi32(u6, u7);
  988. // stage 3
  989. u0 = _mm_unpacklo_epi16(s2, s3);
  990. u1 = _mm_unpackhi_epi16(s2, s3);
  991. u2 = _mm_unpacklo_epi16(s6, s7);
  992. u3 = _mm_unpackhi_epi16(s6, s7);
  993. v0 = _mm_madd_epi16(u0, k__cospi_p16_p16);
  994. v1 = _mm_madd_epi16(u1, k__cospi_p16_p16);
  995. v2 = _mm_madd_epi16(u0, k__cospi_p16_m16);
  996. v3 = _mm_madd_epi16(u1, k__cospi_p16_m16);
  997. v4 = _mm_madd_epi16(u2, k__cospi_p16_p16);
  998. v5 = _mm_madd_epi16(u3, k__cospi_p16_p16);
  999. v6 = _mm_madd_epi16(u2, k__cospi_p16_m16);
  1000. v7 = _mm_madd_epi16(u3, k__cospi_p16_m16);
  1001. u0 = _mm_add_epi32(v0, k__DCT_CONST_ROUNDING);
  1002. u1 = _mm_add_epi32(v1, k__DCT_CONST_ROUNDING);
  1003. u2 = _mm_add_epi32(v2, k__DCT_CONST_ROUNDING);
  1004. u3 = _mm_add_epi32(v3, k__DCT_CONST_ROUNDING);
  1005. u4 = _mm_add_epi32(v4, k__DCT_CONST_ROUNDING);
  1006. u5 = _mm_add_epi32(v5, k__DCT_CONST_ROUNDING);
  1007. u6 = _mm_add_epi32(v6, k__DCT_CONST_ROUNDING);
  1008. u7 = _mm_add_epi32(v7, k__DCT_CONST_ROUNDING);
  1009. v0 = _mm_srai_epi32(u0, DCT_CONST_BITS);
  1010. v1 = _mm_srai_epi32(u1, DCT_CONST_BITS);
  1011. v2 = _mm_srai_epi32(u2, DCT_CONST_BITS);
  1012. v3 = _mm_srai_epi32(u3, DCT_CONST_BITS);
  1013. v4 = _mm_srai_epi32(u4, DCT_CONST_BITS);
  1014. v5 = _mm_srai_epi32(u5, DCT_CONST_BITS);
  1015. v6 = _mm_srai_epi32(u6, DCT_CONST_BITS);
  1016. v7 = _mm_srai_epi32(u7, DCT_CONST_BITS);
  1017. s2 = _mm_packs_epi32(v0, v1);
  1018. s3 = _mm_packs_epi32(v2, v3);
  1019. s6 = _mm_packs_epi32(v4, v5);
  1020. s7 = _mm_packs_epi32(v6, v7);
  1021. // FIXME(jingning): do subtract using bit inversion?
  1022. in[0] = s0;
  1023. in[1] = _mm_sub_epi16(k__const_0, s4);
  1024. in[2] = s6;
  1025. in[3] = _mm_sub_epi16(k__const_0, s2);
  1026. in[4] = s3;
  1027. in[5] = _mm_sub_epi16(k__const_0, s7);
  1028. in[6] = s5;
  1029. in[7] = _mm_sub_epi16(k__const_0, s1);
  1030. // transpose
  1031. array_transpose_8x8(in, in);
  1032. }
  1033. void vp9_fht8x8_sse2(const int16_t *input, tran_low_t *output, int stride,
  1034. int tx_type) {
  1035. __m128i in[8];
  1036. switch (tx_type) {
  1037. case DCT_DCT: vpx_fdct8x8_sse2(input, output, stride); break;
  1038. case ADST_DCT:
  1039. load_buffer_8x8(input, in, stride);
  1040. fadst8_sse2(in);
  1041. fdct8_sse2(in);
  1042. right_shift_8x8(in, 1);
  1043. write_buffer_8x8(output, in, 8);
  1044. break;
  1045. case DCT_ADST:
  1046. load_buffer_8x8(input, in, stride);
  1047. fdct8_sse2(in);
  1048. fadst8_sse2(in);
  1049. right_shift_8x8(in, 1);
  1050. write_buffer_8x8(output, in, 8);
  1051. break;
  1052. case ADST_ADST:
  1053. load_buffer_8x8(input, in, stride);
  1054. fadst8_sse2(in);
  1055. fadst8_sse2(in);
  1056. right_shift_8x8(in, 1);
  1057. write_buffer_8x8(output, in, 8);
  1058. break;
  1059. default: assert(0); break;
  1060. }
  1061. }
  1062. static INLINE void load_buffer_16x16(const int16_t *input, __m128i *in0,
  1063. __m128i *in1, int stride) {
  1064. // load first 8 columns
  1065. load_buffer_8x8(input, in0, stride);
  1066. load_buffer_8x8(input + 8 * stride, in0 + 8, stride);
  1067. input += 8;
  1068. // load second 8 columns
  1069. load_buffer_8x8(input, in1, stride);
  1070. load_buffer_8x8(input + 8 * stride, in1 + 8, stride);
  1071. }
  1072. static INLINE void write_buffer_16x16(tran_low_t *output, __m128i *in0,
  1073. __m128i *in1, int stride) {
  1074. // write first 8 columns
  1075. write_buffer_8x8(output, in0, stride);
  1076. write_buffer_8x8(output + 8 * stride, in0 + 8, stride);
  1077. // write second 8 columns
  1078. output += 8;
  1079. write_buffer_8x8(output, in1, stride);
  1080. write_buffer_8x8(output + 8 * stride, in1 + 8, stride);
  1081. }
  1082. static INLINE void array_transpose_16x16(__m128i *res0, __m128i *res1) {
  1083. __m128i tbuf[8];
  1084. array_transpose_8x8(res0, res0);
  1085. array_transpose_8x8(res1, tbuf);
  1086. array_transpose_8x8(res0 + 8, res1);
  1087. array_transpose_8x8(res1 + 8, res1 + 8);
  1088. res0[8] = tbuf[0];
  1089. res0[9] = tbuf[1];
  1090. res0[10] = tbuf[2];
  1091. res0[11] = tbuf[3];
  1092. res0[12] = tbuf[4];
  1093. res0[13] = tbuf[5];
  1094. res0[14] = tbuf[6];
  1095. res0[15] = tbuf[7];
  1096. }
  1097. static INLINE void right_shift_16x16(__m128i *res0, __m128i *res1) {
  1098. // perform rounding operations
  1099. right_shift_8x8(res0, 2);
  1100. right_shift_8x8(res0 + 8, 2);
  1101. right_shift_8x8(res1, 2);
  1102. right_shift_8x8(res1 + 8, 2);
  1103. }
  1104. static void fdct16_8col(__m128i *in) {
  1105. // perform 16x16 1-D DCT for 8 columns
  1106. __m128i i[8], s[8], p[8], t[8], u[16], v[16];
  1107. const __m128i k__cospi_p16_p16 = _mm_set1_epi16((int16_t)cospi_16_64);
  1108. const __m128i k__cospi_p16_m16 = pair_set_epi16(cospi_16_64, -cospi_16_64);
  1109. const __m128i k__cospi_m16_p16 = pair_set_epi16(-cospi_16_64, cospi_16_64);
  1110. const __m128i k__cospi_p24_p08 = pair_set_epi16(cospi_24_64, cospi_8_64);
  1111. const __m128i k__cospi_p08_m24 = pair_set_epi16(cospi_8_64, -cospi_24_64);
  1112. const __m128i k__cospi_m08_p24 = pair_set_epi16(-cospi_8_64, cospi_24_64);
  1113. const __m128i k__cospi_p28_p04 = pair_set_epi16(cospi_28_64, cospi_4_64);
  1114. const __m128i k__cospi_m04_p28 = pair_set_epi16(-cospi_4_64, cospi_28_64);
  1115. const __m128i k__cospi_p12_p20 = pair_set_epi16(cospi_12_64, cospi_20_64);
  1116. const __m128i k__cospi_m20_p12 = pair_set_epi16(-cospi_20_64, cospi_12_64);
  1117. const __m128i k__cospi_p30_p02 = pair_set_epi16(cospi_30_64, cospi_2_64);
  1118. const __m128i k__cospi_p14_p18 = pair_set_epi16(cospi_14_64, cospi_18_64);
  1119. const __m128i k__cospi_m02_p30 = pair_set_epi16(-cospi_2_64, cospi_30_64);
  1120. const __m128i k__cospi_m18_p14 = pair_set_epi16(-cospi_18_64, cospi_14_64);
  1121. const __m128i k__cospi_p22_p10 = pair_set_epi16(cospi_22_64, cospi_10_64);
  1122. const __m128i k__cospi_p06_p26 = pair_set_epi16(cospi_6_64, cospi_26_64);
  1123. const __m128i k__cospi_m10_p22 = pair_set_epi16(-cospi_10_64, cospi_22_64);
  1124. const __m128i k__cospi_m26_p06 = pair_set_epi16(-cospi_26_64, cospi_6_64);
  1125. const __m128i k__DCT_CONST_ROUNDING = _mm_set1_epi32(DCT_CONST_ROUNDING);
  1126. // stage 1
  1127. i[0] = _mm_add_epi16(in[0], in[15]);
  1128. i[1] = _mm_add_epi16(in[1], in[14]);
  1129. i[2] = _mm_add_epi16(in[2], in[13]);
  1130. i[3] = _mm_add_epi16(in[3], in[12]);
  1131. i[4] = _mm_add_epi16(in[4], in[11]);
  1132. i[5] = _mm_add_epi16(in[5], in[10]);
  1133. i[6] = _mm_add_epi16(in[6], in[9]);
  1134. i[7] = _mm_add_epi16(in[7], in[8]);
  1135. s[0] = _mm_sub_epi16(in[7], in[8]);
  1136. s[1] = _mm_sub_epi16(in[6], in[9]);
  1137. s[2] = _mm_sub_epi16(in[5], in[10]);
  1138. s[3] = _mm_sub_epi16(in[4], in[11]);
  1139. s[4] = _mm_sub_epi16(in[3], in[12]);
  1140. s[5] = _mm_sub_epi16(in[2], in[13]);
  1141. s[6] = _mm_sub_epi16(in[1], in[14]);
  1142. s[7] = _mm_sub_epi16(in[0], in[15]);
  1143. p[0] = _mm_add_epi16(i[0], i[7]);
  1144. p[1] = _mm_add_epi16(i[1], i[6]);
  1145. p[2] = _mm_add_epi16(i[2], i[5]);
  1146. p[3] = _mm_add_epi16(i[3], i[4]);
  1147. p[4] = _mm_sub_epi16(i[3], i[4]);
  1148. p[5] = _mm_sub_epi16(i[2], i[5]);
  1149. p[6] = _mm_sub_epi16(i[1], i[6]);
  1150. p[7] = _mm_sub_epi16(i[0], i[7]);
  1151. u[0] = _mm_add_epi16(p[0], p[3]);
  1152. u[1] = _mm_add_epi16(p[1], p[2]);
  1153. u[2] = _mm_sub_epi16(p[1], p[2]);
  1154. u[3] = _mm_sub_epi16(p[0], p[3]);
  1155. v[0] = _mm_unpacklo_epi16(u[0], u[1]);
  1156. v[1] = _mm_unpackhi_epi16(u[0], u[1]);
  1157. v[2] = _mm_unpacklo_epi16(u[2], u[3]);
  1158. v[3] = _mm_unpackhi_epi16(u[2], u[3]);
  1159. u[0] = _mm_madd_epi16(v[0], k__cospi_p16_p16);
  1160. u[1] = _mm_madd_epi16(v[1], k__cospi_p16_p16);
  1161. u[2] = _mm_madd_epi16(v[0], k__cospi_p16_m16);
  1162. u[3] = _mm_madd_epi16(v[1], k__cospi_p16_m16);
  1163. u[4] = _mm_madd_epi16(v[2], k__cospi_p24_p08);
  1164. u[5] = _mm_madd_epi16(v[3], k__cospi_p24_p08);
  1165. u[6] = _mm_madd_epi16(v[2], k__cospi_m08_p24);
  1166. u[7] = _mm_madd_epi16(v[3], k__cospi_m08_p24);
  1167. v[0] = _mm_add_epi32(u[0], k__DCT_CONST_ROUNDING);
  1168. v[1] = _mm_add_epi32(u[1], k__DCT_CONST_ROUNDING);
  1169. v[2] = _mm_add_epi32(u[2], k__DCT_CONST_ROUNDING);
  1170. v[3] = _mm_add_epi32(u[3], k__DCT_CONST_ROUNDING);
  1171. v[4] = _mm_add_epi32(u[4], k__DCT_CONST_ROUNDING);
  1172. v[5] = _mm_add_epi32(u[5], k__DCT_CONST_ROUNDING);
  1173. v[6] = _mm_add_epi32(u[6], k__DCT_CONST_ROUNDING);
  1174. v[7] = _mm_add_epi32(u[7], k__DCT_CONST_ROUNDING);
  1175. u[0] = _mm_srai_epi32(v[0], DCT_CONST_BITS);
  1176. u[1] = _mm_srai_epi32(v[1], DCT_CONST_BITS);
  1177. u[2] = _mm_srai_epi32(v[2], DCT_CONST_BITS);
  1178. u[3] = _mm_srai_epi32(v[3], DCT_CONST_BITS);
  1179. u[4] = _mm_srai_epi32(v[4], DCT_CONST_BITS);
  1180. u[5] = _mm_srai_epi32(v[5], DCT_CONST_BITS);
  1181. u[6] = _mm_srai_epi32(v[6], DCT_CONST_BITS);
  1182. u[7] = _mm_srai_epi32(v[7], DCT_CONST_BITS);
  1183. in[0] = _mm_packs_epi32(u[0], u[1]);
  1184. in[4] = _mm_packs_epi32(u[4], u[5]);
  1185. in[8] = _mm_packs_epi32(u[2], u[3]);
  1186. in[12] = _mm_packs_epi32(u[6], u[7]);
  1187. u[0] = _mm_unpacklo_epi16(p[5], p[6]);
  1188. u[1] = _mm_unpackhi_epi16(p[5], p[6]);
  1189. v[0] = _mm_madd_epi16(u[0], k__cospi_m16_p16);
  1190. v[1] = _mm_madd_epi16(u[1], k__cospi_m16_p16);
  1191. v[2] = _mm_madd_epi16(u[0], k__cospi_p16_p16);
  1192. v[3] = _mm_madd_epi16(u[1], k__cospi_p16_p16);
  1193. u[0] = _mm_add_epi32(v[0], k__DCT_CONST_ROUNDING);
  1194. u[1] = _mm_add_epi32(v[1], k__DCT_CONST_ROUNDING);
  1195. u[2] = _mm_add_epi32(v[2], k__DCT_CONST_ROUNDING);
  1196. u[3] = _mm_add_epi32(v[3], k__DCT_CONST_ROUNDING);
  1197. v[0] = _mm_srai_epi32(u[0], DCT_CONST_BITS);
  1198. v[1] = _mm_srai_epi32(u[1], DCT_CONST_BITS);
  1199. v[2] = _mm_srai_epi32(u[2], DCT_CONST_BITS);
  1200. v[3] = _mm_srai_epi32(u[3], DCT_CONST_BITS);
  1201. u[0] = _mm_packs_epi32(v[0], v[1]);
  1202. u[1] = _mm_packs_epi32(v[2], v[3]);
  1203. t[0] = _mm_add_epi16(p[4], u[0]);
  1204. t[1] = _mm_sub_epi16(p[4], u[0]);
  1205. t[2] = _mm_sub_epi16(p[7], u[1]);
  1206. t[3] = _mm_add_epi16(p[7], u[1]);
  1207. u[0] = _mm_unpacklo_epi16(t[0], t[3]);
  1208. u[1] = _mm_unpackhi_epi16(t[0], t[3]);
  1209. u[2] = _mm_unpacklo_epi16(t[1], t[2]);
  1210. u[3] = _mm_unpackhi_epi16(t[1], t[2]);
  1211. v[0] = _mm_madd_epi16(u[0], k__cospi_p28_p04);
  1212. v[1] = _mm_madd_epi16(u[1], k__cospi_p28_p04);
  1213. v[2] = _mm_madd_epi16(u[2], k__cospi_p12_p20);
  1214. v[3] = _mm_madd_epi16(u[3], k__cospi_p12_p20);
  1215. v[4] = _mm_madd_epi16(u[2], k__cospi_m20_p12);
  1216. v[5] = _mm_madd_epi16(u[3], k__cospi_m20_p12);
  1217. v[6] = _mm_madd_epi16(u[0], k__cospi_m04_p28);
  1218. v[7] = _mm_madd_epi16(u[1], k__cospi_m04_p28);
  1219. u[0] = _mm_add_epi32(v[0], k__DCT_CONST_ROUNDING);
  1220. u[1] = _mm_add_epi32(v[1], k__DCT_CONST_ROUNDING);
  1221. u[2] = _mm_add_epi32(v[2], k__DCT_CONST_ROUNDING);
  1222. u[3] = _mm_add_epi32(v[3], k__DCT_CONST_ROUNDING);
  1223. u[4] = _mm_add_epi32(v[4], k__DCT_CONST_ROUNDING);
  1224. u[5] = _mm_add_epi32(v[5], k__DCT_CONST_ROUNDING);
  1225. u[6] = _mm_add_epi32(v[6], k__DCT_CONST_ROUNDING);
  1226. u[7] = _mm_add_epi32(v[7], k__DCT_CONST_ROUNDING);
  1227. v[0] = _mm_srai_epi32(u[0], DCT_CONST_BITS);
  1228. v[1] = _mm_srai_epi32(u[1], DCT_CONST_BITS);
  1229. v[2] = _mm_srai_epi32(u[2], DCT_CONST_BITS);
  1230. v[3] = _mm_srai_epi32(u[3], DCT_CONST_BITS);
  1231. v[4] = _mm_srai_epi32(u[4], DCT_CONST_BITS);
  1232. v[5] = _mm_srai_epi32(u[5], DCT_CONST_BITS);
  1233. v[6] = _mm_srai_epi32(u[6], DCT_CONST_BITS);
  1234. v[7] = _mm_srai_epi32(u[7], DCT_CONST_BITS);
  1235. in[2] = _mm_packs_epi32(v[0], v[1]);
  1236. in[6] = _mm_packs_epi32(v[4], v[5]);
  1237. in[10] = _mm_packs_epi32(v[2], v[3]);
  1238. in[14] = _mm_packs_epi32(v[6], v[7]);
  1239. // stage 2
  1240. u[0] = _mm_unpacklo_epi16(s[2], s[5]);
  1241. u[1] = _mm_unpackhi_epi16(s[2], s[5]);
  1242. u[2] = _mm_unpacklo_epi16(s[3], s[4]);
  1243. u[3] = _mm_unpackhi_epi16(s[3], s[4]);
  1244. v[0] = _mm_madd_epi16(u[0], k__cospi_m16_p16);
  1245. v[1] = _mm_madd_epi16(u[1], k__cospi_m16_p16);
  1246. v[2] = _mm_madd_epi16(u[2], k__cospi_m16_p16);
  1247. v[3] = _mm_madd_epi16(u[3], k__cospi_m16_p16);
  1248. v[4] = _mm_madd_epi16(u[2], k__cospi_p16_p16);
  1249. v[5] = _mm_madd_epi16(u[3], k__cospi_p16_p16);
  1250. v[6] = _mm_madd_epi16(u[0], k__cospi_p16_p16);
  1251. v[7] = _mm_madd_epi16(u[1], k__cospi_p16_p16);
  1252. u[0] = _mm_add_epi32(v[0], k__DCT_CONST_ROUNDING);
  1253. u[1] = _mm_add_epi32(v[1], k__DCT_CONST_ROUNDING);
  1254. u[2] = _mm_add_epi32(v[2], k__DCT_CONST_ROUNDING);
  1255. u[3] = _mm_add_epi32(v[3], k__DCT_CONST_ROUNDING);
  1256. u[4] = _mm_add_epi32(v[4], k__DCT_CONST_ROUNDING);
  1257. u[5] = _mm_add_epi32(v[5], k__DCT_CONST_ROUNDING);
  1258. u[6] = _mm_add_epi32(v[6], k__DCT_CONST_ROUNDING);
  1259. u[7] = _mm_add_epi32(v[7], k__DCT_CONST_ROUNDING);
  1260. v[0] = _mm_srai_epi32(u[0], DCT_CONST_BITS);
  1261. v[1] = _mm_srai_epi32(u[1], DCT_CONST_BITS);
  1262. v[2] = _mm_srai_epi32(u[2], DCT_CONST_BITS);
  1263. v[3] = _mm_srai_epi32(u[3], DCT_CONST_BITS);
  1264. v[4] = _mm_srai_epi32(u[4], DCT_CONST_BITS);
  1265. v[5] = _mm_srai_epi32(u[5], DCT_CONST_BITS);
  1266. v[6] = _mm_srai_epi32(u[6], DCT_CONST_BITS);
  1267. v[7] = _mm_srai_epi32(u[7], DCT_CONST_BITS);
  1268. t[2] = _mm_packs_epi32(v[0], v[1]);
  1269. t[3] = _mm_packs_epi32(v[2], v[3]);
  1270. t[4] = _mm_packs_epi32(v[4], v[5]);
  1271. t[5] = _mm_packs_epi32(v[6], v[7]);
  1272. // stage 3
  1273. p[0] = _mm_add_epi16(s[0], t[3]);
  1274. p[1] = _mm_add_epi16(s[1], t[2]);
  1275. p[2] = _mm_sub_epi16(s[1], t[2]);
  1276. p[3] = _mm_sub_epi16(s[0], t[3]);
  1277. p[4] = _mm_sub_epi16(s[7], t[4]);
  1278. p[5] = _mm_sub_epi16(s[6], t[5]);
  1279. p[6] = _mm_add_epi16(s[6], t[5]);
  1280. p[7] = _mm_add_epi16(s[7], t[4]);
  1281. // stage 4
  1282. u[0] = _mm_unpacklo_epi16(p[1], p[6]);
  1283. u[1] = _mm_unpackhi_epi16(p[1], p[6]);
  1284. u[2] = _mm_unpacklo_epi16(p[2], p[5]);
  1285. u[3] = _mm_unpackhi_epi16(p[2], p[5]);
  1286. v[0] = _mm_madd_epi16(u[0], k__cospi_m08_p24);
  1287. v[1] = _mm_madd_epi16(u[1], k__cospi_m08_p24);
  1288. v[2] = _mm_madd_epi16(u[2], k__cospi_p24_p08);
  1289. v[3] = _mm_madd_epi16(u[3], k__cospi_p24_p08);
  1290. v[4] = _mm_madd_epi16(u[2], k__cospi_p08_m24);
  1291. v[5] = _mm_madd_epi16(u[3], k__cospi_p08_m24);
  1292. v[6] = _mm_madd_epi16(u[0], k__cospi_p24_p08);
  1293. v[7] = _mm_madd_epi16(u[1], k__cospi_p24_p08);
  1294. u[0] = _mm_add_epi32(v[0], k__DCT_CONST_ROUNDING);
  1295. u[1] = _mm_add_epi32(v[1], k__DCT_CONST_ROUNDING);
  1296. u[2] = _mm_add_epi32(v[2], k__DCT_CONST_ROUNDING);
  1297. u[3] = _mm_add_epi32(v[3], k__DCT_CONST_ROUNDING);
  1298. u[4] = _mm_add_epi32(v[4], k__DCT_CONST_ROUNDING);
  1299. u[5] = _mm_add_epi32(v[5], k__DCT_CONST_ROUNDING);
  1300. u[6] = _mm_add_epi32(v[6], k__DCT_CONST_ROUNDING);
  1301. u[7] = _mm_add_epi32(v[7], k__DCT_CONST_ROUNDING);
  1302. v[0] = _mm_srai_epi32(u[0], DCT_CONST_BITS);
  1303. v[1] = _mm_srai_epi32(u[1], DCT_CONST_BITS);
  1304. v[2] = _mm_srai_epi32(u[2], DCT_CONST_BITS);
  1305. v[3] = _mm_srai_epi32(u[3], DCT_CONST_BITS);
  1306. v[4] = _mm_srai_epi32(u[4], DCT_CONST_BITS);
  1307. v[5] = _mm_srai_epi32(u[5], DCT_CONST_BITS);
  1308. v[6] = _mm_srai_epi32(u[6], DCT_CONST_BITS);
  1309. v[7] = _mm_srai_epi32(u[7], DCT_CONST_BITS);
  1310. t[1] = _mm_packs_epi32(v[0], v[1]);
  1311. t[2] = _mm_packs_epi32(v[2], v[3]);
  1312. t[5] = _mm_packs_epi32(v[4], v[5]);
  1313. t[6] = _mm_packs_epi32(v[6], v[7]);
  1314. // stage 5
  1315. s[0] = _mm_add_epi16(p[0], t[1]);
  1316. s[1] = _mm_sub_epi16(p[0], t[1]);
  1317. s[2] = _mm_add_epi16(p[3], t[2]);
  1318. s[3] = _mm_sub_epi16(p[3], t[2]);
  1319. s[4] = _mm_sub_epi16(p[4], t[5]);
  1320. s[5] = _mm_add_epi16(p[4], t[5]);
  1321. s[6] = _mm_sub_epi16(p[7], t[6]);
  1322. s[7] = _mm_add_epi16(p[7], t[6]);
  1323. // stage 6
  1324. u[0] = _mm_unpacklo_epi16(s[0], s[7]);
  1325. u[1] = _mm_unpackhi_epi16(s[0], s[7]);
  1326. u[2] = _mm_unpacklo_epi16(s[1], s[6]);
  1327. u[3] = _mm_unpackhi_epi16(s[1], s[6]);
  1328. u[4] = _mm_unpacklo_epi16(s[2], s[5]);
  1329. u[5] = _mm_unpackhi_epi16(s[2], s[5]);
  1330. u[6] = _mm_unpacklo_epi16(s[3], s[4]);
  1331. u[7] = _mm_unpackhi_epi16(s[3], s[4]);
  1332. v[0] = _mm_madd_epi16(u[0], k__cospi_p30_p02);
  1333. v[1] = _mm_madd_epi16(u[1], k__cospi_p30_p02);
  1334. v[2] = _mm_madd_epi16(u[2], k__cospi_p14_p18);
  1335. v[3] = _mm_madd_epi16(u[3], k__cospi_p14_p18);
  1336. v[4] = _mm_madd_epi16(u[4], k__cospi_p22_p10);
  1337. v[5] = _mm_madd_epi16(u[5], k__cospi_p22_p10);
  1338. v[6] = _mm_madd_epi16(u[6], k__cospi_p06_p26);
  1339. v[7] = _mm_madd_epi16(u[7], k__cospi_p06_p26);
  1340. v[8] = _mm_madd_epi16(u[6], k__cospi_m26_p06);
  1341. v[9] = _mm_madd_epi16(u[7], k__cospi_m26_p06);
  1342. v[10] = _mm_madd_epi16(u[4], k__cospi_m10_p22);
  1343. v[11] = _mm_madd_epi16(u[5], k__cospi_m10_p22);
  1344. v[12] = _mm_madd_epi16(u[2], k__cospi_m18_p14);
  1345. v[13] = _mm_madd_epi16(u[3], k__cospi_m18_p14);
  1346. v[14] = _mm_madd_epi16(u[0], k__cospi_m02_p30);
  1347. v[15] = _mm_madd_epi16(u[1], k__cospi_m02_p30);
  1348. u[0] = _mm_add_epi32(v[0], k__DCT_CONST_ROUNDING);
  1349. u[1] = _mm_add_epi32(v[1], k__DCT_CONST_ROUNDING);
  1350. u[2] = _mm_add_epi32(v[2], k__DCT_CONST_ROUNDING);
  1351. u[3] = _mm_add_epi32(v[3], k__DCT_CONST_ROUNDING);
  1352. u[4] = _mm_add_epi32(v[4], k__DCT_CONST_ROUNDING);
  1353. u[5] = _mm_add_epi32(v[5], k__DCT_CONST_ROUNDING);
  1354. u[6] = _mm_add_epi32(v[6], k__DCT_CONST_ROUNDING);
  1355. u[7] = _mm_add_epi32(v[7], k__DCT_CONST_ROUNDING);
  1356. u[8] = _mm_add_epi32(v[8], k__DCT_CONST_ROUNDING);
  1357. u[9] = _mm_add_epi32(v[9], k__DCT_CONST_ROUNDING);
  1358. u[10] = _mm_add_epi32(v[10], k__DCT_CONST_ROUNDING);
  1359. u[11] = _mm_add_epi32(v[11], k__DCT_CONST_ROUNDING);
  1360. u[12] = _mm_add_epi32(v[12], k__DCT_CONST_ROUNDING);
  1361. u[13] = _mm_add_epi32(v[13], k__DCT_CONST_ROUNDING);
  1362. u[14] = _mm_add_epi32(v[14], k__DCT_CONST_ROUNDING);
  1363. u[15] = _mm_add_epi32(v[15], k__DCT_CONST_ROUNDING);
  1364. v[0] = _mm_srai_epi32(u[0], DCT_CONST_BITS);
  1365. v[1] = _mm_srai_epi32(u[1], DCT_CONST_BITS);
  1366. v[2] = _mm_srai_epi32(u[2], DCT_CONST_BITS);
  1367. v[3] = _mm_srai_epi32(u[3], DCT_CONST_BITS);
  1368. v[4] = _mm_srai_epi32(u[4], DCT_CONST_BITS);
  1369. v[5] = _mm_srai_epi32(u[5], DCT_CONST_BITS);
  1370. v[6] = _mm_srai_epi32(u[6], DCT_CONST_BITS);
  1371. v[7] = _mm_srai_epi32(u[7], DCT_CONST_BITS);
  1372. v[8] = _mm_srai_epi32(u[8], DCT_CONST_BITS);
  1373. v[9] = _mm_srai_epi32(u[9], DCT_CONST_BITS);
  1374. v[10] = _mm_srai_epi32(u[10], DCT_CONST_BITS);
  1375. v[11] = _mm_srai_epi32(u[11], DCT_CONST_BITS);
  1376. v[12] = _mm_srai_epi32(u[12], DCT_CONST_BITS);
  1377. v[13] = _mm_srai_epi32(u[13], DCT_CONST_BITS);
  1378. v[14] = _mm_srai_epi32(u[14], DCT_CONST_BITS);
  1379. v[15] = _mm_srai_epi32(u[15], DCT_CONST_BITS);
  1380. in[1] = _mm_packs_epi32(v[0], v[1]);
  1381. in[9] = _mm_packs_epi32(v[2], v[3]);
  1382. in[5] = _mm_packs_epi32(v[4], v[5]);
  1383. in[13] = _mm_packs_epi32(v[6], v[7]);
  1384. in[3] = _mm_packs_epi32(v[8], v[9]);
  1385. in[11] = _mm_packs_epi32(v[10], v[11]);
  1386. in[7] = _mm_packs_epi32(v[12], v[13]);
  1387. in[15] = _mm_packs_epi32(v[14], v[15]);
  1388. }
  1389. static void fadst16_8col(__m128i *in) {
  1390. // perform 16x16 1-D ADST for 8 columns
  1391. __m128i s[16], x[16], u[32], v[32];
  1392. const __m128i k__cospi_p01_p31 = pair_set_epi16(cospi_1_64, cospi_31_64);
  1393. const __m128i k__cospi_p31_m01 = pair_set_epi16(cospi_31_64, -cospi_1_64);
  1394. const __m128i k__cospi_p05_p27 = pair_set_epi16(cospi_5_64, cospi_27_64);
  1395. const __m128i k__cospi_p27_m05 = pair_set_epi16(cospi_27_64, -cospi_5_64);
  1396. const __m128i k__cospi_p09_p23 = pair_set_epi16(cospi_9_64, cospi_23_64);
  1397. const __m128i k__cospi_p23_m09 = pair_set_epi16(cospi_23_64, -cospi_9_64);
  1398. const __m128i k__cospi_p13_p19 = pair_set_epi16(cospi_13_64, cospi_19_64);
  1399. const __m128i k__cospi_p19_m13 = pair_set_epi16(cospi_19_64, -cospi_13_64);
  1400. const __m128i k__cospi_p17_p15 = pair_set_epi16(cospi_17_64, cospi_15_64);
  1401. const __m128i k__cospi_p15_m17 = pair_set_epi16(cospi_15_64, -cospi_17_64);
  1402. const __m128i k__cospi_p21_p11 = pair_set_epi16(cospi_21_64, cospi_11_64);
  1403. const __m128i k__cospi_p11_m21 = pair_set_epi16(cospi_11_64, -cospi_21_64);
  1404. const __m128i k__cospi_p25_p07 = pair_set_epi16(cospi_25_64, cospi_7_64);
  1405. const __m128i k__cospi_p07_m25 = pair_set_epi16(cospi_7_64, -cospi_25_64);
  1406. const __m128i k__cospi_p29_p03 = pair_set_epi16(cospi_29_64, cospi_3_64);
  1407. const __m128i k__cospi_p03_m29 = pair_set_epi16(cospi_3_64, -cospi_29_64);
  1408. const __m128i k__cospi_p04_p28 = pair_set_epi16(cospi_4_64, cospi_28_64);
  1409. const __m128i k__cospi_p28_m04 = pair_set_epi16(cospi_28_64, -cospi_4_64);
  1410. const __m128i k__cospi_p20_p12 = pair_set_epi16(cospi_20_64, cospi_12_64);
  1411. const __m128i k__cospi_p12_m20 = pair_set_epi16(cospi_12_64, -cospi_20_64);
  1412. const __m128i k__cospi_m28_p04 = pair_set_epi16(-cospi_28_64, cospi_4_64);
  1413. const __m128i k__cospi_m12_p20 = pair_set_epi16(-cospi_12_64, cospi_20_64);
  1414. const __m128i k__cospi_p08_p24 = pair_set_epi16(cospi_8_64, cospi_24_64);
  1415. const __m128i k__cospi_p24_m08 = pair_set_epi16(cospi_24_64, -cospi_8_64);
  1416. const __m128i k__cospi_m24_p08 = pair_set_epi16(-cospi_24_64, cospi_8_64);
  1417. const __m128i k__cospi_m16_m16 = _mm_set1_epi16((int16_t)-cospi_16_64);
  1418. const __m128i k__cospi_p16_p16 = _mm_set1_epi16((int16_t)cospi_16_64);
  1419. const __m128i k__cospi_p16_m16 = pair_set_epi16(cospi_16_64, -cospi_16_64);
  1420. const __m128i k__cospi_m16_p16 = pair_set_epi16(-cospi_16_64, cospi_16_64);
  1421. const __m128i k__DCT_CONST_ROUNDING = _mm_set1_epi32(DCT_CONST_ROUNDING);
  1422. const __m128i kZero = _mm_set1_epi16(0);
  1423. u[0] = _mm_unpacklo_epi16(in[15], in[0]);
  1424. u[1] = _mm_unpackhi_epi16(in[15], in[0]);
  1425. u[2] = _mm_unpacklo_epi16(in[13], in[2]);
  1426. u[3] = _mm_unpackhi_epi16(in[13], in[2]);
  1427. u[4] = _mm_unpacklo_epi16(in[11], in[4]);
  1428. u[5] = _mm_unpackhi_epi16(in[11], in[4]);
  1429. u[6] = _mm_unpacklo_epi16(in[9], in[6]);
  1430. u[7] = _mm_unpackhi_epi16(in[9], in[6]);
  1431. u[8] = _mm_unpacklo_epi16(in[7], in[8]);
  1432. u[9] = _mm_unpackhi_epi16(in[7], in[8]);
  1433. u[10] = _mm_unpacklo_epi16(in[5], in[10]);
  1434. u[11] = _mm_unpackhi_epi16(in[5], in[10]);
  1435. u[12] = _mm_unpacklo_epi16(in[3], in[12]);
  1436. u[13] = _mm_unpackhi_epi16(in[3], in[12]);
  1437. u[14] = _mm_unpacklo_epi16(in[1], in[14]);
  1438. u[15] = _mm_unpackhi_epi16(in[1], in[14]);
  1439. v[0] = _mm_madd_epi16(u[0], k__cospi_p01_p31);
  1440. v[1] = _mm_madd_epi16(u[1], k__cospi_p01_p31);
  1441. v[2] = _mm_madd_epi16(u[0], k__cospi_p31_m01);
  1442. v[3] = _mm_madd_epi16(u[1], k__cospi_p31_m01);
  1443. v[4] = _mm_madd_epi16(u[2], k__cospi_p05_p27);
  1444. v[5] = _mm_madd_epi16(u[3], k__cospi_p05_p27);
  1445. v[6] = _mm_madd_epi16(u[2], k__cospi_p27_m05);
  1446. v[7] = _mm_madd_epi16(u[3], k__cospi_p27_m05);
  1447. v[8] = _mm_madd_epi16(u[4], k__cospi_p09_p23);
  1448. v[9] = _mm_madd_epi16(u[5], k__cospi_p09_p23);
  1449. v[10] = _mm_madd_epi16(u[4], k__cospi_p23_m09);
  1450. v[11] = _mm_madd_epi16(u[5], k__cospi_p23_m09);
  1451. v[12] = _mm_madd_epi16(u[6], k__cospi_p13_p19);
  1452. v[13] = _mm_madd_epi16(u[7], k__cospi_p13_p19);
  1453. v[14] = _mm_madd_epi16(u[6], k__cospi_p19_m13);
  1454. v[15] = _mm_madd_epi16(u[7], k__cospi_p19_m13);
  1455. v[16] = _mm_madd_epi16(u[8], k__cospi_p17_p15);
  1456. v[17] = _mm_madd_epi16(u[9], k__cospi_p17_p15);
  1457. v[18] = _mm_madd_epi16(u[8], k__cospi_p15_m17);
  1458. v[19] = _mm_madd_epi16(u[9], k__cospi_p15_m17);
  1459. v[20] = _mm_madd_epi16(u[10], k__cospi_p21_p11);
  1460. v[21] = _mm_madd_epi16(u[11], k__cospi_p21_p11);
  1461. v[22] = _mm_madd_epi16(u[10], k__cospi_p11_m21);
  1462. v[23] = _mm_madd_epi16(u[11], k__cospi_p11_m21);
  1463. v[24] = _mm_madd_epi16(u[12], k__cospi_p25_p07);
  1464. v[25] = _mm_madd_epi16(u[13], k__cospi_p25_p07);
  1465. v[26] = _mm_madd_epi16(u[12], k__cospi_p07_m25);
  1466. v[27] = _mm_madd_epi16(u[13], k__cospi_p07_m25);
  1467. v[28] = _mm_madd_epi16(u[14], k__cospi_p29_p03);
  1468. v[29] = _mm_madd_epi16(u[15], k__cospi_p29_p03);
  1469. v[30] = _mm_madd_epi16(u[14], k__cospi_p03_m29);
  1470. v[31] = _mm_madd_epi16(u[15], k__cospi_p03_m29);
  1471. u[0] = _mm_add_epi32(v[0], v[16]);
  1472. u[1] = _mm_add_epi32(v[1], v[17]);
  1473. u[2] = _mm_add_epi32(v[2], v[18]);
  1474. u[3] = _mm_add_epi32(v[3], v[19]);
  1475. u[4] = _mm_add_epi32(v[4], v[20]);
  1476. u[5] = _mm_add_epi32(v[5], v[21]);
  1477. u[6] = _mm_add_epi32(v[6], v[22]);
  1478. u[7] = _mm_add_epi32(v[7], v[23]);
  1479. u[8] = _mm_add_epi32(v[8], v[24]);
  1480. u[9] = _mm_add_epi32(v[9], v[25]);
  1481. u[10] = _mm_add_epi32(v[10], v[26]);
  1482. u[11] = _mm_add_epi32(v[11], v[27]);
  1483. u[12] = _mm_add_epi32(v[12], v[28]);
  1484. u[13] = _mm_add_epi32(v[13], v[29]);
  1485. u[14] = _mm_add_epi32(v[14], v[30]);
  1486. u[15] = _mm_add_epi32(v[15], v[31]);
  1487. u[16] = _mm_sub_epi32(v[0], v[16]);
  1488. u[17] = _mm_sub_epi32(v[1], v[17]);
  1489. u[18] = _mm_sub_epi32(v[2], v[18]);
  1490. u[19] = _mm_sub_epi32(v[3], v[19]);
  1491. u[20] = _mm_sub_epi32(v[4], v[20]);
  1492. u[21] = _mm_sub_epi32(v[5], v[21]);
  1493. u[22] = _mm_sub_epi32(v[6], v[22]);
  1494. u[23] = _mm_sub_epi32(v[7], v[23]);
  1495. u[24] = _mm_sub_epi32(v[8], v[24]);
  1496. u[25] = _mm_sub_epi32(v[9], v[25]);
  1497. u[26] = _mm_sub_epi32(v[10], v[26]);
  1498. u[27] = _mm_sub_epi32(v[11], v[27]);
  1499. u[28] = _mm_sub_epi32(v[12], v[28]);
  1500. u[29] = _mm_sub_epi32(v[13], v[29]);
  1501. u[30] = _mm_sub_epi32(v[14], v[30]);
  1502. u[31] = _mm_sub_epi32(v[15], v[31]);
  1503. v[0] = _mm_add_epi32(u[0], k__DCT_CONST_ROUNDING);
  1504. v[1] = _mm_add_epi32(u[1], k__DCT_CONST_ROUNDING);
  1505. v[2] = _mm_add_epi32(u[2], k__DCT_CONST_ROUNDING);
  1506. v[3] = _mm_add_epi32(u[3], k__DCT_CONST_ROUNDING);
  1507. v[4] = _mm_add_epi32(u[4], k__DCT_CONST_ROUNDING);
  1508. v[5] = _mm_add_epi32(u[5], k__DCT_CONST_ROUNDING);
  1509. v[6] = _mm_add_epi32(u[6], k__DCT_CONST_ROUNDING);
  1510. v[7] = _mm_add_epi32(u[7], k__DCT_CONST_ROUNDING);
  1511. v[8] = _mm_add_epi32(u[8], k__DCT_CONST_ROUNDING);
  1512. v[9] = _mm_add_epi32(u[9], k__DCT_CONST_ROUNDING);
  1513. v[10] = _mm_add_epi32(u[10], k__DCT_CONST_ROUNDING);
  1514. v[11] = _mm_add_epi32(u[11], k__DCT_CONST_ROUNDING);
  1515. v[12] = _mm_add_epi32(u[12], k__DCT_CONST_ROUNDING);
  1516. v[13] = _mm_add_epi32(u[13], k__DCT_CONST_ROUNDING);
  1517. v[14] = _mm_add_epi32(u[14], k__DCT_CONST_ROUNDING);
  1518. v[15] = _mm_add_epi32(u[15], k__DCT_CONST_ROUNDING);
  1519. v[16] = _mm_add_epi32(u[16], k__DCT_CONST_ROUNDING);
  1520. v[17] = _mm_add_epi32(u[17], k__DCT_CONST_ROUNDING);
  1521. v[18] = _mm_add_epi32(u[18], k__DCT_CONST_ROUNDING);
  1522. v[19] = _mm_add_epi32(u[19], k__DCT_CONST_ROUNDING);
  1523. v[20] = _mm_add_epi32(u[20], k__DCT_CONST_ROUNDING);
  1524. v[21] = _mm_add_epi32(u[21], k__DCT_CONST_ROUNDING);
  1525. v[22] = _mm_add_epi32(u[22], k__DCT_CONST_ROUNDING);
  1526. v[23] = _mm_add_epi32(u[23], k__DCT_CONST_ROUNDING);
  1527. v[24] = _mm_add_epi32(u[24], k__DCT_CONST_ROUNDING);
  1528. v[25] = _mm_add_epi32(u[25], k__DCT_CONST_ROUNDING);
  1529. v[26] = _mm_add_epi32(u[26], k__DCT_CONST_ROUNDING);
  1530. v[27] = _mm_add_epi32(u[27], k__DCT_CONST_ROUNDING);
  1531. v[28] = _mm_add_epi32(u[28], k__DCT_CONST_ROUNDING);
  1532. v[29] = _mm_add_epi32(u[29], k__DCT_CONST_ROUNDING);
  1533. v[30] = _mm_add_epi32(u[30], k__DCT_CONST_ROUNDING);
  1534. v[31] = _mm_add_epi32(u[31], k__DCT_CONST_ROUNDING);
  1535. u[0] = _mm_srai_epi32(v[0], DCT_CONST_BITS);
  1536. u[1] = _mm_srai_epi32(v[1], DCT_CONST_BITS);
  1537. u[2] = _mm_srai_epi32(v[2], DCT_CONST_BITS);
  1538. u[3] = _mm_srai_epi32(v[3], DCT_CONST_BITS);
  1539. u[4] = _mm_srai_epi32(v[4], DCT_CONST_BITS);
  1540. u[5] = _mm_srai_epi32(v[5], DCT_CONST_BITS);
  1541. u[6] = _mm_srai_epi32(v[6], DCT_CONST_BITS);
  1542. u[7] = _mm_srai_epi32(v[7], DCT_CONST_BITS);
  1543. u[8] = _mm_srai_epi32(v[8], DCT_CONST_BITS);
  1544. u[9] = _mm_srai_epi32(v[9], DCT_CONST_BITS);
  1545. u[10] = _mm_srai_epi32(v[10], DCT_CONST_BITS);
  1546. u[11] = _mm_srai_epi32(v[11], DCT_CONST_BITS);
  1547. u[12] = _mm_srai_epi32(v[12], DCT_CONST_BITS);
  1548. u[13] = _mm_srai_epi32(v[13], DCT_CONST_BITS);
  1549. u[14] = _mm_srai_epi32(v[14], DCT_CONST_BITS);
  1550. u[15] = _mm_srai_epi32(v[15], DCT_CONST_BITS);
  1551. u[16] = _mm_srai_epi32(v[16], DCT_CONST_BITS);
  1552. u[17] = _mm_srai_epi32(v[17], DCT_CONST_BITS);
  1553. u[18] = _mm_srai_epi32(v[18], DCT_CONST_BITS);
  1554. u[19] = _mm_srai_epi32(v[19], DCT_CONST_BITS);
  1555. u[20] = _mm_srai_epi32(v[20], DCT_CONST_BITS);
  1556. u[21] = _mm_srai_epi32(v[21], DCT_CONST_BITS);
  1557. u[22] = _mm_srai_epi32(v[22], DCT_CONST_BITS);
  1558. u[23] = _mm_srai_epi32(v[23], DCT_CONST_BITS);
  1559. u[24] = _mm_srai_epi32(v[24], DCT_CONST_BITS);
  1560. u[25] = _mm_srai_epi32(v[25], DCT_CONST_BITS);
  1561. u[26] = _mm_srai_epi32(v[26], DCT_CONST_BITS);
  1562. u[27] = _mm_srai_epi32(v[27], DCT_CONST_BITS);
  1563. u[28] = _mm_srai_epi32(v[28], DCT_CONST_BITS);
  1564. u[29] = _mm_srai_epi32(v[29], DCT_CONST_BITS);
  1565. u[30] = _mm_srai_epi32(v[30], DCT_CONST_BITS);
  1566. u[31] = _mm_srai_epi32(v[31], DCT_CONST_BITS);
  1567. s[0] = _mm_packs_epi32(u[0], u[1]);
  1568. s[1] = _mm_packs_epi32(u[2], u[3]);
  1569. s[2] = _mm_packs_epi32(u[4], u[5]);
  1570. s[3] = _mm_packs_epi32(u[6], u[7]);
  1571. s[4] = _mm_packs_epi32(u[8], u[9]);
  1572. s[5] = _mm_packs_epi32(u[10], u[11]);
  1573. s[6] = _mm_packs_epi32(u[12], u[13]);
  1574. s[7] = _mm_packs_epi32(u[14], u[15]);
  1575. s[8] = _mm_packs_epi32(u[16], u[17]);
  1576. s[9] = _mm_packs_epi32(u[18], u[19]);
  1577. s[10] = _mm_packs_epi32(u[20], u[21]);
  1578. s[11] = _mm_packs_epi32(u[22], u[23]);
  1579. s[12] = _mm_packs_epi32(u[24], u[25]);
  1580. s[13] = _mm_packs_epi32(u[26], u[27]);
  1581. s[14] = _mm_packs_epi32(u[28], u[29]);
  1582. s[15] = _mm_packs_epi32(u[30], u[31]);
  1583. // stage 2
  1584. u[0] = _mm_unpacklo_epi16(s[8], s[9]);
  1585. u[1] = _mm_unpackhi_epi16(s[8], s[9]);
  1586. u[2] = _mm_unpacklo_epi16(s[10], s[11]);
  1587. u[3] = _mm_unpackhi_epi16(s[10], s[11]);
  1588. u[4] = _mm_unpacklo_epi16(s[12], s[13]);
  1589. u[5] = _mm_unpackhi_epi16(s[12], s[13]);
  1590. u[6] = _mm_unpacklo_epi16(s[14], s[15]);
  1591. u[7] = _mm_unpackhi_epi16(s[14], s[15]);
  1592. v[0] = _mm_madd_epi16(u[0], k__cospi_p04_p28);
  1593. v[1] = _mm_madd_epi16(u[1], k__cospi_p04_p28);
  1594. v[2] = _mm_madd_epi16(u[0], k__cospi_p28_m04);
  1595. v[3] = _mm_madd_epi16(u[1], k__cospi_p28_m04);
  1596. v[4] = _mm_madd_epi16(u[2], k__cospi_p20_p12);
  1597. v[5] = _mm_madd_epi16(u[3], k__cospi_p20_p12);
  1598. v[6] = _mm_madd_epi16(u[2], k__cospi_p12_m20);
  1599. v[7] = _mm_madd_epi16(u[3], k__cospi_p12_m20);
  1600. v[8] = _mm_madd_epi16(u[4], k__cospi_m28_p04);
  1601. v[9] = _mm_madd_epi16(u[5], k__cospi_m28_p04);
  1602. v[10] = _mm_madd_epi16(u[4], k__cospi_p04_p28);
  1603. v[11] = _mm_madd_epi16(u[5], k__cospi_p04_p28);
  1604. v[12] = _mm_madd_epi16(u[6], k__cospi_m12_p20);
  1605. v[13] = _mm_madd_epi16(u[7], k__cospi_m12_p20);
  1606. v[14] = _mm_madd_epi16(u[6], k__cospi_p20_p12);
  1607. v[15] = _mm_madd_epi16(u[7], k__cospi_p20_p12);
  1608. u[0] = _mm_add_epi32(v[0], v[8]);
  1609. u[1] = _mm_add_epi32(v[1], v[9]);
  1610. u[2] = _mm_add_epi32(v[2], v[10]);
  1611. u[3] = _mm_add_epi32(v[3], v[11]);
  1612. u[4] = _mm_add_epi32(v[4], v[12]);
  1613. u[5] = _mm_add_epi32(v[5], v[13]);
  1614. u[6] = _mm_add_epi32(v[6], v[14]);
  1615. u[7] = _mm_add_epi32(v[7], v[15]);
  1616. u[8] = _mm_sub_epi32(v[0], v[8]);
  1617. u[9] = _mm_sub_epi32(v[1], v[9]);
  1618. u[10] = _mm_sub_epi32(v[2], v[10]);
  1619. u[11] = _mm_sub_epi32(v[3], v[11]);
  1620. u[12] = _mm_sub_epi32(v[4], v[12]);
  1621. u[13] = _mm_sub_epi32(v[5], v[13]);
  1622. u[14] = _mm_sub_epi32(v[6], v[14]);
  1623. u[15] = _mm_sub_epi32(v[7], v[15]);
  1624. v[0] = _mm_add_epi32(u[0], k__DCT_CONST_ROUNDING);
  1625. v[1] = _mm_add_epi32(u[1], k__DCT_CONST_ROUNDING);
  1626. v[2] = _mm_add_epi32(u[2], k__DCT_CONST_ROUNDING);
  1627. v[3] = _mm_add_epi32(u[3], k__DCT_CONST_ROUNDING);
  1628. v[4] = _mm_add_epi32(u[4], k__DCT_CONST_ROUNDING);
  1629. v[5] = _mm_add_epi32(u[5], k__DCT_CONST_ROUNDING);
  1630. v[6] = _mm_add_epi32(u[6], k__DCT_CONST_ROUNDING);
  1631. v[7] = _mm_add_epi32(u[7], k__DCT_CONST_ROUNDING);
  1632. v[8] = _mm_add_epi32(u[8], k__DCT_CONST_ROUNDING);
  1633. v[9] = _mm_add_epi32(u[9], k__DCT_CONST_ROUNDING);
  1634. v[10] = _mm_add_epi32(u[10], k__DCT_CONST_ROUNDING);
  1635. v[11] = _mm_add_epi32(u[11], k__DCT_CONST_ROUNDING);
  1636. v[12] = _mm_add_epi32(u[12], k__DCT_CONST_ROUNDING);
  1637. v[13] = _mm_add_epi32(u[13], k__DCT_CONST_ROUNDING);
  1638. v[14] = _mm_add_epi32(u[14], k__DCT_CONST_ROUNDING);
  1639. v[15] = _mm_add_epi32(u[15], k__DCT_CONST_ROUNDING);
  1640. u[0] = _mm_srai_epi32(v[0], DCT_CONST_BITS);
  1641. u[1] = _mm_srai_epi32(v[1], DCT_CONST_BITS);
  1642. u[2] = _mm_srai_epi32(v[2], DCT_CONST_BITS);
  1643. u[3] = _mm_srai_epi32(v[3], DCT_CONST_BITS);
  1644. u[4] = _mm_srai_epi32(v[4], DCT_CONST_BITS);
  1645. u[5] = _mm_srai_epi32(v[5], DCT_CONST_BITS);
  1646. u[6] = _mm_srai_epi32(v[6], DCT_CONST_BITS);
  1647. u[7] = _mm_srai_epi32(v[7], DCT_CONST_BITS);
  1648. u[8] = _mm_srai_epi32(v[8], DCT_CONST_BITS);
  1649. u[9] = _mm_srai_epi32(v[9], DCT_CONST_BITS);
  1650. u[10] = _mm_srai_epi32(v[10], DCT_CONST_BITS);
  1651. u[11] = _mm_srai_epi32(v[11], DCT_CONST_BITS);
  1652. u[12] = _mm_srai_epi32(v[12], DCT_CONST_BITS);
  1653. u[13] = _mm_srai_epi32(v[13], DCT_CONST_BITS);
  1654. u[14] = _mm_srai_epi32(v[14], DCT_CONST_BITS);
  1655. u[15] = _mm_srai_epi32(v[15], DCT_CONST_BITS);
  1656. x[0] = _mm_add_epi16(s[0], s[4]);
  1657. x[1] = _mm_add_epi16(s[1], s[5]);
  1658. x[2] = _mm_add_epi16(s[2], s[6]);
  1659. x[3] = _mm_add_epi16(s[3], s[7]);
  1660. x[4] = _mm_sub_epi16(s[0], s[4]);
  1661. x[5] = _mm_sub_epi16(s[1], s[5]);
  1662. x[6] = _mm_sub_epi16(s[2], s[6]);
  1663. x[7] = _mm_sub_epi16(s[3], s[7]);
  1664. x[8] = _mm_packs_epi32(u[0], u[1]);
  1665. x[9] = _mm_packs_epi32(u[2], u[3]);
  1666. x[10] = _mm_packs_epi32(u[4], u[5]);
  1667. x[11] = _mm_packs_epi32(u[6], u[7]);
  1668. x[12] = _mm_packs_epi32(u[8], u[9]);
  1669. x[13] = _mm_packs_epi32(u[10], u[11]);
  1670. x[14] = _mm_packs_epi32(u[12], u[13]);
  1671. x[15] = _mm_packs_epi32(u[14], u[15]);
  1672. // stage 3
  1673. u[0] = _mm_unpacklo_epi16(x[4], x[5]);
  1674. u[1] = _mm_unpackhi_epi16(x[4], x[5]);
  1675. u[2] = _mm_unpacklo_epi16(x[6], x[7]);
  1676. u[3] = _mm_unpackhi_epi16(x[6], x[7]);
  1677. u[4] = _mm_unpacklo_epi16(x[12], x[13]);
  1678. u[5] = _mm_unpackhi_epi16(x[12], x[13]);
  1679. u[6] = _mm_unpacklo_epi16(x[14], x[15]);
  1680. u[7] = _mm_unpackhi_epi16(x[14], x[15]);
  1681. v[0] = _mm_madd_epi16(u[0], k__cospi_p08_p24);
  1682. v[1] = _mm_madd_epi16(u[1], k__cospi_p08_p24);
  1683. v[2] = _mm_madd_epi16(u[0], k__cospi_p24_m08);
  1684. v[3] = _mm_madd_epi16(u[1], k__cospi_p24_m08);
  1685. v[4] = _mm_madd_epi16(u[2], k__cospi_m24_p08);
  1686. v[5] = _mm_madd_epi16(u[3], k__cospi_m24_p08);
  1687. v[6] = _mm_madd_epi16(u[2], k__cospi_p08_p24);
  1688. v[7] = _mm_madd_epi16(u[3], k__cospi_p08_p24);
  1689. v[8] = _mm_madd_epi16(u[4], k__cospi_p08_p24);
  1690. v[9] = _mm_madd_epi16(u[5], k__cospi_p08_p24);
  1691. v[10] = _mm_madd_epi16(u[4], k__cospi_p24_m08);
  1692. v[11] = _mm_madd_epi16(u[5], k__cospi_p24_m08);
  1693. v[12] = _mm_madd_epi16(u[6], k__cospi_m24_p08);
  1694. v[13] = _mm_madd_epi16(u[7], k__cospi_m24_p08);
  1695. v[14] = _mm_madd_epi16(u[6], k__cospi_p08_p24);
  1696. v[15] = _mm_madd_epi16(u[7], k__cospi_p08_p24);
  1697. u[0] = _mm_add_epi32(v[0], v[4]);
  1698. u[1] = _mm_add_epi32(v[1], v[5]);
  1699. u[2] = _mm_add_epi32(v[2], v[6]);
  1700. u[3] = _mm_add_epi32(v[3], v[7]);
  1701. u[4] = _mm_sub_epi32(v[0], v[4]);
  1702. u[5] = _mm_sub_epi32(v[1], v[5]);
  1703. u[6] = _mm_sub_epi32(v[2], v[6]);
  1704. u[7] = _mm_sub_epi32(v[3], v[7]);
  1705. u[8] = _mm_add_epi32(v[8], v[12]);
  1706. u[9] = _mm_add_epi32(v[9], v[13]);
  1707. u[10] = _mm_add_epi32(v[10], v[14]);
  1708. u[11] = _mm_add_epi32(v[11], v[15]);
  1709. u[12] = _mm_sub_epi32(v[8], v[12]);
  1710. u[13] = _mm_sub_epi32(v[9], v[13]);
  1711. u[14] = _mm_sub_epi32(v[10], v[14]);
  1712. u[15] = _mm_sub_epi32(v[11], v[15]);
  1713. u[0] = _mm_add_epi32(u[0], k__DCT_CONST_ROUNDING);
  1714. u[1] = _mm_add_epi32(u[1], k__DCT_CONST_ROUNDING);
  1715. u[2] = _mm_add_epi32(u[2], k__DCT_CONST_ROUNDING);
  1716. u[3] = _mm_add_epi32(u[3], k__DCT_CONST_ROUNDING);
  1717. u[4] = _mm_add_epi32(u[4], k__DCT_CONST_ROUNDING);
  1718. u[5] = _mm_add_epi32(u[5], k__DCT_CONST_ROUNDING);
  1719. u[6] = _mm_add_epi32(u[6], k__DCT_CONST_ROUNDING);
  1720. u[7] = _mm_add_epi32(u[7], k__DCT_CONST_ROUNDING);
  1721. u[8] = _mm_add_epi32(u[8], k__DCT_CONST_ROUNDING);
  1722. u[9] = _mm_add_epi32(u[9], k__DCT_CONST_ROUNDING);
  1723. u[10] = _mm_add_epi32(u[10], k__DCT_CONST_ROUNDING);
  1724. u[11] = _mm_add_epi32(u[11], k__DCT_CONST_ROUNDING);
  1725. u[12] = _mm_add_epi32(u[12], k__DCT_CONST_ROUNDING);
  1726. u[13] = _mm_add_epi32(u[13], k__DCT_CONST_ROUNDING);
  1727. u[14] = _mm_add_epi32(u[14], k__DCT_CONST_ROUNDING);
  1728. u[15] = _mm_add_epi32(u[15], k__DCT_CONST_ROUNDING);
  1729. v[0] = _mm_srai_epi32(u[0], DCT_CONST_BITS);
  1730. v[1] = _mm_srai_epi32(u[1], DCT_CONST_BITS);
  1731. v[2] = _mm_srai_epi32(u[2], DCT_CONST_BITS);
  1732. v[3] = _mm_srai_epi32(u[3], DCT_CONST_BITS);
  1733. v[4] = _mm_srai_epi32(u[4], DCT_CONST_BITS);
  1734. v[5] = _mm_srai_epi32(u[5], DCT_CONST_BITS);
  1735. v[6] = _mm_srai_epi32(u[6], DCT_CONST_BITS);
  1736. v[7] = _mm_srai_epi32(u[7], DCT_CONST_BITS);
  1737. v[8] = _mm_srai_epi32(u[8], DCT_CONST_BITS);
  1738. v[9] = _mm_srai_epi32(u[9], DCT_CONST_BITS);
  1739. v[10] = _mm_srai_epi32(u[10], DCT_CONST_BITS);
  1740. v[11] = _mm_srai_epi32(u[11], DCT_CONST_BITS);
  1741. v[12] = _mm_srai_epi32(u[12], DCT_CONST_BITS);
  1742. v[13] = _mm_srai_epi32(u[13], DCT_CONST_BITS);
  1743. v[14] = _mm_srai_epi32(u[14], DCT_CONST_BITS);
  1744. v[15] = _mm_srai_epi32(u[15], DCT_CONST_BITS);
  1745. s[0] = _mm_add_epi16(x[0], x[2]);
  1746. s[1] = _mm_add_epi16(x[1], x[3]);
  1747. s[2] = _mm_sub_epi16(x[0], x[2]);
  1748. s[3] = _mm_sub_epi16(x[1], x[3]);
  1749. s[4] = _mm_packs_epi32(v[0], v[1]);
  1750. s[5] = _mm_packs_epi32(v[2], v[3]);
  1751. s[6] = _mm_packs_epi32(v[4], v[5]);
  1752. s[7] = _mm_packs_epi32(v[6], v[7]);
  1753. s[8] = _mm_add_epi16(x[8], x[10]);
  1754. s[9] = _mm_add_epi16(x[9], x[11]);
  1755. s[10] = _mm_sub_epi16(x[8], x[10]);
  1756. s[11] = _mm_sub_epi16(x[9], x[11]);
  1757. s[12] = _mm_packs_epi32(v[8], v[9]);
  1758. s[13] = _mm_packs_epi32(v[10], v[11]);
  1759. s[14] = _mm_packs_epi32(v[12], v[13]);
  1760. s[15] = _mm_packs_epi32(v[14], v[15]);
  1761. // stage 4
  1762. u[0] = _mm_unpacklo_epi16(s[2], s[3]);
  1763. u[1] = _mm_unpackhi_epi16(s[2], s[3]);
  1764. u[2] = _mm_unpacklo_epi16(s[6], s[7]);
  1765. u[3] = _mm_unpackhi_epi16(s[6], s[7]);
  1766. u[4] = _mm_unpacklo_epi16(s[10], s[11]);
  1767. u[5] = _mm_unpackhi_epi16(s[10], s[11]);
  1768. u[6] = _mm_unpacklo_epi16(s[14], s[15]);
  1769. u[7] = _mm_unpackhi_epi16(s[14], s[15]);
  1770. v[0] = _mm_madd_epi16(u[0], k__cospi_m16_m16);
  1771. v[1] = _mm_madd_epi16(u[1], k__cospi_m16_m16);
  1772. v[2] = _mm_madd_epi16(u[0], k__cospi_p16_m16);
  1773. v[3] = _mm_madd_epi16(u[1], k__cospi_p16_m16);
  1774. v[4] = _mm_madd_epi16(u[2], k__cospi_p16_p16);
  1775. v[5] = _mm_madd_epi16(u[3], k__cospi_p16_p16);
  1776. v[6] = _mm_madd_epi16(u[2], k__cospi_m16_p16);
  1777. v[7] = _mm_madd_epi16(u[3], k__cospi_m16_p16);
  1778. v[8] = _mm_madd_epi16(u[4], k__cospi_p16_p16);
  1779. v[9] = _mm_madd_epi16(u[5], k__cospi_p16_p16);
  1780. v[10] = _mm_madd_epi16(u[4], k__cospi_m16_p16);
  1781. v[11] = _mm_madd_epi16(u[5], k__cospi_m16_p16);
  1782. v[12] = _mm_madd_epi16(u[6], k__cospi_m16_m16);
  1783. v[13] = _mm_madd_epi16(u[7], k__cospi_m16_m16);
  1784. v[14] = _mm_madd_epi16(u[6], k__cospi_p16_m16);
  1785. v[15] = _mm_madd_epi16(u[7], k__cospi_p16_m16);
  1786. u[0] = _mm_add_epi32(v[0], k__DCT_CONST_ROUNDING);
  1787. u[1] = _mm_add_epi32(v[1], k__DCT_CONST_ROUNDING);
  1788. u[2] = _mm_add_epi32(v[2], k__DCT_CONST_ROUNDING);
  1789. u[3] = _mm_add_epi32(v[3], k__DCT_CONST_ROUNDING);
  1790. u[4] = _mm_add_epi32(v[4], k__DCT_CONST_ROUNDING);
  1791. u[5] = _mm_add_epi32(v[5], k__DCT_CONST_ROUNDING);
  1792. u[6] = _mm_add_epi32(v[6], k__DCT_CONST_ROUNDING);
  1793. u[7] = _mm_add_epi32(v[7], k__DCT_CONST_ROUNDING);
  1794. u[8] = _mm_add_epi32(v[8], k__DCT_CONST_ROUNDING);
  1795. u[9] = _mm_add_epi32(v[9], k__DCT_CONST_ROUNDING);
  1796. u[10] = _mm_add_epi32(v[10], k__DCT_CONST_ROUNDING);
  1797. u[11] = _mm_add_epi32(v[11], k__DCT_CONST_ROUNDING);
  1798. u[12] = _mm_add_epi32(v[12], k__DCT_CONST_ROUNDING);
  1799. u[13] = _mm_add_epi32(v[13], k__DCT_CONST_ROUNDING);
  1800. u[14] = _mm_add_epi32(v[14], k__DCT_CONST_ROUNDING);
  1801. u[15] = _mm_add_epi32(v[15], k__DCT_CONST_ROUNDING);
  1802. v[0] = _mm_srai_epi32(u[0], DCT_CONST_BITS);
  1803. v[1] = _mm_srai_epi32(u[1], DCT_CONST_BITS);
  1804. v[2] = _mm_srai_epi32(u[2], DCT_CONST_BITS);
  1805. v[3] = _mm_srai_epi32(u[3], DCT_CONST_BITS);
  1806. v[4] = _mm_srai_epi32(u[4], DCT_CONST_BITS);
  1807. v[5] = _mm_srai_epi32(u[5], DCT_CONST_BITS);
  1808. v[6] = _mm_srai_epi32(u[6], DCT_CONST_BITS);
  1809. v[7] = _mm_srai_epi32(u[7], DCT_CONST_BITS);
  1810. v[8] = _mm_srai_epi32(u[8], DCT_CONST_BITS);
  1811. v[9] = _mm_srai_epi32(u[9], DCT_CONST_BITS);
  1812. v[10] = _mm_srai_epi32(u[10], DCT_CONST_BITS);
  1813. v[11] = _mm_srai_epi32(u[11], DCT_CONST_BITS);
  1814. v[12] = _mm_srai_epi32(u[12], DCT_CONST_BITS);
  1815. v[13] = _mm_srai_epi32(u[13], DCT_CONST_BITS);
  1816. v[14] = _mm_srai_epi32(u[14], DCT_CONST_BITS);
  1817. v[15] = _mm_srai_epi32(u[15], DCT_CONST_BITS);
  1818. in[0] = s[0];
  1819. in[1] = _mm_sub_epi16(kZero, s[8]);
  1820. in[2] = s[12];
  1821. in[3] = _mm_sub_epi16(kZero, s[4]);
  1822. in[4] = _mm_packs_epi32(v[4], v[5]);
  1823. in[5] = _mm_packs_epi32(v[12], v[13]);
  1824. in[6] = _mm_packs_epi32(v[8], v[9]);
  1825. in[7] = _mm_packs_epi32(v[0], v[1]);
  1826. in[8] = _mm_packs_epi32(v[2], v[3]);
  1827. in[9] = _mm_packs_epi32(v[10], v[11]);
  1828. in[10] = _mm_packs_epi32(v[14], v[15]);
  1829. in[11] = _mm_packs_epi32(v[6], v[7]);
  1830. in[12] = s[5];
  1831. in[13] = _mm_sub_epi16(kZero, s[13]);
  1832. in[14] = s[9];
  1833. in[15] = _mm_sub_epi16(kZero, s[1]);
  1834. }
  1835. static void fdct16_sse2(__m128i *in0, __m128i *in1) {
  1836. fdct16_8col(in0);
  1837. fdct16_8col(in1);
  1838. array_transpose_16x16(in0, in1);
  1839. }
  1840. static void fadst16_sse2(__m128i *in0, __m128i *in1) {
  1841. fadst16_8col(in0);
  1842. fadst16_8col(in1);
  1843. array_transpose_16x16(in0, in1);
  1844. }
  1845. void vp9_fht16x16_sse2(const int16_t *input, tran_low_t *output, int stride,
  1846. int tx_type) {
  1847. __m128i in0[16], in1[16];
  1848. switch (tx_type) {
  1849. case DCT_DCT: vpx_fdct16x16_sse2(input, output, stride); break;
  1850. case ADST_DCT:
  1851. load_buffer_16x16(input, in0, in1, stride);
  1852. fadst16_sse2(in0, in1);
  1853. right_shift_16x16(in0, in1);
  1854. fdct16_sse2(in0, in1);
  1855. write_buffer_16x16(output, in0, in1, 16);
  1856. break;
  1857. case DCT_ADST:
  1858. load_buffer_16x16(input, in0, in1, stride);
  1859. fdct16_sse2(in0, in1);
  1860. right_shift_16x16(in0, in1);
  1861. fadst16_sse2(in0, in1);
  1862. write_buffer_16x16(output, in0, in1, 16);
  1863. break;
  1864. case ADST_ADST:
  1865. load_buffer_16x16(input, in0, in1, stride);
  1866. fadst16_sse2(in0, in1);
  1867. right_shift_16x16(in0, in1);
  1868. fadst16_sse2(in0, in1);
  1869. write_buffer_16x16(output, in0, in1, 16);
  1870. break;
  1871. default: assert(0); break;
  1872. }
  1873. }