arm_float_to_f16.c 3.4 KB

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