arm_mat_scale_q15.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. #if defined(ARM_MATH_MVEI)
  50. arm_status arm_mat_scale_q15(
  51. const arm_matrix_instance_q15 * pSrc,
  52. q15_t scaleFract,
  53. int32_t shift,
  54. arm_matrix_instance_q15 * pDst)
  55. {
  56. arm_status status; /* Status of matrix scaling */
  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. uint32_t blkCnt; /* loop counters */
  61. q15x8_t vecIn, vecOut;
  62. q15_t const *pInVec;
  63. int32_t totShift = shift + 1; /* shift to apply after scaling */
  64. pInVec = (q15_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 >> 3;
  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); pInVec += 8;
  88. /* multiply input with scaler value */
  89. vecOut = vmulhq(vecIn, vdupq_n_s16(scaleFract));
  90. /* apply shifting */
  91. vecOut = vqshlq_r(vecOut, totShift);
  92. vst1q(pOut, vecOut); pOut += 8;
  93. /*
  94. * Decrement the blockSize loop counter
  95. */
  96. blkCnt--;
  97. }
  98. /*
  99. * tail
  100. * (will be merged thru tail predication)
  101. */
  102. blkCnt = numSamples & 7;
  103. if (blkCnt > 0U)
  104. {
  105. mve_pred16_t p0 = vctp16q(blkCnt);
  106. vecIn = vld1q(pInVec); pInVec += 8;
  107. vecOut = vmulhq(vecIn, vdupq_n_s16(scaleFract));
  108. vecOut = vqshlq_r(vecOut, totShift);
  109. vstrhq_p(pOut, vecOut, p0);
  110. }
  111. /* Set status as ARM_MATH_SUCCESS */
  112. status = ARM_MATH_SUCCESS;
  113. }
  114. /* Return to application */
  115. return (status);
  116. }
  117. #else
  118. arm_status arm_mat_scale_q15(
  119. const arm_matrix_instance_q15 * pSrc,
  120. q15_t scaleFract,
  121. int32_t shift,
  122. arm_matrix_instance_q15 * pDst)
  123. {
  124. q15_t *pIn = pSrc->pData; /* Input data matrix pointer */
  125. q15_t *pOut = pDst->pData; /* Output data matrix pointer */
  126. uint32_t numSamples; /* Total number of elements in the matrix */
  127. uint32_t blkCnt; /* Loop counter */
  128. arm_status status; /* Status of matrix scaling */
  129. int32_t kShift = 15 - shift; /* Total shift to apply after scaling */
  130. #if defined (ARM_MATH_LOOPUNROLL) && defined (ARM_MATH_DSP)
  131. q31_t inA1, inA2;
  132. q31_t out1, out2, out3, out4; /* Temporary output variables */
  133. q15_t in1, in2, in3, in4; /* Temporary input variables */
  134. #endif
  135. #ifdef ARM_MATH_MATRIX_CHECK
  136. /* Check for matrix mismatch condition */
  137. if ((pSrc->numRows != pDst->numRows) ||
  138. (pSrc->numCols != pDst->numCols) )
  139. {
  140. /* Set status as ARM_MATH_SIZE_MISMATCH */
  141. status = ARM_MATH_SIZE_MISMATCH;
  142. }
  143. else
  144. #endif /* #ifdef ARM_MATH_MATRIX_CHECK */
  145. {
  146. /* Total number of samples in input matrix */
  147. numSamples = (uint32_t) pSrc->numRows * pSrc->numCols;
  148. #if defined (ARM_MATH_LOOPUNROLL)
  149. /* Loop unrolling: Compute 4 outputs at a time */
  150. blkCnt = numSamples >> 2U;
  151. while (blkCnt > 0U)
  152. {
  153. /* C(m,n) = A(m,n) * k */
  154. #if defined (ARM_MATH_DSP)
  155. /* read 2 times 2 samples at a time from source */
  156. inA1 = read_q15x2_ia ((q15_t **) &pIn);
  157. inA2 = read_q15x2_ia ((q15_t **) &pIn);
  158. /* Scale inputs and store result in temporary variables
  159. * in single cycle by packing the outputs */
  160. out1 = (q31_t) ((q15_t) (inA1 >> 16) * scaleFract);
  161. out2 = (q31_t) ((q15_t) (inA1 ) * scaleFract);
  162. out3 = (q31_t) ((q15_t) (inA2 >> 16) * scaleFract);
  163. out4 = (q31_t) ((q15_t) (inA2 ) * scaleFract);
  164. /* apply shifting */
  165. out1 = out1 >> kShift;
  166. out2 = out2 >> kShift;
  167. out3 = out3 >> kShift;
  168. out4 = out4 >> kShift;
  169. /* saturate the output */
  170. in1 = (q15_t) (__SSAT(out1, 16));
  171. in2 = (q15_t) (__SSAT(out2, 16));
  172. in3 = (q15_t) (__SSAT(out3, 16));
  173. in4 = (q15_t) (__SSAT(out4, 16));
  174. /* store result to destination */
  175. write_q15x2_ia (&pOut, __PKHBT(in2, in1, 16));
  176. write_q15x2_ia (&pOut, __PKHBT(in4, in3, 16));
  177. #else
  178. *pOut++ = (q15_t) (__SSAT(((q31_t) (*pIn++) * scaleFract) >> kShift, 16));
  179. *pOut++ = (q15_t) (__SSAT(((q31_t) (*pIn++) * scaleFract) >> kShift, 16));
  180. *pOut++ = (q15_t) (__SSAT(((q31_t) (*pIn++) * scaleFract) >> kShift, 16));
  181. *pOut++ = (q15_t) (__SSAT(((q31_t) (*pIn++) * scaleFract) >> kShift, 16));
  182. #endif
  183. /* Decrement loop counter */
  184. blkCnt--;
  185. }
  186. /* Loop unrolling: Compute remaining outputs */
  187. blkCnt = numSamples % 0x4U;
  188. #else
  189. /* Initialize blkCnt with number of samples */
  190. blkCnt = numSamples;
  191. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  192. while (blkCnt > 0U)
  193. {
  194. /* C(m,n) = A(m,n) * k */
  195. /* Scale, saturate and store result in destination buffer. */
  196. *pOut++ = (q15_t) (__SSAT(((q31_t) (*pIn++) * scaleFract) >> kShift, 16));
  197. /* Decrement loop counter */
  198. blkCnt--;
  199. }
  200. /* Set status as ARM_MATH_SUCCESS */
  201. status = ARM_MATH_SUCCESS;
  202. }
  203. /* Return to application */
  204. return (status);
  205. }
  206. #endif /* defined(ARM_MATH_MVEI) */
  207. /**
  208. @} end of MatrixScale group
  209. */