[ep-processor-libraries/dsplib.git] / ti / dsplib / src / DSPF_sp_fftSPxSP_r2c / c66 / DSPF_sp_fftSPxSP_r2c_d.c
1 /* ======================================================================== */
2 /* DSPF_sp_fftSPxSP_r2c_d.c -- Real to Complex FFT with Mixed Radix */
3 /* Driver code; tests kernel and reports result in stdout */
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 #include <stdio.h>
41 #include <time.h>
42 #include <stdlib.h>
43 #include <limits.h>
44 #include <math.h>
45 #include <c6x.h>
47 #include "DSPF_sp_fftSPxSP_r2c.h"
48 #include "DSPF_sp_fftSPxSP_r2c_cn.h"
50 /* Defines */
51 #if defined(__TI_EABI__)
52 #define kernel_size _kernel_size
53 #endif
54 extern char kernel_size;
56 #define CYCLE_FORMULA_NX_PT1 128
57 #define CYCLE_FORMULA_NX_PT2 256
58 #define FORMULA_SIZE 2
59 int form_cycle [FORMULA_SIZE];
61 void tw_gen (float *w, int n);
62 void tw_gen_cn (float *w, int n);
64 /* ======================================================================== */
65 /* Kernel-specific alignments */
66 /* ======================================================================== */
67 #pragma DATA_ALIGN(x_i, 8);
68 #pragma DATA_ALIGN(x_cn, 8);
70 #pragma DATA_ALIGN(w_i, 8);
71 #pragma DATA_ALIGN(w_cn, 8);
73 #pragma DATA_ALIGN(y_i, 8);
74 #pragma DATA_ALIGN(y_cn, 8);
75 #pragma DATA_ALIGN(brev, 8);
77 /* ======================================================================== */
78 /* Parameters of fixed dataset. */
79 /* ======================================================================== */
80 #define MAXN (1024)
81 #define M (2*MAXN)
82 #define PAD (16)
84 /* ======================================================================== */
85 /* Initialized arrays with fixed test data. */
86 /* ======================================================================== */
88 float x_i [M + 2 * PAD];
89 float x_cn[M + 2 * PAD];
91 float w_i [M + 2 * PAD];
92 float w_cn[M + 2 * PAD];
94 float y_i [M + 2 * PAD];
95 float y_cn[M + 2 * PAD];
97 /* ======================================================================== */
98 /* Generate pointers to skip beyond array padding */
99 /* ======================================================================== */
100 float *const ptr_x_i = x_i + PAD;
101 float *const ptr_x_cn = x_cn + PAD;
103 float *const ptr_w_i = w_i + PAD;
104 float *const ptr_w_cn = w_cn + PAD;
106 float *const ptr_y_i = y_i + PAD;
107 float *const ptr_y_cn = y_cn + PAD;
109 unsigned char brev[64] = {
110 0x0, 0x20, 0x10, 0x30, 0x8, 0x28, 0x18, 0x38,
111 0x4, 0x24, 0x14, 0x34, 0xc, 0x2c, 0x1c, 0x3c,
112 0x2, 0x22, 0x12, 0x32, 0xa, 0x2a, 0x1a, 0x3a,
113 0x6, 0x26, 0x16, 0x36, 0xe, 0x2e, 0x1e, 0x3e,
114 0x1, 0x21, 0x11, 0x31, 0x9, 0x29, 0x19, 0x39,
115 0x5, 0x25, 0x15, 0x35, 0xd, 0x2d, 0x1d, 0x3d,
116 0x3, 0x23, 0x13, 0x33, 0xb, 0x2b, 0x1b, 0x3b,
117 0x7, 0x27, 0x17, 0x37, 0xf, 0x2f, 0x1f, 0x3f
118 };
120 /* ======================================================================== */
121 /* MAIN -- Top level driver for the test. */
122 /* ======================================================================== */
124 int main ()
125 {
126 int i, j, rad_cn, rad_i, N, k;
127 clock_t t_start, t_stop, t_overhead, t_opt, t_cn;
128 float diff, max_diff;
130 for (N = 32, k = 1; N <= MAXN; N = N * 2, k++)
131 {
133 memset (x_i, 0x55, sizeof (x_i) );
134 memset (x_cn, 0x55, sizeof (x_cn));
136 /* ---------------------------------------------------------------- */
137 /* Initialize input vector temporarily. */
138 /* ---------------------------------------------------------------- */
139 for (i = 0; i < N; i++)
140 {
141 x_cn[PAD + 2*i] = sin (2 * 3.1415 * 50 * i / (double) N);
142 x_cn[PAD + 2*i + 1] = 0;
143 x_i [PAD + i] = x_cn[PAD + 2*i];
144 }
146 /* ---------------------------------------------------------------- */
147 /* Force uninitialized arrays to fixed values. */
148 /* ---------------------------------------------------------------- */
149 memset (y_i, 0xA5, sizeof (y_i) );
150 memset (y_cn, 0xA5, sizeof (y_cn));
152 /* ---------------------------------------------------------------- */
153 /* Initialize timer for clock */
154 TSCL= 0,TSCH=0;
155 /* Compute the overhead of calling _itoll(TSCH, TSCL) twice to get timing info */
156 /* ---------------------------------------------------------------- */
157 t_start = _itoll(TSCH, TSCL);
158 t_stop = _itoll(TSCH, TSCL);
159 t_overhead = t_stop - t_start;
161 /* ---------------------------------------------------------------- */
162 /* Generate twiddle factors. */
163 /* ---------------------------------------------------------------- */
164 j = 0;
165 for (i = 0; i <= 31; i++)
166 if ((N & (1 << i)) == 0)
167 j++;
168 else
169 break;
171 if (j % 2 == 0) {
172 rad_cn = 4;
173 rad_i = 2;
174 }
175 else {
176 rad_cn = 2;
177 rad_i = 4;
178 }
180 tw_gen (ptr_w_i, N);
181 tw_gen_cn (ptr_w_cn, N);
183 printf("DSPF_sp_fftSPxSP\tIter#: %d\t", k);
184 t_start = _itoll(TSCH, TSCL);
185 DSPF_sp_fftSPxSP_r2c_cn (N, &ptr_x_cn[0], &ptr_w_cn[0], ptr_y_cn, brev, rad_cn, 0, N);
186 t_stop = _itoll(TSCH, TSCL);
187 t_cn = (t_stop - t_start) - t_overhead;
189 t_start = _itoll(TSCH, TSCL);
190 DSPF_sp_fftSPxSP_r2c (N, &ptr_x_i[0], &ptr_w_i[0], ptr_y_i, brev, rad_i, 0, N);
191 t_stop = _itoll(TSCH, TSCL);
192 t_opt = (t_stop - t_start) - t_overhead;
194 /* ---------------------------------------------------------------- */
195 /* compute percent difference and track max difference */
196 /* ---------------------------------------------------------------- */
197 diff = 0; max_diff = 0;
198 for(i=0; i<N; i++) {
199 diff = ptr_y_cn[i] - ptr_y_i[i];
200 if (diff < 0) diff *= -1;
201 if (diff > max_diff)
202 max_diff = diff;
203 }
205 if (max_diff > 0.01)
206 printf("Intrinsic Failure max_diff = %f", max_diff);
207 else
208 printf("Intrinsic Successful ");
210 printf("\tN = %d\tnatC: %d\toptC: %d\n", N, t_cn, t_opt);
212 if (N == CYCLE_FORMULA_NX_PT1)
213 form_cycle[0] = t_opt;
214 if (N == CYCLE_FORMULA_NX_PT2)
215 form_cycle[1] = t_opt;
216 }
218 /* Provide memory information */
219 #ifdef __TI_COMPILER_VERSION__ // for TI compiler only
220 printf("Memory: %d bytes\n", &kernel_size);
221 #endif
223 /* Provide profiling information */
224 printf("Cycles: %d (N=128) %d (N=256)\n", form_cycle[0], form_cycle[1]);
225 }
227 /* Function for generating Specialized sequence of twiddle factors */
228 void tw_gen_cn (float *w, int n)
229 {
230 int i, j, k;
231 const double PI = 3.141592654;
233 for (j = 1, k = 0; j <= n >> 2; j = j << 2)
234 {
235 for (i = 0; i < n >> 2; i += j)
236 {
237 #ifdef _LITTLE_ENDIAN
238 w[k] = (float) sin (2 * PI * i / n);
239 w[k + 1] = (float) cos (2 * PI * i / n);
240 w[k + 2] = (float) sin (4 * PI * i / n);
241 w[k + 3] = (float) cos (4 * PI * i / n);
242 w[k + 4] = (float) sin (6 * PI * i / n);
243 w[k + 5] = (float) cos (6 * PI * i / n);
244 #else
245 w[k] = (float) cos (2 * PI * i / n);
246 w[k + 1] = (float) -sin (2 * PI * i / n);
247 w[k + 2] = (float) cos (4 * PI * i / n);
248 w[k + 3] = (float) -sin (4 * PI * i / n);
249 w[k + 4] = (float) cos (6 * PI * i / n);
250 w[k + 5] = (float) -sin (6 * PI * i / n);
251 #endif
252 k += 6;
253 }
254 }
255 }
257 void tw_gen (float *w, int n)
258 {
259 int i, j, k;
260 const double PI = 3.141592654;
262 for (i = 1, k = 0; i < n >> 2; i++) {
263 #ifdef _LITTLE_ENDIAN
264 w[k ] = sin (2 * PI * i / n);
265 w[k + 1] = cos (2 * PI * i / n);
266 #else
267 w[k ] = cos (2 * PI * i / n);
268 w[k + 1] = -sin (2 * PI * i / n);
269 #endif
270 k += 2;
271 }
273 for (j = 1; j <= n >> 3; j = j << 2) {
274 for (i = 0; i < n >> 3; i += j) {
275 #ifdef _LITTLE_ENDIAN
276 w[k] = (float) sin ( 4 * PI * i / n);
277 w[k + 1] = (float) cos ( 4 * PI * i / n);
278 w[k + 2] = (float) sin ( 8 * PI * i / n);
279 w[k + 3] = (float) cos ( 8 * PI * i / n);
280 w[k + 4] = (float) sin (12 * PI * i / n);
281 w[k + 5] = (float) cos (12 * PI * i / n);
282 #else
283 w[k] = (float) cos ( 4 * PI * i / n);
284 w[k + 1] = (float) -sin ( 4 * PI * i / n);
285 w[k + 2] = (float) cos ( 8 * PI * i / n);
286 w[k + 3] = (float) -sin ( 8 * PI * i / n);
287 w[k + 4] = (float) cos (12 * PI * i / n);
288 w[k + 5] = (float) -sin (12 * PI * i / n);
289 #endif
290 k += 6;
291 }
292 }
293 }
295 /* ======================================================================= */
296 /* End of file: DSPF_sp_fftSPxSP_r2c_d.c */
297 /* ----------------------------------------------------------------------- */
298 /* Copyright (c) 2011 Texas Instruments, Incorporated. */
299 /* All Rights Reserved. */
300 /* ======================================================================= */