arm_shift_q31.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. void arm_shift_q31(
  61. const q31_t * pSrc,
  62. int8_t shiftBits,
  63. q31_t * pDst,
  64. uint32_t blockSize)
  65. {
  66. uint32_t blkCnt; /* Loop counter */
  67. uint8_t sign = (shiftBits & 0x80); /* Sign of shiftBits */
  68. #if defined (ARM_MATH_LOOPUNROLL)
  69. q31_t in, out; /* Temporary variables */
  70. /* Loop unrolling: Compute 4 outputs at a time */
  71. blkCnt = blockSize >> 2U;
  72. /* If the shift value is positive then do right shift else left shift */
  73. if (sign == 0U)
  74. {
  75. while (blkCnt > 0U)
  76. {
  77. /* C = A << shiftBits */
  78. /* Shift input and store result in destination buffer. */
  79. in = *pSrc++;
  80. out = in << shiftBits;
  81. if (in != (out >> shiftBits))
  82. out = 0x7FFFFFFF ^ (in >> 31);
  83. *pDst++ = out;
  84. in = *pSrc++;
  85. out = in << shiftBits;
  86. if (in != (out >> shiftBits))
  87. out = 0x7FFFFFFF ^ (in >> 31);
  88. *pDst++ = out;
  89. in = *pSrc++;
  90. out = in << shiftBits;
  91. if (in != (out >> shiftBits))
  92. out = 0x7FFFFFFF ^ (in >> 31);
  93. *pDst++ = out;
  94. in = *pSrc++;
  95. out = in << shiftBits;
  96. if (in != (out >> shiftBits))
  97. out = 0x7FFFFFFF ^ (in >> 31);
  98. *pDst++ = out;
  99. /* Decrement loop counter */
  100. blkCnt--;
  101. }
  102. }
  103. else
  104. {
  105. while (blkCnt > 0U)
  106. {
  107. /* C = A >> shiftBits */
  108. /* Shift input and store results in destination buffer. */
  109. *pDst++ = (*pSrc++ >> -shiftBits);
  110. *pDst++ = (*pSrc++ >> -shiftBits);
  111. *pDst++ = (*pSrc++ >> -shiftBits);
  112. *pDst++ = (*pSrc++ >> -shiftBits);
  113. /* Decrement loop counter */
  114. blkCnt--;
  115. }
  116. }
  117. /* Loop unrolling: Compute remaining outputs */
  118. blkCnt = blockSize % 0x4U;
  119. #else
  120. /* Initialize blkCnt with number of samples */
  121. blkCnt = blockSize;
  122. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  123. /* If the shift value is positive then do right shift else left shift */
  124. if (sign == 0U)
  125. {
  126. while (blkCnt > 0U)
  127. {
  128. /* C = A << shiftBits */
  129. /* Shift input and store result in destination buffer. */
  130. *pDst++ = clip_q63_to_q31((q63_t) *pSrc++ << shiftBits);
  131. /* Decrement loop counter */
  132. blkCnt--;
  133. }
  134. }
  135. else
  136. {
  137. while (blkCnt > 0U)
  138. {
  139. /* C = A >> shiftBits */
  140. /* Shift input and store result in destination buffer. */
  141. *pDst++ = (*pSrc++ >> -shiftBits);
  142. /* Decrement loop counter */
  143. blkCnt--;
  144. }
  145. }
  146. }
  147. /**
  148. @} end of BasicShift group
  149. */