1 /* ----------------------------------------------------------------------
2 * Project: CMSIS DSP Library
3 * Title: arm_scale_q7.c
4 * Description: Multiplies a Q7 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 Q7 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.7 format.
50 These are multiplied to yield a 2.14 intermediate result and this is shifted with saturation to 1.7 format.
51 */
52
53 #if defined(ARM_MATH_MVEI) && !defined(ARM_MATH_AUTOVECTORIZE)
54
55 #include "arm_helium_utils.h"
56
arm_scale_q7(const q7_t * pSrc,q7_t scaleFract,int8_t shift,q7_t * pDst,uint32_t blockSize)57 ARM_DSP_ATTRIBUTE void arm_scale_q7(
58 const q7_t * pSrc,
59 q7_t scaleFract,
60 int8_t shift,
61 q7_t * pDst,
62 uint32_t blockSize)
63 {
64 uint32_t blkCnt; /* loop counters */
65 q7x16_t vecSrc;
66 q7x16_t vecDst = { 0 };
67 q15x8_t low, high;
68
69
70 /* Compute 16 outputs at a time */
71 blkCnt = blockSize >> 4;
72
73 while (blkCnt > 0U)
74 {
75 /*
76 * C = A * scale
77 * Scale the input and then store the result in the destination buffer.
78 */
79 vecSrc = vld1q(pSrc);
80
81 low = vmullbq_int(vecSrc, vdupq_n_s8(scaleFract));
82 low = vqshlq_r(low, shift);
83 vecDst = vqshrnbq_n_s16(vecDst,low,7);
84 high = vmulltq_int(vecSrc, vdupq_n_s8(scaleFract));
85 high = vqshlq_r(high, shift);
86 vecDst = vqshrntq_n_s16(vecDst,high,7);
87
88 vst1q(pDst, vecDst);
89 /*
90 * Decrement the blockSize loop counter
91 */
92 blkCnt--;
93 /*
94 * advance vector source and destination pointers
95 */
96 pSrc += 16;
97 pDst += 16;
98 }
99 /*
100 * tail
101 */
102 blkCnt = blockSize & 0xF;
103 if (blkCnt > 0U)
104 {
105 mve_pred16_t p0 = vctp8q(blkCnt);
106 vecSrc = vld1q(pSrc);
107 low = vmullbq_int_s8(vecSrc, vdupq_n_s8(scaleFract));
108 low = vqshlq_r(low, shift);
109 vecDst = vqshrnbq_n_s16(vecDst,low,7);
110
111 high = vmulltq_int_s8(vecSrc, vdupq_n_s8(scaleFract));
112 high = vqshlq_r(high, shift);
113 vecDst = vqshrntq_n_s16(vecDst,high,7);
114
115 /* narrowing & merge */
116 vstrbq_p_s8(pDst, vecDst, p0);
117 }
118
119 }
120
121 #else
arm_scale_q7(const q7_t * pSrc,q7_t scaleFract,int8_t shift,q7_t * pDst,uint32_t blockSize)122 ARM_DSP_ATTRIBUTE void arm_scale_q7(
123 const q7_t * pSrc,
124 q7_t scaleFract,
125 int8_t shift,
126 q7_t * pDst,
127 uint32_t blockSize)
128 {
129 uint32_t blkCnt; /* Loop counter */
130 int8_t kShift = 7 - shift; /* Shift to apply after scaling */
131
132 #if defined (ARM_MATH_LOOPUNROLL)
133
134 #if defined (ARM_MATH_DSP)
135 q7_t in1, in2, in3, in4; /* Temporary input variables */
136 q7_t out1, out2, out3, out4; /* Temporary output variables */
137 #endif
138
139 /* Loop unrolling: Compute 4 outputs at a time */
140 blkCnt = blockSize >> 2U;
141
142 while (blkCnt > 0U)
143 {
144 /* C = A * scale */
145
146 #if defined (ARM_MATH_DSP)
147 /* Reading 4 inputs from memory */
148 in1 = *pSrc++;
149 in2 = *pSrc++;
150 in3 = *pSrc++;
151 in4 = *pSrc++;
152
153 /* Scale inputs and store result in the temporary variable. */
154 out1 = (q7_t) (__SSAT(((in1) * scaleFract) >> kShift, 8));
155 out2 = (q7_t) (__SSAT(((in2) * scaleFract) >> kShift, 8));
156 out3 = (q7_t) (__SSAT(((in3) * scaleFract) >> kShift, 8));
157 out4 = (q7_t) (__SSAT(((in4) * scaleFract) >> kShift, 8));
158
159 /* Pack and store result in destination buffer (in single write) */
160 write_q7x4_ia (&pDst, __PACKq7(out1, out2, out3, out4));
161 #else
162 *pDst++ = (q7_t) (__SSAT((((q15_t) *pSrc++ * scaleFract) >> kShift), 8));
163 *pDst++ = (q7_t) (__SSAT((((q15_t) *pSrc++ * scaleFract) >> kShift), 8));
164 *pDst++ = (q7_t) (__SSAT((((q15_t) *pSrc++ * scaleFract) >> kShift), 8));
165 *pDst++ = (q7_t) (__SSAT((((q15_t) *pSrc++ * scaleFract) >> kShift), 8));
166 #endif
167
168 /* Decrement loop counter */
169 blkCnt--;
170 }
171
172 /* Loop unrolling: Compute remaining outputs */
173 blkCnt = blockSize % 0x4U;
174
175 #else
176
177 /* Initialize blkCnt with number of samples */
178 blkCnt = blockSize;
179
180 #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
181
182 while (blkCnt > 0U)
183 {
184 /* C = A * scale */
185
186 /* Scale input and store result in destination buffer. */
187 *pDst++ = (q7_t) (__SSAT((((q15_t) *pSrc++ * scaleFract) >> kShift), 8));
188
189 /* Decrement loop counter */
190 blkCnt--;
191 }
192
193 }
194 #endif /* defined(ARM_MATH_MVEI) */
195
196 /**
197 @} end of BasicScale group
198 */
199