arm_shift_q31.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_shift_q31.c
  4. * Description: Shifts the elements of a Q31 vector by a specified number of bits
  5. *
  6. * $Date: 23 April 2021
  7. * $Revision: V1.9.0
  8. *
  9. * Target Processor: Cortex-M and Cortex-A cores
  10. * -------------------------------------------------------------------- */
  11. /*
  12. * Copyright (C) 2010-2021 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 "dsp/basic_math_functions.h"
  29. /**
  30. @ingroup groupMath
  31. */
  32. /**
  33. @defgroup BasicShift Vector Shift
  34. Shifts the elements of a fixed-point vector by a specified number of bits.
  35. There are separate functions for Q7, Q15, and Q31 data types.
  36. The underlying algorithm used is:
  37. <pre>
  38. pDst[n] = pSrc[n] << shift, 0 <= n < blockSize.
  39. </pre>
  40. If <code>shift</code> is positive then the elements of the vector are shifted to the left.
  41. If <code>shift</code> is negative then the elements of the vector are shifted to the right.
  42. The functions support in-place computation allowing the source and destination
  43. pointers to reference the same memory buffer.
  44. */
  45. /**
  46. @addtogroup BasicShift
  47. @{
  48. */
  49. /**
  50. @brief Shifts the elements of a Q31 vector a specified number of bits.
  51. @param[in] pSrc points to the input vector
  52. @param[in] shiftBits number of bits to shift. A positive value shifts left; a negative value shifts right.
  53. @param[out] pDst points to the output vector
  54. @param[in] blockSize number of samples in the vector
  55. @par Scaling and Overflow Behavior
  56. The function uses saturating arithmetic.
  57. Results outside of the allowable Q31 range [0x80000000 0x7FFFFFFF] are saturated.
  58. */
  59. #if defined(ARM_MATH_MVEI) && !defined(ARM_MATH_AUTOVECTORIZE)
  60. #include "arm_helium_utils.h"
  61. void arm_shift_q31(
  62. const q31_t * pSrc,
  63. int8_t shiftBits,
  64. q31_t * pDst,
  65. uint32_t blockSize)
  66. {
  67. uint32_t blkCnt; /* loop counters */
  68. q31x4_t vecSrc;
  69. q31x4_t vecDst;
  70. /* Compute 4 outputs at a time */
  71. blkCnt = blockSize >> 2;
  72. while (blkCnt > 0U)
  73. {
  74. /*
  75. * C = A (>> or <<) shiftBits
  76. * Shift the input and then store the result in the destination buffer.
  77. */
  78. vecSrc = vld1q((q31_t const *) pSrc);
  79. vecDst = vqshlq_r(vecSrc, shiftBits);
  80. vst1q(pDst, vecDst);
  81. /*
  82. * Decrement the blockSize loop counter
  83. */
  84. blkCnt--;
  85. /*
  86. * advance vector source and destination pointers
  87. */
  88. pSrc += 4;
  89. pDst += 4;
  90. }
  91. /*
  92. * tail
  93. */
  94. blkCnt = blockSize & 3;
  95. if (blkCnt > 0U)
  96. {
  97. mve_pred16_t p0 = vctp32q(blkCnt);
  98. vecSrc = vld1q((q31_t const *) pSrc);
  99. vecDst = vqshlq_r(vecSrc, shiftBits);
  100. vstrwq_p(pDst, vecDst, p0);
  101. }
  102. }
  103. #else
  104. void arm_shift_q31(
  105. const q31_t * pSrc,
  106. int8_t shiftBits,
  107. q31_t * pDst,
  108. uint32_t blockSize)
  109. {
  110. uint32_t blkCnt; /* Loop counter */
  111. uint8_t sign = (shiftBits & 0x80); /* Sign of shiftBits */
  112. #if defined (ARM_MATH_LOOPUNROLL)
  113. q31_t in, out; /* Temporary variables */
  114. /* Loop unrolling: Compute 4 outputs at a time */
  115. blkCnt = blockSize >> 2U;
  116. /* If the shift value is positive then do right shift else left shift */
  117. if (sign == 0U)
  118. {
  119. while (blkCnt > 0U)
  120. {
  121. /* C = A << shiftBits */
  122. /* Shift input and store result in destination buffer. */
  123. in = *pSrc++;
  124. out = in << shiftBits;
  125. if (in != (out >> shiftBits))
  126. out = 0x7FFFFFFF ^ (in >> 31);
  127. *pDst++ = out;
  128. in = *pSrc++;
  129. out = in << shiftBits;
  130. if (in != (out >> shiftBits))
  131. out = 0x7FFFFFFF ^ (in >> 31);
  132. *pDst++ = out;
  133. in = *pSrc++;
  134. out = in << shiftBits;
  135. if (in != (out >> shiftBits))
  136. out = 0x7FFFFFFF ^ (in >> 31);
  137. *pDst++ = out;
  138. in = *pSrc++;
  139. out = in << shiftBits;
  140. if (in != (out >> shiftBits))
  141. out = 0x7FFFFFFF ^ (in >> 31);
  142. *pDst++ = out;
  143. /* Decrement loop counter */
  144. blkCnt--;
  145. }
  146. }
  147. else
  148. {
  149. while (blkCnt > 0U)
  150. {
  151. /* C = A >> shiftBits */
  152. /* Shift input and store results in destination buffer. */
  153. *pDst++ = (*pSrc++ >> -shiftBits);
  154. *pDst++ = (*pSrc++ >> -shiftBits);
  155. *pDst++ = (*pSrc++ >> -shiftBits);
  156. *pDst++ = (*pSrc++ >> -shiftBits);
  157. /* Decrement loop counter */
  158. blkCnt--;
  159. }
  160. }
  161. /* Loop unrolling: Compute remaining outputs */
  162. blkCnt = blockSize % 0x4U;
  163. #else
  164. /* Initialize blkCnt with number of samples */
  165. blkCnt = blockSize;
  166. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  167. /* If the shift value is positive then do right shift else left shift */
  168. if (sign == 0U)
  169. {
  170. while (blkCnt > 0U)
  171. {
  172. /* C = A << shiftBits */
  173. /* Shift input and store result in destination buffer. */
  174. *pDst++ = clip_q63_to_q31((q63_t) *pSrc++ << shiftBits);
  175. /* Decrement loop counter */
  176. blkCnt--;
  177. }
  178. }
  179. else
  180. {
  181. while (blkCnt > 0U)
  182. {
  183. /* C = A >> shiftBits */
  184. /* Shift input and store result in destination buffer. */
  185. *pDst++ = (*pSrc++ >> -shiftBits);
  186. /* Decrement loop counter */
  187. blkCnt--;
  188. }
  189. }
  190. }
  191. #endif /* defined(ARM_MATH_MVEI) */
  192. /**
  193. @} end of BasicShift group
  194. */