arm_scale_q31.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_scale_q31.c
  4. * Description: Multiplies a Q31 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. @addtogroup BasicScale
  34. @{
  35. */
  36. /**
  37. @brief Multiplies a Q31 vector by a scalar.
  38. @param[in] pSrc points to the input vector
  39. @param[in] scaleFract fractional portion of the scale value
  40. @param[in] shift number of bits to shift the result by
  41. @param[out] pDst points to the output vector
  42. @param[in] blockSize number of samples in each vector
  43. @return none
  44. @par Scaling and Overflow Behavior
  45. The input data <code>*pSrc</code> and <code>scaleFract</code> are in 1.31 format.
  46. These are multiplied to yield a 2.62 intermediate result and this is shifted with saturation to 1.31 format.
  47. */
  48. void arm_scale_q31(
  49. const q31_t *pSrc,
  50. q31_t scaleFract,
  51. int8_t shift,
  52. q31_t *pDst,
  53. uint32_t blockSize)
  54. {
  55. uint32_t blkCnt; /* Loop counter */
  56. q31_t in, out; /* Temporary variables */
  57. int8_t kShift = shift + 1; /* Shift to apply after scaling */
  58. int8_t sign = (kShift & 0x80);
  59. #if defined (ARM_MATH_LOOPUNROLL)
  60. /* Loop unrolling: Compute 4 outputs at a time */
  61. blkCnt = blockSize >> 2U;
  62. if (sign == 0U)
  63. {
  64. while (blkCnt > 0U)
  65. {
  66. /* C = A * scale */
  67. /* Scale input and store result in destination buffer. */
  68. in = *pSrc++; /* read input from source */
  69. in = ((q63_t) in * scaleFract) >> 32; /* multiply input with scaler value */
  70. out = in << kShift; /* apply shifting */
  71. if (in != (out >> kShift)) /* saturate the result */
  72. out = 0x7FFFFFFF ^ (in >> 31);
  73. *pDst++ = out; /* Store result destination */
  74. in = *pSrc++;
  75. in = ((q63_t) in * scaleFract) >> 32;
  76. out = in << kShift;
  77. if (in != (out >> kShift))
  78. out = 0x7FFFFFFF ^ (in >> 31);
  79. *pDst++ = out;
  80. in = *pSrc++;
  81. in = ((q63_t) in * scaleFract) >> 32;
  82. out = in << kShift;
  83. if (in != (out >> kShift))
  84. out = 0x7FFFFFFF ^ (in >> 31);
  85. *pDst++ = out;
  86. in = *pSrc++;
  87. in = ((q63_t) in * scaleFract) >> 32;
  88. out = in << kShift;
  89. if (in != (out >> kShift))
  90. out = 0x7FFFFFFF ^ (in >> 31);
  91. *pDst++ = out;
  92. /* Decrement loop counter */
  93. blkCnt--;
  94. }
  95. }
  96. else
  97. {
  98. while (blkCnt > 0U)
  99. {
  100. /* C = A * scale */
  101. /* Scale input and store result in destination buffer. */
  102. in = *pSrc++; /* read four inputs from source */
  103. in = ((q63_t) in * scaleFract) >> 32; /* multiply input with scaler value */
  104. out = in >> -kShift; /* apply shifting */
  105. *pDst++ = out; /* Store result destination */
  106. in = *pSrc++;
  107. in = ((q63_t) in * scaleFract) >> 32;
  108. out = in >> -kShift;
  109. *pDst++ = out;
  110. in = *pSrc++;
  111. in = ((q63_t) in * scaleFract) >> 32;
  112. out = in >> -kShift;
  113. *pDst++ = out;
  114. in = *pSrc++;
  115. in = ((q63_t) in * scaleFract) >> 32;
  116. out = in >> -kShift;
  117. *pDst++ = out;
  118. /* Decrement loop counter */
  119. blkCnt--;
  120. }
  121. }
  122. /* Loop unrolling: Compute remaining outputs */
  123. blkCnt = blockSize % 0x4U;
  124. #else
  125. /* Initialize blkCnt with number of samples */
  126. blkCnt = blockSize;
  127. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  128. if (sign == 0U)
  129. {
  130. while (blkCnt > 0U)
  131. {
  132. /* C = A * scale */
  133. /* Scale input and store result in destination buffer. */
  134. in = *pSrc++;
  135. in = ((q63_t) in * scaleFract) >> 32;
  136. out = in << kShift;
  137. if (in != (out >> kShift))
  138. out = 0x7FFFFFFF ^ (in >> 31);
  139. *pDst++ = out;
  140. /* Decrement loop counter */
  141. blkCnt--;
  142. }
  143. }
  144. else
  145. {
  146. while (blkCnt > 0U)
  147. {
  148. /* C = A * scale */
  149. /* Scale input and store result in destination buffer. */
  150. in = *pSrc++;
  151. in = ((q63_t) in * scaleFract) >> 32;
  152. out = in >> -kShift;
  153. *pDst++ = out;
  154. /* Decrement loop counter */
  155. blkCnt--;
  156. }
  157. }
  158. }
  159. /**
  160. @} end of BasicScale group
  161. */