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