]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - ep-processor-libraries/dsplib.git/blob - ti/dsplib/src/DSPF_sp_vecsum_sq/c674/DSPF_sp_vecsum_sq_d.c
DSPLIB: optimized signal processing functions for TI DSPs
[ep-processor-libraries/dsplib.git] / ti / dsplib / src / DSPF_sp_vecsum_sq / c674 / DSPF_sp_vecsum_sq_d.c
1 /* ======================================================================= */
2 /* DSPF_sp_vecsum_sq_d.c -- Vector Sum of Squares                          */
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_vecsum_sq_cn.h"
50 #include "DSPF_sp_vecsum_sq.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
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(y_opt, 8);
76 #pragma DATA_ALIGN(y_cn, 8);
77 #pragma DATA_ALIGN(ptr_x, 8);
79 /* ======================================================================= */
80 /* Parameters of fixed dataset                                             */
81 /* ======================================================================= */
82 #define N (128)
84 float y_opt, y_cn;
85 float ptr_x[N];
87 /* ======================================================================= */
88 /* Prototypes for timing functions                                         */
89 /* ======================================================================= */
90 clock_t time_opt(int n);
91 clock_t time_cn(int n);
93 /* ======================================================================= */
94 /* Prototypes for utility functions                                        */
95 /* ======================================================================= */
96 void UTIL_fillRandSP(float *ptr_x, int n, float factor);
98 /* ======================================================================= */
99 /* Main -- Top level driver for testing the algorithm                      */
100 /* ======================================================================= */
101 void main()
103     clock_t t_overhead, t_start, t_stop, t_opt, t_cn;
104     float pct_diff, max_pct_diff = 0;
105     int i, j=1, n;
106     int form_error = 0;
107     
108     /* ------------------------------------------------------------------- */
109     /* Compute the overhead of calling clock twice to get timing info      */
110     /* ------------------------------------------------------------------- */
111     /* Initialize timer for clock */
112     TSCL= 0,TSCH=0;
113     t_start = _itoll(TSCH, TSCL);
114     t_stop = _itoll(TSCH, TSCL);
115     t_overhead = t_stop - t_start;
117     /* ------------------------------------------------------------------- */
118     /*  Fill input arrays with randomized data in range (-10, 10).         */
119     /* ------------------------------------------------------------------- */
120     UTIL_fillRandSP(ptr_x, N, 10.0);
122     for (n = 16; n <= N; n += 16) {
123       /* ----------------------------------------------------------------- */
124       /* Call the individual timing routines                               */
125       /* ----------------------------------------------------------------- */
126       t_opt = time_opt(n) - t_overhead;
127       t_cn  = time_cn(n)  - t_overhead;
129       printf("DSPF_sp_vecsum_sq\tIter#: %d\t", j++);
131       /* ----------------------------------------------------------------- */
132       /* compute percent difference and track max difference               */
133       /* ----------------------------------------------------------------- */
134       pct_diff = (y_cn - y_opt) / y_cn * 100.0;
135       if (pct_diff < 0) pct_diff *= -1;
136       if (pct_diff > max_pct_diff) max_pct_diff = pct_diff;
138       if (max_pct_diff > 0.02)
139           printf("Result Failure, max_pct_diff=%f\n", max_pct_diff);
140       else
141           printf("Result Successful ");         
142     
143       /* ----------------------------------------------------------------- */
144       /* Print timing results                                              */
145       /* ----------------------------------------------------------------- */
146       printf("\tN = %d\tnatC: %d\toptC: %d\n", n, t_cn, t_opt);
147       if (n == CYCLE_FORMULA_NX_PT1)
148         form_cycle[0] = t_opt;
149       if (n == CYCLE_FORMULA_NX_PT2)
150         form_cycle[1] = t_opt;
151     }
153     /* Provide memory information */
154 #ifdef __TI_COMPILER_VERSION__            // for TI compiler only
155     printf("Memory:  %d bytes\n", &kernel_size);
156 #endif
158     /* Provide profiling information */
159     for (i = 0; i < FORMULA_SIZE; i++) {
160         form_temp[i] = 0;
161         for (j = 0; j < FORMULA_SIZE; j++) {
162             form_temp[i] += form_inv[i][j] * form_cycle[j];
163         }
164         if (i != (FORMULA_SIZE-1)) {
165             form_result[i] = (int) (form_temp[i] * FORMULA_DEVIDE + 0.5);
166             if ((form_result[i] - form_temp[i] * FORMULA_DEVIDE) >  0.1 ||
167                 (form_result[i] - form_temp[i] * FORMULA_DEVIDE) < -0.1) {
168                 form_error = 1;
169             }
170         }
171         else {
172             form_result[i] = (int) (form_temp[i] + 0.5);
173         }
174     }
176     if (!form_error)
177         if (FORMULA_DEVIDE == 1) {
178             printf("Cycles:  %d*Nx + %d \n", form_result[0], form_result[1]);
179         }
180         else {
181             printf("Cycles:  %d/%d*Nx + %d \n", form_result[0], FORMULA_DEVIDE, form_result[1]);
182         }
183     else
184         printf("Cycles Formula Not Available\n");
187 /* ======================================================================= */
188 /* Prototypes for timing functions                                         */
189 /* ======================================================================= */
190 clock_t time_cn(int n)
192     clock_t t_start, t_stop;
194     /* ------------------------------------------------------------------- */
195     /* Measure the cycle count                                             */
196     /* ------------------------------------------------------------------- */
197     t_start = _itoll(TSCH, TSCL);
198     y_cn = DSPF_sp_vecsum_sq_cn(ptr_x, n);
199     t_stop = _itoll(TSCH, TSCL);
201     return t_stop - t_start;
204 clock_t time_opt(int n)
206     clock_t t_start, t_stop;
207     
208     /* ------------------------------------------------------------------- */
209     /* Measure the cycle count                                             */
210     /* ------------------------------------------------------------------- */
211     t_start = _itoll(TSCH, TSCL);
212     y_opt = DSPF_sp_vecsum_sq(ptr_x, n);
213     t_stop = _itoll(TSCH, TSCL);
215     return t_stop - t_start;
218 void UTIL_fillRandSP(float *ptr_x, int n, float factor)
220     float rand_midpoint = RAND_MAX / 2.0;
221     int i;
223     // fill array with floats in the range (-factor, factor)
224     for (i = 0; i < n; i++)
225         ptr_x[i] = ((rand() - rand_midpoint) / rand_midpoint) * factor;
227 /* ======================================================================= */
228 /*  End of file:  DSPF_sp_vecsum_sq_d.c                                    */
229 /* ----------------------------------------------------------------------- */
230 /*            Copyright (c) 2011 Texas Instruments, Incorporated.          */
231 /*                           All Rights Reserved.                          */
232 /* ======================================================================= */