arm_q15_to_q7.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_q15_to_q7.c
  4. * Description: Converts the elements of the Q15 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 q15_to_x
  34. @{
  35. */
  36. /**
  37. @brief Converts the elements of the Q15 vector to Q7 vector.
  38. @param[in] pSrc points to the Q15 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] >> 8; 0 <= n < blockSize.
  46. </pre>
  47. */
  48. #if defined(ARM_MATH_MVEI) && !defined(ARM_MATH_AUTOVECTORIZE)
  49. void arm_q15_to_q7(
  50. const q15_t * pSrc,
  51. q7_t * pDst,
  52. uint32_t blockSize)
  53. {
  54. uint32_t blkCnt; /* loop counters */
  55. q15x8x2_t tmp;
  56. q15_t const *pSrcVec;
  57. q7x16_t vecDst;
  58. pSrcVec = (q15_t const *) pSrc;
  59. blkCnt = blockSize >> 4;
  60. while (blkCnt > 0U)
  61. {
  62. /* C = (q7_t) A >> 8 */
  63. /* convert from q15 to q7 and then store the results in the destination buffer */
  64. tmp = vld2q(pSrcVec);
  65. pSrcVec += 16;
  66. vecDst = vqshrnbq_n_s16(vecDst, tmp.val[0], 8);
  67. vecDst = vqshrntq_n_s16(vecDst, tmp.val[1], 8);
  68. vst1q(pDst, vecDst);
  69. pDst += 16;
  70. /*
  71. * Decrement the blockSize loop counter
  72. */
  73. blkCnt--;
  74. }
  75. blkCnt = blockSize & 0xF;
  76. while (blkCnt > 0U)
  77. {
  78. /* C = (q7_t) A >> 8 */
  79. /* Convert from q15 to q7 and store result in destination buffer */
  80. *pDst++ = (q7_t) (*pSrcVec++ >> 8);
  81. /* Decrement loop counter */
  82. blkCnt--;
  83. }
  84. }
  85. #else
  86. void arm_q15_to_q7(
  87. const q15_t * pSrc,
  88. q7_t * pDst,
  89. uint32_t blockSize)
  90. {
  91. uint32_t blkCnt; /* Loop counter */
  92. const q15_t *pIn = pSrc; /* Source pointer */
  93. #if defined (ARM_MATH_LOOPUNROLL) && defined (ARM_MATH_DSP)
  94. q31_t in1, in2;
  95. q31_t out1, out2;
  96. #endif
  97. #if defined (ARM_MATH_LOOPUNROLL)
  98. /* Loop unrolling: Compute 4 outputs at a time */
  99. blkCnt = blockSize >> 2U;
  100. while (blkCnt > 0U)
  101. {
  102. /* C = (q7_t) A >> 8 */
  103. /* Convert from q15 to q7 and store result in destination buffer */
  104. #if defined (ARM_MATH_DSP)
  105. in1 = read_q15x2_ia (&pIn);
  106. in2 = read_q15x2_ia (&pIn);
  107. #ifndef ARM_MATH_BIG_ENDIAN
  108. out1 = __PKHTB(in2, in1, 16);
  109. out2 = __PKHBT(in2, in1, 16);
  110. #else
  111. out1 = __PKHTB(in1, in2, 16);
  112. out2 = __PKHBT(in1, in2, 16);
  113. #endif /* #ifndef ARM_MATH_BIG_ENDIAN */
  114. /* rotate packed value by 24 */
  115. out2 = ((uint32_t) out2 << 8) | ((uint32_t) out2 >> 24);
  116. /* anding with 0xff00ff00 to get two 8 bit values */
  117. out1 = out1 & 0xFF00FF00;
  118. /* anding with 0x00ff00ff to get two 8 bit values */
  119. out2 = out2 & 0x00FF00FF;
  120. /* oring two values(contains two 8 bit values) to get four packed 8 bit values */
  121. out1 = out1 | out2;
  122. /* store 4 samples at a time to destiantion buffer */
  123. write_q7x4_ia (&pDst, out1);
  124. #else
  125. *pDst++ = (q7_t) (*pIn++ >> 8);
  126. *pDst++ = (q7_t) (*pIn++ >> 8);
  127. *pDst++ = (q7_t) (*pIn++ >> 8);
  128. *pDst++ = (q7_t) (*pIn++ >> 8);
  129. #endif /* #if defined (ARM_MATH_DSP) */
  130. /* Decrement loop counter */
  131. blkCnt--;
  132. }
  133. /* Loop unrolling: Compute remaining outputs */
  134. blkCnt = blockSize % 0x4U;
  135. #else
  136. /* Initialize blkCnt with number of samples */
  137. blkCnt = blockSize;
  138. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  139. while (blkCnt > 0U)
  140. {
  141. /* C = (q7_t) A >> 8 */
  142. /* Convert from q15 to q7 and store result in destination buffer */
  143. *pDst++ = (q7_t) (*pIn++ >> 8);
  144. /* Decrement loop counter */
  145. blkCnt--;
  146. }
  147. }
  148. #endif /* defined(ARM_MATH_MVEI) */
  149. /**
  150. @} end of q15_to_x group
  151. */