arm_mat_scale_q31.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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: 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 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 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.31 format.
  47. These are multiplied to yield a 2.62 intermediate result which is shifted with saturation to 1.31 format.
  48. */
  49. arm_status arm_mat_scale_q31(
  50. const arm_matrix_instance_q31 * pSrc,
  51. q31_t scaleFract,
  52. int32_t shift,
  53. arm_matrix_instance_q31 * pDst)
  54. {
  55. q31_t *pIn = pSrc->pData; /* Input data matrix pointer */
  56. q31_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 = shift + 1; /* Shift to apply after scaling */
  61. q31_t in, out; /* Temporary variabels */
  62. #ifdef ARM_MATH_MATRIX_CHECK
  63. /* Check for matrix mismatch condition */
  64. if ((pSrc->numRows != pDst->numRows) ||
  65. (pSrc->numCols != pDst->numCols) )
  66. {
  67. /* Set status as ARM_MATH_SIZE_MISMATCH */
  68. status = ARM_MATH_SIZE_MISMATCH;
  69. }
  70. else
  71. #endif /* #ifdef ARM_MATH_MATRIX_CHECK */
  72. {
  73. /* Total number of samples in input matrix */
  74. numSamples = (uint32_t) pSrc->numRows * pSrc->numCols;
  75. #if defined (ARM_MATH_LOOPUNROLL)
  76. /* Loop unrolling: Compute 4 outputs at a time */
  77. blkCnt = numSamples >> 2U;
  78. while (blkCnt > 0U)
  79. {
  80. /* C(m,n) = A(m,n) * k */
  81. /* Scale, saturate and store result in destination buffer. */
  82. in = *pIn++; /* read four inputs from source */
  83. in = ((q63_t) in * scaleFract) >> 32; /* multiply input with scaler value */
  84. out = in << kShift; /* apply shifting */
  85. if (in != (out >> kShift)) /* saturate the results. */
  86. out = 0x7FFFFFFF ^ (in >> 31);
  87. *pOut++ = out; /* Store result destination */
  88. in = *pIn++;
  89. in = ((q63_t) in * scaleFract) >> 32;
  90. out = in << kShift;
  91. if (in != (out >> kShift))
  92. out = 0x7FFFFFFF ^ (in >> 31);
  93. *pOut++ = out;
  94. in = *pIn++;
  95. in = ((q63_t) in * scaleFract) >> 32;
  96. out = in << kShift;
  97. if (in != (out >> kShift))
  98. out = 0x7FFFFFFF ^ (in >> 31);
  99. *pOut++ = out;
  100. in = *pIn++;
  101. in = ((q63_t) in * scaleFract) >> 32;
  102. out = in << kShift;
  103. if (in != (out >> kShift))
  104. out = 0x7FFFFFFF ^ (in >> 31);
  105. *pOut++ = out;
  106. /* Decrement loop counter */
  107. blkCnt--;
  108. }
  109. /* Loop unrolling: Compute remaining outputs */
  110. blkCnt = numSamples % 0x4U;
  111. #else
  112. /* Initialize blkCnt with number of samples */
  113. blkCnt = numSamples;
  114. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  115. while (blkCnt > 0U)
  116. {
  117. /* C(m,n) = A(m,n) * k */
  118. /* Scale, saturate and store result in destination buffer. */
  119. in = *pIn++;
  120. in = ((q63_t) in * scaleFract) >> 32;
  121. out = in << kShift;
  122. if (in != (out >> kShift))
  123. out = 0x7FFFFFFF ^ (in >> 31);
  124. *pOut++ = out;
  125. /* Decrement loop counter */
  126. blkCnt--;
  127. }
  128. /* Set status as ARM_MATH_SUCCESS */
  129. status = ARM_MATH_SUCCESS;
  130. }
  131. /* Return to application */
  132. return (status);
  133. }
  134. /**
  135. @} end of MatrixScale group
  136. */