UCOS_TI_LM3S_Keil
首页
相关页面
结构体
文件
文件列表
全局定义
全部
结构体
文件
函数
变量
类型定义
宏定义
页
lib_math.c
浏览该文件的文档.
1
/*
2
*********************************************************************************************************
3
* uC/LIB
4
* CUSTOM LIBRARY MODULES
5
*
6
* (c) Copyright 2004-2009; Micrium, Inc.; Weston, FL
7
*
8
* All rights reserved. Protected by international copyright laws.
9
*
10
* uC/LIB is provided in source form for FREE evaluation, for educational
11
* use or peaceful research. If you plan on using uC/LIB in a commercial
12
* product you need to contact Micrium to properly license its use in your
13
* product. We provide ALL the source code for your convenience and to
14
* help you experience uC/LIB. The fact that the source code is provided
15
* does NOT mean that you can use it without paying a licensing fee.
16
*
17
* Knowledge of the source code may NOT be used to develop a similar product.
18
*
19
* Please help us continue to provide the Embedded community with the finest
20
* software available. Your honesty is greatly appreciated.
21
*********************************************************************************************************
22
*/
23
24
/*
25
*********************************************************************************************************
26
*
27
* MATHEMATIC OPERATIONS
28
*
29
* Filename : lib_math.c
30
* Version : V1.30
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
/*
64
*********************************************************************************************************
65
* INCLUDE FILES
66
*********************************************************************************************************
67
*/
68
69
#define LIB_MATH_MODULE
70
#include <
lib_math.h
>
71
72
73
/*$PAGE*/
74
/*
75
*********************************************************************************************************
76
* LOCAL DEFINES
77
*********************************************************************************************************
78
*/
79
80
81
/*
82
*********************************************************************************************************
83
* LOCAL CONSTANTS
84
*********************************************************************************************************
85
*/
86
87
88
/*
89
*********************************************************************************************************
90
* LOCAL DATA TYPES
91
*********************************************************************************************************
92
*/
93
94
95
/*
96
*********************************************************************************************************
97
* LOCAL TABLES
98
*********************************************************************************************************
99
*/
100
101
102
/*
103
*********************************************************************************************************
104
* LOCAL GLOBAL VARIABLES
105
*********************************************************************************************************
106
*/
107
108
RAND_NBR
Math_RandSeedCur
;
/* Cur rand nbr seed. */
109
110
111
/*
112
*********************************************************************************************************
113
* LOCAL FUNCTION PROTOTYPES
114
*********************************************************************************************************
115
*/
116
117
118
/*
119
*********************************************************************************************************
120
* LOCAL CONFIGURATION ERRORS
121
*********************************************************************************************************
122
*/
123
124
125
/*$PAGE*/
126
/*
127
*********************************************************************************************************
128
* Math_Init()
129
*
130
* Description : (1) Initialize Mathematic Module :
131
*
132
* (a) Initialize random number seed value
133
*
134
*
135
* Argument(s) : none.
136
*
137
* Return(s) : none.
138
*
139
* Caller(s) : Application.
140
*
141
* Note(s) : (2) IEEE Std 1003.1, 2004 Edition, Section 'rand() : DESCRIPTION' states that "if rand()
142
* is called before any calls to srand() are made, the same sequence shall be generated
143
* as when srand() is first called with a seed value of 1".
144
*********************************************************************************************************
145
*/
146
147
void
Math_Init
(
void
)
148
{
149
Math_RandSetSeed
((
RAND_NBR
)
RAND_SEED_INIT_VAL
);
/* See Note #2. */
150
}
151
152
153
/*$PAGE*/
154
/*
155
*********************************************************************************************************
156
* Math_RandSetSeed()
157
*
158
* Description : Set the current pseudo-random number generator seed.
159
*
160
* Argument(s) : seed Initial (or current) value to set for the pseudo-random number sequence.
161
*
162
* Return(s) : none.
163
*
164
* Caller(s) : Application.
165
*
166
* Note(s) : (1) IEEE Std 1003.1, 2004 Edition, Section 'rand() : DESCRIPTION' states that "srand()
167
* ... uses the argument as a seed for a new sequence of pseudo-random numbers to be
168
* returned by subsequent calls to rand()".
169
*
170
* (2) 'Math_RandSeedCur' MUST always be accessed exclusively in critical sections.
171
*
172
* See also 'Math_Rand() Note #1b'.
173
*********************************************************************************************************
174
*/
175
176
void
Math_RandSetSeed
(
RAND_NBR
seed)
177
{
178
CPU_SR_ALLOC
();
179
180
181
CPU_CRITICAL_ENTER
();
182
Math_RandSeedCur
= seed;
183
CPU_CRITICAL_EXIT
();
184
}
185
186
187
/*$PAGE*/
188
/*
189
*********************************************************************************************************
190
* Math_Rand()
191
*
192
* Description : Calculate the next pseudo-random number.
193
*
194
* Argument(s) : none.
195
*
196
* Return(s) : Next pseudo-random number in the sequence after 'Math_RandSeedCur'.
197
*
198
* Caller(s) : Application.
199
*
200
* Note(s) : (1) (a) The pseudo-random number generator is implemented as a Linear Congruential
201
* Generator (LCG).
202
*
203
* (b) The pseudo-random number generated is in the range [0, RAND_LCG_PARAM_M].
204
*
205
* See also 'Math_RandSeed() Note #1'.
206
*
207
* (2) (a) IEEE Std 1003.1, 2004 Edition, Section 'rand() : DESCRIPTION' states that "rand()
208
* ... need not be reentrant ... [and] is not required to be thread-safe".
209
*
210
* (b) However, in order to implement Math_Rand() as re-entrant; 'Math_RandSeedCur' MUST
211
* always be accessed & updated exclusively in critical sections.
212
*
213
* See also 'Math_RandSeed() Note #2'.
214
*********************************************************************************************************
215
*/
216
217
RAND_NBR
Math_Rand
(
void
)
218
{
219
RAND_NBR
seed;
220
RAND_NBR
rand_nbr;
221
CPU_SR_ALLOC
();
222
223
224
CPU_CRITICAL_ENTER
();
225
seed =
Math_RandSeedCur
;
226
rand_nbr =
Math_RandSeed
(seed);
227
Math_RandSeedCur
= rand_nbr;
228
CPU_CRITICAL_EXIT
();
229
230
return
(rand_nbr);
231
}
232
233
234
/*$PAGE*/
235
/*
236
*********************************************************************************************************
237
* Math_RandSeed()
238
*
239
* Description : Calculate the next pseudo-random number.
240
*
241
* Argument(s) : seed Initial (or current) value for the pseudo-random number sequence.
242
*
243
* Return(s) : Next pseudo-random number in the sequence after 'seed'.
244
*
245
* Caller(s) : Math_Rand(),
246
* Application.
247
*
248
* Note(s) : (1) (a) BSD/ANSI-C implements rand() as a Linear Congruential Generator (LCG) :
249
*
250
* (A) random_number = [(a * random_number ) + b] modulo m
251
* n + 1 n
252
*
253
* where
254
* (1) (a) random_number Next random number to generate
255
* n+1
256
* (b) random_number Previous random number generated
257
* n
258
*
259
* (2) a = RAND_LCG_PARAM_A LCG multiplier
260
* (3) b = RAND_LCG_PARAM_B LCG incrementor
261
* (4) m = RAND_LCG_PARAM_M + 1 LCG modulus
262
*
263
* (b) The pseudo-random number generated is in the range [0, RAND_LCG_PARAM_M].
264
*
265
See also 'lib_math.h RANDOM NUMBER DEFINES Note #1b'.
266
*
267
* (2) (a) IEEE Std 1003.1, 2004 Edition, Section 'rand() : DESCRIPTION' states that "rand()
268
* ... need not be reentrant ... [and] is not required to be thread-safe".
269
*
270
* (b) However, Math_RandSeed() is re-entrant since it calculates the next random number
271
* using ONLY local variables.
272
*********************************************************************************************************
273
*/
274
275
RAND_NBR
Math_RandSeed
(
RAND_NBR
seed)
276
{
277
RAND_NBR
rand_nbr;
278
279
280
rand_nbr = (((
RAND_NBR
)
RAND_LCG_PARAM_A
* seed) + (
RAND_NBR
)
RAND_LCG_PARAM_B
) % ((
RAND_NBR
)
RAND_LCG_PARAM_M
+ 1);
281
282
return
(rand_nbr);
283
}
284
UCOS_TI_LM3S_Keil
uC-LIB
lib_math.c
生成于 2013年 十月 1日 星期二 12:18:39 , 为 UCOS_TI_LM3S_Keil使用
1.8.4