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.

250 lines
6.5KB

  1. /*
  2. * FFT/IFFT transforms
  3. * AltiVec-enabled
  4. * Copyright (c) 2003 Romain Dolbeau <romain@dolbeau.org>
  5. * Based on code Copyright (c) 2002 Fabrice Bellard.
  6. *
  7. * This file is part of FFmpeg.
  8. *
  9. * FFmpeg is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2.1 of the License, or (at your option) any later version.
  13. *
  14. * FFmpeg is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with FFmpeg; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. */
  23. #include "../dsputil.h"
  24. #include "gcc_fixes.h"
  25. #include "dsputil_altivec.h"
  26. /*
  27. those three macros are from libavcodec/fft.c
  28. and are required for the reference C code
  29. */
  30. /* butter fly op */
  31. #define BF(pre, pim, qre, qim, pre1, pim1, qre1, qim1) \
  32. {\
  33. FFTSample ax, ay, bx, by;\
  34. bx=pre1;\
  35. by=pim1;\
  36. ax=qre1;\
  37. ay=qim1;\
  38. pre = (bx + ax);\
  39. pim = (by + ay);\
  40. qre = (bx - ax);\
  41. qim = (by - ay);\
  42. }
  43. #define MUL16(a,b) ((a) * (b))
  44. #define CMUL(pre, pim, are, aim, bre, bim) \
  45. {\
  46. pre = (MUL16(are, bre) - MUL16(aim, bim));\
  47. pim = (MUL16(are, bim) + MUL16(bre, aim));\
  48. }
  49. /**
  50. * Do a complex FFT with the parameters defined in ff_fft_init(). The
  51. * input data must be permuted before with s->revtab table. No
  52. * 1.0/sqrt(n) normalization is done.
  53. * AltiVec-enabled
  54. * This code assumes that the 'z' pointer is 16 bytes-aligned
  55. * It also assumes all FFTComplex are 8 bytes-aligned pair of float
  56. * The code is exactly the same as the SSE version, except
  57. * that successive MUL + ADD/SUB have been merged into
  58. * fused multiply-add ('vec_madd' in altivec)
  59. */
  60. void ff_fft_calc_altivec(FFTContext *s, FFTComplex *z)
  61. {
  62. POWERPC_PERF_DECLARE(altivec_fft_num, s->nbits >= 6);
  63. #ifdef ALTIVEC_USE_REFERENCE_C_CODE
  64. int ln = s->nbits;
  65. int j, np, np2;
  66. int nblocks, nloops;
  67. register FFTComplex *p, *q;
  68. FFTComplex *exptab = s->exptab;
  69. int l;
  70. FFTSample tmp_re, tmp_im;
  71. POWERPC_PERF_START_COUNT(altivec_fft_num, s->nbits >= 6);
  72. np = 1 << ln;
  73. /* pass 0 */
  74. p=&z[0];
  75. j=(np >> 1);
  76. do {
  77. BF(p[0].re, p[0].im, p[1].re, p[1].im,
  78. p[0].re, p[0].im, p[1].re, p[1].im);
  79. p+=2;
  80. } while (--j != 0);
  81. /* pass 1 */
  82. p=&z[0];
  83. j=np >> 2;
  84. if (s->inverse) {
  85. do {
  86. BF(p[0].re, p[0].im, p[2].re, p[2].im,
  87. p[0].re, p[0].im, p[2].re, p[2].im);
  88. BF(p[1].re, p[1].im, p[3].re, p[3].im,
  89. p[1].re, p[1].im, -p[3].im, p[3].re);
  90. p+=4;
  91. } while (--j != 0);
  92. } else {
  93. do {
  94. BF(p[0].re, p[0].im, p[2].re, p[2].im,
  95. p[0].re, p[0].im, p[2].re, p[2].im);
  96. BF(p[1].re, p[1].im, p[3].re, p[3].im,
  97. p[1].re, p[1].im, p[3].im, -p[3].re);
  98. p+=4;
  99. } while (--j != 0);
  100. }
  101. /* pass 2 .. ln-1 */
  102. nblocks = np >> 3;
  103. nloops = 1 << 2;
  104. np2 = np >> 1;
  105. do {
  106. p = z;
  107. q = z + nloops;
  108. for (j = 0; j < nblocks; ++j) {
  109. BF(p->re, p->im, q->re, q->im,
  110. p->re, p->im, q->re, q->im);
  111. p++;
  112. q++;
  113. for(l = nblocks; l < np2; l += nblocks) {
  114. CMUL(tmp_re, tmp_im, exptab[l].re, exptab[l].im, q->re, q->im);
  115. BF(p->re, p->im, q->re, q->im,
  116. p->re, p->im, tmp_re, tmp_im);
  117. p++;
  118. q++;
  119. }
  120. p += nloops;
  121. q += nloops;
  122. }
  123. nblocks = nblocks >> 1;
  124. nloops = nloops << 1;
  125. } while (nblocks != 0);
  126. POWERPC_PERF_STOP_COUNT(altivec_fft_num, s->nbits >= 6);
  127. #else /* ALTIVEC_USE_REFERENCE_C_CODE */
  128. #ifdef CONFIG_DARWIN
  129. register const vector float vczero = (const vector float)(0.);
  130. #else
  131. register const vector float vczero = (const vector float){0.,0.,0.,0.};
  132. #endif
  133. int ln = s->nbits;
  134. int j, np, np2;
  135. int nblocks, nloops;
  136. register FFTComplex *p, *q;
  137. FFTComplex *cptr, *cptr1;
  138. int k;
  139. POWERPC_PERF_START_COUNT(altivec_fft_num, s->nbits >= 6);
  140. np = 1 << ln;
  141. {
  142. vector float *r, a, b, a1, c1, c2;
  143. r = (vector float *)&z[0];
  144. c1 = vcii(p,p,n,n);
  145. if (s->inverse)
  146. {
  147. c2 = vcii(p,p,n,p);
  148. }
  149. else
  150. {
  151. c2 = vcii(p,p,p,n);
  152. }
  153. j = (np >> 2);
  154. do {
  155. a = vec_ld(0, r);
  156. a1 = vec_ld(sizeof(vector float), r);
  157. b = vec_perm(a,a,vcprmle(1,0,3,2));
  158. a = vec_madd(a,c1,b);
  159. /* do the pass 0 butterfly */
  160. b = vec_perm(a1,a1,vcprmle(1,0,3,2));
  161. b = vec_madd(a1,c1,b);
  162. /* do the pass 0 butterfly */
  163. /* multiply third by -i */
  164. b = vec_perm(b,b,vcprmle(2,3,1,0));
  165. /* do the pass 1 butterfly */
  166. vec_st(vec_madd(b,c2,a), 0, r);
  167. vec_st(vec_nmsub(b,c2,a), sizeof(vector float), r);
  168. r += 2;
  169. } while (--j != 0);
  170. }
  171. /* pass 2 .. ln-1 */
  172. nblocks = np >> 3;
  173. nloops = 1 << 2;
  174. np2 = np >> 1;
  175. cptr1 = s->exptab1;
  176. do {
  177. p = z;
  178. q = z + nloops;
  179. j = nblocks;
  180. do {
  181. cptr = cptr1;
  182. k = nloops >> 1;
  183. do {
  184. vector float a,b,c,t1;
  185. a = vec_ld(0, (float*)p);
  186. b = vec_ld(0, (float*)q);
  187. /* complex mul */
  188. c = vec_ld(0, (float*)cptr);
  189. /* cre*re cim*re */
  190. t1 = vec_madd(c, vec_perm(b,b,vcprmle(2,2,0,0)),vczero);
  191. c = vec_ld(sizeof(vector float), (float*)cptr);
  192. /* -cim*im cre*im */
  193. b = vec_madd(c, vec_perm(b,b,vcprmle(3,3,1,1)),t1);
  194. /* butterfly */
  195. vec_st(vec_add(a,b), 0, (float*)p);
  196. vec_st(vec_sub(a,b), 0, (float*)q);
  197. p += 2;
  198. q += 2;
  199. cptr += 4;
  200. } while (--k);
  201. p += nloops;
  202. q += nloops;
  203. } while (--j);
  204. cptr1 += nloops * 2;
  205. nblocks = nblocks >> 1;
  206. nloops = nloops << 1;
  207. } while (nblocks != 0);
  208. POWERPC_PERF_STOP_COUNT(altivec_fft_num, s->nbits >= 6);
  209. #endif /* ALTIVEC_USE_REFERENCE_C_CODE */
  210. }