1 /* ----------------------------------------------------------------------
2  * Project:      CMSIS DSP Library
3  * Title:        arm_mult_q31.c
4  * Description:  Q31 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         Q31 vector multiplication.
42   @param[in]     pSrcA      points to the first input vector.
43   @param[in]     pSrcB      points to the second input vector.
44   @param[out]    pDst       points to the output vector.
45   @param[in]     blockSize  number of samples in each vector.
46   @return        none
47 
48   @par           Scaling and Overflow Behavior
49                    The function uses saturating arithmetic.
50                    Results outside of the allowable Q31 range[0x80000000 0x7FFFFFFF] are saturated.
51  */
52 #if defined(ARM_MATH_MVEI) && !defined(ARM_MATH_AUTOVECTORIZE)
53 
54 #include "arm_helium_utils.h"
55 
arm_mult_q31(const q31_t * pSrcA,const q31_t * pSrcB,q31_t * pDst,uint32_t blockSize)56 void arm_mult_q31(
57     const q31_t * pSrcA,
58     const q31_t * pSrcB,
59     q31_t * pDst,
60     uint32_t blockSize)
61 {
62     uint32_t  blkCnt;           /* loop counters */
63     q31x4_t vecA, vecB;
64 
65     /* Compute 4 outputs at a time */
66     blkCnt = blockSize >> 2;
67     while (blkCnt > 0U)
68     {
69         /*
70          * C = A * B
71          * Multiply the inputs and then store the results in the destination buffer.
72          */
73         vecA = vld1q(pSrcA);
74         vecB = vld1q(pSrcB);
75         vst1q(pDst, vqdmulhq(vecA, vecB));
76         /*
77          * Decrement the blockSize loop counter
78          */
79         blkCnt--;
80         /*
81          * advance vector source and destination pointers
82          */
83         pSrcA  += 4;
84         pSrcB  += 4;
85         pDst   += 4;
86     }
87     /*
88      * tail
89      */
90     blkCnt = blockSize & 3;
91     if (blkCnt > 0U)
92     {
93         mve_pred16_t p0 = vctp32q(blkCnt);
94         vecA = vld1q(pSrcA);
95         vecB = vld1q(pSrcB);
96         vstrwq_p(pDst, vqdmulhq(vecA, vecB), p0);
97     }
98 }
99 
100 #else
arm_mult_q31(const q31_t * pSrcA,const q31_t * pSrcB,q31_t * pDst,uint32_t blockSize)101 void arm_mult_q31(
102   const q31_t * pSrcA,
103   const q31_t * pSrcB,
104         q31_t * pDst,
105         uint32_t blockSize)
106 {
107         uint32_t blkCnt;                               /* Loop counter */
108         q31_t out;                                     /* Temporary output variable */
109 
110 #if defined (ARM_MATH_LOOPUNROLL)
111 
112   /* Loop unrolling: Compute 4 outputs at a time */
113   blkCnt = blockSize >> 2U;
114 
115   while (blkCnt > 0U)
116   {
117     /* C = A * B */
118 
119     /* Multiply inputs and store result in destination buffer. */
120     out = ((q63_t) *pSrcA++ * *pSrcB++) >> 32;
121     out = __SSAT(out, 31);
122     *pDst++ = out << 1U;
123 
124     out = ((q63_t) *pSrcA++ * *pSrcB++) >> 32;
125     out = __SSAT(out, 31);
126     *pDst++ = out << 1U;
127 
128     out = ((q63_t) *pSrcA++ * *pSrcB++) >> 32;
129     out = __SSAT(out, 31);
130     *pDst++ = out << 1U;
131 
132     out = ((q63_t) *pSrcA++ * *pSrcB++) >> 32;
133     out = __SSAT(out, 31);
134     *pDst++ = out << 1U;
135 
136     /* Decrement loop counter */
137     blkCnt--;
138   }
139 
140   /* Loop unrolling: Compute remaining outputs */
141   blkCnt = blockSize % 0x4U;
142 
143 #else
144 
145   /* Initialize blkCnt with number of samples */
146   blkCnt = blockSize;
147 
148 #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
149 
150   while (blkCnt > 0U)
151   {
152     /* C = A * B */
153 
154     /* Multiply inputs and store result in destination buffer. */
155     out = ((q63_t) *pSrcA++ * *pSrcB++) >> 32;
156     out = __SSAT(out, 31);
157     *pDst++ = out << 1U;
158 
159     /* Decrement loop counter */
160     blkCnt--;
161   }
162 
163 }
164 #endif /* defined(ARM_MATH_MVEI) */
165 
166 /**
167   @} end of BasicMult group
168  */
169