arm_shift_q15.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_shift_q15.c
  4. * Description: Shifts the elements of a Q15 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. @addtogroup BasicShift
  34. @{
  35. */
  36. /**
  37. @brief Shifts the elements of a Q15 vector a specified number of bits
  38. @param[in] pSrc points to the input vector
  39. @param[in] shiftBits number of bits to shift. A positive value shifts left; a negative value shifts right.
  40. @param[out] pDst points to the output vector
  41. @param[in] blockSize number of samples in each vector
  42. @return none
  43. @par Scaling and Overflow Behavior
  44. The function uses saturating arithmetic.
  45. Results outside of the allowable Q15 range [0x8000 0x7FFF] are saturated.
  46. */
  47. void arm_shift_q15(
  48. const q15_t * pSrc,
  49. int8_t shiftBits,
  50. q15_t * pDst,
  51. uint32_t blockSize)
  52. {
  53. uint32_t blkCnt; /* Loop counter */
  54. uint8_t sign = (shiftBits & 0x80); /* Sign of shiftBits */
  55. #if defined (ARM_MATH_LOOPUNROLL)
  56. #if defined (ARM_MATH_DSP)
  57. q15_t in1, in2; /* Temporary input variables */
  58. #endif
  59. /* Loop unrolling: Compute 4 outputs at a time */
  60. blkCnt = blockSize >> 2U;
  61. /* If the shift value is positive then do right shift else left shift */
  62. if (sign == 0U)
  63. {
  64. while (blkCnt > 0U)
  65. {
  66. /* C = A << shiftBits */
  67. #if defined (ARM_MATH_DSP)
  68. /* read 2 samples from source */
  69. in1 = *pSrc++;
  70. in2 = *pSrc++;
  71. /* Shift the inputs and then store the results in the destination buffer. */
  72. #ifndef ARM_MATH_BIG_ENDIAN
  73. write_q15x2_ia (&pDst, __PKHBT(__SSAT((in1 << shiftBits), 16),
  74. __SSAT((in2 << shiftBits), 16), 16));
  75. #else
  76. write_q15x2_ia (&pDst, __PKHBT(__SSAT((in2 << shiftBits), 16),
  77. __SSAT((in1 << shiftBits), 16), 16));
  78. #endif /* #ifndef ARM_MATH_BIG_ENDIAN */
  79. /* read 2 samples from source */
  80. in1 = *pSrc++;
  81. in2 = *pSrc++;
  82. #ifndef ARM_MATH_BIG_ENDIAN
  83. write_q15x2_ia (&pDst, __PKHBT(__SSAT((in1 << shiftBits), 16),
  84. __SSAT((in2 << shiftBits), 16), 16));
  85. #else
  86. write_q15x2_ia (&pDst, __PKHBT(__SSAT((in2 << shiftBits), 16),
  87. __SSAT((in1 << shiftBits), 16), 16));
  88. #endif /* #ifndef ARM_MATH_BIG_ENDIAN */
  89. #else
  90. *pDst++ = __SSAT(((q31_t) *pSrc++ << shiftBits), 16);
  91. *pDst++ = __SSAT(((q31_t) *pSrc++ << shiftBits), 16);
  92. *pDst++ = __SSAT(((q31_t) *pSrc++ << shiftBits), 16);
  93. *pDst++ = __SSAT(((q31_t) *pSrc++ << shiftBits), 16);
  94. #endif
  95. /* Decrement loop counter */
  96. blkCnt--;
  97. }
  98. }
  99. else
  100. {
  101. while (blkCnt > 0U)
  102. {
  103. /* C = A >> shiftBits */
  104. #if defined (ARM_MATH_DSP)
  105. /* read 2 samples from source */
  106. in1 = *pSrc++;
  107. in2 = *pSrc++;
  108. /* Shift the inputs and then store the results in the destination buffer. */
  109. #ifndef ARM_MATH_BIG_ENDIAN
  110. write_q15x2_ia (&pDst, __PKHBT((in1 >> -shiftBits),
  111. (in2 >> -shiftBits), 16));
  112. #else
  113. write_q15x2_ia (&pDst, __PKHBT((in2 >> -shiftBits),
  114. (in1 >> -shiftBits), 16));
  115. #endif /* #ifndef ARM_MATH_BIG_ENDIAN */
  116. /* read 2 samples from source */
  117. in1 = *pSrc++;
  118. in2 = *pSrc++;
  119. #ifndef ARM_MATH_BIG_ENDIAN
  120. write_q15x2_ia (&pDst, __PKHBT((in1 >> -shiftBits),
  121. (in2 >> -shiftBits), 16));
  122. #else
  123. write_q15x2_ia (&pDst, __PKHBT((in2 >> -shiftBits),
  124. (in1 >> -shiftBits), 16));
  125. #endif /* #ifndef ARM_MATH_BIG_ENDIAN */
  126. #else
  127. *pDst++ = (*pSrc++ >> -shiftBits);
  128. *pDst++ = (*pSrc++ >> -shiftBits);
  129. *pDst++ = (*pSrc++ >> -shiftBits);
  130. *pDst++ = (*pSrc++ >> -shiftBits);
  131. #endif
  132. /* Decrement loop counter */
  133. blkCnt--;
  134. }
  135. }
  136. /* Loop unrolling: Compute remaining outputs */
  137. blkCnt = blockSize % 0x4U;
  138. #else
  139. /* Initialize blkCnt with number of samples */
  140. blkCnt = blockSize;
  141. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  142. /* If the shift value is positive then do right shift else left shift */
  143. if (sign == 0U)
  144. {
  145. while (blkCnt > 0U)
  146. {
  147. /* C = A << shiftBits */
  148. /* Shift input and store result in destination buffer. */
  149. *pDst++ = __SSAT(((q31_t) *pSrc++ << shiftBits), 16);
  150. /* Decrement loop counter */
  151. blkCnt--;
  152. }
  153. }
  154. else
  155. {
  156. while (blkCnt > 0U)
  157. {
  158. /* C = A >> shiftBits */
  159. /* Shift input and store result in destination buffer. */
  160. *pDst++ = (*pSrc++ >> -shiftBits);
  161. /* Decrement loop counter */
  162. blkCnt--;
  163. }
  164. }
  165. }
  166. /**
  167. @} end of BasicShift group
  168. */