1 /* ----------------------------------------------------------------------
2 * Project: CMSIS DSP Library
3 * Title: arm_mat_scale_f32.c
4 * Description: Multiplies a floating-point 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 @defgroup MatrixScale Matrix Scale
37
38 Multiplies a matrix by a scalar. This is accomplished by multiplying each element in the
39 matrix by the scalar. For example:
40
41 @par Matrix Scaling of a 3 x 3 matrix
42
43 \f[
44 \begin{pmatrix}
45 a_{1,1} & a_{1,2} & a_{1,3} \\
46 a_{2,1} & a_{2,2} & a_{2,3} \\
47 a_{3,1} & a_{3,2} & a_{3,3} \\
48 \end{pmatrix}
49 * K =
50 \begin{pmatrix}
51 K a_{1,1} & K a_{1,2} & K a_{1,3} \\
52 K a_{2,1} & K a_{2,2} & K a_{2,3} \\
53 K a_{3,1} & K a_{3,2} & K a_{3,3} \\
54 \end{pmatrix}
55 \f]
56
57 The function checks to make sure that the input and output matrices are of the same size.
58
59 In the fixed-point Q15 and Q31 functions, <code>scale</code> is represented by
60 a fractional multiplication <code>scaleFract</code> and an arithmetic shift <code>shift</code>.
61 The shift allows the gain of the scaling operation to exceed 1.0.
62 The overall scale factor applied to the fixed-point data is
63 <pre>
64 scale = scaleFract * 2^shift.
65 </pre>
66 */
67
68 /**
69 @addtogroup MatrixScale
70 @{
71 */
72
73 /**
74 @brief Floating-point matrix scaling.
75 @param[in] pSrc points to input matrix
76 @param[in] scale scale factor to be applied
77 @param[out] pDst points to output matrix structure
78 @return execution status
79 - \ref ARM_MATH_SUCCESS : Operation successful
80 - \ref ARM_MATH_SIZE_MISMATCH : Matrix size check failed
81 */
82 #if defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE)
arm_mat_scale_f32(const arm_matrix_instance_f32 * pSrc,float32_t scale,arm_matrix_instance_f32 * pDst)83 ARM_DSP_ATTRIBUTE arm_status arm_mat_scale_f32(
84 const arm_matrix_instance_f32 * pSrc,
85 float32_t scale,
86 arm_matrix_instance_f32 * pDst)
87 {
88 arm_status status; /* status of matrix scaling */
89 #ifdef ARM_MATH_MATRIX_CHECK
90 /* Check for matrix mismatch condition */
91 if ((pSrc->numRows != pDst->numRows) || (pSrc->numCols != pDst->numCols))
92 {
93 /* Set status as ARM_MATH_SIZE_MISMATCH */
94 status = ARM_MATH_SIZE_MISMATCH;
95 }
96 else
97 #endif /* #ifdef ARM_MATH_MATRIX_CHECK */
98 {
99 float32_t *pIn = pSrc->pData; /* input data matrix pointer */
100 float32_t *pOut = pDst->pData; /* output data matrix pointer */
101 uint32_t numSamples; /* total number of elements in the matrix */
102 uint32_t blkCnt; /* loop counters */
103 f32x4_t vecIn, vecOut;
104 float32_t const *pInVec;
105
106 pInVec = (float32_t const *) pIn;
107 /*
108 * Total number of samples in the input matrix
109 */
110 numSamples = (uint32_t) pSrc->numRows * pSrc->numCols;
111 blkCnt = numSamples >> 2;
112 while (blkCnt > 0U)
113 {
114 /*
115 * C(m,n) = A(m,n) * scale
116 * Scaling and results are stored in the destination buffer.
117 */
118 vecIn = vld1q(pInVec);
119 pInVec += 4;
120
121 vecOut = vecIn * scale;
122
123 vst1q(pOut, vecOut);
124 pOut += 4;
125 /*
126 * Decrement the blockSize loop counter
127 */
128 blkCnt--;
129 }
130 /*
131 * tail
132 */
133 blkCnt = numSamples & 3;
134 if (blkCnt > 0U)
135 {
136 mve_pred16_t p0 = vctp32q(blkCnt);
137 vecIn = vld1q(pInVec);
138 vecOut = vecIn * scale;
139
140 vstrwq_p(pOut, vecOut, p0);
141 }
142 /* Set status as ARM_MATH_SUCCESS */
143 status = ARM_MATH_SUCCESS;
144 }
145
146 /* Return to application */
147 return (status);
148
149 }
150 #else
151 #if defined(ARM_MATH_NEON_EXPERIMENTAL)
arm_mat_scale_f32(const arm_matrix_instance_f32 * pSrc,float32_t scale,arm_matrix_instance_f32 * pDst)152 ARM_DSP_ATTRIBUTE arm_status arm_mat_scale_f32(
153 const arm_matrix_instance_f32 * pSrc,
154 float32_t scale,
155 arm_matrix_instance_f32 * pDst)
156 {
157 float32_t *pIn = pSrc->pData; /* input data matrix pointer */
158 float32_t *pOut = pDst->pData; /* output data matrix pointer */
159 uint32_t numSamples; /* total number of elements in the matrix */
160 uint32_t blkCnt; /* loop counters */
161 arm_status status; /* status of matrix scaling */
162
163
164 #ifdef ARM_MATH_MATRIX_CHECK
165 /* Check for matrix mismatch condition */
166 if ((pSrc->numRows != pDst->numRows) || (pSrc->numCols != pDst->numCols))
167 {
168 /* Set status as ARM_MATH_SIZE_MISMATCH */
169 status = ARM_MATH_SIZE_MISMATCH;
170 }
171 else
172 #endif /* #ifdef ARM_MATH_MATRIX_CHECK */
173 {
174 float32x4_t vec1;
175 float32x4_t res;
176
177 /* Total number of samples in the input matrix */
178 numSamples = (uint32_t) pSrc->numRows * pSrc->numCols;
179
180 blkCnt = numSamples >> 2;
181
182 /* Compute 4 outputs at a time.
183 ** a second loop below computes the remaining 1 to 3 samples. */
184 while (blkCnt > 0U)
185 {
186 /* C(m,n) = A(m,n) * scale */
187 /* Scaling and results are stored in the destination buffer. */
188 vec1 = vld1q_f32(pIn);
189 res = vmulq_f32(vec1, vdupq_n_f32(scale));
190 vst1q_f32(pOut, res);
191
192 /* update pointers to process next sampels */
193 pIn += 4U;
194 pOut += 4U;
195
196 /* Decrement the numSamples loop counter */
197 blkCnt--;
198 }
199
200 /* If the numSamples is not a multiple of 4, compute any remaining output samples here.
201 ** No loop unrolling is used. */
202 blkCnt = numSamples % 0x4U;
203
204 while (blkCnt > 0U)
205 {
206 /* C(m,n) = A(m,n) * scale */
207 /* The results are stored in the destination buffer. */
208 *pOut++ = (*pIn++) * scale;
209
210 /* Decrement the loop counter */
211 blkCnt--;
212 }
213
214 /* Set status as ARM_MATH_SUCCESS */
215 status = ARM_MATH_SUCCESS;
216 }
217
218 /* Return to application */
219 return (status);
220 }
221 #else
arm_mat_scale_f32(const arm_matrix_instance_f32 * pSrc,float32_t scale,arm_matrix_instance_f32 * pDst)222 ARM_DSP_ATTRIBUTE arm_status arm_mat_scale_f32(
223 const arm_matrix_instance_f32 * pSrc,
224 float32_t scale,
225 arm_matrix_instance_f32 * pDst)
226 {
227 float32_t *pIn = pSrc->pData; /* Input data matrix pointer */
228 float32_t *pOut = pDst->pData; /* Output data matrix pointer */
229 uint32_t numSamples; /* Total number of elements in the matrix */
230 uint32_t blkCnt; /* Loop counters */
231 arm_status status; /* Status of matrix scaling */
232
233 #ifdef ARM_MATH_MATRIX_CHECK
234
235 /* Check for matrix mismatch condition */
236 if ((pSrc->numRows != pDst->numRows) ||
237 (pSrc->numCols != pDst->numCols) )
238 {
239 /* Set status as ARM_MATH_SIZE_MISMATCH */
240 status = ARM_MATH_SIZE_MISMATCH;
241 }
242 else
243
244 #endif /* #ifdef ARM_MATH_MATRIX_CHECK */
245
246 {
247 /* Total number of samples in input matrix */
248 numSamples = (uint32_t) pSrc->numRows * pSrc->numCols;
249
250 #if defined (ARM_MATH_LOOPUNROLL)
251
252 /* Loop unrolling: Compute 4 outputs at a time */
253 blkCnt = numSamples >> 2U;
254
255 while (blkCnt > 0U)
256 {
257 /* C(m,n) = A(m,n) * scale */
258
259 /* Scale and store result in destination buffer. */
260 *pOut++ = (*pIn++) * scale;
261 *pOut++ = (*pIn++) * scale;
262 *pOut++ = (*pIn++) * scale;
263 *pOut++ = (*pIn++) * scale;
264
265 /* Decrement loop counter */
266 blkCnt--;
267 }
268
269 /* Loop unrolling: Compute remaining outputs */
270 blkCnt = numSamples % 0x4U;
271
272 #else
273
274 /* Initialize blkCnt with number of samples */
275 blkCnt = numSamples;
276
277 #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
278
279 while (blkCnt > 0U)
280 {
281 /* C(m,n) = A(m,n) * scale */
282
283 /* Scale and store result in destination buffer. */
284 *pOut++ = (*pIn++) * scale;
285
286 /* Decrement loop counter */
287 blkCnt--;
288 }
289
290 /* Set status as ARM_MATH_SUCCESS */
291 status = ARM_MATH_SUCCESS;
292 }
293
294 /* Return to application */
295 return (status);
296 }
297 #endif /* #if defined(ARM_MATH_NEON) */
298 #endif /* defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE) */
299
300 /**
301 @} end of MatrixScale group
302 */
303