matrix_functions.h 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868
  1. /******************************************************************************
  2. * @file matrix_functions.h
  3. * @brief Public header file for NMSIS DSP Library
  4. * @version V1.10.1
  5. * @date 10 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 MATRIX_FUNCTIONS_H_
  27. #define MATRIX_FUNCTIONS_H_
  28. #include "riscv_math_types.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. /**
  37. * @defgroup groupMatrix Matrix Functions
  38. *
  39. * This set of functions provides basic matrix math operations.
  40. * The functions operate on matrix data structures. For example,
  41. * the type
  42. * definition for the floating-point matrix structure is shown
  43. * below:
  44. * <pre>
  45. * typedef struct
  46. * {
  47. * uint16_t numRows; // number of rows of the matrix.
  48. * uint16_t numCols; // number of columns of the matrix.
  49. * float32_t *pData; // points to the data of the matrix.
  50. * } riscv_matrix_instance_f32;
  51. * </pre>
  52. * There are similar definitions for Q15 and Q31 data types.
  53. *
  54. * The structure specifies the size of the matrix and then points to
  55. * an array of data. The array is of size <code>numRows X numCols</code>
  56. * and the values are arranged in row order. That is, the
  57. * matrix element (i, j) is stored at:
  58. * <pre>
  59. * pData[i*numCols + j]
  60. * </pre>
  61. *
  62. * \par Init Functions
  63. * There is an associated initialization function for each type of matrix
  64. * data structure.
  65. * The initialization function sets the values of the internal structure fields.
  66. * Refer to \ref riscv_mat_init_f32(), \ref riscv_mat_init_q31() and \ref riscv_mat_init_q15()
  67. * for floating-point, Q31 and Q15 types, respectively.
  68. *
  69. * \par
  70. * Use of the initialization function is optional. However, if initialization function is used
  71. * then the instance structure cannot be placed into a const data section.
  72. * To place the instance structure in a const data
  73. * section, manually initialize the data structure. For example:
  74. * <pre>
  75. * <code>riscv_matrix_instance_f32 S = {nRows, nColumns, pData};</code>
  76. * <code>riscv_matrix_instance_q31 S = {nRows, nColumns, pData};</code>
  77. * <code>riscv_matrix_instance_q15 S = {nRows, nColumns, pData};</code>
  78. * </pre>
  79. * where <code>nRows</code> specifies the number of rows, <code>nColumns</code>
  80. * specifies the number of columns, and <code>pData</code> points to the
  81. * data array.
  82. *
  83. * \par Size Checking
  84. * By default all of the matrix functions perform size checking on the input and
  85. * output matrices. For example, the matrix addition function verifies that the
  86. * two input matrices and the output matrix all have the same number of rows and
  87. * columns. If the size check fails the functions return:
  88. * <pre>
  89. * RISCV_MATH_SIZE_MISMATCH
  90. * </pre>
  91. * Otherwise the functions return
  92. * <pre>
  93. * RISCV_MATH_SUCCESS
  94. * </pre>
  95. * There is some overhead associated with this matrix size checking.
  96. * The matrix size checking is enabled via the \#define
  97. * <pre>
  98. * RISCV_MATH_MATRIX_CHECK
  99. * </pre>
  100. * within the library project settings. By default this macro is defined
  101. * and size checking is enabled. By changing the project settings and
  102. * undefining this macro size checking is eliminated and the functions
  103. * run a bit faster. With size checking disabled the functions always
  104. * return <code>RISCV_MATH_SUCCESS</code>.
  105. */
  106. #define DEFAULT_HOUSEHOLDER_THRESHOLD_F64 (1.0e-16)
  107. #define DEFAULT_HOUSEHOLDER_THRESHOLD_F32 (1.0e-12f)
  108. /**
  109. * @brief Instance structure for the floating-point matrix structure.
  110. */
  111. typedef struct
  112. {
  113. uint16_t numRows; /**< number of rows of the matrix. */
  114. uint16_t numCols; /**< number of columns of the matrix. */
  115. float32_t *pData; /**< points to the data of the matrix. */
  116. } riscv_matrix_instance_f32;
  117. /**
  118. * @brief Instance structure for the floating-point matrix structure.
  119. */
  120. typedef struct
  121. {
  122. uint16_t numRows; /**< number of rows of the matrix. */
  123. uint16_t numCols; /**< number of columns of the matrix. */
  124. float64_t *pData; /**< points to the data of the matrix. */
  125. } riscv_matrix_instance_f64;
  126. /**
  127. * @brief Instance structure for the Q7 matrix structure.
  128. */
  129. typedef struct
  130. {
  131. uint16_t numRows; /**< number of rows of the matrix. */
  132. uint16_t numCols; /**< number of columns of the matrix. */
  133. q7_t *pData; /**< points to the data of the matrix. */
  134. } riscv_matrix_instance_q7;
  135. /**
  136. * @brief Instance structure for the Q15 matrix structure.
  137. */
  138. typedef struct
  139. {
  140. uint16_t numRows; /**< number of rows of the matrix. */
  141. uint16_t numCols; /**< number of columns of the matrix. */
  142. q15_t *pData; /**< points to the data of the matrix. */
  143. } riscv_matrix_instance_q15;
  144. /**
  145. * @brief Instance structure for the Q31 matrix structure.
  146. */
  147. typedef struct
  148. {
  149. uint16_t numRows; /**< number of rows of the matrix. */
  150. uint16_t numCols; /**< number of columns of the matrix. */
  151. q31_t *pData; /**< points to the data of the matrix. */
  152. } riscv_matrix_instance_q31;
  153. /**
  154. * @brief Floating-point matrix addition.
  155. * @param[in] pSrcA points to the first input matrix structure
  156. * @param[in] pSrcB points to the second input matrix structure
  157. * @param[out] pDst points to output matrix structure
  158. * @return The function returns either
  159. * <code>RISCV_MATH_SIZE_MISMATCH</code> or <code>RISCV_MATH_SUCCESS</code> based on the outcome of size checking.
  160. */
  161. riscv_status riscv_mat_add_f32(
  162. const riscv_matrix_instance_f32 * pSrcA,
  163. const riscv_matrix_instance_f32 * pSrcB,
  164. riscv_matrix_instance_f32 * pDst);
  165. /**
  166. * @brief Q15 matrix addition.
  167. * @param[in] pSrcA points to the first input matrix structure
  168. * @param[in] pSrcB points to the second input matrix structure
  169. * @param[out] pDst points to output matrix structure
  170. * @return The function returns either
  171. * <code>RISCV_MATH_SIZE_MISMATCH</code> or <code>RISCV_MATH_SUCCESS</code> based on the outcome of size checking.
  172. */
  173. riscv_status riscv_mat_add_q15(
  174. const riscv_matrix_instance_q15 * pSrcA,
  175. const riscv_matrix_instance_q15 * pSrcB,
  176. riscv_matrix_instance_q15 * pDst);
  177. /**
  178. * @brief Q31 matrix addition.
  179. * @param[in] pSrcA points to the first input matrix structure
  180. * @param[in] pSrcB points to the second input matrix structure
  181. * @param[out] pDst points to output matrix structure
  182. * @return The function returns either
  183. * <code>RISCV_MATH_SIZE_MISMATCH</code> or <code>RISCV_MATH_SUCCESS</code> based on the outcome of size checking.
  184. */
  185. riscv_status riscv_mat_add_q31(
  186. const riscv_matrix_instance_q31 * pSrcA,
  187. const riscv_matrix_instance_q31 * pSrcB,
  188. riscv_matrix_instance_q31 * pDst);
  189. /**
  190. * @brief Floating-point, complex, matrix multiplication.
  191. * @param[in] pSrcA points to the first input matrix structure
  192. * @param[in] pSrcB points to the second input matrix structure
  193. * @param[out] pDst points to output matrix structure
  194. * @return The function returns either
  195. * <code>RISCV_MATH_SIZE_MISMATCH</code> or <code>RISCV_MATH_SUCCESS</code> based on the outcome of size checking.
  196. */
  197. riscv_status riscv_mat_cmplx_mult_f32(
  198. const riscv_matrix_instance_f32 * pSrcA,
  199. const riscv_matrix_instance_f32 * pSrcB,
  200. riscv_matrix_instance_f32 * pDst);
  201. /**
  202. * @brief Q15, complex, matrix multiplication.
  203. * @param[in] pSrcA points to the first input matrix structure
  204. * @param[in] pSrcB points to the second input matrix structure
  205. * @param[out] pDst points to output matrix structure
  206. * @return The function returns either
  207. * <code>RISCV_MATH_SIZE_MISMATCH</code> or <code>RISCV_MATH_SUCCESS</code> based on the outcome of size checking.
  208. */
  209. riscv_status riscv_mat_cmplx_mult_q15(
  210. const riscv_matrix_instance_q15 * pSrcA,
  211. const riscv_matrix_instance_q15 * pSrcB,
  212. riscv_matrix_instance_q15 * pDst,
  213. q15_t * pScratch);
  214. /**
  215. * @brief Q31, complex, matrix multiplication.
  216. * @param[in] pSrcA points to the first input matrix structure
  217. * @param[in] pSrcB points to the second input matrix structure
  218. * @param[out] pDst points to output matrix structure
  219. * @return The function returns either
  220. * <code>RISCV_MATH_SIZE_MISMATCH</code> or <code>RISCV_MATH_SUCCESS</code> based on the outcome of size checking.
  221. */
  222. riscv_status riscv_mat_cmplx_mult_q31(
  223. const riscv_matrix_instance_q31 * pSrcA,
  224. const riscv_matrix_instance_q31 * pSrcB,
  225. riscv_matrix_instance_q31 * pDst);
  226. /**
  227. * @brief Floating-point matrix transpose.
  228. * @param[in] pSrc points to the input matrix
  229. * @param[out] pDst points to the output matrix
  230. * @return The function returns either <code>RISCV_MATH_SIZE_MISMATCH</code>
  231. * or <code>RISCV_MATH_SUCCESS</code> based on the outcome of size checking.
  232. */
  233. riscv_status riscv_mat_trans_f32(
  234. const riscv_matrix_instance_f32 * pSrc,
  235. riscv_matrix_instance_f32 * pDst);
  236. /**
  237. * @brief Floating-point matrix transpose.
  238. * @param[in] pSrc points to the input matrix
  239. * @param[out] pDst points to the output matrix
  240. * @return The function returns either <code>RISCV_MATH_SIZE_MISMATCH</code>
  241. * or <code>RISCV_MATH_SUCCESS</code> based on the outcome of size checking.
  242. */
  243. riscv_status riscv_mat_trans_f64(
  244. const riscv_matrix_instance_f64 * pSrc,
  245. riscv_matrix_instance_f64 * pDst);
  246. /**
  247. * @brief Floating-point complex matrix transpose.
  248. * @param[in] pSrc points to the input matrix
  249. * @param[out] pDst points to the output matrix
  250. * @return The function returns either <code>RISCV_MATH_SIZE_MISMATCH</code>
  251. * or <code>RISCV_MATH_SUCCESS</code> based on the outcome of size checking.
  252. */
  253. riscv_status riscv_mat_cmplx_trans_f32(
  254. const riscv_matrix_instance_f32 * pSrc,
  255. riscv_matrix_instance_f32 * pDst);
  256. /**
  257. * @brief Q15 matrix transpose.
  258. * @param[in] pSrc points to the input matrix
  259. * @param[out] pDst points to the output matrix
  260. * @return The function returns either <code>RISCV_MATH_SIZE_MISMATCH</code>
  261. * or <code>RISCV_MATH_SUCCESS</code> based on the outcome of size checking.
  262. */
  263. riscv_status riscv_mat_trans_q15(
  264. const riscv_matrix_instance_q15 * pSrc,
  265. riscv_matrix_instance_q15 * pDst);
  266. /**
  267. * @brief Q15 complex matrix transpose.
  268. * @param[in] pSrc points to the input matrix
  269. * @param[out] pDst points to the output matrix
  270. * @return The function returns either <code>RISCV_MATH_SIZE_MISMATCH</code>
  271. * or <code>RISCV_MATH_SUCCESS</code> based on the outcome of size checking.
  272. */
  273. riscv_status riscv_mat_cmplx_trans_q15(
  274. const riscv_matrix_instance_q15 * pSrc,
  275. riscv_matrix_instance_q15 * pDst);
  276. /**
  277. * @brief Q7 matrix transpose.
  278. * @param[in] pSrc points to the input matrix
  279. * @param[out] pDst points to the output matrix
  280. * @return The function returns either <code>RISCV_MATH_SIZE_MISMATCH</code>
  281. * or <code>RISCV_MATH_SUCCESS</code> based on the outcome of size checking.
  282. */
  283. riscv_status riscv_mat_trans_q7(
  284. const riscv_matrix_instance_q7 * pSrc,
  285. riscv_matrix_instance_q7 * pDst);
  286. /**
  287. * @brief Q31 matrix transpose.
  288. * @param[in] pSrc points to the input matrix
  289. * @param[out] pDst points to the output matrix
  290. * @return The function returns either <code>RISCV_MATH_SIZE_MISMATCH</code>
  291. * or <code>RISCV_MATH_SUCCESS</code> based on the outcome of size checking.
  292. */
  293. riscv_status riscv_mat_trans_q31(
  294. const riscv_matrix_instance_q31 * pSrc,
  295. riscv_matrix_instance_q31 * pDst);
  296. /**
  297. * @brief Q31 complex matrix transpose.
  298. * @param[in] pSrc points to the input matrix
  299. * @param[out] pDst points to the output matrix
  300. * @return The function returns either <code>RISCV_MATH_SIZE_MISMATCH</code>
  301. * or <code>RISCV_MATH_SUCCESS</code> based on the outcome of size checking.
  302. */
  303. riscv_status riscv_mat_cmplx_trans_q31(
  304. const riscv_matrix_instance_q31 * pSrc,
  305. riscv_matrix_instance_q31 * pDst);
  306. /**
  307. * @brief Floating-point matrix multiplication
  308. * @param[in] pSrcA points to the first input matrix structure
  309. * @param[in] pSrcB points to the second input matrix structure
  310. * @param[out] pDst points to output matrix structure
  311. * @return The function returns either
  312. * <code>RISCV_MATH_SIZE_MISMATCH</code> or <code>RISCV_MATH_SUCCESS</code> based on the outcome of size checking.
  313. */
  314. riscv_status riscv_mat_mult_f32(
  315. const riscv_matrix_instance_f32 * pSrcA,
  316. const riscv_matrix_instance_f32 * pSrcB,
  317. riscv_matrix_instance_f32 * pDst);
  318. /**
  319. * @brief Floating-point matrix multiplication
  320. * @param[in] pSrcA points to the first input matrix structure
  321. * @param[in] pSrcB points to the second input matrix structure
  322. * @param[out] pDst points to output matrix structure
  323. * @return The function returns either
  324. * <code>RISCV_MATH_SIZE_MISMATCH</code> or <code>RISCV_MATH_SUCCESS</code> based on the outcome of size checking.
  325. */
  326. riscv_status riscv_mat_mult_f64(
  327. const riscv_matrix_instance_f64 * pSrcA,
  328. const riscv_matrix_instance_f64 * pSrcB,
  329. riscv_matrix_instance_f64 * pDst);
  330. /**
  331. * @brief Floating-point matrix and vector multiplication
  332. * @param[in] pSrcMat points to the input matrix structure
  333. * @param[in] pVec points to vector
  334. * @param[out] pDst points to output vector
  335. */
  336. void riscv_mat_vec_mult_f32(
  337. const riscv_matrix_instance_f32 *pSrcMat,
  338. const float32_t *pVec,
  339. float32_t *pDst);
  340. /**
  341. * @brief Q7 matrix multiplication
  342. * @param[in] pSrcA points to the first input matrix structure
  343. * @param[in] pSrcB points to the second input matrix structure
  344. * @param[out] pDst points to output matrix structure
  345. * @param[in] pState points to the array for storing intermediate results
  346. * @return The function returns either
  347. * <code>RISCV_MATH_SIZE_MISMATCH</code> or <code>RISCV_MATH_SUCCESS</code> based on the outcome of size checking.
  348. */
  349. riscv_status riscv_mat_mult_q7(
  350. const riscv_matrix_instance_q7 * pSrcA,
  351. const riscv_matrix_instance_q7 * pSrcB,
  352. riscv_matrix_instance_q7 * pDst,
  353. q7_t * pState);
  354. /**
  355. * @brief Q7 matrix and vector multiplication
  356. * @param[in] pSrcMat points to the input matrix structure
  357. * @param[in] pVec points to vector
  358. * @param[out] pDst points to output vector
  359. */
  360. void riscv_mat_vec_mult_q7(
  361. const riscv_matrix_instance_q7 *pSrcMat,
  362. const q7_t *pVec,
  363. q7_t *pDst);
  364. /**
  365. * @brief Q15 matrix multiplication
  366. * @param[in] pSrcA points to the first input matrix structure
  367. * @param[in] pSrcB points to the second input matrix structure
  368. * @param[out] pDst points to output matrix structure
  369. * @param[in] pState points to the array for storing intermediate results
  370. * @return The function returns either
  371. * <code>RISCV_MATH_SIZE_MISMATCH</code> or <code>RISCV_MATH_SUCCESS</code> based on the outcome of size checking.
  372. */
  373. riscv_status riscv_mat_mult_q15(
  374. const riscv_matrix_instance_q15 * pSrcA,
  375. const riscv_matrix_instance_q15 * pSrcB,
  376. riscv_matrix_instance_q15 * pDst,
  377. q15_t * pState);
  378. /**
  379. * @brief Q15 matrix and vector multiplication
  380. * @param[in] pSrcMat points to the input matrix structure
  381. * @param[in] pVec points to vector
  382. * @param[out] pDst points to output vector
  383. */
  384. void riscv_mat_vec_mult_q15(
  385. const riscv_matrix_instance_q15 *pSrcMat,
  386. const q15_t *pVec,
  387. q15_t *pDst);
  388. /**
  389. * @brief Q15 matrix multiplication (fast variant) for RISC-V Core with DSP enabled
  390. * @param[in] pSrcA points to the first input matrix structure
  391. * @param[in] pSrcB points to the second input matrix structure
  392. * @param[out] pDst points to output matrix structure
  393. * @param[in] pState points to the array for storing intermediate results
  394. * @return The function returns either
  395. * <code>RISCV_MATH_SIZE_MISMATCH</code> or <code>RISCV_MATH_SUCCESS</code> based on the outcome of size checking.
  396. */
  397. riscv_status riscv_mat_mult_fast_q15(
  398. const riscv_matrix_instance_q15 * pSrcA,
  399. const riscv_matrix_instance_q15 * pSrcB,
  400. riscv_matrix_instance_q15 * pDst,
  401. q15_t * pState);
  402. /**
  403. * @brief Q31 matrix multiplication
  404. * @param[in] pSrcA points to the first input matrix structure
  405. * @param[in] pSrcB points to the second input matrix structure
  406. * @param[out] pDst points to output matrix structure
  407. * @return The function returns either
  408. * <code>RISCV_MATH_SIZE_MISMATCH</code> or <code>RISCV_MATH_SUCCESS</code> based on the outcome of size checking.
  409. */
  410. riscv_status riscv_mat_mult_q31(
  411. const riscv_matrix_instance_q31 * pSrcA,
  412. const riscv_matrix_instance_q31 * pSrcB,
  413. riscv_matrix_instance_q31 * pDst);
  414. /**
  415. * @brief Q31 matrix multiplication
  416. * @param[in] pSrcA points to the first input matrix structure
  417. * @param[in] pSrcB points to the second input matrix structure
  418. * @param[out] pDst points to output matrix structure
  419. * @param[in] pState points to the array for storing intermediate results
  420. * @return The function returns either
  421. * <code>RISCV_MATH_SIZE_MISMATCH</code> or <code>RISCV_MATH_SUCCESS</code> based on the outcome of size checking.
  422. */
  423. riscv_status riscv_mat_mult_opt_q31(
  424. const riscv_matrix_instance_q31 * pSrcA,
  425. const riscv_matrix_instance_q31 * pSrcB,
  426. riscv_matrix_instance_q31 * pDst,
  427. q31_t *pState);
  428. /**
  429. * @brief Q31 matrix and vector multiplication
  430. * @param[in] pSrcMat points to the input matrix structure
  431. * @param[in] pVec points to vector
  432. * @param[out] pDst points to output vector
  433. */
  434. void riscv_mat_vec_mult_q31(
  435. const riscv_matrix_instance_q31 *pSrcMat,
  436. const q31_t *pVec,
  437. q31_t *pDst);
  438. /**
  439. * @brief Q31 matrix multiplication (fast variant) for RISC-V Core with DSP enabled
  440. * @param[in] pSrcA points to the first input matrix structure
  441. * @param[in] pSrcB points to the second input matrix structure
  442. * @param[out] pDst points to output matrix structure
  443. * @return The function returns either
  444. * <code>RISCV_MATH_SIZE_MISMATCH</code> or <code>RISCV_MATH_SUCCESS</code> based on the outcome of size checking.
  445. */
  446. riscv_status riscv_mat_mult_fast_q31(
  447. const riscv_matrix_instance_q31 * pSrcA,
  448. const riscv_matrix_instance_q31 * pSrcB,
  449. riscv_matrix_instance_q31 * pDst);
  450. /**
  451. * @brief Floating-point matrix subtraction
  452. * @param[in] pSrcA points to the first input matrix structure
  453. * @param[in] pSrcB points to the second input matrix structure
  454. * @param[out] pDst points to output matrix structure
  455. * @return The function returns either
  456. * <code>RISCV_MATH_SIZE_MISMATCH</code> or <code>RISCV_MATH_SUCCESS</code> based on the outcome of size checking.
  457. */
  458. riscv_status riscv_mat_sub_f32(
  459. const riscv_matrix_instance_f32 * pSrcA,
  460. const riscv_matrix_instance_f32 * pSrcB,
  461. riscv_matrix_instance_f32 * pDst);
  462. /**
  463. * @brief Floating-point matrix subtraction
  464. * @param[in] pSrcA points to the first input matrix structure
  465. * @param[in] pSrcB points to the second input matrix structure
  466. * @param[out] pDst points to output matrix structure
  467. * @return The function returns either
  468. * <code>RISCV_MATH_SIZE_MISMATCH</code> or <code>RISCV_MATH_SUCCESS</code> based on the outcome of size checking.
  469. */
  470. riscv_status riscv_mat_sub_f64(
  471. const riscv_matrix_instance_f64 * pSrcA,
  472. const riscv_matrix_instance_f64 * pSrcB,
  473. riscv_matrix_instance_f64 * pDst);
  474. /**
  475. * @brief Q15 matrix subtraction
  476. * @param[in] pSrcA points to the first input matrix structure
  477. * @param[in] pSrcB points to the second input matrix structure
  478. * @param[out] pDst points to output matrix structure
  479. * @return The function returns either
  480. * <code>RISCV_MATH_SIZE_MISMATCH</code> or <code>RISCV_MATH_SUCCESS</code> based on the outcome of size checking.
  481. */
  482. riscv_status riscv_mat_sub_q15(
  483. const riscv_matrix_instance_q15 * pSrcA,
  484. const riscv_matrix_instance_q15 * pSrcB,
  485. riscv_matrix_instance_q15 * pDst);
  486. /**
  487. * @brief Q31 matrix subtraction
  488. * @param[in] pSrcA points to the first input matrix structure
  489. * @param[in] pSrcB points to the second input matrix structure
  490. * @param[out] pDst points to output matrix structure
  491. * @return The function returns either
  492. * <code>RISCV_MATH_SIZE_MISMATCH</code> or <code>RISCV_MATH_SUCCESS</code> based on the outcome of size checking.
  493. */
  494. riscv_status riscv_mat_sub_q31(
  495. const riscv_matrix_instance_q31 * pSrcA,
  496. const riscv_matrix_instance_q31 * pSrcB,
  497. riscv_matrix_instance_q31 * pDst);
  498. /**
  499. * @brief Floating-point matrix scaling.
  500. * @param[in] pSrc points to the input matrix
  501. * @param[in] scale scale factor
  502. * @param[out] pDst points to the output matrix
  503. * @return The function returns either
  504. * <code>RISCV_MATH_SIZE_MISMATCH</code> or <code>RISCV_MATH_SUCCESS</code> based on the outcome of size checking.
  505. */
  506. riscv_status riscv_mat_scale_f32(
  507. const riscv_matrix_instance_f32 * pSrc,
  508. float32_t scale,
  509. riscv_matrix_instance_f32 * pDst);
  510. /**
  511. * @brief Q15 matrix scaling.
  512. * @param[in] pSrc points to input matrix
  513. * @param[in] scaleFract fractional portion of the scale factor
  514. * @param[in] shift number of bits to shift the result by
  515. * @param[out] pDst points to output matrix
  516. * @return The function returns either
  517. * <code>RISCV_MATH_SIZE_MISMATCH</code> or <code>RISCV_MATH_SUCCESS</code> based on the outcome of size checking.
  518. */
  519. riscv_status riscv_mat_scale_q15(
  520. const riscv_matrix_instance_q15 * pSrc,
  521. q15_t scaleFract,
  522. int32_t shift,
  523. riscv_matrix_instance_q15 * pDst);
  524. /**
  525. * @brief Q31 matrix scaling.
  526. * @param[in] pSrc points to input matrix
  527. * @param[in] scaleFract fractional portion of the scale factor
  528. * @param[in] shift number of bits to shift the result by
  529. * @param[out] pDst points to output matrix structure
  530. * @return The function returns either
  531. * <code>RISCV_MATH_SIZE_MISMATCH</code> or <code>RISCV_MATH_SUCCESS</code> based on the outcome of size checking.
  532. */
  533. riscv_status riscv_mat_scale_q31(
  534. const riscv_matrix_instance_q31 * pSrc,
  535. q31_t scaleFract,
  536. int32_t shift,
  537. riscv_matrix_instance_q31 * pDst);
  538. /**
  539. * @brief Q31 matrix initialization.
  540. * @param[in,out] S points to an instance of the Q31-type matrix structure.
  541. * @param[in] nRows number of rows in the matrix.
  542. * @param[in] nColumns number of columns in the matrix.
  543. * @param[in] pData points to the matrix data array.
  544. */
  545. void riscv_mat_init_q31(
  546. riscv_matrix_instance_q31 * S,
  547. uint16_t nRows,
  548. uint16_t nColumns,
  549. q31_t * pData);
  550. /**
  551. * @brief Q15 matrix initialization.
  552. * @param[in,out] S points to an instance of the Q15-type matrix structure.
  553. * @param[in] nRows number of rows in the matrix.
  554. * @param[in] nColumns number of columns in the matrix.
  555. * @param[in] pData points to the matrix data array.
  556. */
  557. void riscv_mat_init_q15(
  558. riscv_matrix_instance_q15 * S,
  559. uint16_t nRows,
  560. uint16_t nColumns,
  561. q15_t * pData);
  562. /**
  563. * @brief Q7 matrix initialization.
  564. * @param[in,out] S points to an instance of the Q7-type matrix structure.
  565. * @param[in] nRows number of rows in the matrix.
  566. * @param[in] nColumns number of columns in the matrix.
  567. * @param[in] pData points to the matrix data array.
  568. */
  569. void riscv_mat_init_q7(
  570. riscv_matrix_instance_q7 * S,
  571. uint16_t nRows,
  572. uint16_t nColumns,
  573. q7_t * pData);
  574. /**
  575. * @brief Floating-point matrix initialization.
  576. * @param[in,out] S points to an instance of the floating-point matrix structure.
  577. * @param[in] nRows number of rows in the matrix.
  578. * @param[in] nColumns number of columns in the matrix.
  579. * @param[in] pData points to the matrix data array.
  580. */
  581. void riscv_mat_init_f32(
  582. riscv_matrix_instance_f32 * S,
  583. uint16_t nRows,
  584. uint16_t nColumns,
  585. float32_t * pData);
  586. /**
  587. * @brief Floating-point matrix initialization.
  588. * @param[in,out] S points to an instance of the floating-point matrix structure.
  589. * @param[in] nRows number of rows in the matrix.
  590. * @param[in] nColumns number of columns in the matrix.
  591. * @param[in] pData points to the matrix data array.
  592. */
  593. void riscv_mat_init_f64(
  594. riscv_matrix_instance_f64 * S,
  595. uint16_t nRows,
  596. uint16_t nColumns,
  597. float64_t * pData);
  598. /**
  599. * @brief Floating-point matrix inverse.
  600. * @param[in] src points to the instance of the input floating-point matrix structure.
  601. * @param[out] dst points to the instance of the output floating-point matrix structure.
  602. * @return The function returns RISCV_MATH_SIZE_MISMATCH, if the dimensions do not match.
  603. * If the input matrix is singular (does not have an inverse), then the algorithm terminates and returns error status RISCV_MATH_SINGULAR.
  604. */
  605. riscv_status riscv_mat_inverse_f32(
  606. const riscv_matrix_instance_f32 * src,
  607. riscv_matrix_instance_f32 * dst);
  608. /**
  609. * @brief Floating-point matrix inverse.
  610. * @param[in] src points to the instance of the input floating-point matrix structure.
  611. * @param[out] dst points to the instance of the output floating-point matrix structure.
  612. * @return The function returns RISCV_MATH_SIZE_MISMATCH, if the dimensions do not match.
  613. * If the input matrix is singular (does not have an inverse), then the algorithm terminates and returns error status RISCV_MATH_SINGULAR.
  614. */
  615. riscv_status riscv_mat_inverse_f64(
  616. const riscv_matrix_instance_f64 * src,
  617. riscv_matrix_instance_f64 * dst);
  618. /**
  619. * @brief Floating-point Cholesky decomposition of Symmetric Positive Definite Matrix.
  620. * @param[in] src points to the instance of the input floating-point matrix structure.
  621. * @param[out] dst points to the instance of the output floating-point matrix structure.
  622. * @return The function returns RISCV_MATH_SIZE_MISMATCH, if the dimensions do not match.
  623. * If the input matrix does not have a decomposition, then the algorithm terminates and returns error status RISCV_MATH_DECOMPOSITION_FAILURE.
  624. * If the matrix is ill conditioned or only semi-definite, then it is better using the LDL^t decomposition.
  625. * The decomposition is returning a lower triangular matrix.
  626. */
  627. riscv_status riscv_mat_cholesky_f64(
  628. const riscv_matrix_instance_f64 * src,
  629. riscv_matrix_instance_f64 * dst);
  630. /**
  631. * @brief Floating-point Cholesky decomposition of Symmetric Positive Definite Matrix.
  632. * @param[in] src points to the instance of the input floating-point matrix structure.
  633. * @param[out] dst points to the instance of the output floating-point matrix structure.
  634. * @return The function returns RISCV_MATH_SIZE_MISMATCH, if the dimensions do not match.
  635. * If the input matrix does not have a decomposition, then the algorithm terminates and returns error status RISCV_MATH_DECOMPOSITION_FAILURE.
  636. * If the matrix is ill conditioned or only semi-definite, then it is better using the LDL^t decomposition.
  637. * The decomposition is returning a lower triangular matrix.
  638. */
  639. riscv_status riscv_mat_cholesky_f32(
  640. const riscv_matrix_instance_f32 * src,
  641. riscv_matrix_instance_f32 * dst);
  642. /**
  643. * @brief Solve UT . X = A where UT is an upper triangular matrix
  644. * @param[in] ut The upper triangular matrix
  645. * @param[in] a The matrix a
  646. * @param[out] dst The solution X of UT . X = A
  647. * @return The function returns RISCV_MATH_SINGULAR, if the system can't be solved.
  648. */
  649. riscv_status riscv_mat_solve_upper_triangular_f32(
  650. const riscv_matrix_instance_f32 * ut,
  651. const riscv_matrix_instance_f32 * a,
  652. riscv_matrix_instance_f32 * dst);
  653. /**
  654. * @brief Solve LT . X = A where LT is a lower triangular matrix
  655. * @param[in] lt The lower triangular matrix
  656. * @param[in] a The matrix a
  657. * @param[out] dst The solution X of LT . X = A
  658. * @return The function returns RISCV_MATH_SINGULAR, if the system can't be solved.
  659. */
  660. riscv_status riscv_mat_solve_lower_triangular_f32(
  661. const riscv_matrix_instance_f32 * lt,
  662. const riscv_matrix_instance_f32 * a,
  663. riscv_matrix_instance_f32 * dst);
  664. /**
  665. * @brief Solve UT . X = A where UT is an upper triangular matrix
  666. * @param[in] ut The upper triangular matrix
  667. * @param[in] a The matrix a
  668. * @param[out] dst The solution X of UT . X = A
  669. * @return The function returns RISCV_MATH_SINGULAR, if the system can't be solved.
  670. */
  671. riscv_status riscv_mat_solve_upper_triangular_f64(
  672. const riscv_matrix_instance_f64 * ut,
  673. const riscv_matrix_instance_f64 * a,
  674. riscv_matrix_instance_f64 * dst);
  675. /**
  676. * @brief Solve LT . X = A where LT is a lower triangular matrix
  677. * @param[in] lt The lower triangular matrix
  678. * @param[in] a The matrix a
  679. * @param[out] dst The solution X of LT . X = A
  680. * @return The function returns RISCV_MATH_SINGULAR, if the system can't be solved.
  681. */
  682. riscv_status riscv_mat_solve_lower_triangular_f64(
  683. const riscv_matrix_instance_f64 * lt,
  684. const riscv_matrix_instance_f64 * a,
  685. riscv_matrix_instance_f64 * dst);
  686. /**
  687. * @brief Floating-point LDL decomposition of Symmetric Positive Semi-Definite Matrix.
  688. * @param[in] src points to the instance of the input floating-point matrix structure.
  689. * @param[out] l points to the instance of the output floating-point triangular matrix structure.
  690. * @param[out] d points to the instance of the output floating-point diagonal matrix structure.
  691. * @param[out] p points to the instance of the output floating-point permutation vector.
  692. * @return The function returns RISCV_MATH_SIZE_MISMATCH, if the dimensions do not match.
  693. * If the input matrix does not have a decomposition, then the algorithm terminates and returns error status RISCV_MATH_DECOMPOSITION_FAILURE.
  694. * The decomposition is returning a lower triangular matrix.
  695. */
  696. riscv_status riscv_mat_ldlt_f32(
  697. const riscv_matrix_instance_f32 * src,
  698. riscv_matrix_instance_f32 * l,
  699. riscv_matrix_instance_f32 * d,
  700. uint16_t * pp);
  701. /**
  702. * @brief Floating-point LDL decomposition of Symmetric Positive Semi-Definite Matrix.
  703. * @param[in] src points to the instance of the input floating-point matrix structure.
  704. * @param[out] l points to the instance of the output floating-point triangular matrix structure.
  705. * @param[out] d points to the instance of the output floating-point diagonal matrix structure.
  706. * @param[out] p points to the instance of the output floating-point permutation vector.
  707. * @return The function returns RISCV_MATH_SIZE_MISMATCH, if the dimensions do not match.
  708. * If the input matrix does not have a decomposition, then the algorithm terminates and returns error status RISCV_MATH_DECOMPOSITION_FAILURE.
  709. * The decomposition is returning a lower triangular matrix.
  710. */
  711. riscv_status riscv_mat_ldlt_f64(
  712. const riscv_matrix_instance_f64 * src,
  713. riscv_matrix_instance_f64 * l,
  714. riscv_matrix_instance_f64 * d,
  715. uint16_t * pp);
  716. /**
  717. @brief QR decomposition of a m x n floating point matrix with m >= n.
  718. @param[in] pSrc points to input matrix structure. The source matrix is modified by the function.
  719. @param[in] threshold norm2 threshold.
  720. @param[out] pOutR points to output R matrix structure of dimension m x n
  721. @param[out] pOutQ points to output Q matrix structure of dimension m x m
  722. @param[out] pOutTau points to Householder scaling factors of dimension n
  723. @param[inout] pTmpA points to a temporary vector of dimension m.
  724. @param[inout] pTmpB points to a temporary vector of dimension n.
  725. @return execution status
  726. - \ref RISCV_MATH_SUCCESS : Operation successful
  727. - \ref RISCV_MATH_SIZE_MISMATCH : Matrix size check failed
  728. - \ref RISCV_MATH_SINGULAR : Input matrix is found to be singular (non-invertible)
  729. */
  730. riscv_status riscv_mat_qr_f32(
  731. const riscv_matrix_instance_f32 * pSrc,
  732. const float32_t threshold,
  733. riscv_matrix_instance_f32 * pOutR,
  734. riscv_matrix_instance_f32 * pOutQ,
  735. float32_t * pOutTau,
  736. float32_t *pTmpA,
  737. float32_t *pTmpB
  738. );
  739. /**
  740. @brief QR decomposition of a m x n floating point matrix with m >= n.
  741. @param[in] pSrc points to input matrix structure. The source matrix is modified by the function.
  742. @param[in] threshold norm2 threshold.
  743. @param[out] pOutR points to output R matrix structure of dimension m x n
  744. @param[out] pOutQ points to output Q matrix structure of dimension m x m
  745. @param[out] pOutTau points to Householder scaling factors of dimension n
  746. @param[inout] pTmpA points to a temporary vector of dimension m.
  747. @param[inout] pTmpB points to a temporary vector of dimension n.
  748. @return execution status
  749. - \ref RISCV_MATH_SUCCESS : Operation successful
  750. - \ref RISCV_MATH_SIZE_MISMATCH : Matrix size check failed
  751. - \ref RISCV_MATH_SINGULAR : Input matrix is found to be singular (non-invertible)
  752. */
  753. riscv_status riscv_mat_qr_f64(
  754. const riscv_matrix_instance_f64 * pSrc,
  755. const float64_t threshold,
  756. riscv_matrix_instance_f64 * pOutR,
  757. riscv_matrix_instance_f64 * pOutQ,
  758. float64_t * pOutTau,
  759. float64_t *pTmpA,
  760. float64_t *pTmpB
  761. );
  762. /**
  763. @brief Householder transform of a floating point vector.
  764. @param[in] pSrc points to the input vector.
  765. @param[in] threshold norm2 threshold.
  766. @param[in] blockSize dimension of the vector space.
  767. @param[outQ] pOut points to the output vector.
  768. @return beta return the scaling factor beta
  769. */
  770. float32_t riscv_householder_f32(
  771. const float32_t * pSrc,
  772. const float32_t threshold,
  773. uint32_t blockSize,
  774. float32_t * pOut
  775. );
  776. /**
  777. @brief Householder transform of a double floating point vector.
  778. @param[in] pSrc points to the input vector.
  779. @param[in] threshold norm2 threshold.
  780. @param[in] blockSize dimension of the vector space.
  781. @param[outQ] pOut points to the output vector.
  782. @return beta return the scaling factor beta
  783. */
  784. float64_t riscv_householder_f64(
  785. const float64_t * pSrc,
  786. const float64_t threshold,
  787. uint32_t blockSize,
  788. float64_t * pOut
  789. );
  790. #ifdef __cplusplus
  791. }
  792. #endif
  793. #endif /* ifndef _MATRIX_FUNCTIONS_H_ */