arm_float_to_f16.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 float_to_x
  35. @{
  36. */
  37. /**
  38. @brief Converts the elements of the floating-point vector to f16 vector.
  39. @param[in] pSrc points to the f32 input vector
  40. @param[out] pDst points to the f16 output vector
  41. @param[in] blockSize number of samples in each vector
  42. */
  43. #if defined(ARM_MATH_MVE_FLOAT16) && !defined(ARM_MATH_AUTOVECTORIZE) && defined(__CMSIS_GCC_H)
  44. #pragma GCC warning "Scalar version of arm_float_to_f16 built. Helium version has build issues with gcc."
  45. #endif
  46. #if defined(ARM_MATH_MVE_FLOAT16) && !defined(ARM_MATH_AUTOVECTORIZE) && !defined(__CMSIS_GCC_H)
  47. void arm_float_to_f16(
  48. const float32_t * pSrc,
  49. float16_t * pDst,
  50. uint32_t blockSize)
  51. {
  52. int32_t blkCnt; /* loop counters */
  53. float32x4x2_t tmp;
  54. float16x8_t vecDst;
  55. float32_t const *pSrcVec;
  56. pSrcVec = (float32_t const *) pSrc;
  57. blkCnt = blockSize >> 3;
  58. while (blkCnt > 0)
  59. {
  60. /* convert from float32 to float16 and then store the results in the destination buffer */
  61. tmp = vld2q(pSrcVec); pSrcVec += 8;
  62. /* narrow / merge */
  63. vecDst = vcvtbq_f16_f32(vecDst, tmp.val[0]);
  64. vecDst = vcvttq_f16_f32(vecDst, tmp.val[1]);
  65. vst1q(pDst, vecDst); pDst += 8;
  66. /*
  67. * Decrement the blockSize loop counter
  68. */
  69. blkCnt--;
  70. }
  71. /*
  72. * tail
  73. */
  74. blkCnt = blockSize & 7;
  75. if (blkCnt > 0)
  76. {
  77. mve_pred16_t p0 = vctp16q(blkCnt);
  78. tmp = vld2q(pSrcVec);
  79. vecDst = vcvtbq_f16_f32(vecDst, tmp.val[0]);
  80. vecDst = vcvttq_f16_f32(vecDst, tmp.val[1]);
  81. vstrhq_p(pDst, vecDst, p0);
  82. }
  83. }
  84. #else
  85. void arm_float_to_f16(
  86. const float32_t * pSrc,
  87. float16_t * pDst,
  88. uint32_t blockSize)
  89. {
  90. const float32_t *pIn = pSrc; /* Src pointer */
  91. uint32_t blkCnt; /* loop counter */
  92. /*
  93. * Loop over blockSize number of values
  94. */
  95. blkCnt = blockSize;
  96. while (blkCnt > 0U)
  97. {
  98. *pDst++ = (float16_t) * pIn++;
  99. /*
  100. * Decrement the loop counter
  101. */
  102. blkCnt--;
  103. }
  104. }
  105. #endif /* defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE) */
  106. /**
  107. @} end of float_to_x group
  108. */
  109. #endif /* #if defined(ARM_FLOAT16_SUPPORTED) */