arm_mat_scale_f32.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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: 27. January 2017
  7. * $Revision: V.1.5.1
  8. *
  9. * Target Processor: Cortex-M cores
  10. * -------------------------------------------------------------------- */
  11. /*
  12. * Copyright (C) 2010-2017 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. *
  35. * Multiplies a matrix by a scalar. This is accomplished by multiplying each element in the
  36. * matrix by the scalar. For example:
  37. * \image html MatrixScale.gif "Matrix Scaling of a 3 x 3 matrix"
  38. *
  39. * The function checks to make sure that the input and output matrices are of the same size.
  40. *
  41. * In the fixed-point Q15 and Q31 functions, <code>scale</code> is represented by
  42. * a fractional multiplication <code>scaleFract</code> and an arithmetic shift <code>shift</code>.
  43. * The shift allows the gain of the scaling operation to exceed 1.0.
  44. * The overall scale factor applied to the fixed-point data is
  45. * <pre>
  46. * scale = scaleFract * 2^shift.
  47. * </pre>
  48. */
  49. /**
  50. * @addtogroup MatrixScale
  51. * @{
  52. */
  53. /**
  54. * @brief Floating-point matrix scaling.
  55. * @param[in] *pSrc points to input matrix structure
  56. * @param[in] scale scale factor to be applied
  57. * @param[out] *pDst points to output matrix structure
  58. * @return The function returns either <code>ARM_MATH_SIZE_MISMATCH</code>
  59. * or <code>ARM_MATH_SUCCESS</code> based on the outcome of size checking.
  60. *
  61. */
  62. arm_status arm_mat_scale_f32(
  63. const arm_matrix_instance_f32 * pSrc,
  64. float32_t scale,
  65. arm_matrix_instance_f32 * pDst)
  66. {
  67. float32_t *pIn = pSrc->pData; /* input data matrix pointer */
  68. float32_t *pOut = pDst->pData; /* output data matrix pointer */
  69. uint32_t numSamples; /* total number of elements in the matrix */
  70. uint32_t blkCnt; /* loop counters */
  71. arm_status status; /* status of matrix scaling */
  72. #if defined (ARM_MATH_DSP)
  73. float32_t in1, in2, in3, in4; /* temporary variables */
  74. float32_t out1, out2, out3, out4; /* temporary variables */
  75. #endif // #if defined (ARM_MATH_DSP)
  76. #ifdef ARM_MATH_MATRIX_CHECK
  77. /* Check for matrix mismatch condition */
  78. if ((pSrc->numRows != pDst->numRows) || (pSrc->numCols != pDst->numCols))
  79. {
  80. /* Set status as ARM_MATH_SIZE_MISMATCH */
  81. status = ARM_MATH_SIZE_MISMATCH;
  82. }
  83. else
  84. #endif /* #ifdef ARM_MATH_MATRIX_CHECK */
  85. {
  86. /* Total number of samples in the input matrix */
  87. numSamples = (uint32_t) pSrc->numRows * pSrc->numCols;
  88. #if defined (ARM_MATH_DSP)
  89. /* Run the below code for Cortex-M4 and Cortex-M3 */
  90. /* Loop Unrolling */
  91. blkCnt = numSamples >> 2;
  92. /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
  93. ** a second loop below computes the remaining 1 to 3 samples. */
  94. while (blkCnt > 0U)
  95. {
  96. /* C(m,n) = A(m,n) * scale */
  97. /* Scaling and results are stored in the destination buffer. */
  98. in1 = pIn[0];
  99. in2 = pIn[1];
  100. in3 = pIn[2];
  101. in4 = pIn[3];
  102. out1 = in1 * scale;
  103. out2 = in2 * scale;
  104. out3 = in3 * scale;
  105. out4 = in4 * scale;
  106. pOut[0] = out1;
  107. pOut[1] = out2;
  108. pOut[2] = out3;
  109. pOut[3] = out4;
  110. /* update pointers to process next sampels */
  111. pIn += 4U;
  112. pOut += 4U;
  113. /* Decrement the numSamples loop counter */
  114. blkCnt--;
  115. }
  116. /* If the numSamples is not a multiple of 4, compute any remaining output samples here.
  117. ** No loop unrolling is used. */
  118. blkCnt = numSamples % 0x4U;
  119. #else
  120. /* Run the below code for Cortex-M0 */
  121. /* Initialize blkCnt with number of samples */
  122. blkCnt = numSamples;
  123. #endif /* #if defined (ARM_MATH_DSP) */
  124. while (blkCnt > 0U)
  125. {
  126. /* C(m,n) = A(m,n) * scale */
  127. /* The results are stored in the destination buffer. */
  128. *pOut++ = (*pIn++) * scale;
  129. /* Decrement the loop counter */
  130. blkCnt--;
  131. }
  132. /* Set status as ARM_MATH_SUCCESS */
  133. status = ARM_MATH_SUCCESS;
  134. }
  135. /* Return to application */
  136. return (status);
  137. }
  138. /**
  139. * @} end of MatrixScale group
  140. */