arm_f16_to_q15.c 4.2 KB

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