1 /* ======================================================================= */
2 /* DSP_q15tofl_d.c -- Float to Q15 conversion */
3 /* Driver code implementation */
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 "DSP_q15tofl_cn.h"
50 #include "DSP_q15tofl.h"
52 /* Defines */
53 #if defined(__TI_EABI__)
54 #define kernel_size _kernel_size
55 #endif
57 extern char kernel_size;
59 #define FORMULA_SIZE 2
60 #define FORMULA_DEVIDE 2
61 #define CYCLE_FORMULA_NX_PT1 32
62 #define CYCLE_FORMULA_NX_PT2 64
63 /* inverse of [32 1] */
64 /* [64 1] */
65 float form_inv[FORMULA_SIZE][FORMULA_SIZE] =
66 {{-0.0313, 0.0313},
67 { 2.0000, -1.0000}
68 };
69 float form_temp [FORMULA_SIZE];
70 int form_cycle [FORMULA_SIZE];
71 int form_result[FORMULA_SIZE];
73 /* ======================================================================= */
74 /* Inform the compiler the arrays are double word alligned */
75 /* ======================================================================= */
76 #pragma DATA_ALIGN(q15, 8);
77 #pragma DATA_ALIGN(flt_c, 8);
78 #pragma DATA_ALIGN(flt_i, 8);
80 /* ======================================================================= */
81 /* Parameters of fixed dataset */
82 /* ======================================================================= */
83 #define N (128)
85 /* ======================================================================= */
86 /* Initilized arrays with fixed test data */
87 /* ======================================================================= */
88 short q15[N] =
89 {
90 0x76E2, 0x8B0E, 0x4A09, 0xBA35, 0xEB22, 0xE6EB, 0x8CCF, 0xDF71,
91 0xAACF, 0x3CE2, 0xADDA, 0x3E77, 0x8CA7, 0x6C43, 0x0F68, 0xCCF6,
92 0x5A0A, 0xCD7B, 0x8397, 0xA5BC, 0xB196, 0x963C, 0x61C8, 0xE43A,
93 0x1C8B, 0x55F2, 0x32B9, 0xB3DD, 0xE468, 0x64CE, 0x3F29, 0x2815,
94 0x99CB, 0x813E, 0xCAB7, 0xE031, 0xE954, 0x8AF2, 0x9943, 0x2780,
95 0x8DC1, 0x2C18, 0xA0EA, 0x6371, 0x9033, 0x2F1F, 0xF36D, 0xB972,
96 0x5B46, 0x9837, 0x6009, 0x7354, 0x7CBB, 0xCB4E, 0x4D5F, 0x90E4,
97 0x27B1, 0xCBF3, 0xD4CD, 0xCA53, 0xD3A7, 0xDD56, 0xEF72, 0x866E,
98 0x637B, 0x6CE5, 0x084D, 0x3A65, 0xCA8D, 0x45D0, 0x8A5D, 0xB246,
99 0x031B, 0xBC84, 0xB401, 0x9DC2, 0xB285, 0xE273, 0x6C99, 0xE626,
100 0x3D8A, 0x5E49, 0x0742, 0xDAE3, 0x7488, 0x5DF7, 0x36BD, 0xA466,
101 0xBE5F, 0x8DAB, 0x6647, 0xCF7F, 0x8A8D, 0xBFF5, 0x3E81, 0x867D,
102 0x1CD2, 0x1103, 0xF7C9, 0x8B0F, 0xAE8D, 0x03A4, 0x22BE, 0x7CC3,
103 0x739C, 0x9D08, 0xC9DF, 0xB2AA, 0x155F, 0x2D3D, 0x79AC, 0xD731,
104 0x9654, 0x0273, 0x9582, 0x5C69, 0xBC3B, 0xB957, 0x3641, 0x293F,
105 0x3533, 0x94DA, 0x1E69, 0xE5A3, 0xD45A, 0xC82B, 0x8758, 0xFAC3,
106 };
108 /* ======================================================================== */
109 /* Result arrays, with padding to catch output overrun. */
110 /* ======================================================================== */
111 float flt_c[N];
112 float flt_i[N];
114 /* ======================================================================= */
115 /* Prototypes for timing functions */
116 /* ======================================================================= */
117 clock_t time_c(int n);
118 clock_t time_i(int n);
120 /* ======================================================================= */
121 /* MAIN -- Top level driver for testing the algorithm */
122 /* ======================================================================= */
123 int main()
124 {
125 clock_t t_overhead, t_start, t_stop;
126 clock_t t_c, t_i;
127 int i, j, n, fail = 0;
128 int form_error = 0;
130 /* =================================================================== */
131 /* Initialize timer for clock */
132 TSCL= 0,TSCH=0;
133 /* Compute overhead of calling _itoll(TSCH, TSCL) twice to get timing info */
134 /* =================================================================== */
135 t_start = _itoll(TSCH, TSCL);
136 t_stop = _itoll(TSCH, TSCL);
137 t_overhead = t_stop - t_start;
139 for(n = 4, i = 1; n <= N; i++, n += 4) {
140 /* -------------------------------------------------------------------- */
141 /* Fill output buffers with fixed values. */
142 /* -------------------------------------------------------------------- */
143 memset(flt_c, 0x5A, sizeof(flt_c));
144 memset(flt_i, 0x5A, sizeof(flt_i));
146 /* =================================================================== */
147 /* Call the individual timing routines, and subtract off overhead */
148 /* =================================================================== */
149 t_c = time_c(n) - t_overhead; // Calculate time to run Natural C Algorithm
150 t_i = time_i(n) - t_overhead; // Calculate time to run Optimized C Algorithm
152 /* =================================================================== */
153 /* Print timing results */
154 /* =================================================================== */
155 printf("DSP_q15tofl\tIter#: %d\t", i);
157 /* =================================================================== */
158 /* Check the results arrays, and report any failures */
159 /* =================================================================== */
160 if (memcmp(flt_i, flt_c, sizeof(flt_c))) {
161 fail++;
162 printf("Result Failure (flt_i)");
163 }
164 else
165 printf("Result Successful (flt_i)");
167 printf("\tN = %d\tnatC: %d\toptC: %d\n", n, t_c, t_i);
169 if (n == CYCLE_FORMULA_NX_PT1)
170 form_cycle[0] = t_i;
171 if (n == CYCLE_FORMULA_NX_PT2)
172 form_cycle[1] = t_i;
173 }
175 /* Provide memory information */
176 #ifdef __TI_COMPILER_VERSION__ // for TI compiler only
177 printf("Memory: %d bytes\n", &kernel_size);
178 #endif
180 /* Provide profiling information */
181 for (i = 0; i < FORMULA_SIZE; i++) {
182 form_temp[i] = 0;
183 for (j = 0; j < FORMULA_SIZE; j++) {
184 form_temp[i] += form_inv[i][j] * form_cycle[j];
185 }
186 if (i != (FORMULA_SIZE-1)) {
187 form_result[i] = (int) (form_temp[i] * FORMULA_DEVIDE + 0.5);
188 if ((form_result[i] - form_temp[i] * FORMULA_DEVIDE) > 0.1 ||
189 (form_result[i] - form_temp[i] * FORMULA_DEVIDE) < -0.1) {
190 form_error = 1;
191 }
192 }
193 else {
194 form_result[i] = (int) (form_temp[i] + 0.5);
195 }
196 }
198 if (!form_error)
199 if (FORMULA_DEVIDE == 1) {
200 printf("Cycles: %d*Nx + %d \n", form_result[0], form_result[1]);
201 }
202 else {
203 printf("Cycles: %d/%d*Nx + %d \n", form_result[0], FORMULA_DEVIDE, form_result[1]);
204 }
205 else
206 printf("Cycles Formula Not Available\n");
208 return(fail);
209 }
211 /* ======================================================================= */
212 /* Prototypes for timing functions */
213 /* ======================================================================= */
214 clock_t time_c(int n)
215 {
216 clock_t t_start, t_stop;
218 t_start = _itoll(TSCH, TSCL);
219 DSP_q15tofl_cn(q15, flt_c, n);
220 t_stop = _itoll(TSCH, TSCL);
221 return t_stop - t_start;
222 }
224 /* ======================================================================= */
225 /* Prototypes for timing functions */
226 /* ======================================================================= */
227 clock_t time_i(int n)
228 {
229 clock_t t_start, t_stop;
231 t_start = _itoll(TSCH, TSCL);
232 DSP_q15tofl(q15, flt_i, n);
233 t_stop = _itoll(TSCH, TSCL);
234 return t_stop - t_start;
235 }
237 /* ======================================================================= */
238 /* End of file: DSP_q15tofl_d.c */
239 /* ----------------------------------------------------------------------- */
240 /* Copyright (c) 2011 Texas Instruments, Incorporated. */
241 /* All Rights Reserved. */
242 /* ======================================================================= */