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.

248 lines
8.7KB

  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 $16, %0 \n\t"
  94. "movaps (%2,%0), %%xmm1 \n\t"
  95. "movaps (%1,%0), %%xmm0 \n\t"
  96. "movaps %%xmm1, %%xmm2 \n\t"
  97. "shufps $0xA0, %%xmm1, %%xmm1 \n\t"
  98. "shufps $0xF5, %%xmm2, %%xmm2 \n\t"
  99. "mulps (%3,%0,2), %%xmm1 \n\t" // cre*re cim*re
  100. "mulps 16(%3,%0,2), %%xmm2 \n\t" // -cim*im cre*im
  101. "addps %%xmm2, %%xmm1 \n\t"
  102. "movaps %%xmm0, %%xmm3 \n\t"
  103. "addps %%xmm1, %%xmm0 \n\t"
  104. "subps %%xmm1, %%xmm3 \n\t"
  105. "movaps %%xmm0, (%1,%0) \n\t"
  106. "movaps %%xmm3, (%2,%0) \n\t"
  107. "jg 1b \n\t"
  108. :"+r"(i)
  109. :"r"(p), "r"(p + nloops), "r"(cptr)
  110. );
  111. p += nloops*2;
  112. } while (--j);
  113. cptr += nloops*2;
  114. nblocks >>= 1;
  115. nloops <<= 1;
  116. } while (nblocks != 0);
  117. }
  118. void ff_imdct_calc_sse(MDCTContext *s, FFTSample *output,
  119. const FFTSample *input, FFTSample *tmp)
  120. {
  121. long k, n8, n4, n2, n;
  122. const uint16_t *revtab = s->fft.revtab;
  123. const FFTSample *tcos = s->tcos;
  124. const FFTSample *tsin = s->tsin;
  125. const FFTSample *in1, *in2;
  126. FFTComplex *z = (FFTComplex *)tmp;
  127. n = 1 << s->nbits;
  128. n2 = n >> 1;
  129. n4 = n >> 2;
  130. n8 = n >> 3;
  131. asm volatile ("movaps %0, %%xmm7\n\t"::"m"(*p1m1p1m1));
  132. /* pre rotation */
  133. in1 = input;
  134. in2 = input + n2 - 4;
  135. /* Complex multiplication
  136. Two complex products per iteration, we could have 4 with 8 xmm
  137. registers, 8 with 16 xmm registers.
  138. Maybe we should unroll more.
  139. */
  140. for (k = 0; k < n4; k += 2) {
  141. asm volatile (
  142. "movaps %0, %%xmm0 \n\t" // xmm0 = r0 X r1 X : in2
  143. "movaps %1, %%xmm3 \n\t" // xmm3 = X i1 X i0: in1
  144. "movlps %2, %%xmm1 \n\t" // xmm1 = X X R1 R0: tcos
  145. "movlps %3, %%xmm2 \n\t" // xmm2 = X X I1 I0: tsin
  146. "shufps $95, %%xmm0, %%xmm0 \n\t" // xmm0 = r1 r1 r0 r0
  147. "shufps $160,%%xmm3, %%xmm3 \n\t" // xmm3 = i1 i1 i0 i0
  148. "unpcklps %%xmm2, %%xmm1 \n\t" // xmm1 = I1 R1 I0 R0
  149. "movaps %%xmm1, %%xmm2 \n\t" // xmm2 = I1 R1 I0 R0
  150. "xorps %%xmm7, %%xmm2 \n\t" // xmm2 = -I1 R1 -I0 R0
  151. "mulps %%xmm1, %%xmm0 \n\t" // xmm0 = rI rR rI rR
  152. "shufps $177,%%xmm2, %%xmm2 \n\t" // xmm2 = R1 -I1 R0 -I0
  153. "mulps %%xmm2, %%xmm3 \n\t" // xmm3 = Ri -Ii Ri -Ii
  154. "addps %%xmm3, %%xmm0 \n\t" // xmm0 = result
  155. ::"m"(in2[-2*k]), "m"(in1[2*k]),
  156. "m"(tcos[k]), "m"(tsin[k])
  157. );
  158. /* Should be in the same block, hack for gcc2.95 & gcc3 */
  159. asm (
  160. "movlps %%xmm0, %0 \n\t"
  161. "movhps %%xmm0, %1 \n\t"
  162. :"=m"(z[revtab[k]]), "=m"(z[revtab[k + 1]])
  163. );
  164. }
  165. ff_fft_calc_sse(&s->fft, z);
  166. /* Not currently needed, added for safety */
  167. asm volatile ("movaps %0, %%xmm7\n\t"::"m"(*p1m1p1m1));
  168. /* post rotation + reordering */
  169. for (k = 0; k < n4; k += 2) {
  170. asm (
  171. "movaps %0, %%xmm0 \n\t" // xmm0 = i1 r1 i0 r0: z
  172. "movlps %1, %%xmm1 \n\t" // xmm1 = X X R1 R0: tcos
  173. "movaps %%xmm0, %%xmm3 \n\t" // xmm3 = i1 r1 i0 r0
  174. "movlps %2, %%xmm2 \n\t" // xmm2 = X X I1 I0: tsin
  175. "shufps $160,%%xmm0, %%xmm0 \n\t" // xmm0 = r1 r1 r0 r0
  176. "shufps $245,%%xmm3, %%xmm3 \n\t" // xmm3 = i1 i1 i0 i0
  177. "unpcklps %%xmm2, %%xmm1 \n\t" // xmm1 = I1 R1 I0 R0
  178. "movaps %%xmm1, %%xmm2 \n\t" // xmm2 = I1 R1 I0 R0
  179. "xorps %%xmm7, %%xmm2 \n\t" // xmm2 = -I1 R1 -I0 R0
  180. "mulps %%xmm1, %%xmm0 \n\t" // xmm0 = rI rR rI rR
  181. "shufps $177,%%xmm2, %%xmm2 \n\t" // xmm2 = R1 -I1 R0 -I0
  182. "mulps %%xmm2, %%xmm3 \n\t" // xmm3 = Ri -Ii Ri -Ii
  183. "addps %%xmm3, %%xmm0 \n\t" // xmm0 = result
  184. "movaps %%xmm0, %0 \n\t"
  185. :"+m"(z[k])
  186. :"m"(tcos[k]), "m"(tsin[k])
  187. );
  188. }
  189. /*
  190. Mnemonics:
  191. 0 = z[k].re
  192. 1 = z[k].im
  193. 2 = z[k + 1].re
  194. 3 = z[k + 1].im
  195. 4 = z[-k - 2].re
  196. 5 = z[-k - 2].im
  197. 6 = z[-k - 1].re
  198. 7 = z[-k - 1].im
  199. */
  200. k = 16-n;
  201. asm volatile("movaps %0, %%xmm7 \n\t"::"m"(*m1m1m1m1));
  202. asm volatile(
  203. "1: \n\t"
  204. "movaps -16(%4,%0), %%xmm1 \n\t" // xmm1 = 4 5 6 7 = z[-2-k]
  205. "neg %0 \n\t"
  206. "movaps (%4,%0), %%xmm0 \n\t" // xmm0 = 0 1 2 3 = z[k]
  207. "xorps %%xmm7, %%xmm0 \n\t" // xmm0 = -0 -1 -2 -3
  208. "movaps %%xmm0, %%xmm2 \n\t" // xmm2 = -0 -1 -2 -3
  209. "shufps $141,%%xmm1, %%xmm0 \n\t" // xmm0 = -1 -3 4 6
  210. "shufps $216,%%xmm1, %%xmm2 \n\t" // xmm2 = -0 -2 5 7
  211. "shufps $156,%%xmm0, %%xmm0 \n\t" // xmm0 = -1 6 -3 4 !
  212. "shufps $156,%%xmm2, %%xmm2 \n\t" // xmm2 = -0 7 -2 5 !
  213. "movaps %%xmm0, (%1,%0) \n\t" // output[2*k]
  214. "movaps %%xmm2, (%2,%0) \n\t" // output[n2+2*k]
  215. "neg %0 \n\t"
  216. "shufps $27, %%xmm0, %%xmm0 \n\t" // xmm0 = 4 -3 6 -1
  217. "xorps %%xmm7, %%xmm0 \n\t" // xmm0 = -4 3 -6 1 !
  218. "shufps $27, %%xmm2, %%xmm2 \n\t" // xmm2 = 5 -2 7 -0 !
  219. "movaps %%xmm0, -16(%2,%0) \n\t" // output[n2-4-2*k]
  220. "movaps %%xmm2, -16(%3,%0) \n\t" // output[n-4-2*k]
  221. "add $16, %0 \n\t"
  222. "jle 1b \n\t"
  223. :"+r"(k)
  224. :"r"(output), "r"(output+n2), "r"(output+n), "r"(z+n8)
  225. :"memory"
  226. );
  227. }