lib_math.lst
来自「stm32+ucos-ii」· LST 代码 · 共 424 行 · 第 1/2 页
LST
424 行
158 * Argument(s) : seed Initial (or current) value to set for the pseudo-random number sequence.
159 *
160 * Return(s) : none.
161 *
162 * Caller(s) : Application.
163 *
164 * Note(s) : (1) IEEE Std 1003.1, 2004 Edition, Section 'rand() : DESCRIPTION' states that "srand()
165 * ... uses the argument as a seed for a new sequence of pseudo-random numbers to be
166 * returned by subsequent calls to rand()".
167 *
168 * (2) 'Math_RandSeedCur' MUST always be accessed exclusively in critical sections.
169 *
170 * See also 'Math_Rand() Note #1b'.
171 *********************************************************************************************************
172 */
173
\ In section .text, align 2, keep-with-next
174 void Math_RandSetSeed (RAND_NBR seed)
175 {
\ Math_RandSetSeed:
\ 00000000 38B5 PUSH {R3-R5,LR}
\ 00000002 0400 MOVS R4,R0
176 CPU_SR_ALLOC();
\ 00000004 0025 MOVS R5,#+0
177
178
179 CPU_CRITICAL_ENTER();
\ 00000006 ........ BL CPU_SR_Save
\ 0000000A 0500 MOVS R5,R0
180 Math_RandSeedCur = seed;
\ 0000000C .... LDR.N R0,??DataTable2
\ 0000000E 0460 STR R4,[R0, #+0]
181 CPU_CRITICAL_EXIT();
\ 00000010 2800 MOVS R0,R5
\ 00000012 ........ BL CPU_SR_Restore
182 }
\ 00000016 31BD POP {R0,R4,R5,PC} ;; return
183
184
185 /*$PAGE*/
186 /*
187 *********************************************************************************************************
188 * Math_Rand()
189 *
190 * Description : Calculate the next pseudo-random number.
191 *
192 * Argument(s) : none.
193 *
194 * Return(s) : Next pseudo-random number in the sequence after 'Math_RandSeedCur'.
195 *
196 * Caller(s) : Application.
197 *
198 * Note(s) : (1) (a) The pseudo-random number generator is implemented as a Linear Congruential
199 * Generator (LCG).
200 *
201 * (b) The pseudo-random number generated is in the range [0, RAND_LCG_PARAM_M].
202 *
203 * See also 'Math_RandSeed() Note #1'.
204 *
205 * (2) (a) IEEE Std 1003.1, 2004 Edition, Section 'rand() : DESCRIPTION' states that "rand()
206 * ... need not be reentrant ... [and] is not required to be thread-safe".
207 *
208 * (b) However, in order to implement Math_Rand() as re-entrant; 'Math_RandSeedCur' MUST
209 * always be accessed & updated exclusively in critical sections.
210 *
211 * See also 'Math_RandSeed() Note #2'.
212 *********************************************************************************************************
213 */
214
\ In section .text, align 2, keep-with-next
215 RAND_NBR Math_Rand (void)
216 {
\ Math_Rand:
\ 00000000 70B5 PUSH {R4-R6,LR}
217 RAND_NBR seed;
218 RAND_NBR rand_nbr;
219 CPU_SR_ALLOC();
\ 00000002 0026 MOVS R6,#+0
220
221
222 CPU_CRITICAL_ENTER();
\ 00000004 ........ BL CPU_SR_Save
\ 00000008 0600 MOVS R6,R0
223 seed = Math_RandSeedCur;
\ 0000000A .... LDR.N R0,??DataTable2
\ 0000000C 0068 LDR R0,[R0, #+0]
\ 0000000E 0400 MOVS R4,R0
224 rand_nbr = Math_RandSeed(seed);
\ 00000010 2000 MOVS R0,R4
\ 00000012 ........ BL Math_RandSeed
\ 00000016 0500 MOVS R5,R0
225 Math_RandSeedCur = rand_nbr;
\ 00000018 .... LDR.N R0,??DataTable2
\ 0000001A 0560 STR R5,[R0, #+0]
226 CPU_CRITICAL_EXIT();
\ 0000001C 3000 MOVS R0,R6
\ 0000001E ........ BL CPU_SR_Restore
227
228 return (rand_nbr);
\ 00000022 2800 MOVS R0,R5
\ 00000024 70BD POP {R4-R6,PC} ;; return
229 }
230
231
232 /*$PAGE*/
233 /*
234 *********************************************************************************************************
235 * Math_RandSeed()
236 *
237 * Description : Calculate the next pseudo-random number.
238 *
239 * Argument(s) : seed Initial (or current) value for the pseudo-random number sequence.
240 *
241 * Return(s) : Next pseudo-random number in the sequence after 'seed'.
242 *
243 * Caller(s) : Math_Rand(),
244 * Application.
245 *
246 * Note(s) : (1) (a) BSD/ANSI-C implements rand() as a Linear Congruential Generator (LCG) :
247 *
248 * (A) random_number = [(a * random_number ) + b] modulo m
249 * n + 1 n
250 *
251 * where
252 * (1) (a) random_number Next random number to generate
253 * n+1
254 * (b) random_number Previous random number generated
255 * n
256 *
257 * (2) a = RAND_LCG_PARAM_A LCG multiplier
258 * (3) b = RAND_LCG_PARAM_B LCG incrementor
259 * (4) m = RAND_LCG_PARAM_M + 1 LCG modulus
260 *
261 * (b) The pseudo-random number generated is in the range [0, RAND_LCG_PARAM_M].
262 *
263 See also 'lib_math.h RANDOM NUMBER DEFINES Note #1b'.
264 *
265 * (2) (a) IEEE Std 1003.1, 2004 Edition, Section 'rand() : DESCRIPTION' states that "rand()
266 * ... need not be reentrant ... [and] is not required to be thread-safe".
267 *
268 * (b) However, Math_RandSeed() is re-entrant since it calculates the next random number
269 * using ONLY local variables.
270 *********************************************************************************************************
271 */
272
\ In section .text, align 2, keep-with-next
273 RAND_NBR Math_RandSeed (RAND_NBR seed)
274 {
\ Math_RandSeed:
\ 00000000 10B4 PUSH {R4}
\ 00000002 0100 MOVS R1,R0
275 RAND_NBR rand_nbr;
276
277
278 rand_nbr = (((RAND_NBR)RAND_LCG_PARAM_A * seed) + (RAND_NBR)RAND_LCG_PARAM_B) % ((RAND_NBR)RAND_LCG_PARAM_M + 1u);
\ 00000004 .... LDR.N R2,??DataTable2_1 ;; 0x41c64e6d
\ 00000006 43F23903 MOVW R3,#+12345
\ 0000000A 02FB0132 MLA R2,R2,R1,R3
\ 0000000E 5FF00043 MOVS R3,#-2147483648
\ 00000012 B2FBF3F4 UDIV R4,R2,R3
\ 00000016 04FB1324 MLS R4,R4,R3,R2
\ 0000001A 2000 MOVS R0,R4
279
280 return (rand_nbr);
\ 0000001C 10BC POP {R4}
\ 0000001E 7047 BX LR ;; return
281 }
\ In section .text, align 4, keep-with-next
\ ??DataTable2:
\ 00000000 ........ DC32 Math_RandSeedCur
\ In section .text, align 4, keep-with-next
\ ??DataTable2_1:
\ 00000000 6D4EC641 DC32 0x41c64e6d
282
Maximum stack usage in bytes:
Function .cstack
-------- -------
Math_Init 8
Math_Rand 16
Math_RandSeed 4
Math_RandSetSeed 16
Section sizes:
Function/Label Bytes
-------------- -----
Math_RandSeedCur 4
Math_Init 10
Math_RandSetSeed 24
Math_Rand 38
Math_RandSeed 32
??DataTable2 4
??DataTable2_1 4
4 bytes in section .bss
112 bytes in section .text
112 bytes of CODE memory
4 bytes of DATA memory
Errors: none
Warnings: none
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?