support_functions.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. /******************************************************************************
  2. * @file support_functions.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_H_
  27. #define SUPPORT_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 groupSupport Support Functions
  38. */
  39. /**
  40. * @brief Converts the elements of the 64 bit floating-point vector to floating-point vector.
  41. * @param[in] pSrc points to the floating-point 64 input vector
  42. * @param[out] pDst points to the floating-point output vector
  43. * @param[in] blockSize length of the input vector
  44. */
  45. void riscv_f64_to_float(
  46. const float64_t * pSrc,
  47. float32_t * pDst,
  48. uint32_t blockSize);
  49. /**
  50. * @brief Converts the elements of the 64 bit floating-point vector to Q31 vector.
  51. * @param[in] pSrc points to the floating-point 64 input vector
  52. * @param[out] pDst points to the Q31 output vector
  53. * @param[in] blockSize length of the input vector
  54. */
  55. void riscv_f64_to_q31(
  56. const float64_t * pSrc,
  57. q31_t * pDst,
  58. uint32_t blockSize);
  59. /**
  60. * @brief Converts the elements of the 64 bit floating-point vector to Q15 vector.
  61. * @param[in] pSrc points to the floating-point 64 input vector
  62. * @param[out] pDst points to the Q15 output vector
  63. * @param[in] blockSize length of the input vector
  64. */
  65. void riscv_f64_to_q15(
  66. const float64_t * pSrc,
  67. q15_t * pDst,
  68. uint32_t blockSize);
  69. /**
  70. * @brief Converts the elements of the 64 bit floating-point vector to Q7 vector.
  71. * @param[in] pSrc points to the floating-point 64 input vector
  72. * @param[out] pDst points to the Q7 output vector
  73. * @param[in] blockSize length of the input vector
  74. */
  75. void riscv_f64_to_q7(
  76. const float64_t * pSrc,
  77. q7_t * pDst,
  78. uint32_t blockSize);
  79. /**
  80. * @brief Converts the elements of the floating-point vector to 64 bit floating-point vector.
  81. * @param[in] pSrc points to the floating-point input vector
  82. * @param[out] pDst points to the 64 bit floating-point output vector
  83. * @param[in] blockSize length of the input vector
  84. */
  85. void riscv_float_to_f64(
  86. const float32_t * pSrc,
  87. float64_t * pDst,
  88. uint32_t blockSize);
  89. /**
  90. * @brief Converts the elements of the floating-point vector to Q31 vector.
  91. * @param[in] pSrc points to the floating-point input vector
  92. * @param[out] pDst points to the Q31 output vector
  93. * @param[in] blockSize length of the input vector
  94. */
  95. void riscv_float_to_q31(
  96. const float32_t * pSrc,
  97. q31_t * pDst,
  98. uint32_t blockSize);
  99. /**
  100. * @brief Converts the elements of the floating-point vector to Q15 vector.
  101. * @param[in] pSrc points to the floating-point input vector
  102. * @param[out] pDst points to the Q15 output vector
  103. * @param[in] blockSize length of the input vector
  104. */
  105. void riscv_float_to_q15(
  106. const float32_t * pSrc,
  107. q15_t * pDst,
  108. uint32_t blockSize);
  109. /**
  110. * @brief Converts the elements of the floating-point vector to Q7 vector.
  111. * @param[in] pSrc points to the floating-point input vector
  112. * @param[out] pDst points to the Q7 output vector
  113. * @param[in] blockSize length of the input vector
  114. */
  115. void riscv_float_to_q7(
  116. const float32_t * pSrc,
  117. q7_t * pDst,
  118. uint32_t blockSize);
  119. /**
  120. * @brief Converts the elements of the Q31 vector to 64 bit floating-point vector.
  121. * @param[in] pSrc is input pointer
  122. * @param[out] pDst is output pointer
  123. * @param[in] blockSize is the number of samples to process
  124. */
  125. void riscv_q31_to_f64(
  126. const q31_t * pSrc,
  127. float64_t * pDst,
  128. uint32_t blockSize);
  129. /**
  130. * @brief Converts the elements of the Q31 vector to floating-point vector.
  131. * @param[in] pSrc is input pointer
  132. * @param[out] pDst is output pointer
  133. * @param[in] blockSize is the number of samples to process
  134. */
  135. void riscv_q31_to_float(
  136. const q31_t * pSrc,
  137. float32_t * pDst,
  138. uint32_t blockSize);
  139. /**
  140. * @brief Converts the elements of the Q31 vector to Q15 vector.
  141. * @param[in] pSrc is input pointer
  142. * @param[out] pDst is output pointer
  143. * @param[in] blockSize is the number of samples to process
  144. */
  145. void riscv_q31_to_q15(
  146. const q31_t * pSrc,
  147. q15_t * pDst,
  148. uint32_t blockSize);
  149. /**
  150. * @brief Converts the elements of the Q31 vector to Q7 vector.
  151. * @param[in] pSrc is input pointer
  152. * @param[out] pDst is output pointer
  153. * @param[in] blockSize is the number of samples to process
  154. */
  155. void riscv_q31_to_q7(
  156. const q31_t * pSrc,
  157. q7_t * pDst,
  158. uint32_t blockSize);
  159. /**
  160. * @brief Converts the elements of the Q15 vector to 64 bit floating-point vector.
  161. * @param[in] pSrc is input pointer
  162. * @param[out] pDst is output pointer
  163. * @param[in] blockSize is the number of samples to process
  164. */
  165. void riscv_q15_to_f64(
  166. const q15_t * pSrc,
  167. float64_t * pDst,
  168. uint32_t blockSize);
  169. /**
  170. * @brief Converts the elements of the Q15 vector to floating-point vector.
  171. * @param[in] pSrc is input pointer
  172. * @param[out] pDst is output pointer
  173. * @param[in] blockSize is the number of samples to process
  174. */
  175. void riscv_q15_to_float(
  176. const q15_t * pSrc,
  177. float32_t * pDst,
  178. uint32_t blockSize);
  179. /**
  180. * @brief Converts the elements of the Q15 vector to Q31 vector.
  181. * @param[in] pSrc is input pointer
  182. * @param[out] pDst is output pointer
  183. * @param[in] blockSize is the number of samples to process
  184. */
  185. void riscv_q15_to_q31(
  186. const q15_t * pSrc,
  187. q31_t * pDst,
  188. uint32_t blockSize);
  189. /**
  190. * @brief Converts the elements of the Q15 vector to Q7 vector.
  191. * @param[in] pSrc is input pointer
  192. * @param[out] pDst is output pointer
  193. * @param[in] blockSize is the number of samples to process
  194. */
  195. void riscv_q15_to_q7(
  196. const q15_t * pSrc,
  197. q7_t * pDst,
  198. uint32_t blockSize);
  199. /**
  200. * @brief Converts the elements of the Q7 vector to 64 bit floating-point vector.
  201. * @param[in] pSrc is input pointer
  202. * @param[out] pDst is output pointer
  203. * @param[in] blockSize is the number of samples to process
  204. */
  205. void riscv_q7_to_f64(
  206. const q7_t * pSrc,
  207. float64_t * pDst,
  208. uint32_t blockSize);
  209. /**
  210. * @brief Converts the elements of the Q7 vector to floating-point vector.
  211. * @param[in] pSrc is input pointer
  212. * @param[out] pDst is output pointer
  213. * @param[in] blockSize is the number of samples to process
  214. */
  215. void riscv_q7_to_float(
  216. const q7_t * pSrc,
  217. float32_t * pDst,
  218. uint32_t blockSize);
  219. /**
  220. * @brief Converts the elements of the Q7 vector to Q31 vector.
  221. * @param[in] pSrc input pointer
  222. * @param[out] pDst output pointer
  223. * @param[in] blockSize number of samples to process
  224. */
  225. void riscv_q7_to_q31(
  226. const q7_t * pSrc,
  227. q31_t * pDst,
  228. uint32_t blockSize);
  229. /**
  230. * @brief Converts the elements of the Q7 vector to Q15 vector.
  231. * @param[in] pSrc input pointer
  232. * @param[out] pDst output pointer
  233. * @param[in] blockSize number of samples to process
  234. */
  235. void riscv_q7_to_q15(
  236. const q7_t * pSrc,
  237. q15_t * pDst,
  238. uint32_t blockSize);
  239. /**
  240. * @brief Struct for specifying sorting algorithm
  241. */
  242. typedef enum
  243. {
  244. RISCV_SORT_BITONIC = 0,
  245. /**< Bitonic sort */
  246. RISCV_SORT_BUBBLE = 1,
  247. /**< Bubble sort */
  248. RISCV_SORT_HEAP = 2,
  249. /**< Heap sort */
  250. RISCV_SORT_INSERTION = 3,
  251. /**< Insertion sort */
  252. RISCV_SORT_QUICK = 4,
  253. /**< Quick sort */
  254. RISCV_SORT_SELECTION = 5
  255. /**< Selection sort */
  256. } riscv_sort_alg;
  257. /**
  258. * @brief Struct for specifying sorting algorithm
  259. */
  260. typedef enum
  261. {
  262. RISCV_SORT_DESCENDING = 0,
  263. /**< Descending order (9 to 0) */
  264. RISCV_SORT_ASCENDING = 1
  265. /**< Ascending order (0 to 9) */
  266. } riscv_sort_dir;
  267. /**
  268. * @brief Instance structure for the sorting algorithms.
  269. */
  270. typedef struct
  271. {
  272. riscv_sort_alg alg; /**< Sorting algorithm selected */
  273. riscv_sort_dir dir; /**< Sorting order (direction) */
  274. } riscv_sort_instance_f32;
  275. /**
  276. * @param[in] S points to an instance of the sorting structure.
  277. * @param[in] pSrc points to the block of input data.
  278. * @param[out] pDst points to the block of output data.
  279. * @param[in] blockSize number of samples to process.
  280. */
  281. void riscv_sort_f32(
  282. const riscv_sort_instance_f32 * S,
  283. float32_t * pSrc,
  284. float32_t * pDst,
  285. uint32_t blockSize);
  286. /**
  287. * @param[in,out] S points to an instance of the sorting structure.
  288. * @param[in] alg Selected algorithm.
  289. * @param[in] dir Sorting order.
  290. */
  291. void riscv_sort_init_f32(
  292. riscv_sort_instance_f32 * S,
  293. riscv_sort_alg alg,
  294. riscv_sort_dir dir);
  295. /**
  296. * @brief Instance structure for the sorting algorithms.
  297. */
  298. typedef struct
  299. {
  300. riscv_sort_dir dir; /**< Sorting order (direction) */
  301. float32_t * buffer; /**< Working buffer */
  302. } riscv_merge_sort_instance_f32;
  303. /**
  304. * @param[in] S points to an instance of the sorting structure.
  305. * @param[in,out] pSrc points to the block of input data.
  306. * @param[out] pDst points to the block of output data
  307. * @param[in] blockSize number of samples to process.
  308. */
  309. void riscv_merge_sort_f32(
  310. const riscv_merge_sort_instance_f32 * S,
  311. float32_t *pSrc,
  312. float32_t *pDst,
  313. uint32_t blockSize);
  314. /**
  315. * @param[in,out] S points to an instance of the sorting structure.
  316. * @param[in] dir Sorting order.
  317. * @param[in] buffer Working buffer.
  318. */
  319. void riscv_merge_sort_init_f32(
  320. riscv_merge_sort_instance_f32 * S,
  321. riscv_sort_dir dir,
  322. float32_t * buffer);
  323. /**
  324. * @brief Copies the elements of a floating-point vector.
  325. * @param[in] pSrc input pointer
  326. * @param[out] pDst output pointer
  327. * @param[in] blockSize number of samples to process
  328. */
  329. void riscv_copy_f32(
  330. const float32_t * pSrc,
  331. float32_t * pDst,
  332. uint32_t blockSize);
  333. /**
  334. * @brief Copies the elements of a floating-point vector.
  335. * @param[in] pSrc input pointer
  336. * @param[out] pDst output pointer
  337. * @param[in] blockSize number of samples to process
  338. */
  339. void riscv_copy_f64(
  340. const float64_t * pSrc,
  341. float64_t * pDst,
  342. uint32_t blockSize);
  343. /**
  344. * @brief Copies the elements of a Q7 vector.
  345. * @param[in] pSrc input pointer
  346. * @param[out] pDst output pointer
  347. * @param[in] blockSize number of samples to process
  348. */
  349. void riscv_copy_q7(
  350. const q7_t * pSrc,
  351. q7_t * pDst,
  352. uint32_t blockSize);
  353. /**
  354. * @brief Copies the elements of a Q15 vector.
  355. * @param[in] pSrc input pointer
  356. * @param[out] pDst output pointer
  357. * @param[in] blockSize number of samples to process
  358. */
  359. void riscv_copy_q15(
  360. const q15_t * pSrc,
  361. q15_t * pDst,
  362. uint32_t blockSize);
  363. /**
  364. * @brief Copies the elements of a Q31 vector.
  365. * @param[in] pSrc input pointer
  366. * @param[out] pDst output pointer
  367. * @param[in] blockSize number of samples to process
  368. */
  369. void riscv_copy_q31(
  370. const q31_t * pSrc,
  371. q31_t * pDst,
  372. uint32_t blockSize);
  373. /**
  374. * @brief Fills a constant value into a floating-point vector.
  375. * @param[in] value input value to be filled
  376. * @param[out] pDst output pointer
  377. * @param[in] blockSize number of samples to process
  378. */
  379. void riscv_fill_f32(
  380. float32_t value,
  381. float32_t * pDst,
  382. uint32_t blockSize);
  383. /**
  384. * @brief Fills a constant value into a floating-point vector.
  385. * @param[in] value input value to be filled
  386. * @param[out] pDst output pointer
  387. * @param[in] blockSize number of samples to process
  388. */
  389. void riscv_fill_f64(
  390. float64_t value,
  391. float64_t * pDst,
  392. uint32_t blockSize);
  393. /**
  394. * @brief Fills a constant value into a Q7 vector.
  395. * @param[in] value input value to be filled
  396. * @param[out] pDst output pointer
  397. * @param[in] blockSize number of samples to process
  398. */
  399. void riscv_fill_q7(
  400. q7_t value,
  401. q7_t * pDst,
  402. uint32_t blockSize);
  403. /**
  404. * @brief Fills a constant value into a Q15 vector.
  405. * @param[in] value input value to be filled
  406. * @param[out] pDst output pointer
  407. * @param[in] blockSize number of samples to process
  408. */
  409. void riscv_fill_q15(
  410. q15_t value,
  411. q15_t * pDst,
  412. uint32_t blockSize);
  413. /**
  414. * @brief Fills a constant value into a Q31 vector.
  415. * @param[in] value input value to be filled
  416. * @param[out] pDst output pointer
  417. * @param[in] blockSize number of samples to process
  418. */
  419. void riscv_fill_q31(
  420. q31_t value,
  421. q31_t * pDst,
  422. uint32_t blockSize);
  423. /**
  424. * @brief Weighted average
  425. *
  426. *
  427. * @param[in] *in Array of input values.
  428. * @param[in] *weigths Weights
  429. * @param[in] blockSize Number of samples in the input array.
  430. * @return Weighted average
  431. *
  432. */
  433. float32_t riscv_weighted_average_f32(const float32_t *in
  434. , const float32_t *weigths
  435. , uint32_t blockSize);
  436. /**
  437. * @brief Barycenter
  438. *
  439. *
  440. * @param[in] in List of vectors
  441. * @param[in] weights Weights of the vectors
  442. * @param[out] out Barycenter
  443. * @param[in] nbVectors Number of vectors
  444. * @param[in] vecDim Dimension of space (vector dimension)
  445. *
  446. */
  447. void riscv_barycenter_f32(const float32_t *in
  448. , const float32_t *weights
  449. , float32_t *out
  450. , uint32_t nbVectors
  451. , uint32_t vecDim);
  452. #ifdef __cplusplus
  453. }
  454. #endif
  455. #endif /* ifndef _SUPPORT_FUNCTIONS_H_ */