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
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.
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:
42 * pDst[n] = pSrc[n] << shift, 0 <= n < blockSize.
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.
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
66 * <b>Scaling and Overflow Behavior:</b>
68 * The function uses saturating arithmetic.
69 * Results outside of the allowable Q31 range [0x80000000 0x7FFFFFFF] will be saturated.
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 */
87 blkCnt
= blockSize
>> 2U;
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. */
96 /* C = A << shiftBits */
97 /* Shift the input and then store the results in the destination buffer. */
100 out1
= in1
<< shiftBits
;
102 out2
= in2
<< shiftBits
;
104 if (in1
!= (out1
>> shiftBits
))
105 out1
= 0x7FFFFFFF ^ (in1
>> 31);
107 if (in2
!= (out2
>> shiftBits
))
108 out2
= 0x7FFFFFFF ^ (in2
>> 31);
111 out3
= in3
<< shiftBits
;
113 out4
= in4
<< shiftBits
;
115 if (in3
!= (out3
>> shiftBits
))
116 out3
= 0x7FFFFFFF ^ (in3
>> 31);
118 if (in4
!= (out4
>> shiftBits
))
119 out4
= 0x7FFFFFFF ^ (in4
>> 31);
124 /* Update destination pointer to process next sampels */
128 /* Decrement the loop counter */
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. */
139 /* C = A >> shiftBits */
140 /* Shift the input and then store the results in the destination buffer. */
146 *pDst
= (in1
>> -shiftBits
);
147 *(pDst
+ 1) = (in2
>> -shiftBits
);
148 *(pDst
+ 2) = (in3
>> -shiftBits
);
149 *(pDst
+ 3) = (in4
>> -shiftBits
);
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
;
166 /* Run the below code for Cortex-M0 */
169 /* Initialize blkCnt with number of samples */
172 #endif /* #if defined (ARM_MATH_DSP) */
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 */
190 * @} end of shift group