Merge pull request #11270 from haslinghuis/rename_attr
[betaflight.git] / lib / main / CMSIS / DSP / Source / BasicMathFunctions / arm_shift_q7.c
blobfd508b47ba72e988b4776fda44e87f6d38630e62
1 /* ----------------------------------------------------------------------
2 * Project: CMSIS DSP Library
3 * Title: arm_shift_q7.c
4 * Description: Processing function for the Q7 Shifting
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 * @{
41 /**
42 * @brief Shifts the elements of a Q7 vector a specified number of bits.
43 * @param[in] *pSrc points to the input vector
44 * @param[in] shiftBits number of bits to shift. A positive value shifts left; a negative value shifts right.
45 * @param[out] *pDst points to the output vector
46 * @param[in] blockSize number of samples in the vector
47 * @return none.
49 * \par Conditions for optimum performance
50 * Input and output buffers should be aligned by 32-bit
53 * <b>Scaling and Overflow Behavior:</b>
54 * \par
55 * The function uses saturating arithmetic.
56 * Results outside of the allowable Q7 range [0x8 0x7F] will be saturated.
59 void arm_shift_q7(
60 q7_t * pSrc,
61 int8_t shiftBits,
62 q7_t * pDst,
63 uint32_t blockSize)
65 uint32_t blkCnt; /* loop counter */
66 uint8_t sign; /* Sign of shiftBits */
68 #if defined (ARM_MATH_DSP)
70 /* Run the below code for Cortex-M4 and Cortex-M3 */
71 q7_t in1; /* Input value1 */
72 q7_t in2; /* Input value2 */
73 q7_t in3; /* Input value3 */
74 q7_t in4; /* Input value4 */
77 /*loop Unrolling */
78 blkCnt = blockSize >> 2U;
80 /* Getting the sign of shiftBits */
81 sign = (shiftBits & 0x80);
83 /* If the shift value is positive then do right shift else left shift */
84 if (sign == 0U)
86 /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
87 ** a second loop below computes the remaining 1 to 3 samples. */
88 while (blkCnt > 0U)
90 /* C = A << shiftBits */
91 /* Read 4 inputs */
92 in1 = *pSrc;
93 in2 = *(pSrc + 1);
94 in3 = *(pSrc + 2);
95 in4 = *(pSrc + 3);
97 /* Store the Shifted result in the destination buffer in single cycle by packing the outputs */
98 *__SIMD32(pDst)++ = __PACKq7(__SSAT((in1 << shiftBits), 8),
99 __SSAT((in2 << shiftBits), 8),
100 __SSAT((in3 << shiftBits), 8),
101 __SSAT((in4 << shiftBits), 8));
102 /* Update source pointer to process next sampels */
103 pSrc += 4U;
105 /* Decrement the loop counter */
106 blkCnt--;
109 /* If the blockSize is not a multiple of 4, compute any remaining output samples here.
110 ** No loop unrolling is used. */
111 blkCnt = blockSize % 0x4U;
113 while (blkCnt > 0U)
115 /* C = A << shiftBits */
116 /* Shift the input and then store the result in the destination buffer. */
117 *pDst++ = (q7_t) __SSAT((*pSrc++ << shiftBits), 8);
119 /* Decrement the loop counter */
120 blkCnt--;
123 else
125 shiftBits = -shiftBits;
126 /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
127 ** a second loop below computes the remaining 1 to 3 samples. */
128 while (blkCnt > 0U)
130 /* C = A >> shiftBits */
131 /* Read 4 inputs */
132 in1 = *pSrc;
133 in2 = *(pSrc + 1);
134 in3 = *(pSrc + 2);
135 in4 = *(pSrc + 3);
137 /* Store the Shifted result in the destination buffer in single cycle by packing the outputs */
138 *__SIMD32(pDst)++ = __PACKq7((in1 >> shiftBits), (in2 >> shiftBits),
139 (in3 >> shiftBits), (in4 >> shiftBits));
142 pSrc += 4U;
144 /* Decrement the loop counter */
145 blkCnt--;
148 /* If the blockSize is not a multiple of 4, compute any remaining output samples here.
149 ** No loop unrolling is used. */
150 blkCnt = blockSize % 0x4U;
152 while (blkCnt > 0U)
154 /* C = A >> shiftBits */
155 /* Shift the input and then store the result in the destination buffer. */
156 in1 = *pSrc++;
157 *pDst++ = (in1 >> shiftBits);
159 /* Decrement the loop counter */
160 blkCnt--;
164 #else
166 /* Run the below code for Cortex-M0 */
168 /* Getting the sign of shiftBits */
169 sign = (shiftBits & 0x80);
171 /* If the shift value is positive then do right shift else left shift */
172 if (sign == 0U)
174 /* Initialize blkCnt with number of samples */
175 blkCnt = blockSize;
177 while (blkCnt > 0U)
179 /* C = A << shiftBits */
180 /* Shift the input and then store the result in the destination buffer. */
181 *pDst++ = (q7_t) __SSAT(((q15_t) * pSrc++ << shiftBits), 8);
183 /* Decrement the loop counter */
184 blkCnt--;
187 else
189 /* Initialize blkCnt with number of samples */
190 blkCnt = blockSize;
192 while (blkCnt > 0U)
194 /* C = A >> shiftBits */
195 /* Shift the input and then store the result in the destination buffer. */
196 *pDst++ = (*pSrc++ >> -shiftBits);
198 /* Decrement the loop counter */
199 blkCnt--;
203 #endif /* #if defined (ARM_MATH_DSP) */
207 * @} end of shift group