arm_shift_q15.c 7.0 KB

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