]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - ep-processor-libraries/dsplib.git/blob - ti/dsplib/src/DSP_iir_lat/c64P/DSP_iir_lat_d.c
DSPLIB: optimized signal processing functions for TI DSPs
[ep-processor-libraries/dsplib.git] / ti / dsplib / src / DSP_iir_lat / c64P / DSP_iir_lat_d.c
1 /* ======================================================================= */
2 /* DSP_iir_lat_d.c -- IIR lattice Filter                                   */
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_iir_lat_cn.h"
50 #include "DSP_iir_lat.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          4
60 #define FORMULA_DEVIDE        4
61 #define CYCLE_FORMULA_NX_PT1  20
62 #define CYCLE_FORMULA_NX_PT2  16
63 #define CYCLE_FORMULA_NR_PT1  32
64 #define CYCLE_FORMULA_NR_PT2  28
65 /* inverse of [20*32 20 32 1] */
66 /*            [20*28 20 28 1] */
67 /*            [16*32 16 32 1] */
68 /*            [16*28 16 28 1] */
69 float form_inv[FORMULA_SIZE][FORMULA_SIZE] = 
70 {{ 0.0625,   -0.0625,   -0.0625,    0.0625},
71  {-1.7500,    2.0000,    1.7500,   -2.0000},
72  {-1.0000,    1.0000,    1.2500,   -1.2500},
73  {28.0000,  -32.0000,  -35.0000,   40.0000}
74 };
75 float form_temp  [FORMULA_SIZE];
76 int   form_cycle [FORMULA_SIZE];
77 int   form_result[FORMULA_SIZE];
79 /* ======================================================================= */
80 /* Kernel specific data allignment(s)                                      */
81 /* ======================================================================= */
82 #pragma DATA_ALIGN(x, 8);
83 #pragma DATA_ALIGN(k, 8);
84 #pragma DATA_ALIGN(b_c, 8);
85 #pragma DATA_ALIGN(b_i, 8);
86 #pragma DATA_ALIGN(r_c, 8);
87 #pragma DATA_ALIGN(r_i, 8);
89 /* ======================================================================= */
90 /*  Parameters of fixed dataset.                                           */
91 /* ======================================================================= */
92 #define NK  (32)
93 #define NK1 (NK+1)
94 #define NX  (20)
95 #define PAD (16)
98 /* ======================================================================= */
99 /*  Initialized arrays with fixed test data.                               */
100 /* ======================================================================= */
101 short x[NX + 2*PAD] =
103     0x5A5A,  0x5A5A,  0x5A5A,  0x5A5A, 0x5A5A,  0x5A5A,  0x5A5A,  0x5A5A,
104     0x5A5A,  0x5A5A,  0x5A5A,  0x5A5A, 0x5A5A,  0x5A5A,  0x5A5A,  0x5A5A,
106     0xBBCD,  0xABCD,  0x1223,  0x1111, 0x2222,  0x3333,    2191,  738, 
107     -242,    308,     1411,   -322,    603,     197,      -1972,  2191, 
108     738,    -242,     308,     0,
110     0x5A5A,  0x5A5A,  0x5A5A,  0x5A5A, 0x5A5A,  0x5A5A,  0x5A5A,  0x5A5A,
111     0x5A5A,  0x5A5A,  0x5A5A,  0x5A5A, 0x5A5A,  0x5A5A,  0x5A5A,  0x5A5A,
112 };
114 const short k[NK] =
116     -13839,   22437,  -27054,   19741,
117     -16792,  -30550,  -25856,   6973,
118     -13839,   22437,  -27054,   19741,
119     -16792,  -30550,  -25856,   6973,
120     -13839,   22437,  -27054,   19741,
121     -16792,  -30550,  -25856,   6973,
122     -13839,   22437,  -27054,   19741,
123     -16792,  -30550,  -25856,   6973,
124 };
126 int  b_c[NK1 + 2*PAD] =
128     0x5A5A5A5A,  0x5A5A5A5A,  0x5A5A5A5A,  0x5A5A5A5A,
129     0x5A5A5A5A,  0x5A5A5A5A,  0x5A5A5A5A,  0x5A5A5A5A,
130     0x5A5A5A5A,  0x5A5A5A5A,  0x5A5A5A5A,  0x5A5A5A5A,
131     0x5A5A5A5A,  0x5A5A5A5A,  0x5A5A5A5A,  0x5A5A5A5A,
133     0x000A0000,  0x00050000,  0x00A00000,  0x00110000,
134     0x00340000,  0xF6B50000,  0x003C0000,  0xFFE10000,
135     0x009D0000,
137     0x5A5A5A5A,  0x5A5A5A5A,  0x5A5A5A5A,  0x5A5A5A5A,
138     0x5A5A5A5A,  0x5A5A5A5A,  0x5A5A5A5A,  0x5A5A5A5A,
139     0x5A5A5A5A,  0x5A5A5A5A,  0x5A5A5A5A,  0x5A5A5A5A,
140     0x5A5A5A5A,  0x5A5A5A5A,  0x5A5A5A5A,  0x5A5A5A5A,
141 };
143 int b_i[NK1 + 2*PAD];
145 short r_c[NX + 2*PAD];
146 short r_i[NX + 2*PAD];
148 /* ======================================================================= */
149 /*  Generate pointers to skip beyond array padding                         */
150 /* ======================================================================= */
151 short *const ptr_x = x + PAD;
153 int *const ptr_b_c = b_c + PAD;
154 int *const ptr_b_i = b_i + PAD;
156 short *const ptr_r_c = r_c + PAD;
157 short *const ptr_r_i = r_i + PAD;
159 /* ======================================================================= */
160 /*  Prototypes for timing functions.                                       */
161 /* ======================================================================= */
162 clock_t time_c(int nx, int nk);
163 clock_t time_i(int nx, int nk);
165 /* ======================================================================= */
166 /* Main -- Top level driver for testing the algorithm                      */
167 /* ======================================================================= */
168 int main()
170     clock_t t_overhead, t_start, t_stop, t_c, t_i;
171     int i, j, nx, nk, fail = 0;
172     int form_error = 0;
174     /* ------------------------------------------------------------------- */
175     /* Compute the overhead of calling clock twice to get timing info      */
176     /* ------------------------------------------------------------------- */
177     /* Initialize timer for clock */
178     TSCL= 0,TSCH=0;
179     t_start = _itoll(TSCH, TSCL);
180     t_stop = _itoll(TSCH, TSCL);
181     t_overhead = t_stop - t_start;
183     for (nx = 2, j = 1; nx <= NX; nx += 2) {
184         for (nk = 4; nk <= NK; j++, nk += 4) {
185             /* ------------------------------------------------------------------- */
186             /* Initialize all arrays                                               */
187             /* ------------------------------------------------------------------- */
188             memset(r_c, 0xA5, sizeof(r_c));
189             memset(r_i, 0xA5, sizeof(r_i));
190             memcpy(b_i, b_c, sizeof(b_i));
192             /* ------------------------------------------------------------------- */
193             /* natC and optC maintain b[] << 15                                    */
194             /* ------------------------------------------------------------------- */
195             for (i = 0; i < (NK1 + 2*PAD); i++)
196                 b_c[i] = b_i[i] >> 1;
198             /* ------------------------------------------------------------------- */
199             /* Call the individual timing routines                                 */
200             /* ------------------------------------------------------------------- */
201             t_c = time_c(nx, nk) - t_overhead;
202             t_i = time_i(nx, nk) - t_overhead;
204             /* ------------------------------------------------------------------- */
205             /* Print timing results                                                */
206             /* ------------------------------------------------------------------- */
207             printf("DSP_iir_lat\tIter#: %d\t", j);
209             /* ------------------------------------------------------------------- */
210             /* Check the results array                                             */
211             /* ------------------------------------------------------------------- */
212             if (memcmp(r_i, r_c, sizeof(r_c))) {
213                 fail++;
214                 printf("Result Failure (r_i)");
215             }
216             else
217                 printf("Result Successful (r_i)");
219             printf("\tNX = %d\tNK = %d\tnatC: %d\toptC: %d\n", nx, nk, t_c, t_i);
221             if (nx == CYCLE_FORMULA_NX_PT1 && nk == CYCLE_FORMULA_NR_PT1)
222                 form_cycle[0] = t_i;
223             if (nx == CYCLE_FORMULA_NX_PT1 && nk == CYCLE_FORMULA_NR_PT2)
224                 form_cycle[1] = t_i;
225             if (nx == CYCLE_FORMULA_NX_PT2 && nk == CYCLE_FORMULA_NR_PT1)
226                 form_cycle[2] = t_i;
227             if (nx == CYCLE_FORMULA_NX_PT2 && nk == CYCLE_FORMULA_NR_PT2)
228                 form_cycle[3] = t_i;
229         }
230     }
232     /* Provide memory information */
233 #ifdef __TI_COMPILER_VERSION__            // for TI compiler only
234     printf("Memory:  %d bytes\n", &kernel_size);
235 #endif
237     /* Provide profiling information */
238     for (i = 0; i < FORMULA_SIZE; i++) {
239         form_temp[i] = 0;
240         for (j = 0; j < FORMULA_SIZE; j++) {
241             form_temp[i] += form_inv[i][j] * form_cycle[j];
242         }
243         if (i != (FORMULA_SIZE-1)) {
244             form_result[i] = (int) (form_temp[i] * FORMULA_DEVIDE + 0.5);
245             if ((form_result[i] - form_temp[i] * FORMULA_DEVIDE) >  0.1 ||
246                 (form_result[i] - form_temp[i] * FORMULA_DEVIDE) < -0.1) {
247                 form_error = 1;
248             }
249         }
250         else {
251             form_result[i] = (int) (form_temp[i] + 0.5);
252         }
253     }
255     if (!form_error) {
256       printf("Cycles:  ");
257       if (form_result[0])
258         printf("%d/%d*Nr*Nh + ", form_result[0], FORMULA_DEVIDE);
259       if (form_result[1])
260         printf("%d/%d*Nr + ", form_result[1], FORMULA_DEVIDE);
261       if (form_result[2])
262         printf("%d/%d*Nh + ", form_result[2], FORMULA_DEVIDE);
263       if (form_result[3])
264         printf("%d\n", form_result[3]);
265     }
266     else
267       printf("Cycles Formula Not Available\n");
269     return (fail);
272 /* ======================================================================= */
273 /* Prototypes for timing functions                                         */
274 /* ======================================================================= */
275 clock_t time_c(int nx, int nk)
277     clock_t t_start, t_stop;
279     t_start = _itoll(TSCH, TSCL);
280     DSP_iir_lat_cn(ptr_x, nx, k, nk, ptr_b_c, ptr_r_c);
281     t_stop = _itoll(TSCH, TSCL);
282     return t_stop - t_start;
285 clock_t time_i(int nx, int nk)
287     clock_t t_start, t_stop;
288     t_start = _itoll(TSCH, TSCL);
289     DSP_iir_lat(ptr_x, nx, k, nk, ptr_b_i, ptr_r_i);
290     t_stop = _itoll(TSCH, TSCL);
291     return t_stop - t_start;
294 /* ======================================================================= */
295 /*  End of file:  DSP_iir_d.c                                              */
296 /* ----------------------------------------------------------------------- */
297 /*            Copyright (c) 2011 Texas Instruments, Incorporated.          */
298 /*                           All Rights Reserved.                          */
299 /* ======================================================================= */