1 /* ----------------------------------------------------------------------
2 * Project: CMSIS DSP Library
3 * Title: arm_shift_q7.c
4 * Description: Processing function for the Q7 Shifting
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 BasicShift
37 @{
38 */
39
40 /**
41 @brief Shifts the elements of a Q7 vector a specified number of bits
42 @param[in] pSrc points to the input vector
43 @param[in] shiftBits number of bits to shift. A positive value shifts left; a negative value shifts right.
44 @param[out] pDst points to the output vector
45 @param[in] blockSize number of samples in each vector
46
47 @par onditions for optimum performance
48 Input and output buffers should be aligned by 32-bit
49 @par Scaling and Overflow Behavior
50 The function uses saturating arithmetic.
51 Results outside of the allowable Q7 range [0x80 0x7F] are saturated.
52 */
53
54 #if defined(ARM_MATH_MVEI) && !defined(ARM_MATH_AUTOVECTORIZE)
55
56 #include "arm_helium_utils.h"
57
arm_shift_q7(const q7_t * pSrc,int8_t shiftBits,q7_t * pDst,uint32_t blockSize)58 ARM_DSP_ATTRIBUTE void arm_shift_q7(
59 const q7_t * pSrc,
60 int8_t shiftBits,
61 q7_t * pDst,
62 uint32_t blockSize)
63 {
64 uint32_t blkCnt; /* loop counters */
65 q7x16_t vecSrc;
66 q7x16_t vecDst;
67
68 /* Compute 16 outputs at a time */
69 blkCnt = blockSize >> 4;
70 while (blkCnt > 0U)
71 {
72 /*
73 * C = A (>> or <<) shiftBits
74 * Shift the input and then store the result in the destination buffer.
75 */
76 vecSrc = vld1q(pSrc);
77 vecDst = vqshlq_r(vecSrc, shiftBits);
78 vst1q(pDst, vecDst);
79 /*
80 * Decrement the blockSize loop counter
81 */
82 blkCnt--;
83 /*
84 * advance vector source and destination pointers
85 */
86 pSrc += 16;
87 pDst += 16;
88 }
89 /*
90 * tail
91 */
92 blkCnt = blockSize & 0xF;
93 if (blkCnt > 0U)
94 {
95 mve_pred16_t p0 = vctp8q(blkCnt);
96 vecSrc = vld1q(pSrc);
97 vecDst = vqshlq_r(vecSrc, shiftBits);
98 vstrbq_p(pDst, vecDst, p0);
99 }
100 }
101
102 #else
arm_shift_q7(const q7_t * pSrc,int8_t shiftBits,q7_t * pDst,uint32_t blockSize)103 ARM_DSP_ATTRIBUTE void arm_shift_q7(
104 const q7_t * pSrc,
105 int8_t shiftBits,
106 q7_t * pDst,
107 uint32_t blockSize)
108 {
109 uint32_t blkCnt; /* Loop counter */
110 uint8_t sign = (shiftBits & 0x80); /* Sign of shiftBits */
111
112 #if defined (ARM_MATH_LOOPUNROLL)
113
114 #if defined (ARM_MATH_DSP)
115 q7_t in1, in2, in3, in4; /* Temporary input variables */
116 #endif
117
118 /* Loop unrolling: Compute 4 outputs at a time */
119 blkCnt = blockSize >> 2U;
120
121 /* If the shift value is positive then do right shift else left shift */
122 if (sign == 0U)
123 {
124 while (blkCnt > 0U)
125 {
126 /* C = A << shiftBits */
127
128 #if defined (ARM_MATH_DSP)
129 /* Read 4 inputs */
130 in1 = *pSrc++;
131 in2 = *pSrc++;
132 in3 = *pSrc++;
133 in4 = *pSrc++;
134
135 /* Pack and store result in destination buffer (in single write) */
136 write_q7x4_ia (&pDst, __PACKq7(__SSAT(((q15_t) in1 << shiftBits), 8),
137 __SSAT(((q15_t) in2 << shiftBits), 8),
138 __SSAT(((q15_t) in3 << shiftBits), 8),
139 __SSAT(((q15_t) in4 << shiftBits), 8) ));
140 #else
141 *pDst++ = (q7_t) __SSAT(((q15_t) *pSrc++ << shiftBits), 8);
142 *pDst++ = (q7_t) __SSAT(((q15_t) *pSrc++ << shiftBits), 8);
143 *pDst++ = (q7_t) __SSAT(((q15_t) *pSrc++ << shiftBits), 8);
144 *pDst++ = (q7_t) __SSAT(((q15_t) *pSrc++ << shiftBits), 8);
145 #endif
146
147 /* Decrement loop counter */
148 blkCnt--;
149 }
150 }
151 else
152 {
153 while (blkCnt > 0U)
154 {
155 /* C = A >> shiftBits */
156
157 #if defined (ARM_MATH_DSP)
158 /* Read 4 inputs */
159 in1 = *pSrc++;
160 in2 = *pSrc++;
161 in3 = *pSrc++;
162 in4 = *pSrc++;
163
164 /* Pack and store result in destination buffer (in single write) */
165 write_q7x4_ia (&pDst, __PACKq7((in1 >> -shiftBits),
166 (in2 >> -shiftBits),
167 (in3 >> -shiftBits),
168 (in4 >> -shiftBits) ));
169 #else
170 *pDst++ = (*pSrc++ >> -shiftBits);
171 *pDst++ = (*pSrc++ >> -shiftBits);
172 *pDst++ = (*pSrc++ >> -shiftBits);
173 *pDst++ = (*pSrc++ >> -shiftBits);
174 #endif
175
176 /* Decrement loop counter */
177 blkCnt--;
178 }
179 }
180
181 /* Loop unrolling: Compute remaining outputs */
182 blkCnt = blockSize % 0x4U;
183
184 #else
185
186 /* Initialize blkCnt with number of samples */
187 blkCnt = blockSize;
188
189 #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
190
191 /* If the shift value is positive then do right shift else left shift */
192 if (sign == 0U)
193 {
194 while (blkCnt > 0U)
195 {
196 /* C = A << shiftBits */
197
198 /* Shift input and store result in destination buffer. */
199 *pDst++ = (q7_t) __SSAT(((q15_t) *pSrc++ << shiftBits), 8);
200
201 /* Decrement loop counter */
202 blkCnt--;
203 }
204 }
205 else
206 {
207 while (blkCnt > 0U)
208 {
209 /* C = A >> shiftBits */
210
211 /* Shift input and store result in destination buffer. */
212 *pDst++ = (*pSrc++ >> -shiftBits);
213
214 /* Decrement loop counter */
215 blkCnt--;
216 }
217 }
218
219 }
220 #endif /* defined(ARM_MATH_MVEI) */
221
222 /**
223 @} end of BasicShift group
224 */
225