[ep-processor-libraries/dsplib.git] / ti / dsplib / src / DSPF_sp_fftSPxSP_r2c / c66 / DSPF_sp_fftSPxSP_r2c_cn.c
1 /* ======================================================================= */
2 /* DSPF_sp_fftSPxSP_r2c_cn.c -- Forward FFT with Mixed Radix */
3 /* Natural C Implementation */
4 /* */
5 /* Rev 0.0.3 */
6 /* */
7 /* Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/ */
8 /* */
9 /* */
10 /* Redistribution and use in source and binary forms, with or without */
11 /* modification, are permitted provided that the following conditions */
12 /* are met: */
13 /* */
14 /* Redistributions of source code must retain the above copyright */
15 /* notice, this list of conditions and the following disclaimer. */
16 /* */
17 /* Redistributions in binary form must reproduce the above copyright */
18 /* notice, this list of conditions and the following disclaimer in the */
19 /* documentation and/or other materials provided with the */
20 /* distribution. */
21 /* */
22 /* Neither the name of Texas Instruments Incorporated nor the names of */
23 /* its contributors may be used to endorse or promote products derived */
24 /* from this software without specific prior written permission. */
25 /* */
26 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS */
27 /* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT */
28 /* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR */
29 /* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT */
30 /* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, */
31 /* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT */
32 /* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, */
33 /* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY */
34 /* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */
35 /* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE */
36 /* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
37 /* */
38 /* ======================================================================= */
40 #pragma CODE_SECTION(DSPF_sp_fftSPxSP_r2c_cn, ".text:natural");
42 #include "DSPF_sp_fftSPxSP_r2c_cn.h"
44 #ifndef _TMS320C6x
46 unsigned int bitr(unsigned int a)
47 {
48 unsigned int i, b=0;
50 for(i=0; i<32; i++)
51 {
52 b |= (((a >> i) & 0x1) << (31 -i));
53 }
55 return(b);
56 }
58 #endif
60 void DSPF_sp_fftSPxSP_r2c_cn (int N, float *ptr_x, float *ptr_w, float *ptr_y,
61 unsigned char *brev, int n_min, int offset, int n_max)
62 {
64 int i, j, k, l1, l2, h2, predj, l0, radix;
65 int tw_offset, stride, fft_jmp;
66 float x0, x1, x2, x3, x4, x5, x6, x7;
67 float xt0, yt0, xt1, yt1, xt2, yt2, yt3;
68 float yt4, yt5, yt6, yt7;
69 float si1, si2, si3, co1, co2, co3;
70 float xh0, xh1, xh20, xh21, xl0, xl1, xl20, xl21;
71 float x_0, x_1, x_l1, x_l1p1, x_h2, x_h2p1, x_l2, x_l2p1;
72 float xl0_0, xl1_0, xl0_1, xl1_1;
73 float xh0_0, xh1_0, xh0_1, xh1_1;
74 float *x, *w, *y0, *ptr_x0, *ptr_x2;
76 radix = n_min;
77 stride = N; /* n is the number of complex samples */
78 tw_offset = 0;
79 while (stride > radix)
80 {
81 j = 0;
82 fft_jmp = stride + (stride >> 1);
83 h2 = stride >> 1;
84 l1 = stride;
85 l2 = stride + (stride >> 1);
86 x = ptr_x;
87 w = ptr_w + tw_offset;
89 for (i = 0; i < N; i += 4)
90 {
91 #ifdef _LITTLE_ENDIAN
92 si1 = w[j];
93 co1 = w[j + 1];
94 si2 = w[j + 2];
95 co2 = w[j + 3];
96 si3 = w[j + 4];
97 co3 = w[j + 5];
98 #else
99 co1 = w[j];
100 si1 = -w[j + 1];
101 co2 = w[j + 2];
102 si2 = -w[j + 3];
103 co3 = w[j + 4];
104 si3 = -w[j + 5];
105 #endif
106 x_0 = x[0];
107 x_1 = x[1];
108 x_h2 = x[h2];
109 x_h2p1 = x[h2 + 1];
110 x_l1 = x[l1];
111 x_l1p1 = x[l1 + 1];
112 x_l2 = x[l2];
113 x_l2p1 = x[l2 + 1];
115 xh0 = x_0 + x_l1;
116 xh1 = x_1 + x_l1p1;
117 xl0 = x_0 - x_l1;
118 xl1 = x_1 - x_l1p1;
120 xh20 = x_h2 + x_l2;
121 xh21 = x_h2p1 + x_l2p1;
122 xl20 = x_h2 - x_l2;
123 xl21 = x_h2p1 - x_l2p1;
125 ptr_x0 = x;
126 ptr_x0[0] = xh0 + xh20;
127 ptr_x0[1] = xh1 + xh21;
129 ptr_x2 = ptr_x0;
130 x += 2;
131 j += 6;
132 predj = (j - fft_jmp);
133 if (!predj)
134 x += fft_jmp;
135 if (!predj)
136 j = 0;
138 xt0 = xh0 - xh20;
139 yt0 = xh1 - xh21;
140 xt1 = xl0 + xl21;
141 yt2 = xl1 + xl20;
142 xt2 = xl0 - xl21;
143 yt1 = xl1 - xl20;
145 ptr_x2[l1] = xt1 * co1 + yt1 * si1;
146 ptr_x2[l1 + 1] = yt1 * co1 - xt1 * si1;
147 ptr_x2[h2] = xt0 * co2 + yt0 * si2;
148 ptr_x2[h2 + 1] = yt0 * co2 - xt0 * si2;
149 ptr_x2[l2] = xt2 * co3 + yt2 * si3;
150 ptr_x2[l2 + 1] = yt2 * co3 - xt2 * si3;
151 }
152 tw_offset += fft_jmp;
153 stride = stride >> 2;
154 } /* end while */
156 j = offset >> 2;
157 ptr_x0 = ptr_x;
158 y0 = ptr_y;
160 /* l0 = _norm(n_max) +3; get size of fft */
161 l0 = 0;
162 for (k = 30; k >= 0; k--)
163 if ((n_max & (1 << k)) == 0)
164 l0++;
165 else
166 break;
167 l0 = l0 + 3;
168 if (radix <= 4)
169 for (i = 0; i < N; i += 4)
170 {
171 /* reversal computation */
172 k = bitr(j) >> l0;
173 j++; /* multiple of 4 index */
175 x0 = ptr_x0[0];
176 x1 = ptr_x0[1];
177 x2 = ptr_x0[2];
178 x3 = ptr_x0[3];
179 x4 = ptr_x0[4];
180 x5 = ptr_x0[5];
181 x6 = ptr_x0[6];
182 x7 = ptr_x0[7];
183 ptr_x0 += 8;
185 xh0_0 = x0 + x4;
186 xh1_0 = x1 + x5;
187 xh0_1 = x2 + x6;
188 xh1_1 = x3 + x7;
190 if (radix == 2)
191 {
192 xh0_0 = x0;
193 xh1_0 = x1;
194 xh0_1 = x2;
195 xh1_1 = x3;
196 }
198 yt0 = xh0_0 + xh0_1;
199 yt1 = xh1_0 + xh1_1;
200 yt4 = xh0_0 - xh0_1;
201 yt5 = xh1_0 - xh1_1;
203 xl0_0 = x0 - x4;
204 xl1_0 = x1 - x5;
205 xl0_1 = x2 - x6;
206 xl1_1 = x3 - x7;
208 if (radix == 2)
209 {
210 xl0_0 = x4;
211 xl1_0 = x5;
212 xl1_1 = x6;
213 xl0_1 = x7;
214 }
216 yt2 = xl0_0 + xl1_1;
217 yt3 = xl1_0 - xl0_1;
218 yt6 = xl0_0 - xl1_1;
219 yt7 = xl1_0 + xl0_1;
221 if (radix == 2)
222 {
223 yt7 = xl1_0 - xl0_1;
224 yt3 = xl1_0 + xl0_1;
225 }
227 y0[k] = yt0;
228 y0[k + 1] = yt1;
229 k += n_max >> 1;
230 y0[k] = yt2;
231 y0[k + 1] = yt3;
232 k += n_max >> 1;
233 y0[k] = yt4;
234 y0[k + 1] = yt5;
235 k += n_max >> 1;
236 y0[k] = yt6;
237 y0[k + 1] = yt7;
238 }
239 }
241 /* ======================================================================= */
242 /* End of file: DSPF_sp_fftSPxSP_r2c_cn.c */
243 /* ----------------------------------------------------------------------- */
244 /* Copyright (c) 2011 Texas Instruments, Incorporated. */
245 /* All Rights Reserved. */
246 /* ======================================================================= */