]>
git.gir.st - tmk_keyboard.git/blob - tmk_core/tool/mbed/mbed-sdk/libraries/dsp/cmsis_dsp/MatrixFunctions/arm_mat_mult_q15.c
1 /* ----------------------------------------------------------------------
2 * Copyright (C) 2010-2013 ARM Limited. All rights reserved.
4 * $Date: 17. January 2013
7 * Project: CMSIS DSP Library
8 * Title: arm_mat_mult_q15.c
10 * Description: Q15 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 * -------------------------------------------------------------------- */
44 * @ingroup groupMatrix
48 * @addtogroup MatrixMult
54 * @brief Q15 matrix multiplication
55 * @param[in] *pSrcA points to the first input matrix structure
56 * @param[in] *pSrcB points to the second input matrix structure
57 * @param[out] *pDst points to output matrix structure
58 * @param[in] *pState points to the array for storing intermediate results
59 * @return The function returns either
60 * <code>ARM_MATH_SIZE_MISMATCH</code> or <code>ARM_MATH_SUCCESS</code> based on the outcome of size checking.
63 * <b>Scaling and Overflow Behavior:</b>
66 * The function is implemented using a 64-bit internal accumulator. The inputs to the
67 * multiplications are in 1.15 format and multiplications yield a 2.30 result.
68 * The 2.30 intermediate
69 * results are accumulated in a 64-bit accumulator in 34.30 format. This approach
70 * provides 33 guard bits and there is no risk of overflow. The 34.30 result is then
71 * truncated to 34.15 format by discarding the low 15 bits and then saturated to
75 * Refer to <code>arm_mat_mult_fast_q15()</code> for a faster but less precise version of this function for Cortex-M3 and Cortex-M4.
79 arm_status
arm_mat_mult_q15(
80 const arm_matrix_instance_q15
* pSrcA
,
81 const arm_matrix_instance_q15
* pSrcB
,
82 arm_matrix_instance_q15
* pDst
,
83 q15_t
* pState CMSIS_UNUSED
)
85 q63_t sum
; /* accumulator */
87 #ifndef ARM_MATH_CM0_FAMILY
89 /* Run the below code for Cortex-M4 and Cortex-M3 */
91 q15_t
*pSrcBT
= pState
; /* input data matrix pointer for transpose */
92 q15_t
*pInA
= pSrcA
->pData
; /* input data matrix pointer A of Q15 type */
93 q15_t
*pInB
= pSrcB
->pData
; /* input data matrix pointer B of Q15 type */
94 q15_t
*px
; /* Temporary output data matrix pointer */
95 uint16_t numRowsA
= pSrcA
->numRows
; /* number of rows of input matrix A */
96 uint16_t numColsB
= pSrcB
->numCols
; /* number of columns of input matrix B */
97 uint16_t numColsA
= pSrcA
->numCols
; /* number of columns of input matrix A */
98 uint16_t numRowsB
= pSrcB
->numRows
; /* number of rows of input matrix A */
99 uint16_t col
, i
= 0u, row
= numRowsB
, colCnt
; /* loop counters */
100 arm_status status
; /* status of matrix multiplication */
102 #ifndef UNALIGNED_SUPPORT_DISABLE
104 q31_t in
; /* Temporary variable to hold the input value */
105 q31_t pSourceA1
, pSourceB1
, pSourceA2
, pSourceB2
;
109 q15_t in
; /* Temporary variable to hold the input value */
110 q15_t inA1
, inB1
, inA2
, inB2
;
112 #endif /* #ifndef UNALIGNED_SUPPORT_DISABLE */
114 #ifdef ARM_MATH_MATRIX_CHECK
115 /* Check for matrix mismatch condition */
116 if((pSrcA
->numCols
!= pSrcB
->numRows
) ||
117 (pSrcA
->numRows
!= pDst
->numRows
) || (pSrcB
->numCols
!= pDst
->numCols
))
119 /* Set status as ARM_MATH_SIZE_MISMATCH */
120 status
= ARM_MATH_SIZE_MISMATCH
;
123 #endif /* #ifdef ARM_MATH_MATRIX_CHECK */
125 /* Matrix transpose */
128 /* Apply loop unrolling and exchange the columns with row elements */
131 /* The pointer px is set to starting address of the column being processed */
134 /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
135 ** a second loop below computes the remaining 1 to 3 samples. */
138 #ifndef UNALIGNED_SUPPORT_DISABLE
140 /* Read two elements from the row */
141 in
= *__SIMD32(pInB
)++;
143 /* Unpack and store one element in the destination */
144 #ifndef ARM_MATH_BIG_ENDIAN
150 *px
= (q15_t
) ((in
& (q31_t
) 0xffff0000) >> 16);
152 #endif /* #ifndef ARM_MATH_BIG_ENDIAN */
154 /* Update the pointer px to point to the next row of the transposed matrix */
157 /* Unpack and store the second element in the destination */
158 #ifndef ARM_MATH_BIG_ENDIAN
160 *px
= (q15_t
) ((in
& (q31_t
) 0xffff0000) >> 16);
166 #endif /* #ifndef ARM_MATH_BIG_ENDIAN */
168 /* Update the pointer px to point to the next row of the transposed matrix */
171 /* Read two elements from the row */
172 in
= *__SIMD32(pInB
)++;
174 /* Unpack and store one element in the destination */
175 #ifndef ARM_MATH_BIG_ENDIAN
181 *px
= (q15_t
) ((in
& (q31_t
) 0xffff0000) >> 16);
183 #endif /* #ifndef ARM_MATH_BIG_ENDIAN */
185 /* Update the pointer px to point to the next row of the transposed matrix */
188 /* Unpack and store the second element in the destination */
190 #ifndef ARM_MATH_BIG_ENDIAN
192 *px
= (q15_t
) ((in
& (q31_t
) 0xffff0000) >> 16);
198 #endif /* #ifndef ARM_MATH_BIG_ENDIAN */
200 /* Update the pointer px to point to the next row of the transposed matrix */
205 /* Read one element from the row */
208 /* Store one element in the destination */
211 /* Update the pointer px to point to the next row of the transposed matrix */
214 /* Read one element from the row */
217 /* Store one element in the destination */
220 /* Update the pointer px to point to the next row of the transposed matrix */
223 /* Read one element from the row */
226 /* Store one element in the destination */
229 /* Update the pointer px to point to the next row of the transposed matrix */
232 /* Read one element from the row */
235 /* Store one element in the destination */
238 /* Update the pointer px to point to the next row of the transposed matrix */
241 #endif /* #ifndef UNALIGNED_SUPPORT_DISABLE */
243 /* Decrement the column loop counter */
247 /* If the columns of pSrcB is not a multiple of 4, compute any remaining output samples here.
248 ** No loop unrolling is used. */
249 col
= numColsB
% 0x4u
;
253 /* Read and store the input element in the destination */
256 /* Update the pointer px to point to the next row of the transposed matrix */
259 /* Decrement the column loop counter */
265 /* Decrement the row loop counter */
270 /* Reset the variables for the usage in the following multiplication process */
275 /* The following loop performs the dot-product of each row in pSrcA with each column in pSrcB */
279 /* For every row wise process, the column loop counter is to be initiated */
282 /* For every row wise process, the pIn2 pointer is set
283 ** to the starting address of the transposed pSrcB data */
289 /* Set the variable sum, that acts as accumulator, to zero */
292 /* Apply loop unrolling and compute 2 MACs simultaneously. */
293 colCnt
= numColsA
>> 2;
295 /* Initiate the pointer pIn1 to point to the starting address of the column being processed */
296 pInA
= pSrcA
->pData
+ i
;
299 /* matrix multiplication */
302 /* c(m,n) = a(1,1)*b(1,1) + a(1,2) * b(2,1) + .... + a(m,p)*b(p,n) */
303 #ifndef UNALIGNED_SUPPORT_DISABLE
305 /* read real and imag values from pSrcA and pSrcB buffer */
306 pSourceA1
= *__SIMD32(pInA
)++;
307 pSourceB1
= *__SIMD32(pInB
)++;
309 pSourceA2
= *__SIMD32(pInA
)++;
310 pSourceB2
= *__SIMD32(pInB
)++;
312 /* Multiply and Accumlates */
313 sum
= __SMLALD(pSourceA1
, pSourceB1
, sum
);
314 sum
= __SMLALD(pSourceA2
, pSourceB2
, sum
);
317 /* read real and imag values from pSrcA and pSrcB buffer */
321 /* Multiply and Accumlates */
327 /* Multiply and Accumlates */
332 /* Multiply and Accumlates */
336 #endif /* #ifndef UNALIGNED_SUPPORT_DISABLE */
338 /* Decrement the loop counter */
342 /* process remaining column samples */
343 colCnt
= numColsA
& 3u;
347 /* c(m,n) = a(1,1)*b(1,1) + a(1,2) * b(2,1) + .... + a(m,p)*b(p,n) */
348 sum
+= *pInA
++ * *pInB
++;
350 /* Decrement the loop counter */
354 /* Saturate and store the result in the destination buffer */
355 *px
= (q15_t
) (__SSAT((sum
>> 15), 16));
358 /* Decrement the column loop counter */
365 /* Decrement the row loop counter */
372 /* Run the below code for Cortex-M0 */
374 q15_t
*pIn1
= pSrcA
->pData
; /* input data matrix pointer A */
375 q15_t
*pIn2
= pSrcB
->pData
; /* input data matrix pointer B */
376 q15_t
*pInA
= pSrcA
->pData
; /* input data matrix pointer A of Q15 type */
377 q15_t
*pInB
= pSrcB
->pData
; /* input data matrix pointer B of Q15 type */
378 q15_t
*pOut
= pDst
->pData
; /* output data matrix pointer */
379 q15_t
*px
; /* Temporary output data matrix pointer */
380 uint16_t numColsB
= pSrcB
->numCols
; /* number of columns of input matrix B */
381 uint16_t numColsA
= pSrcA
->numCols
; /* number of columns of input matrix A */
382 uint16_t numRowsA
= pSrcA
->numRows
; /* number of rows of input matrix A */
383 uint16_t col
, i
= 0u, row
= numRowsA
, colCnt
; /* loop counters */
384 arm_status status
; /* status of matrix multiplication */
386 #ifdef ARM_MATH_MATRIX_CHECK
388 /* Check for matrix mismatch condition */
389 if((pSrcA
->numCols
!= pSrcB
->numRows
) ||
390 (pSrcA
->numRows
!= pDst
->numRows
) || (pSrcB
->numCols
!= pDst
->numCols
))
392 /* Set status as ARM_MATH_SIZE_MISMATCH */
393 status
= ARM_MATH_SIZE_MISMATCH
;
396 #endif /* #ifdef ARM_MATH_MATRIX_CHECK */
399 /* The following loop performs the dot-product of each row in pSrcA with each column in pSrcB */
403 /* Output pointer is set to starting address of the row being processed */
406 /* For every row wise process, the column loop counter is to be initiated */
409 /* For every row wise process, the pIn2 pointer is set
410 ** to the starting address of the pSrcB data */
416 /* Set the variable sum, that acts as accumulator, to zero */
419 /* Initiate the pointer pIn1 to point to the starting address of pSrcA */
422 /* Matrix A columns number of MAC operations are to be performed */
425 /* matrix multiplication */
428 /* c(m,n) = a(1,1)*b(1,1) + a(1,2) * b(2,1) + .... + a(m,p)*b(p,n) */
429 /* Perform the multiply-accumulates */
430 sum
+= (q31_t
) * pIn1
++ * *pIn2
;
433 /* Decrement the loop counter */
437 /* Convert the result from 34.30 to 1.15 format and store the saturated value in destination buffer */
438 /* Saturate and store the result in the destination buffer */
439 *px
++ = (q15_t
) __SSAT((sum
>> 15), 16);
441 /* Decrement the column loop counter */
444 /* Update the pointer pIn2 to point to the starting address of the next column */
445 pIn2
= pInB
+ (numColsB
- col
);
449 /* Update the pointer pSrcA to point to the starting address of the next row */
451 pInA
= pInA
+ numColsA
;
453 /* Decrement the row loop counter */
458 #endif /* #ifndef ARM_MATH_CM0_FAMILY */
459 /* set status as ARM_MATH_SUCCESS */
460 status
= ARM_MATH_SUCCESS
;
463 /* Return to application */
468 * @} end of MatrixMult group