]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - ep-processor-libraries/dsplib.git/blob - ti/dsplib/src/DSP_mat_mul_cplx/c64P/DSP_mat_mul_cplx_d.c
DSPLIB: optimized signal processing functions for TI DSPs
[ep-processor-libraries/dsplib.git] / ti / dsplib / src / DSP_mat_mul_cplx / c64P / DSP_mat_mul_cplx_d.c
1 /* ======================================================================= */
2 /* DSP_mat_mul_cplx_d.c -- Perform Matrix Multiplication                   */
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 #include "DSP_mat_mul_cplx_cn.h"
47 #include "DSP_mat_mul_cplx.h"
49 /* Defines */
50 #if defined(__TI_EABI__)
51 #define kernel_size _kernel_size
52 #endif
54 extern char kernel_size;
56 #define FORMULA_SIZE          8
57 #define FORMULA_DEVIDE        2
58 #define CYCLE_FORMULA_R1_PT1  12
59 #define CYCLE_FORMULA_R1_PT2  8
60 #define CYCLE_FORMULA_R2_PT1  12
61 #define CYCLE_FORMULA_R2_PT2  8
62 #define CYCLE_FORMULA_C2_PT1  12
63 #define CYCLE_FORMULA_C2_PT2  8
64 /* inverse of [12*12*12 12*12 12*12 12*12 12 12 12 1] */
65 /*            [12*12* 8 12*12 12* 8 12* 8 12 12  8 1] */
66 /*            [12* 8*12 12* 8 12*12  8*12 12  8 12 1] */
67 /*            [ 8*12*12  8*12  8*12 12*12  8 12 12 1] */
68 /*            [12* 8* 8 12* 8 12* 8  8* 8 12  8  8 1] */
69 /*            [ 8*12* 8  8*12  8* 8 12* 8  8 12  8 1] */
70 /*            [ 8* 8*12  8* 8  8*12  8*12  8  8 12 1] */
71 /*            [ 8* 8* 8  8* 8  8* 8  8* 8  8  8  8 1] */
72 float form_inv[FORMULA_SIZE][FORMULA_SIZE] = 
73 {{ 0.0156,   -0.0156,   -0.0156,   -0.0156,    0.0156,    0.0156,    0.0156,   -0.0156},
74  {-0.1250,    0.1875,    0.1250,    0.1250,   -0.1875,   -0.1875,   -0.1250,    0.1875},
75  {-0.1250,    0.1250,    0.1875,    0.1250,   -0.1875,   -0.1250,   -0.1875,    0.1875},
76  {-0.1250,    0.1250,    0.1250,    0.1875,   -0.1250,   -0.1875,   -0.1875,    0.1875},
77  { 1.0000,   -1.5000,   -1.5000,   -1.0000,    2.2500,    1.5000,    1.5000,   -2.2500},
78  { 1.0000,   -1.5000,   -1.0000,   -1.5000,    1.5000,    2.2500,    1.5000,   -2.2500},
79  { 1.0000,   -1.0000,   -1.5000,   -1.5000,    1.5000,    1.5000,    2.2500,   -2.2500},
80  {-8.0000,   12.0000,   12.0000,   12.0000,  -18.0000,  -18.0000,  -18.0000,   27.0000}
81 };
82 float form_temp  [FORMULA_SIZE];
83 int   form_cycle [FORMULA_SIZE];
84 int   form_result[FORMULA_SIZE];
86 /* ======================================================================== */
87 /*  Kernel-specific alignments                                              */
88 /* ======================================================================== */
89 #pragma DATA_ALIGN(x, 8);
90 #pragma DATA_ALIGN(y, 8);
91 #pragma DATA_ALIGN(r_c, 8);
92 #pragma DATA_ALIGN(r_i, 8);
94 /* ======================================================================== */
95 /*  Parameters of fixed dataset.                                            */
96 /* ======================================================================== */
97 #define R1   (16)
98 #define C1R2 (16)
99 #define   C2 (16)
100 #define R1C1 (R1*C1R2)
101 #define R1C2 (R1*C2)
102 #define R2C2 (C2*C1R2)
104 short x[R1C1*2];
105 short y[R2C2*2];
107 short r_c[R1C2*2];
108 short r_i[R1C2*2];
110 // Right shift the result by qs bits
111 int qs = 0;
113 /* ======================================================================== */
114 /*  Generate pointers to point to matrices                                  */
115 /* ======================================================================== */
116 const short *const ptr_x = (short *)x;
117 const short *const ptr_y = (short *)y;
118 short *const ptr_r_c = (short *)r_c;
119 short *const ptr_r_i = (short *)r_i;
121 /* ======================================================================== */
122 /*  Prototypes for timing functions.                                        */
123 /* ======================================================================== */
124 clock_t time_c(int r1, int c1r2, int c2);
125 clock_t time_i(int r1, int c1r2, int c2);
127 /* ======================================================================== */
128 /*  MAIN -- Top level driver for the test.                                  */
129 /* ======================================================================== */
130 int main()
131 {   
132     clock_t t_overhead, t_start, t_stop;
133     clock_t t_c, t_i;
134     int i, j, r1 = R1, c1r2 = C1R2, c2 = C2, fail = 0;
135     int form_error = 0;
137     /* -------------------------------------------------------------------- */
138     /* Initialize timer for clock */
139     TSCL= 0,TSCH=0;
140     /*  Compute the overhead of calling _itoll(TSCH, TSCL) twice to get timing info.   */
141     /* -------------------------------------------------------------------- */
142     t_start    = _itoll(TSCH, TSCL);
143     t_stop     = _itoll(TSCH, TSCL);
144     t_overhead = t_stop - t_start;
146     /* =================== */    
147     /* Initialize matrices */
148     /* =================== */
149     for (i=0; i<R1C2*2; i++)  {
150         x[i] = (short) ((int) rand()>>2);
151     }
152     for (i=0; i<R2C2*2; i++) {
153         y[i] = (short) ((int) rand()>>2);
154     }
156     for(r1 = 4, i = 1; r1 <= R1; r1 += 4) {
157         for(c1r2 = 4; c1r2 <= C1R2; c1r2 += 4) {
158             for(c2 = 4; c2 <= C2; i++, c2 += 4) {
159  
160                 /* -------------------------------------------------------- */
161                 /*  Force uninitialized arrays to fixed values.             */
162                 /* -------------------------------------------------------- */
163                 memset(r_c, 0xA5, sizeof(r_c));
164                 memset(r_i, 0xA5, sizeof(r_i));
166                 /* -------------------------------------------------------- */
167                 /*  Call the individual timing routines, rhead.             */
168                 /*  and subtract off overhead.                              */
169                 /* -------------------------------------------------------- */
170                 t_c = time_c(r1, c1r2, c2) - t_overhead;
171             
172                 t_i = time_i(r1, c1r2, c2) - t_overhead;
173     
174                 /* -------------------------------------------------------- */
175                 /*  Print timing results.                                   */
176                 /* -------------------------------------------------------- */
177                 printf("DSP_mat_mul_cplx\tIter#: %d\t", i);
179                 /* -------------------------------------------------------- */
180                 /*  Check the results arrays, and report any failures.      */
181                 /* -------------------------------------------------------- */
182                 if (memcmp(r_i, r_c, sizeof(r_c))) {
183                     fail++;
184                     printf("Result Failure (r_i)");
185                 }
186                 else
187                     printf("Result Succesful (r_i)");
189                 printf("\tR1 = %d\tC1R2 = %d\tC2 = %d\tnatC: %d\tintC: %d\n", r1, c1r2, c2, t_c, t_i);
191                 if (c2 == CYCLE_FORMULA_C2_PT1 && c1r2 == CYCLE_FORMULA_R2_PT1 && r1 == CYCLE_FORMULA_R1_PT1)
192                     form_cycle[0] = t_i;
193                 if (c2 == CYCLE_FORMULA_C2_PT1 && c1r2 == CYCLE_FORMULA_R2_PT2 && r1 == CYCLE_FORMULA_R1_PT1)
194                     form_cycle[1] = t_i;
195                 if (c2 == CYCLE_FORMULA_C2_PT2 && c1r2 == CYCLE_FORMULA_R2_PT1 && r1 == CYCLE_FORMULA_R1_PT1)
196                     form_cycle[2] = t_i;
197                 if (c2 == CYCLE_FORMULA_C2_PT1 && c1r2 == CYCLE_FORMULA_R2_PT1 && r1 == CYCLE_FORMULA_R1_PT2)
198                     form_cycle[3] = t_i;
199                 if (c2 == CYCLE_FORMULA_C2_PT2 && c1r2 == CYCLE_FORMULA_R2_PT2 && r1 == CYCLE_FORMULA_R1_PT1)
200                     form_cycle[4] = t_i;
201                 if (c2 == CYCLE_FORMULA_C2_PT1 && c1r2 == CYCLE_FORMULA_R2_PT2 && r1 == CYCLE_FORMULA_R1_PT2)
202                     form_cycle[5] = t_i;
203                 if (c2 == CYCLE_FORMULA_C2_PT2 && c1r2 == CYCLE_FORMULA_R2_PT1 && r1 == CYCLE_FORMULA_R1_PT2)
204                     form_cycle[6] = t_i;
205                 if (c2 == CYCLE_FORMULA_C2_PT2 && c1r2 == CYCLE_FORMULA_R2_PT2 && r1 == CYCLE_FORMULA_R1_PT2)
206                     form_cycle[7] = t_i;
207            }
208         }
209     }
211     /* Provide memory information */
212 #ifdef __TI_COMPILER_VERSION__            // for TI compiler only
213     printf("Memory:  %d bytes\n", &kernel_size);
214 #endif
216     /* Provide profiling information */
217     for (i = 0; i < FORMULA_SIZE; i++) {
218         form_temp[i] = 0;
219         for (j = 0; j < FORMULA_SIZE; j++) {
220             form_temp[i] += form_inv[i][j] * form_cycle[j];
221         }
222         if (i != (FORMULA_SIZE-1)) {
223             form_result[i] = (int) (form_temp[i] * FORMULA_DEVIDE + 0.5);
224             if ((form_result[i] - form_temp[i] * FORMULA_DEVIDE) >  0.1 ||
225                 (form_result[i] - form_temp[i] * FORMULA_DEVIDE) < -0.1) {
226                 form_error = 1;
227             }
228         }
229         else {
230             form_result[i] = (int) (form_temp[i] + 0.5);
231         }
232     }
234     if (!form_error) {
235       printf("Cycles:  ");
236       if (form_result[0])
237         printf("%d/%d*r1*c2*c1 + ", form_result[0], FORMULA_DEVIDE);
238       if (form_result[1])
239         printf("%d/%d*r1*c2 + ", form_result[1], FORMULA_DEVIDE);
240       if (form_result[2])
241         printf("%d/%d*r1*c1 + ", form_result[2], FORMULA_DEVIDE);
242       if (form_result[3])
243         printf("%d/%d*c2*c1 + ", form_result[3], FORMULA_DEVIDE);
244       if (form_result[4])
245         printf("%d/%d*r1 + ", form_result[4], FORMULA_DEVIDE);
246       if (form_result[5])
247         printf("%d/%d*c2 + ", form_result[5], FORMULA_DEVIDE);
248       if (form_result[6])
249         printf("%d/%d*c1 + ", form_result[6], FORMULA_DEVIDE);
250       if (form_result[7])
251         printf("%d\n", form_result[7]);
252     }
253     else
254       printf("Cycles Formula Not Available\n");
256     return (fail);
259 /* ======================================================================== */
260 /*  TIME_C   -- Measure elapsed time for natural C version.                 */
261 /* ======================================================================== */
262 clock_t time_c(int r1, int c1r2, int c2)
264     clock_t t_start, t_stop;
266     t_start = _itoll(TSCH, TSCL);
267     DSP_mat_mul_cplx_cn(ptr_x, r1, c1r2, ptr_y, c2, ptr_r_c, qs);
268     t_stop = _itoll(TSCH, TSCL);
269     return t_stop - t_start;
272 /* ======================================================================== */
273 /*  TIME_I   -- Measure elapsed time for intrinsic C version.               */
274 /* ======================================================================== */
275 clock_t time_i(int r1, int c1r2, int c2)
277     clock_t t_start, t_stop;
279     t_start = _itoll(TSCH, TSCL);
280     DSP_mat_mul_cplx(ptr_x, r1, c1r2, ptr_y, c2, ptr_r_i, qs);
281     t_stop = _itoll(TSCH, TSCL);
282     return t_stop - t_start;
285 /* ======================================================================== */
286 /*  End of file:  DSP_mat_mul_cplx_d.c                                      */
287 /* ------------------------------------------------------------------------ */
288 /*          Copyright (C) 2011 Texas Instruments, Incorporated.             */
289 /*                          All Rights Reserved.                            */
290 /* ======================================================================== */