arm_mat_scale_f32.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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: 18. March 2019
  7. * $Revision: V1.6.0
  8. *
  9. * Target Processor: Cortex-M cores
  10. * -------------------------------------------------------------------- */
  11. /*
  12. * Copyright (C) 2010-2019 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 "arm_math.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_NEON_EXPERIMENTAL)
  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. float32_t *pIn = pSrc->pData; /* input data matrix pointer */
  66. float32_t *pOut = pDst->pData; /* output data matrix pointer */
  67. uint32_t numSamples; /* total number of elements in the matrix */
  68. uint32_t blkCnt; /* loop counters */
  69. arm_status status; /* status of matrix scaling */
  70. float32_t in1, in2, in3, in4; /* temporary variables */
  71. float32_t out1, out2, out3, out4; /* temporary variables */
  72. #ifdef ARM_MATH_MATRIX_CHECK
  73. /* Check for matrix mismatch condition */
  74. if ((pSrc->numRows != pDst->numRows) || (pSrc->numCols != pDst->numCols))
  75. {
  76. /* Set status as ARM_MATH_SIZE_MISMATCH */
  77. status = ARM_MATH_SIZE_MISMATCH;
  78. }
  79. else
  80. #endif /* #ifdef ARM_MATH_MATRIX_CHECK */
  81. {
  82. float32x4_t vec1;
  83. float32x4_t res;
  84. /* Total number of samples in the input matrix */
  85. numSamples = (uint32_t) pSrc->numRows * pSrc->numCols;
  86. blkCnt = numSamples >> 2;
  87. /* Compute 4 outputs at a time.
  88. ** a second loop below computes the remaining 1 to 3 samples. */
  89. while (blkCnt > 0U)
  90. {
  91. /* C(m,n) = A(m,n) * scale */
  92. /* Scaling and results are stored in the destination buffer. */
  93. vec1 = vld1q_f32(pIn);
  94. res = vmulq_f32(vec1, vdupq_n_f32(scale));
  95. vst1q_f32(pOut, res);
  96. /* update pointers to process next sampels */
  97. pIn += 4U;
  98. pOut += 4U;
  99. /* Decrement the numSamples loop counter */
  100. blkCnt--;
  101. }
  102. /* If the numSamples is not a multiple of 4, compute any remaining output samples here.
  103. ** No loop unrolling is used. */
  104. blkCnt = numSamples % 0x4U;
  105. while (blkCnt > 0U)
  106. {
  107. /* C(m,n) = A(m,n) * scale */
  108. /* The results are stored in the destination buffer. */
  109. *pOut++ = (*pIn++) * scale;
  110. /* Decrement the loop counter */
  111. blkCnt--;
  112. }
  113. /* Set status as ARM_MATH_SUCCESS */
  114. status = ARM_MATH_SUCCESS;
  115. }
  116. /* Return to application */
  117. return (status);
  118. }
  119. #else
  120. arm_status arm_mat_scale_f32(
  121. const arm_matrix_instance_f32 * pSrc,
  122. float32_t scale,
  123. arm_matrix_instance_f32 * pDst)
  124. {
  125. float32_t *pIn = pSrc->pData; /* Input data matrix pointer */
  126. float32_t *pOut = pDst->pData; /* Output data matrix pointer */
  127. uint32_t numSamples; /* Total number of elements in the matrix */
  128. uint32_t blkCnt; /* Loop counters */
  129. arm_status status; /* Status of matrix scaling */
  130. #ifdef ARM_MATH_MATRIX_CHECK
  131. /* Check for matrix mismatch condition */
  132. if ((pSrc->numRows != pDst->numRows) ||
  133. (pSrc->numCols != pDst->numCols) )
  134. {
  135. /* Set status as ARM_MATH_SIZE_MISMATCH */
  136. status = ARM_MATH_SIZE_MISMATCH;
  137. }
  138. else
  139. #endif /* #ifdef ARM_MATH_MATRIX_CHECK */
  140. {
  141. /* Total number of samples in input matrix */
  142. numSamples = (uint32_t) pSrc->numRows * pSrc->numCols;
  143. #if defined (ARM_MATH_LOOPUNROLL)
  144. /* Loop unrolling: Compute 4 outputs at a time */
  145. blkCnt = numSamples >> 2U;
  146. while (blkCnt > 0U)
  147. {
  148. /* C(m,n) = A(m,n) * scale */
  149. /* Scale and store result in destination buffer. */
  150. *pOut++ = (*pIn++) * scale;
  151. *pOut++ = (*pIn++) * scale;
  152. *pOut++ = (*pIn++) * scale;
  153. *pOut++ = (*pIn++) * scale;
  154. /* Decrement loop counter */
  155. blkCnt--;
  156. }
  157. /* Loop unrolling: Compute remaining outputs */
  158. blkCnt = numSamples % 0x4U;
  159. #else
  160. /* Initialize blkCnt with number of samples */
  161. blkCnt = numSamples;
  162. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  163. while (blkCnt > 0U)
  164. {
  165. /* C(m,n) = A(m,n) * scale */
  166. /* Scale and store result in destination buffer. */
  167. *pOut++ = (*pIn++) * scale;
  168. /* Decrement loop counter */
  169. blkCnt--;
  170. }
  171. /* Set status as ARM_MATH_SUCCESS */
  172. status = ARM_MATH_SUCCESS;
  173. }
  174. /* Return to application */
  175. return (status);
  176. }
  177. #endif /* #if defined(ARM_MATH_NEON) */
  178. /**
  179. @} end of MatrixScale group
  180. */