arm_q15_to_q7.c 4.7 KB

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