1 /* ======================================================================= */
2 /* DSPF_sp_convol_d.c -- Convolution */
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_convol_cn.h"
50 #include "DSPF_sp_convol.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 120
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 /* [120*16 120 16 1] */
68 /* [120*12 120 12 1] */
70 float form_inv[FORMULA_SIZE][FORMULA_SIZE] =
71 {{ 0.0313, -0.0313, -0.0313, 0.0313},
72 { -0.3750, 0.5000, 0.3750, -0.5000},
73 { -3.7500, 3.7500, 4.0000, -4.0000},
74 { 45.0000, -60.0000, -48.0000, 64.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_opt, 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 NH (16)
93 #define NY (128)
95 float ptr_y_opt[NY];
96 float ptr_y_cn[NY];
97 float ptr_h[NH];
98 float ptr_x[NY + NH - 1];
100 /* ======================================================================= */
101 /* Prototypes for timing functions */
102 /* ======================================================================= */
103 clock_t time_opt(int nh, int ny);
104 clock_t time_cn(int nh, int ny);
106 /* ======================================================================= */
107 /* Prototypes for utility functions */
108 /* ======================================================================= */
109 void UTIL_fillRandFilterSP(float *ptr_h, int N);
110 void UTIL_fillRandSP(float *ptr_x, int N, float factor);
112 /* ======================================================================= */
113 /* Main -- Top level driver for testing the algorithm */
114 /* ======================================================================= */
115 void main()
116 {
117 clock_t t_overhead, t_start, t_stop, t_opt, t_cn;
118 int form_error = 0;
119 int i, j=1, nh, ny;
120 float pct_diff, max_pct_diff = 0;
122 /* ------------------------------------------------------------------- */
123 /* Compute the overhead of calling clock twice to get timing info */
124 /* ------------------------------------------------------------------- */
125 /* Initialize timer for clock */
126 TSCL= 0,TSCH=0;
127 t_start = _itoll(TSCH, TSCL);
128 t_stop = _itoll(TSCH, TSCL);
129 t_overhead = t_stop - t_start;
131 /* ------------------------------------------------------------------- */
132 /* Generate random input (with zero padding) and MA filter. */
133 /* ------------------------------------------------------------------- */
134 memset(ptr_x, 0, sizeof(ptr_x));
135 memset(ptr_h, 0, sizeof(ptr_h));
136 UTIL_fillRandSP(ptr_x + NH - 1, NY - (NH - 1), 10.0);
137 UTIL_fillRandFilterSP(ptr_h, NH);
139 for(nh = 4; nh <= NH; nh += 4) {
140 for(ny = 32; ny <= NY; ny += 8) {
141 /* --------------------------------------------------------------- */
142 /* Clear state/output buffers with fixed values. */
143 /* --------------------------------------------------------------- */
144 memset(ptr_y_opt, 0, sizeof(ptr_y_opt));
145 memset(ptr_y_cn, 0, sizeof(ptr_y_cn));
147 /* --------------------------------------------------------------- */
148 /* Call the individual timing routines */
149 /* --------------------------------------------------------------- */
150 t_opt = time_opt(nh, ny) - t_overhead;
151 t_cn = time_cn(nh, ny) - t_overhead;
153 printf("DSPF_sp_convol\tIter#: %d\t", j++);
155 /* --------------------------------------------------------------- */
156 /* compute percent difference and track max difference */
157 /* --------------------------------------------------------------- */
158 for(i=0; i<ny; i++) {
159 pct_diff = (ptr_y_cn[i] - ptr_y_opt[i]) / ptr_y_cn[i] * 100.0;
160 if (pct_diff < 0) pct_diff *= -1;
161 if (pct_diff > max_pct_diff) max_pct_diff = pct_diff;
162 }
163 if (max_pct_diff > 0.01)
164 printf("Result Failure max_pct_diff = %f", max_pct_diff);
165 else
166 printf("Result Successful ");
168 /* --------------------------------------------------------------- */
169 /* Print timing results */
170 /* --------------------------------------------------------------- */
171 printf("\tNH = %d\tNY = %d\tnatC: %d\toptC: %d\n", nh, ny, t_cn, t_opt);
173 if (ny == CYCLE_FORMULA_NY_PT1 && nh == CYCLE_FORMULA_NH_PT1)
174 form_cycle[0] = t_opt;
175 if (ny == CYCLE_FORMULA_NY_PT1 && nh == CYCLE_FORMULA_NH_PT2)
176 form_cycle[1] = t_opt;
177 if (ny == CYCLE_FORMULA_NY_PT2 && nh == CYCLE_FORMULA_NH_PT1)
178 form_cycle[2] = t_opt;
179 if (ny == CYCLE_FORMULA_NY_PT2 && nh == CYCLE_FORMULA_NH_PT2)
180 form_cycle[3] = t_opt;
181 }
182 }
184 /* Provide memory information */
185 #ifdef __TI_COMPILER_VERSION__ // for TI compiler only
186 printf("Memory: %d bytes\n", &kernel_size);
187 #endif
189 /* Provide profiling information */
190 for (i = 0; i < FORMULA_SIZE; i++) {
191 form_temp[i] = 0;
192 for (j = 0; j < FORMULA_SIZE; j++) {
193 form_temp[i] += form_inv[i][j] * form_cycle[j];
194 }
195 if (i != (FORMULA_SIZE-1)) {
196 form_result[i] = (int) (form_temp[i] * FORMULA_DEVIDE + 0.5);
197 if ((form_result[i] - form_temp[i] * FORMULA_DEVIDE) > 0.1 ||
198 (form_result[i] - form_temp[i] * FORMULA_DEVIDE) < -0.1) {
199 form_error = 1;
200 }
201 }
202 else {
203 form_result[i] = (int) (form_temp[i] + 0.5);
204 }
205 }
207 if (!form_error) {
208 printf("Cycles: ");
209 if (form_result[0])
210 printf("%d/%d*Ny*Nh + ", form_result[0], FORMULA_DEVIDE);
211 if (form_result[1])
212 printf("%d/%d*Ny + ", form_result[1], FORMULA_DEVIDE);
213 if (form_result[2])
214 printf("%d/%d*Nh + ", form_result[2], FORMULA_DEVIDE);
215 if (form_result[3])
216 printf("%d\n", form_result[3]);
217 }
218 else
219 printf("Cycles Formula Not Available\n");
220 }
222 /* ======================================================================= */
223 /* Prototypes for timing functions */
224 /* ======================================================================= */
225 clock_t time_cn(int nh, int ny)
226 {
227 clock_t t_start, t_stop;
229 /* ------------------------------------------------------------------- */
230 /* Measure the cycle count */
231 /* ------------------------------------------------------------------- */
232 t_start = _itoll(TSCH, TSCL);
233 DSPF_sp_convol_cn(ptr_x, ptr_h, ptr_y_cn, nh, ny);
234 t_stop = _itoll(TSCH, TSCL);
236 return t_stop - t_start;
237 }
239 clock_t time_opt(int nh, int ny)
240 {
241 clock_t t_start, t_stop;
243 /* ------------------------------------------------------------------- */
244 /* Measure the cycle count */
245 /* ------------------------------------------------------------------- */
246 t_start = _itoll(TSCH, TSCL);
247 DSPF_sp_convol(ptr_x, ptr_h, ptr_y_opt, nh, ny);
248 t_stop = _itoll(TSCH, TSCL);
250 return t_stop - t_start;
251 }
253 void UTIL_fillRandFilterSP(float *ptr_h, int N)
254 {
255 float frand_max = RAND_MAX, pair_sum = 2.0 / N;
256 int i;
258 // create a moving average filter of length N
259 // filter taps must be non-negative and sum to 1
260 // N must be an even number
261 for (i = 0; i < N / 2; i++)
262 {
263 ptr_h[i] = (rand() / frand_max) * pair_sum;
264 ptr_h[N - 1 - i] = pair_sum - ptr_h[i];
265 }
266 }
268 void UTIL_fillRandSP(float *ptr_x, int N, float factor)
269 {
270 float rand_midpoint = RAND_MAX / 2.0;
271 int i;
273 // fill array with floats in the range (-factor, factor)
274 for (i = 0; i < N; i++)
275 ptr_x[i] = ((rand() - rand_midpoint) / rand_midpoint) * factor;
276 }
278 /* ======================================================================= */
279 /* End of file: DSPF_sp_convol_d.c */
280 /* ----------------------------------------------------------------------- */
281 /* Copyright (c) 2011 Texas Instruments, Incorporated. */
282 /* All Rights Reserved. */
283 /* ======================================================================= */