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.

174 lines
5.1KB

  1. /*
  2. * FFT/MDCT transform with Extended 3DNow! optimizations
  3. * Copyright (c) 2006-2008 Zuxy MENG Jie, Loren Merritt
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; 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. #include "fft.h"
  24. DECLARE_ALIGNED(8, static const unsigned int, m1m1)[2] = { 1U<<31, 1U<<31 };
  25. #ifdef EMULATE_3DNOWEXT
  26. #define PSWAPD(s,d)\
  27. "movq "#s","#d"\n"\
  28. "psrlq $32,"#d"\n"\
  29. "punpckldq "#s","#d"\n"
  30. #define ff_fft_calc_3dn2 ff_fft_calc_3dn
  31. #define ff_fft_dispatch_3dn2 ff_fft_dispatch_3dn
  32. #define ff_fft_dispatch_interleave_3dn2 ff_fft_dispatch_interleave_3dn
  33. #define ff_imdct_calc_3dn2 ff_imdct_calc_3dn
  34. #define ff_imdct_half_3dn2 ff_imdct_half_3dn
  35. #else
  36. #define PSWAPD(s,d) "pswapd "#s","#d"\n"
  37. #endif
  38. void ff_fft_dispatch_3dn2(FFTComplex *z, int nbits);
  39. void ff_fft_dispatch_interleave_3dn2(FFTComplex *z, int nbits);
  40. void ff_fft_calc_3dn2(FFTContext *s, FFTComplex *z)
  41. {
  42. int n = 1<<s->nbits;
  43. int i;
  44. ff_fft_dispatch_interleave_3dn2(z, s->nbits);
  45. __asm__ volatile("femms");
  46. if(n <= 8)
  47. for(i=0; i<n; i+=2)
  48. FFSWAP(FFTSample, z[i].im, z[i+1].re);
  49. }
  50. void ff_imdct_half_3dn2(FFTContext *s, FFTSample *output, const FFTSample *input)
  51. {
  52. x86_reg j, k;
  53. long n = s->mdct_size;
  54. long n2 = n >> 1;
  55. long n4 = n >> 2;
  56. long n8 = n >> 3;
  57. const uint16_t *revtab = s->revtab;
  58. const FFTSample *tcos = s->tcos;
  59. const FFTSample *tsin = s->tsin;
  60. const FFTSample *in1, *in2;
  61. FFTComplex *z = (FFTComplex *)output;
  62. /* pre rotation */
  63. in1 = input;
  64. in2 = input + n2 - 1;
  65. #ifdef EMULATE_3DNOWEXT
  66. __asm__ volatile("movd %0, %%mm7" ::"r"(1U<<31));
  67. #endif
  68. for(k = 0; k < n4; k++) {
  69. // FIXME a single block is faster, but gcc 2.95 and 3.4.x on 32bit can't compile it
  70. __asm__ volatile(
  71. "movd %0, %%mm0 \n"
  72. "movd %2, %%mm1 \n"
  73. "punpckldq %1, %%mm0 \n"
  74. "punpckldq %3, %%mm1 \n"
  75. "movq %%mm0, %%mm2 \n"
  76. PSWAPD( %%mm1, %%mm3 )
  77. "pfmul %%mm1, %%mm0 \n"
  78. "pfmul %%mm3, %%mm2 \n"
  79. #ifdef EMULATE_3DNOWEXT
  80. "movq %%mm0, %%mm1 \n"
  81. "punpckhdq %%mm2, %%mm0 \n"
  82. "punpckldq %%mm2, %%mm1 \n"
  83. "pxor %%mm7, %%mm0 \n"
  84. "pfadd %%mm1, %%mm0 \n"
  85. #else
  86. "pfpnacc %%mm2, %%mm0 \n"
  87. #endif
  88. ::"m"(in2[-2*k]), "m"(in1[2*k]),
  89. "m"(tcos[k]), "m"(tsin[k])
  90. );
  91. __asm__ volatile(
  92. "movq %%mm0, %0 \n\t"
  93. :"=m"(z[revtab[k]])
  94. );
  95. }
  96. ff_fft_dispatch_3dn2(z, s->nbits);
  97. #define CMUL(j,mm0,mm1)\
  98. "movq (%2,"#j",2), %%mm6 \n"\
  99. "movq 8(%2,"#j",2), "#mm0"\n"\
  100. "movq %%mm6, "#mm1"\n"\
  101. "movq "#mm0",%%mm7 \n"\
  102. "pfmul (%3,"#j"), %%mm6 \n"\
  103. "pfmul (%4,"#j"), "#mm0"\n"\
  104. "pfmul (%4,"#j"), "#mm1"\n"\
  105. "pfmul (%3,"#j"), %%mm7 \n"\
  106. "pfsub %%mm6, "#mm0"\n"\
  107. "pfadd %%mm7, "#mm1"\n"
  108. /* post rotation */
  109. j = -n2;
  110. k = n2-8;
  111. __asm__ volatile(
  112. "1: \n"
  113. CMUL(%0, %%mm0, %%mm1)
  114. CMUL(%1, %%mm2, %%mm3)
  115. "movd %%mm0, (%2,%0,2) \n"
  116. "movd %%mm1,12(%2,%1,2) \n"
  117. "movd %%mm2, (%2,%1,2) \n"
  118. "movd %%mm3,12(%2,%0,2) \n"
  119. "psrlq $32, %%mm0 \n"
  120. "psrlq $32, %%mm1 \n"
  121. "psrlq $32, %%mm2 \n"
  122. "psrlq $32, %%mm3 \n"
  123. "movd %%mm0, 8(%2,%0,2) \n"
  124. "movd %%mm1, 4(%2,%1,2) \n"
  125. "movd %%mm2, 8(%2,%1,2) \n"
  126. "movd %%mm3, 4(%2,%0,2) \n"
  127. "sub $8, %1 \n"
  128. "add $8, %0 \n"
  129. "jl 1b \n"
  130. :"+r"(j), "+r"(k)
  131. :"r"(z+n8), "r"(tcos+n8), "r"(tsin+n8)
  132. :"memory"
  133. );
  134. __asm__ volatile("femms");
  135. }
  136. void ff_imdct_calc_3dn2(FFTContext *s, FFTSample *output, const FFTSample *input)
  137. {
  138. x86_reg j, k;
  139. long n = s->mdct_size;
  140. long n4 = n >> 2;
  141. ff_imdct_half_3dn2(s, output+n4, input);
  142. j = -n;
  143. k = n-8;
  144. __asm__ volatile(
  145. "movq %4, %%mm7 \n"
  146. "1: \n"
  147. PSWAPD((%2,%1), %%mm0)
  148. PSWAPD((%3,%0), %%mm1)
  149. "pxor %%mm7, %%mm0 \n"
  150. "movq %%mm1, (%3,%1) \n"
  151. "movq %%mm0, (%2,%0) \n"
  152. "sub $8, %1 \n"
  153. "add $8, %0 \n"
  154. "jl 1b \n"
  155. :"+r"(j), "+r"(k)
  156. :"r"(output+n4), "r"(output+n4*3),
  157. "m"(*m1m1)
  158. );
  159. __asm__ volatile("femms");
  160. }