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
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.
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
49 * <b>Scaling and Overflow Behavior:</b>
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.
62 int8_t kShift
= shift
+ 1; /* Shift to apply after scaling */
63 int8_t sign
= (kShift
& 0x80);
64 uint32_t blkCnt
; /* loop counter */
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 */
76 blkCnt
= blockSize
>> 2U;
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. */
84 /* read four inputs from source */
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;
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
;
113 if (in3
!= (out3
>> kShift
))
114 out3
= 0x7FFFFFFF ^ (in3
>> 31);
116 if (in4
!= (out4
>> kShift
))
117 out4
= 0x7FFFFFFF ^ (in4
>> 31);
119 /* Store result destination */
123 /* Update pointers to process next sampels */
127 /* Decrement the loop counter */
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. */
138 /* read four inputs from source */
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;
151 out1
= in1
>> -kShift
;
152 out2
= in2
>> -kShift
;
154 out3
= in3
>> -kShift
;
155 out4
= in4
>> -kShift
;
157 /* Store result destination */
164 /* Update pointers to process next sampels */
168 /* Decrement the loop counter */
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
;
178 /* Run the below code for Cortex-M0 */
180 /* Initialize blkCnt with number of samples */
183 #endif /* #if defined (ARM_MATH_DSP) */
190 /* Scale the input and then store the result in the destination buffer. */
192 in
= ((q63_t
) in
* scaleFract
) >> 32;
196 if (in
!= (out
>> kShift
))
197 out
= 0x7FFFFFFF ^ (in
>> 31);
201 /* Decrement the loop counter */
210 /* Scale the input and then store the result in the destination buffer. */
212 in
= ((q63_t
) in
* scaleFract
) >> 32;
218 /* Decrement the loop counter */
226 * @} end of scale group