1 /* ----------------------------------------------------------------------
2 * Project: CMSIS DSP Library
3 * Title: arm_sub_q15.c
4 * Description: Q15 vector subtraction
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 BasicSub
37 @{
38 */
39
40 /**
41 @brief Q15 vector subtraction.
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_sub_q15(const q15_t * pSrcA,const q15_t * pSrcB,q15_t * pDst,uint32_t blockSize)57 void arm_sub_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 * Subtract and then store the results in the destination buffer.
74 */
75 vecA = vld1q(pSrcA);
76 vecB = vld1q(pSrcB);
77 vst1q(pDst, vqsubq(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, vqsubq(vecA, vecB), p0);
99 }
100 }
101
102
103 #else
arm_sub_q15(const q15_t * pSrcA,const q15_t * pSrcB,q15_t * pDst,uint32_t blockSize)104 void arm_sub_q15(
105 const q15_t * pSrcA,
106 const q15_t * pSrcB,
107 q15_t * pDst,
108 uint32_t blockSize)
109 {
110 uint32_t blkCnt; /* Loop counter */
111
112 #if defined (ARM_MATH_LOOPUNROLL)
113
114 #if defined (ARM_MATH_DSP)
115 q31_t inA1, inA2;
116 q31_t inB1, inB2;
117 #endif
118
119 /* Loop unrolling: Compute 4 outputs at a time */
120 blkCnt = blockSize >> 2U;
121
122 while (blkCnt > 0U)
123 {
124 /* C = A - B */
125
126 #if defined (ARM_MATH_DSP)
127 /* read 2 times 2 samples at a time from sourceA */
128 inA1 = read_q15x2_ia ((q15_t **) &pSrcA);
129 inA2 = read_q15x2_ia ((q15_t **) &pSrcA);
130 /* read 2 times 2 samples at a time from sourceB */
131 inB1 = read_q15x2_ia ((q15_t **) &pSrcB);
132 inB2 = read_q15x2_ia ((q15_t **) &pSrcB);
133
134 /* Subtract and store 2 times 2 samples at a time */
135 write_q15x2_ia (&pDst, __QSUB16(inA1, inB1));
136 write_q15x2_ia (&pDst, __QSUB16(inA2, inB2));
137 #else
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 *pDst++ = (q15_t) __SSAT(((q31_t) *pSrcA++ - *pSrcB++), 16);
142 #endif
143
144 /* Decrement loop counter */
145 blkCnt--;
146 }
147
148 /* Loop unrolling: Compute remaining outputs */
149 blkCnt = blockSize % 0x4U;
150
151 #else
152
153 /* Initialize blkCnt with number of samples */
154 blkCnt = blockSize;
155
156 #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
157
158 while (blkCnt > 0U)
159 {
160 /* C = A - B */
161
162 /* Subtract and store result in destination buffer. */
163 #if defined (ARM_MATH_DSP)
164 *pDst++ = (q15_t) __QSUB16(*pSrcA++, *pSrcB++);
165 #else
166 *pDst++ = (q15_t) __SSAT(((q31_t) *pSrcA++ - *pSrcB++), 16);
167 #endif
168
169 /* Decrement loop counter */
170 blkCnt--;
171 }
172
173 }
174 #endif /* defined(ARM_MATH_MVEI) */
175
176 /**
177 @} end of BasicSub group
178 */
179