1 /* ----------------------------------------------------------------------
2 * Project: CMSIS DSP Library
3 * Title: arm_mat_mult_f32.c
4 * Description: Floating-point matrix multiplication
6 * $Date: 27. January 2017
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.
32 * @ingroup groupMatrix
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>.
53 * @addtogroup MatrixMult
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
;
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 */
108 /* Output pointer is set to starting address of the row being processed */
111 /* For every row wise process, the column loop counter is to be initiated */
114 /* For every row wise process, the pIn2 pointer is set
115 ** to the starting address of the pSrcB data */
123 /* Set the variable sum, that acts as accumulator, to zero */
126 /* Initiate the pointer pIn1 to point to the starting address of the column being processed */
129 /* Apply loop unrolling and compute 4 MACs simultaneously. */
130 colCnt
= numColsA
>> 2U;
132 /* matrix multiplication */
135 /* c(m,n) = a(1,1)*b(1,1) + a(1,2) * b(2,1) + .... + a(m,p)*b(p,n) */
155 /* Decrement the loop count */
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
;
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
);
169 /* Decrement the loop counter */
173 /* Store the result in the destination buffer */
176 /* Update the pointer pIn2 to point to the starting address of the next column */
178 pIn2
= pSrcB
->pData
+ j
;
180 /* Decrement the column loop counter */
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
;
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 */
211 /* Output pointer is set to starting address of the row being processed */
214 /* For every row wise process, the column loop counter is to be initiated */
217 /* For every row wise process, the pIn2 pointer is set
218 ** to the starting address of the pSrcB data */
224 /* Set the variable sum, that acts as accumulator, to zero */
227 /* Initialize the pointer pIn1 to point to the starting address of the row being processed */
230 /* Matrix A columns number of MAC operations are to be performed */
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
);
239 /* Decrement the loop counter */
243 /* Store the result in the destination buffer */
246 /* Decrement the column loop counter */
249 /* Update the pointer pIn2 to point to the starting address of the next column */
250 pIn2
= pInB
+ (numColsB
- col
);
254 #endif /* #if defined (ARM_MATH_DSP) */
256 /* Update the pointer pInA to point to the starting address of the next row */
258 pInA
= pInA
+ numColsA
;
260 /* Decrement the row loop counter */
264 /* Set status as ARM_MATH_SUCCESS */
265 status
= ARM_MATH_SUCCESS
;
268 /* Return to application */
273 * @} end of MatrixMult group