您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 

287 行
13 KiB

  1. /*
  2. *********************************************************************************************************
  3. * uC/LIB
  4. * Custom Library Modules
  5. *
  6. * Copyright 2004-2021 Silicon Laboratories Inc. www.silabs.com
  7. *
  8. * SPDX-License-Identifier: APACHE-2.0
  9. *
  10. * This software is subject to an open source license and is distributed by
  11. * Silicon Laboratories Inc. pursuant to the terms of the Apache License,
  12. * Version 2.0 available at www.apache.org/licenses/LICENSE-2.0.
  13. *
  14. *********************************************************************************************************
  15. */
  16. /*
  17. *********************************************************************************************************
  18. *
  19. * MATHEMATIC OPERATIONS
  20. *
  21. * Filename : lib_math.h
  22. * Version : V1.39.01
  23. *********************************************************************************************************
  24. * Note(s) : (1) NO compiler-supplied standard library functions are used in library or product software.
  25. *
  26. * (a) ALL standard library functions are implemented in the custom library modules :
  27. *
  28. * (1) \<Custom Library Directory>\lib_*.*
  29. *
  30. * (2) \<Custom Library Directory>\Ports\<cpu>\<compiler>\lib*_a.*
  31. *
  32. * where
  33. * <Custom Library Directory> directory path for custom library software
  34. * <cpu> directory name for specific processor (CPU)
  35. * <compiler> directory name for specific compiler
  36. *
  37. * (b) Product-specific library functions are implemented in individual products.
  38. *
  39. *********************************************************************************************************
  40. * Notice(s) : (1) The Institute of Electrical and Electronics Engineers and The Open Group, have given
  41. * us permission to reprint portions of their documentation. Portions of this text are
  42. * reprinted and reproduced in electronic form from the IEEE Std 1003.1, 2004 Edition,
  43. * Standard for Information Technology -- Portable Operating System Interface (POSIX),
  44. * The Open Group Base Specifications Issue 6, Copyright (C) 2001-2004 by the Institute
  45. * of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any
  46. * discrepancy between these versions and the original IEEE and The Open Group Standard,
  47. * the original IEEE and The Open Group Standard is the referee document. The original
  48. * Standard can be obtained online at http://www.opengroup.org/unix/online.html.
  49. *********************************************************************************************************
  50. */
  51. /*
  52. *********************************************************************************************************
  53. * MODULE
  54. *
  55. * Note(s) : (1) This mathematics library header file is protected from multiple pre-processor inclusion
  56. * through use of the mathematics library module present pre-processor macro definition.
  57. *********************************************************************************************************
  58. */
  59. #ifndef LIB_MATH_MODULE_PRESENT /* See Note #1. */
  60. #define LIB_MATH_MODULE_PRESENT
  61. /*
  62. *********************************************************************************************************
  63. * INCLUDE FILES
  64. *
  65. * Note(s) : (1) The custom library software files are located in the following directories :
  66. *
  67. * (a) \<Custom Library Directory>\lib_*.*
  68. *
  69. * where
  70. * <Custom Library Directory> directory path for custom library software
  71. *
  72. * (2) CPU-configuration software files are located in the following directories :
  73. *
  74. * (a) \<CPU-Compiler Directory>\cpu_*.*
  75. * (b) \<CPU-Compiler Directory>\<cpu>\<compiler>\cpu*.*
  76. *
  77. * where
  78. * <CPU-Compiler Directory> directory path for common CPU-compiler software
  79. * <cpu> directory name for specific processor (CPU)
  80. * <compiler> directory name for specific compiler
  81. *
  82. * (3) Compiler MUST be configured to include as additional include path directories :
  83. *
  84. * (a) '\<Custom Library Directory>\' directory See Note #1a
  85. *
  86. * (b) (1) '\<CPU-Compiler Directory>\' directory See Note #2a
  87. * (2) '\<CPU-Compiler Directory>\<cpu>\<compiler>\' directory See Note #2b
  88. *
  89. * (4) NO compiler-supplied standard library functions SHOULD be used.
  90. *********************************************************************************************************
  91. */
  92. #include <cpu.h>
  93. #include <cpu_core.h>
  94. #include <lib_def.h>
  95. /*
  96. *********************************************************************************************************
  97. * EXTERNS
  98. *********************************************************************************************************
  99. */
  100. #ifdef LIB_MATH_MODULE
  101. #define LIB_MATH_EXT
  102. #else
  103. #define LIB_MATH_EXT extern
  104. #endif
  105. /*
  106. *********************************************************************************************************
  107. * DEFINES
  108. *********************************************************************************************************
  109. */
  110. /*
  111. *********************************************************************************************************
  112. * RANDOM NUMBER DEFINES
  113. *
  114. * Note(s) : (1) (a) IEEE Std 1003.1, 2004 Edition, Section 'rand() : DESCRIPTION' states that "if rand()
  115. * is called before any calls to srand() are made, the same sequence shall be generated
  116. * as when srand() is first called with a seed value of 1".
  117. *
  118. * (b) (1) BSD/ANSI-C implements rand() as a Linear Congruential Generator (LCG) :
  119. *
  120. * (A) random_number = [(a * random_number ) + b] modulo m
  121. * n + 1 n
  122. *
  123. * where
  124. * (1) (a) random_number Next random number to generate
  125. * n+1
  126. * (b) random_number Previous random number generated
  127. * n
  128. * (c) random_number Initial random number seed
  129. * 0 See also Note #1a
  130. *
  131. * (2) a = 1103515245 LCG multiplier
  132. * (3) b = 12345 LCG incrementor
  133. * (4) m = RAND_MAX + 1 LCG modulus See also Note #1b2
  134. *
  135. * (2) (A) IEEE Std 1003.1, 2004 Edition, Section 'rand() : DESCRIPTION' states that
  136. * "rand() ... shall compute a sequence of pseudo-random integers in the range
  137. * [0, {RAND_MAX}] with a period of at least 2^32".
  138. *
  139. * (B) However, BSD/ANSI-C 'stdlib.h' defines "RAND_MAX" as "0x7fffffff", or 2^31;
  140. * which therefore limits the range AND period to no more than 2^31.
  141. *********************************************************************************************************
  142. */
  143. #define RAND_SEED_INIT_VAL 1u /* See Note #1a. */
  144. #define RAND_LCG_PARAM_M 0x7FFFFFFFu /* See Note #1b2B. */
  145. #define RAND_LCG_PARAM_A 1103515245u /* See Note #1b1A2. */
  146. #define RAND_LCG_PARAM_B 12345u /* See Note #1b1A3. */
  147. /*
  148. *********************************************************************************************************
  149. * DATA TYPES
  150. *********************************************************************************************************
  151. */
  152. /*
  153. *********************************************************************************************************
  154. * RANDOM NUMBER DATA TYPE
  155. *********************************************************************************************************
  156. */
  157. typedef CPU_INT32U RAND_NBR;
  158. /*
  159. *********************************************************************************************************
  160. * GLOBAL VARIABLES
  161. *********************************************************************************************************
  162. */
  163. /*
  164. *********************************************************************************************************
  165. * MACROS
  166. *********************************************************************************************************
  167. */
  168. /*
  169. *********************************************************************************************************
  170. * MATH_IS_PWR2()
  171. *
  172. * Description : Determine if a value is a power of 2.
  173. *
  174. * Argument(s) : nbr Value.
  175. *
  176. * Return(s) : DEF_YES, 'nbr' is a power of 2.
  177. *
  178. * DEF_NO, 'nbr' is not a power of 2.
  179. *
  180. * Caller(s) : Application.
  181. *
  182. * Note(s) : none.
  183. *********************************************************************************************************
  184. */
  185. #define MATH_IS_PWR2(nbr) ((((nbr) != 0u) && (((nbr) & ((nbr) - 1u)) == 0u)) ? DEF_YES : DEF_NO)
  186. /*
  187. *********************************************************************************************************
  188. * MATH_ROUND_INC_UP_PWR2()
  189. *
  190. * Description : Round value up to the next (power of 2) increment.
  191. *
  192. * Argument(s) : nbr Value to round.
  193. *
  194. * inc Increment to use. MUST be a power of 2.
  195. *
  196. * Return(s) : Rounded up value.
  197. *
  198. * Caller(s) : Application.
  199. *
  200. * Note(s) : none.
  201. *********************************************************************************************************
  202. */
  203. #define MATH_ROUND_INC_UP_PWR2(nbr, inc) (((nbr) & ~((inc) - 1)) + (((nbr) & ((inc) - 1)) == 0 ? 0 : (inc)))
  204. /*
  205. *********************************************************************************************************
  206. * MATH_ROUND_INC_UP()
  207. *
  208. * Description : Round value up to the next increment.
  209. *
  210. * Argument(s) : nbr Value to round.
  211. *
  212. * inc Increment to use.
  213. *
  214. * Return(s) : Rounded up value.
  215. *
  216. * Caller(s) : Application.
  217. *
  218. * Note(s) : none.
  219. *********************************************************************************************************
  220. */
  221. #define MATH_ROUND_INC_UP(nbr, inc) (((nbr) + ((inc) - 1)) / (inc) * (inc))
  222. /*
  223. *********************************************************************************************************
  224. * FUNCTION PROTOTYPES
  225. *********************************************************************************************************
  226. */
  227. void Math_Init (void);
  228. /* ------------------ RAND NBR FNCTS ------------------ */
  229. void Math_RandSetSeed(RAND_NBR seed);
  230. RAND_NBR Math_Rand (void);
  231. RAND_NBR Math_RandSeed (RAND_NBR seed);
  232. /*
  233. *********************************************************************************************************
  234. * CONFIGURATION ERRORS
  235. *********************************************************************************************************
  236. */
  237. /*
  238. *********************************************************************************************************
  239. * MODULE END
  240. *
  241. * Note(s) : (1) See 'lib_math.h MODULE'.
  242. *********************************************************************************************************
  243. */
  244. #endif /* End of lib math module include. */