arm_scale_f32.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. void arm_scale_f32(
  65. const float32_t *pSrc,
  66. float32_t scale,
  67. float32_t *pDst,
  68. uint32_t blockSize)
  69. {
  70. uint32_t blkCnt; /* Loop counter */
  71. #if defined(ARM_MATH_NEON_EXPERIMENTAL)
  72. float32x4_t vec1;
  73. float32x4_t res;
  74. /* Compute 4 outputs at a time */
  75. blkCnt = blockSize >> 2U;
  76. while (blkCnt > 0U)
  77. {
  78. /* C = A * scale */
  79. /* Scale the input and then store the results in the destination buffer. */
  80. vec1 = vld1q_f32(pSrc);
  81. res = vmulq_f32(vec1, vdupq_n_f32(scale));
  82. vst1q_f32(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. #else
  92. #if defined (ARM_MATH_LOOPUNROLL)
  93. /* Loop unrolling: Compute 4 outputs at a time */
  94. blkCnt = blockSize >> 2U;
  95. while (blkCnt > 0U)
  96. {
  97. /* C = A * scale */
  98. /* Scale input and store result in destination buffer. */
  99. *pDst++ = (*pSrc++) * scale;
  100. *pDst++ = (*pSrc++) * scale;
  101. *pDst++ = (*pSrc++) * scale;
  102. *pDst++ = (*pSrc++) * scale;
  103. /* Decrement loop counter */
  104. blkCnt--;
  105. }
  106. /* Loop unrolling: Compute remaining outputs */
  107. blkCnt = blockSize % 0x4U;
  108. #else
  109. /* Initialize blkCnt with number of samples */
  110. blkCnt = blockSize;
  111. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  112. #endif /* #if defined(ARM_MATH_NEON_EXPERIMENTAL) */
  113. while (blkCnt > 0U)
  114. {
  115. /* C = A * scale */
  116. /* Scale input and store result in destination buffer. */
  117. *pDst++ = (*pSrc++) * scale;
  118. /* Decrement loop counter */
  119. blkCnt--;
  120. }
  121. }
  122. /**
  123. @} end of BasicScale group
  124. */