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
6.7KB

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