1 /* ----------------------------------------------------------------------
2 * Copyright (C) 2010-2014 ARM Limited. All rights reserved.
4 * $Date: 19. March 2015
7 * Project: CMSIS DSP Library
8 * Title: arm_cmplx_mat_mult_q15.c
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
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
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 * -------------------------------------------------------------------- */
43 * @ingroup groupMatrix
47 * @addtogroup CmplxMatrixMult
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
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
69 * <b>Scaling and Overflow Behavior:</b>
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
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
,
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 */
111 q31_t in
; /* Temporary variable to hold the input value */
113 q31_t pSourceA
, pSourceB
;
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
;
127 /* Matrix transpose */
130 /* Apply loop unrolling and exchange the columns with row elements */
133 /* The pointer px is set to starting address of the column being processed */
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. */
140 #ifdef UNALIGNED_SUPPORT_DISABLE
141 /* Read two elements from the row */
147 /* Update the pointer px to point to the next row of the transposed matrix */
150 /* Read two elements from the row */
156 /* Update the pointer px to point to the next row of the transposed matrix */
159 /* Read two elements from the row */
165 /* Update the pointer px to point to the next row of the transposed matrix */
168 /* Read two elements from the row */
174 /* Update the pointer px to point to the next row of the transposed matrix */
177 /* Decrement the column loop counter */
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
;
187 /* Read two elements from the row */
194 /* Read two elements from the row */
195 in
= *__SIMD32(pInB
)++;
199 /* Update the pointer px to point to the next row of the transposed matrix */
203 /* Read two elements from the row */
204 in
= *__SIMD32(pInB
)++;
208 /* Update the pointer px to point to the next row of the transposed matrix */
211 /* Read two elements from the row */
212 in
= *__SIMD32(pInB
)++;
216 /* Update the pointer px to point to the next row of the transposed matrix */
219 /* Read two elements from the row */
220 in
= *__SIMD32(pInB
)++;
224 /* Update the pointer px to point to the next row of the transposed matrix */
227 /* Decrement the column loop counter */
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
;
237 /* Read two elements from the row */
238 in
= *__SIMD32(pInB
)++;
243 /* Update the pointer px to point to the next row of the transposed matrix */
246 /* Decrement the column loop counter */
252 /* Decrement the row loop counter */
257 /* Reset the variables for the usage in the following multiplication process */
262 /* The following loop performs the dot-product of each row in pSrcA with each column in pSrcB */
266 /* For every row wise process, the column loop counter is to be initiated */
269 /* For every row wise process, the pIn2 pointer is set
270 ** to the starting address of the transposed pSrcB data */
276 /* Set the variable sum, that acts as accumulator, to zero */
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 */
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 */
297 /* read real and imag values from pSrcB buffer */
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 */
310 /* read next real and imag values from pSrcB buffer */
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
;
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
);
333 prod1
= __SMUSD(pSourceA
, pSourceB
);
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
);
347 prod1
= __SMUSD(pSourceA
, pSourceB
);
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 */
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 */
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
;
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
);
387 prod1
= __SMUSD(pSourceA
, pSourceB
);
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 */
409 /* Decrement the row loop counter */
414 /* set status as ARM_MATH_SUCCESS */
415 status
= ARM_MATH_SUCCESS
;
418 /* Return to application */
423 * @} end of MatrixMult group