arm_scale_f32.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_scale_f32.c
  4. * Description: Multiplies a floating-point vector 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/basic_math_functions.h"
  29. /**
  30. @ingroup groupMath
  31. */
  32. /**
  33. @defgroup BasicScale Vector Scale
  34. Multiply a vector by a scalar value. For floating-point data, the algorithm used is:
  35. <pre>
  36. pDst[n] = pSrc[n] * scale, 0 <= n < blockSize.
  37. </pre>
  38. In the fixed-point Q7, Q15, and Q31 functions, <code>scale</code> is represented by
  39. a fractional multiplication <code>scaleFract</code> and an arithmetic shift <code>shift</code>.
  40. The shift allows the gain of the scaling operation to exceed 1.0.
  41. The algorithm used with fixed-point data is:
  42. <pre>
  43. pDst[n] = (pSrc[n] * scaleFract) << shift, 0 <= n < blockSize.
  44. </pre>
  45. The overall scale factor applied to the fixed-point data is
  46. <pre>
  47. scale = scaleFract * 2^shift.
  48. </pre>
  49. The functions support in-place computation allowing the source and destination
  50. pointers to reference the same memory buffer.
  51. */
  52. /**
  53. @addtogroup BasicScale
  54. @{
  55. */
  56. /**
  57. @brief Multiplies a floating-point vector by a scalar.
  58. @param[in] pSrc points to the input vector
  59. @param[in] scale scale factor to be applied
  60. @param[out] pDst points to the output vector
  61. @param[in] blockSize number of samples in each vector
  62. */
  63. #if defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE)
  64. #include "arm_helium_utils.h"
  65. void arm_scale_f32(
  66. const float32_t * pSrc,
  67. float32_t scale,
  68. float32_t * pDst,
  69. uint32_t blockSize)
  70. {
  71. uint32_t blkCnt; /* Loop counter */
  72. f32x4_t vec1;
  73. f32x4_t res;
  74. /* Compute 4 outputs at a time */
  75. blkCnt = blockSize >> 2U;
  76. while (blkCnt > 0U)
  77. {
  78. /* C = A + offset */
  79. /* Add offset and then store the results in the destination buffer. */
  80. vec1 = vld1q(pSrc);
  81. res = vmulq(vec1,scale);
  82. vst1q(pDst, res);
  83. /* Increment pointers */
  84. pSrc += 4;
  85. pDst += 4;
  86. /* Decrement the loop counter */
  87. blkCnt--;
  88. }
  89. /* Tail */
  90. blkCnt = blockSize & 0x3;
  91. if (blkCnt > 0U)
  92. {
  93. mve_pred16_t p0 = vctp32q(blkCnt);
  94. vec1 = vld1q((float32_t const *) pSrc);
  95. vstrwq_p(pDst, vmulq(vec1, scale), p0);
  96. }
  97. }
  98. #else
  99. void arm_scale_f32(
  100. const float32_t *pSrc,
  101. float32_t scale,
  102. float32_t *pDst,
  103. uint32_t blockSize)
  104. {
  105. uint32_t blkCnt; /* Loop counter */
  106. #if defined(ARM_MATH_NEON_EXPERIMENTAL)
  107. f32x4_t vec1;
  108. f32x4_t res;
  109. /* Compute 4 outputs at a time */
  110. blkCnt = blockSize >> 2U;
  111. while (blkCnt > 0U)
  112. {
  113. /* C = A * scale */
  114. /* Scale the input and then store the results in the destination buffer. */
  115. vec1 = vld1q_f32(pSrc);
  116. res = vmulq_f32(vec1, vdupq_n_f32(scale));
  117. vst1q_f32(pDst, res);
  118. /* Increment pointers */
  119. pSrc += 4;
  120. pDst += 4;
  121. /* Decrement the loop counter */
  122. blkCnt--;
  123. }
  124. /* Tail */
  125. blkCnt = blockSize & 0x3;
  126. #else
  127. #if defined (ARM_MATH_LOOPUNROLL)
  128. /* Loop unrolling: Compute 4 outputs at a time */
  129. blkCnt = blockSize >> 2U;
  130. while (blkCnt > 0U)
  131. {
  132. float32_t in1, in2, in3, in4;
  133. /* C = A * scale */
  134. /* Scale input and store result in destination buffer. */
  135. in1 = (*pSrc++) * scale;
  136. in2 = (*pSrc++) * scale;
  137. in3 = (*pSrc++) * scale;
  138. in4 = (*pSrc++) * scale;
  139. *pDst++ = in1;
  140. *pDst++ = in2;
  141. *pDst++ = in3;
  142. *pDst++ = in4;
  143. /* Decrement loop counter */
  144. blkCnt--;
  145. }
  146. /* Loop unrolling: Compute remaining outputs */
  147. blkCnt = blockSize % 0x4U;
  148. #else
  149. /* Initialize blkCnt with number of samples */
  150. blkCnt = blockSize;
  151. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  152. #endif /* #if defined(ARM_MATH_NEON_EXPERIMENTAL) */
  153. while (blkCnt > 0U)
  154. {
  155. /* C = A * scale */
  156. /* Scale input and store result in destination buffer. */
  157. *pDst++ = (*pSrc++) * scale;
  158. /* Decrement loop counter */
  159. blkCnt--;
  160. }
  161. }
  162. #endif /* defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE) */
  163. /**
  164. @} end of BasicScale group
  165. */