1 /* ----------------------------------------------------------------------
2 * Project: CMSIS DSP Library
3 * Title: arm_mat_scale_q15.c
4 * Description: Multiplies a Q15 matrix 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/matrix_functions.h"
30
31 /**
32 @ingroup groupMatrix
33 */
34
35 /**
36 @addtogroup MatrixScale
37 @{
38 */
39
40 /**
41 @brief Q15 matrix scaling.
42 @param[in] pSrc points to input matrix
43 @param[in] scaleFract fractional portion of the scale factor
44 @param[in] shift number of bits to shift the result by
45 @param[out] pDst points to output matrix structure
46 @return execution status
47 - \ref ARM_MATH_SUCCESS : Operation successful
48 - \ref ARM_MATH_SIZE_MISMATCH : Matrix size check failed
49
50 @par Scaling and Overflow Behavior
51 The input data <code>*pSrc</code> and <code>scaleFract</code> are in 1.15 format.
52 These are multiplied to yield a 2.30 intermediate result and this is shifted with saturation to 1.15 format.
53 */
54 #if defined(ARM_MATH_MVEI) && !defined(ARM_MATH_AUTOVECTORIZE)
arm_mat_scale_q15(const arm_matrix_instance_q15 * pSrc,q15_t scaleFract,int32_t shift,arm_matrix_instance_q15 * pDst)55 arm_status arm_mat_scale_q15(
56 const arm_matrix_instance_q15 * pSrc,
57 q15_t scaleFract,
58 int32_t shift,
59 arm_matrix_instance_q15 * pDst)
60 {
61 arm_status status; /* Status of matrix scaling */
62 q15_t *pIn = pSrc->pData; /* input data matrix pointer */
63 q15_t *pOut = pDst->pData; /* output data matrix pointer */
64 uint32_t numSamples; /* total number of elements in the matrix */
65 uint32_t blkCnt; /* loop counters */
66 q15x8_t vecIn, vecOut;
67 q15_t const *pInVec;
68 int32_t totShift = shift + 1; /* shift to apply after scaling */
69
70 pInVec = (q15_t const *) pIn;
71
72 #ifdef ARM_MATH_MATRIX_CHECK
73
74 /* Check for matrix mismatch condition */
75 if ((pSrc->numRows != pDst->numRows) ||
76 (pSrc->numCols != pDst->numCols) )
77 {
78 /* Set status as ARM_MATH_SIZE_MISMATCH */
79 status = ARM_MATH_SIZE_MISMATCH;
80 }
81 else
82
83 #endif /* #ifdef ARM_MATH_MATRIX_CHECK */
84
85 {
86 /*
87 * Total number of samples in the input matrix
88 */
89 numSamples = (uint32_t) pSrc->numRows * pSrc->numCols;
90 blkCnt = numSamples >> 3;
91 while (blkCnt > 0U)
92 {
93 /*
94 * C(m,n) = A(m,n) * scale
95 * Scaling and results are stored in the destination buffer.
96 */
97 vecIn = vld1q(pInVec); pInVec += 8;
98
99 /* multiply input with scaler value */
100 vecOut = vmulhq(vecIn, vdupq_n_s16(scaleFract));
101 /* apply shifting */
102 vecOut = vqshlq_r(vecOut, totShift);
103
104 vst1q(pOut, vecOut); pOut += 8;
105
106 /*
107 * Decrement the blockSize loop counter
108 */
109 blkCnt--;
110 }
111 /*
112 * tail
113 * (will be merged thru tail predication)
114 */
115 blkCnt = numSamples & 7;
116 if (blkCnt > 0U)
117 {
118 mve_pred16_t p0 = vctp16q(blkCnt);
119 vecIn = vld1q(pInVec); pInVec += 8;
120 vecOut = vmulhq(vecIn, vdupq_n_s16(scaleFract));
121 vecOut = vqshlq_r(vecOut, totShift);
122 vstrhq_p(pOut, vecOut, p0);
123 }
124 /* Set status as ARM_MATH_SUCCESS */
125 status = ARM_MATH_SUCCESS;
126 }
127
128 /* Return to application */
129 return (status);
130 }
131
132 #else
arm_mat_scale_q15(const arm_matrix_instance_q15 * pSrc,q15_t scaleFract,int32_t shift,arm_matrix_instance_q15 * pDst)133 arm_status arm_mat_scale_q15(
134 const arm_matrix_instance_q15 * pSrc,
135 q15_t scaleFract,
136 int32_t shift,
137 arm_matrix_instance_q15 * pDst)
138 {
139 q15_t *pIn = pSrc->pData; /* Input data matrix pointer */
140 q15_t *pOut = pDst->pData; /* Output data matrix pointer */
141 uint32_t numSamples; /* Total number of elements in the matrix */
142 uint32_t blkCnt; /* Loop counter */
143 arm_status status; /* Status of matrix scaling */
144 int32_t kShift = 15 - shift; /* Total shift to apply after scaling */
145
146 #if defined (ARM_MATH_LOOPUNROLL) && defined (ARM_MATH_DSP)
147 q31_t inA1, inA2;
148 q31_t out1, out2, out3, out4; /* Temporary output variables */
149 q15_t in1, in2, in3, in4; /* Temporary input variables */
150 #endif
151
152 #ifdef ARM_MATH_MATRIX_CHECK
153
154 /* Check for matrix mismatch condition */
155 if ((pSrc->numRows != pDst->numRows) ||
156 (pSrc->numCols != pDst->numCols) )
157 {
158 /* Set status as ARM_MATH_SIZE_MISMATCH */
159 status = ARM_MATH_SIZE_MISMATCH;
160 }
161 else
162
163 #endif /* #ifdef ARM_MATH_MATRIX_CHECK */
164
165 {
166 /* Total number of samples in input matrix */
167 numSamples = (uint32_t) pSrc->numRows * pSrc->numCols;
168
169 #if defined (ARM_MATH_LOOPUNROLL)
170
171 /* Loop unrolling: Compute 4 outputs at a time */
172 blkCnt = numSamples >> 2U;
173
174 while (blkCnt > 0U)
175 {
176 /* C(m,n) = A(m,n) * k */
177
178 #if defined (ARM_MATH_DSP)
179 /* read 2 times 2 samples at a time from source */
180 inA1 = read_q15x2_ia ((q15_t **) &pIn);
181 inA2 = read_q15x2_ia ((q15_t **) &pIn);
182
183 /* Scale inputs and store result in temporary variables
184 * in single cycle by packing the outputs */
185 out1 = (q31_t) ((q15_t) (inA1 >> 16) * scaleFract);
186 out2 = (q31_t) ((q15_t) (inA1 ) * scaleFract);
187 out3 = (q31_t) ((q15_t) (inA2 >> 16) * scaleFract);
188 out4 = (q31_t) ((q15_t) (inA2 ) * scaleFract);
189
190 /* apply shifting */
191 out1 = out1 >> kShift;
192 out2 = out2 >> kShift;
193 out3 = out3 >> kShift;
194 out4 = out4 >> kShift;
195
196 /* saturate the output */
197 in1 = (q15_t) (__SSAT(out1, 16));
198 in2 = (q15_t) (__SSAT(out2, 16));
199 in3 = (q15_t) (__SSAT(out3, 16));
200 in4 = (q15_t) (__SSAT(out4, 16));
201
202 /* store result to destination */
203 write_q15x2_ia (&pOut, __PKHBT(in2, in1, 16));
204 write_q15x2_ia (&pOut, __PKHBT(in4, in3, 16));
205
206 #else
207 *pOut++ = (q15_t) (__SSAT(((q31_t) (*pIn++) * scaleFract) >> kShift, 16));
208 *pOut++ = (q15_t) (__SSAT(((q31_t) (*pIn++) * scaleFract) >> kShift, 16));
209 *pOut++ = (q15_t) (__SSAT(((q31_t) (*pIn++) * scaleFract) >> kShift, 16));
210 *pOut++ = (q15_t) (__SSAT(((q31_t) (*pIn++) * scaleFract) >> kShift, 16));
211 #endif
212
213 /* Decrement loop counter */
214 blkCnt--;
215 }
216
217 /* Loop unrolling: Compute remaining outputs */
218 blkCnt = numSamples % 0x4U;
219
220 #else
221
222 /* Initialize blkCnt with number of samples */
223 blkCnt = numSamples;
224
225 #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
226
227 while (blkCnt > 0U)
228 {
229 /* C(m,n) = A(m,n) * k */
230
231 /* Scale, saturate and store result in destination buffer. */
232 *pOut++ = (q15_t) (__SSAT(((q31_t) (*pIn++) * scaleFract) >> kShift, 16));
233
234 /* Decrement loop counter */
235 blkCnt--;
236 }
237
238 /* Set status as ARM_MATH_SUCCESS */
239 status = ARM_MATH_SUCCESS;
240 }
241
242 /* Return to application */
243 return (status);
244 }
245 #endif /* defined(ARM_MATH_MVEI) */
246
247 /**
248 @} end of MatrixScale group
249 */
250