before merging master
[inav.git] / lib / main / CMSIS / DSP / Source / BasicMathFunctions / arm_mult_q15.c
blob8e20963b34c5af633690575b4c2706791b81153e
1 /* ----------------------------------------------------------------------
2 * Project: CMSIS DSP Library
3 * Title: arm_mult_q15.c
4 * Description: Q15 vector 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 groupMath
35 /**
36 * @addtogroup BasicMult
37 * @{
41 /**
42 * @brief Q15 vector multiplication
43 * @param[in] *pSrcA points to the first input vector
44 * @param[in] *pSrcB points to the second input vector
45 * @param[out] *pDst points to the output vector
46 * @param[in] blockSize number of samples in each vector
47 * @return none.
49 * <b>Scaling and Overflow Behavior:</b>
50 * \par
51 * The function uses saturating arithmetic.
52 * Results outside of the allowable Q15 range [0x8000 0x7FFF] will be saturated.
55 void arm_mult_q15(
56 q15_t * pSrcA,
57 q15_t * pSrcB,
58 q15_t * pDst,
59 uint32_t blockSize)
61 uint32_t blkCnt; /* loop counters */
63 #if defined (ARM_MATH_DSP)
65 /* Run the below code for Cortex-M4 and Cortex-M3 */
66 q31_t inA1, inA2, inB1, inB2; /* temporary input variables */
67 q15_t out1, out2, out3, out4; /* temporary output variables */
68 q31_t mul1, mul2, mul3, mul4; /* temporary variables */
70 /* loop Unrolling */
71 blkCnt = blockSize >> 2U;
73 /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
74 ** a second loop below computes the remaining 1 to 3 samples. */
75 while (blkCnt > 0U)
77 /* read two samples at a time from sourceA */
78 inA1 = *__SIMD32(pSrcA)++;
79 /* read two samples at a time from sourceB */
80 inB1 = *__SIMD32(pSrcB)++;
81 /* read two samples at a time from sourceA */
82 inA2 = *__SIMD32(pSrcA)++;
83 /* read two samples at a time from sourceB */
84 inB2 = *__SIMD32(pSrcB)++;
86 /* multiply mul = sourceA * sourceB */
87 mul1 = (q31_t) ((q15_t) (inA1 >> 16) * (q15_t) (inB1 >> 16));
88 mul2 = (q31_t) ((q15_t) inA1 * (q15_t) inB1);
89 mul3 = (q31_t) ((q15_t) (inA2 >> 16) * (q15_t) (inB2 >> 16));
90 mul4 = (q31_t) ((q15_t) inA2 * (q15_t) inB2);
92 /* saturate result to 16 bit */
93 out1 = (q15_t) __SSAT(mul1 >> 15, 16);
94 out2 = (q15_t) __SSAT(mul2 >> 15, 16);
95 out3 = (q15_t) __SSAT(mul3 >> 15, 16);
96 out4 = (q15_t) __SSAT(mul4 >> 15, 16);
98 /* store the result */
99 #ifndef ARM_MATH_BIG_ENDIAN
101 *__SIMD32(pDst)++ = __PKHBT(out2, out1, 16);
102 *__SIMD32(pDst)++ = __PKHBT(out4, out3, 16);
104 #else
106 *__SIMD32(pDst)++ = __PKHBT(out2, out1, 16);
107 *__SIMD32(pDst)++ = __PKHBT(out4, out3, 16);
109 #endif /* #ifndef ARM_MATH_BIG_ENDIAN */
111 /* Decrement the blockSize loop counter */
112 blkCnt--;
115 /* If the blockSize is not a multiple of 4, compute any remaining output samples here.
116 ** No loop unrolling is used. */
117 blkCnt = blockSize % 0x4U;
119 #else
121 /* Run the below code for Cortex-M0 */
123 /* Initialize blkCnt with number of samples */
124 blkCnt = blockSize;
126 #endif /* #if defined (ARM_MATH_DSP) */
129 while (blkCnt > 0U)
131 /* C = A * B */
132 /* Multiply the inputs and store the result in the destination buffer */
133 *pDst++ = (q15_t) __SSAT((((q31_t) (*pSrcA++) * (*pSrcB++)) >> 15), 16);
135 /* Decrement the blockSize loop counter */
136 blkCnt--;
141 * @} end of BasicMult group