arm_scale_f32.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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: 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 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. @return none
  63. */
  64. #if defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE)
  65. #include "arm_helium_utils.h"
  66. void arm_scale_f32(
  67. const float32_t * pSrc,
  68. float32_t scale,
  69. float32_t * pDst,
  70. uint32_t blockSize)
  71. {
  72. uint32_t blkCnt; /* Loop counter */
  73. f32x4_t vec1;
  74. f32x4_t res;
  75. /* Compute 4 outputs at a time */
  76. blkCnt = blockSize >> 2U;
  77. while (blkCnt > 0U)
  78. {
  79. /* C = A + offset */
  80. /* Add offset and then store the results in the destination buffer. */
  81. vec1 = vld1q(pSrc);
  82. res = vmulq(vec1,scale);
  83. vst1q(pDst, res);
  84. /* Increment pointers */
  85. pSrc += 4;
  86. pDst += 4;
  87. /* Decrement the loop counter */
  88. blkCnt--;
  89. }
  90. /* Tail */
  91. blkCnt = blockSize & 0x3;
  92. if (blkCnt > 0U)
  93. {
  94. mve_pred16_t p0 = vctp32q(blkCnt);
  95. vec1 = vld1q((float32_t const *) pSrc);
  96. vstrwq_p(pDst, vmulq(vec1, scale), p0);
  97. }
  98. }
  99. #else
  100. void arm_scale_f32(
  101. const float32_t *pSrc,
  102. float32_t scale,
  103. float32_t *pDst,
  104. uint32_t blockSize)
  105. {
  106. uint32_t blkCnt; /* Loop counter */
  107. #if defined(ARM_MATH_NEON_EXPERIMENTAL)
  108. f32x4_t vec1;
  109. f32x4_t res;
  110. /* Compute 4 outputs at a time */
  111. blkCnt = blockSize >> 2U;
  112. while (blkCnt > 0U)
  113. {
  114. /* C = A * scale */
  115. /* Scale the input and then store the results in the destination buffer. */
  116. vec1 = vld1q_f32(pSrc);
  117. res = vmulq_f32(vec1, vdupq_n_f32(scale));
  118. vst1q_f32(pDst, res);
  119. /* Increment pointers */
  120. pSrc += 4;
  121. pDst += 4;
  122. /* Decrement the loop counter */
  123. blkCnt--;
  124. }
  125. /* Tail */
  126. blkCnt = blockSize & 0x3;
  127. #else
  128. #if defined (ARM_MATH_LOOPUNROLL)
  129. /* Loop unrolling: Compute 4 outputs at a time */
  130. blkCnt = blockSize >> 2U;
  131. while (blkCnt > 0U)
  132. {
  133. float32_t in1, in2, in3, in4;
  134. /* C = A * scale */
  135. /* Scale input and store result in destination buffer. */
  136. in1 = (*pSrc++) * scale;
  137. in2 = (*pSrc++) * scale;
  138. in3 = (*pSrc++) * scale;
  139. in4 = (*pSrc++) * scale;
  140. *pDst++ = in1;
  141. *pDst++ = in2;
  142. *pDst++ = in3;
  143. *pDst++ = in4;
  144. /* Decrement loop counter */
  145. blkCnt--;
  146. }
  147. /* Loop unrolling: Compute remaining outputs */
  148. blkCnt = blockSize % 0x4U;
  149. #else
  150. /* Initialize blkCnt with number of samples */
  151. blkCnt = blockSize;
  152. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  153. #endif /* #if defined(ARM_MATH_NEON_EXPERIMENTAL) */
  154. while (blkCnt > 0U)
  155. {
  156. /* C = A * scale */
  157. /* Scale input and store result in destination buffer. */
  158. *pDst++ = (*pSrc++) * scale;
  159. /* Decrement loop counter */
  160. blkCnt--;
  161. }
  162. }
  163. #endif /* defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE) */
  164. /**
  165. @} end of BasicScale group
  166. */