lib_math.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*
  2. *********************************************************************************************************
  3. * uC/LIB
  4. * CUSTOM LIBRARY MODULES
  5. *
  6. * (c) Copyright 2004-2011; 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 contact us at www.micrium.com.
  19. *********************************************************************************************************
  20. */
  21. /*
  22. *********************************************************************************************************
  23. *
  24. * MATHEMATIC OPERATIONS
  25. *
  26. * Filename : lib_math.c
  27. * Version : V1.35.00
  28. * Programmer(s) : SR
  29. * ITJ
  30. *********************************************************************************************************
  31. * Note(s) : (1) NO compiler-supplied standard library functions are used in library or product software.
  32. *
  33. * (a) ALL standard library functions are implemented in the custom library modules :
  34. *
  35. * (1) \<Custom Library Directory>\lib_*.*
  36. *
  37. * (2) \<Custom Library Directory>\Ports\<cpu>\<compiler>\lib*_a.*
  38. *
  39. * where
  40. * <Custom Library Directory> directory path for custom library software
  41. * <cpu> directory name for specific processor (CPU)
  42. * <compiler> directory name for specific compiler
  43. *
  44. * (b) Product-specific library functions are implemented in individual products.
  45. *
  46. *********************************************************************************************************
  47. * Notice(s) : (1) The Institute of Electrical and Electronics Engineers and The Open Group, have given
  48. * us permission to reprint portions of their documentation. Portions of this text are
  49. * reprinted and reproduced in electronic form from the IEEE Std 1003.1, 2004 Edition,
  50. * Standard for Information Technology -- Portable Operating System Interface (POSIX),
  51. * The Open Group Base Specifications Issue 6, Copyright (C) 2001-2004 by the Institute
  52. * of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any
  53. * discrepancy between these versions and the original IEEE and The Open Group Standard,
  54. * the original IEEE and The Open Group Standard is the referee document. The original
  55. * Standard can be obtained online at http://www.opengroup.org/unix/online.html.
  56. *********************************************************************************************************
  57. */
  58. /*
  59. *********************************************************************************************************
  60. * INCLUDE FILES
  61. *********************************************************************************************************
  62. */
  63. #define LIB_MATH_MODULE
  64. #include <lib_math.h>
  65. /*$PAGE*/
  66. /*
  67. *********************************************************************************************************
  68. * LOCAL DEFINES
  69. *********************************************************************************************************
  70. */
  71. /*
  72. *********************************************************************************************************
  73. * LOCAL CONSTANTS
  74. *********************************************************************************************************
  75. */
  76. /*
  77. *********************************************************************************************************
  78. * LOCAL DATA TYPES
  79. *********************************************************************************************************
  80. */
  81. /*
  82. *********************************************************************************************************
  83. * LOCAL TABLES
  84. *********************************************************************************************************
  85. */
  86. /*
  87. *********************************************************************************************************
  88. * LOCAL GLOBAL VARIABLES
  89. *********************************************************************************************************
  90. */
  91. RAND_NBR Math_RandSeedCur; /* Cur rand nbr seed. */
  92. /*
  93. *********************************************************************************************************
  94. * LOCAL FUNCTION PROTOTYPES
  95. *********************************************************************************************************
  96. */
  97. /*
  98. *********************************************************************************************************
  99. * LOCAL CONFIGURATION ERRORS
  100. *********************************************************************************************************
  101. */
  102. /*$PAGE*/
  103. /*
  104. *********************************************************************************************************
  105. * Math_Init()
  106. *
  107. * Description : (1) Initialize Mathematic Module :
  108. *
  109. * (a) Initialize random number seed value
  110. *
  111. *
  112. * Argument(s) : none.
  113. *
  114. * Return(s) : none.
  115. *
  116. * Caller(s) : Application.
  117. *
  118. * Note(s) : (2) IEEE Std 1003.1, 2004 Edition, Section 'rand() : DESCRIPTION' states that "if rand()
  119. * is called before any calls to srand() are made, the same sequence shall be generated
  120. * as when srand() is first called with a seed value of 1".
  121. *********************************************************************************************************
  122. */
  123. void Math_Init (void)
  124. {
  125. Math_RandSetSeed((RAND_NBR)RAND_SEED_INIT_VAL); /* See Note #2. */
  126. }
  127. /*$PAGE*/
  128. /*
  129. *********************************************************************************************************
  130. * Math_RandSetSeed()
  131. *
  132. * Description : Set the current pseudo-random number generator seed.
  133. *
  134. * Argument(s) : seed Initial (or current) value to set for the pseudo-random number sequence.
  135. *
  136. * Return(s) : none.
  137. *
  138. * Caller(s) : Application.
  139. *
  140. * Note(s) : (1) IEEE Std 1003.1, 2004 Edition, Section 'rand() : DESCRIPTION' states that "srand()
  141. * ... uses the argument as a seed for a new sequence of pseudo-random numbers to be
  142. * returned by subsequent calls to rand()".
  143. *
  144. * (2) 'Math_RandSeedCur' MUST always be accessed exclusively in critical sections.
  145. *
  146. * See also 'Math_Rand() Note #1b'.
  147. *********************************************************************************************************
  148. */
  149. void Math_RandSetSeed (RAND_NBR seed)
  150. {
  151. CPU_SR_ALLOC();
  152. CPU_CRITICAL_ENTER();
  153. Math_RandSeedCur = seed;
  154. CPU_CRITICAL_EXIT();
  155. }
  156. /*$PAGE*/
  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. /*$PAGE*/
  198. /*
  199. *********************************************************************************************************
  200. * Math_RandSeed()
  201. *
  202. * Description : Calculate the next pseudo-random number.
  203. *
  204. * Argument(s) : seed Initial (or current) value for the pseudo-random number sequence.
  205. *
  206. * Return(s) : Next pseudo-random number in the sequence after 'seed'.
  207. *
  208. * Caller(s) : Math_Rand(),
  209. * Application.
  210. *
  211. * Note(s) : (1) (a) BSD/ANSI-C implements rand() as a Linear Congruential Generator (LCG) :
  212. *
  213. * (A) random_number = [(a * random_number ) + b] modulo m
  214. * n + 1 n
  215. *
  216. * where
  217. * (1) (a) random_number Next random number to generate
  218. * n+1
  219. * (b) random_number Previous random number generated
  220. * n
  221. *
  222. * (2) a = RAND_LCG_PARAM_A LCG multiplier
  223. * (3) b = RAND_LCG_PARAM_B LCG incrementor
  224. * (4) m = RAND_LCG_PARAM_M + 1 LCG modulus
  225. *
  226. * (b) The pseudo-random number generated is in the range [0, RAND_LCG_PARAM_M].
  227. *
  228. See also 'lib_math.h RANDOM NUMBER DEFINES Note #1b'.
  229. *
  230. * (2) (a) IEEE Std 1003.1, 2004 Edition, Section 'rand() : DESCRIPTION' states that "rand()
  231. * ... need not be reentrant ... [and] is not required to be thread-safe".
  232. *
  233. * (b) However, Math_RandSeed() is re-entrant since it calculates the next random number
  234. * using ONLY local variables.
  235. *********************************************************************************************************
  236. */
  237. RAND_NBR Math_RandSeed (RAND_NBR seed)
  238. {
  239. RAND_NBR rand_nbr;
  240. rand_nbr = (((RAND_NBR)RAND_LCG_PARAM_A * seed) + (RAND_NBR)RAND_LCG_PARAM_B) % ((RAND_NBR)RAND_LCG_PARAM_M + 1u);
  241. return (rand_nbr);
  242. }