1 /* ----------------------------------------------------------------------
2  * Project:      CMSIS DSP Library
3  * Title:        arm_mult_q15.c
4  * Description:  Q15 vector multiplication
5  *
6  * $Date:        23 April 2021
7  * $Revision:    V1.9.0
8  *
9  * Target Processor: Cortex-M and Cortex-A cores
10  * -------------------------------------------------------------------- */
11 /*
12  * Copyright (C) 2010-2021 ARM Limited or its affiliates. All rights reserved.
13  *
14  * SPDX-License-Identifier: Apache-2.0
15  *
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
19  *
20  * www.apache.org/licenses/LICENSE-2.0
21  *
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.
27  */
28 
29 #include "dsp/basic_math_functions.h"
30 
31 /**
32   @ingroup groupMath
33  */
34 
35 /**
36   @addtogroup BasicMult
37   @{
38  */
39 
40 /**
41   @brief         Q15 vector multiplication
42   @param[in]     pSrcA      points to first input vector
43   @param[in]     pSrcB      points to second input vector
44   @param[out]    pDst       points to output vector
45   @param[in]     blockSize  number of samples in each vector
46 
47   @par           Scaling and Overflow Behavior
48                    The function uses saturating arithmetic.
49                    Results outside of the allowable Q15 range [0x8000 0x7FFF] are saturated.
50  */
51 #if defined(ARM_MATH_MVEI) && !defined(ARM_MATH_AUTOVECTORIZE)
52 
53 #include "arm_helium_utils.h"
54 
arm_mult_q15(const q15_t * pSrcA,const q15_t * pSrcB,q15_t * pDst,uint32_t blockSize)55 ARM_DSP_ATTRIBUTE void arm_mult_q15(
56     const q15_t * pSrcA,
57     const q15_t * pSrcB,
58     q15_t * pDst,
59     uint32_t blockSize)
60 {
61     uint32_t  blkCnt;           /* loop counters */
62     q15x8_t vecA, vecB;
63 
64     /* Compute 8 outputs at a time */
65     blkCnt = blockSize >> 3;
66     while (blkCnt > 0U)
67     {
68         /*
69          * C = A * B
70          * Multiply the inputs and then store the results in the destination buffer.
71          */
72         vecA = vld1q(pSrcA);
73         vecB = vld1q(pSrcB);
74         vst1q(pDst, vqdmulhq(vecA, vecB));
75         /*
76          * Decrement the blockSize loop counter
77          */
78         blkCnt--;
79         /*
80          * advance vector source and destination pointers
81          */
82         pSrcA  += 8;
83         pSrcB  += 8;
84         pDst   += 8;
85     }
86     /*
87      * tail
88      */
89     blkCnt = blockSize & 7;
90     if (blkCnt > 0U)
91     {
92         mve_pred16_t p0 = vctp16q(blkCnt);
93         vecA = vld1q(pSrcA);
94         vecB = vld1q(pSrcB);
95         vstrhq_p(pDst, vqdmulhq(vecA, vecB), p0);
96     }
97 }
98 
99 #else
arm_mult_q15(const q15_t * pSrcA,const q15_t * pSrcB,q15_t * pDst,uint32_t blockSize)100 ARM_DSP_ATTRIBUTE void arm_mult_q15(
101   const q15_t * pSrcA,
102   const q15_t * pSrcB,
103         q15_t * pDst,
104         uint32_t blockSize)
105 {
106         uint32_t blkCnt;                               /* Loop counter */
107 
108 #if defined (ARM_MATH_LOOPUNROLL)
109 
110 #if defined (ARM_MATH_DSP)
111   q31_t inA1, inA2, inB1, inB2;                  /* Temporary input variables */
112   q15_t out1, out2, out3, out4;                  /* Temporary output variables */
113   q31_t mul1, mul2, mul3, mul4;                  /* Temporary variables */
114 #endif
115 
116   /* Loop unrolling: Compute 4 outputs at a time */
117   blkCnt = blockSize >> 2U;
118 
119   while (blkCnt > 0U)
120   {
121     /* C = A * B */
122 
123 #if defined (ARM_MATH_DSP)
124     /* read 2 samples at a time from sourceA */
125     inA1 = read_q15x2_ia (&pSrcA);
126     /* read 2 samples at a time from sourceB */
127     inB1 = read_q15x2_ia (&pSrcB);
128     /* read 2 samples at a time from sourceA */
129     inA2 = read_q15x2_ia (&pSrcA);
130     /* read 2 samples at a time from sourceB */
131     inB2 = read_q15x2_ia (&pSrcB);
132 
133     /* multiply mul = sourceA * sourceB */
134     mul1 = (q31_t) ((q15_t) (inA1 >> 16) * (q15_t) (inB1 >> 16));
135     mul2 = (q31_t) ((q15_t) (inA1      ) * (q15_t) (inB1      ));
136     mul3 = (q31_t) ((q15_t) (inA2 >> 16) * (q15_t) (inB2 >> 16));
137     mul4 = (q31_t) ((q15_t) (inA2      ) * (q15_t) (inB2      ));
138 
139     /* saturate result to 16 bit */
140     out1 = (q15_t) __SSAT(mul1 >> 15, 16);
141     out2 = (q15_t) __SSAT(mul2 >> 15, 16);
142     out3 = (q15_t) __SSAT(mul3 >> 15, 16);
143     out4 = (q15_t) __SSAT(mul4 >> 15, 16);
144 
145     /* store result to destination */
146 #ifndef ARM_MATH_BIG_ENDIAN
147     write_q15x2_ia (&pDst, __PKHBT(out2, out1, 16));
148     write_q15x2_ia (&pDst, __PKHBT(out4, out3, 16));
149 #else
150     write_q15x2_ia (&pDst, __PKHBT(out1, out2, 16));
151     write_q15x2_ia (&pDst, __PKHBT(out3, out4, 16));
152 #endif /* #ifndef ARM_MATH_BIG_ENDIAN */
153 
154 #else
155     *pDst++ = (q15_t) __SSAT((((q31_t) (*pSrcA++) * (*pSrcB++)) >> 15), 16);
156     *pDst++ = (q15_t) __SSAT((((q31_t) (*pSrcA++) * (*pSrcB++)) >> 15), 16);
157     *pDst++ = (q15_t) __SSAT((((q31_t) (*pSrcA++) * (*pSrcB++)) >> 15), 16);
158     *pDst++ = (q15_t) __SSAT((((q31_t) (*pSrcA++) * (*pSrcB++)) >> 15), 16);
159 #endif
160 
161     /* Decrement loop counter */
162     blkCnt--;
163   }
164 
165   /* Loop unrolling: Compute remaining outputs */
166   blkCnt = blockSize % 0x4U;
167 
168 #else
169 
170   /* Initialize blkCnt with number of samples */
171   blkCnt = blockSize;
172 
173 #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
174 
175   while (blkCnt > 0U)
176   {
177     /* C = A * B */
178 
179     /* Multiply inputs and store result in destination buffer. */
180     *pDst++ = (q15_t) __SSAT((((q31_t) (*pSrcA++) * (*pSrcB++)) >> 15), 16);
181 
182     /* Decrement loop counter */
183     blkCnt--;
184   }
185 
186 }
187 #endif /* defined(ARM_MATH_MVEI) */
188 
189 /**
190   @} end of BasicMult group
191  */
192