arm_scale_f32.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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: 27. January 2017
  7. * $Revision: V.1.5.1
  8. *
  9. * Target Processor: Cortex-M cores
  10. * -------------------------------------------------------------------- */
  11. /*
  12. * Copyright (C) 2010-2017 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 scale Vector Scale
  34. *
  35. * Multiply a vector by a scalar value. For floating-point data, the algorithm used is:
  36. *
  37. * <pre>
  38. * pDst[n] = pSrc[n] * scale, 0 <= n < blockSize.
  39. * </pre>
  40. *
  41. * In the fixed-point Q7, Q15, and Q31 functions, <code>scale</code> is represented by
  42. * a fractional multiplication <code>scaleFract</code> and an arithmetic shift <code>shift</code>.
  43. * The shift allows the gain of the scaling operation to exceed 1.0.
  44. * The algorithm used with fixed-point data is:
  45. *
  46. * <pre>
  47. * pDst[n] = (pSrc[n] * scaleFract) << shift, 0 <= n < blockSize.
  48. * </pre>
  49. *
  50. * The overall scale factor applied to the fixed-point data is
  51. * <pre>
  52. * scale = scaleFract * 2^shift.
  53. * </pre>
  54. *
  55. * The functions support in-place computation allowing the source and destination
  56. * pointers to reference the same memory buffer.
  57. */
  58. /**
  59. * @addtogroup scale
  60. * @{
  61. */
  62. /**
  63. * @brief Multiplies a floating-point vector by a scalar.
  64. * @param[in] *pSrc points to the input vector
  65. * @param[in] scale scale factor to be applied
  66. * @param[out] *pDst points to the output vector
  67. * @param[in] blockSize number of samples in the vector
  68. * @return none.
  69. */
  70. void arm_scale_f32(
  71. float32_t * pSrc,
  72. float32_t scale,
  73. float32_t * pDst,
  74. uint32_t blockSize)
  75. {
  76. uint32_t blkCnt; /* loop counter */
  77. #if defined (ARM_MATH_DSP)
  78. /* Run the below code for Cortex-M4 and Cortex-M3 */
  79. float32_t in1, in2, in3, in4; /* temporary variabels */
  80. /*loop Unrolling */
  81. blkCnt = blockSize >> 2U;
  82. /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
  83. ** a second loop below computes the remaining 1 to 3 samples. */
  84. while (blkCnt > 0U)
  85. {
  86. /* C = A * scale */
  87. /* Scale the input and then store the results in the destination buffer. */
  88. /* read input samples from source */
  89. in1 = *pSrc;
  90. in2 = *(pSrc + 1);
  91. /* multiply with scaling factor */
  92. in1 = in1 * scale;
  93. /* read input sample from source */
  94. in3 = *(pSrc + 2);
  95. /* multiply with scaling factor */
  96. in2 = in2 * scale;
  97. /* read input sample from source */
  98. in4 = *(pSrc + 3);
  99. /* multiply with scaling factor */
  100. in3 = in3 * scale;
  101. in4 = in4 * scale;
  102. /* store the result to destination */
  103. *pDst = in1;
  104. *(pDst + 1) = in2;
  105. *(pDst + 2) = in3;
  106. *(pDst + 3) = in4;
  107. /* update pointers to process next samples */
  108. pSrc += 4U;
  109. pDst += 4U;
  110. /* Decrement the loop counter */
  111. blkCnt--;
  112. }
  113. /* If the blockSize is not a multiple of 4, compute any remaining output samples here.
  114. ** No loop unrolling is used. */
  115. blkCnt = blockSize % 0x4U;
  116. #else
  117. /* Run the below code for Cortex-M0 */
  118. /* Initialize blkCnt with number of samples */
  119. blkCnt = blockSize;
  120. #endif /* #if defined (ARM_MATH_DSP) */
  121. while (blkCnt > 0U)
  122. {
  123. /* C = A * scale */
  124. /* Scale the input and then store the result in the destination buffer. */
  125. *pDst++ = (*pSrc++) * scale;
  126. /* Decrement the loop counter */
  127. blkCnt--;
  128. }
  129. }
  130. /**
  131. * @} end of scale group
  132. */