arm_scale_q31.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /* ----------------------------------------------------------------------
  2. * Copyright (C) 2010-2014 ARM Limited. All rights reserved.
  3. *
  4. * $Date: 12. March 2014
  5. * $Revision: V1.4.4
  6. *
  7. * Project: CMSIS DSP Library
  8. * Title: arm_scale_q31.c
  9. *
  10. * Description: Multiplies a Q31 vector by a scalar.
  11. *
  12. * Target Processor: Cortex-M4/Cortex-M3/Cortex-M0
  13. *
  14. * Redistribution and use in source and binary forms, with or without
  15. * modification, are permitted provided that the following conditions
  16. * are met:
  17. * - Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. * - Redistributions in binary form must reproduce the above copyright
  20. * notice, this list of conditions and the following disclaimer in
  21. * the documentation and/or other materials provided with the
  22. * distribution.
  23. * - Neither the name of ARM LIMITED nor the names of its contributors
  24. * may be used to endorse or promote products derived from this
  25. * software without specific prior written permission.
  26. *
  27. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  28. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  29. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  30. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  31. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  32. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  33. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  34. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  35. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  37. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  38. * POSSIBILITY OF SUCH DAMAGE.
  39. * -------------------------------------------------------------------- */
  40. #include "arm_math.h"
  41. /**
  42. * @ingroup groupMath
  43. */
  44. /**
  45. * @addtogroup scale
  46. * @{
  47. */
  48. /**
  49. * @brief Multiplies a Q31 vector by a scalar.
  50. * @param[in] *pSrc points to the input vector
  51. * @param[in] scaleFract fractional portion of the scale value
  52. * @param[in] shift number of bits to shift the result by
  53. * @param[out] *pDst points to the output vector
  54. * @param[in] blockSize number of samples in the vector
  55. * @return none.
  56. *
  57. * <b>Scaling and Overflow Behavior:</b>
  58. * \par
  59. * The input data <code>*pSrc</code> and <code>scaleFract</code> are in 1.31 format.
  60. * These are multiplied to yield a 2.62 intermediate result and this is shifted with saturation to 1.31 format.
  61. */
  62. void arm_scale_q31(
  63. q31_t * pSrc,
  64. q31_t scaleFract,
  65. int8_t shift,
  66. q31_t * pDst,
  67. uint32_t blockSize)
  68. {
  69. int8_t kShift = shift + 1; /* Shift to apply after scaling */
  70. int8_t sign = (kShift & 0x80);
  71. uint32_t blkCnt; /* loop counter */
  72. q31_t in, out;
  73. #ifndef ARM_MATH_CM0_FAMILY
  74. /* Run the below code for Cortex-M4 and Cortex-M3 */
  75. q31_t in1, in2, in3, in4; /* temporary input variables */
  76. q31_t out1, out2, out3, out4; /* temporary output variabels */
  77. /*loop Unrolling */
  78. blkCnt = blockSize >> 2u;
  79. if(sign == 0u)
  80. {
  81. /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
  82. ** a second loop below computes the remaining 1 to 3 samples. */
  83. while(blkCnt > 0u)
  84. {
  85. /* read four inputs from source */
  86. in1 = *pSrc;
  87. in2 = *(pSrc + 1);
  88. in3 = *(pSrc + 2);
  89. in4 = *(pSrc + 3);
  90. /* multiply input with scaler value */
  91. in1 = ((q63_t) in1 * scaleFract) >> 32;
  92. in2 = ((q63_t) in2 * scaleFract) >> 32;
  93. in3 = ((q63_t) in3 * scaleFract) >> 32;
  94. in4 = ((q63_t) in4 * scaleFract) >> 32;
  95. /* apply shifting */
  96. out1 = in1 << kShift;
  97. out2 = in2 << kShift;
  98. /* saturate the results. */
  99. if(in1 != (out1 >> kShift))
  100. out1 = 0x7FFFFFFF ^ (in1 >> 31);
  101. if(in2 != (out2 >> kShift))
  102. out2 = 0x7FFFFFFF ^ (in2 >> 31);
  103. out3 = in3 << kShift;
  104. out4 = in4 << kShift;
  105. *pDst = out1;
  106. *(pDst + 1) = out2;
  107. if(in3 != (out3 >> kShift))
  108. out3 = 0x7FFFFFFF ^ (in3 >> 31);
  109. if(in4 != (out4 >> kShift))
  110. out4 = 0x7FFFFFFF ^ (in4 >> 31);
  111. /* Store result destination */
  112. *(pDst + 2) = out3;
  113. *(pDst + 3) = out4;
  114. /* Update pointers to process next sampels */
  115. pSrc += 4u;
  116. pDst += 4u;
  117. /* Decrement the loop counter */
  118. blkCnt--;
  119. }
  120. }
  121. else
  122. {
  123. /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
  124. ** a second loop below computes the remaining 1 to 3 samples. */
  125. while(blkCnt > 0u)
  126. {
  127. /* read four inputs from source */
  128. in1 = *pSrc;
  129. in2 = *(pSrc + 1);
  130. in3 = *(pSrc + 2);
  131. in4 = *(pSrc + 3);
  132. /* multiply input with scaler value */
  133. in1 = ((q63_t) in1 * scaleFract) >> 32;
  134. in2 = ((q63_t) in2 * scaleFract) >> 32;
  135. in3 = ((q63_t) in3 * scaleFract) >> 32;
  136. in4 = ((q63_t) in4 * scaleFract) >> 32;
  137. /* apply shifting */
  138. out1 = in1 >> -kShift;
  139. out2 = in2 >> -kShift;
  140. out3 = in3 >> -kShift;
  141. out4 = in4 >> -kShift;
  142. /* Store result destination */
  143. *pDst = out1;
  144. *(pDst + 1) = out2;
  145. *(pDst + 2) = out3;
  146. *(pDst + 3) = out4;
  147. /* Update pointers to process next sampels */
  148. pSrc += 4u;
  149. pDst += 4u;
  150. /* Decrement the loop counter */
  151. blkCnt--;
  152. }
  153. }
  154. /* If the blockSize is not a multiple of 4, compute any remaining output samples here.
  155. ** No loop unrolling is used. */
  156. blkCnt = blockSize % 0x4u;
  157. #else
  158. /* Run the below code for Cortex-M0 */
  159. /* Initialize blkCnt with number of samples */
  160. blkCnt = blockSize;
  161. #endif /* #ifndef ARM_MATH_CM0_FAMILY */
  162. if(sign == 0)
  163. {
  164. while(blkCnt > 0u)
  165. {
  166. /* C = A * scale */
  167. /* Scale the input and then store the result in the destination buffer. */
  168. in = *pSrc++;
  169. in = ((q63_t) in * scaleFract) >> 32;
  170. out = in << kShift;
  171. if(in != (out >> kShift))
  172. out = 0x7FFFFFFF ^ (in >> 31);
  173. *pDst++ = out;
  174. /* Decrement the loop counter */
  175. blkCnt--;
  176. }
  177. }
  178. else
  179. {
  180. while(blkCnt > 0u)
  181. {
  182. /* C = A * scale */
  183. /* Scale the input and then store the result in the destination buffer. */
  184. in = *pSrc++;
  185. in = ((q63_t) in * scaleFract) >> 32;
  186. out = in >> -kShift;
  187. *pDst++ = out;
  188. /* Decrement the loop counter */
  189. blkCnt--;
  190. }
  191. }
  192. }
  193. /**
  194. * @} end of scale group
  195. */