arm_mat_scale_q31.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_mat_scale_q31.c
  4. * Description: Multiplies a Q31 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 Q31 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.31 format.
  49. * These are multiplied to yield a 2.62 intermediate result and this is shifted with saturation to 1.31 format.
  50. */
  51. arm_status arm_mat_scale_q31(
  52. const arm_matrix_instance_q31 * pSrc,
  53. q31_t scaleFract,
  54. int32_t shift,
  55. arm_matrix_instance_q31 * pDst)
  56. {
  57. q31_t *pIn = pSrc->pData; /* input data matrix pointer */
  58. q31_t *pOut = pDst->pData; /* output data matrix pointer */
  59. uint32_t numSamples; /* total number of elements in the matrix */
  60. int32_t totShift = shift + 1; /* shift to apply after scaling */
  61. uint32_t blkCnt; /* loop counters */
  62. arm_status status; /* status of matrix scaling */
  63. q31_t in1, in2, out1; /* temporary variabels */
  64. #if defined (ARM_MATH_DSP)
  65. q31_t in3, in4, out2, out3, out4; /* temporary variables */
  66. #endif // #ifndef ARM_MAT_CM0
  67. #ifdef ARM_MATH_MATRIX_CHECK
  68. /* Check for matrix mismatch */
  69. if ((pSrc->numRows != pDst->numRows) || (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 the input matrix */
  78. numSamples = (uint32_t) pSrc->numRows * pSrc->numCols;
  79. #if defined (ARM_MATH_DSP)
  80. /* Run the below code for Cortex-M4 and Cortex-M3 */
  81. /* Loop Unrolling */
  82. blkCnt = numSamples >> 2U;
  83. /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
  84. ** a second loop below computes the remaining 1 to 3 samples. */
  85. while (blkCnt > 0U)
  86. {
  87. /* C(m,n) = A(m,n) * k */
  88. /* Read values from input */
  89. in1 = *pIn;
  90. in2 = *(pIn + 1);
  91. in3 = *(pIn + 2);
  92. in4 = *(pIn + 3);
  93. /* multiply input with scaler value */
  94. in1 = ((q63_t) in1 * scaleFract) >> 32;
  95. in2 = ((q63_t) in2 * scaleFract) >> 32;
  96. in3 = ((q63_t) in3 * scaleFract) >> 32;
  97. in4 = ((q63_t) in4 * scaleFract) >> 32;
  98. /* apply shifting */
  99. out1 = in1 << totShift;
  100. out2 = in2 << totShift;
  101. /* saturate the results. */
  102. if (in1 != (out1 >> totShift))
  103. out1 = 0x7FFFFFFF ^ (in1 >> 31);
  104. if (in2 != (out2 >> totShift))
  105. out2 = 0x7FFFFFFF ^ (in2 >> 31);
  106. out3 = in3 << totShift;
  107. out4 = in4 << totShift;
  108. *pOut = out1;
  109. *(pOut + 1) = out2;
  110. if (in3 != (out3 >> totShift))
  111. out3 = 0x7FFFFFFF ^ (in3 >> 31);
  112. if (in4 != (out4 >> totShift))
  113. out4 = 0x7FFFFFFF ^ (in4 >> 31);
  114. *(pOut + 2) = out3;
  115. *(pOut + 3) = out4;
  116. /* update pointers to process next sampels */
  117. pIn += 4U;
  118. pOut += 4U;
  119. /* Decrement the numSamples loop counter */
  120. blkCnt--;
  121. }
  122. /* If the numSamples is not a multiple of 4, compute any remaining output samples here.
  123. ** No loop unrolling is used. */
  124. blkCnt = numSamples % 0x4U;
  125. #else
  126. /* Run the below code for Cortex-M0 */
  127. /* Initialize blkCnt with number of samples */
  128. blkCnt = numSamples;
  129. #endif /* #if defined (ARM_MATH_DSP) */
  130. while (blkCnt > 0U)
  131. {
  132. /* C(m,n) = A(m,n) * k */
  133. /* Scale, saturate and then store the results in the destination buffer. */
  134. in1 = *pIn++;
  135. in2 = ((q63_t) in1 * scaleFract) >> 32;
  136. out1 = in2 << totShift;
  137. if (in2 != (out1 >> totShift))
  138. out1 = 0x7FFFFFFF ^ (in2 >> 31);
  139. *pOut++ = out1;
  140. /* Decrement the numSamples loop counter */
  141. blkCnt--;
  142. }
  143. /* Set status as ARM_MATH_SUCCESS */
  144. status = ARM_MATH_SUCCESS;
  145. }
  146. /* Return to application */
  147. return (status);
  148. }
  149. /**
  150. * @} end of MatrixScale group
  151. */