lib_math.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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.c
  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. * INCLUDE FILES
  64. *********************************************************************************************************
  65. */
  66. #define MICRIUM_SOURCE
  67. #define LIB_MATH_MODULE
  68. #include <lib_math.h>
  69. /*
  70. *********************************************************************************************************
  71. * LOCAL DEFINES
  72. *********************************************************************************************************
  73. */
  74. /*
  75. *********************************************************************************************************
  76. * LOCAL CONSTANTS
  77. *********************************************************************************************************
  78. */
  79. /*
  80. *********************************************************************************************************
  81. * LOCAL DATA TYPES
  82. *********************************************************************************************************
  83. */
  84. /*
  85. *********************************************************************************************************
  86. * LOCAL TABLES
  87. *********************************************************************************************************
  88. */
  89. /*
  90. *********************************************************************************************************
  91. * LOCAL GLOBAL VARIABLES
  92. *********************************************************************************************************
  93. */
  94. RAND_NBR Math_RandSeedCur; /* Cur rand nbr seed. */
  95. /*
  96. *********************************************************************************************************
  97. * LOCAL FUNCTION PROTOTYPES
  98. *********************************************************************************************************
  99. */
  100. /*
  101. *********************************************************************************************************
  102. * LOCAL CONFIGURATION ERRORS
  103. *********************************************************************************************************
  104. */
  105. /*
  106. *********************************************************************************************************
  107. * Math_Init()
  108. *
  109. * Description : (1) Initialize Mathematic Module :
  110. *
  111. * (a) Initialize random number seed value
  112. *
  113. *
  114. * Argument(s) : none.
  115. *
  116. * Return(s) : none.
  117. *
  118. * Caller(s) : Application.
  119. *
  120. * Note(s) : (2) IEEE Std 1003.1, 2004 Edition, Section 'rand() : DESCRIPTION' states that "if rand()
  121. * is called before any calls to srand() are made, the same sequence shall be generated
  122. * as when srand() is first called with a seed value of 1".
  123. *********************************************************************************************************
  124. */
  125. void Math_Init (void)
  126. {
  127. Math_RandSetSeed((RAND_NBR)RAND_SEED_INIT_VAL); /* See Note #2. */
  128. }
  129. /*
  130. *********************************************************************************************************
  131. * Math_RandSetSeed()
  132. *
  133. * Description : Set the current pseudo-random number generator seed.
  134. *
  135. * Argument(s) : seed Initial (or current) value to set for the pseudo-random number sequence.
  136. *
  137. * Return(s) : none.
  138. *
  139. * Caller(s) : Application.
  140. *
  141. * Note(s) : (1) IEEE Std 1003.1, 2004 Edition, Section 'rand() : DESCRIPTION' states that "srand()
  142. * ... uses the argument as a seed for a new sequence of pseudo-random numbers to be
  143. * returned by subsequent calls to rand()".
  144. *
  145. * (2) 'Math_RandSeedCur' MUST always be accessed exclusively in critical sections.
  146. *
  147. * See also 'Math_Rand() Note #1b'.
  148. *********************************************************************************************************
  149. */
  150. void Math_RandSetSeed (RAND_NBR seed)
  151. {
  152. CPU_SR_ALLOC();
  153. CPU_CRITICAL_ENTER();
  154. Math_RandSeedCur = seed;
  155. CPU_CRITICAL_EXIT();
  156. }
  157. /*
  158. *********************************************************************************************************
  159. * Math_Rand()
  160. *
  161. * Description : Calculate the next pseudo-random number.
  162. *
  163. * Argument(s) : none.
  164. *
  165. * Return(s) : Next pseudo-random number in the sequence after 'Math_RandSeedCur'.
  166. *
  167. * Caller(s) : Application.
  168. *
  169. * Note(s) : (1) (a) The pseudo-random number generator is implemented as a Linear Congruential
  170. * Generator (LCG).
  171. *
  172. * (b) The pseudo-random number generated is in the range [0, RAND_LCG_PARAM_M].
  173. *
  174. * See also 'Math_RandSeed() Note #1'.
  175. *
  176. * (2) (a) IEEE Std 1003.1, 2004 Edition, Section 'rand() : DESCRIPTION' states that "rand()
  177. * ... need not be reentrant ... [and] is not required to be thread-safe".
  178. *
  179. * (b) However, in order to implement Math_Rand() as re-entrant; 'Math_RandSeedCur' MUST
  180. * always be accessed & updated exclusively in critical sections.
  181. *
  182. * See also 'Math_RandSeed() Note #2'.
  183. *********************************************************************************************************
  184. */
  185. RAND_NBR Math_Rand (void)
  186. {
  187. RAND_NBR seed;
  188. RAND_NBR rand_nbr;
  189. CPU_SR_ALLOC();
  190. CPU_CRITICAL_ENTER();
  191. seed = Math_RandSeedCur;
  192. rand_nbr = Math_RandSeed(seed);
  193. Math_RandSeedCur = rand_nbr;
  194. CPU_CRITICAL_EXIT();
  195. return (rand_nbr);
  196. }
  197. /*
  198. *********************************************************************************************************
  199. * Math_RandSeed()
  200. *
  201. * Description : Calculate the next pseudo-random number.
  202. *
  203. * Argument(s) : seed Initial (or current) value for the pseudo-random number sequence.
  204. *
  205. * Return(s) : Next pseudo-random number in the sequence after 'seed'.
  206. *
  207. * Caller(s) : Math_Rand(),
  208. * Application.
  209. *
  210. * Note(s) : (1) (a) BSD/ANSI-C implements rand() as a Linear Congruential Generator (LCG) :
  211. *
  212. * (A) random_number = [(a * random_number ) + b] modulo m
  213. * n + 1 n
  214. *
  215. * where
  216. * (1) (a) random_number Next random number to generate
  217. * n+1
  218. * (b) random_number Previous random number generated
  219. * n
  220. *
  221. * (2) a = RAND_LCG_PARAM_A LCG multiplier
  222. * (3) b = RAND_LCG_PARAM_B LCG incrementor
  223. * (4) m = RAND_LCG_PARAM_M + 1 LCG modulus
  224. *
  225. * (b) The pseudo-random number generated is in the range [0, RAND_LCG_PARAM_M].
  226. *
  227. See also 'lib_math.h RANDOM NUMBER DEFINES Note #1b'.
  228. *
  229. * (2) (a) IEEE Std 1003.1, 2004 Edition, Section 'rand() : DESCRIPTION' states that "rand()
  230. * ... need not be reentrant ... [and] is not required to be thread-safe".
  231. *
  232. * (b) However, Math_RandSeed() is re-entrant since it calculates the next random number
  233. * using ONLY local variables.
  234. *********************************************************************************************************
  235. */
  236. RAND_NBR Math_RandSeed (RAND_NBR seed)
  237. {
  238. RAND_NBR rand_nbr;
  239. rand_nbr = (((RAND_NBR)RAND_LCG_PARAM_A * seed) + (RAND_NBR)RAND_LCG_PARAM_B) % ((RAND_NBR)RAND_LCG_PARAM_M + 1u);
  240. return (rand_nbr);
  241. }