arm_f16_to_q15.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_float_to_q15.c
  4. * Description: Converts the elements of the floating-point vector to Q15 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_f16.h"
  29. #if defined(ARM_FLOAT16_SUPPORTED)
  30. /**
  31. @ingroup groupSupport
  32. */
  33. /**
  34. @addtogroup f16_to_x
  35. @{
  36. */
  37. /**
  38. @brief Converts the elements of the f16 vector to Q15 vector.
  39. @param[in] pSrc points to the f16 input vector
  40. @param[out] pDst points to the Q15 output vector
  41. @param[in] blockSize number of samples in each vector
  42. @return none
  43. @par Details
  44. The equation used for the conversion process is:
  45. <pre>
  46. pDst[n] = (q15_t)(pSrc[n] * 32768); 0 <= n < blockSize.
  47. </pre>
  48. @par Scaling and Overflow Behavior
  49. The function uses saturating arithmetic.
  50. Results outside of the allowable Q15 range [0x8000 0x7FFF] are saturated.
  51. @note
  52. In order to apply rounding in scalar version, the library should be rebuilt with the ROUNDING macro
  53. defined in the preprocessor section of project options.
  54. */
  55. #if defined(ARM_MATH_MVE_FLOAT16) && !defined(ARM_MATH_AUTOVECTORIZE)
  56. void arm_f16_to_q15(
  57. const float16_t * pSrc,
  58. q15_t * pDst,
  59. uint32_t blockSize)
  60. {
  61. float16_t maxQ = (float16_t) Q15_MAX;
  62. float16x8_t vecDst;
  63. do {
  64. mve_pred16_t p = vctp16q(blockSize);
  65. vecDst = vldrhq_z_f16((float16_t const *) pSrc, p);
  66. /* C = A * 32767 */
  67. /* convert from float to Q15 and then store the results in the destination buffer */
  68. vecDst = vmulq_m(vuninitializedq_f16(), vecDst, maxQ, p);
  69. vstrhq_p_s16(pDst,
  70. vcvtaq_m(vuninitializedq_s16(), vecDst, p), p);
  71. /*
  72. * Decrement the blockSize loop counter
  73. * Advance vector source and destination pointers
  74. */
  75. pSrc += 8;
  76. pDst += 8;
  77. blockSize -= 8;
  78. }
  79. while ((int32_t) blockSize > 0);
  80. }
  81. #else
  82. void arm_f16_to_q15(
  83. const float16_t * pSrc,
  84. q15_t * pDst,
  85. uint32_t blockSize)
  86. {
  87. const float16_t *pIn = pSrc; /* Src pointer */
  88. uint32_t blkCnt; /* loop counter */
  89. #ifdef ARM_MATH_ROUNDING
  90. float16_t in;
  91. #endif /* #ifdef ARM_MATH_ROUNDING */
  92. /*
  93. * Loop over blockSize number of values
  94. */
  95. blkCnt = blockSize;
  96. while (blkCnt > 0U)
  97. {
  98. #ifdef ARM_MATH_ROUNDING
  99. /*
  100. * C = A * 65536
  101. */
  102. /*
  103. * convert from float to Q31 and then store the results in the destination buffer
  104. */
  105. in = *pIn++;
  106. in = (in * 32768.0);
  107. in += in > 0.0 ? 0.5 : -0.5;
  108. *pDst++ = clip_q31_to_q15((q31_t) (in));
  109. #else
  110. /*
  111. * C = A * 32768
  112. */
  113. /*
  114. * convert from float to Q31 and then store the results in the destination buffer
  115. */
  116. *pDst++ = clip_q31_to_q15((q31_t) ((_Float16)*pIn++ * 32768.0f16));
  117. #endif /* #ifdef ARM_MATH_ROUNDING */
  118. /*
  119. * Decrement the loop counter
  120. */
  121. blkCnt--;
  122. }
  123. }
  124. #endif /* defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE) */
  125. /**
  126. @} end of f16_to_x group
  127. */
  128. #endif /* #if defined(ARM_FLOAT16_SUPPORTED) */