arm_shift_q15.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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: 23 April 2021
  7. * $Revision: V1.9.0
  8. *
  9. * Target Processor: Cortex-M and Cortex-A cores
  10. * -------------------------------------------------------------------- */
  11. /*
  12. * Copyright (C) 2010-2021 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 "dsp/basic_math_functions.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. @par Scaling and Overflow Behavior
  43. The function uses saturating arithmetic.
  44. Results outside of the allowable Q15 range [0x8000 0x7FFF] are saturated.
  45. */
  46. #if defined(ARM_MATH_MVEI) && !defined(ARM_MATH_AUTOVECTORIZE)
  47. #include "arm_helium_utils.h"
  48. void arm_shift_q15(
  49. const q15_t * pSrc,
  50. int8_t shiftBits,
  51. q15_t * pDst,
  52. uint32_t blockSize)
  53. {
  54. uint32_t blkCnt; /* loop counters */
  55. q15x8_t vecSrc;
  56. q15x8_t vecDst;
  57. /* Compute 8 outputs at a time */
  58. blkCnt = blockSize >> 3;
  59. while (blkCnt > 0U)
  60. {
  61. /*
  62. * C = A (>> or <<) shiftBits
  63. * Shift the input and then store the result in the destination buffer.
  64. */
  65. vecSrc = vld1q(pSrc);
  66. vecDst = vqshlq_r(vecSrc, shiftBits);
  67. vst1q(pDst, vecDst);
  68. /*
  69. * Decrement the blockSize loop counter
  70. */
  71. blkCnt--;
  72. /*
  73. * advance vector source and destination pointers
  74. */
  75. pSrc += 8;
  76. pDst += 8;
  77. }
  78. /*
  79. * tail
  80. */
  81. blkCnt = blockSize & 7;
  82. if (blkCnt > 0U)
  83. {
  84. mve_pred16_t p0 = vctp16q(blkCnt);
  85. vecSrc = vld1q(pSrc);
  86. vecDst = vqshlq_r(vecSrc, shiftBits);
  87. vstrhq_p(pDst, vecDst, p0);
  88. }
  89. }
  90. #else
  91. void arm_shift_q15(
  92. const q15_t * pSrc,
  93. int8_t shiftBits,
  94. q15_t * pDst,
  95. uint32_t blockSize)
  96. {
  97. uint32_t blkCnt; /* Loop counter */
  98. uint8_t sign = (shiftBits & 0x80); /* Sign of shiftBits */
  99. #if defined (ARM_MATH_LOOPUNROLL)
  100. #if defined (ARM_MATH_DSP)
  101. q15_t in1, in2; /* Temporary input variables */
  102. #endif
  103. /* Loop unrolling: Compute 4 outputs at a time */
  104. blkCnt = blockSize >> 2U;
  105. /* If the shift value is positive then do right shift else left shift */
  106. if (sign == 0U)
  107. {
  108. while (blkCnt > 0U)
  109. {
  110. /* C = A << shiftBits */
  111. #if defined (ARM_MATH_DSP)
  112. /* read 2 samples from source */
  113. in1 = *pSrc++;
  114. in2 = *pSrc++;
  115. /* Shift the inputs and then store the results in the destination buffer. */
  116. #ifndef ARM_MATH_BIG_ENDIAN
  117. write_q15x2_ia (&pDst, __PKHBT(__SSAT(((q31_t) in1 << shiftBits), 16),
  118. __SSAT(((q31_t) in2 << shiftBits), 16), 16));
  119. #else
  120. write_q15x2_ia (&pDst, __PKHBT(__SSAT(((q31_t) in2 << shiftBits), 16),
  121. __SSAT(((q31_t) in1 << shiftBits), 16), 16));
  122. #endif /* #ifndef ARM_MATH_BIG_ENDIAN */
  123. /* read 2 samples from source */
  124. in1 = *pSrc++;
  125. in2 = *pSrc++;
  126. #ifndef ARM_MATH_BIG_ENDIAN
  127. write_q15x2_ia (&pDst, __PKHBT(__SSAT(((q31_t) in1 << shiftBits), 16),
  128. __SSAT(((q31_t) in2 << shiftBits), 16), 16));
  129. #else
  130. write_q15x2_ia (&pDst, __PKHBT(__SSAT(((q31_t) in2 << shiftBits), 16),
  131. __SSAT(((q31_t) in1 << shiftBits), 16), 16));
  132. #endif /* #ifndef ARM_MATH_BIG_ENDIAN */
  133. #else
  134. *pDst++ = __SSAT(((q31_t) *pSrc++ << shiftBits), 16);
  135. *pDst++ = __SSAT(((q31_t) *pSrc++ << shiftBits), 16);
  136. *pDst++ = __SSAT(((q31_t) *pSrc++ << shiftBits), 16);
  137. *pDst++ = __SSAT(((q31_t) *pSrc++ << shiftBits), 16);
  138. #endif
  139. /* Decrement loop counter */
  140. blkCnt--;
  141. }
  142. }
  143. else
  144. {
  145. while (blkCnt > 0U)
  146. {
  147. /* C = A >> shiftBits */
  148. #if defined (ARM_MATH_DSP)
  149. /* read 2 samples from source */
  150. in1 = *pSrc++;
  151. in2 = *pSrc++;
  152. /* Shift the inputs and then store the results in the destination buffer. */
  153. #ifndef ARM_MATH_BIG_ENDIAN
  154. write_q15x2_ia (&pDst, __PKHBT((in1 >> -shiftBits),
  155. (in2 >> -shiftBits), 16));
  156. #else
  157. write_q15x2_ia (&pDst, __PKHBT((in2 >> -shiftBits),
  158. (in1 >> -shiftBits), 16));
  159. #endif /* #ifndef ARM_MATH_BIG_ENDIAN */
  160. /* read 2 samples from source */
  161. in1 = *pSrc++;
  162. in2 = *pSrc++;
  163. #ifndef ARM_MATH_BIG_ENDIAN
  164. write_q15x2_ia (&pDst, __PKHBT((in1 >> -shiftBits),
  165. (in2 >> -shiftBits), 16));
  166. #else
  167. write_q15x2_ia (&pDst, __PKHBT((in2 >> -shiftBits),
  168. (in1 >> -shiftBits), 16));
  169. #endif /* #ifndef ARM_MATH_BIG_ENDIAN */
  170. #else
  171. *pDst++ = (*pSrc++ >> -shiftBits);
  172. *pDst++ = (*pSrc++ >> -shiftBits);
  173. *pDst++ = (*pSrc++ >> -shiftBits);
  174. *pDst++ = (*pSrc++ >> -shiftBits);
  175. #endif
  176. /* Decrement loop counter */
  177. blkCnt--;
  178. }
  179. }
  180. /* Loop unrolling: Compute remaining outputs */
  181. blkCnt = blockSize % 0x4U;
  182. #else
  183. /* Initialize blkCnt with number of samples */
  184. blkCnt = blockSize;
  185. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  186. /* If the shift value is positive then do right shift else left shift */
  187. if (sign == 0U)
  188. {
  189. while (blkCnt > 0U)
  190. {
  191. /* C = A << shiftBits */
  192. /* Shift input and store result in destination buffer. */
  193. *pDst++ = __SSAT(((q31_t) *pSrc++ << shiftBits), 16);
  194. /* Decrement loop counter */
  195. blkCnt--;
  196. }
  197. }
  198. else
  199. {
  200. while (blkCnt > 0U)
  201. {
  202. /* C = A >> shiftBits */
  203. /* Shift input and store result in destination buffer. */
  204. *pDst++ = (*pSrc++ >> -shiftBits);
  205. /* Decrement loop counter */
  206. blkCnt--;
  207. }
  208. }
  209. }
  210. #endif /* defined(ARM_MATH_MVEI) */
  211. /**
  212. @} end of BasicShift group
  213. */