1 /* ----------------------------------------------------------------------
2  * Project:      CMSIS DSP Library
3  * Title:        arm_dot_prod_q7.c
4  * Description:  Q7 dot product
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 BasicDotProd
37   @{
38  */
39 
40 /**
41   @brief         Dot product of Q7 vectors.
42   @param[in]     pSrcA      points to the first input vector
43   @param[in]     pSrcB      points to the second input vector
44   @param[in]     blockSize  number of samples in each vector
45   @param[out]    result     output result returned here
46 
47   @par           Scaling and Overflow Behavior
48                    The intermediate multiplications are in 1.7 x 1.7 = 2.14 format and these
49                    results are added to an accumulator in 18.14 format.
50                    Nonsaturating additions are used and there is no danger of wrap around as long as
51                    the vectors are less than 2^18 elements long.
52                    The return result is in 18.14 format.
53  */
54 
55 #if defined(ARM_MATH_MVEI) && !defined(ARM_MATH_AUTOVECTORIZE)
56 
57 #include "arm_helium_utils.h"
58 
arm_dot_prod_q7(const q7_t * pSrcA,const q7_t * pSrcB,uint32_t blockSize,q31_t * result)59 ARM_DSP_ATTRIBUTE void arm_dot_prod_q7(
60     const q7_t * pSrcA,
61     const q7_t * pSrcB,
62     uint32_t blockSize,
63     q31_t * result)
64 {
65     uint32_t  blkCnt;           /* loop counters */
66     q7x16_t vecA;
67     q7x16_t vecB;
68     q31_t     sum = 0;
69 
70     /* Compute 16 outputs at a time */
71     blkCnt = blockSize >> 4;
72     while (blkCnt > 0U)
73     {
74         /*
75          * C = A[0]* B[0] + A[1]* B[1] + A[2]* B[2] + .....+ A[blockSize-1]* B[blockSize-1]
76          * Calculate dot product and then store the result in a temporary buffer.
77          */
78         vecA = vld1q(pSrcA);
79         vecB = vld1q(pSrcB);
80         sum = vmladavaq(sum, vecA, vecB);
81         /*
82          * Decrement the blockSize loop counter
83          */
84         blkCnt--;
85         /*
86          * advance vector source and destination pointers
87          */
88         pSrcA += 16;
89         pSrcB += 16;
90     }
91     /*
92      * tail
93      */
94     blkCnt = blockSize & 0xF;
95     if (blkCnt > 0U)
96     {
97         mve_pred16_t p0 = vctp8q(blkCnt);
98         vecA = vld1q(pSrcA);
99         vecB = vld1q(pSrcB);
100         sum = vmladavaq_p(sum, vecA, vecB, p0);
101     }
102 
103     *result = sum;
104 }
105 #else
arm_dot_prod_q7(const q7_t * pSrcA,const q7_t * pSrcB,uint32_t blockSize,q31_t * result)106 ARM_DSP_ATTRIBUTE void arm_dot_prod_q7(
107   const q7_t * pSrcA,
108   const q7_t * pSrcB,
109         uint32_t blockSize,
110         q31_t * result)
111 {
112         uint32_t blkCnt;                               /* Loop counter */
113         q31_t sum = 0;                                 /* Temporary return variable */
114 
115 #if defined (ARM_MATH_LOOPUNROLL)
116 
117 #if defined (ARM_MATH_DSP)
118   q31_t input1, input2;                          /* Temporary variables */
119   q31_t inA1, inA2, inB1, inB2;                  /* Temporary variables */
120 #endif
121 
122   /* Loop unrolling: Compute 4 outputs at a time */
123   blkCnt = blockSize >> 2U;
124 
125   while (blkCnt > 0U)
126   {
127     /* C = A[0]* B[0] + A[1]* B[1] + A[2]* B[2] + .....+ A[blockSize-1]* B[blockSize-1] */
128 
129 #if defined (ARM_MATH_DSP)
130     /* read 4 samples at a time from sourceA */
131     input1 = read_q7x4_ia (&pSrcA);
132     /* read 4 samples at a time from sourceB */
133     input2 = read_q7x4_ia (&pSrcB);
134 
135     /* extract two q7_t samples to q15_t samples */
136     inA1 = __SXTB16(__ROR(input1, 8));
137     /* extract reminaing two samples */
138     inA2 = __SXTB16(input1);
139     /* extract two q7_t samples to q15_t samples */
140     inB1 = __SXTB16(__ROR(input2, 8));
141     /* extract reminaing two samples */
142     inB2 = __SXTB16(input2);
143 
144     /* multiply and accumulate two samples at a time */
145     sum = __SMLAD(inA1, inB1, sum);
146     sum = __SMLAD(inA2, inB2, sum);
147 #else
148     sum += (q31_t) ((q15_t) *pSrcA++ * *pSrcB++);
149     sum += (q31_t) ((q15_t) *pSrcA++ * *pSrcB++);
150     sum += (q31_t) ((q15_t) *pSrcA++ * *pSrcB++);
151     sum += (q31_t) ((q15_t) *pSrcA++ * *pSrcB++);
152 #endif
153 
154     /* Decrement loop counter */
155     blkCnt--;
156   }
157 
158   /* Loop unrolling: Compute remaining outputs */
159   blkCnt = blockSize % 0x4U;
160 
161 #else
162 
163   /* Initialize blkCnt with number of samples */
164   blkCnt = blockSize;
165 
166 #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
167 
168   while (blkCnt > 0U)
169   {
170     /* C = A[0]* B[0] + A[1]* B[1] + A[2]* B[2] + .....+ A[blockSize-1]* B[blockSize-1] */
171 
172     /* Calculate dot product and store result in a temporary buffer. */
173 //#if defined (ARM_MATH_DSP)
174 //    sum  = __SMLAD(*pSrcA++, *pSrcB++, sum);
175 //#else
176     sum += (q31_t) ((q15_t) *pSrcA++ * *pSrcB++);
177 //#endif
178 
179     /* Decrement loop counter */
180     blkCnt--;
181   }
182 
183   /* Store result in destination buffer in 18.14 format */
184   *result = sum;
185 }
186 #endif /* defined(ARM_MATH_MVEI) */
187 
188 /**
189   @} end of BasicDotProd group
190  */
191