arm_mat_scale_f16.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_mat_scale_f16.c
  4. * Description: Multiplies a floating-point 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_f16.h"
  29. #if defined(ARM_FLOAT16_SUPPORTED)
  30. /**
  31. @ingroup groupMatrix
  32. */
  33. /**
  34. @addtogroup MatrixScale
  35. @{
  36. */
  37. /**
  38. @brief Floating-point matrix scaling.
  39. @param[in] pSrc points to input matrix
  40. @param[in] scale scale factor to be applied
  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. */
  46. #if defined(ARM_MATH_MVE_FLOAT16) && !defined(ARM_MATH_AUTOVECTORIZE)
  47. arm_status arm_mat_scale_f16(
  48. const arm_matrix_instance_f16 * pSrc,
  49. float16_t scale,
  50. arm_matrix_instance_f16 * pDst)
  51. {
  52. arm_status status; /* status of matrix scaling */
  53. #ifdef ARM_MATH_MATRIX_CHECK
  54. /* Check for matrix mismatch condition */
  55. if ((pSrc->numRows != pDst->numRows) || (pSrc->numCols != pDst->numCols))
  56. {
  57. /* Set status as ARM_MATH_SIZE_MISMATCH */
  58. status = ARM_MATH_SIZE_MISMATCH;
  59. }
  60. else
  61. #endif /* #ifdef ARM_MATH_MATRIX_CHECK */
  62. {
  63. float16_t *pIn = pSrc->pData; /* input data matrix pointer */
  64. float16_t *pOut = pDst->pData; /* output data matrix pointer */
  65. uint32_t numSamples; /* total number of elements in the matrix */
  66. uint32_t blkCnt; /* loop counters */
  67. f16x8_t vecIn, vecOut, vecScale;
  68. float16_t const *pInVec;
  69. pInVec = (float16_t const *) pIn;
  70. vecScale = vdupq_n_f16(scale);
  71. /*
  72. * Total number of samples in the input matrix
  73. */
  74. numSamples = (uint32_t) pSrc->numRows * pSrc->numCols;
  75. blkCnt = numSamples >> 3;
  76. while (blkCnt > 0U)
  77. {
  78. /*
  79. * C(m,n) = A(m,n) * scale
  80. * Scaling and results are stored in the destination buffer.
  81. */
  82. vecIn = vld1q(pInVec);
  83. pInVec += 8;
  84. vecOut = vmulq_f16(vecIn, vecScale);
  85. vst1q(pOut, vecOut);
  86. pOut += 8;
  87. /*
  88. * Decrement the blockSize loop counter
  89. */
  90. blkCnt--;
  91. }
  92. /*
  93. * tail
  94. */
  95. blkCnt = numSamples & 7;
  96. if (blkCnt > 0U)
  97. {
  98. mve_pred16_t p0 = vctp16q(blkCnt);
  99. vecIn = vld1q(pInVec);
  100. vecOut = vecIn * scale;
  101. vstrhq_p(pOut, vecOut, p0);
  102. }
  103. /* Set status as ARM_MATH_SUCCESS */
  104. status = ARM_MATH_SUCCESS;
  105. }
  106. /* Return to application */
  107. return (status);
  108. }
  109. #else
  110. arm_status arm_mat_scale_f16(
  111. const arm_matrix_instance_f16 * pSrc,
  112. float16_t scale,
  113. arm_matrix_instance_f16 * pDst)
  114. {
  115. float16_t *pIn = pSrc->pData; /* Input data matrix pointer */
  116. float16_t *pOut = pDst->pData; /* Output data matrix pointer */
  117. uint32_t numSamples; /* Total number of elements in the matrix */
  118. uint32_t blkCnt; /* Loop counters */
  119. arm_status status; /* Status of matrix scaling */
  120. #ifdef ARM_MATH_MATRIX_CHECK
  121. /* Check for matrix mismatch condition */
  122. if ((pSrc->numRows != pDst->numRows) ||
  123. (pSrc->numCols != pDst->numCols) )
  124. {
  125. /* Set status as ARM_MATH_SIZE_MISMATCH */
  126. status = ARM_MATH_SIZE_MISMATCH;
  127. }
  128. else
  129. #endif /* #ifdef ARM_MATH_MATRIX_CHECK */
  130. {
  131. /* Total number of samples in input matrix */
  132. numSamples = (uint32_t) pSrc->numRows * pSrc->numCols;
  133. #if defined (ARM_MATH_LOOPUNROLL)
  134. /* Loop unrolling: Compute 4 outputs at a time */
  135. blkCnt = numSamples >> 2U;
  136. while (blkCnt > 0U)
  137. {
  138. /* C(m,n) = A(m,n) * scale */
  139. /* Scale and store result in destination buffer. */
  140. *pOut++ = (_Float16)(*pIn++) * (_Float16)scale;
  141. *pOut++ = (_Float16)(*pIn++) * (_Float16)scale;
  142. *pOut++ = (_Float16)(*pIn++) * (_Float16)scale;
  143. *pOut++ = (_Float16)(*pIn++) * (_Float16)scale;
  144. /* Decrement loop counter */
  145. blkCnt--;
  146. }
  147. /* Loop unrolling: Compute remaining outputs */
  148. blkCnt = numSamples % 0x4U;
  149. #else
  150. /* Initialize blkCnt with number of samples */
  151. blkCnt = numSamples;
  152. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  153. while (blkCnt > 0U)
  154. {
  155. /* C(m,n) = A(m,n) * scale */
  156. /* Scale and store result in destination buffer. */
  157. *pOut++ = (_Float16)(*pIn++) * (_Float16)scale;
  158. /* Decrement loop counter */
  159. blkCnt--;
  160. }
  161. /* Set status as ARM_MATH_SUCCESS */
  162. status = ARM_MATH_SUCCESS;
  163. }
  164. /* Return to application */
  165. return (status);
  166. }
  167. #endif /* defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE) */
  168. /**
  169. @} end of MatrixScale group
  170. */
  171. #endif /* #if defined(ARM_FLOAT16_SUPPORTED) */