before merging master
[inav.git] / lib / main / CMSIS / DSP / Source / BasicMathFunctions / arm_shift_q31.c
blob7e728d4ede7fbac9a211553f9c91a36c9d3dca2d
1 /* ----------------------------------------------------------------------
2 * Project: CMSIS DSP Library
3 * Title: arm_shift_q31.c
4 * Description: Shifts the elements of a Q31 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
34 /**
35 * @defgroup shift Vector Shift
37 * Shifts the elements of a fixed-point vector by a specified number of bits.
38 * There are separate functions for Q7, Q15, and Q31 data types.
39 * The underlying algorithm used is:
41 * <pre>
42 * pDst[n] = pSrc[n] << shift, 0 <= n < blockSize.
43 * </pre>
45 * If <code>shift</code> is positive then the elements of the vector are shifted to the left.
46 * If <code>shift</code> is negative then the elements of the vector are shifted to the right.
48 * The functions support in-place computation allowing the source and destination
49 * pointers to reference the same memory buffer.
52 /**
53 * @addtogroup shift
54 * @{
57 /**
58 * @brief Shifts the elements of a Q31 vector a specified number of bits.
59 * @param[in] *pSrc points to the input vector
60 * @param[in] shiftBits number of bits to shift. A positive value shifts left; a negative value shifts right.
61 * @param[out] *pDst points to the output vector
62 * @param[in] blockSize number of samples in the vector
63 * @return none.
66 * <b>Scaling and Overflow Behavior:</b>
67 * \par
68 * The function uses saturating arithmetic.
69 * Results outside of the allowable Q31 range [0x80000000 0x7FFFFFFF] will be saturated.
72 void arm_shift_q31(
73 q31_t * pSrc,
74 int8_t shiftBits,
75 q31_t * pDst,
76 uint32_t blockSize)
78 uint32_t blkCnt; /* loop counter */
79 uint8_t sign = (shiftBits & 0x80); /* Sign of shiftBits */
81 #if defined (ARM_MATH_DSP)
83 q31_t in1, in2, in3, in4; /* Temporary input variables */
84 q31_t out1, out2, out3, out4; /* Temporary output variables */
86 /*loop Unrolling */
87 blkCnt = blockSize >> 2U;
90 if (sign == 0U)
92 /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
93 ** a second loop below computes the remaining 1 to 3 samples. */
94 while (blkCnt > 0U)
96 /* C = A << shiftBits */
97 /* Shift the input and then store the results in the destination buffer. */
98 in1 = *pSrc;
99 in2 = *(pSrc + 1);
100 out1 = in1 << shiftBits;
101 in3 = *(pSrc + 2);
102 out2 = in2 << shiftBits;
103 in4 = *(pSrc + 3);
104 if (in1 != (out1 >> shiftBits))
105 out1 = 0x7FFFFFFF ^ (in1 >> 31);
107 if (in2 != (out2 >> shiftBits))
108 out2 = 0x7FFFFFFF ^ (in2 >> 31);
110 *pDst = out1;
111 out3 = in3 << shiftBits;
112 *(pDst + 1) = out2;
113 out4 = in4 << shiftBits;
115 if (in3 != (out3 >> shiftBits))
116 out3 = 0x7FFFFFFF ^ (in3 >> 31);
118 if (in4 != (out4 >> shiftBits))
119 out4 = 0x7FFFFFFF ^ (in4 >> 31);
121 *(pDst + 2) = out3;
122 *(pDst + 3) = out4;
124 /* Update destination pointer to process next sampels */
125 pSrc += 4U;
126 pDst += 4U;
128 /* Decrement the loop counter */
129 blkCnt--;
132 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 /* C = A >> shiftBits */
140 /* Shift the input and then store the results in the destination buffer. */
141 in1 = *pSrc;
142 in2 = *(pSrc + 1);
143 in3 = *(pSrc + 2);
144 in4 = *(pSrc + 3);
146 *pDst = (in1 >> -shiftBits);
147 *(pDst + 1) = (in2 >> -shiftBits);
148 *(pDst + 2) = (in3 >> -shiftBits);
149 *(pDst + 3) = (in4 >> -shiftBits);
152 pSrc += 4U;
153 pDst += 4U;
155 blkCnt--;
160 /* If the blockSize is not a multiple of 4, compute any remaining output samples here.
161 ** No loop unrolling is used. */
162 blkCnt = blockSize % 0x4U;
164 #else
166 /* Run the below code for Cortex-M0 */
169 /* Initialize blkCnt with number of samples */
170 blkCnt = blockSize;
172 #endif /* #if defined (ARM_MATH_DSP) */
175 while (blkCnt > 0U)
177 /* C = A (>> or <<) shiftBits */
178 /* Shift the input and then store the result in the destination buffer. */
179 *pDst++ = (sign == 0U) ? clip_q63_to_q31((q63_t) * pSrc++ << shiftBits) :
180 (*pSrc++ >> -shiftBits);
182 /* Decrement the loop counter */
183 blkCnt--;
190 * @} end of shift group