arm_mat_scale_q31.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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: 23 April 2021
  7. * $Revision: V1.9.0
  8. *
  9. * Target Processor: Cortex-M and Cortex-A cores
  10. * -------------------------------------------------------------------- */
  11. /*
  12. * Copyright (C) 2010-2021 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 "dsp/matrix_functions.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. #if defined(ARM_MATH_MVEI) && !defined(ARM_MATH_AUTOVECTORIZE)
  50. arm_status arm_mat_scale_q31(
  51. const arm_matrix_instance_q31 * pSrc,
  52. q31_t scaleFract,
  53. int32_t shift,
  54. arm_matrix_instance_q31 * pDst)
  55. {
  56. q31_t *pIn = pSrc->pData; /* input data matrix pointer */
  57. q31_t *pOut = pDst->pData; /* output data matrix pointer */
  58. uint32_t numSamples; /* total number of elements in the matrix */
  59. uint32_t blkCnt; /* loop counters */
  60. q31x4_t vecIn, vecOut;
  61. q31_t const *pInVec;
  62. int32_t totShift = shift + 1; /* shift to apply after scaling */
  63. arm_status status; /* Status of matrix scaling */
  64. pInVec = (q31_t const *) pIn;
  65. #ifdef ARM_MATH_MATRIX_CHECK
  66. /* Check for matrix mismatch condition */
  67. if ((pSrc->numRows != pDst->numRows) ||
  68. (pSrc->numCols != pDst->numCols) )
  69. {
  70. /* Set status as ARM_MATH_SIZE_MISMATCH */
  71. status = ARM_MATH_SIZE_MISMATCH;
  72. }
  73. else
  74. #endif /* #ifdef ARM_MATH_MATRIX_CHECK */
  75. {
  76. /*
  77. * Total number of samples in the input matrix
  78. */
  79. numSamples = (uint32_t) pSrc->numRows * pSrc->numCols;
  80. blkCnt = numSamples >> 2;
  81. while (blkCnt > 0U)
  82. {
  83. /*
  84. * C(m,n) = A(m,n) * scale
  85. * Scaling and results are stored in the destination buffer.
  86. */
  87. vecIn = vld1q(pInVec);
  88. pInVec += 4;
  89. /* multiply input with scaler value */
  90. vecOut = vmulhq(vecIn, vdupq_n_s32(scaleFract));
  91. /* apply shifting */
  92. vecOut = vqshlq_r(vecOut, totShift);
  93. vst1q(pOut, vecOut);
  94. pOut += 4;
  95. /*
  96. * Decrement the blockSize loop counter
  97. */
  98. blkCnt--;
  99. }
  100. /*
  101. * tail
  102. */
  103. blkCnt = numSamples & 3;
  104. if (blkCnt > 0U)
  105. {
  106. mve_pred16_t p0 = vctp32q(blkCnt);
  107. vecIn = vld1q(pInVec);
  108. pInVec += 4;
  109. vecOut = vmulhq(vecIn, vdupq_n_s32(scaleFract));
  110. vecOut = vqshlq_r(vecOut, totShift);
  111. vstrwq_p(pOut, vecOut, p0);
  112. }
  113. /* Set status as ARM_MATH_SUCCESS */
  114. status = ARM_MATH_SUCCESS;
  115. }
  116. /* Return to application */
  117. return (status);
  118. }
  119. #else
  120. arm_status arm_mat_scale_q31(
  121. const arm_matrix_instance_q31 * pSrc,
  122. q31_t scaleFract,
  123. int32_t shift,
  124. arm_matrix_instance_q31 * pDst)
  125. {
  126. q31_t *pIn = pSrc->pData; /* Input data matrix pointer */
  127. q31_t *pOut = pDst->pData; /* Output data matrix pointer */
  128. uint32_t numSamples; /* Total number of elements in the matrix */
  129. uint32_t blkCnt; /* Loop counter */
  130. arm_status status; /* Status of matrix scaling */
  131. int32_t kShift = shift + 1; /* Shift to apply after scaling */
  132. q31_t in, out; /* Temporary variabels */
  133. #ifdef ARM_MATH_MATRIX_CHECK
  134. /* Check for matrix mismatch condition */
  135. if ((pSrc->numRows != pDst->numRows) ||
  136. (pSrc->numCols != pDst->numCols) )
  137. {
  138. /* Set status as ARM_MATH_SIZE_MISMATCH */
  139. status = ARM_MATH_SIZE_MISMATCH;
  140. }
  141. else
  142. #endif /* #ifdef ARM_MATH_MATRIX_CHECK */
  143. {
  144. /* Total number of samples in input matrix */
  145. numSamples = (uint32_t) pSrc->numRows * pSrc->numCols;
  146. #if defined (ARM_MATH_LOOPUNROLL)
  147. /* Loop unrolling: Compute 4 outputs at a time */
  148. blkCnt = numSamples >> 2U;
  149. while (blkCnt > 0U)
  150. {
  151. /* C(m,n) = A(m,n) * k */
  152. /* Scale, saturate and store result in destination buffer. */
  153. in = *pIn++; /* read four inputs from source */
  154. in = ((q63_t) in * scaleFract) >> 32; /* multiply input with scaler value */
  155. out = in << kShift; /* apply shifting */
  156. if (in != (out >> kShift)) /* saturate the results. */
  157. out = 0x7FFFFFFF ^ (in >> 31);
  158. *pOut++ = out; /* Store result destination */
  159. in = *pIn++;
  160. in = ((q63_t) in * scaleFract) >> 32;
  161. out = in << kShift;
  162. if (in != (out >> kShift))
  163. out = 0x7FFFFFFF ^ (in >> 31);
  164. *pOut++ = out;
  165. in = *pIn++;
  166. in = ((q63_t) in * scaleFract) >> 32;
  167. out = in << kShift;
  168. if (in != (out >> kShift))
  169. out = 0x7FFFFFFF ^ (in >> 31);
  170. *pOut++ = out;
  171. in = *pIn++;
  172. in = ((q63_t) in * scaleFract) >> 32;
  173. out = in << kShift;
  174. if (in != (out >> kShift))
  175. out = 0x7FFFFFFF ^ (in >> 31);
  176. *pOut++ = out;
  177. /* Decrement loop counter */
  178. blkCnt--;
  179. }
  180. /* Loop unrolling: Compute remaining outputs */
  181. blkCnt = numSamples % 0x4U;
  182. #else
  183. /* Initialize blkCnt with number of samples */
  184. blkCnt = numSamples;
  185. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  186. while (blkCnt > 0U)
  187. {
  188. /* C(m,n) = A(m,n) * k */
  189. /* Scale, saturate and store result in destination buffer. */
  190. in = *pIn++;
  191. in = ((q63_t) in * scaleFract) >> 32;
  192. out = in << kShift;
  193. if (in != (out >> kShift))
  194. out = 0x7FFFFFFF ^ (in >> 31);
  195. *pOut++ = out;
  196. /* Decrement loop counter */
  197. blkCnt--;
  198. }
  199. /* Set status as ARM_MATH_SUCCESS */
  200. status = ARM_MATH_SUCCESS;
  201. }
  202. /* Return to application */
  203. return (status);
  204. }
  205. #endif /* defined(ARM_MATH_MVEI) */
  206. /**
  207. @} end of MatrixScale group
  208. */