1 /* ----------------------------------------------------------------------
2  * Project:      CMSIS DSP Library
3  * Title:        arm_shift_q15.c
4  * Description:  Shifts the elements of a Q15 vector by a specified number of bits
5  *
6  * $Date:        27. January 2017
7  * $Revision:    V.1.5.1
8  *
9  * Target Processor: Cortex-M cores
10  * -------------------------------------------------------------------- */
11 /*
12  * Copyright (C) 2010-2017 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 "arm_math.h"
30 
31 /**
32  * @ingroup groupMath
33  */
34 
35 /**
36  * @addtogroup shift
37  * @{
38  */
39 
40 /**
41  * @brief  Shifts the elements of a Q15 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 the vector
46  * @return none.
47  *
48  * <b>Scaling and Overflow Behavior:</b>
49  * \par
50  * The function uses saturating arithmetic.
51  * Results outside of the allowable Q15 range [0x8000 0x7FFF] will be saturated.
52  */
53 
arm_shift_q15(q15_t * pSrc,int8_t shiftBits,q15_t * pDst,uint32_t blockSize)54 void arm_shift_q15(
55   q15_t * pSrc,
56   int8_t shiftBits,
57   q15_t * pDst,
58   uint32_t blockSize)
59 {
60   uint32_t blkCnt;                               /* loop counter */
61   uint8_t sign;                                  /* Sign of shiftBits */
62 
63 #if defined (ARM_MATH_DSP)
64 
65 /* Run the below code for Cortex-M4 and Cortex-M3 */
66 
67   q15_t in1, in2;                                /* Temporary variables */
68 
69 
70   /*loop Unrolling */
71   blkCnt = blockSize >> 2u;
72 
73   /* Getting the sign of shiftBits */
74   sign = (shiftBits & 0x80);
75 
76   /* If the shift value is positive then do right shift else left shift */
77   if (sign == 0u)
78   {
79     /* First part of the processing with loop unrolling.  Compute 4 outputs at a time.
80      ** a second loop below computes the remaining 1 to 3 samples. */
81     while (blkCnt > 0u)
82     {
83       /* Read 2 inputs */
84       in1 = *pSrc++;
85       in2 = *pSrc++;
86       /* C = A << shiftBits */
87       /* Shift the inputs and then store the results in the destination buffer. */
88 #ifndef  ARM_MATH_BIG_ENDIAN
89 
90       *__SIMD32(pDst)++ = __PKHBT(__SSAT((in1 << shiftBits), 16),
91                                   __SSAT((in2 << shiftBits), 16), 16);
92 
93 #else
94 
95       *__SIMD32(pDst)++ = __PKHBT(__SSAT((in2 << shiftBits), 16),
96                                   __SSAT((in1 << shiftBits), 16), 16);
97 
98 #endif /* #ifndef  ARM_MATH_BIG_ENDIAN    */
99 
100       in1 = *pSrc++;
101       in2 = *pSrc++;
102 
103 #ifndef  ARM_MATH_BIG_ENDIAN
104 
105       *__SIMD32(pDst)++ = __PKHBT(__SSAT((in1 << shiftBits), 16),
106                                   __SSAT((in2 << shiftBits), 16), 16);
107 
108 #else
109 
110       *__SIMD32(pDst)++ = __PKHBT(__SSAT((in2 << shiftBits), 16),
111                                   __SSAT((in1 << shiftBits), 16), 16);
112 
113 #endif /* #ifndef  ARM_MATH_BIG_ENDIAN    */
114 
115       /* Decrement the loop counter */
116       blkCnt--;
117     }
118 
119     /* If the blockSize is not a multiple of 4, compute any remaining output samples here.
120      ** No loop unrolling is used. */
121     blkCnt = blockSize % 0x4u;
122 
123     while (blkCnt > 0u)
124     {
125       /* C = A << shiftBits */
126       /* Shift and then store the results in the destination buffer. */
127       *pDst++ = __SSAT((*pSrc++ << shiftBits), 16);
128 
129       /* Decrement the loop counter */
130       blkCnt--;
131     }
132   }
133   else
134   {
135     /* First part of the processing with loop unrolling.  Compute 4 outputs at a time.
136      ** a second loop below computes the remaining 1 to 3 samples. */
137     while (blkCnt > 0u)
138     {
139       /* Read 2 inputs */
140       in1 = *pSrc++;
141       in2 = *pSrc++;
142 
143       /* C = A >> shiftBits */
144       /* Shift the inputs and then store the results in the destination buffer. */
145 #ifndef  ARM_MATH_BIG_ENDIAN
146 
147       *__SIMD32(pDst)++ = __PKHBT((in1 >> -shiftBits),
148                                   (in2 >> -shiftBits), 16);
149 
150 #else
151 
152       *__SIMD32(pDst)++ = __PKHBT((in2 >> -shiftBits),
153                                   (in1 >> -shiftBits), 16);
154 
155 #endif /* #ifndef  ARM_MATH_BIG_ENDIAN    */
156 
157       in1 = *pSrc++;
158       in2 = *pSrc++;
159 
160 #ifndef  ARM_MATH_BIG_ENDIAN
161 
162       *__SIMD32(pDst)++ = __PKHBT((in1 >> -shiftBits),
163                                   (in2 >> -shiftBits), 16);
164 
165 #else
166 
167       *__SIMD32(pDst)++ = __PKHBT((in2 >> -shiftBits),
168                                   (in1 >> -shiftBits), 16);
169 
170 #endif /* #ifndef  ARM_MATH_BIG_ENDIAN    */
171 
172       /* Decrement the loop counter */
173       blkCnt--;
174     }
175 
176     /* If the blockSize is not a multiple of 4, compute any remaining output samples here.
177      ** No loop unrolling is used. */
178     blkCnt = blockSize % 0x4u;
179 
180     while (blkCnt > 0u)
181     {
182       /* C = A >> shiftBits */
183       /* Shift the inputs and then store the results in the destination buffer. */
184       *pDst++ = (*pSrc++ >> -shiftBits);
185 
186       /* Decrement the loop counter */
187       blkCnt--;
188     }
189   }
190 
191 #else
192 
193   /* Run the below code for Cortex-M0 */
194 
195   /* Getting the sign of shiftBits */
196   sign = (shiftBits & 0x80);
197 
198   /* If the shift value is positive then do right shift else left shift */
199   if (sign == 0u)
200   {
201     /* Initialize blkCnt with number of samples */
202     blkCnt = blockSize;
203 
204     while (blkCnt > 0u)
205     {
206       /* C = A << shiftBits */
207       /* Shift and then store the results in the destination buffer. */
208       *pDst++ = __SSAT(((q31_t) * pSrc++ << shiftBits), 16);
209 
210       /* Decrement the loop counter */
211       blkCnt--;
212     }
213   }
214   else
215   {
216     /* Initialize blkCnt with number of samples */
217     blkCnt = blockSize;
218 
219     while (blkCnt > 0u)
220     {
221       /* C = A >> shiftBits */
222       /* Shift the inputs and then store the results in the destination buffer. */
223       *pDst++ = (*pSrc++ >> -shiftBits);
224 
225       /* Decrement the loop counter */
226       blkCnt--;
227     }
228   }
229 
230 #endif /* #if defined (ARM_MATH_DSP) */
231 
232 }
233 
234 /**
235  * @} end of shift group
236  */
237