arm_shift_q31.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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: 27. January 2017
  7. * $Revision: V.1.5.1
  8. *
  9. * Target Processor: Cortex-M cores
  10. * -------------------------------------------------------------------- */
  11. /*
  12. * Copyright (C) 2010-2017 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 shift Vector Shift
  34. *
  35. * Shifts the elements of a fixed-point vector by a specified number of bits.
  36. * There are separate functions for Q7, Q15, and Q31 data types.
  37. * The underlying algorithm used is:
  38. *
  39. * <pre>
  40. * pDst[n] = pSrc[n] << shift, 0 <= n < blockSize.
  41. * </pre>
  42. *
  43. * If <code>shift</code> is positive then the elements of the vector are shifted to the left.
  44. * If <code>shift</code> is negative then the elements of the vector are shifted to the right.
  45. *
  46. * The functions support in-place computation allowing the source and destination
  47. * pointers to reference the same memory buffer.
  48. */
  49. /**
  50. * @addtogroup shift
  51. * @{
  52. */
  53. /**
  54. * @brief Shifts the elements of a Q31 vector a specified number of bits.
  55. * @param[in] *pSrc points to the input vector
  56. * @param[in] shiftBits number of bits to shift. A positive value shifts left; a negative value shifts right.
  57. * @param[out] *pDst points to the output vector
  58. * @param[in] blockSize number of samples in the vector
  59. * @return none.
  60. *
  61. *
  62. * <b>Scaling and Overflow Behavior:</b>
  63. * \par
  64. * The function uses saturating arithmetic.
  65. * Results outside of the allowable Q31 range [0x80000000 0x7FFFFFFF] will be saturated.
  66. */
  67. void arm_shift_q31(
  68. q31_t * pSrc,
  69. int8_t shiftBits,
  70. q31_t * pDst,
  71. uint32_t blockSize)
  72. {
  73. uint32_t blkCnt; /* loop counter */
  74. uint8_t sign = (shiftBits & 0x80); /* Sign of shiftBits */
  75. #if defined (ARM_MATH_DSP)
  76. q31_t in1, in2, in3, in4; /* Temporary input variables */
  77. q31_t out1, out2, out3, out4; /* Temporary output variables */
  78. /*loop Unrolling */
  79. blkCnt = blockSize >> 2U;
  80. if (sign == 0U)
  81. {
  82. /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
  83. ** a second loop below computes the remaining 1 to 3 samples. */
  84. while (blkCnt > 0U)
  85. {
  86. /* C = A << shiftBits */
  87. /* Shift the input and then store the results in the destination buffer. */
  88. in1 = *pSrc;
  89. in2 = *(pSrc + 1);
  90. out1 = in1 << shiftBits;
  91. in3 = *(pSrc + 2);
  92. out2 = in2 << shiftBits;
  93. in4 = *(pSrc + 3);
  94. if (in1 != (out1 >> shiftBits))
  95. out1 = 0x7FFFFFFF ^ (in1 >> 31);
  96. if (in2 != (out2 >> shiftBits))
  97. out2 = 0x7FFFFFFF ^ (in2 >> 31);
  98. *pDst = out1;
  99. out3 = in3 << shiftBits;
  100. *(pDst + 1) = out2;
  101. out4 = in4 << shiftBits;
  102. if (in3 != (out3 >> shiftBits))
  103. out3 = 0x7FFFFFFF ^ (in3 >> 31);
  104. if (in4 != (out4 >> shiftBits))
  105. out4 = 0x7FFFFFFF ^ (in4 >> 31);
  106. *(pDst + 2) = out3;
  107. *(pDst + 3) = out4;
  108. /* Update destination pointer to process next sampels */
  109. pSrc += 4U;
  110. pDst += 4U;
  111. /* Decrement the loop counter */
  112. blkCnt--;
  113. }
  114. }
  115. else
  116. {
  117. /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
  118. ** a second loop below computes the remaining 1 to 3 samples. */
  119. while (blkCnt > 0U)
  120. {
  121. /* C = A >> shiftBits */
  122. /* Shift the input and then store the results in the destination buffer. */
  123. in1 = *pSrc;
  124. in2 = *(pSrc + 1);
  125. in3 = *(pSrc + 2);
  126. in4 = *(pSrc + 3);
  127. *pDst = (in1 >> -shiftBits);
  128. *(pDst + 1) = (in2 >> -shiftBits);
  129. *(pDst + 2) = (in3 >> -shiftBits);
  130. *(pDst + 3) = (in4 >> -shiftBits);
  131. pSrc += 4U;
  132. pDst += 4U;
  133. blkCnt--;
  134. }
  135. }
  136. /* If the blockSize is not a multiple of 4, compute any remaining output samples here.
  137. ** No loop unrolling is used. */
  138. blkCnt = blockSize % 0x4U;
  139. #else
  140. /* Run the below code for Cortex-M0 */
  141. /* Initialize blkCnt with number of samples */
  142. blkCnt = blockSize;
  143. #endif /* #if defined (ARM_MATH_DSP) */
  144. while (blkCnt > 0U)
  145. {
  146. /* C = A (>> or <<) shiftBits */
  147. /* Shift the input and then store the result in the destination buffer. */
  148. *pDst++ = (sign == 0U) ? clip_q63_to_q31((q63_t) * pSrc++ << shiftBits) :
  149. (*pSrc++ >> -shiftBits);
  150. /* Decrement the loop counter */
  151. blkCnt--;
  152. }
  153. }
  154. /**
  155. * @} end of shift group
  156. */