]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - ep-processor-libraries/dsplib.git/blob - ti/dsplib/src/DSP_mat_mul/c66/DSP_mat_mul_cn.c
DSPLIB: optimized signal processing functions for TI DSPs
[ep-processor-libraries/dsplib.git] / ti / dsplib / src / DSP_mat_mul / c66 / DSP_mat_mul_cn.c
1 /* ======================================================================== */
2 /* DSP_mat_mul_cn.c -- Perform Matrix Multiplication                        */
3 /*                     Natural C Implementation                             */
4 /*                                                                          */
5 /* Rev 0.0.1                                                                */
6 /*                                                                          */
7 /*  USAGE                                                                   */
8 /*      This routine is C-callable and can be called as:                    */
9 /*                                                                          */
10 /*          void DSP_mat_mul_cn (                                           */
11 /*              const short *restrict x, int r1, int c1r2,                  */
12 /*              const short *restrict y,         int c2,                    */
13 /*              short       *restrict r,                                    */
14 /*              int                   qs                                    */
15 /*          );                                                              */
16 /*                                                                          */
17 /*      x  == Pointer to r1 by c1 input matrix.                             */
18 /*      y  == Pointer to r2 by c2 input matrix.                             */
19 /*      r  == Pointer to r1 by c2 output matrix.                            */
20 /*                                                                          */
21 /*      r1   == Number of rows in x.                                        */
22 /*      c1r2 == Number of columns in x.  Also number of rows in y.          */
23 /*      c2   == Number of columns in y.                                     */
24 /*                                                                          */
25 /*      qs == Final right-shift to apply to the result.                     */
26 /*                                                                          */
27 /*  DESCRIPTION                                                             */
28 /*      This function computes the expression "r = x * y" for the           */
29 /*      matrices x and y.  The columnar dimension of x must match           */
30 /*      the row dimension of y.  The resulting matrix has the same          */
31 /*      number of rows as x and the same number of columns as y.            */
32 /*                                                                          */
33 /*      The values stored in the matrices are assumed to be fixed-point     */
34 /*      or integer values.  All intermediate sums are retained to 32-bit    */
35 /*      precision, and no overflow checking is performed.  The results      */
36 /*      are right-shifted by a user-specified amount, and then truncated    */
37 /*      to 16 bits.                                                         */
38 /*                                                                          */
39 /*      This code is suitable for dense matrices.  No optimizations are     */
40 /*      made for sparse matrices.                                           */
41 /*                                                                          */
42 /*  ASSUMPTIONS                                                             */
43 /*      The arrays 'x', 'y', and 'r' are stored in distinct arrays.  That   */
44 /*      is, in-place processing is not allowed.                             */
45 /*                                                                          */
46 /*      The input matrices have minimum dimensions of at least 1 row and    */
47 /*      1 column, and no more than 32767 rows or 32767 columns.             */
48 /*                                                                          */
49 /* Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/  */ 
50 /*                                                                         */
51 /*                                                                         */
52 /*  Redistribution and use in source and binary forms, with or without     */
53 /*  modification, are permitted provided that the following conditions     */
54 /*  are met:                                                               */
55 /*                                                                         */
56 /*    Redistributions of source code must retain the above copyright       */
57 /*    notice, this list of conditions and the following disclaimer.        */
58 /*                                                                         */
59 /*    Redistributions in binary form must reproduce the above copyright    */
60 /*    notice, this list of conditions and the following disclaimer in the  */
61 /*    documentation and/or other materials provided with the               */
62 /*    distribution.                                                        */
63 /*                                                                         */
64 /*    Neither the name of Texas Instruments Incorporated nor the names of  */
65 /*    its contributors may be used to endorse or promote products derived  */
66 /*    from this software without specific prior written permission.        */
67 /*                                                                         */
68 /*  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS    */
69 /*  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT      */
70 /*  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR  */
71 /*  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT   */
72 /*  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,  */
73 /*  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT       */
74 /*  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,  */
75 /*  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY  */
76 /*  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT    */
77 /*  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE  */
78 /*  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.   */
79 /*                                                                         */
80 /* ======================================================================= */
82 #pragma CODE_SECTION(DSP_mat_mul_cn, ".text:ansi");
84 #include "DSP_mat_mul_cn.h"
86 void DSP_mat_mul_cn (
87     const short *restrict x, int r1, int c1r2,
88     const short *restrict y,         int c2,
89     short       *restrict r,
90     int                   qs
91 )
92 {
93     int i, j, k;
94     int sum;
96 #ifndef NOASSUME
97     _nassert(r1 >= 1 && r1 <= 32767);
98     _nassert(c1r2 >= 1 && c1r2 <= 32767);
99     _nassert(c2 >= 1 && c2 <= 32767);
100 #endif
102     /* -------------------------------------------------------------------- */
103     /*  Multiply each row in x by each column in y.  The product of row m   */
104     /*  in x and column n in y is placed in position (m,n) in the result.   */
105     /* -------------------------------------------------------------------- */
106     for (i = 0; i < r1; i++) {
107         for (j = 0; j < c2; j++) {
108             for (k = 0, sum = 0; k < c1r2; k++)
109                 sum += x[k + i*c1r2] * y[j + k*c2];
110             r[j + i*c2] = sum >> qs;
111         }
112     }
115 /* ======================================================================== */
116 /*  End of file:  DSP_mat_mul_cn.c                                          */
117 /* ------------------------------------------------------------------------ */
118 /*          Copyright (C) 2011 Texas Instruments, Incorporated.             */
119 /*                          All Rights Reserved.                            */
120 /* ======================================================================== */