before merging master
[inav.git] / lib / main / CMSIS / DSP / Source / BasicMathFunctions / arm_shift_q15.c
blobd2cd03779d5b33af706dea84d7a442fde2cc10e9
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
6 * $Date: 27. January 2017
7 * $Revision: V.1.5.1
9 * Target Processor: Cortex-M cores
10 * -------------------------------------------------------------------- */
12 * Copyright (C) 2010-2017 ARM Limited or its affiliates. All rights reserved.
14 * SPDX-License-Identifier: Apache-2.0
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
20 * www.apache.org/licenses/LICENSE-2.0
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.
29 #include "arm_math.h"
31 /**
32 * @ingroup groupMath
35 /**
36 * @addtogroup shift
37 * @{
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.
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.
54 void arm_shift_q15(
55 q15_t * pSrc,
56 int8_t shiftBits,
57 q15_t * pDst,
58 uint32_t blockSize)
60 uint32_t blkCnt; /* loop counter */
61 uint8_t sign; /* Sign of shiftBits */
63 #if defined (ARM_MATH_DSP)
65 /* Run the below code for Cortex-M4 and Cortex-M3 */
67 q15_t in1, in2; /* Temporary variables */
70 /*loop Unrolling */
71 blkCnt = blockSize >> 2U;
73 /* Getting the sign of shiftBits */
74 sign = (shiftBits & 0x80);
76 /* If the shift value is positive then do right shift else left shift */
77 if (sign == 0U)
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)
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
90 *__SIMD32(pDst)++ = __PKHBT(__SSAT((in1 << shiftBits), 16),
91 __SSAT((in2 << shiftBits), 16), 16);
93 #else
95 *__SIMD32(pDst)++ = __PKHBT(__SSAT((in2 << shiftBits), 16),
96 __SSAT((in1 << shiftBits), 16), 16);
98 #endif /* #ifndef ARM_MATH_BIG_ENDIAN */
100 in1 = *pSrc++;
101 in2 = *pSrc++;
103 #ifndef ARM_MATH_BIG_ENDIAN
105 *__SIMD32(pDst)++ = __PKHBT(__SSAT((in1 << shiftBits), 16),
106 __SSAT((in2 << shiftBits), 16), 16);
108 #else
110 *__SIMD32(pDst)++ = __PKHBT(__SSAT((in2 << shiftBits), 16),
111 __SSAT((in1 << shiftBits), 16), 16);
113 #endif /* #ifndef ARM_MATH_BIG_ENDIAN */
115 /* Decrement the loop counter */
116 blkCnt--;
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;
123 while (blkCnt > 0U)
125 /* C = A << shiftBits */
126 /* Shift and then store the results in the destination buffer. */
127 *pDst++ = __SSAT((*pSrc++ << shiftBits), 16);
129 /* Decrement the loop counter */
130 blkCnt--;
133 else
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)
139 /* Read 2 inputs */
140 in1 = *pSrc++;
141 in2 = *pSrc++;
143 /* C = A >> shiftBits */
144 /* Shift the inputs and then store the results in the destination buffer. */
145 #ifndef ARM_MATH_BIG_ENDIAN
147 *__SIMD32(pDst)++ = __PKHBT((in1 >> -shiftBits),
148 (in2 >> -shiftBits), 16);
150 #else
152 *__SIMD32(pDst)++ = __PKHBT((in2 >> -shiftBits),
153 (in1 >> -shiftBits), 16);
155 #endif /* #ifndef ARM_MATH_BIG_ENDIAN */
157 in1 = *pSrc++;
158 in2 = *pSrc++;
160 #ifndef ARM_MATH_BIG_ENDIAN
162 *__SIMD32(pDst)++ = __PKHBT((in1 >> -shiftBits),
163 (in2 >> -shiftBits), 16);
165 #else
167 *__SIMD32(pDst)++ = __PKHBT((in2 >> -shiftBits),
168 (in1 >> -shiftBits), 16);
170 #endif /* #ifndef ARM_MATH_BIG_ENDIAN */
172 /* Decrement the loop counter */
173 blkCnt--;
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;
180 while (blkCnt > 0U)
182 /* C = A >> shiftBits */
183 /* Shift the inputs and then store the results in the destination buffer. */
184 *pDst++ = (*pSrc++ >> -shiftBits);
186 /* Decrement the loop counter */
187 blkCnt--;
191 #else
193 /* Run the below code for Cortex-M0 */
195 /* Getting the sign of shiftBits */
196 sign = (shiftBits & 0x80);
198 /* If the shift value is positive then do right shift else left shift */
199 if (sign == 0U)
201 /* Initialize blkCnt with number of samples */
202 blkCnt = blockSize;
204 while (blkCnt > 0U)
206 /* C = A << shiftBits */
207 /* Shift and then store the results in the destination buffer. */
208 *pDst++ = __SSAT(((q31_t) * pSrc++ << shiftBits), 16);
210 /* Decrement the loop counter */
211 blkCnt--;
214 else
216 /* Initialize blkCnt with number of samples */
217 blkCnt = blockSize;
219 while (blkCnt > 0U)
221 /* C = A >> shiftBits */
222 /* Shift the inputs and then store the results in the destination buffer. */
223 *pDst++ = (*pSrc++ >> -shiftBits);
225 /* Decrement the loop counter */
226 blkCnt--;
230 #endif /* #if defined (ARM_MATH_DSP) */
235 * @} end of shift group