arm_mat_scale_q15.c 5.3 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: 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. * @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 The function returns either
  43. * <code>ARM_MATH_SIZE_MISMATCH</code> or <code>ARM_MATH_SUCCESS</code> based on the outcome of size checking.
  44. *
  45. * @details
  46. * <b>Scaling and Overflow Behavior:</b>
  47. * \par
  48. * The input data <code>*pSrc</code> and <code>scaleFract</code> are in 1.15 format.
  49. * These are multiplied to yield a 2.30 intermediate result and this is shifted with saturation to 1.15 format.
  50. */
  51. arm_status arm_mat_scale_q15(
  52. const arm_matrix_instance_q15 * pSrc,
  53. q15_t scaleFract,
  54. int32_t shift,
  55. arm_matrix_instance_q15 * pDst)
  56. {
  57. q15_t *pIn = pSrc->pData; /* input data matrix pointer */
  58. q15_t *pOut = pDst->pData; /* output data matrix pointer */
  59. uint32_t numSamples; /* total number of elements in the matrix */
  60. int32_t totShift = 15 - shift; /* total shift to apply after scaling */
  61. uint32_t blkCnt; /* loop counters */
  62. arm_status status; /* status of matrix scaling */
  63. #if defined (ARM_MATH_DSP)
  64. q15_t in1, in2, in3, in4;
  65. q31_t out1, out2, out3, out4;
  66. q31_t inA1, inA2;
  67. #endif // #if defined (ARM_MATH_DSP)
  68. #ifdef ARM_MATH_MATRIX_CHECK
  69. /* Check for matrix mismatch */
  70. if ((pSrc->numRows != pDst->numRows) || (pSrc->numCols != pDst->numCols))
  71. {
  72. /* Set status as ARM_MATH_SIZE_MISMATCH */
  73. status = ARM_MATH_SIZE_MISMATCH;
  74. }
  75. else
  76. #endif // #ifdef ARM_MATH_MATRIX_CHECK
  77. {
  78. /* Total number of samples in the input matrix */
  79. numSamples = (uint32_t) pSrc->numRows * pSrc->numCols;
  80. #if defined (ARM_MATH_DSP)
  81. /* Run the below code for Cortex-M4 and Cortex-M3 */
  82. /* Loop Unrolling */
  83. blkCnt = numSamples >> 2;
  84. /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
  85. ** a second loop below computes the remaining 1 to 3 samples. */
  86. while (blkCnt > 0U)
  87. {
  88. /* C(m,n) = A(m,n) * k */
  89. /* Scale, saturate and then store the results in the destination buffer. */
  90. /* Reading 2 inputs from memory */
  91. inA1 = _SIMD32_OFFSET(pIn);
  92. inA2 = _SIMD32_OFFSET(pIn + 2);
  93. /* C = A * scale */
  94. /* Scale the inputs and then store the 2 results in the destination buffer
  95. * in single cycle by packing the outputs */
  96. out1 = (q31_t) ((q15_t) (inA1 >> 16) * scaleFract);
  97. out2 = (q31_t) ((q15_t) inA1 * scaleFract);
  98. out3 = (q31_t) ((q15_t) (inA2 >> 16) * scaleFract);
  99. out4 = (q31_t) ((q15_t) inA2 * scaleFract);
  100. out1 = out1 >> totShift;
  101. inA1 = _SIMD32_OFFSET(pIn + 4);
  102. out2 = out2 >> totShift;
  103. inA2 = _SIMD32_OFFSET(pIn + 6);
  104. out3 = out3 >> totShift;
  105. out4 = out4 >> totShift;
  106. in1 = (q15_t) (__SSAT(out1, 16));
  107. in2 = (q15_t) (__SSAT(out2, 16));
  108. in3 = (q15_t) (__SSAT(out3, 16));
  109. in4 = (q15_t) (__SSAT(out4, 16));
  110. _SIMD32_OFFSET(pOut) = __PKHBT(in2, in1, 16);
  111. _SIMD32_OFFSET(pOut + 2) = __PKHBT(in4, in3, 16);
  112. /* update pointers to process next sampels */
  113. pIn += 4U;
  114. pOut += 4U;
  115. /* Decrement the numSamples loop counter */
  116. blkCnt--;
  117. }
  118. /* If the numSamples is not a multiple of 4, compute any remaining output samples here.
  119. ** No loop unrolling is used. */
  120. blkCnt = numSamples % 0x4U;
  121. #else
  122. /* Run the below code for Cortex-M0 */
  123. /* Initialize blkCnt with number of samples */
  124. blkCnt = numSamples;
  125. #endif /* #if defined (ARM_MATH_DSP) */
  126. while (blkCnt > 0U)
  127. {
  128. /* C(m,n) = A(m,n) * k */
  129. /* Scale, saturate and then store the results in the destination buffer. */
  130. *pOut++ =
  131. (q15_t) (__SSAT(((q31_t) (*pIn++) * scaleFract) >> totShift, 16));
  132. /* Decrement the numSamples loop counter */
  133. blkCnt--;
  134. }
  135. /* Set status as ARM_MATH_SUCCESS */
  136. status = ARM_MATH_SUCCESS;
  137. }
  138. /* Return to application */
  139. return (status);
  140. }
  141. /**
  142. * @} end of MatrixScale group
  143. */