fft_sse.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * FFT/MDCT transform with SSE optimizations
  3. * Copyright (c) 2008 Loren Merritt
  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 "libavutil/x86_cpu.h"
  22. #include "libavcodec/dsputil.h"
  23. DECLARE_ASM_CONST(16, int, m1m1m1m1)[4] =
  24. { 1 << 31, 1 << 31, 1 << 31, 1 << 31 };
  25. void ff_fft_dispatch_sse(FFTComplex *z, int nbits);
  26. void ff_fft_dispatch_interleave_sse(FFTComplex *z, int nbits);
  27. void ff_fft_calc_sse(FFTContext *s, FFTComplex *z)
  28. {
  29. int n = 1 << s->nbits;
  30. ff_fft_dispatch_interleave_sse(z, s->nbits);
  31. if(n <= 16) {
  32. x86_reg i = -8*n;
  33. __asm__ volatile(
  34. "1: \n"
  35. "movaps (%0,%1), %%xmm0 \n"
  36. "movaps %%xmm0, %%xmm1 \n"
  37. "unpcklps 16(%0,%1), %%xmm0 \n"
  38. "unpckhps 16(%0,%1), %%xmm1 \n"
  39. "movaps %%xmm0, (%0,%1) \n"
  40. "movaps %%xmm1, 16(%0,%1) \n"
  41. "add $32, %0 \n"
  42. "jl 1b \n"
  43. :"+r"(i)
  44. :"r"(z+n)
  45. :"memory"
  46. );
  47. }
  48. }
  49. void ff_fft_permute_sse(FFTContext *s, FFTComplex *z)
  50. {
  51. int n = 1 << s->nbits;
  52. int i;
  53. for(i=0; i<n; i+=2) {
  54. __asm__ volatile(
  55. "movaps %2, %%xmm0 \n"
  56. "movlps %%xmm0, %0 \n"
  57. "movhps %%xmm0, %1 \n"
  58. :"=m"(s->tmp_buf[s->revtab[i]]),
  59. "=m"(s->tmp_buf[s->revtab[i+1]])
  60. :"m"(z[i])
  61. );
  62. }
  63. memcpy(z, s->tmp_buf, n*sizeof(FFTComplex));
  64. }
  65. void ff_imdct_half_sse(MDCTContext *s, FFTSample *output, const FFTSample *input)
  66. {
  67. av_unused x86_reg i, j, k, l;
  68. long n = 1 << s->nbits;
  69. long n2 = n >> 1;
  70. long n4 = n >> 2;
  71. long n8 = n >> 3;
  72. const uint16_t *revtab = s->fft.revtab + n8;
  73. const FFTSample *tcos = s->tcos;
  74. const FFTSample *tsin = s->tsin;
  75. FFTComplex *z = (FFTComplex *)output;
  76. /* pre rotation */
  77. for(k=n8-2; k>=0; k-=2) {
  78. __asm__ volatile(
  79. "movaps (%2,%1,2), %%xmm0 \n" // { z[k].re, z[k].im, z[k+1].re, z[k+1].im }
  80. "movaps -16(%2,%0,2), %%xmm1 \n" // { z[-k-2].re, z[-k-2].im, z[-k-1].re, z[-k-1].im }
  81. "movaps %%xmm0, %%xmm2 \n"
  82. "shufps $0x88, %%xmm1, %%xmm0 \n" // { z[k].re, z[k+1].re, z[-k-2].re, z[-k-1].re }
  83. "shufps $0x77, %%xmm2, %%xmm1 \n" // { z[-k-1].im, z[-k-2].im, z[k+1].im, z[k].im }
  84. "movlps (%3,%1), %%xmm4 \n"
  85. "movlps (%4,%1), %%xmm5 \n"
  86. "movhps -8(%3,%0), %%xmm4 \n" // { cos[k], cos[k+1], cos[-k-2], cos[-k-1] }
  87. "movhps -8(%4,%0), %%xmm5 \n" // { sin[k], sin[k+1], sin[-k-2], sin[-k-1] }
  88. "movaps %%xmm0, %%xmm2 \n"
  89. "movaps %%xmm1, %%xmm3 \n"
  90. "mulps %%xmm5, %%xmm0 \n" // re*sin
  91. "mulps %%xmm4, %%xmm1 \n" // im*cos
  92. "mulps %%xmm4, %%xmm2 \n" // re*cos
  93. "mulps %%xmm5, %%xmm3 \n" // im*sin
  94. "subps %%xmm0, %%xmm1 \n" // -> re
  95. "addps %%xmm3, %%xmm2 \n" // -> im
  96. "movaps %%xmm1, %%xmm0 \n"
  97. "unpcklps %%xmm2, %%xmm1 \n" // { z[k], z[k+1] }
  98. "unpckhps %%xmm2, %%xmm0 \n" // { z[-k-2], z[-k-1] }
  99. ::"r"(-4*k), "r"(4*k),
  100. "r"(input+n4), "r"(tcos+n8), "r"(tsin+n8)
  101. );
  102. #if ARCH_X86_64
  103. // if we have enough regs, don't let gcc make the luts latency-bound
  104. // but if not, latency is faster than spilling
  105. __asm__("movlps %%xmm0, %0 \n"
  106. "movhps %%xmm0, %1 \n"
  107. "movlps %%xmm1, %2 \n"
  108. "movhps %%xmm1, %3 \n"
  109. :"=m"(z[revtab[-k-2]]),
  110. "=m"(z[revtab[-k-1]]),
  111. "=m"(z[revtab[ k ]]),
  112. "=m"(z[revtab[ k+1]])
  113. );
  114. #else
  115. __asm__("movlps %%xmm0, %0" :"=m"(z[revtab[-k-2]]));
  116. __asm__("movhps %%xmm0, %0" :"=m"(z[revtab[-k-1]]));
  117. __asm__("movlps %%xmm1, %0" :"=m"(z[revtab[ k ]]));
  118. __asm__("movhps %%xmm1, %0" :"=m"(z[revtab[ k+1]]));
  119. #endif
  120. }
  121. ff_fft_dispatch_sse(z, s->fft.nbits);
  122. /* post rotation + reinterleave + reorder */
  123. #define CMUL(j,xmm0,xmm1)\
  124. "movaps (%2,"#j",2), %%xmm6 \n"\
  125. "movaps 16(%2,"#j",2), "#xmm0"\n"\
  126. "movaps %%xmm6, "#xmm1"\n"\
  127. "movaps "#xmm0",%%xmm7 \n"\
  128. "mulps (%3,"#j"), %%xmm6 \n"\
  129. "mulps (%4,"#j"), "#xmm0"\n"\
  130. "mulps (%4,"#j"), "#xmm1"\n"\
  131. "mulps (%3,"#j"), %%xmm7 \n"\
  132. "subps %%xmm6, "#xmm0"\n"\
  133. "addps %%xmm7, "#xmm1"\n"
  134. j = -n2;
  135. k = n2-16;
  136. __asm__ volatile(
  137. "1: \n"
  138. CMUL(%0, %%xmm0, %%xmm1)
  139. CMUL(%1, %%xmm4, %%xmm5)
  140. "shufps $0x1b, %%xmm1, %%xmm1 \n"
  141. "shufps $0x1b, %%xmm5, %%xmm5 \n"
  142. "movaps %%xmm4, %%xmm6 \n"
  143. "unpckhps %%xmm1, %%xmm4 \n"
  144. "unpcklps %%xmm1, %%xmm6 \n"
  145. "movaps %%xmm0, %%xmm2 \n"
  146. "unpcklps %%xmm5, %%xmm0 \n"
  147. "unpckhps %%xmm5, %%xmm2 \n"
  148. "movaps %%xmm6, (%2,%1,2) \n"
  149. "movaps %%xmm4, 16(%2,%1,2) \n"
  150. "movaps %%xmm0, (%2,%0,2) \n"
  151. "movaps %%xmm2, 16(%2,%0,2) \n"
  152. "sub $16, %1 \n"
  153. "add $16, %0 \n"
  154. "jl 1b \n"
  155. :"+&r"(j), "+&r"(k)
  156. :"r"(z+n8), "r"(tcos+n8), "r"(tsin+n8)
  157. :"memory"
  158. );
  159. }
  160. void ff_imdct_calc_sse(MDCTContext *s, FFTSample *output, const FFTSample *input)
  161. {
  162. x86_reg j, k;
  163. long n = 1 << s->nbits;
  164. long n4 = n >> 2;
  165. ff_imdct_half_sse(s, output+n4, input);
  166. j = -n;
  167. k = n-16;
  168. __asm__ volatile(
  169. "movaps "MANGLE(m1m1m1m1)", %%xmm7 \n"
  170. "1: \n"
  171. "movaps (%2,%1), %%xmm0 \n"
  172. "movaps (%3,%0), %%xmm1 \n"
  173. "shufps $0x1b, %%xmm0, %%xmm0 \n"
  174. "shufps $0x1b, %%xmm1, %%xmm1 \n"
  175. "xorps %%xmm7, %%xmm0 \n"
  176. "movaps %%xmm1, (%3,%1) \n"
  177. "movaps %%xmm0, (%2,%0) \n"
  178. "sub $16, %1 \n"
  179. "add $16, %0 \n"
  180. "jl 1b \n"
  181. :"+r"(j), "+r"(k)
  182. :"r"(output+n4), "r"(output+n4*3)
  183. );
  184. }