]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - ep-processor-libraries/dsplib.git/blob - ti/dsplib/src/DSPF_sp_vecrecip/c674/DSPF_sp_vecrecip_d.c
DSPLIB: optimized signal processing functions for TI DSPs
[ep-processor-libraries/dsplib.git] / ti / dsplib / src / DSPF_sp_vecrecip / c674 / DSPF_sp_vecrecip_d.c
1 /* ======================================================================= */
2 /* DSPF_sp_vecrecip_d.c -- Vector Reciprocal                               */
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_vecrecip_cn.h"
50 #include "DSPF_sp_vecrecip.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_y_opt, 8);
76 #pragma DATA_ALIGN(ptr_y_cn, 8);
77 #pragma DATA_ALIGN(ptr_x, 8);
79 /* ======================================================================= */
80 /* Parameters of fixed dataset                                             */
81 /* ======================================================================= */
82 #define N (128)
84 float ptr_y_opt[N];
85 float ptr_y_cn[N];
86 float ptr_x[N];
88 /* ======================================================================= */
89 /* Prototypes for timing functions                                         */
90 /* ======================================================================= */
91 clock_t time_opt(int n);
92 clock_t time_cn(int n);
94 /* ======================================================================= */
95 /* Main -- Top level driver for testing the algorithm                      */
96 /* ======================================================================= */
97 void main()
98 {
99     clock_t t_overhead, t_start, t_stop, t_opt, t_cn;
100     float pct_diff, max_pct_diff = 0;
101     int i, j=1, n;
102     int form_error = 0;    
103     
104     /* ------------------------------------------------------------------- */
105     /* Compute the overhead of calling clock twice to get timing info      */
106     /* ------------------------------------------------------------------- */
107     /* Initialize timer for clock */
108     TSCL= 0,TSCH=0;
109     t_start = _itoll(TSCH, TSCL);
110     t_stop = _itoll(TSCH, TSCL);
111     t_overhead = t_stop - t_start;
113     /* ------------------------------------------------------------------- */
114     /*  Fill input arrays with randomized data in range (-10, 10).         */
115     /* ------------------------------------------------------------------- */
116     for (i = 0; i < N; i++)
117         ptr_x[i] = ((rand() - (RAND_MAX / 2.0)) / (RAND_MAX / 2.0)) * 10.0;
119     for (n = 8; n <= N; n += 8) {
120       /* ----------------------------------------------------------------- */
121       /*  Clear state/output buffers with fixed values.                    */
122       /* ----------------------------------------------------------------- */
123       memset(ptr_y_opt, 0, sizeof(ptr_y_opt));
124       memset(ptr_y_cn, 0, sizeof(ptr_y_cn));
126       /* ----------------------------------------------------------------- */
127       /* Call the individual timing routines                               */
128       /* ----------------------------------------------------------------- */
129       t_opt = time_opt(n) - t_overhead;
130       t_cn  = time_cn(n)  - t_overhead;
132       printf("DSPF_sp_vecrecip\tIter#: %d\t", j++);
134       /* ----------------------------------------------------------------- */
135       /* compute percent difference and track max difference               */
136       /* ----------------------------------------------------------------- */
137       for (i = 0; i < n; i++) {           
138           pct_diff = (ptr_y_cn[i] - ptr_y_opt[i]) / ptr_y_cn[i] * 100.0;
139           if (pct_diff < 0) pct_diff *= -1;
140           if (pct_diff > max_pct_diff) max_pct_diff = pct_diff;
141       }    
142       if (max_pct_diff > 0.02)
143           printf("Result Failure, max_pct_diff=%f\n", max_pct_diff);
144       else
145           printf("Result Successful ");
146       
147       /* ----------------------------------------------------------------- */
148       /* Print timing results                                              */
149       /* ----------------------------------------------------------------- */
150       printf("\tN = %d\tnatC: %d\toptC: %d\n", n, t_cn, t_opt);
151       if (n == CYCLE_FORMULA_NX_PT1)
152         form_cycle[0] = t_opt;
153       if (n == CYCLE_FORMULA_NX_PT2)
154         form_cycle[1] = t_opt;
155     }
156     /* Provide memory information */
157 #ifdef __TI_COMPILER_VERSION__            // for TI compiler only
158     printf("Memory:  %d bytes\n", &kernel_size);
159 #endif
161     /* Provide profiling information */
162     for (i = 0; i < FORMULA_SIZE; i++) {
163         form_temp[i] = 0;
164         for (j = 0; j < FORMULA_SIZE; j++) {
165             form_temp[i] += form_inv[i][j] * form_cycle[j];
166         }
167         if (i != (FORMULA_SIZE-1)) {
168             form_result[i] = (int) (form_temp[i] * FORMULA_DEVIDE + 0.5);
169             if ((form_result[i] - form_temp[i] * FORMULA_DEVIDE) >  0.1 ||
170                 (form_result[i] - form_temp[i] * FORMULA_DEVIDE) < -0.1) {
171                 form_error = 1;
172             }
173         }
174         else {
175             form_result[i] = (int) (form_temp[i] + 0.5);
176         }
177     }
179     if (!form_error)
180         if (FORMULA_DEVIDE == 1) {
181             printf("Cycles:  %d*Nx + %d \n", form_result[0], form_result[1]);
182         }
183         else {
184             printf("Cycles:  %d/%d*Nx + %d \n", form_result[0], FORMULA_DEVIDE, form_result[1]);
185         }
186     else
187         printf("Cycles Formula Not Available\n");
191 /* ======================================================================= */
192 /* Prototypes for timing functions                                         */
193 /* ======================================================================= */
194 clock_t time_cn(int n)
196     clock_t t_start, t_stop;
198     /* ------------------------------------------------------------------- */
199     /* Measure the cycle count                                             */
200     /* ------------------------------------------------------------------- */
201     t_start = _itoll(TSCH, TSCL);
202     DSPF_sp_vecrecip_cn(ptr_x, ptr_y_cn, n);
203     t_stop = _itoll(TSCH, TSCL);
205     return t_stop - t_start;
208 clock_t time_opt(int n)
210     clock_t t_start, t_stop;
211     
212     /* ------------------------------------------------------------------- */
213     /* Measure the cycle count                                             */
214     /* ------------------------------------------------------------------- */
215     t_start = _itoll(TSCH, TSCL);
216     DSPF_sp_vecrecip(ptr_x, ptr_y_opt, n);
217     t_stop = _itoll(TSCH, TSCL);
219     return t_stop - t_start;
222 /* ======================================================================= */
223 /*  End of file:  DSPF_sp_vecrecip_d.c                                     */
224 /* ----------------------------------------------------------------------- */
225 /*            Copyright (c) 2011 Texas Instruments, Incorporated.          */
226 /*                           All Rights Reserved.                          */
227 /* ======================================================================= */