arm_mat_scale_f32.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /* ----------------------------------------------------------------------
  2. * Copyright (C) 2010-2014 ARM Limited. All rights reserved.
  3. *
  4. * $Date: 31. July 2014
  5. * $Revision: V1.4.4
  6. *
  7. * Project: CMSIS DSP Library
  8. * Title: arm_mat_scale_f32.c
  9. *
  10. * Description: Multiplies a floating-point matrix by a scalar.
  11. *
  12. * Target Processor: Cortex-M4/Cortex-M3/Cortex-M0
  13. *
  14. * Redistribution and use in source and binary forms, with or without
  15. * modification, are permitted provided that the following conditions
  16. * are met:
  17. * - Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. * - Redistributions in binary form must reproduce the above copyright
  20. * notice, this list of conditions and the following disclaimer in
  21. * the documentation and/or other materials provided with the
  22. * distribution.
  23. * - Neither the name of ARM LIMITED nor the names of its contributors
  24. * may be used to endorse or promote products derived from this
  25. * software without specific prior written permission.
  26. *
  27. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  28. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  29. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  30. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  31. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  32. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  33. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  34. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  35. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  37. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  38. * POSSIBILITY OF SUCH DAMAGE.
  39. * -------------------------------------------------------------------- */
  40. #include "arm_math.h"
  41. /**
  42. * @ingroup groupMatrix
  43. */
  44. /**
  45. * @defgroup MatrixScale Matrix Scale
  46. *
  47. * Multiplies a matrix by a scalar. This is accomplished by multiplying each element in the
  48. * matrix by the scalar. For example:
  49. * \image html MatrixScale.gif "Matrix Scaling of a 3 x 3 matrix"
  50. *
  51. * The function checks to make sure that the input and output matrices are of the same size.
  52. *
  53. * In the fixed-point Q15 and Q31 functions, <code>scale</code> is represented by
  54. * a fractional multiplication <code>scaleFract</code> and an arithmetic shift <code>shift</code>.
  55. * The shift allows the gain of the scaling operation to exceed 1.0.
  56. * The overall scale factor applied to the fixed-point data is
  57. * <pre>
  58. * scale = scaleFract * 2^shift.
  59. * </pre>
  60. */
  61. /**
  62. * @addtogroup MatrixScale
  63. * @{
  64. */
  65. /**
  66. * @brief Floating-point matrix scaling.
  67. * @param[in] *pSrc points to input matrix structure
  68. * @param[in] scale scale factor to be applied
  69. * @param[out] *pDst points to output matrix structure
  70. * @return The function returns either <code>ARM_MATH_SIZE_MISMATCH</code>
  71. * or <code>ARM_MATH_SUCCESS</code> based on the outcome of size checking.
  72. *
  73. */
  74. arm_status arm_mat_scale_f32(
  75. const arm_matrix_instance_f32 * pSrc,
  76. float32_t scale,
  77. arm_matrix_instance_f32 * pDst)
  78. {
  79. float32_t *pIn = pSrc->pData; /* input data matrix pointer */
  80. float32_t *pOut = pDst->pData; /* output data matrix pointer */
  81. uint32_t numSamples; /* total number of elements in the matrix */
  82. uint32_t blkCnt; /* loop counters */
  83. arm_status status; /* status of matrix scaling */
  84. #ifndef ARM_MATH_CM0_FAMILY
  85. float32_t in1, in2, in3, in4; /* temporary variables */
  86. float32_t out1, out2, out3, out4; /* temporary variables */
  87. #endif // #ifndef ARM_MATH_CM0_FAMILY
  88. #ifdef ARM_MATH_MATRIX_CHECK
  89. /* Check for matrix mismatch condition */
  90. if((pSrc->numRows != pDst->numRows) || (pSrc->numCols != pDst->numCols))
  91. {
  92. /* Set status as ARM_MATH_SIZE_MISMATCH */
  93. status = ARM_MATH_SIZE_MISMATCH;
  94. }
  95. else
  96. #endif /* #ifdef ARM_MATH_MATRIX_CHECK */
  97. {
  98. /* Total number of samples in the input matrix */
  99. numSamples = (uint32_t) pSrc->numRows * pSrc->numCols;
  100. #ifndef ARM_MATH_CM0_FAMILY
  101. /* Run the below code for Cortex-M4 and Cortex-M3 */
  102. /* Loop Unrolling */
  103. blkCnt = numSamples >> 2;
  104. /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
  105. ** a second loop below computes the remaining 1 to 3 samples. */
  106. while(blkCnt > 0u)
  107. {
  108. /* C(m,n) = A(m,n) * scale */
  109. /* Scaling and results are stored in the destination buffer. */
  110. in1 = pIn[0];
  111. in2 = pIn[1];
  112. in3 = pIn[2];
  113. in4 = pIn[3];
  114. out1 = in1 * scale;
  115. out2 = in2 * scale;
  116. out3 = in3 * scale;
  117. out4 = in4 * scale;
  118. pOut[0] = out1;
  119. pOut[1] = out2;
  120. pOut[2] = out3;
  121. pOut[3] = out4;
  122. /* update pointers to process next sampels */
  123. pIn += 4u;
  124. pOut += 4u;
  125. /* Decrement the numSamples loop counter */
  126. blkCnt--;
  127. }
  128. /* If the numSamples is not a multiple of 4, compute any remaining output samples here.
  129. ** No loop unrolling is used. */
  130. blkCnt = numSamples % 0x4u;
  131. #else
  132. /* Run the below code for Cortex-M0 */
  133. /* Initialize blkCnt with number of samples */
  134. blkCnt = numSamples;
  135. #endif /* #ifndef ARM_MATH_CM0_FAMILY */
  136. while(blkCnt > 0u)
  137. {
  138. /* C(m,n) = A(m,n) * scale */
  139. /* The results are stored in the destination buffer. */
  140. *pOut++ = (*pIn++) * scale;
  141. /* Decrement the loop counter */
  142. blkCnt--;
  143. }
  144. /* Set status as ARM_MATH_SUCCESS */
  145. status = ARM_MATH_SUCCESS;
  146. }
  147. /* Return to application */
  148. return (status);
  149. }
  150. /**
  151. * @} end of MatrixScale group
  152. */