1 /* ----------------------------------------------------------------------
2  * Project:      CMSIS DSP Library
3  * Title:        arm_add_q15.c
4  * Description:  Q15 vector addition
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 BasicAdd
37   @{
38  */
39 
40 /**
41   @brief         Q15 vector addition.
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 Q15 range [0x8000 0x7FFF] are saturated.
51  */
52 
53 #if defined(ARM_MATH_MVEI) && !defined(ARM_MATH_AUTOVECTORIZE)
54 
55 #include "arm_helium_utils.h"
56 
arm_add_q15(const q15_t * pSrcA,const q15_t * pSrcB,q15_t * pDst,uint32_t blockSize)57 void arm_add_q15(
58     const q15_t * pSrcA,
59     const q15_t * pSrcB,
60     q15_t * pDst,
61     uint32_t blockSize)
62 {
63     uint32_t  blkCnt;           /* loop counters */
64     q15x8_t vecA;
65     q15x8_t vecB;
66 
67     /* Compute 8 outputs at a time */
68     blkCnt = blockSize >> 3;
69     while (blkCnt > 0U)
70     {
71         /*
72          * C = A + B
73          * Add and then store the results in the destination buffer.
74          */
75         vecA = vld1q(pSrcA);
76         vecB = vld1q(pSrcB);
77         vst1q(pDst, vqaddq(vecA, vecB));
78         /*
79          * Decrement the blockSize loop counter
80          */
81         blkCnt--;
82         /*
83          * advance vector source and destination pointers
84          */
85         pSrcA  += 8;
86         pSrcB  += 8;
87         pDst   += 8;
88     }
89     /*
90      * tail
91      */
92     blkCnt = blockSize & 7;
93     if (blkCnt > 0U)
94     {
95         mve_pred16_t p0 = vctp16q(blkCnt);
96         vecA = vld1q(pSrcA);
97         vecB = vld1q(pSrcB);
98         vstrhq_p(pDst, vqaddq(vecA, vecB), p0);
99     }
100 }
101 
102 #else
arm_add_q15(const q15_t * pSrcA,const q15_t * pSrcB,q15_t * pDst,uint32_t blockSize)103 void arm_add_q15(
104   const q15_t * pSrcA,
105   const q15_t * pSrcB,
106         q15_t * pDst,
107         uint32_t blockSize)
108 {
109         uint32_t blkCnt;                               /* Loop counter */
110 
111 #if defined (ARM_MATH_LOOPUNROLL)
112 
113 #if defined (ARM_MATH_DSP)
114   q31_t inA1, inA2;
115   q31_t inB1, inB2;
116 #endif
117 
118   /* Loop unrolling: Compute 4 outputs at a time */
119   blkCnt = blockSize >> 2U;
120 
121   while (blkCnt > 0U)
122   {
123     /* C = A + B */
124 
125 #if defined (ARM_MATH_DSP)
126     /* read 2 times 2 samples at a time from sourceA */
127     inA1 = read_q15x2_ia ((q15_t **) &pSrcA);
128     inA2 = read_q15x2_ia ((q15_t **) &pSrcA);
129     /* read 2 times 2 samples at a time from sourceB */
130     inB1 = read_q15x2_ia ((q15_t **) &pSrcB);
131     inB2 = read_q15x2_ia ((q15_t **) &pSrcB);
132 
133     /* Add and store 2 times 2 samples at a time */
134     write_q15x2_ia (&pDst, __QADD16(inA1, inB1));
135     write_q15x2_ia (&pDst, __QADD16(inA2, inB2));
136 #else
137     *pDst++ = (q15_t) __SSAT(((q31_t) *pSrcA++ + *pSrcB++), 16);
138     *pDst++ = (q15_t) __SSAT(((q31_t) *pSrcA++ + *pSrcB++), 16);
139     *pDst++ = (q15_t) __SSAT(((q31_t) *pSrcA++ + *pSrcB++), 16);
140     *pDst++ = (q15_t) __SSAT(((q31_t) *pSrcA++ + *pSrcB++), 16);
141 #endif
142 
143     /* Decrement loop counter */
144     blkCnt--;
145   }
146 
147   /* Loop unrolling: Compute remaining outputs */
148   blkCnt = blockSize % 0x4U;
149 
150 #else
151 
152   /* Initialize blkCnt with number of samples */
153   blkCnt = blockSize;
154 
155 #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
156 
157   while (blkCnt > 0U)
158   {
159     /* C = A + B */
160 
161     /* Add and store result in destination buffer. */
162 #if defined (ARM_MATH_DSP)
163     *pDst++ = (q15_t) __QADD16(*pSrcA++, *pSrcB++);
164 #else
165     *pDst++ = (q15_t) __SSAT(((q31_t) *pSrcA++ + *pSrcB++), 16);
166 #endif
167 
168     /* Decrement loop counter */
169     blkCnt--;
170   }
171 
172 }
173 #endif /* defined(ARM_MATH_MVEI) */
174 /**
175   @} end of BasicAdd group
176  */
177