提交 | 用户 | age
|
8b51c7
|
1 |
/* ---------------------------------------------------------------------- |
Q |
2 |
* Copyright (C) 2010-2014 ARM Limited. All rights reserved. |
|
3 |
* |
|
4 |
* $Date: 19. March 2015 |
|
5 |
* $Revision: V.1.4.5 |
|
6 |
* |
|
7 |
* Project: CMSIS DSP Library |
|
8 |
* Title: arm_mat_mult_f32.c |
|
9 |
* |
|
10 |
* Description: Floating-point matrix multiplication. |
|
11 |
* |
|
12 |
* Target Processor: Cortex-M4/Cortex-M3/Cortex-M0 |
|
13 |
* |
|
14 |
* Redistribution and use in source and binary forms, with or without |
|
15 |
* modification, are permitted provided that the following conditions |
|
16 |
* are met: |
|
17 |
* - Redistributions of source code must retain the above copyright |
|
18 |
* notice, this list of conditions and the following disclaimer. |
|
19 |
* - Redistributions in binary form must reproduce the above copyright |
|
20 |
* notice, this list of conditions and the following disclaimer in |
|
21 |
* the documentation and/or other materials provided with the |
|
22 |
* distribution. |
|
23 |
* - Neither the name of ARM LIMITED nor the names of its contributors |
|
24 |
* may be used to endorse or promote products derived from this |
|
25 |
* software without specific prior written permission. |
|
26 |
* |
|
27 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
28 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
29 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
|
30 |
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
|
31 |
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
|
32 |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
|
33 |
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
|
34 |
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
|
35 |
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
|
36 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
|
37 |
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
|
38 |
* POSSIBILITY OF SUCH DAMAGE. |
|
39 |
* -------------------------------------------------------------------- */ |
|
40 |
|
|
41 |
#include "arm_math.h" |
|
42 |
|
|
43 |
/** |
|
44 |
* @ingroup groupMatrix |
|
45 |
*/ |
|
46 |
|
|
47 |
/** |
|
48 |
* @defgroup MatrixMult Matrix Multiplication |
|
49 |
* |
|
50 |
* Multiplies two matrices. |
|
51 |
* |
|
52 |
* \image html MatrixMultiplication.gif "Multiplication of two 3 x 3 matrices" |
|
53 |
|
|
54 |
* Matrix multiplication is only defined if the number of columns of the |
|
55 |
* first matrix equals the number of rows of the second matrix. |
|
56 |
* Multiplying an <code>M x N</code> matrix with an <code>N x P</code> matrix results |
|
57 |
* in an <code>M x P</code> matrix. |
|
58 |
* When matrix size checking is enabled, the functions check: (1) that the inner dimensions of |
|
59 |
* <code>pSrcA</code> and <code>pSrcB</code> are equal; and (2) that the size of the output |
|
60 |
* matrix equals the outer dimensions of <code>pSrcA</code> and <code>pSrcB</code>. |
|
61 |
*/ |
|
62 |
|
|
63 |
|
|
64 |
/** |
|
65 |
* @addtogroup MatrixMult |
|
66 |
* @{ |
|
67 |
*/ |
|
68 |
|
|
69 |
/** |
|
70 |
* @brief Floating-point matrix multiplication. |
|
71 |
* @param[in] *pSrcA points to the first input matrix structure |
|
72 |
* @param[in] *pSrcB points to the second input matrix structure |
|
73 |
* @param[out] *pDst points to output matrix structure |
|
74 |
* @return The function returns either |
|
75 |
* <code>ARM_MATH_SIZE_MISMATCH</code> or <code>ARM_MATH_SUCCESS</code> based on the outcome of size checking. |
|
76 |
*/ |
|
77 |
|
|
78 |
arm_status arm_mat_mult_f32( |
|
79 |
const arm_matrix_instance_f32 * pSrcA, |
|
80 |
const arm_matrix_instance_f32 * pSrcB, |
|
81 |
arm_matrix_instance_f32 * pDst) |
|
82 |
{ |
|
83 |
float32_t *pIn1 = pSrcA->pData; /* input data matrix pointer A */ |
|
84 |
float32_t *pIn2 = pSrcB->pData; /* input data matrix pointer B */ |
|
85 |
float32_t *pInA = pSrcA->pData; /* input data matrix pointer A */ |
|
86 |
float32_t *pOut = pDst->pData; /* output data matrix pointer */ |
|
87 |
float32_t *px; /* Temporary output data matrix pointer */ |
|
88 |
float32_t sum; /* Accumulator */ |
|
89 |
uint16_t numRowsA = pSrcA->numRows; /* number of rows of input matrix A */ |
|
90 |
uint16_t numColsB = pSrcB->numCols; /* number of columns of input matrix B */ |
|
91 |
uint16_t numColsA = pSrcA->numCols; /* number of columns of input matrix A */ |
|
92 |
|
|
93 |
#ifndef ARM_MATH_CM0_FAMILY |
|
94 |
|
|
95 |
/* Run the below code for Cortex-M4 and Cortex-M3 */ |
|
96 |
|
|
97 |
float32_t in1, in2, in3, in4; |
|
98 |
uint16_t col, i = 0u, j, row = numRowsA, colCnt; /* loop counters */ |
|
99 |
arm_status status; /* status of matrix multiplication */ |
|
100 |
|
|
101 |
#ifdef ARM_MATH_MATRIX_CHECK |
|
102 |
|
|
103 |
|
|
104 |
/* Check for matrix mismatch condition */ |
|
105 |
if((pSrcA->numCols != pSrcB->numRows) || |
|
106 |
(pSrcA->numRows != pDst->numRows) || (pSrcB->numCols != pDst->numCols)) |
|
107 |
{ |
|
108 |
|
|
109 |
/* Set status as ARM_MATH_SIZE_MISMATCH */ |
|
110 |
status = ARM_MATH_SIZE_MISMATCH; |
|
111 |
} |
|
112 |
else |
|
113 |
#endif /* #ifdef ARM_MATH_MATRIX_CHECK */ |
|
114 |
|
|
115 |
{ |
|
116 |
/* The following loop performs the dot-product of each row in pSrcA with each column in pSrcB */ |
|
117 |
/* row loop */ |
|
118 |
do |
|
119 |
{ |
|
120 |
/* Output pointer is set to starting address of the row being processed */ |
|
121 |
px = pOut + i; |
|
122 |
|
|
123 |
/* For every row wise process, the column loop counter is to be initiated */ |
|
124 |
col = numColsB; |
|
125 |
|
|
126 |
/* For every row wise process, the pIn2 pointer is set |
|
127 |
** to the starting address of the pSrcB data */ |
|
128 |
pIn2 = pSrcB->pData; |
|
129 |
|
|
130 |
j = 0u; |
|
131 |
|
|
132 |
/* column loop */ |
|
133 |
do |
|
134 |
{ |
|
135 |
/* Set the variable sum, that acts as accumulator, to zero */ |
|
136 |
sum = 0.0f; |
|
137 |
|
|
138 |
/* Initiate the pointer pIn1 to point to the starting address of the column being processed */ |
|
139 |
pIn1 = pInA; |
|
140 |
|
|
141 |
/* Apply loop unrolling and compute 4 MACs simultaneously. */ |
|
142 |
colCnt = numColsA >> 2u; |
|
143 |
|
|
144 |
/* matrix multiplication */ |
|
145 |
while(colCnt > 0u) |
|
146 |
{ |
|
147 |
/* c(m,n) = a(1,1)*b(1,1) + a(1,2) * b(2,1) + .... + a(m,p)*b(p,n) */ |
|
148 |
in3 = *pIn2; |
|
149 |
pIn2 += numColsB; |
|
150 |
in1 = pIn1[0]; |
|
151 |
in2 = pIn1[1]; |
|
152 |
sum += in1 * in3; |
|
153 |
in4 = *pIn2; |
|
154 |
pIn2 += numColsB; |
|
155 |
sum += in2 * in4; |
|
156 |
|
|
157 |
in3 = *pIn2; |
|
158 |
pIn2 += numColsB; |
|
159 |
in1 = pIn1[2]; |
|
160 |
in2 = pIn1[3]; |
|
161 |
sum += in1 * in3; |
|
162 |
in4 = *pIn2; |
|
163 |
pIn2 += numColsB; |
|
164 |
sum += in2 * in4; |
|
165 |
pIn1 += 4u; |
|
166 |
|
|
167 |
/* Decrement the loop count */ |
|
168 |
colCnt--; |
|
169 |
} |
|
170 |
|
|
171 |
/* If the columns of pSrcA is not a multiple of 4, compute any remaining MACs here. |
|
172 |
** No loop unrolling is used. */ |
|
173 |
colCnt = numColsA % 0x4u; |
|
174 |
|
|
175 |
while(colCnt > 0u) |
|
176 |
{ |
|
177 |
/* c(m,n) = a(1,1)*b(1,1) + a(1,2) * b(2,1) + .... + a(m,p)*b(p,n) */ |
|
178 |
sum += *pIn1++ * (*pIn2); |
|
179 |
pIn2 += numColsB; |
|
180 |
|
|
181 |
/* Decrement the loop counter */ |
|
182 |
colCnt--; |
|
183 |
} |
|
184 |
|
|
185 |
/* Store the result in the destination buffer */ |
|
186 |
*px++ = sum; |
|
187 |
|
|
188 |
/* Update the pointer pIn2 to point to the starting address of the next column */ |
|
189 |
j++; |
|
190 |
pIn2 = pSrcB->pData + j; |
|
191 |
|
|
192 |
/* Decrement the column loop counter */ |
|
193 |
col--; |
|
194 |
|
|
195 |
} while(col > 0u); |
|
196 |
|
|
197 |
#else |
|
198 |
|
|
199 |
/* Run the below code for Cortex-M0 */ |
|
200 |
|
|
201 |
float32_t *pInB = pSrcB->pData; /* input data matrix pointer B */ |
|
202 |
uint16_t col, i = 0u, row = numRowsA, colCnt; /* loop counters */ |
|
203 |
arm_status status; /* status of matrix multiplication */ |
|
204 |
|
|
205 |
#ifdef ARM_MATH_MATRIX_CHECK |
|
206 |
|
|
207 |
/* Check for matrix mismatch condition */ |
|
208 |
if((pSrcA->numCols != pSrcB->numRows) || |
|
209 |
(pSrcA->numRows != pDst->numRows) || (pSrcB->numCols != pDst->numCols)) |
|
210 |
{ |
|
211 |
|
|
212 |
/* Set status as ARM_MATH_SIZE_MISMATCH */ |
|
213 |
status = ARM_MATH_SIZE_MISMATCH; |
|
214 |
} |
|
215 |
else |
|
216 |
#endif /* #ifdef ARM_MATH_MATRIX_CHECK */ |
|
217 |
|
|
218 |
{ |
|
219 |
/* The following loop performs the dot-product of each row in pInA with each column in pInB */ |
|
220 |
/* row loop */ |
|
221 |
do |
|
222 |
{ |
|
223 |
/* Output pointer is set to starting address of the row being processed */ |
|
224 |
px = pOut + i; |
|
225 |
|
|
226 |
/* For every row wise process, the column loop counter is to be initiated */ |
|
227 |
col = numColsB; |
|
228 |
|
|
229 |
/* For every row wise process, the pIn2 pointer is set |
|
230 |
** to the starting address of the pSrcB data */ |
|
231 |
pIn2 = pSrcB->pData; |
|
232 |
|
|
233 |
/* column loop */ |
|
234 |
do |
|
235 |
{ |
|
236 |
/* Set the variable sum, that acts as accumulator, to zero */ |
|
237 |
sum = 0.0f; |
|
238 |
|
|
239 |
/* Initialize the pointer pIn1 to point to the starting address of the row being processed */ |
|
240 |
pIn1 = pInA; |
|
241 |
|
|
242 |
/* Matrix A columns number of MAC operations are to be performed */ |
|
243 |
colCnt = numColsA; |
|
244 |
|
|
245 |
while(colCnt > 0u) |
|
246 |
{ |
|
247 |
/* c(m,n) = a(1,1)*b(1,1) + a(1,2) * b(2,1) + .... + a(m,p)*b(p,n) */ |
|
248 |
sum += *pIn1++ * (*pIn2); |
|
249 |
pIn2 += numColsB; |
|
250 |
|
|
251 |
/* Decrement the loop counter */ |
|
252 |
colCnt--; |
|
253 |
} |
|
254 |
|
|
255 |
/* Store the result in the destination buffer */ |
|
256 |
*px++ = sum; |
|
257 |
|
|
258 |
/* Decrement the column loop counter */ |
|
259 |
col--; |
|
260 |
|
|
261 |
/* Update the pointer pIn2 to point to the starting address of the next column */ |
|
262 |
pIn2 = pInB + (numColsB - col); |
|
263 |
|
|
264 |
} while(col > 0u); |
|
265 |
|
|
266 |
#endif /* #ifndef ARM_MATH_CM0_FAMILY */ |
|
267 |
|
|
268 |
/* Update the pointer pInA to point to the starting address of the next row */ |
|
269 |
i = i + numColsB; |
|
270 |
pInA = pInA + numColsA; |
|
271 |
|
|
272 |
/* Decrement the row loop counter */ |
|
273 |
row--; |
|
274 |
|
|
275 |
} while(row > 0u); |
|
276 |
/* Set status as ARM_MATH_SUCCESS */ |
|
277 |
status = ARM_MATH_SUCCESS; |
|
278 |
} |
|
279 |
|
|
280 |
/* Return to application */ |
|
281 |
return (status); |
|
282 |
} |
|
283 |
|
|
284 |
/** |
|
285 |
* @} end of MatrixMult group |
|
286 |
*/ |