lib_math.h 13 KB

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