1 /* ----------------------------------------------------------------------
2 * Project: CMSIS DSP Library
3 * Title: arm_scale_q15.c
4 * Description: Multiplies a Q15 vector by a scalar
5 *
6 * $Date: 23 April 2021
7 * $Revision: V1.9.0
8 *
9 * Target Processor: Cortex-M and Cortex-A cores
10 * -------------------------------------------------------------------- */
11 /*
12 * Copyright (C) 2010-2021 ARM Limited or its affiliates. All rights reserved.
13 *
14 * SPDX-License-Identifier: Apache-2.0
15 *
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
19 *
20 * www.apache.org/licenses/LICENSE-2.0
21 *
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.
27 */
28
29 #include "dsp/basic_math_functions.h"
30
31 /**
32 @ingroup groupMath
33 */
34
35 /**
36 @addtogroup BasicScale
37 @{
38 */
39
40 /**
41 @brief Multiplies a Q15 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 each vector
47
48 @par Scaling and Overflow Behavior
49 The input data <code>*pSrc</code> and <code>scaleFract</code> are in 1.15 format.
50 These are multiplied to yield a 2.30 intermediate result and this is shifted with saturation to 1.15 format.
51 */
52
53 #if defined(ARM_MATH_MVEI) && !defined(ARM_MATH_AUTOVECTORIZE)
54
55 #include "arm_helium_utils.h"
56
arm_scale_q15(const q15_t * pSrc,q15_t scaleFract,int8_t shift,q15_t * pDst,uint32_t blockSize)57 ARM_DSP_ATTRIBUTE void arm_scale_q15(
58 const q15_t * pSrc,
59 q15_t scaleFract,
60 int8_t shift,
61 q15_t * pDst,
62 uint32_t blockSize)
63 {
64 uint32_t blkCnt; /* loop counters */
65 q15x8_t vecSrc;
66 q15x8_t vecDst = { 0 };
67 q31x4_t low, high;
68
69 /* Compute 8 outputs at a time */
70 blkCnt = blockSize >> 3;
71
72 while (blkCnt > 0U)
73 {
74 /*
75 * C = A * scale
76 * Scale the input and then store the result in the destination buffer.
77 */
78 vecSrc = vld1q(pSrc);
79 low = vmullbq_int(vecSrc, vdupq_n_s16(scaleFract));
80 low = vqshlq_r(low, shift);
81 vecDst = vqshrnbq_n_s32(vecDst,low,15);
82
83 high = vmulltq_int(vecSrc, vdupq_n_s16(scaleFract));
84 high = vqshlq_r(high, shift);
85 vecDst = vqshrntq_n_s32(vecDst,high,15);
86
87 vst1q(pDst, vecDst);
88 /*
89 * Decrement the blockSize loop counter
90 */
91 blkCnt--;
92 /*
93 * advance vector source and destination pointers
94 */
95 pSrc += 8;
96 pDst += 8;
97 }
98 /*
99 * tail
100 */
101 blkCnt = blockSize & 7;
102 if (blkCnt > 0U)
103 {
104 mve_pred16_t p0 = vctp16q(blkCnt);
105 vecSrc = vld1q(pSrc);
106 low = vmullbq_int(vecSrc, vdupq_n_s16(scaleFract));
107 low = vqshlq_r(low, shift);
108 vecDst = vqshrnbq_n_s32(vecDst,low,15);
109
110 high = vmulltq_int(vecSrc, vdupq_n_s16(scaleFract));
111 high = vqshlq_r(high, shift);
112 vecDst = vqshrntq_n_s32(vecDst,high,15);
113 vstrhq_p(pDst, vecDst, p0);
114 }
115
116 }
117
118
119 #else
arm_scale_q15(const q15_t * pSrc,q15_t scaleFract,int8_t shift,q15_t * pDst,uint32_t blockSize)120 ARM_DSP_ATTRIBUTE void arm_scale_q15(
121 const q15_t *pSrc,
122 q15_t scaleFract,
123 int8_t shift,
124 q15_t *pDst,
125 uint32_t blockSize)
126 {
127 uint32_t blkCnt; /* Loop counter */
128 int8_t kShift = 15 - shift; /* Shift to apply after scaling */
129
130 #if defined (ARM_MATH_LOOPUNROLL)
131 #if defined (ARM_MATH_DSP)
132 q31_t inA1, inA2;
133 q31_t out1, out2, out3, out4; /* Temporary output variables */
134 q15_t in1, in2, in3, in4; /* Temporary input variables */
135 #endif
136 #endif
137
138 #if defined (ARM_MATH_LOOPUNROLL)
139
140 /* Loop unrolling: Compute 4 outputs at a time */
141 blkCnt = blockSize >> 2U;
142
143 while (blkCnt > 0U)
144 {
145 /* C = A * scale */
146
147 #if defined (ARM_MATH_DSP)
148 /* read 2 times 2 samples at a time from source */
149 inA1 = read_q15x2_ia (&pSrc);
150 inA2 = read_q15x2_ia (&pSrc);
151
152 /* Scale inputs and store result in temporary variables
153 * in single cycle by packing the outputs */
154 out1 = (q31_t) ((q15_t) (inA1 >> 16) * scaleFract);
155 out2 = (q31_t) ((q15_t) (inA1 ) * scaleFract);
156 out3 = (q31_t) ((q15_t) (inA2 >> 16) * scaleFract);
157 out4 = (q31_t) ((q15_t) (inA2 ) * scaleFract);
158
159 /* apply shifting */
160 out1 = out1 >> kShift;
161 out2 = out2 >> kShift;
162 out3 = out3 >> kShift;
163 out4 = out4 >> kShift;
164
165 /* saturate the output */
166 in1 = (q15_t) (__SSAT(out1, 16));
167 in2 = (q15_t) (__SSAT(out2, 16));
168 in3 = (q15_t) (__SSAT(out3, 16));
169 in4 = (q15_t) (__SSAT(out4, 16));
170
171 /* store result to destination */
172 write_q15x2_ia (&pDst, __PKHBT(in2, in1, 16));
173 write_q15x2_ia (&pDst, __PKHBT(in4, in3, 16));
174 #else
175 *pDst++ = (q15_t) (__SSAT(((q31_t) *pSrc++ * scaleFract) >> kShift, 16));
176 *pDst++ = (q15_t) (__SSAT(((q31_t) *pSrc++ * scaleFract) >> kShift, 16));
177 *pDst++ = (q15_t) (__SSAT(((q31_t) *pSrc++ * scaleFract) >> kShift, 16));
178 *pDst++ = (q15_t) (__SSAT(((q31_t) *pSrc++ * scaleFract) >> kShift, 16));
179 #endif
180
181 /* Decrement loop counter */
182 blkCnt--;
183 }
184
185 /* Loop unrolling: Compute remaining outputs */
186 blkCnt = blockSize % 0x4U;
187
188 #else
189
190 /* Initialize blkCnt with number of samples */
191 blkCnt = blockSize;
192
193 #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
194
195 while (blkCnt > 0U)
196 {
197 /* C = A * scale */
198
199 /* Scale input and store result in destination buffer. */
200 *pDst++ = (q15_t) (__SSAT(((q31_t) *pSrc++ * scaleFract) >> kShift, 16));
201
202 /* Decrement loop counter */
203 blkCnt--;
204 }
205
206 }
207 #endif /* defined(ARM_MATH_MVEI) */
208
209 /**
210 @} end of BasicScale group
211 */
212