arm_mat_scale_f32.c 8.2 KB

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