arm_shift_q15.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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: 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. * @addtogroup shift
  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 the vector
  42. * @return none.
  43. *
  44. * <b>Scaling and Overflow Behavior:</b>
  45. * \par
  46. * The function uses saturating arithmetic.
  47. * Results outside of the allowable Q15 range [0x8000 0x7FFF] will be saturated.
  48. */
  49. void arm_shift_q15(
  50. q15_t * pSrc,
  51. int8_t shiftBits,
  52. q15_t * pDst,
  53. uint32_t blockSize)
  54. {
  55. uint32_t blkCnt; /* loop counter */
  56. uint8_t sign; /* Sign of shiftBits */
  57. #if defined (ARM_MATH_DSP)
  58. /* Run the below code for Cortex-M4 and Cortex-M3 */
  59. q15_t in1, in2; /* Temporary variables */
  60. /*loop Unrolling */
  61. blkCnt = blockSize >> 2U;
  62. /* Getting the sign of shiftBits */
  63. sign = (shiftBits & 0x80);
  64. /* If the shift value is positive then do right shift else left shift */
  65. if (sign == 0U)
  66. {
  67. /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
  68. ** a second loop below computes the remaining 1 to 3 samples. */
  69. while (blkCnt > 0U)
  70. {
  71. /* Read 2 inputs */
  72. in1 = *pSrc++;
  73. in2 = *pSrc++;
  74. /* C = A << shiftBits */
  75. /* Shift the inputs and then store the results in the destination buffer. */
  76. #ifndef ARM_MATH_BIG_ENDIAN
  77. *__SIMD32(pDst)++ = __PKHBT(__SSAT((in1 << shiftBits), 16),
  78. __SSAT((in2 << shiftBits), 16), 16);
  79. #else
  80. *__SIMD32(pDst)++ = __PKHBT(__SSAT((in2 << shiftBits), 16),
  81. __SSAT((in1 << shiftBits), 16), 16);
  82. #endif /* #ifndef ARM_MATH_BIG_ENDIAN */
  83. in1 = *pSrc++;
  84. in2 = *pSrc++;
  85. #ifndef ARM_MATH_BIG_ENDIAN
  86. *__SIMD32(pDst)++ = __PKHBT(__SSAT((in1 << shiftBits), 16),
  87. __SSAT((in2 << shiftBits), 16), 16);
  88. #else
  89. *__SIMD32(pDst)++ = __PKHBT(__SSAT((in2 << shiftBits), 16),
  90. __SSAT((in1 << shiftBits), 16), 16);
  91. #endif /* #ifndef ARM_MATH_BIG_ENDIAN */
  92. /* Decrement the loop counter */
  93. blkCnt--;
  94. }
  95. /* If the blockSize is not a multiple of 4, compute any remaining output samples here.
  96. ** No loop unrolling is used. */
  97. blkCnt = blockSize % 0x4U;
  98. while (blkCnt > 0U)
  99. {
  100. /* C = A << shiftBits */
  101. /* Shift and then store the results in the destination buffer. */
  102. *pDst++ = __SSAT((*pSrc++ << shiftBits), 16);
  103. /* Decrement the loop counter */
  104. blkCnt--;
  105. }
  106. }
  107. else
  108. {
  109. /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
  110. ** a second loop below computes the remaining 1 to 3 samples. */
  111. while (blkCnt > 0U)
  112. {
  113. /* Read 2 inputs */
  114. in1 = *pSrc++;
  115. in2 = *pSrc++;
  116. /* C = A >> shiftBits */
  117. /* Shift the inputs and then store the results in the destination buffer. */
  118. #ifndef ARM_MATH_BIG_ENDIAN
  119. *__SIMD32(pDst)++ = __PKHBT((in1 >> -shiftBits),
  120. (in2 >> -shiftBits), 16);
  121. #else
  122. *__SIMD32(pDst)++ = __PKHBT((in2 >> -shiftBits),
  123. (in1 >> -shiftBits), 16);
  124. #endif /* #ifndef ARM_MATH_BIG_ENDIAN */
  125. in1 = *pSrc++;
  126. in2 = *pSrc++;
  127. #ifndef ARM_MATH_BIG_ENDIAN
  128. *__SIMD32(pDst)++ = __PKHBT((in1 >> -shiftBits),
  129. (in2 >> -shiftBits), 16);
  130. #else
  131. *__SIMD32(pDst)++ = __PKHBT((in2 >> -shiftBits),
  132. (in1 >> -shiftBits), 16);
  133. #endif /* #ifndef ARM_MATH_BIG_ENDIAN */
  134. /* Decrement the loop counter */
  135. blkCnt--;
  136. }
  137. /* If the blockSize is not a multiple of 4, compute any remaining output samples here.
  138. ** No loop unrolling is used. */
  139. blkCnt = blockSize % 0x4U;
  140. while (blkCnt > 0U)
  141. {
  142. /* C = A >> shiftBits */
  143. /* Shift the inputs and then store the results in the destination buffer. */
  144. *pDst++ = (*pSrc++ >> -shiftBits);
  145. /* Decrement the loop counter */
  146. blkCnt--;
  147. }
  148. }
  149. #else
  150. /* Run the below code for Cortex-M0 */
  151. /* Getting the sign of shiftBits */
  152. sign = (shiftBits & 0x80);
  153. /* If the shift value is positive then do right shift else left shift */
  154. if (sign == 0U)
  155. {
  156. /* Initialize blkCnt with number of samples */
  157. blkCnt = blockSize;
  158. while (blkCnt > 0U)
  159. {
  160. /* C = A << shiftBits */
  161. /* Shift and then store the results in the destination buffer. */
  162. *pDst++ = __SSAT(((q31_t) * pSrc++ << shiftBits), 16);
  163. /* Decrement the loop counter */
  164. blkCnt--;
  165. }
  166. }
  167. else
  168. {
  169. /* Initialize blkCnt with number of samples */
  170. blkCnt = blockSize;
  171. while (blkCnt > 0U)
  172. {
  173. /* C = A >> shiftBits */
  174. /* Shift the inputs and then store the results in the destination buffer. */
  175. *pDst++ = (*pSrc++ >> -shiftBits);
  176. /* Decrement the loop counter */
  177. blkCnt--;
  178. }
  179. }
  180. #endif /* #if defined (ARM_MATH_DSP) */
  181. }
  182. /**
  183. * @} end of shift group
  184. */