matrix_functions_f16.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /******************************************************************************
  2. * @file matrix_functions_f16.h
  3. * @brief Public header file for NMSIS DSP Library
  4. * @version V1.10.0
  5. * @date 08 July 2021
  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 MATRIX_FUNCTIONS_F16_H_
  27. #define MATRIX_FUNCTIONS_F16_H_
  28. #ifdef __cplusplus
  29. extern "C"
  30. {
  31. #endif
  32. #include "riscv_math_types_f16.h"
  33. #include "riscv_math_memory.h"
  34. #include "dsp/none.h"
  35. #include "dsp/utils.h"
  36. #if defined(RISCV_FLOAT16_SUPPORTED)
  37. #define DEFAULT_HOUSEHOLDER_THRESHOLD_F16 (1.0e-3f)
  38. /**
  39. * @brief Instance structure for the floating-point matrix structure.
  40. */
  41. typedef struct
  42. {
  43. uint16_t numRows; /**< number of rows of the matrix. */
  44. uint16_t numCols; /**< number of columns of the matrix. */
  45. float16_t *pData; /**< points to the data of the matrix. */
  46. } riscv_matrix_instance_f16;
  47. /**
  48. * @brief Floating-point matrix addition.
  49. * @param[in] pSrcA points to the first input matrix structure
  50. * @param[in] pSrcB points to the second input matrix structure
  51. * @param[out] pDst points to output matrix structure
  52. * @return The function returns either
  53. * <code>RISCV_MATH_SIZE_MISMATCH</code> or <code>RISCV_MATH_SUCCESS</code> based on the outcome of size checking.
  54. */
  55. riscv_status riscv_mat_add_f16(
  56. const riscv_matrix_instance_f16 * pSrcA,
  57. const riscv_matrix_instance_f16 * pSrcB,
  58. riscv_matrix_instance_f16 * pDst);
  59. /**
  60. * @brief Floating-point, complex, matrix multiplication.
  61. * @param[in] pSrcA points to the first input matrix structure
  62. * @param[in] pSrcB points to the second input matrix structure
  63. * @param[out] pDst points to output matrix structure
  64. * @return The function returns either
  65. * <code>RISCV_MATH_SIZE_MISMATCH</code> or <code>RISCV_MATH_SUCCESS</code> based on the outcome of size checking.
  66. */
  67. riscv_status riscv_mat_cmplx_mult_f16(
  68. const riscv_matrix_instance_f16 * pSrcA,
  69. const riscv_matrix_instance_f16 * pSrcB,
  70. riscv_matrix_instance_f16 * pDst);
  71. /**
  72. * @brief Floating-point matrix transpose.
  73. * @param[in] pSrc points to the input matrix
  74. * @param[out] pDst points to the output matrix
  75. * @return The function returns either <code>RISCV_MATH_SIZE_MISMATCH</code>
  76. * or <code>RISCV_MATH_SUCCESS</code> based on the outcome of size checking.
  77. */
  78. riscv_status riscv_mat_trans_f16(
  79. const riscv_matrix_instance_f16 * pSrc,
  80. riscv_matrix_instance_f16 * pDst);
  81. /**
  82. * @brief Floating-point complex matrix transpose.
  83. * @param[in] pSrc points to the input matrix
  84. * @param[out] pDst points to the output matrix
  85. * @return The function returns either <code>RISCV_MATH_SIZE_MISMATCH</code>
  86. * or <code>RISCV_MATH_SUCCESS</code> based on the outcome of size checking.
  87. */
  88. riscv_status riscv_mat_cmplx_trans_f16(
  89. const riscv_matrix_instance_f16 * pSrc,
  90. riscv_matrix_instance_f16 * pDst);
  91. /**
  92. * @brief Floating-point matrix multiplication
  93. * @param[in] pSrcA points to the first input matrix structure
  94. * @param[in] pSrcB points to the second input matrix structure
  95. * @param[out] pDst points to output matrix structure
  96. * @return The function returns either
  97. * <code>RISCV_MATH_SIZE_MISMATCH</code> or <code>RISCV_MATH_SUCCESS</code> based on the outcome of size checking.
  98. */
  99. riscv_status riscv_mat_mult_f16(
  100. const riscv_matrix_instance_f16 * pSrcA,
  101. const riscv_matrix_instance_f16 * pSrcB,
  102. riscv_matrix_instance_f16 * pDst);
  103. /**
  104. * @brief Floating-point matrix and vector multiplication
  105. * @param[in] pSrcMat points to the input matrix structure
  106. * @param[in] pVec points to vector
  107. * @param[out] pDst points to output vector
  108. */
  109. void riscv_mat_vec_mult_f16(
  110. const riscv_matrix_instance_f16 *pSrcMat,
  111. const float16_t *pVec,
  112. float16_t *pDst);
  113. /**
  114. * @brief Floating-point matrix subtraction
  115. * @param[in] pSrcA points to the first input matrix structure
  116. * @param[in] pSrcB points to the second input matrix structure
  117. * @param[out] pDst points to output matrix structure
  118. * @return The function returns either
  119. * <code>RISCV_MATH_SIZE_MISMATCH</code> or <code>RISCV_MATH_SUCCESS</code> based on the outcome of size checking.
  120. */
  121. riscv_status riscv_mat_sub_f16(
  122. const riscv_matrix_instance_f16 * pSrcA,
  123. const riscv_matrix_instance_f16 * pSrcB,
  124. riscv_matrix_instance_f16 * pDst);
  125. /**
  126. * @brief Floating-point matrix scaling.
  127. * @param[in] pSrc points to the input matrix
  128. * @param[in] scale scale factor
  129. * @param[out] pDst points to the output matrix
  130. * @return The function returns either
  131. * <code>RISCV_MATH_SIZE_MISMATCH</code> or <code>RISCV_MATH_SUCCESS</code> based on the outcome of size checking.
  132. */
  133. riscv_status riscv_mat_scale_f16(
  134. const riscv_matrix_instance_f16 * pSrc,
  135. float16_t scale,
  136. riscv_matrix_instance_f16 * pDst);
  137. /**
  138. * @brief Floating-point matrix initialization.
  139. * @param[in,out] S points to an instance of the floating-point matrix structure.
  140. * @param[in] nRows number of rows in the matrix.
  141. * @param[in] nColumns number of columns in the matrix.
  142. * @param[in] pData points to the matrix data array.
  143. */
  144. void riscv_mat_init_f16(
  145. riscv_matrix_instance_f16 * S,
  146. uint16_t nRows,
  147. uint16_t nColumns,
  148. float16_t * pData);
  149. /**
  150. * @brief Floating-point matrix inverse.
  151. * @param[in] src points to the instance of the input floating-point matrix structure.
  152. * @param[out] dst points to the instance of the output floating-point matrix structure.
  153. * @return The function returns RISCV_MATH_SIZE_MISMATCH, if the dimensions do not match.
  154. * If the input matrix is singular (does not have an inverse), then the algorithm terminates and returns error status RISCV_MATH_SINGULAR.
  155. */
  156. riscv_status riscv_mat_inverse_f16(
  157. const riscv_matrix_instance_f16 * src,
  158. riscv_matrix_instance_f16 * dst);
  159. /**
  160. * @brief Floating-point Cholesky decomposition of Symmetric Positive Definite Matrix.
  161. * @param[in] src points to the instance of the input floating-point matrix structure.
  162. * @param[out] dst points to the instance of the output floating-point matrix structure.
  163. * @return The function returns RISCV_MATH_SIZE_MISMATCH, if the dimensions do not match.
  164. * If the input matrix does not have a decomposition, then the algorithm terminates and returns error status RISCV_MATH_DECOMPOSITION_FAILURE.
  165. * If the matrix is ill conditioned or only semi-definite, then it is better using the LDL^t decomposition.
  166. * The decomposition is returning a lower triangular matrix.
  167. */
  168. riscv_status riscv_mat_cholesky_f16(
  169. const riscv_matrix_instance_f16 * src,
  170. riscv_matrix_instance_f16 * dst);
  171. /**
  172. * @brief Solve UT . X = A where UT is an upper triangular matrix
  173. * @param[in] ut The upper triangular matrix
  174. * @param[in] a The matrix a
  175. * @param[out] dst The solution X of UT . X = A
  176. * @return The function returns RISCV_MATH_SINGULAR, if the system can't be solved.
  177. */
  178. riscv_status riscv_mat_solve_upper_triangular_f16(
  179. const riscv_matrix_instance_f16 * ut,
  180. const riscv_matrix_instance_f16 * a,
  181. riscv_matrix_instance_f16 * dst);
  182. /**
  183. * @brief Solve LT . X = A where LT is a lower triangular matrix
  184. * @param[in] lt The lower triangular matrix
  185. * @param[in] a The matrix a
  186. * @param[out] dst The solution X of LT . X = A
  187. * @return The function returns RISCV_MATH_SINGULAR, if the system can't be solved.
  188. */
  189. riscv_status riscv_mat_solve_lower_triangular_f16(
  190. const riscv_matrix_instance_f16 * lt,
  191. const riscv_matrix_instance_f16 * a,
  192. riscv_matrix_instance_f16 * dst);
  193. /**
  194. @brief QR decomposition of a m x n floating point matrix with m >= n.
  195. @param[in] pSrc points to input matrix structure. The source matrix is modified by the function.
  196. @param[in] threshold norm2 threshold.
  197. @param[out] pOutR points to output R matrix structure of dimension m x n
  198. @param[out] pOutQ points to output Q matrix structure of dimension m x m
  199. @param[out] pOutTau points to Householder scaling factors of dimension n
  200. @param[inout] pTmpA points to a temporary vector of dimension m.
  201. @param[inout] pTmpB points to a temporary vector of dimension n.
  202. @return execution status
  203. - \ref RISCV_MATH_SUCCESS : Operation successful
  204. - \ref RISCV_MATH_SIZE_MISMATCH : Matrix size check failed
  205. - \ref RISCV_MATH_SINGULAR : Input matrix is found to be singular (non-invertible)
  206. */
  207. riscv_status riscv_mat_qr_f16(
  208. const riscv_matrix_instance_f16 * pSrc,
  209. const float16_t threshold,
  210. riscv_matrix_instance_f16 * pOutR,
  211. riscv_matrix_instance_f16 * pOutQ,
  212. float16_t * pOutTau,
  213. float16_t *pTmpA,
  214. float16_t *pTmpB
  215. );
  216. /**
  217. @brief Householder transform of a half floating point vector.
  218. @param[in] pSrc points to the input vector.
  219. @param[in] threshold norm2 threshold.
  220. @param[in] blockSize dimension of the vector space.
  221. @param[outQ] pOut points to the output vector.
  222. @return beta return the scaling factor beta
  223. */
  224. float16_t riscv_householder_f16(
  225. const float16_t * pSrc,
  226. const float16_t threshold,
  227. uint32_t blockSize,
  228. float16_t * pOut
  229. );
  230. #endif /*defined(RISCV_FLOAT16_SUPPORTED)*/
  231. #ifdef __cplusplus
  232. }
  233. #endif
  234. #endif /* ifndef _MATRIX_FUNCTIONS_F16_H_ */