support_functions_f16.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /******************************************************************************
  2. * @file support_functions_f16.h
  3. * @brief Public header file for NMSIS DSP Library
  4. * @version V1.10.1
  5. * @date 18 August 2022
  6. * Target Processor: RISC-V cores
  7. ******************************************************************************/
  8. /*
  9. * Copyright (c) 2010-2020 Arm Limited or its affiliates. All rights reserved.
  10. * Copyright (c) 2019 Nuclei Limited. All rights reserved.
  11. *
  12. * SPDX-License-Identifier: Apache-2.0
  13. *
  14. * Licensed under the Apache License, Version 2.0 (the License); you may
  15. * not use this file except in compliance with the License.
  16. * You may obtain a copy of the License at
  17. *
  18. * www.apache.org/licenses/LICENSE-2.0
  19. *
  20. * Unless required by applicable law or agreed to in writing, software
  21. * distributed under the License is distributed on an AS IS BASIS, WITHOUT
  22. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  23. * See the License for the specific language governing permissions and
  24. * limitations under the License.
  25. */
  26. #ifndef SUPPORT_FUNCTIONS_F16_H_
  27. #define SUPPORT_FUNCTIONS_F16_H_
  28. #include "riscv_math_types_f16.h"
  29. #include "riscv_math_memory.h"
  30. #include "dsp/none.h"
  31. #include "dsp/utils.h"
  32. #ifdef __cplusplus
  33. extern "C"
  34. {
  35. #endif
  36. #if defined(RISCV_FLOAT16_SUPPORTED)
  37. /**
  38. * @brief Copies the elements of a floating-point vector.
  39. * @param[in] pSrc input pointer
  40. * @param[out] pDst output pointer
  41. * @param[in] blockSize number of samples to process
  42. */
  43. void riscv_copy_f16(const float16_t * pSrc, float16_t * pDst, uint32_t blockSize);
  44. /**
  45. * @brief Fills a constant value into a floating-point vector.
  46. * @param[in] value input value to be filled
  47. * @param[out] pDst output pointer
  48. * @param[in] blockSize number of samples to process
  49. */
  50. void riscv_fill_f16(float16_t value, float16_t * pDst, uint32_t blockSize);
  51. /**
  52. * @brief Converts the elements of the floating-point vector to Q31 vector.
  53. * @param[in] pSrc points to the f16 input vector
  54. * @param[out] pDst points to the q15 output vector
  55. * @param[in] blockSize length of the input vector
  56. */
  57. void riscv_f16_to_q15(const float16_t * pSrc, q15_t * pDst, uint32_t blockSize);
  58. /**
  59. * @brief Converts the elements of the floating-point vector to Q31 vector.
  60. * @param[in] pSrc points to the q15 input vector
  61. * @param[out] pDst points to the f16 output vector
  62. * @param[in] blockSize length of the input vector
  63. */
  64. void riscv_q15_to_f16(const q15_t * pSrc, float16_t * pDst, uint32_t blockSize);
  65. /**
  66. * @brief Converts the elements of the 64 bit floating-point vector to 16 bit floating-point vector.
  67. * @param[in] pSrc points to the f64 input vector
  68. * @param[out] pDst points to the f16 output vector
  69. * @param[in] blockSize length of the input vector
  70. */
  71. void riscv_f64_to_f16(const float64_t * pSrc, float16_t * pDst, uint32_t blockSize);
  72. /**
  73. * @brief Converts the elements of the 16 bit floating-point vector to 64 bit floating-point vector.
  74. * @param[in] pSrc points to the f16 input vector
  75. * @param[out] pDst points to the f64 output vector
  76. * @param[in] blockSize length of the input vector
  77. */
  78. void riscv_f16_to_f64(const float16_t * pSrc, float64_t * pDst, uint32_t blockSize);
  79. /**
  80. * @brief Converts the elements of the floating-point vector to Q31 vector.
  81. * @param[in] pSrc points to the f32 input vector
  82. * @param[out] pDst points to the f16 output vector
  83. * @param[in] blockSize length of the input vector
  84. */
  85. void riscv_float_to_f16(const float32_t * pSrc, float16_t * pDst, uint32_t blockSize);
  86. /**
  87. * @brief Converts the elements of the floating-point vector to Q31 vector.
  88. * @param[in] pSrc points to the f16 input vector
  89. * @param[out] pDst points to the f32 output vector
  90. * @param[in] blockSize length of the input vector
  91. */
  92. void riscv_f16_to_float(const float16_t * pSrc, float32_t * pDst, uint32_t blockSize);
  93. /**
  94. * @brief Weighted average
  95. * @param[in] *in Array of input values.
  96. * @param[in] *weigths Weights
  97. * @param[in] blockSize Number of samples in the input array.
  98. * @return Weighted average
  99. */
  100. float16_t riscv_weighted_average_f16(const float16_t *in
  101. , const float16_t *weigths
  102. , uint32_t blockSize);
  103. /**
  104. * @brief Barycenter
  105. * @param[in] in List of vectors
  106. * @param[in] weights Weights of the vectors
  107. * @param[out] out Barycenter
  108. * @param[in] nbVectors Number of vectors
  109. * @param[in] vecDim Dimension of space (vector dimension)
  110. */
  111. void riscv_barycenter_f16(const float16_t *in
  112. , const float16_t *weights
  113. , float16_t *out
  114. , uint32_t nbVectors
  115. , uint32_t vecDim);
  116. /**
  117. @ingroup groupSupport
  118. */
  119. /**
  120. * @defgroup typecast Typecasting
  121. */
  122. /**
  123. @addtogroup typecast
  124. @{
  125. */
  126. /**
  127. * @brief Interpret a f16 as an s16 value
  128. * @param[in] x input value.
  129. * @return return value.
  130. *
  131. * @par Description
  132. * It is a typecast. No conversion of the float to int is done.
  133. * The memcpy will be optimized out by the compiler.
  134. * memcpy is used to prevent type punning issues.
  135. * With gcc, -fno-builtins MUST not be used or the
  136. * memcpy will not be optimized out.
  137. */
  138. __STATIC_INLINE int16_t riscv_typecast_s16_f16(float16_t x)
  139. {
  140. int16_t res;
  141. res=*(int16_t*)memcpy((char*)&res,(char*)&x,sizeof(float16_t));
  142. return(res);
  143. }
  144. /**
  145. * @brief Interpret an s16 as an f16 value
  146. * @param[in] x input value.
  147. * @return return value.
  148. *
  149. * @par Description
  150. * It is a typecast. No conversion of the int to float is done.
  151. * The memcpy will be optimized out by the compiler.
  152. * memcpy is used to prevent type punning issues.
  153. * With gcc, -fno-builtins MUST not be used or the
  154. * memcpy will not be optimized out.
  155. */
  156. __STATIC_INLINE float16_t riscv_typecast_f16_s16(int16_t x)
  157. {
  158. float16_t res;
  159. res=*(float16_t*)memcpy((char*)&res,(char*)&x,sizeof(int16_t));
  160. return(res);
  161. }
  162. /**
  163. @} end of typecast group
  164. */
  165. #endif /*defined(RISCV_FLOAT16_SUPPORTED)*/
  166. #ifdef __cplusplus
  167. }
  168. #endif
  169. #endif /* ifndef _SUPPORT_FUNCTIONS_F16_H_ */