]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - ep-processor-libraries/dsplib.git/blob - ti/dsplib/src/DSPF_sp_fir_r2/c66/DSPF_sp_fir_r2_d.c
DSPLIB: optimized signal processing functions for TI DSPs
[ep-processor-libraries/dsplib.git] / ti / dsplib / src / DSPF_sp_fir_r2 / c66 / DSPF_sp_fir_r2_d.c
1 /* ======================================================================= */
2 /* DSPF_sp_fir_r2_d.c -- FIR Filter                                        */
3 /*                Driver code; tests kernel and reports result in stdout   */
4 /*                                                                         */
5 /* Rev 0.0.1                                                               */
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 <c6x.h>
46 /* ======================================================================= */
47 /* Interface header files for the natural C and optimized C code           */
48 /* ======================================================================= */
49 #include "DSPF_sp_fir_r2_cn.h"
50 #include "DSPF_sp_fir_r2.h"
52 /* Defines */
53 #if defined(__TI_EABI__)
54 #define kernel_size _kernel_size
55 #endif
57 extern char kernel_size;
58 #define FORMULA_SIZE          4
59 #define FORMULA_DEVIDE        8
60 #define CYCLE_FORMULA_NY_PT1  128
61 #define CYCLE_FORMULA_NY_PT2  112
62 #define CYCLE_FORMULA_NH_PT1  16
63 #define CYCLE_FORMULA_NH_PT2  12
65 /* inverse of [128*16 128 16 1] */
66 /*            [128*12 128 12 1] */
67 /*            [112*16 112 16 1] */
68 /*            [112*12 112 12 1] */
70 float form_inv[FORMULA_SIZE][FORMULA_SIZE] = 
71 {{  0.0156,   -0.0156,   -0.0156,    0.0156},
72  { -0.1875,    0.2500,    0.1875,   -0.2500},
73  { -1.7500,    1.7500,    2.0000,   -2.0000},
74  { 21.0000,  -28.0000,  -24.0000,   32.0000}
75 };
77 float form_temp  [FORMULA_SIZE];
78 int   form_cycle [FORMULA_SIZE];
79 int   form_result[FORMULA_SIZE];
81 /* ======================================================================= */
82 /* Tell compiler arrays are double word alligned                           */
83 /* ======================================================================= */
84 #pragma DATA_ALIGN(ptr_y_i, 8);
85 #pragma DATA_ALIGN(ptr_y_cn, 8);
86 #pragma DATA_ALIGN(ptr_h, 8);
87 #pragma DATA_ALIGN(ptr_x, 8);
89 /* ======================================================================= */
90 /* Parameters of fixed dataset                                             */
91 /* ======================================================================= */
92 #define NY (128)
93 #define NH (16)
95 float ptr_y_i[NY];
96 float ptr_y_cn[NY];
97 float ptr_h[NH + 4];
98 float ptr_x[NY + NH - 1 + 4];
100 /* ======================================================================= */
101 /* Prototypes for timing functions                                         */
102 /* ======================================================================= */
103 clock_t time_i(int ny, int nh);
104 clock_t time_cn(int ny, int nh);
106 /* ======================================================================= */
107 /* Prototypes for utility functions                                        */
108 /* ======================================================================= */
109 void UTIL_fillRandSP(float *ptr_x, int N, float factor);
110 void UTIL_fillRandFilterSP(float *ptr_h, int N);
112 /* ======================================================================= */
113 /* Main -- Top level driver for testing the algorithm                      */
114 /* ======================================================================= */
115 void main()
117     clock_t t_overhead, t_start, t_stop, t_opt, t_cn;
118     int i, j=1, ny, nh, form_error = 0;
119     float pct_diff, max_pct_diff = 0;
121     /* ------------------------------------------------------------------- */
122     /* Compute the overhead of calling clock twice to get timing info      */
123     /* ------------------------------------------------------------------- */
124     /* Initialize timer for clock */
125     TSCL= 0,TSCH=0;
126     t_start = _itoll(TSCH, TSCL);
127     t_stop = _itoll(TSCH, TSCL);
128     t_overhead = t_stop - t_start;
130     /* ------------------------------------------------------------------- */
131     /*  Generate random input (with zero padding) and MA filter.           */
132     /* ------------------------------------------------------------------- */
133     memset(ptr_x, 0, sizeof(ptr_x));
134     memset(ptr_h, 0, sizeof(ptr_h));
135     UTIL_fillRandSP(ptr_x + NH - 1, NY, 10.0);
136     UTIL_fillRandFilterSP(ptr_h, NH);
137  
138     for(ny = 32; ny <= NY; ny += 16) {
139       for(nh = 4; nh <= NH; nh += 2) {
140         /* --------------------------------------------------------------- */
141         /*  Clear state/output buffers with fixed values.                  */
142         /* --------------------------------------------------------------- */
143         memset(ptr_y_i,  0, sizeof(ptr_y_i));
144         memset(ptr_y_cn, 0, sizeof(ptr_y_cn));
146         /* --------------------------------------------------------------- */
147         /* Call the individual timing routines                             */
148         /* --------------------------------------------------------------- */
149         t_opt  = time_i(ny, nh) - t_overhead;
150         t_cn = time_cn(ny, nh)  - t_overhead;
152         printf("DSPF_sp_fir_r2\tIter#: %d\t", j++);
154         /* --------------------------------------------------------------- */
155         /* compute percent difference and track max difference             */
156         /* --------------------------------------------------------------- */
157         for(i=0; i<ny; i++) {
158           pct_diff = (ptr_y_cn[i] - ptr_y_i[i]) / ptr_y_cn[i] * 100.0;
159           if (pct_diff < 0) pct_diff *= -1;
160           if (pct_diff > max_pct_diff) max_pct_diff = pct_diff;
161         }
162         if (max_pct_diff > 0.01)
163             printf("Result Failure  max_pct_diff = %f", max_pct_diff);
164         else
165             printf("Result Successful ");
166             
167         /* --------------------------------------------------------------- */
168         /* Print timing results                                            */
169         /* --------------------------------------------------------------- */
170         printf("\tNX = %d\tNH = %d\tnatC: %d\toptC: %d\n", ny, nh, t_cn, t_opt);
172         if (ny == CYCLE_FORMULA_NY_PT1 && nh == CYCLE_FORMULA_NH_PT1)
173             form_cycle[0] = t_opt;
174         if (ny == CYCLE_FORMULA_NY_PT1 && nh == CYCLE_FORMULA_NH_PT2)
175             form_cycle[1] = t_opt;
176         if (ny == CYCLE_FORMULA_NY_PT2 && nh == CYCLE_FORMULA_NH_PT1)
177             form_cycle[2] = t_opt;
178         if (ny == CYCLE_FORMULA_NY_PT2 && nh == CYCLE_FORMULA_NH_PT2)
179             form_cycle[3] = t_opt;    
180       }
181     }
182     /* Provide memory information */
183 #ifdef __TI_COMPILER_VERSION__            // for TI compiler only
184     printf("Memory:  %d bytes\n", &kernel_size);
185 #endif
186     
187         /* Provide profiling information */
188     for (i = 0; i < FORMULA_SIZE; i++) {
189         form_temp[i] = 0;
190         for (j = 0; j < FORMULA_SIZE; j++) {
191             form_temp[i] += form_inv[i][j] * form_cycle[j];
192         }
193         if (i != (FORMULA_SIZE-1)) {
194             form_result[i] = (int) (form_temp[i] * FORMULA_DEVIDE + 0.5);
195             if ((form_result[i] - form_temp[i] * FORMULA_DEVIDE) >  0.1 ||
196                 (form_result[i] - form_temp[i] * FORMULA_DEVIDE) < -0.1) {
197                 form_error = 1;
198             }
199         }
200         else {
201             form_result[i] = (int) (form_temp[i] + 0.5);
202         }
203     }
205     if (!form_error) {
206         printf("Cycles:  ");
207         if (form_result[0])
208           printf("%d/%d*Nr*Nh + ", form_result[0], FORMULA_DEVIDE);
209         if (form_result[1])
210           printf("%d/%d*Nr + ", form_result[1], FORMULA_DEVIDE);
211         if (form_result[2])
212           printf("%d/%d*Nh + ", form_result[2], FORMULA_DEVIDE);
213         if (form_result[3])
214           printf("%d\n", form_result[3]);
215     }
216     else
217         printf("Cycles Formula Not Available\n");    
220 /* ======================================================================= */
221 /* Prototypes for timing functions                                         */
222 /* ======================================================================= */
223 clock_t time_cn(int ny, int nh)
225     clock_t t_start, t_stop;
227     /* ------------------------------------------------------------------- */
228     /* Measure the cycle count                                             */
229     /* ------------------------------------------------------------------- */
230     t_start = _itoll(TSCH, TSCL);
231     DSPF_sp_fir_r2_cn(ptr_x, ptr_h, ptr_y_cn, nh, ny);
232     t_stop = _itoll(TSCH, TSCL);
234     return t_stop - t_start;
237 clock_t time_i(int ny, int nh)
239     clock_t t_start, t_stop;
240     
241     /* ------------------------------------------------------------------- */
242     /* Measure the cycle count                                             */
243     /* ------------------------------------------------------------------- */
244     t_start = _itoll(TSCH, TSCL);
245     DSPF_sp_fir_r2(ptr_x, ptr_h, ptr_y_i, nh, ny);
246     t_stop = _itoll(TSCH, TSCL);
248     return t_stop - t_start;
251 void UTIL_fillRandSP(float *ptr_x, int N, float factor)
253     float rand_midpoint = RAND_MAX / 2.0;
254     int i;
256     // fill array with floats in the range (-factor, factor)
257     for (i = 0; i < N; i++)
258         ptr_x[i] = ((rand() - rand_midpoint) / rand_midpoint) * factor;
261 void UTIL_fillRandFilterSP(float *ptr_h, int N)
263     float frand_max = RAND_MAX, pair_sum = 2.0 / N;
264     int i;
266     // create a moving average filter of length N
267     // filter taps must be non-negative and sum to 1
268     // N must be an even number
269     for (i = 0; i < N / 2; i++)
270     {
271         ptr_h[i] = (rand() / frand_max) * pair_sum;
272         ptr_h[N - 1 - i] = pair_sum - ptr_h[i];
273     }
275 /* ======================================================================= */
276 /*  End of file:  DSPF_sp_fir_r2_d.c                                       */
277 /* ----------------------------------------------------------------------- */
278 /*            Copyright (c) 2011 Texas Instruments, Incorporated.          */
279 /*                           All Rights Reserved.                          */
280 /* ======================================================================= */