1 /* ----------------------------------------------------------------------
2  * Project:      CMSIS DSP Library
3  * Title:        arm_dot_prod_q15.c
4  * Description:  Q15 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 Q15 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.15 x 1.15 = 2.30 format and these
49                    results are added to a 64-bit accumulator in 34.30 format.
50                    Nonsaturating additions are used and given that there are 33 guard bits in the accumulator
51                    there is no risk of overflow.
52                    The return result is in 34.30 format.
53  */
54 #if defined(ARM_MATH_MVEI) && !defined(ARM_MATH_AUTOVECTORIZE)
55 
56 #include "arm_helium_utils.h"
57 
arm_dot_prod_q15(const q15_t * pSrcA,const q15_t * pSrcB,uint32_t blockSize,q63_t * result)58 void arm_dot_prod_q15(
59     const q15_t * pSrcA,
60     const q15_t * pSrcB,
61     uint32_t blockSize,
62     q63_t * result)
63 {
64     uint32_t  blkCnt;           /* loop counters */
65     q15x8_t vecA;
66     q15x8_t vecB;
67     q63_t     sum = 0LL;
68 
69     /* Compute 8 outputs at a time */
70     blkCnt = blockSize >> 3;
71     while (blkCnt > 0U)
72     {
73         /*
74          * C = A[0]* B[0] + A[1]* B[1] + A[2]* B[2] + .....+ A[blockSize-1]* B[blockSize-1]
75          * Calculate dot product and then store the result in a temporary buffer.
76          */
77         vecA = vld1q(pSrcA);
78         vecB = vld1q(pSrcB);
79         sum = vmlaldavaq(sum, vecA, vecB);
80         /*
81          * Decrement the blockSize loop counter
82          */
83         blkCnt--;
84         /*
85          * advance vector source and destination pointers
86          */
87         pSrcA += 8;
88         pSrcB += 8;
89     }
90     /*
91      * tail
92      */
93     blkCnt = blockSize & 7;
94     if (blkCnt > 0U)
95     {
96         mve_pred16_t p0 = vctp16q(blkCnt);
97         vecA = vld1q(pSrcA);
98         vecB = vld1q(pSrcB);
99         sum = vmlaldavaq_p(sum, vecA, vecB, p0);
100     }
101 
102     *result = sum;
103 }
104 
105 #else
arm_dot_prod_q15(const q15_t * pSrcA,const q15_t * pSrcB,uint32_t blockSize,q63_t * result)106 void arm_dot_prod_q15(
107   const q15_t * pSrcA,
108   const q15_t * pSrcB,
109         uint32_t blockSize,
110         q63_t * result)
111 {
112         uint32_t blkCnt;                               /* Loop counter */
113         q63_t sum = 0;                                 /* Temporary return variable */
114 
115 #if defined (ARM_MATH_LOOPUNROLL)
116 
117   /* Loop unrolling: Compute 4 outputs at a time */
118   blkCnt = blockSize >> 2U;
119 
120   while (blkCnt > 0U)
121   {
122     /* C = A[0]* B[0] + A[1]* B[1] + A[2]* B[2] + .....+ A[blockSize-1]* B[blockSize-1] */
123 
124 #if defined (ARM_MATH_DSP)
125     /* Calculate dot product and store result in a temporary buffer. */
126     sum = __SMLALD(read_q15x2_ia (&pSrcA), read_q15x2_ia (&pSrcB), sum);
127     sum = __SMLALD(read_q15x2_ia (&pSrcA), read_q15x2_ia (&pSrcB), sum);
128 #else
129     sum += (q63_t)((q31_t) *pSrcA++ * *pSrcB++);
130     sum += (q63_t)((q31_t) *pSrcA++ * *pSrcB++);
131     sum += (q63_t)((q31_t) *pSrcA++ * *pSrcB++);
132     sum += (q63_t)((q31_t) *pSrcA++ * *pSrcB++);
133 #endif
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[0]* B[0] + A[1]* B[1] + A[2]* B[2] + .....+ A[blockSize-1]* B[blockSize-1] */
152 
153     /* Calculate dot product and store result in a temporary buffer. */
154 //#if defined (ARM_MATH_DSP)
155 //    sum  = __SMLALD(*pSrcA++, *pSrcB++, sum);
156 //#else
157     sum += (q63_t)((q31_t) *pSrcA++ * *pSrcB++);
158 //#endif
159 
160     /* Decrement loop counter */
161     blkCnt--;
162   }
163 
164   /* Store result in destination buffer in 34.30 format */
165   *result = sum;
166 }
167 #endif /* defined(ARM_MATH_MVEI) */
168 
169 /**
170   @} end of BasicDotProd group
171  */
172