F1 and F3 HAL / LL libraries
[betaflight.git] / lib / main / STM32F3 / Drivers / CMSIS / DSP_Lib / Source / MatrixFunctions / arm_mat_cmplx_mult_q15.c
blob8e3fc06c2b84d5139774d698254220c6664b698b
1 /* ----------------------------------------------------------------------
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_cmplx_mat_mult_q15.c
9 *
10 * Description: Q15 complex matrix multiplication.
12 * Target Processor: Cortex-M4/Cortex-M3/Cortex-M0
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.
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 #include "arm_math.h"
42 /**
43 * @ingroup groupMatrix
46 /**
47 * @addtogroup CmplxMatrixMult
48 * @{
52 /**
53 * @brief Q15 Complex matrix multiplication
54 * @param[in] *pSrcA points to the first input complex matrix structure
55 * @param[in] *pSrcB points to the second input complex matrix structure
56 * @param[out] *pDst points to output complex matrix structure
57 * @param[in] *pScratch points to the array for storing intermediate results
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.
61 * \par Conditions for optimum performance
62 * Input, output and state buffers should be aligned by 32-bit
64 * \par Restrictions
65 * If the silicon does not support unaligned memory access enable the macro UNALIGNED_SUPPORT_DISABLE
66 * In this case input, output, scratch buffers should be aligned by 32-bit
68 * @details
69 * <b>Scaling and Overflow Behavior:</b>
71 * \par
72 * The function is implemented using a 64-bit internal accumulator. The inputs to the
73 * multiplications are in 1.15 format and multiplications yield a 2.30 result.
74 * The 2.30 intermediate
75 * results are accumulated in a 64-bit accumulator in 34.30 format. This approach
76 * provides 33 guard bits and there is no risk of overflow. The 34.30 result is then
77 * truncated to 34.15 format by discarding the low 15 bits and then saturated to
78 * 1.15 format.
80 * \par
81 * Refer to <code>arm_mat_mult_fast_q15()</code> for a faster but less precise version of this function.
88 arm_status arm_mat_cmplx_mult_q15(
89 const arm_matrix_instance_q15 * pSrcA,
90 const arm_matrix_instance_q15 * pSrcB,
91 arm_matrix_instance_q15 * pDst,
92 q15_t * pScratch)
94 /* accumulator */
95 q15_t *pSrcBT = pScratch; /* input data matrix pointer for transpose */
96 q15_t *pInA = pSrcA->pData; /* input data matrix pointer A of Q15 type */
97 q15_t *pInB = pSrcB->pData; /* input data matrix pointer B of Q15 type */
98 q15_t *px; /* Temporary output data matrix pointer */
99 uint16_t numRowsA = pSrcA->numRows; /* number of rows of input matrix A */
100 uint16_t numColsB = pSrcB->numCols; /* number of columns of input matrix B */
101 uint16_t numColsA = pSrcA->numCols; /* number of columns of input matrix A */
102 uint16_t numRowsB = pSrcB->numRows; /* number of rows of input matrix A */
103 uint16_t col, i = 0u, row = numRowsB, colCnt; /* loop counters */
104 arm_status status; /* status of matrix multiplication */
105 q63_t sumReal, sumImag;
107 #ifdef UNALIGNED_SUPPORT_DISABLE
108 q15_t in; /* Temporary variable to hold the input value */
109 q15_t a, b, c, d;
110 #else
111 q31_t in; /* Temporary variable to hold the input value */
112 q31_t prod1, prod2;
113 q31_t pSourceA, pSourceB;
114 #endif
116 #ifdef ARM_MATH_MATRIX_CHECK
117 /* Check for matrix mismatch condition */
118 if((pSrcA->numCols != pSrcB->numRows) ||
119 (pSrcA->numRows != pDst->numRows) || (pSrcB->numCols != pDst->numCols))
121 /* Set status as ARM_MATH_SIZE_MISMATCH */
122 status = ARM_MATH_SIZE_MISMATCH;
124 else
125 #endif
127 /* Matrix transpose */
130 /* Apply loop unrolling and exchange the columns with row elements */
131 col = numColsB >> 2;
133 /* The pointer px is set to starting address of the column being processed */
134 px = pSrcBT + i;
136 /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
137 ** a second loop below computes the remaining 1 to 3 samples. */
138 while(col > 0u)
140 #ifdef UNALIGNED_SUPPORT_DISABLE
141 /* Read two elements from the row */
142 in = *pInB++;
143 *px = in;
144 in = *pInB++;
145 px[1] = in;
147 /* Update the pointer px to point to the next row of the transposed matrix */
148 px += numRowsB * 2;
150 /* Read two elements from the row */
151 in = *pInB++;
152 *px = in;
153 in = *pInB++;
154 px[1] = in;
156 /* Update the pointer px to point to the next row of the transposed matrix */
157 px += numRowsB * 2;
159 /* Read two elements from the row */
160 in = *pInB++;
161 *px = in;
162 in = *pInB++;
163 px[1] = in;
165 /* Update the pointer px to point to the next row of the transposed matrix */
166 px += numRowsB * 2;
168 /* Read two elements from the row */
169 in = *pInB++;
170 *px = in;
171 in = *pInB++;
172 px[1] = in;
174 /* Update the pointer px to point to the next row of the transposed matrix */
175 px += numRowsB * 2;
177 /* Decrement the column loop counter */
178 col--;
181 /* If the columns of pSrcB is not a multiple of 4, compute any remaining output samples here.
182 ** No loop unrolling is used. */
183 col = numColsB % 0x4u;
185 while(col > 0u)
187 /* Read two elements from the row */
188 in = *pInB++;
189 *px = in;
190 in = *pInB++;
191 px[1] = in;
192 #else
194 /* Read two elements from the row */
195 in = *__SIMD32(pInB)++;
197 *__SIMD32(px) = in;
199 /* Update the pointer px to point to the next row of the transposed matrix */
200 px += numRowsB * 2;
203 /* Read two elements from the row */
204 in = *__SIMD32(pInB)++;
206 *__SIMD32(px) = in;
208 /* Update the pointer px to point to the next row of the transposed matrix */
209 px += numRowsB * 2;
211 /* Read two elements from the row */
212 in = *__SIMD32(pInB)++;
214 *__SIMD32(px) = in;
216 /* Update the pointer px to point to the next row of the transposed matrix */
217 px += numRowsB * 2;
219 /* Read two elements from the row */
220 in = *__SIMD32(pInB)++;
222 *__SIMD32(px) = in;
224 /* Update the pointer px to point to the next row of the transposed matrix */
225 px += numRowsB * 2;
227 /* Decrement the column loop counter */
228 col--;
231 /* If the columns of pSrcB is not a multiple of 4, compute any remaining output samples here.
232 ** No loop unrolling is used. */
233 col = numColsB % 0x4u;
235 while(col > 0u)
237 /* Read two elements from the row */
238 in = *__SIMD32(pInB)++;
240 *__SIMD32(px) = in;
241 #endif
243 /* Update the pointer px to point to the next row of the transposed matrix */
244 px += numRowsB * 2;
246 /* Decrement the column loop counter */
247 col--;
250 i = i + 2u;
252 /* Decrement the row loop counter */
253 row--;
255 } while(row > 0u);
257 /* Reset the variables for the usage in the following multiplication process */
258 row = numRowsA;
259 i = 0u;
260 px = pDst->pData;
262 /* The following loop performs the dot-product of each row in pSrcA with each column in pSrcB */
263 /* row loop */
266 /* For every row wise process, the column loop counter is to be initiated */
267 col = numColsB;
269 /* For every row wise process, the pIn2 pointer is set
270 ** to the starting address of the transposed pSrcB data */
271 pInB = pSrcBT;
273 /* column loop */
276 /* Set the variable sum, that acts as accumulator, to zero */
277 sumReal = 0;
278 sumImag = 0;
280 /* Apply loop unrolling and compute 2 MACs simultaneously. */
281 colCnt = numColsA >> 1;
283 /* Initiate the pointer pIn1 to point to the starting address of the column being processed */
284 pInA = pSrcA->pData + i * 2;
287 /* matrix multiplication */
288 while(colCnt > 0u)
290 /* c(m,n) = a(1,1)*b(1,1) + a(1,2) * b(2,1) + .... + a(m,p)*b(p,n) */
292 #ifdef UNALIGNED_SUPPORT_DISABLE
294 /* read real and imag values from pSrcA buffer */
295 a = *pInA;
296 b = *(pInA + 1u);
297 /* read real and imag values from pSrcB buffer */
298 c = *pInB;
299 d = *(pInB + 1u);
301 /* Multiply and Accumlates */
302 sumReal += (q31_t) a *c;
303 sumImag += (q31_t) a *d;
304 sumReal -= (q31_t) b *d;
305 sumImag += (q31_t) b *c;
307 /* read next real and imag values from pSrcA buffer */
308 a = *(pInA + 2u);
309 b = *(pInA + 3u);
310 /* read next real and imag values from pSrcB buffer */
311 c = *(pInB + 2u);
312 d = *(pInB + 3u);
314 /* update pointer */
315 pInA += 4u;
317 /* Multiply and Accumlates */
318 sumReal += (q31_t) a *c;
319 sumImag += (q31_t) a *d;
320 sumReal -= (q31_t) b *d;
321 sumImag += (q31_t) b *c;
322 /* update pointer */
323 pInB += 4u;
324 #else
325 /* read real and imag values from pSrcA and pSrcB buffer */
326 pSourceA = *__SIMD32(pInA)++;
327 pSourceB = *__SIMD32(pInB)++;
329 /* Multiply and Accumlates */
330 #ifdef ARM_MATH_BIG_ENDIAN
331 prod1 = -__SMUSD(pSourceA, pSourceB);
332 #else
333 prod1 = __SMUSD(pSourceA, pSourceB);
334 #endif
335 prod2 = __SMUADX(pSourceA, pSourceB);
336 sumReal += (q63_t) prod1;
337 sumImag += (q63_t) prod2;
339 /* read real and imag values from pSrcA and pSrcB buffer */
340 pSourceA = *__SIMD32(pInA)++;
341 pSourceB = *__SIMD32(pInB)++;
343 /* Multiply and Accumlates */
344 #ifdef ARM_MATH_BIG_ENDIAN
345 prod1 = -__SMUSD(pSourceA, pSourceB);
346 #else
347 prod1 = __SMUSD(pSourceA, pSourceB);
348 #endif
349 prod2 = __SMUADX(pSourceA, pSourceB);
350 sumReal += (q63_t) prod1;
351 sumImag += (q63_t) prod2;
353 #endif /* #ifdef UNALIGNED_SUPPORT_DISABLE */
355 /* Decrement the loop counter */
356 colCnt--;
359 /* process odd column samples */
360 if((numColsA & 0x1u) > 0u)
362 /* c(m,n) = a(1,1)*b(1,1) + a(1,2) * b(2,1) + .... + a(m,p)*b(p,n) */
364 #ifdef UNALIGNED_SUPPORT_DISABLE
366 /* read real and imag values from pSrcA and pSrcB buffer */
367 a = *pInA++;
368 b = *pInA++;
369 c = *pInB++;
370 d = *pInB++;
372 /* Multiply and Accumlates */
373 sumReal += (q31_t) a *c;
374 sumImag += (q31_t) a *d;
375 sumReal -= (q31_t) b *d;
376 sumImag += (q31_t) b *c;
378 #else
379 /* read real and imag values from pSrcA and pSrcB buffer */
380 pSourceA = *__SIMD32(pInA)++;
381 pSourceB = *__SIMD32(pInB)++;
383 /* Multiply and Accumlates */
384 #ifdef ARM_MATH_BIG_ENDIAN
385 prod1 = -__SMUSD(pSourceA, pSourceB);
386 #else
387 prod1 = __SMUSD(pSourceA, pSourceB);
388 #endif
389 prod2 = __SMUADX(pSourceA, pSourceB);
390 sumReal += (q63_t) prod1;
391 sumImag += (q63_t) prod2;
393 #endif /* #ifdef UNALIGNED_SUPPORT_DISABLE */
397 /* Saturate and store the result in the destination buffer */
399 *px++ = (q15_t) (__SSAT(sumReal >> 15, 16));
400 *px++ = (q15_t) (__SSAT(sumImag >> 15, 16));
402 /* Decrement the column loop counter */
403 col--;
405 } while(col > 0u);
407 i = i + numColsA;
409 /* Decrement the row loop counter */
410 row--;
412 } while(row > 0u);
414 /* set status as ARM_MATH_SUCCESS */
415 status = ARM_MATH_SUCCESS;
418 /* Return to application */
419 return (status);
422 /**
423 * @} end of MatrixMult group