arm_mat_scale_f32.c 8.4 KB

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