arm_mat_scale_q15.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_mat_scale_q15.c
  4. * Description: Multiplies a Q15 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. @addtogroup MatrixScale
  34. @{
  35. */
  36. /**
  37. @brief Q15 matrix scaling.
  38. @param[in] pSrc points to input matrix
  39. @param[in] scaleFract fractional portion of the scale factor
  40. @param[in] shift number of bits to shift the result by
  41. @param[out] pDst points to output matrix structure
  42. @return execution status
  43. - \ref ARM_MATH_SUCCESS : Operation successful
  44. - \ref ARM_MATH_SIZE_MISMATCH : Matrix size check failed
  45. @par Scaling and Overflow Behavior
  46. The input data <code>*pSrc</code> and <code>scaleFract</code> are in 1.15 format.
  47. These are multiplied to yield a 2.30 intermediate result and this is shifted with saturation to 1.15 format.
  48. */
  49. arm_status arm_mat_scale_q15(
  50. const arm_matrix_instance_q15 * pSrc,
  51. q15_t scaleFract,
  52. int32_t shift,
  53. arm_matrix_instance_q15 * pDst)
  54. {
  55. q15_t *pIn = pSrc->pData; /* Input data matrix pointer */
  56. q15_t *pOut = pDst->pData; /* Output data matrix pointer */
  57. uint32_t numSamples; /* Total number of elements in the matrix */
  58. uint32_t blkCnt; /* Loop counter */
  59. arm_status status; /* Status of matrix scaling */
  60. int32_t kShift = 15 - shift; /* Total shift to apply after scaling */
  61. #if defined (ARM_MATH_LOOPUNROLL) && defined (ARM_MATH_DSP)
  62. q31_t inA1, inA2;
  63. q31_t out1, out2, out3, out4; /* Temporary output variables */
  64. q15_t in1, in2, in3, in4; /* Temporary input variables */
  65. #endif
  66. #ifdef ARM_MATH_MATRIX_CHECK
  67. /* Check for matrix mismatch condition */
  68. if ((pSrc->numRows != pDst->numRows) ||
  69. (pSrc->numCols != pDst->numCols) )
  70. {
  71. /* Set status as ARM_MATH_SIZE_MISMATCH */
  72. status = ARM_MATH_SIZE_MISMATCH;
  73. }
  74. else
  75. #endif /* #ifdef ARM_MATH_MATRIX_CHECK */
  76. {
  77. /* Total number of samples in input matrix */
  78. numSamples = (uint32_t) pSrc->numRows * pSrc->numCols;
  79. #if defined (ARM_MATH_LOOPUNROLL)
  80. /* Loop unrolling: Compute 4 outputs at a time */
  81. blkCnt = numSamples >> 2U;
  82. while (blkCnt > 0U)
  83. {
  84. /* C(m,n) = A(m,n) * k */
  85. #if defined (ARM_MATH_DSP)
  86. /* read 2 times 2 samples at a time from source */
  87. inA1 = read_q15x2_ia ((q15_t **) &pIn);
  88. inA2 = read_q15x2_ia ((q15_t **) &pIn);
  89. /* Scale inputs and store result in temporary variables
  90. * in single cycle by packing the outputs */
  91. out1 = (q31_t) ((q15_t) (inA1 >> 16) * scaleFract);
  92. out2 = (q31_t) ((q15_t) (inA1 ) * scaleFract);
  93. out3 = (q31_t) ((q15_t) (inA2 >> 16) * scaleFract);
  94. out4 = (q31_t) ((q15_t) (inA2 ) * scaleFract);
  95. /* apply shifting */
  96. out1 = out1 >> kShift;
  97. out2 = out2 >> kShift;
  98. out3 = out3 >> kShift;
  99. out4 = out4 >> kShift;
  100. /* saturate the output */
  101. in1 = (q15_t) (__SSAT(out1, 16));
  102. in2 = (q15_t) (__SSAT(out2, 16));
  103. in3 = (q15_t) (__SSAT(out3, 16));
  104. in4 = (q15_t) (__SSAT(out4, 16));
  105. /* store result to destination */
  106. write_q15x2_ia (&pOut, __PKHBT(in2, in1, 16));
  107. write_q15x2_ia (&pOut, __PKHBT(in4, in3, 16));
  108. #else
  109. *pOut++ = (q15_t) (__SSAT(((q31_t) (*pIn++) * scaleFract) >> kShift, 16));
  110. *pOut++ = (q15_t) (__SSAT(((q31_t) (*pIn++) * scaleFract) >> kShift, 16));
  111. *pOut++ = (q15_t) (__SSAT(((q31_t) (*pIn++) * scaleFract) >> kShift, 16));
  112. *pOut++ = (q15_t) (__SSAT(((q31_t) (*pIn++) * scaleFract) >> kShift, 16));
  113. #endif
  114. /* Decrement loop counter */
  115. blkCnt--;
  116. }
  117. /* Loop unrolling: Compute remaining outputs */
  118. blkCnt = numSamples % 0x4U;
  119. #else
  120. /* Initialize blkCnt with number of samples */
  121. blkCnt = numSamples;
  122. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  123. while (blkCnt > 0U)
  124. {
  125. /* C(m,n) = A(m,n) * k */
  126. /* Scale, saturate and store result in destination buffer. */
  127. *pOut++ = (q15_t) (__SSAT(((q31_t) (*pIn++) * scaleFract) >> kShift, 16));
  128. /* Decrement loop counter */
  129. blkCnt--;
  130. }
  131. /* Set status as ARM_MATH_SUCCESS */
  132. status = ARM_MATH_SUCCESS;
  133. }
  134. /* Return to application */
  135. return (status);
  136. }
  137. /**
  138. @} end of MatrixScale group
  139. */