arm_q31_to_q7.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_q31_to_q7.c
  4. * Description: Converts the elements of the Q31 vector to Q7 vector
  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/support_functions.h"
  29. /**
  30. @ingroup groupSupport
  31. */
  32. /**
  33. @addtogroup q31_to_x
  34. @{
  35. */
  36. /**
  37. @brief Converts the elements of the Q31 vector to Q7 vector.
  38. @param[in] pSrc points to the Q31 input vector
  39. @param[out] pDst points to the Q7 output vector
  40. @param[in] blockSize number of samples in each vector
  41. @return none
  42. @par Details
  43. The equation used for the conversion process is:
  44. <pre>
  45. pDst[n] = (q7_t) pSrc[n] >> 24; 0 <= n < blockSize.
  46. </pre>
  47. */
  48. #if defined(ARM_MATH_MVEI) && !defined(ARM_MATH_AUTOVECTORIZE)
  49. void arm_q31_to_q7(
  50. const q31_t * pSrc,
  51. q7_t * pDst,
  52. uint32_t blockSize)
  53. {
  54. uint32_t blkCnt; /* loop counters */
  55. q31x4x4_t tmp;
  56. q15x8_t evVec, oddVec;
  57. q7x16_t vecDst;
  58. q31_t const *pSrcVec;
  59. pSrcVec = (q31_t const *) pSrc;
  60. blkCnt = blockSize >> 4;
  61. while (blkCnt > 0U)
  62. {
  63. tmp = vld4q(pSrcVec);
  64. pSrcVec += 16;
  65. /* C = (q7_t) A >> 24 */
  66. /* convert from q31 to q7 and then store the results in the destination buffer */
  67. /*
  68. * narrow and pack evens
  69. */
  70. evVec = vshrnbq_n_s32(evVec, tmp.val[0], 16);
  71. evVec = vshrntq_n_s32(evVec, tmp.val[2], 16);
  72. /*
  73. * narrow and pack odds
  74. */
  75. oddVec = vshrnbq_n_s32(oddVec, tmp.val[1], 16);
  76. oddVec = vshrntq_n_s32(oddVec, tmp.val[3], 16);
  77. /*
  78. * narrow & merge
  79. */
  80. vecDst = vshrnbq_n_s16(vecDst, evVec, 8);
  81. vecDst = vshrntq_n_s16(vecDst, oddVec, 8);
  82. vst1q(pDst, vecDst);
  83. pDst += 16;
  84. /*
  85. * Decrement the blockSize loop counter
  86. */
  87. blkCnt--;
  88. }
  89. /*
  90. * tail
  91. */
  92. blkCnt = blockSize & 0xF;
  93. while (blkCnt > 0U)
  94. {
  95. /* C = (q7_t) (A >> 24) */
  96. /* Convert from q31 to q7 and store result in destination buffer */
  97. *pDst++ = (q7_t) (*pSrcVec++ >> 24);
  98. /* Decrement loop counter */
  99. blkCnt--;
  100. }
  101. }
  102. #else
  103. void arm_q31_to_q7(
  104. const q31_t * pSrc,
  105. q7_t * pDst,
  106. uint32_t blockSize)
  107. {
  108. uint32_t blkCnt; /* Loop counter */
  109. const q31_t *pIn = pSrc; /* Source pointer */
  110. #if defined (ARM_MATH_LOOPUNROLL)
  111. q7_t out1, out2, out3, out4;
  112. /* Loop unrolling: Compute 4 outputs at a time */
  113. blkCnt = blockSize >> 2U;
  114. while (blkCnt > 0U)
  115. {
  116. /* C = (q7_t) (A >> 24) */
  117. /* Convert from q31 to q7 and store result in destination buffer */
  118. out1 = (q7_t) (*pIn++ >> 24);
  119. out2 = (q7_t) (*pIn++ >> 24);
  120. out3 = (q7_t) (*pIn++ >> 24);
  121. out4 = (q7_t) (*pIn++ >> 24);
  122. write_q7x4_ia (&pDst, __PACKq7(out1, out2, out3, out4));
  123. /* Decrement loop counter */
  124. blkCnt--;
  125. }
  126. /* Loop unrolling: Compute remaining outputs */
  127. blkCnt = blockSize % 0x4U;
  128. #else
  129. /* Initialize blkCnt with number of samples */
  130. blkCnt = blockSize;
  131. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  132. while (blkCnt > 0U)
  133. {
  134. /* C = (q7_t) (A >> 24) */
  135. /* Convert from q31 to q7 and store result in destination buffer */
  136. *pDst++ = (q7_t) (*pIn++ >> 24);
  137. /* Decrement loop counter */
  138. blkCnt--;
  139. }
  140. }
  141. #endif /* defined(ARM_MATH_MVEI) */
  142. /**
  143. @} end of q31_to_x group
  144. */