Merge pull request #11270 from haslinghuis/rename_attr
[betaflight.git] / lib / main / CMSIS / DSP / Source / MatrixFunctions / arm_mat_cmplx_mult_f32.c
blobbb8341e4ee13f7bfae5c491b2e011a7a079633db
1 /* ----------------------------------------------------------------------
2 * Project: CMSIS DSP Library
3 * Title: arm_mat_cmplx_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 CmplxMatrixMult Complex Matrix Multiplication
38 * Complex Matrix multiplication is only defined if the number of columns of the
39 * first matrix equals the number of rows of the second matrix.
40 * Multiplying an <code>M x N</code> matrix with an <code>N x P</code> matrix results
41 * in an <code>M x P</code> matrix.
42 * When matrix size checking is enabled, the functions check: (1) that the inner dimensions of
43 * <code>pSrcA</code> and <code>pSrcB</code> are equal; and (2) that the size of the output
44 * matrix equals the outer dimensions of <code>pSrcA</code> and <code>pSrcB</code>.
48 /**
49 * @addtogroup CmplxMatrixMult
50 * @{
53 /**
54 * @brief Floating-point Complex matrix multiplication.
55 * @param[in] *pSrcA points to the first input complex matrix structure
56 * @param[in] *pSrcB points to the second input complex matrix structure
57 * @param[out] *pDst points to output complex matrix structure
58 * @return The function returns either
59 * <code>ARM_MATH_SIZE_MISMATCH</code> or <code>ARM_MATH_SUCCESS</code> based on the outcome of size checking.
62 arm_status arm_mat_cmplx_mult_f32(
63 const arm_matrix_instance_f32 * pSrcA,
64 const arm_matrix_instance_f32 * pSrcB,
65 arm_matrix_instance_f32 * pDst)
67 float32_t *pIn1 = pSrcA->pData; /* input data matrix pointer A */
68 float32_t *pIn2 = pSrcB->pData; /* input data matrix pointer B */
69 float32_t *pInA = pSrcA->pData; /* input data matrix pointer A */
70 float32_t *pOut = pDst->pData; /* output data matrix pointer */
71 float32_t *px; /* Temporary output data matrix pointer */
72 uint16_t numRowsA = pSrcA->numRows; /* number of rows of input matrix A */
73 uint16_t numColsB = pSrcB->numCols; /* number of columns of input matrix B */
74 uint16_t numColsA = pSrcA->numCols; /* number of columns of input matrix A */
75 float32_t sumReal1, sumImag1; /* accumulator */
76 float32_t a0, b0, c0, d0;
77 float32_t a1, b1, c1, d1;
78 float32_t sumReal2, sumImag2; /* accumulator */
81 /* Run the below code for Cortex-M4 and Cortex-M3 */
83 uint16_t col, i = 0U, j, row = numRowsA, colCnt; /* loop counters */
84 arm_status status; /* status of matrix multiplication */
86 #ifdef ARM_MATH_MATRIX_CHECK
89 /* Check for matrix mismatch condition */
90 if ((pSrcA->numCols != pSrcB->numRows) ||
91 (pSrcA->numRows != pDst->numRows) || (pSrcB->numCols != pDst->numCols))
94 /* Set status as ARM_MATH_SIZE_MISMATCH */
95 status = ARM_MATH_SIZE_MISMATCH;
97 else
98 #endif /* #ifdef ARM_MATH_MATRIX_CHECK */
101 /* The following loop performs the dot-product of each row in pSrcA with each column in pSrcB */
102 /* row loop */
105 /* Output pointer is set to starting address of the row being processed */
106 px = pOut + 2 * i;
108 /* For every row wise process, the column loop counter is to be initiated */
109 col = numColsB;
111 /* For every row wise process, the pIn2 pointer is set
112 ** to the starting address of the pSrcB data */
113 pIn2 = pSrcB->pData;
115 j = 0U;
117 /* column loop */
120 /* Set the variable sum, that acts as accumulator, to zero */
121 sumReal1 = 0.0f;
122 sumImag1 = 0.0f;
124 sumReal2 = 0.0f;
125 sumImag2 = 0.0f;
127 /* Initiate the pointer pIn1 to point to the starting address of the column being processed */
128 pIn1 = pInA;
130 /* Apply loop unrolling and compute 4 MACs simultaneously. */
131 colCnt = numColsA >> 2;
133 /* matrix multiplication */
134 while (colCnt > 0U)
137 /* Reading real part of complex matrix A */
138 a0 = *pIn1;
140 /* Reading real part of complex matrix B */
141 c0 = *pIn2;
143 /* Reading imaginary part of complex matrix A */
144 b0 = *(pIn1 + 1U);
146 /* Reading imaginary part of complex matrix B */
147 d0 = *(pIn2 + 1U);
149 sumReal1 += a0 * c0;
150 sumImag1 += b0 * c0;
152 pIn1 += 2U;
153 pIn2 += 2 * numColsB;
155 sumReal2 -= b0 * d0;
156 sumImag2 += a0 * d0;
158 /* c(m,n) = a(1,1)*b(1,1) + a(1,2) * b(2,1) + .... + a(m,p)*b(p,n) */
160 a1 = *pIn1;
161 c1 = *pIn2;
163 b1 = *(pIn1 + 1U);
164 d1 = *(pIn2 + 1U);
166 sumReal1 += a1 * c1;
167 sumImag1 += b1 * c1;
169 pIn1 += 2U;
170 pIn2 += 2 * numColsB;
172 sumReal2 -= b1 * d1;
173 sumImag2 += a1 * d1;
175 a0 = *pIn1;
176 c0 = *pIn2;
178 b0 = *(pIn1 + 1U);
179 d0 = *(pIn2 + 1U);
181 sumReal1 += a0 * c0;
182 sumImag1 += b0 * c0;
184 pIn1 += 2U;
185 pIn2 += 2 * numColsB;
187 sumReal2 -= b0 * d0;
188 sumImag2 += a0 * d0;
190 /* c(m,n) = a(1,1)*b(1,1) + a(1,2) * b(2,1) + .... + a(m,p)*b(p,n) */
192 a1 = *pIn1;
193 c1 = *pIn2;
195 b1 = *(pIn1 + 1U);
196 d1 = *(pIn2 + 1U);
198 sumReal1 += a1 * c1;
199 sumImag1 += b1 * c1;
201 pIn1 += 2U;
202 pIn2 += 2 * numColsB;
204 sumReal2 -= b1 * d1;
205 sumImag2 += a1 * d1;
207 /* Decrement the loop count */
208 colCnt--;
211 /* If the columns of pSrcA is not a multiple of 4, compute any remaining MACs here.
212 ** No loop unrolling is used. */
213 colCnt = numColsA % 0x4U;
215 while (colCnt > 0U)
217 /* c(m,n) = a(1,1)*b(1,1) + a(1,2) * b(2,1) + .... + a(m,p)*b(p,n) */
218 a1 = *pIn1;
219 c1 = *pIn2;
221 b1 = *(pIn1 + 1U);
222 d1 = *(pIn2 + 1U);
224 sumReal1 += a1 * c1;
225 sumImag1 += b1 * c1;
227 pIn1 += 2U;
228 pIn2 += 2 * numColsB;
230 sumReal2 -= b1 * d1;
231 sumImag2 += a1 * d1;
233 /* Decrement the loop counter */
234 colCnt--;
237 sumReal1 += sumReal2;
238 sumImag1 += sumImag2;
240 /* Store the result in the destination buffer */
241 *px++ = sumReal1;
242 *px++ = sumImag1;
244 /* Update the pointer pIn2 to point to the starting address of the next column */
245 j++;
246 pIn2 = pSrcB->pData + 2U * j;
248 /* Decrement the column loop counter */
249 col--;
251 } while (col > 0U);
253 /* Update the pointer pInA to point to the starting address of the next row */
254 i = i + numColsB;
255 pInA = pInA + 2 * numColsA;
257 /* Decrement the row loop counter */
258 row--;
260 } while (row > 0U);
262 /* Set status as ARM_MATH_SUCCESS */
263 status = ARM_MATH_SUCCESS;
266 /* Return to application */
267 return (status);
271 * @} end of MatrixMult group