]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - ep-processor-libraries/dsplib.git/blob - ti/dsplib/src/DSPF_sp_iir/c674/DSPF_sp_iir_d.c
DSPLIB: optimized signal processing functions for TI DSPs
[ep-processor-libraries/dsplib.git] / ti / dsplib / src / DSPF_sp_iir / c674 / DSPF_sp_iir_d.c
1 /* ======================================================================= */
2 /* DSP_iir_d.c -- IIR Filter                                               */
3 /*                Driver code; tests kernel and reports result in stdout   */
4 /*                                                                         */
5 /* Rev 0.0.2                                                               */
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_iir_cn.h"
50 #include "DSPF_sp_iir.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          2
59 #define FORMULA_DEVIDE        2//1
60 #define CYCLE_FORMULA_NX_PT1  64
61 #define CYCLE_FORMULA_NX_PT2  128
62 /* inverse of [ 64 1] */
63 /*            [128 1] */
64 float form_inv[FORMULA_SIZE][FORMULA_SIZE] = 
65 {{-0.0156,  0.0156},
66  { 2.0000, -1.0000}
67 };
68 float form_temp  [FORMULA_SIZE];
69 int   form_cycle [FORMULA_SIZE];
70 int   form_result[FORMULA_SIZE];
72 /* ======================================================================= */
73 /* Tell compiler arrays are double word alligned                           */
74 /* ======================================================================= */
75 #pragma DATA_ALIGN(ptr_y1_opt, 8);
76 #pragma DATA_ALIGN(ptr_y2_opt, 8);
77 #pragma DATA_ALIGN(ptr_y1_asm, 8);
78 #pragma DATA_ALIGN(ptr_y2_asm, 8);
79 #pragma DATA_ALIGN(ptr_y1_cn, 8);
80 #pragma DATA_ALIGN(ptr_y2_cn, 8);
81 #pragma DATA_ALIGN(ptr_ha, 8);
82 #pragma DATA_ALIGN(ptr_hb, 8);
83 #pragma DATA_ALIGN(ptr_x, 8);
85 /* ======================================================================= */
86 /* Parameters of fixed dataset                                             */
87 /* ======================================================================= */
88 #define N (128)
89 #define PAD (4)
91 float ptr_y1_opt[N + PAD];
92 float ptr_y2_opt[N + PAD];
93 float ptr_y1_asm[N + PAD];
94 float ptr_y2_asm[N + PAD];
96 float ptr_y1_cn[N + PAD];
97 float ptr_y2_cn[N + PAD];
99 float ptr_x[N + PAD];
101 /* ======================================================================= */
102 /* Initilized arrays with fixed test data                                  */
103 /* ======================================================================= */
104 float ptr_ha[PAD + 1] = { 1, -0.390641, 0.534300, -0.084233, 0.020651 };
105 float ptr_hb[PAD + 1] = { 0.067504, 0.270019, 0.405028, 0.270019, 0.067504 };
107 /* ======================================================================= */
108 /* Prototypes for timing functions                                         */
109 /* ======================================================================= */
110 clock_t time_opt(int n);
111 clock_t time_i(int n);
112 clock_t time_cn(int n);
114 /* ======================================================================= */
115 /* Prototypes for utility functions                                        */
116 /* ======================================================================= */
117 void UTIL_fillRandSP(float *ptr_x, int n, float factor);
119 /* ======================================================================= */
120 /* Main -- Top level driver for testing the algorithm                      */
121 /* ======================================================================= */
122 void main()
124     clock_t t_overhead, t_start, t_stop, t_opt, t_i, t_cn;
125     float pct_diff, max_pct_diff = 0;
126     int i, j=1, n, form_error = 0;
128     /* ------------------------------------------------------------------- */
129     /* Compute the overhead of calling clock twice to get timing info      */
130     /* ------------------------------------------------------------------- */
131     /* Initialize timer for clock */
132     TSCL= 0,TSCH=0;
133     t_start = _itoll(TSCH, TSCL);
134     t_stop = _itoll(TSCH, TSCL);
135     t_overhead = t_stop - t_start;
137     /* ------------------------------------------------------------------- */
138     /*  Generate random input in range (-10, 10).                          */
139     /* ------------------------------------------------------------------- */
140     UTIL_fillRandSP(ptr_x, N + PAD, 10.0);
142     for (n = 8; n <= N; n += 8) {
143       /* ----------------------------------------------------------------- */
144       /*  Clear state/output buffers with fixed values.                    */
145       /* ----------------------------------------------------------------- */
146       memset(ptr_y1_opt, 0, sizeof(ptr_y1_opt));
147       memset(ptr_y2_opt, 0, sizeof(ptr_y2_opt));
148       memset(ptr_y1_asm, 0, sizeof(ptr_y1_asm));
149       memset(ptr_y2_asm, 0, sizeof(ptr_y2_asm));
151       memset(ptr_y1_cn, 0, sizeof(ptr_y1_cn));
152       memset(ptr_y2_cn, 0, sizeof(ptr_y2_cn));
154       /* ----------------------------------------------------------------- */
155       /* Call the individual timing routines                               */
156       /* ----------------------------------------------------------------- */
157       t_opt = time_opt(n) - t_overhead;
158       t_i   = time_i(n) - t_overhead;
159       t_cn  = time_cn(n)  - t_overhead;
161       printf("DSPF_sp_iir\tIter#: %d\t", j++);
162       /* ----------------------------------------------------------------- */
163       /* compute percent difference and track max difference               */
164       /* ----------------------------------------------------------------- */
165       pct_diff = 0; max_pct_diff = 0;
166       for (i = 0; i < (n + PAD); i++) {           
167           pct_diff = (ptr_y2_cn[i] - ptr_y2_opt[i]) / ptr_y2_cn[i] * 100.0;
168           if (pct_diff < 0) pct_diff *= -1;
169           if (pct_diff > max_pct_diff) max_pct_diff = pct_diff;
170       }    
171       if (max_pct_diff > 0.02)
172           printf("Intrinsic Failure, max_pct_diff=%f\t", max_pct_diff);
173       else
174           printf("Intrinsic Successful\t");
175                    
176       /* ----------------------------------------------------------------- */
177       /* compute percent difference and track max difference               */
178       /* ----------------------------------------------------------------- */
179       pct_diff = 0; max_pct_diff = 0;
180       for (i = 0; i < (n + PAD); i++) {           
181           pct_diff = (ptr_y2_cn[i] - ptr_y2_asm[i]) / ptr_y2_cn[i] * 100.0;
182           if (pct_diff < 0) pct_diff *= -1;
183           if (pct_diff > max_pct_diff) max_pct_diff = pct_diff;
184       }    
185       if (max_pct_diff > 0.02)
186           printf("Asm Failure, max_pct_diff=%f\t", max_pct_diff);
187       else
188           printf("Asm Successful\t");
189                    
190       /* ----------------------------------------------------------------- */
191       /* Print timing results                                              */
192       /* ----------------------------------------------------------------- */
193       printf("\tN = %d\tnatC: %d\toptC: %d\tAsm: %d\n", n, t_cn, t_i, t_opt);
194       if (n == CYCLE_FORMULA_NX_PT1)
195         form_cycle[0] = t_opt;
196       if (n == CYCLE_FORMULA_NX_PT2)
197         form_cycle[1] = t_opt;
198     }
200     /* Provide memory information */
201 #ifdef __TI_COMPILER_VERSION__            // for TI compiler only
202     printf("Memory:  %d bytes\n", &kernel_size);
203 #endif
205     /* Provide profiling information */
206     for (i = 0; i < FORMULA_SIZE; i++) {
207         form_temp[i] = 0;
208         for (j = 0; j < FORMULA_SIZE; j++) {
209             form_temp[i] += form_inv[i][j] * form_cycle[j];
210         }
211         if (i != (FORMULA_SIZE-1)) {
212             form_result[i] = (int) (form_temp[i] * FORMULA_DEVIDE + 0.5);
213             if ((form_result[i] - form_temp[i] * FORMULA_DEVIDE) >  0.1 ||
214                 (form_result[i] - form_temp[i] * FORMULA_DEVIDE) < -0.1) {
215                 form_error = 1;
216             }
217         }
218         else {
219             form_result[i] = (int) (form_temp[i] + 0.5);
220         }
221     }
223     if (!form_error)
224         if (FORMULA_DEVIDE == 1) {
225             printf("Cycles:  %d*Nr + %d \n", form_result[0], form_result[1]);
226         }
227         else {
228             printf("Cycles:  %d/%d*Nr + %d \n", form_result[0], FORMULA_DEVIDE, form_result[1]);
229         }
230     else
231         printf("Cycles Formula Not Available\n");
234 /* ======================================================================= */
235 /* Prototypes for timing functions                                         */
236 /* ======================================================================= */
237 clock_t time_cn(int n)
239     clock_t t_start, t_stop;
241     /* ------------------------------------------------------------------- */
242     /* Measure the cycle count                                             */
243     /* ------------------------------------------------------------------- */
244     t_start = _itoll(TSCH, TSCL);
245     DSPF_sp_iir_cn(ptr_y1_cn, ptr_x, ptr_y2_cn, ptr_hb, ptr_ha, n);
246     t_stop = _itoll(TSCH, TSCL);
248     return t_stop - t_start;
251 clock_t time_opt(int n)
253     clock_t t_start, t_stop;
254     
255     /* ------------------------------------------------------------------- */
256     /* Measure the cycle count                                             */
257     /* ------------------------------------------------------------------- */
258     t_start = _itoll(TSCH, TSCL);
259     DSPF_sp_iir(ptr_y1_asm, ptr_x, ptr_y2_asm, ptr_hb, ptr_ha, n);
260     t_stop = _itoll(TSCH, TSCL);
262     return t_stop - t_start;
265 clock_t time_i(int n)
267     clock_t t_start, t_stop;
268     
269     /* ------------------------------------------------------------------- */
270     /* Measure the cycle count                                             */
271     /* ------------------------------------------------------------------- */
272     t_start = _itoll(TSCH, TSCL);
273     DSPF_sp_iir_opt(ptr_y1_opt, ptr_x, ptr_y2_opt, ptr_hb, ptr_ha, n);
274     t_stop = _itoll(TSCH, TSCL);
276     return t_stop - t_start;
279 void UTIL_fillRandSP(float *ptr_x, int n, float factor)
281     float rand_midpoint = RAND_MAX / 2.0;
282     int i;
284     // fill array with floats in the range (-factor, factor)
285     for (i = 0; i < n; i++)
286         ptr_x[i] = ((rand() - rand_midpoint) / rand_midpoint) * factor;
288 /* ======================================================================= */
289 /*  End of file:  DSPF_sp_iir_d.c                                          */
290 /* ----------------------------------------------------------------------- */
291 /*            Copyright (c) 2011 Texas Instruments, Incorporated.          */
292 /*                           All Rights Reserved.                          */
293 /* ======================================================================= */