arm_shift_q7.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_shift_q7.c
  4. * Description: Processing function for the Q7 Shifting
  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 Q7 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 onditions for optimum performance
  43. Input and output buffers should be aligned by 32-bit
  44. @par Scaling and Overflow Behavior
  45. The function uses saturating arithmetic.
  46. Results outside of the allowable Q7 range [0x80 0x7F] are saturated.
  47. */
  48. #if defined(ARM_MATH_MVEI) && !defined(ARM_MATH_AUTOVECTORIZE)
  49. #include "arm_helium_utils.h"
  50. void arm_shift_q7(
  51. const q7_t * pSrc,
  52. int8_t shiftBits,
  53. q7_t * pDst,
  54. uint32_t blockSize)
  55. {
  56. uint32_t blkCnt; /* loop counters */
  57. q7x16_t vecSrc;
  58. q7x16_t vecDst;
  59. /* Compute 16 outputs at a time */
  60. blkCnt = blockSize >> 4;
  61. while (blkCnt > 0U)
  62. {
  63. /*
  64. * C = A (>> or <<) shiftBits
  65. * Shift the input and then store the result in the destination buffer.
  66. */
  67. vecSrc = vld1q(pSrc);
  68. vecDst = vqshlq_r(vecSrc, shiftBits);
  69. vst1q(pDst, vecDst);
  70. /*
  71. * Decrement the blockSize loop counter
  72. */
  73. blkCnt--;
  74. /*
  75. * advance vector source and destination pointers
  76. */
  77. pSrc += 16;
  78. pDst += 16;
  79. }
  80. /*
  81. * tail
  82. */
  83. blkCnt = blockSize & 0xF;
  84. if (blkCnt > 0U)
  85. {
  86. mve_pred16_t p0 = vctp8q(blkCnt);
  87. vecSrc = vld1q(pSrc);
  88. vecDst = vqshlq_r(vecSrc, shiftBits);
  89. vstrbq_p(pDst, vecDst, p0);
  90. }
  91. }
  92. #else
  93. void arm_shift_q7(
  94. const q7_t * pSrc,
  95. int8_t shiftBits,
  96. q7_t * pDst,
  97. uint32_t blockSize)
  98. {
  99. uint32_t blkCnt; /* Loop counter */
  100. uint8_t sign = (shiftBits & 0x80); /* Sign of shiftBits */
  101. #if defined (ARM_MATH_LOOPUNROLL)
  102. #if defined (ARM_MATH_DSP)
  103. q7_t in1, in2, in3, in4; /* Temporary input variables */
  104. #endif
  105. /* Loop unrolling: Compute 4 outputs at a time */
  106. blkCnt = blockSize >> 2U;
  107. /* If the shift value is positive then do right shift else left shift */
  108. if (sign == 0U)
  109. {
  110. while (blkCnt > 0U)
  111. {
  112. /* C = A << shiftBits */
  113. #if defined (ARM_MATH_DSP)
  114. /* Read 4 inputs */
  115. in1 = *pSrc++;
  116. in2 = *pSrc++;
  117. in3 = *pSrc++;
  118. in4 = *pSrc++;
  119. /* Pack and store result in destination buffer (in single write) */
  120. write_q7x4_ia (&pDst, __PACKq7(__SSAT(((q15_t) in1 << shiftBits), 8),
  121. __SSAT(((q15_t) in2 << shiftBits), 8),
  122. __SSAT(((q15_t) in3 << shiftBits), 8),
  123. __SSAT(((q15_t) in4 << shiftBits), 8) ));
  124. #else
  125. *pDst++ = (q7_t) __SSAT(((q15_t) *pSrc++ << shiftBits), 8);
  126. *pDst++ = (q7_t) __SSAT(((q15_t) *pSrc++ << shiftBits), 8);
  127. *pDst++ = (q7_t) __SSAT(((q15_t) *pSrc++ << shiftBits), 8);
  128. *pDst++ = (q7_t) __SSAT(((q15_t) *pSrc++ << shiftBits), 8);
  129. #endif
  130. /* Decrement loop counter */
  131. blkCnt--;
  132. }
  133. }
  134. else
  135. {
  136. while (blkCnt > 0U)
  137. {
  138. /* C = A >> shiftBits */
  139. #if defined (ARM_MATH_DSP)
  140. /* Read 4 inputs */
  141. in1 = *pSrc++;
  142. in2 = *pSrc++;
  143. in3 = *pSrc++;
  144. in4 = *pSrc++;
  145. /* Pack and store result in destination buffer (in single write) */
  146. write_q7x4_ia (&pDst, __PACKq7((in1 >> -shiftBits),
  147. (in2 >> -shiftBits),
  148. (in3 >> -shiftBits),
  149. (in4 >> -shiftBits) ));
  150. #else
  151. *pDst++ = (*pSrc++ >> -shiftBits);
  152. *pDst++ = (*pSrc++ >> -shiftBits);
  153. *pDst++ = (*pSrc++ >> -shiftBits);
  154. *pDst++ = (*pSrc++ >> -shiftBits);
  155. #endif
  156. /* Decrement loop counter */
  157. blkCnt--;
  158. }
  159. }
  160. /* Loop unrolling: Compute remaining outputs */
  161. blkCnt = blockSize % 0x4U;
  162. #else
  163. /* Initialize blkCnt with number of samples */
  164. blkCnt = blockSize;
  165. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  166. /* If the shift value is positive then do right shift else left shift */
  167. if (sign == 0U)
  168. {
  169. while (blkCnt > 0U)
  170. {
  171. /* C = A << shiftBits */
  172. /* Shift input and store result in destination buffer. */
  173. *pDst++ = (q7_t) __SSAT(((q15_t) *pSrc++ << shiftBits), 8);
  174. /* Decrement loop counter */
  175. blkCnt--;
  176. }
  177. }
  178. else
  179. {
  180. while (blkCnt > 0U)
  181. {
  182. /* C = A >> shiftBits */
  183. /* Shift input and store result in destination buffer. */
  184. *pDst++ = (*pSrc++ >> -shiftBits);
  185. /* Decrement loop counter */
  186. blkCnt--;
  187. }
  188. }
  189. }
  190. #endif /* defined(ARM_MATH_MVEI) */
  191. /**
  192. @} end of BasicShift group
  193. */