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.

246 lines
6.6KB

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