comparison ppc/fft_altivec.c @ 12046:ae57be2ef58c libavcodec

PPC: Altivec split-radix FFT 1.8x faster than altivec radix-2 on a G4 8% faster vorbis decoding Patch (mostly) by Loren Merritt
author mru
date Thu, 01 Jul 2010 23:21:39 +0000
parents 3cd4cd0509cd
children c80c7a717156
comparison
equal deleted inserted replaced
12045:f399ea693f46 12046:ae57be2ef58c
1 /* 1 /*
2 * FFT/IFFT transforms 2 * FFT/IFFT transforms
3 * AltiVec-enabled 3 * AltiVec-enabled
4 * Copyright (c) 2003 Romain Dolbeau <romain@dolbeau.org> 4 * Copyright (c) 2009 Loren Merritt
5 * Based on code Copyright (c) 2002 Fabrice Bellard
6 * 5 *
7 * This file is part of FFmpeg. 6 * This file is part of FFmpeg.
8 * 7 *
9 * FFmpeg is free software; you can redistribute it and/or 8 * FFmpeg is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public 9 * modify it under the terms of the GNU Lesser General Public
20 * License along with FFmpeg; if not, write to the Free Software 19 * License along with FFmpeg; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */ 21 */
23 #include "libavcodec/fft.h" 22 #include "libavcodec/fft.h"
24 #include "util_altivec.h" 23 #include "util_altivec.h"
25 #include "dsputil_altivec.h" 24 #include "types_altivec.h"
25 #include "regs.h"
26 26
27 /** 27 /**
28 * Do a complex FFT with the parameters defined in ff_fft_init(). The 28 * Do a complex FFT with the parameters defined in ff_fft_init(). The
29 * input data must be permuted before with s->revtab table. No 29 * input data must be permuted before with s->revtab table. No
30 * 1.0/sqrt(n) normalization is done. 30 * 1.0/sqrt(n) normalization is done.
31 * AltiVec-enabled 31 * AltiVec-enabled
32 * This code assumes that the 'z' pointer is 16 bytes-aligned 32 * This code assumes that the 'z' pointer is 16 bytes-aligned
33 * It also assumes all FFTComplex are 8 bytes-aligned pair of float 33 * It also assumes all FFTComplex are 8 bytes-aligned pair of float
34 * The code is exactly the same as the SSE version, except
35 * that successive MUL + ADD/SUB have been merged into
36 * fused multiply-add ('vec_madd' in altivec)
37 */ 34 */
35
36 // Pointers to functions. Not using function pointer syntax, because
37 // that involves an extra level of indirection on some PPC ABIs.
38 extern void *ff_fft_dispatch_altivec[2][15];
39
40 // Convert from simd order to C order.
41 static void swizzle(vec_f *z, int n)
42 {
43 int i;
44 n >>= 1;
45 for (i = 0; i < n; i += 2) {
46 vec_f re = z[i];
47 vec_f im = z[i+1];
48 z[i] = vec_mergeh(re, im);
49 z[i+1] = vec_mergel(re, im);
50 }
51 }
52
38 static void ff_fft_calc_altivec(FFTContext *s, FFTComplex *z) 53 static void ff_fft_calc_altivec(FFTContext *s, FFTComplex *z)
39 { 54 {
40 register const vector float vczero = (const vector float)vec_splat_u32(0.); 55 register vec_f v14 __asm__("v14") = {0,0,0,0};
41 56 register vec_f v15 __asm__("v15") = *(const vec_f*)ff_cos_16;
42 int ln = s->nbits; 57 register vec_f v16 __asm__("v16") = {0, 0.38268343, M_SQRT1_2, 0.92387953};
43 int j, np, np2; 58 register vec_f v17 __asm__("v17") = {-M_SQRT1_2, M_SQRT1_2, M_SQRT1_2,-M_SQRT1_2};
44 int nblocks, nloops; 59 register vec_f v18 __asm__("v18") = { M_SQRT1_2, M_SQRT1_2, M_SQRT1_2, M_SQRT1_2};
45 register FFTComplex *p, *q; 60 register vec_u8 v19 __asm__("v19") = vcprm(s0,3,2,1);
46 FFTComplex *cptr, *cptr1; 61 register vec_u8 v20 __asm__("v20") = vcprm(0,1,s2,s1);
47 int k; 62 register vec_u8 v21 __asm__("v21") = vcprm(2,3,s0,s3);
48 63 register vec_u8 v22 __asm__("v22") = vcprm(2,s3,3,s2);
49 np = 1 << ln; 64 register vec_u8 v23 __asm__("v23") = vcprm(0,1,s0,s1);
50 65 register vec_u8 v24 __asm__("v24") = vcprm(2,3,s2,s3);
51 { 66 register vec_u8 v25 __asm__("v25") = vcprm(2,3,0,1);
52 vector float *r, a, b, a1, c1, c2; 67 register vec_u8 v26 __asm__("v26") = vcprm(1,2,s3,s0);
53 68 register vec_u8 v27 __asm__("v27") = vcprm(0,3,s2,s1);
54 r = (vector float *)&z[0]; 69 register vec_u8 v28 __asm__("v28") = vcprm(0,2,s1,s3);
55 70 register vec_u8 v29 __asm__("v29") = vcprm(1,3,s0,s2);
56 c1 = vcii(p,p,n,n); 71 register FFTSample *const*cos_tabs __asm__("r12") = ff_cos_tabs;
57 72 register FFTComplex *zarg __asm__("r3") = z;
58 if (s->inverse) { 73 __asm__(
59 c2 = vcii(p,p,n,p); 74 "mtctr %0 \n"
60 } else { 75 "li "r(9)", 16 \n"
61 c2 = vcii(p,p,p,n); 76 "subi "r(1)","r(1) ",%1 \n"
62 } 77 "bctrl \n"
63 78 "addi "r(1)","r(1) ",%1 \n"
64 j = (np >> 2); 79 ::"r"(ff_fft_dispatch_altivec[1][s->nbits-2]), "i"(12*sizeof(void*)),
65 do { 80 "r"(zarg), "r"(cos_tabs),
66 a = vec_ld(0, r); 81 "v"(v14),"v"(v15),"v"(v16),"v"(v17),"v"(v18),"v"(v19),"v"(v20),"v"(v21),
67 a1 = vec_ld(sizeof(vector float), r); 82 "v"(v22),"v"(v23),"v"(v24),"v"(v25),"v"(v26),"v"(v27),"v"(v28),"v"(v29)
68 83 : "lr","ctr","r0","r4","r5","r6","r7","r8","r9","r10","r11",
69 b = vec_perm(a,a,vcprmle(1,0,3,2)); 84 "v0","v1","v2","v3","v4","v5","v6","v7","v8","v9","v10","v11","v12","v13"
70 a = vec_madd(a,c1,b); 85 );
71 /* do the pass 0 butterfly */ 86 if (s->nbits <= 4)
72 87 swizzle((vec_f*)z, 1<<s->nbits);
73 b = vec_perm(a1,a1,vcprmle(1,0,3,2));
74 b = vec_madd(a1,c1,b);
75 /* do the pass 0 butterfly */
76
77 /* multiply third by -i */
78 b = vec_perm(b,b,vcprmle(2,3,1,0));
79
80 /* do the pass 1 butterfly */
81 vec_st(vec_madd(b,c2,a), 0, r);
82 vec_st(vec_nmsub(b,c2,a), sizeof(vector float), r);
83
84 r += 2;
85 } while (--j != 0);
86 }
87 /* pass 2 .. ln-1 */
88
89 nblocks = np >> 3;
90 nloops = 1 << 2;
91 np2 = np >> 1;
92
93 cptr1 = s->exptab1;
94 do {
95 p = z;
96 q = z + nloops;
97 j = nblocks;
98 do {
99 cptr = cptr1;
100 k = nloops >> 1;
101 do {
102 vector float a,b,c,t1;
103
104 a = vec_ld(0, (float*)p);
105 b = vec_ld(0, (float*)q);
106
107 /* complex mul */
108 c = vec_ld(0, (float*)cptr);
109 /* cre*re cim*re */
110 t1 = vec_madd(c, vec_perm(b,b,vcprmle(2,2,0,0)),vczero);
111 c = vec_ld(sizeof(vector float), (float*)cptr);
112 /* -cim*im cre*im */
113 b = vec_madd(c, vec_perm(b,b,vcprmle(3,3,1,1)),t1);
114
115 /* butterfly */
116 vec_st(vec_add(a,b), 0, (float*)p);
117 vec_st(vec_sub(a,b), 0, (float*)q);
118
119 p += 2;
120 q += 2;
121 cptr += 4;
122 } while (--k);
123
124 p += nloops;
125 q += nloops;
126 } while (--j);
127 cptr1 += nloops * 2;
128 nblocks = nblocks >> 1;
129 nloops = nloops << 1;
130 } while (nblocks != 0);
131 } 88 }
132 89
133 av_cold void ff_fft_init_altivec(FFTContext *s) 90 av_cold void ff_fft_init_altivec(FFTContext *s)
134 { 91 {
135 s->fft_calc = ff_fft_calc_altivec; 92 if (HAVE_GNU_AS)
136 s->split_radix = 0; 93 s->fft_calc = ff_fft_calc_altivec;
94 s->split_radix = 1;
137 } 95 }