You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

300 lines
11KB

  1. /*
  2. * FFT/MDCT transform with SSE optimizations
  3. * Copyright (c) 2002 Fabrice Bellard.
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "dsputil.h"
  22. static const int p1p1p1m1[4] __attribute__((aligned(16))) =
  23. { 0, 0, 0, 1 << 31 };
  24. static const int p1p1m1p1[4] __attribute__((aligned(16))) =
  25. { 0, 0, 1 << 31, 0 };
  26. static const int p1p1m1m1[4] __attribute__((aligned(16))) =
  27. { 0, 0, 1 << 31, 1 << 31 };
  28. static const int p1m1p1m1[4] __attribute__((aligned(16))) =
  29. { 0, 1 << 31, 0, 1 << 31 };
  30. static const int m1m1m1m1[4] __attribute__((aligned(16))) =
  31. { 1 << 31, 1 << 31, 1 << 31, 1 << 31 };
  32. #if 0
  33. static void print_v4sf(const char *str, __m128 a)
  34. {
  35. float *p = (float *)&a;
  36. printf("%s: %f %f %f %f\n",
  37. str, p[0], p[1], p[2], p[3]);
  38. }
  39. #endif
  40. /* XXX: handle reverse case */
  41. void ff_fft_calc_sse(FFTContext *s, FFTComplex *z)
  42. {
  43. int ln = s->nbits;
  44. long i, j;
  45. long nblocks, nloops;
  46. FFTComplex *p, *cptr;
  47. asm volatile(
  48. "movaps %0, %%xmm4 \n\t"
  49. "movaps %1, %%xmm5 \n\t"
  50. ::"m"(*p1p1m1m1),
  51. "m"(*(s->inverse ? p1p1m1p1 : p1p1p1m1))
  52. );
  53. i = 8 << ln;
  54. asm volatile(
  55. "1: \n\t"
  56. "sub $32, %0 \n\t"
  57. /* do the pass 0 butterfly */
  58. "movaps (%0,%1), %%xmm0 \n\t"
  59. "movaps %%xmm0, %%xmm1 \n\t"
  60. "shufps $0x4E, %%xmm0, %%xmm0 \n\t"
  61. "xorps %%xmm4, %%xmm1 \n\t"
  62. "addps %%xmm1, %%xmm0 \n\t"
  63. "movaps 16(%0,%1), %%xmm2 \n\t"
  64. "movaps %%xmm2, %%xmm3 \n\t"
  65. "shufps $0x4E, %%xmm2, %%xmm2 \n\t"
  66. "xorps %%xmm4, %%xmm3 \n\t"
  67. "addps %%xmm3, %%xmm2 \n\t"
  68. /* multiply third by -i */
  69. /* by toggling the sign bit */
  70. "shufps $0xB4, %%xmm2, %%xmm2 \n\t"
  71. "xorps %%xmm5, %%xmm2 \n\t"
  72. /* do the pass 1 butterfly */
  73. "movaps %%xmm0, %%xmm1 \n\t"
  74. "addps %%xmm2, %%xmm0 \n\t"
  75. "subps %%xmm2, %%xmm1 \n\t"
  76. "movaps %%xmm0, (%0,%1) \n\t"
  77. "movaps %%xmm1, 16(%0,%1) \n\t"
  78. "jg 1b \n\t"
  79. :"+r"(i)
  80. :"r"(z)
  81. );
  82. /* pass 2 .. ln-1 */
  83. nblocks = 1 << (ln-3);
  84. nloops = 1 << 2;
  85. cptr = s->exptab1;
  86. do {
  87. p = z;
  88. j = nblocks;
  89. do {
  90. i = nloops*8;
  91. asm volatile(
  92. "1: \n\t"
  93. "sub $32, %0 \n\t"
  94. "movaps (%2,%0), %%xmm1 \n\t"
  95. "movaps (%1,%0), %%xmm0 \n\t"
  96. "movaps 16(%2,%0), %%xmm5 \n\t"
  97. "movaps 16(%1,%0), %%xmm4 \n\t"
  98. "movaps %%xmm1, %%xmm2 \n\t"
  99. "movaps %%xmm5, %%xmm6 \n\t"
  100. "shufps $0xA0, %%xmm1, %%xmm1 \n\t"
  101. "shufps $0xF5, %%xmm2, %%xmm2 \n\t"
  102. "shufps $0xA0, %%xmm5, %%xmm5 \n\t"
  103. "shufps $0xF5, %%xmm6, %%xmm6 \n\t"
  104. "mulps (%3,%0,2), %%xmm1 \n\t" // cre*re cim*re
  105. "mulps 16(%3,%0,2), %%xmm2 \n\t" // -cim*im cre*im
  106. "mulps 32(%3,%0,2), %%xmm5 \n\t" // cre*re cim*re
  107. "mulps 48(%3,%0,2), %%xmm6 \n\t" // -cim*im cre*im
  108. "addps %%xmm2, %%xmm1 \n\t"
  109. "addps %%xmm6, %%xmm5 \n\t"
  110. "movaps %%xmm0, %%xmm3 \n\t"
  111. "movaps %%xmm4, %%xmm7 \n\t"
  112. "addps %%xmm1, %%xmm0 \n\t"
  113. "subps %%xmm1, %%xmm3 \n\t"
  114. "addps %%xmm5, %%xmm4 \n\t"
  115. "subps %%xmm5, %%xmm7 \n\t"
  116. "movaps %%xmm0, (%1,%0) \n\t"
  117. "movaps %%xmm3, (%2,%0) \n\t"
  118. "movaps %%xmm4, 16(%1,%0) \n\t"
  119. "movaps %%xmm7, 16(%2,%0) \n\t"
  120. "jg 1b \n\t"
  121. :"+r"(i)
  122. :"r"(p), "r"(p + nloops), "r"(cptr)
  123. );
  124. p += nloops*2;
  125. } while (--j);
  126. cptr += nloops*2;
  127. nblocks >>= 1;
  128. nloops <<= 1;
  129. } while (nblocks != 0);
  130. }
  131. void ff_imdct_calc_sse(MDCTContext *s, FFTSample *output,
  132. const FFTSample *input, FFTSample *tmp)
  133. {
  134. long k, n8, n4, n2, n;
  135. const uint16_t *revtab = s->fft.revtab;
  136. const FFTSample *tcos = s->tcos;
  137. const FFTSample *tsin = s->tsin;
  138. const FFTSample *in1, *in2;
  139. FFTComplex *z = (FFTComplex *)tmp;
  140. n = 1 << s->nbits;
  141. n2 = n >> 1;
  142. n4 = n >> 2;
  143. n8 = n >> 3;
  144. #ifdef ARCH_X86_64
  145. asm volatile ("movaps %0, %%xmm8\n\t"::"m"(*p1m1p1m1));
  146. #define P1M1P1M1 "%%xmm8"
  147. #else
  148. #define P1M1P1M1 "%4"
  149. #endif
  150. /* pre rotation */
  151. in1 = input;
  152. in2 = input + n2 - 4;
  153. /* Complex multiplication */
  154. for (k = 0; k < n4; k += 4) {
  155. asm volatile (
  156. "movaps %0, %%xmm0 \n\t" // xmm0 = r0 X r1 X : in2
  157. "movaps %1, %%xmm3 \n\t" // xmm3 = X i1 X i0: in1
  158. "movaps -16+1*%0, %%xmm4 \n\t" // xmm4 = r0 X r1 X : in2
  159. "movaps 16+1*%1, %%xmm7 \n\t" // xmm7 = X i1 X i0: in1
  160. "movlps %2, %%xmm1 \n\t" // xmm1 = X X R1 R0: tcos
  161. "movlps %3, %%xmm2 \n\t" // xmm2 = X X I1 I0: tsin
  162. "movlps 8+1*%2, %%xmm5 \n\t" // xmm5 = X X R1 R0: tcos
  163. "movlps 8+1*%3, %%xmm6 \n\t" // xmm6 = X X I1 I0: tsin
  164. "shufps $95, %%xmm0, %%xmm0 \n\t" // xmm0 = r1 r1 r0 r0
  165. "shufps $160,%%xmm3, %%xmm3 \n\t" // xmm3 = i1 i1 i0 i0
  166. "shufps $95, %%xmm4, %%xmm4 \n\t" // xmm4 = r1 r1 r0 r0
  167. "shufps $160,%%xmm7, %%xmm7 \n\t" // xmm7 = i1 i1 i0 i0
  168. "unpcklps %%xmm2, %%xmm1 \n\t" // xmm1 = I1 R1 I0 R0
  169. "unpcklps %%xmm6, %%xmm5 \n\t" // xmm5 = I1 R1 I0 R0
  170. "movaps %%xmm1, %%xmm2 \n\t" // xmm2 = I1 R1 I0 R0
  171. "movaps %%xmm5, %%xmm6 \n\t" // xmm6 = I1 R1 I0 R0
  172. "xorps "P1M1P1M1", %%xmm2 \n\t" // xmm2 = -I1 R1 -I0 R0
  173. "xorps "P1M1P1M1", %%xmm6 \n\t" // xmm6 = -I1 R1 -I0 R0
  174. "mulps %%xmm1, %%xmm0 \n\t" // xmm0 = rI rR rI rR
  175. "mulps %%xmm5, %%xmm4 \n\t" // xmm4 = rI rR rI rR
  176. "shufps $177,%%xmm2, %%xmm2 \n\t" // xmm2 = R1 -I1 R0 -I0
  177. "shufps $177,%%xmm6, %%xmm6 \n\t" // xmm6 = R1 -I1 R0 -I0
  178. "mulps %%xmm2, %%xmm3 \n\t" // xmm3 = Ri -Ii Ri -Ii
  179. "mulps %%xmm6, %%xmm7 \n\t" // xmm7 = Ri -Ii Ri -Ii
  180. "addps %%xmm3, %%xmm0 \n\t" // xmm0 = result
  181. "addps %%xmm7, %%xmm4 \n\t" // xmm4 = result
  182. ::"m"(in2[-2*k]), "m"(in1[2*k]),
  183. "m"(tcos[k]), "m"(tsin[k])
  184. #ifndef ARCH_X86_64
  185. ,"m"(*p1m1p1m1)
  186. #endif
  187. );
  188. /* Should be in the same block, hack for gcc2.95 & gcc3 */
  189. asm (
  190. "movlps %%xmm0, %0 \n\t"
  191. "movhps %%xmm0, %1 \n\t"
  192. "movlps %%xmm4, %2 \n\t"
  193. "movhps %%xmm4, %3 \n\t"
  194. :"=m"(z[revtab[k]]), "=m"(z[revtab[k + 1]]),
  195. "=m"(z[revtab[k + 2]]), "=m"(z[revtab[k + 3]])
  196. );
  197. }
  198. ff_fft_calc_sse(&s->fft, z);
  199. #ifndef ARCH_X86_64
  200. #undef P1M1P1M1
  201. #define P1M1P1M1 "%3"
  202. #endif
  203. /* post rotation + reordering */
  204. for (k = 0; k < n4; k += 4) {
  205. asm (
  206. "movaps %0, %%xmm0 \n\t" // xmm0 = i1 r1 i0 r0: z
  207. "movaps 16+1*%0, %%xmm4 \n\t" // xmm4 = i1 r1 i0 r0: z
  208. "movlps %1, %%xmm1 \n\t" // xmm1 = X X R1 R0: tcos
  209. "movlps 8+1*%1, %%xmm5 \n\t" // xmm5 = X X R1 R0: tcos
  210. "movaps %%xmm0, %%xmm3 \n\t" // xmm3 = i1 r1 i0 r0
  211. "movaps %%xmm4, %%xmm7 \n\t" // xmm7 = i1 r1 i0 r0
  212. "movlps %2, %%xmm2 \n\t" // xmm2 = X X I1 I0: tsin
  213. "movlps 8+1*%2, %%xmm6 \n\t" // xmm6 = X X I1 I0: tsin
  214. "shufps $160,%%xmm0, %%xmm0 \n\t" // xmm0 = r1 r1 r0 r0
  215. "shufps $245,%%xmm3, %%xmm3 \n\t" // xmm3 = i1 i1 i0 i0
  216. "shufps $160,%%xmm4, %%xmm4 \n\t" // xmm4 = r1 r1 r0 r0
  217. "shufps $245,%%xmm7, %%xmm7 \n\t" // xmm7 = i1 i1 i0 i0
  218. "unpcklps %%xmm2, %%xmm1 \n\t" // xmm1 = I1 R1 I0 R0
  219. "unpcklps %%xmm6, %%xmm5 \n\t" // xmm5 = I1 R1 I0 R0
  220. "movaps %%xmm1, %%xmm2 \n\t" // xmm2 = I1 R1 I0 R0
  221. "movaps %%xmm5, %%xmm6 \n\t" // xmm6 = I1 R1 I0 R0
  222. "xorps "P1M1P1M1", %%xmm2 \n\t" // xmm2 = -I1 R1 -I0 R0
  223. "mulps %%xmm1, %%xmm0 \n\t" // xmm0 = rI rR rI rR
  224. "xorps "P1M1P1M1", %%xmm6 \n\t" // xmm6 = -I1 R1 -I0 R0
  225. "mulps %%xmm5, %%xmm4 \n\t" // xmm4 = rI rR rI rR
  226. "shufps $177,%%xmm2, %%xmm2 \n\t" // xmm2 = R1 -I1 R0 -I0
  227. "shufps $177,%%xmm6, %%xmm6 \n\t" // xmm6 = R1 -I1 R0 -I0
  228. "mulps %%xmm2, %%xmm3 \n\t" // xmm3 = Ri -Ii Ri -Ii
  229. "mulps %%xmm6, %%xmm7 \n\t" // xmm7 = Ri -Ii Ri -Ii
  230. "addps %%xmm3, %%xmm0 \n\t" // xmm0 = result
  231. "addps %%xmm7, %%xmm4 \n\t" // xmm4 = result
  232. "movaps %%xmm0, %0 \n\t"
  233. "movaps %%xmm4, 16+1*%0\n\t"
  234. :"+m"(z[k])
  235. :"m"(tcos[k]), "m"(tsin[k])
  236. #ifndef ARCH_X86_64
  237. ,"m"(*p1m1p1m1)
  238. #endif
  239. );
  240. }
  241. /*
  242. Mnemonics:
  243. 0 = z[k].re
  244. 1 = z[k].im
  245. 2 = z[k + 1].re
  246. 3 = z[k + 1].im
  247. 4 = z[-k - 2].re
  248. 5 = z[-k - 2].im
  249. 6 = z[-k - 1].re
  250. 7 = z[-k - 1].im
  251. */
  252. k = 16-n;
  253. asm volatile("movaps %0, %%xmm7 \n\t"::"m"(*m1m1m1m1));
  254. asm volatile(
  255. "1: \n\t"
  256. "movaps -16(%4,%0), %%xmm1 \n\t" // xmm1 = 4 5 6 7 = z[-2-k]
  257. "neg %0 \n\t"
  258. "movaps (%4,%0), %%xmm0 \n\t" // xmm0 = 0 1 2 3 = z[k]
  259. "xorps %%xmm7, %%xmm0 \n\t" // xmm0 = -0 -1 -2 -3
  260. "movaps %%xmm0, %%xmm2 \n\t" // xmm2 = -0 -1 -2 -3
  261. "shufps $141,%%xmm1, %%xmm0 \n\t" // xmm0 = -1 -3 4 6
  262. "shufps $216,%%xmm1, %%xmm2 \n\t" // xmm2 = -0 -2 5 7
  263. "shufps $156,%%xmm0, %%xmm0 \n\t" // xmm0 = -1 6 -3 4 !
  264. "shufps $156,%%xmm2, %%xmm2 \n\t" // xmm2 = -0 7 -2 5 !
  265. "movaps %%xmm0, (%1,%0) \n\t" // output[2*k]
  266. "movaps %%xmm2, (%2,%0) \n\t" // output[n2+2*k]
  267. "neg %0 \n\t"
  268. "shufps $27, %%xmm0, %%xmm0 \n\t" // xmm0 = 4 -3 6 -1
  269. "xorps %%xmm7, %%xmm0 \n\t" // xmm0 = -4 3 -6 1 !
  270. "shufps $27, %%xmm2, %%xmm2 \n\t" // xmm2 = 5 -2 7 -0 !
  271. "movaps %%xmm0, -16(%2,%0) \n\t" // output[n2-4-2*k]
  272. "movaps %%xmm2, -16(%3,%0) \n\t" // output[n-4-2*k]
  273. "add $16, %0 \n\t"
  274. "jle 1b \n\t"
  275. :"+r"(k)
  276. :"r"(output), "r"(output+n2), "r"(output+n), "r"(z+n8)
  277. :"memory"
  278. );
  279. }