ALGO_AlgorithmBase_ATY.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. /**
  2. * @file ALGO_AlgorithmBase_ATY.h
  3. *
  4. * @param Project ALGO_Algorithm_ATY_LIB
  5. *
  6. * @author ATY
  7. *
  8. * @copyright
  9. * - Copyright 2017 - 2026 MZ-ATY
  10. * - This code follows:
  11. * - MZ-ATY Various Contents Joint Statement -
  12. * <a href="https://mengze.top/MZ-ATY_VCJS">
  13. * https://mengze.top/MZ-ATY_VCJS</a>
  14. * - CC 4.0 BY-NC-SA -
  15. * <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/">
  16. * https://creativecommons.org/licenses/by-nc-sa/4.0/</a>
  17. * - Your use will be deemed to have accepted the terms of this statement.
  18. *
  19. * @brief functions of base algorithm
  20. *
  21. * @version
  22. * - 1_01_220601 > ATY
  23. * -# Preliminary version, first Release
  24. * - 1_02_251124 > ATY
  25. * -# add and test
  26. ********************************************************************************
  27. */
  28. #ifndef __ALGO_AlgorithmBase_ATY_H
  29. #define __ALGO_AlgorithmBase_ATY_H
  30. #include "INCLUDE_ATY.h"
  31. /******************************* For user *************************************/
  32. /******************************************************************************/
  33. /*
  34. | **Type** | **16-bit Platform** | **32-bit Platform** | **64-bit Unix (Linux/macOS)** | **64-bit Windows** |
  35. |---------------------|:-------------------:|:-------------------:|:-----------------------------:|:------------------:|
  36. | `char` | 1 byte | 1 byte | 1 byte | 1 byte |
  37. | `short` | 2 bytes | 2 bytes | 2 bytes | 2 bytes |
  38. | `int` | 2 bytes | 4 bytes | 4 bytes | 4 bytes |
  39. | `unsigned int` | 2 bytes | 4 bytes | 4 bytes | 4 bytes |
  40. | `float` | 4 bytes | 4 bytes | 4 bytes | 4 bytes |
  41. | `double` | 8 bytes | 8 bytes | 8 bytes | 8 bytes |
  42. | `long` | 4 bytes | 4 bytes | 8 bytes | **4 bytes** |
  43. | `long long` | 8 bytes | 8 bytes | 8 bytes | 8 bytes |
  44. | `unsigned long` | 4 bytes | 4 bytes | 8 bytes | **4 bytes** |
  45. | `Pointer` (`void*`) | 2 bytes | 4 bytes | 8 bytes | 8 bytes |
  46. | MaxAddressableSpace | 64 KB (2^16) | 4 GB (2^32) | Virtually 2^64 | Virtually 2^64 |
  47. */
  48. /* Macros: Basic Helpers ******************************************************/
  49. /**
  50. * @brief Change num to str
  51. * @param num number to change
  52. */
  53. #define ALGO_STR_DEF(num) #num
  54. #define ALGO_NUM_TO_STR(num) ALGO_STR_DEF(num)
  55. /**
  56. * @brief Allocate memory space
  57. * @param name variate name
  58. * @param type variate type
  59. */
  60. #define ALGO_MALLOC(name, type) ((type *) malloc((name) * sizeof(type)))
  61. /**
  62. * @brief Get a byte at the specified address
  63. * @param addr address
  64. */
  65. #define ALGO_MEM_BYTE(addr) (*((byte *)(addr)))
  66. /**
  67. * @brief Gets the number of specified bits
  68. * @param n bit position
  69. */
  70. #define ALGO_BITMASK(n) ((uint32_t)1 << n)
  71. /**
  72. * @brief Get absolute value
  73. * @param x value to deal
  74. */
  75. #define ALGO_ABS(x) ((x) > 0 ? (x) : -(x))
  76. /**
  77. * @brief Get the max between 2 numbers
  78. * @param x value to deal
  79. * @param y value to deal
  80. */
  81. #define ALGO_MAX(x, y) (((x) > (y)) ? (x) : (y))
  82. /**
  83. * @brief Get the min between 2 numbers
  84. * @param x value to deal
  85. * @param y value to deal
  86. */
  87. #define ALGO_MIN(x, y) (((x) < (y)) ? (x) : (y))
  88. /* Macros: Char & Digit Checks ***********************************************/
  89. /**
  90. * @brief Converts a character to uppercase
  91. * @param c character to deal
  92. */
  93. #define ALGO_UPCASE(c) (((c) >= 'a' && (c) <= 'z') ? ((c) - 0x20) : (c))
  94. /**
  95. * @brief Determines if the character is a decimalism number
  96. * @param c character to deal
  97. */
  98. #define ALGO_DECCHK(c) ((c) >= '0' && (c) <= '9')
  99. /**
  100. * @brief Determines if the character is a hexadecimal number
  101. * @param c character to deal
  102. */
  103. #define ALGO_HEXCHK(c) \
  104. (((c) >= '0' && (c) <= '9') \
  105. || ((c) >= 'A' && (c) <= 'F') \
  106. || ((c) >= 'a' && (c) <= 'f'))
  107. /* Macros: Array & Combine ****************************************************/
  108. /**
  109. * @brief Returns the number of array elements
  110. * @param a array to deal
  111. */
  112. #define ALGO_ARR_SIZE(a) (sizeof((a)) / sizeof((a[0])))
  113. /**
  114. * @brief Combine two uint8 to one uint16 in LSB format
  115. * @param a high bit of uint16
  116. * @param b low bit of uint16
  117. */
  118. #define ALGO_COMB16(a, b) ((((uint16_t)(a)) << 8) + (b))
  119. /**
  120. * @brief Combine four uint8 to one uint32 in LSB format
  121. * @param a high bit of uint32
  122. * @param b mid high bit of uint32
  123. * @param c mid low bit of uint32
  124. * @param d low bit of uint32
  125. */
  126. #define ALGO_COMB32(a, b, c, d) \
  127. ((((uint32_t)(a)) << 24) \
  128. + (((uint32_t)(b)) << 16)) \
  129. + ((((uint32_t)(c)) << 8) + (d))
  130. /**
  131. * @brief Get the low byte of a uint16
  132. * @param x value to deal
  133. */
  134. #define ALGO_UINT16_L(x) ((uint8_t)((uint16_t)(x) & 0xFF))
  135. /**
  136. * @brief Get the high byte of a uint16
  137. * @param x value to deal
  138. */
  139. #define ALGO_UINT16_H(x) ((uint8_t)((uint16_t)(x) >> 8))
  140. /* Macros: String & Tokens ****************************************************/
  141. /**
  142. * @brief Change to a character
  143. * @param a value to deal
  144. * @note equal to add '', accept less than 3 characters
  145. */
  146. #define ALGO_CHAR(a) @#a
  147. /**
  148. * @brief Change to string
  149. * @param a value to deal
  150. * @note equal to add ""
  151. */
  152. #define _ALGO_STR(a) #a
  153. #define ALGO_STR(a) ALGO_STR(a)
  154. /**
  155. * @brief Combine two value or characters to string
  156. * @param a value to deal
  157. * @param b value to deal
  158. */
  159. #define _ALGO_CAT(a, b) a##b
  160. #define ALGO_CAT(a, b) _ALGO_CAT(a, b)
  161. /* Macros: Logic **************************************************************/
  162. /**
  163. * @brief Change value to bool
  164. * @param x value to deal
  165. */
  166. #define ALGO_BOOL(x) (x) ? 1 : 0
  167. /**
  168. * @brief Invert value truth-value
  169. * @param x value to deal
  170. */
  171. #define ALGO_NOT(x) (x) ? 0 : 1
  172. /**
  173. * @brief AND two value
  174. * @param x value to deal
  175. * @param y value to deal
  176. */
  177. #define ALGO_AND(x, y) ((x) && (y)) ? 1 : 0
  178. /**
  179. * @brief OR two value
  180. * @param x value to deal
  181. * @param y value to deal
  182. */
  183. #define ALGO_OR(x, y) ((x) || (y)) ? 1 : 0
  184. /* Macros: Rotate *************************************************************/
  185. /**
  186. * @brief ROL rotate shift left
  187. * @param x value to deal
  188. * @param n shift bits
  189. * @todo X&0X00FF or etc
  190. */
  191. #define ALGO_ROTATE_LEFT(x, n) ((x) << (n)) | ((x) >> ((sizeof(x)) - (n)))
  192. /**
  193. * @brief ROR rotate shift right
  194. * @param x value to deal
  195. * @param n shift bits
  196. * @todo X&0X00FF or etc
  197. */
  198. #define ALGO_ROTATE_RIGHT(x, n) ((x) >> (n)) | ((x) << ((sizeof(x)) - (n)))
  199. /* Macros: Endian & Bit Ops ***************************************************/
  200. /**
  201. * @brief Invert little-endian and big-endian
  202. * @param x value to deal
  203. */
  204. #define ALGO_INVERT_BELE(x) \
  205. { \
  206. if(sizeof(x) == sizeof(uint8_t)) {} \
  207. else if(sizeof(x) == sizeof(uint16_t)) \
  208. {x = (uint16_t)((((uint16_t)(x) & 0x00FF) << 8) | (((uint16_t)(x) & 0xFF00) >> 8));} \
  209. else if(sizeof(x) == sizeof(uint32_t)) \
  210. {x = (uint32_t)((((uint32_t)(x) & 0xFF000000) >> 24) | (((uint32_t)(x) & 0x00FF0000) >> 8) \
  211. | (((uint32_t)(x) & 0x0000FF00) << 8) | (((uint32_t)(x) & 0x000000FF) << 24));} \
  212. }
  213. /**
  214. * @brief Invert value every bit(LSB <-> MSB)
  215. * @param x value to deal
  216. */
  217. #define ALGO_INVERT_BITS(x) \
  218. { \
  219. x = ((x & 0xAAAAAAAAAAAAAAAA) >> 1) | ((x & 0x5555555555555555) << 1); \
  220. x = ((x & 0xCCCCCCCCCCCCCCCC) >> 2) | ((x & 0x3333333333333333) << 2); \
  221. x = ((x & 0xF0F0F0F0F0F0F0F0) >> 4) | ((x & 0x0F0F0F0F0F0F0F0F) << 4); \
  222. ALGO_SWAP_BELE(x); \
  223. }
  224. /**
  225. * @brief Invert uint8 value every bit(LSB <-> MSB)
  226. * @param x value to deal
  227. */
  228. #define ALGO_INVERT_BITS_BYTE(x) \
  229. { \
  230. x = ((x & 0xAA) >> 1) | ((x & 0x55) << 1); \
  231. x = ((x & 0xCC) >> 2) | ((x & 0x33) << 2); \
  232. x = ((x & 0xF0) >> 4) | ((x & 0x0F) << 4); \
  233. }
  234. /* Macros: Binary String ******************************************************/
  235. /**
  236. * @brief Convert uint8 to binary string
  237. * @param n value to deal
  238. */
  239. #define ALGO_Uint8ToBin(n) \
  240. ( \
  241. ((n >> 21) & 0x80) | \
  242. ((n >> 18) & 0x40) | \
  243. ((n >> 15) & 0x20) | \
  244. ((n >> 12) & 0x10) | \
  245. ((n >> 9) & 0x08) | \
  246. ((n >> 6) & 0x04) | \
  247. ((n >> 3) & 0x02) | \
  248. ((n ) & 0x01) \
  249. )
  250. #define ALGO_Bin(n) ALGO_Uint8ToBin(0x##n##l)
  251. /* Macros: Swap & Sort *********************************************************/
  252. /**
  253. * @brief Swap numbers in easy way
  254. * @param x value to deal
  255. * @param y value to deal
  256. */
  257. #define ALGO_SWAP_EASY(x, y)\
  258. x = x + y;\
  259. y = x - y;\
  260. x = x - y;
  261. /**
  262. * @brief Swap numbers
  263. * @param dataType x & y data type
  264. * @param vN variableNum is set to deal with multiple calls in one scope
  265. * @param x value to deal
  266. * @param y value to deal
  267. * @note x & y must be the same type
  268. */
  269. #define ALGO_SWAP(dataType, vN, x, y)\
  270. {\
  271. dataType swap_t##vN = x;\
  272. x = y;\
  273. y = swap_t##vN;\
  274. }
  275. /**
  276. * @brief Swap numbers in addr (not tested)
  277. * @param vN variableNum is set to deal with multiple calls in one scope
  278. * @param x value to deal
  279. * @param y value to deal
  280. * @note x & y must be the same type
  281. */
  282. #define ALGO_SWAP_ADDR(vN, x, y)\
  283. {\
  284. uint32_t swapAddr_t##vN = x; \
  285. x = y; \
  286. y = swapAddr_t##vN; \
  287. }
  288. /**
  289. * @brief Sort numbers in group with low first
  290. * @param dataType dataInGroup data type
  291. * @param vN variableNum is set to deal with multiple calls in one scope
  292. * @param dataInGroup data group which need to sort
  293. */
  294. #define ALGO_Sort(dataType, vN, dataInGroup) \
  295. { \
  296. uint32_t as_i##vN, as_j##vN, dataInGroupSize##vN; \
  297. dataInGroupSize##vN = sizeof(dataInGroup)/sizeof(dataType); \
  298. for(as_i##vN = 0; as_i##vN < dataInGroupSize##vN; as_i##vN++) \
  299. { \
  300. for(as_j##vN = as_i##vN + 1; as_j##vN < dataInGroupSize##vN; as_j##vN++) \
  301. { \
  302. if(dataInGroup[as_i##vN] > dataInGroup[as_j##vN]) \
  303. { \
  304. ALGO_SWAP(dataType, _as_0, dataInGroup[as_i##vN], dataInGroup[as_j##vN]); \
  305. } \
  306. } \
  307. } \
  308. }
  309. /**
  310. * @brief Sort numbers in group with high first
  311. * @param dataType dataInGroup data type
  312. * @param vN variableNum is set to deal with multiple calls in one scope
  313. * @param dataInGroup data group which need to sort
  314. */
  315. #define ALGO_Sort_High(dataType, vN, dataInGroup) \
  316. { \
  317. uint32_t as_i##vN, as_j##vN, dataInGroupSize##vN; \
  318. dataInGroupSize##vN = sizeof(dataInGroup)/sizeof(dataType); \
  319. for(as_i##vN = 0; as_i##vN < dataInGroupSize##vN; as_i##vN++) \
  320. { \
  321. for(as_j##vN = as_i##vN + 1; as_j##vN < dataInGroupSize##vN; as_j##vN++) \
  322. { \
  323. if(dataInGroup[as_i##vN] < dataInGroup[as_j##vN]) \
  324. { \
  325. ALGO_SWAP(dataType, _as_0, dataInGroup[as_i##vN], dataInGroup[as_j##vN]); \
  326. } \
  327. } \
  328. } \
  329. }
  330. /**
  331. * @brief Calculate averange with delete extremum
  332. * @param dataType x & y data type
  333. * @param vN variableNum is set to deal with multiple calls in one scope
  334. * @param dataInGroup data group which need to deel
  335. * @param dataOut output number after deel
  336. * @note dataOut & dataInGroup must be the same type
  337. */
  338. #define ALGO_AverageInDelExtremum(dataType, vN, dataInGroup, dataOut) \
  339. { \
  340. uint32_t aaide_i##vN, dataInGroupSize##vN; \
  341. dataInGroupSize##vN = sizeof(dataInGroup)/sizeof(dataType); \
  342. ALGO_Sort(dataType, _aaide_0, dataInGroup); \
  343. dataOut = 0; \
  344. for(aaide_i##vN = 1; aaide_i##vN < dataInGroupSize##vN - 1; aaide_i##vN++) \
  345. dataOut += dataInGroup[aaide_i##vN]; \
  346. dataOut /= dataInGroupSize##vN - 2; \
  347. }
  348. /* APIs: Inversion Group ******************************************************/
  349. void ALGO_InvertUint8_Group(uint8_t* genBuf, uint8_t* srcBuf);
  350. void ALGO_InvertUint16_Group(uint16_t* genBuf, uint16_t* srcBuf);
  351. void ALGO_InvertUint32_Group(uint32_t* genBuf, uint32_t* srcBuf);
  352. void ALGO_InvertBitsN_Group(uint32_t* genBuf, uint32_t* srcBuf, uint8_t len);
  353. /* End of Inversion Group ******************************************************/
  354. /* APIs: Math *****************************************************************/
  355. float ALGO_MATH_POW_EASY(float x, uint8_t n);
  356. int ALGO_MATH_POW_QUICK(int x, int n);
  357. float ALGO_MATH_POW(float x, int n);
  358. int ALGO_MATH_SQRT(int x);
  359. float ALGO_MATH_LogLn(float num);
  360. float ALGO_Sqrt_NewtonNumber(float x);
  361. //Sr-sampling rate, times/second),f-stop freq(Hz),Pi-(3.14...)
  362. //k=(2*Pi*f)/Sr
  363. /* APIs: RC Filter ************************************************************/
  364. typedef struct ALGO_RC_FilterPara{
  365. double k; // filter series
  366. double lVal; // last calc value
  367. }ALGO_RC_FilterPara_t;
  368. double ALGO_RC_LpFilter(ALGO_RC_FilterPara_t* rcPara, double val);
  369. double ALGO_RC_HpFilter(ALGO_RC_FilterPara_t* rcPara, double val);
  370. /* APIs: Utilities ************************************************************/
  371. float ALGO_NumberSuitScop(float valueIn, float scopMin, float scopMax, float step);
  372. uint16_t ALGO_BinarySearch(const double* arr, uint16_t size, double target);
  373. /* APIs: Debug ****************************************************************/
  374. // #define ALGO_DEBUG_ALGO_AlgorithmBase_ATY
  375. #ifdef __DEBUG_ALGO_AlgorithmBase_ATY
  376. void ALGO_Swap_Test(void);
  377. void ALGO_Sort_Test(void);
  378. void ALGO_AverageInDelExtremum_Test(void);
  379. void ALGO_INVERT_Test(void);
  380. void ALGO_Test_Whole(void);
  381. #endif /* __DEBUG_ALGO_AlgorithmBase_ATY */
  382. // #define ALGO_AlgorithmBase_ATY_Test_ATY
  383. #ifdef ALGO_AlgorithmBase_ATY_Test_ATY
  384. uint32_t ALGO_AlgorithmBase_ATY_Test(void);
  385. #endif /* ALGO_AlgorithmBase_ATY_Test_ATY */
  386. #endif /* __ALGO_AlgorithmBase_ATY_H */
  387. /******************************** End Of File *********************************/