transpose_sse2.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /*
  2. * Copyright (c) 2015 The WebM project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #ifndef VPX_DSP_X86_TRANSPOSE_SSE2_H_
  11. #define VPX_DSP_X86_TRANSPOSE_SSE2_H_
  12. #include "./vpx_dsp_rtcd.h"
  13. static INLINE void transpose_16bit_4x4(const __m128i *const in,
  14. __m128i *const out) {
  15. // Unpack 16 bit elements. Goes from:
  16. // in[0]: 00 01 02 03 XX XX XX XX
  17. // in[1]: 10 11 12 13 XX XX XX XX
  18. // in[2]: 20 21 22 23 XX XX XX XX
  19. // in[3]: 30 31 32 33 XX XX XX XX
  20. // to:
  21. // a0: 00 10 01 11 02 12 03 13
  22. // a1: 20 30 21 31 22 32 23 33
  23. const __m128i a0 = _mm_unpacklo_epi16(in[0], in[1]);
  24. const __m128i a1 = _mm_unpacklo_epi16(in[2], in[3]);
  25. // Unpack 32 bit elements resulting in:
  26. // out[0]: 00 10 20 30 01 11 21 31
  27. // out[1]: 02 12 22 32 03 13 23 33
  28. out[0] = _mm_unpacklo_epi32(a0, a1);
  29. out[1] = _mm_unpackhi_epi32(a0, a1);
  30. }
  31. static INLINE void transpose_16bit_4x8(const __m128i *const in,
  32. __m128i *const out) {
  33. // Unpack 16 bit elements. Goes from:
  34. // in[0]: 00 01 02 03 XX XX XX XX
  35. // in[1]: 10 11 12 13 XX XX XX XX
  36. // in[2]: 20 21 22 23 XX XX XX XX
  37. // in[3]: 30 31 32 33 XX XX XX XX
  38. // in[4]: 40 41 42 43 XX XX XX XX
  39. // in[5]: 50 51 52 53 XX XX XX XX
  40. // in[6]: 60 61 62 63 XX XX XX XX
  41. // in[7]: 70 71 72 73 XX XX XX XX
  42. // to:
  43. // a0: 00 10 01 11 02 12 03 13
  44. // a1: 20 30 21 31 22 32 23 33
  45. // a2: 40 50 41 51 42 52 43 53
  46. // a3: 60 70 61 71 62 72 63 73
  47. const __m128i a0 = _mm_unpacklo_epi16(in[0], in[1]);
  48. const __m128i a1 = _mm_unpacklo_epi16(in[2], in[3]);
  49. const __m128i a2 = _mm_unpacklo_epi16(in[4], in[5]);
  50. const __m128i a3 = _mm_unpacklo_epi16(in[6], in[7]);
  51. // Unpack 32 bit elements resulting in:
  52. // b0: 00 10 20 30 01 11 21 31
  53. // b1: 40 50 60 70 41 51 61 71
  54. // b2: 02 12 22 32 03 13 23 33
  55. // b3: 42 52 62 72 43 53 63 73
  56. const __m128i b0 = _mm_unpacklo_epi32(a0, a1);
  57. const __m128i b1 = _mm_unpacklo_epi32(a2, a3);
  58. const __m128i b2 = _mm_unpackhi_epi32(a0, a1);
  59. const __m128i b3 = _mm_unpackhi_epi32(a2, a3);
  60. // Unpack 64 bit elements resulting in:
  61. // out[0]: 00 10 20 30 40 50 60 70
  62. // out[1]: 01 11 21 31 41 51 61 71
  63. // out[2]: 02 12 22 32 42 52 62 72
  64. // out[3]: 03 13 23 33 43 53 63 73
  65. out[0] = _mm_unpacklo_epi64(b0, b1);
  66. out[1] = _mm_unpackhi_epi64(b0, b1);
  67. out[2] = _mm_unpacklo_epi64(b2, b3);
  68. out[3] = _mm_unpackhi_epi64(b2, b3);
  69. }
  70. static INLINE void transpose_16bit_8x8(const __m128i *const in,
  71. __m128i *const out) {
  72. // Unpack 16 bit elements. Goes from:
  73. // in[0]: 00 01 02 03 04 05 06 07
  74. // in[1]: 10 11 12 13 14 15 16 17
  75. // in[2]: 20 21 22 23 24 25 26 27
  76. // in[3]: 30 31 32 33 34 35 36 37
  77. // in[4]: 40 41 42 43 44 45 46 47
  78. // in[5]: 50 51 52 53 54 55 56 57
  79. // in[6]: 60 61 62 63 64 65 66 67
  80. // in[7]: 70 71 72 73 74 75 76 77
  81. // to:
  82. // a0: 00 10 01 11 02 12 03 13
  83. // a1: 20 30 21 31 22 32 23 33
  84. // a2: 40 50 41 51 42 52 43 53
  85. // a3: 60 70 61 71 62 72 63 73
  86. // a4: 04 14 05 15 06 16 07 17
  87. // a5: 24 34 25 35 26 36 27 37
  88. // a6: 44 54 45 55 46 56 47 57
  89. // a7: 64 74 65 75 66 76 67 77
  90. const __m128i a0 = _mm_unpacklo_epi16(in[0], in[1]);
  91. const __m128i a1 = _mm_unpacklo_epi16(in[2], in[3]);
  92. const __m128i a2 = _mm_unpacklo_epi16(in[4], in[5]);
  93. const __m128i a3 = _mm_unpacklo_epi16(in[6], in[7]);
  94. const __m128i a4 = _mm_unpackhi_epi16(in[0], in[1]);
  95. const __m128i a5 = _mm_unpackhi_epi16(in[2], in[3]);
  96. const __m128i a6 = _mm_unpackhi_epi16(in[4], in[5]);
  97. const __m128i a7 = _mm_unpackhi_epi16(in[6], in[7]);
  98. // Unpack 32 bit elements resulting in:
  99. // b0: 00 10 20 30 01 11 21 31
  100. // b1: 40 50 60 70 41 51 61 71
  101. // b2: 04 14 24 34 05 15 25 35
  102. // b3: 44 54 64 74 45 55 65 75
  103. // b4: 02 12 22 32 03 13 23 33
  104. // b5: 42 52 62 72 43 53 63 73
  105. // b6: 06 16 26 36 07 17 27 37
  106. // b7: 46 56 66 76 47 57 67 77
  107. const __m128i b0 = _mm_unpacklo_epi32(a0, a1);
  108. const __m128i b1 = _mm_unpacklo_epi32(a2, a3);
  109. const __m128i b2 = _mm_unpacklo_epi32(a4, a5);
  110. const __m128i b3 = _mm_unpacklo_epi32(a6, a7);
  111. const __m128i b4 = _mm_unpackhi_epi32(a0, a1);
  112. const __m128i b5 = _mm_unpackhi_epi32(a2, a3);
  113. const __m128i b6 = _mm_unpackhi_epi32(a4, a5);
  114. const __m128i b7 = _mm_unpackhi_epi32(a6, a7);
  115. // Unpack 64 bit elements resulting in:
  116. // out[0]: 00 10 20 30 40 50 60 70
  117. // out[1]: 01 11 21 31 41 51 61 71
  118. // out[2]: 02 12 22 32 42 52 62 72
  119. // out[3]: 03 13 23 33 43 53 63 73
  120. // out[4]: 04 14 24 34 44 54 64 74
  121. // out[5]: 05 15 25 35 45 55 65 75
  122. // out[6]: 06 16 26 36 46 56 66 76
  123. // out[7]: 07 17 27 37 47 57 67 77
  124. out[0] = _mm_unpacklo_epi64(b0, b1);
  125. out[1] = _mm_unpackhi_epi64(b0, b1);
  126. out[2] = _mm_unpacklo_epi64(b4, b5);
  127. out[3] = _mm_unpackhi_epi64(b4, b5);
  128. out[4] = _mm_unpacklo_epi64(b2, b3);
  129. out[5] = _mm_unpackhi_epi64(b2, b3);
  130. out[6] = _mm_unpacklo_epi64(b6, b7);
  131. out[7] = _mm_unpackhi_epi64(b6, b7);
  132. }
  133. // Transpose in-place
  134. static INLINE void transpose_16bit_16x16(__m128i *const left,
  135. __m128i *const right) {
  136. __m128i tbuf[8];
  137. transpose_16bit_8x8(left, left);
  138. transpose_16bit_8x8(right, tbuf);
  139. transpose_16bit_8x8(left + 8, right);
  140. transpose_16bit_8x8(right + 8, right + 8);
  141. left[8] = tbuf[0];
  142. left[9] = tbuf[1];
  143. left[10] = tbuf[2];
  144. left[11] = tbuf[3];
  145. left[12] = tbuf[4];
  146. left[13] = tbuf[5];
  147. left[14] = tbuf[6];
  148. left[15] = tbuf[7];
  149. }
  150. static INLINE void transpose_32bit_4x4(const __m128i *const in,
  151. __m128i *const out) {
  152. // Unpack 32 bit elements. Goes from:
  153. // in[0]: 00 01 02 03
  154. // in[1]: 10 11 12 13
  155. // in[2]: 20 21 22 23
  156. // in[3]: 30 31 32 33
  157. // to:
  158. // a0: 00 10 01 11
  159. // a1: 20 30 21 31
  160. // a2: 02 12 03 13
  161. // a3: 22 32 23 33
  162. const __m128i a0 = _mm_unpacklo_epi32(in[0], in[1]);
  163. const __m128i a1 = _mm_unpacklo_epi32(in[2], in[3]);
  164. const __m128i a2 = _mm_unpackhi_epi32(in[0], in[1]);
  165. const __m128i a3 = _mm_unpackhi_epi32(in[2], in[3]);
  166. // Unpack 64 bit elements resulting in:
  167. // out[0]: 00 10 20 30
  168. // out[1]: 01 11 21 31
  169. // out[2]: 02 12 22 32
  170. // out[3]: 03 13 23 33
  171. out[0] = _mm_unpacklo_epi64(a0, a1);
  172. out[1] = _mm_unpackhi_epi64(a0, a1);
  173. out[2] = _mm_unpacklo_epi64(a2, a3);
  174. out[3] = _mm_unpackhi_epi64(a2, a3);
  175. }
  176. static INLINE void transpose_32bit_4x4x2(const __m128i *const in,
  177. __m128i *const out) {
  178. // Unpack 32 bit elements. Goes from:
  179. // in[0]: 00 01 02 03
  180. // in[1]: 10 11 12 13
  181. // in[2]: 20 21 22 23
  182. // in[3]: 30 31 32 33
  183. // in[4]: 04 05 06 07
  184. // in[5]: 14 15 16 17
  185. // in[6]: 24 25 26 27
  186. // in[7]: 34 35 36 37
  187. // to:
  188. // a0: 00 10 01 11
  189. // a1: 20 30 21 31
  190. // a2: 02 12 03 13
  191. // a3: 22 32 23 33
  192. // a4: 04 14 05 15
  193. // a5: 24 34 25 35
  194. // a6: 06 16 07 17
  195. // a7: 26 36 27 37
  196. const __m128i a0 = _mm_unpacklo_epi32(in[0], in[1]);
  197. const __m128i a1 = _mm_unpacklo_epi32(in[2], in[3]);
  198. const __m128i a2 = _mm_unpackhi_epi32(in[0], in[1]);
  199. const __m128i a3 = _mm_unpackhi_epi32(in[2], in[3]);
  200. const __m128i a4 = _mm_unpacklo_epi32(in[4], in[5]);
  201. const __m128i a5 = _mm_unpacklo_epi32(in[6], in[7]);
  202. const __m128i a6 = _mm_unpackhi_epi32(in[4], in[5]);
  203. const __m128i a7 = _mm_unpackhi_epi32(in[6], in[7]);
  204. // Unpack 64 bit elements resulting in:
  205. // out[0]: 00 10 20 30
  206. // out[1]: 01 11 21 31
  207. // out[2]: 02 12 22 32
  208. // out[3]: 03 13 23 33
  209. // out[4]: 04 14 24 34
  210. // out[5]: 05 15 25 35
  211. // out[6]: 06 16 26 36
  212. // out[7]: 07 17 27 37
  213. out[0] = _mm_unpacklo_epi64(a0, a1);
  214. out[1] = _mm_unpackhi_epi64(a0, a1);
  215. out[2] = _mm_unpacklo_epi64(a2, a3);
  216. out[3] = _mm_unpackhi_epi64(a2, a3);
  217. out[4] = _mm_unpacklo_epi64(a4, a5);
  218. out[5] = _mm_unpackhi_epi64(a4, a5);
  219. out[6] = _mm_unpacklo_epi64(a6, a7);
  220. out[7] = _mm_unpackhi_epi64(a6, a7);
  221. }
  222. static INLINE void transpose_32bit_8x4(const __m128i *const in,
  223. __m128i *const out) {
  224. // Unpack 32 bit elements. Goes from:
  225. // in[0]: 00 01 02 03
  226. // in[1]: 04 05 06 07
  227. // in[2]: 10 11 12 13
  228. // in[3]: 14 15 16 17
  229. // in[4]: 20 21 22 23
  230. // in[5]: 24 25 26 27
  231. // in[6]: 30 31 32 33
  232. // in[7]: 34 35 36 37
  233. // to:
  234. // a0: 00 10 01 11
  235. // a1: 20 30 21 31
  236. // a2: 02 12 03 13
  237. // a3: 22 32 23 33
  238. // a4: 04 14 05 15
  239. // a5: 24 34 25 35
  240. // a6: 06 16 07 17
  241. // a7: 26 36 27 37
  242. const __m128i a0 = _mm_unpacklo_epi32(in[0], in[2]);
  243. const __m128i a1 = _mm_unpacklo_epi32(in[4], in[6]);
  244. const __m128i a2 = _mm_unpackhi_epi32(in[0], in[2]);
  245. const __m128i a3 = _mm_unpackhi_epi32(in[4], in[6]);
  246. const __m128i a4 = _mm_unpacklo_epi32(in[1], in[3]);
  247. const __m128i a5 = _mm_unpacklo_epi32(in[5], in[7]);
  248. const __m128i a6 = _mm_unpackhi_epi32(in[1], in[3]);
  249. const __m128i a7 = _mm_unpackhi_epi32(in[5], in[7]);
  250. // Unpack 64 bit elements resulting in:
  251. // out[0]: 00 10 20 30
  252. // out[1]: 01 11 21 31
  253. // out[2]: 02 12 22 32
  254. // out[3]: 03 13 23 33
  255. // out[4]: 04 14 24 34
  256. // out[5]: 05 15 25 35
  257. // out[6]: 06 16 26 36
  258. // out[7]: 07 17 27 37
  259. out[0] = _mm_unpacklo_epi64(a0, a1);
  260. out[1] = _mm_unpackhi_epi64(a0, a1);
  261. out[2] = _mm_unpacklo_epi64(a2, a3);
  262. out[3] = _mm_unpackhi_epi64(a2, a3);
  263. out[4] = _mm_unpacklo_epi64(a4, a5);
  264. out[5] = _mm_unpackhi_epi64(a4, a5);
  265. out[6] = _mm_unpacklo_epi64(a6, a7);
  266. out[7] = _mm_unpackhi_epi64(a6, a7);
  267. }
  268. #endif // VPX_DSP_X86_TRANSPOSE_SSE2_H_