Merge pull request #11270 from haslinghuis/rename_attr
[betaflight.git] / lib / main / CMSIS / DSP / Source / MatrixFunctions / arm_mat_mult_f32.c
bloba038f2ff0cf796689479d247ef85a865a4d87e88
1 /* ----------------------------------------------------------------------
2 * Project: CMSIS DSP Library
3 * Title: arm_mat_mult_f32.c
4 * Description: Floating-point matrix multiplication
6 * $Date: 27. January 2017
7 * $Revision: V.1.5.1
9 * Target Processor: Cortex-M cores
10 * -------------------------------------------------------------------- */
12 * Copyright (C) 2010-2017 ARM Limited or its affiliates. All rights reserved.
14 * SPDX-License-Identifier: Apache-2.0
16 * Licensed under the Apache License, Version 2.0 (the License); you may
17 * not use this file except in compliance with the License.
18 * You may obtain a copy of the License at
20 * www.apache.org/licenses/LICENSE-2.0
22 * Unless required by applicable law or agreed to in writing, software
23 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
24 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25 * See the License for the specific language governing permissions and
26 * limitations under the License.
29 #include "arm_math.h"
31 /**
32 * @ingroup groupMatrix
35 /**
36 * @defgroup MatrixMult Matrix Multiplication
38 * Multiplies two matrices.
40 * \image html MatrixMultiplication.gif "Multiplication of two 3 x 3 matrices"
42 * Matrix multiplication is only defined if the number of columns of the
43 * first matrix equals the number of rows of the second matrix.
44 * Multiplying an <code>M x N</code> matrix with an <code>N x P</code> matrix results
45 * in an <code>M x P</code> matrix.
46 * When matrix size checking is enabled, the functions check: (1) that the inner dimensions of
47 * <code>pSrcA</code> and <code>pSrcB</code> are equal; and (2) that the size of the output
48 * matrix equals the outer dimensions of <code>pSrcA</code> and <code>pSrcB</code>.
52 /**
53 * @addtogroup MatrixMult
54 * @{
57 /**
58 * @brief Floating-point matrix multiplication.
59 * @param[in] *pSrcA points to the first input matrix structure
60 * @param[in] *pSrcB points to the second input matrix structure
61 * @param[out] *pDst points to output matrix structure
62 * @return The function returns either
63 * <code>ARM_MATH_SIZE_MISMATCH</code> or <code>ARM_MATH_SUCCESS</code> based on the outcome of size checking.
66 arm_status arm_mat_mult_f32(
67 const arm_matrix_instance_f32 * pSrcA,
68 const arm_matrix_instance_f32 * pSrcB,
69 arm_matrix_instance_f32 * pDst)
71 float32_t *pIn1 = pSrcA->pData; /* input data matrix pointer A */
72 float32_t *pIn2 = pSrcB->pData; /* input data matrix pointer B */
73 float32_t *pInA = pSrcA->pData; /* input data matrix pointer A */
74 float32_t *pOut = pDst->pData; /* output data matrix pointer */
75 float32_t *px; /* Temporary output data matrix pointer */
76 float32_t sum; /* Accumulator */
77 uint16_t numRowsA = pSrcA->numRows; /* number of rows of input matrix A */
78 uint16_t numColsB = pSrcB->numCols; /* number of columns of input matrix B */
79 uint16_t numColsA = pSrcA->numCols; /* number of columns of input matrix A */
81 #if defined (ARM_MATH_DSP)
83 /* Run the below code for Cortex-M4 and Cortex-M3 */
85 float32_t in1, in2, in3, in4;
86 uint16_t col, i = 0U, j, row = numRowsA, colCnt; /* loop counters */
87 arm_status status; /* status of matrix multiplication */
89 #ifdef ARM_MATH_MATRIX_CHECK
92 /* Check for matrix mismatch condition */
93 if ((pSrcA->numCols != pSrcB->numRows) ||
94 (pSrcA->numRows != pDst->numRows) || (pSrcB->numCols != pDst->numCols))
97 /* Set status as ARM_MATH_SIZE_MISMATCH */
98 status = ARM_MATH_SIZE_MISMATCH;
100 else
101 #endif /* #ifdef ARM_MATH_MATRIX_CHECK */
104 /* The following loop performs the dot-product of each row in pSrcA with each column in pSrcB */
105 /* row loop */
108 /* Output pointer is set to starting address of the row being processed */
109 px = pOut + i;
111 /* For every row wise process, the column loop counter is to be initiated */
112 col = numColsB;
114 /* For every row wise process, the pIn2 pointer is set
115 ** to the starting address of the pSrcB data */
116 pIn2 = pSrcB->pData;
118 j = 0U;
120 /* column loop */
123 /* Set the variable sum, that acts as accumulator, to zero */
124 sum = 0.0f;
126 /* Initiate the pointer pIn1 to point to the starting address of the column being processed */
127 pIn1 = pInA;
129 /* Apply loop unrolling and compute 4 MACs simultaneously. */
130 colCnt = numColsA >> 2U;
132 /* matrix multiplication */
133 while (colCnt > 0U)
135 /* c(m,n) = a(1,1)*b(1,1) + a(1,2) * b(2,1) + .... + a(m,p)*b(p,n) */
136 in3 = *pIn2;
137 pIn2 += numColsB;
138 in1 = pIn1[0];
139 in2 = pIn1[1];
140 sum += in1 * in3;
141 in4 = *pIn2;
142 pIn2 += numColsB;
143 sum += in2 * in4;
145 in3 = *pIn2;
146 pIn2 += numColsB;
147 in1 = pIn1[2];
148 in2 = pIn1[3];
149 sum += in1 * in3;
150 in4 = *pIn2;
151 pIn2 += numColsB;
152 sum += in2 * in4;
153 pIn1 += 4U;
155 /* Decrement the loop count */
156 colCnt--;
159 /* If the columns of pSrcA is not a multiple of 4, compute any remaining MACs here.
160 ** No loop unrolling is used. */
161 colCnt = numColsA % 0x4U;
163 while (colCnt > 0U)
165 /* c(m,n) = a(1,1)*b(1,1) + a(1,2) * b(2,1) + .... + a(m,p)*b(p,n) */
166 sum += *pIn1++ * (*pIn2);
167 pIn2 += numColsB;
169 /* Decrement the loop counter */
170 colCnt--;
173 /* Store the result in the destination buffer */
174 *px++ = sum;
176 /* Update the pointer pIn2 to point to the starting address of the next column */
177 j++;
178 pIn2 = pSrcB->pData + j;
180 /* Decrement the column loop counter */
181 col--;
183 } while (col > 0U);
185 #else
187 /* Run the below code for Cortex-M0 */
189 float32_t *pInB = pSrcB->pData; /* input data matrix pointer B */
190 uint16_t col, i = 0U, row = numRowsA, colCnt; /* loop counters */
191 arm_status status; /* status of matrix multiplication */
193 #ifdef ARM_MATH_MATRIX_CHECK
195 /* Check for matrix mismatch condition */
196 if ((pSrcA->numCols != pSrcB->numRows) ||
197 (pSrcA->numRows != pDst->numRows) || (pSrcB->numCols != pDst->numCols))
200 /* Set status as ARM_MATH_SIZE_MISMATCH */
201 status = ARM_MATH_SIZE_MISMATCH;
203 else
204 #endif /* #ifdef ARM_MATH_MATRIX_CHECK */
207 /* The following loop performs the dot-product of each row in pInA with each column in pInB */
208 /* row loop */
211 /* Output pointer is set to starting address of the row being processed */
212 px = pOut + i;
214 /* For every row wise process, the column loop counter is to be initiated */
215 col = numColsB;
217 /* For every row wise process, the pIn2 pointer is set
218 ** to the starting address of the pSrcB data */
219 pIn2 = pSrcB->pData;
221 /* column loop */
224 /* Set the variable sum, that acts as accumulator, to zero */
225 sum = 0.0f;
227 /* Initialize the pointer pIn1 to point to the starting address of the row being processed */
228 pIn1 = pInA;
230 /* Matrix A columns number of MAC operations are to be performed */
231 colCnt = numColsA;
233 while (colCnt > 0U)
235 /* c(m,n) = a(1,1)*b(1,1) + a(1,2) * b(2,1) + .... + a(m,p)*b(p,n) */
236 sum += *pIn1++ * (*pIn2);
237 pIn2 += numColsB;
239 /* Decrement the loop counter */
240 colCnt--;
243 /* Store the result in the destination buffer */
244 *px++ = sum;
246 /* Decrement the column loop counter */
247 col--;
249 /* Update the pointer pIn2 to point to the starting address of the next column */
250 pIn2 = pInB + (numColsB - col);
252 } while (col > 0U);
254 #endif /* #if defined (ARM_MATH_DSP) */
256 /* Update the pointer pInA to point to the starting address of the next row */
257 i = i + numColsB;
258 pInA = pInA + numColsA;
260 /* Decrement the row loop counter */
261 row--;
263 } while (row > 0U);
264 /* Set status as ARM_MATH_SUCCESS */
265 status = ARM_MATH_SUCCESS;
268 /* Return to application */
269 return (status);
273 * @} end of MatrixMult group