1 /* ----------------------------------------------------------------------
2  * Project:      CMSIS DSP Library
3  * Title:        arm_mult_q7.c
4  * Description:  Q7 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         Q7 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 Q7 range [0x80 0x7F] are saturated.
50  */
51 #if defined(ARM_MATH_MVEI) && !defined(ARM_MATH_AUTOVECTORIZE)
52 
53 #include "arm_helium_utils.h"
54 
arm_mult_q7(const q7_t * pSrcA,const q7_t * pSrcB,q7_t * pDst,uint32_t blockSize)55 ARM_DSP_ATTRIBUTE void arm_mult_q7(
56     const q7_t * pSrcA,
57     const q7_t * pSrcB,
58     q7_t * pDst,
59     uint32_t blockSize)
60 {
61     uint32_t  blkCnt;           /* loop counters */
62     q7x16_t vecA, vecB;
63 
64     /* Compute 16 outputs at a time */
65     blkCnt = blockSize >> 4;
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  += 16;
83         pSrcB  += 16;
84         pDst   += 16;
85     }
86     /*
87      * tail
88      */
89     blkCnt = blockSize & 0xF;
90     if (blkCnt > 0U)
91     {
92         mve_pred16_t p0 = vctp8q(blkCnt);
93         vecA = vld1q(pSrcA);
94         vecB = vld1q(pSrcB);
95         vstrbq_p(pDst, vqdmulhq(vecA, vecB), p0);
96     }
97 }
98 
99 #else
arm_mult_q7(const q7_t * pSrcA,const q7_t * pSrcB,q7_t * pDst,uint32_t blockSize)100 ARM_DSP_ATTRIBUTE void arm_mult_q7(
101   const q7_t * pSrcA,
102   const q7_t * pSrcB,
103         q7_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   q7_t out1, out2, out3, out4;                   /* Temporary output variables */
112 #endif
113 
114   /* Loop unrolling: Compute 4 outputs at a time */
115   blkCnt = blockSize >> 2U;
116 
117   while (blkCnt > 0U)
118   {
119     /* C = A * B */
120 
121 #if defined (ARM_MATH_DSP)
122     /* Multiply inputs and store results in temporary variables */
123     out1 = (q7_t) __SSAT((((q15_t) (*pSrcA++) * (*pSrcB++)) >> 7), 8);
124     out2 = (q7_t) __SSAT((((q15_t) (*pSrcA++) * (*pSrcB++)) >> 7), 8);
125     out3 = (q7_t) __SSAT((((q15_t) (*pSrcA++) * (*pSrcB++)) >> 7), 8);
126     out4 = (q7_t) __SSAT((((q15_t) (*pSrcA++) * (*pSrcB++)) >> 7), 8);
127 
128     /* Pack and store result in destination buffer (in single write) */
129     write_q7x4_ia (&pDst, __PACKq7(out1, out2, out3, out4));
130 #else
131     *pDst++ = (q7_t) __SSAT((((q15_t) (*pSrcA++) * (*pSrcB++)) >> 7), 8);
132     *pDst++ = (q7_t) __SSAT((((q15_t) (*pSrcA++) * (*pSrcB++)) >> 7), 8);
133     *pDst++ = (q7_t) __SSAT((((q15_t) (*pSrcA++) * (*pSrcB++)) >> 7), 8);
134     *pDst++ = (q7_t) __SSAT((((q15_t) (*pSrcA++) * (*pSrcB++)) >> 7), 8);
135 #endif
136 
137     /* Decrement loop counter */
138     blkCnt--;
139   }
140 
141   /* Loop unrolling: Compute remaining outputs */
142   blkCnt = blockSize % 0x4U;
143 
144 #else
145 
146   /* Initialize blkCnt with number of samples */
147   blkCnt = blockSize;
148 
149 #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
150 
151   while (blkCnt > 0U)
152   {
153     /* C = A * B */
154 
155     /* Multiply input and store result in destination buffer. */
156     *pDst++ = (q7_t) __SSAT((((q15_t) (*pSrcA++) * (*pSrcB++)) >> 7), 8);
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