arm_shift_q7.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. void arm_shift_q7(
  50. const q7_t * pSrc,
  51. int8_t shiftBits,
  52. q7_t * pDst,
  53. uint32_t blockSize)
  54. {
  55. uint32_t blkCnt; /* Loop counter */
  56. uint8_t sign = (shiftBits & 0x80); /* Sign of shiftBits */
  57. #if defined (ARM_MATH_LOOPUNROLL)
  58. #if defined (ARM_MATH_DSP)
  59. q7_t in1, in2, in3, in4; /* Temporary input variables */
  60. #endif
  61. /* Loop unrolling: Compute 4 outputs at a time */
  62. blkCnt = blockSize >> 2U;
  63. /* If the shift value is positive then do right shift else left shift */
  64. if (sign == 0U)
  65. {
  66. while (blkCnt > 0U)
  67. {
  68. /* C = A << shiftBits */
  69. #if defined (ARM_MATH_DSP)
  70. /* Read 4 inputs */
  71. in1 = *pSrc++;
  72. in2 = *pSrc++;
  73. in3 = *pSrc++;
  74. in4 = *pSrc++;
  75. /* Pack and store result in destination buffer (in single write) */
  76. write_q7x4_ia (&pDst, __PACKq7(__SSAT((in1 << shiftBits), 8),
  77. __SSAT((in2 << shiftBits), 8),
  78. __SSAT((in3 << shiftBits), 8),
  79. __SSAT((in4 << shiftBits), 8) ));
  80. #else
  81. *pDst++ = (q7_t) __SSAT(((q15_t) *pSrc++ << shiftBits), 8);
  82. *pDst++ = (q7_t) __SSAT(((q15_t) *pSrc++ << shiftBits), 8);
  83. *pDst++ = (q7_t) __SSAT(((q15_t) *pSrc++ << shiftBits), 8);
  84. *pDst++ = (q7_t) __SSAT(((q15_t) *pSrc++ << shiftBits), 8);
  85. #endif
  86. /* Decrement loop counter */
  87. blkCnt--;
  88. }
  89. }
  90. else
  91. {
  92. while (blkCnt > 0U)
  93. {
  94. /* C = A >> shiftBits */
  95. #if defined (ARM_MATH_DSP)
  96. /* Read 4 inputs */
  97. in1 = *pSrc++;
  98. in2 = *pSrc++;
  99. in3 = *pSrc++;
  100. in4 = *pSrc++;
  101. /* Pack and store result in destination buffer (in single write) */
  102. write_q7x4_ia (&pDst, __PACKq7((in1 >> -shiftBits),
  103. (in2 >> -shiftBits),
  104. (in3 >> -shiftBits),
  105. (in4 >> -shiftBits) ));
  106. #else
  107. *pDst++ = (*pSrc++ >> -shiftBits);
  108. *pDst++ = (*pSrc++ >> -shiftBits);
  109. *pDst++ = (*pSrc++ >> -shiftBits);
  110. *pDst++ = (*pSrc++ >> -shiftBits);
  111. #endif
  112. /* Decrement loop counter */
  113. blkCnt--;
  114. }
  115. }
  116. /* Loop unrolling: Compute remaining outputs */
  117. blkCnt = blockSize % 0x4U;
  118. #else
  119. /* Initialize blkCnt with number of samples */
  120. blkCnt = blockSize;
  121. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  122. /* If the shift value is positive then do right shift else left shift */
  123. if (sign == 0U)
  124. {
  125. while (blkCnt > 0U)
  126. {
  127. /* C = A << shiftBits */
  128. /* Shift input and store result in destination buffer. */
  129. *pDst++ = (q7_t) __SSAT(((q15_t) *pSrc++ << shiftBits), 8);
  130. /* Decrement loop counter */
  131. blkCnt--;
  132. }
  133. }
  134. else
  135. {
  136. while (blkCnt > 0U)
  137. {
  138. /* C = A >> shiftBits */
  139. /* Shift input and store result in destination buffer. */
  140. *pDst++ = (*pSrc++ >> -shiftBits);
  141. /* Decrement loop counter */
  142. blkCnt--;
  143. }
  144. }
  145. }
  146. /**
  147. @} end of BasicShift group
  148. */