arm_shift_q7.c 6.1 KB

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