Merge pull request #11270 from haslinghuis/rename_attr
[betaflight.git] / lib / main / CMSIS / DSP / Source / BasicMathFunctions / arm_scale_q31.c
blobe89524d7ed6ac5d00ddded9daa4b4ce51fd89853
1 /* ----------------------------------------------------------------------
2 * Project: CMSIS DSP Library
3 * Title: arm_scale_q31.c
4 * Description: Multiplies a Q31 vector by a scalar
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 scale
37 * @{
40 /**
41 * @brief Multiplies a Q31 vector by a scalar.
42 * @param[in] *pSrc points to the input vector
43 * @param[in] scaleFract fractional portion of the scale value
44 * @param[in] shift number of bits to shift the result by
45 * @param[out] *pDst points to the output vector
46 * @param[in] blockSize number of samples in the vector
47 * @return none.
49 * <b>Scaling and Overflow Behavior:</b>
50 * \par
51 * The input data <code>*pSrc</code> and <code>scaleFract</code> are in 1.31 format.
52 * These are multiplied to yield a 2.62 intermediate result and this is shifted with saturation to 1.31 format.
55 void arm_scale_q31(
56 q31_t * pSrc,
57 q31_t scaleFract,
58 int8_t shift,
59 q31_t * pDst,
60 uint32_t blockSize)
62 int8_t kShift = shift + 1; /* Shift to apply after scaling */
63 int8_t sign = (kShift & 0x80);
64 uint32_t blkCnt; /* loop counter */
65 q31_t in, out;
67 #if defined (ARM_MATH_DSP)
69 /* Run the below code for Cortex-M4 and Cortex-M3 */
71 q31_t in1, in2, in3, in4; /* temporary input variables */
72 q31_t out1, out2, out3, out4; /* temporary output variabels */
75 /*loop Unrolling */
76 blkCnt = blockSize >> 2U;
78 if (sign == 0U)
80 /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
81 ** a second loop below computes the remaining 1 to 3 samples. */
82 while (blkCnt > 0U)
84 /* read four inputs from source */
85 in1 = *pSrc;
86 in2 = *(pSrc + 1);
87 in3 = *(pSrc + 2);
88 in4 = *(pSrc + 3);
90 /* multiply input with scaler value */
91 in1 = ((q63_t) in1 * scaleFract) >> 32;
92 in2 = ((q63_t) in2 * scaleFract) >> 32;
93 in3 = ((q63_t) in3 * scaleFract) >> 32;
94 in4 = ((q63_t) in4 * scaleFract) >> 32;
96 /* apply shifting */
97 out1 = in1 << kShift;
98 out2 = in2 << kShift;
100 /* saturate the results. */
101 if (in1 != (out1 >> kShift))
102 out1 = 0x7FFFFFFF ^ (in1 >> 31);
104 if (in2 != (out2 >> kShift))
105 out2 = 0x7FFFFFFF ^ (in2 >> 31);
107 out3 = in3 << kShift;
108 out4 = in4 << kShift;
110 *pDst = out1;
111 *(pDst + 1) = out2;
113 if (in3 != (out3 >> kShift))
114 out3 = 0x7FFFFFFF ^ (in3 >> 31);
116 if (in4 != (out4 >> kShift))
117 out4 = 0x7FFFFFFF ^ (in4 >> 31);
119 /* Store result destination */
120 *(pDst + 2) = out3;
121 *(pDst + 3) = out4;
123 /* Update pointers to process next sampels */
124 pSrc += 4U;
125 pDst += 4U;
127 /* Decrement the loop counter */
128 blkCnt--;
132 else
134 /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
135 ** a second loop below computes the remaining 1 to 3 samples. */
136 while (blkCnt > 0U)
138 /* read four inputs from source */
139 in1 = *pSrc;
140 in2 = *(pSrc + 1);
141 in3 = *(pSrc + 2);
142 in4 = *(pSrc + 3);
144 /* multiply input with scaler value */
145 in1 = ((q63_t) in1 * scaleFract) >> 32;
146 in2 = ((q63_t) in2 * scaleFract) >> 32;
147 in3 = ((q63_t) in3 * scaleFract) >> 32;
148 in4 = ((q63_t) in4 * scaleFract) >> 32;
150 /* apply shifting */
151 out1 = in1 >> -kShift;
152 out2 = in2 >> -kShift;
154 out3 = in3 >> -kShift;
155 out4 = in4 >> -kShift;
157 /* Store result destination */
158 *pDst = out1;
159 *(pDst + 1) = out2;
161 *(pDst + 2) = out3;
162 *(pDst + 3) = out4;
164 /* Update pointers to process next sampels */
165 pSrc += 4U;
166 pDst += 4U;
168 /* Decrement the loop counter */
169 blkCnt--;
172 /* If the blockSize is not a multiple of 4, compute any remaining output samples here.
173 ** No loop unrolling is used. */
174 blkCnt = blockSize % 0x4U;
176 #else
178 /* Run the below code for Cortex-M0 */
180 /* Initialize blkCnt with number of samples */
181 blkCnt = blockSize;
183 #endif /* #if defined (ARM_MATH_DSP) */
185 if (sign == 0)
187 while (blkCnt > 0U)
189 /* C = A * scale */
190 /* Scale the input and then store the result in the destination buffer. */
191 in = *pSrc++;
192 in = ((q63_t) in * scaleFract) >> 32;
194 out = in << kShift;
196 if (in != (out >> kShift))
197 out = 0x7FFFFFFF ^ (in >> 31);
199 *pDst++ = out;
201 /* Decrement the loop counter */
202 blkCnt--;
205 else
207 while (blkCnt > 0U)
209 /* C = A * scale */
210 /* Scale the input and then store the result in the destination buffer. */
211 in = *pSrc++;
212 in = ((q63_t) in * scaleFract) >> 32;
214 out = in >> -kShift;
216 *pDst++ = out;
218 /* Decrement the loop counter */
219 blkCnt--;
226 * @} end of scale group