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