arm_shift_q31.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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: 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 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. @return none
  56. @par Scaling and Overflow Behavior
  57. The function uses saturating arithmetic.
  58. Results outside of the allowable Q31 range [0x80000000 0x7FFFFFFF] are saturated.
  59. */
  60. #if defined(ARM_MATH_MVEI)
  61. #include "arm_helium_utils.h"
  62. void arm_shift_q31(
  63. const q31_t * pSrc,
  64. int8_t shiftBits,
  65. q31_t * pDst,
  66. uint32_t blockSize)
  67. {
  68. uint32_t blkCnt; /* loop counters */
  69. q31x4_t vecSrc;
  70. q31x4_t vecDst;
  71. /* Compute 4 outputs at a time */
  72. blkCnt = blockSize >> 2;
  73. while (blkCnt > 0U)
  74. {
  75. /*
  76. * C = A (>> or <<) shiftBits
  77. * Shift the input and then store the result in the destination buffer.
  78. */
  79. vecSrc = vld1q((q31_t const *) pSrc);
  80. vecDst = vqshlq_r(vecSrc, shiftBits);
  81. vst1q(pDst, vecDst);
  82. /*
  83. * Decrement the blockSize loop counter
  84. */
  85. blkCnt--;
  86. /*
  87. * advance vector source and destination pointers
  88. */
  89. pSrc += 4;
  90. pDst += 4;
  91. }
  92. /*
  93. * tail
  94. */
  95. blkCnt = blockSize & 3;
  96. if (blkCnt > 0U)
  97. {
  98. mve_pred16_t p0 = vctp32q(blkCnt);
  99. vecSrc = vld1q((q31_t const *) pSrc);
  100. vecDst = vqshlq_r(vecSrc, shiftBits);
  101. vstrwq_p(pDst, vecDst, p0);
  102. }
  103. }
  104. #else
  105. void arm_shift_q31(
  106. const q31_t * pSrc,
  107. int8_t shiftBits,
  108. q31_t * pDst,
  109. uint32_t blockSize)
  110. {
  111. uint32_t blkCnt; /* Loop counter */
  112. uint8_t sign = (shiftBits & 0x80); /* Sign of shiftBits */
  113. #if defined (ARM_MATH_LOOPUNROLL)
  114. q31_t in, out; /* Temporary variables */
  115. /* Loop unrolling: Compute 4 outputs at a time */
  116. blkCnt = blockSize >> 2U;
  117. /* If the shift value is positive then do right shift else left shift */
  118. if (sign == 0U)
  119. {
  120. while (blkCnt > 0U)
  121. {
  122. /* C = A << shiftBits */
  123. /* Shift input and store result in destination buffer. */
  124. in = *pSrc++;
  125. out = in << shiftBits;
  126. if (in != (out >> shiftBits))
  127. out = 0x7FFFFFFF ^ (in >> 31);
  128. *pDst++ = out;
  129. in = *pSrc++;
  130. out = in << shiftBits;
  131. if (in != (out >> shiftBits))
  132. out = 0x7FFFFFFF ^ (in >> 31);
  133. *pDst++ = out;
  134. in = *pSrc++;
  135. out = in << shiftBits;
  136. if (in != (out >> shiftBits))
  137. out = 0x7FFFFFFF ^ (in >> 31);
  138. *pDst++ = out;
  139. in = *pSrc++;
  140. out = in << shiftBits;
  141. if (in != (out >> shiftBits))
  142. out = 0x7FFFFFFF ^ (in >> 31);
  143. *pDst++ = out;
  144. /* Decrement loop counter */
  145. blkCnt--;
  146. }
  147. }
  148. else
  149. {
  150. while (blkCnt > 0U)
  151. {
  152. /* C = A >> shiftBits */
  153. /* Shift input and store results in destination buffer. */
  154. *pDst++ = (*pSrc++ >> -shiftBits);
  155. *pDst++ = (*pSrc++ >> -shiftBits);
  156. *pDst++ = (*pSrc++ >> -shiftBits);
  157. *pDst++ = (*pSrc++ >> -shiftBits);
  158. /* Decrement loop counter */
  159. blkCnt--;
  160. }
  161. }
  162. /* Loop unrolling: Compute remaining outputs */
  163. blkCnt = blockSize % 0x4U;
  164. #else
  165. /* Initialize blkCnt with number of samples */
  166. blkCnt = blockSize;
  167. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  168. /* If the shift value is positive then do right shift else left shift */
  169. if (sign == 0U)
  170. {
  171. while (blkCnt > 0U)
  172. {
  173. /* C = A << shiftBits */
  174. /* Shift input and store result in destination buffer. */
  175. *pDst++ = clip_q63_to_q31((q63_t) *pSrc++ << shiftBits);
  176. /* Decrement loop counter */
  177. blkCnt--;
  178. }
  179. }
  180. else
  181. {
  182. while (blkCnt > 0U)
  183. {
  184. /* C = A >> shiftBits */
  185. /* Shift input and store result in destination buffer. */
  186. *pDst++ = (*pSrc++ >> -shiftBits);
  187. /* Decrement loop counter */
  188. blkCnt--;
  189. }
  190. }
  191. }
  192. #endif /* defined(ARM_MATH_MVEI) */
  193. /**
  194. @} end of BasicShift group
  195. */