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 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_cn, 8);
78 #pragma DATA_ALIGN(ptr_y2_cn, 8);
79 #pragma DATA_ALIGN(ptr_ha, 8);
80 #pragma DATA_ALIGN(ptr_hb, 8);
81 #pragma DATA_ALIGN(ptr_x, 8);
83 /* ======================================================================= */
84 /* Parameters of fixed dataset */
85 /* ======================================================================= */
86 #define N (128)
87 #define PAD (4)
89 float ptr_y1_opt[N + PAD];
90 float ptr_y2_opt[N + PAD];
92 float ptr_y1_cn[N + PAD];
93 float ptr_y2_cn[N + PAD];
95 float ptr_x[N + PAD];
97 /* ======================================================================= */
98 /* Initilized arrays with fixed test data */
99 /* ======================================================================= */
100 float ptr_ha[PAD + 1] = { 1, -0.390641, 0.534300, -0.084233, 0.020651 };
101 float ptr_hb[PAD + 1] = { 0.067504, 0.270019, 0.405028, 0.270019, 0.067504 };
103 /* ======================================================================= */
104 /* Prototypes for timing functions */
105 /* ======================================================================= */
106 clock_t time_opt(int n);
107 clock_t time_cn(int n);
109 /* ======================================================================= */
110 /* Prototypes for utility functions */
111 /* ======================================================================= */
112 void UTIL_fillRandSP(float *ptr_x, int n, float factor);
114 /* ======================================================================= */
115 /* Main -- Top level driver for testing the algorithm */
116 /* ======================================================================= */
117 void main()
118 {
119 clock_t t_overhead, t_start, t_stop, t_opt, t_cn;
120 float pct_diff, max_pct_diff = 0;
121 int i, j=1, n, form_error = 0;
123 /* ------------------------------------------------------------------- */
124 /* Compute the overhead of calling clock twice to get timing info */
125 /* ------------------------------------------------------------------- */
126 /* Initialize timer for clock */
127 TSCL= 0,TSCH=0;
128 t_start = _itoll(TSCH, TSCL);
129 t_stop = _itoll(TSCH, TSCL);
130 t_overhead = t_stop - t_start;
132 /* ------------------------------------------------------------------- */
133 /* Generate random input in range (-10, 10). */
134 /* ------------------------------------------------------------------- */
135 UTIL_fillRandSP(ptr_x, N + PAD, 10.0);
137 for (n = 8; n <= N; n += 8) {
138 /* ----------------------------------------------------------------- */
139 /* Clear state/output buffers with fixed values. */
140 /* ----------------------------------------------------------------- */
141 memset(ptr_y1_opt, 0, sizeof(ptr_y1_opt));
142 memset(ptr_y2_opt, 0, sizeof(ptr_y2_opt));
144 memset(ptr_y1_cn, 0, sizeof(ptr_y1_cn));
145 memset(ptr_y2_cn, 0, sizeof(ptr_y2_cn));
147 /* ----------------------------------------------------------------- */
148 /* Call the individual timing routines */
149 /* ----------------------------------------------------------------- */
150 t_opt = time_opt(n) - t_overhead;
151 t_cn = time_cn(n) - t_overhead;
153 printf("DSPF_sp_iir\tIter#: %d\t", j++);
154 /* ----------------------------------------------------------------- */
155 /* compute percent difference and track max difference */
156 /* ----------------------------------------------------------------- */
157 for (i = 0; i < (n + PAD); i++) {
158 pct_diff = (ptr_y2_cn[i] - ptr_y2_opt[i]) / ptr_y2_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.02)
163 printf("Result Failure, max_pct_diff=%f\n", max_pct_diff);
164 else
165 printf("Result Successful ");
167 /* ----------------------------------------------------------------- */
168 /* Print timing results */
169 /* ----------------------------------------------------------------- */
170 printf("\tN = %d\tnatC: %d\toptC: %d\n", n, t_cn, t_opt);
171 if (n == CYCLE_FORMULA_NX_PT1)
172 form_cycle[0] = t_opt;
173 if (n == CYCLE_FORMULA_NX_PT2)
174 form_cycle[1] = t_opt;
175 }
177 /* Provide memory information */
178 #ifdef __TI_COMPILER_VERSION__ // for TI compiler only
179 printf("Memory: %d bytes\n", &kernel_size);
180 #endif
182 /* Provide profiling information */
183 for (i = 0; i < FORMULA_SIZE; i++) {
184 form_temp[i] = 0;
185 for (j = 0; j < FORMULA_SIZE; j++) {
186 form_temp[i] += form_inv[i][j] * form_cycle[j];
187 }
188 if (i != (FORMULA_SIZE-1)) {
189 form_result[i] = (int) (form_temp[i] * FORMULA_DEVIDE + 0.5);
190 if ((form_result[i] - form_temp[i] * FORMULA_DEVIDE) > 0.1 ||
191 (form_result[i] - form_temp[i] * FORMULA_DEVIDE) < -0.1) {
192 form_error = 1;
193 }
194 }
195 else {
196 form_result[i] = (int) (form_temp[i] + 0.5);
197 }
198 }
200 if (!form_error)
201 if (FORMULA_DEVIDE == 1) {
202 printf("Cycles: %d*Nr + %d \n", form_result[0], form_result[1]);
203 }
204 else {
205 printf("Cycles: %d/%d*Nr + %d \n", form_result[0], FORMULA_DEVIDE, form_result[1]);
206 }
207 else
208 printf("Cycles Formula Not Available\n");
209 }
211 /* ======================================================================= */
212 /* Prototypes for timing functions */
213 /* ======================================================================= */
214 clock_t time_cn(int n)
215 {
216 clock_t t_start, t_stop;
218 /* ------------------------------------------------------------------- */
219 /* Measure the cycle count */
220 /* ------------------------------------------------------------------- */
221 t_start = _itoll(TSCH, TSCL);
222 DSPF_sp_iir_cn(ptr_y1_cn, ptr_x, ptr_y2_cn, ptr_hb, ptr_ha, n);
223 t_stop = _itoll(TSCH, TSCL);
225 return t_stop - t_start;
226 }
228 clock_t time_opt(int n)
229 {
230 clock_t t_start, t_stop;
232 /* ------------------------------------------------------------------- */
233 /* Measure the cycle count */
234 /* ------------------------------------------------------------------- */
235 t_start = _itoll(TSCH, TSCL);
236 DSPF_sp_iir(ptr_y1_opt, ptr_x, ptr_y2_opt, ptr_hb, ptr_ha, n);
237 t_stop = _itoll(TSCH, TSCL);
239 return t_stop - t_start;
240 }
242 void UTIL_fillRandSP(float *ptr_x, int n, float factor)
243 {
244 float rand_midpoint = RAND_MAX / 2.0;
245 int i;
247 // fill array with floats in the range (-factor, factor)
248 for (i = 0; i < n; i++)
249 ptr_x[i] = ((rand() - rand_midpoint) / rand_midpoint) * factor;
250 }
251 /* ======================================================================= */
252 /* End of file: DSPF_sp_iir_d.c */
253 /* ----------------------------------------------------------------------- */
254 /* Copyright (c) 2011 Texas Instruments, Incorporated. */
255 /* All Rights Reserved. */
256 /* ======================================================================= */