NSMBW-Decomp
A decompilation of New Super Mario Bros. Wii
Loading...
Searching...
No Matches
c_random.cpp
1#include <types.h>
2#include <game/cLib/c_random.hpp>
3
4u32 cM_rand_c::mConst1 = 0x19660D;
5u32 cM_rand_c::mConst2 = 0x3C6EF35F;
6
7// This decompiled terribly but at least it matches
8// Original algorithm: https://s3.amazonaws.com/nrbook.com/book_C210_pdf/chap07c.pdf (page 284-285)
9inline u32 cM_rand_c::ranqdStep() {
10
11 // Variables must be declared in this order or there will be regswaps
12 register u32 c, b, a, x;
13 x = mSeed;
14 a = mConst1;
15 b = mConst2;
16
17 // Longlong math needs to be done in asm in order to match...
18 asm {
19 mulhwu c, x, a
20 mullw x, x, a
21 addc x, x, b
22 adde x, x, c
23 }
24
25 return x;
26}
27
28u32 cM_rand_c::ranqd1(u32 max) {
29 mSeed = ranqdStep();
30 return ((u64) mSeed * max) >> 32;
31}
32
34 mSeed = ranqdStep();
35 u32 tmp = 0x3f800000 | (mSeed >> 9 & 0x7fffff);
36 return (*(float *)&tmp)-1.0f;
37}
38
40 mSeed = ranqdStep();
41 u32 tmp = 0x3f800000 | (mSeed >> 9 & 0x7fffff);
42 return (*(float *)&tmp)-1.5f;
43}
u32 ranqdStep()
Implementation of the ranqd1 algorithm.
Definition c_random.cpp:9
u32 mSeed
The current seed.
Definition c_random.hpp:17
float ranqd3()
Generates a floating point number between -0.5 and 0.5.
Definition c_random.cpp:39
u32 ranqd1(u32 max)
Generates an integer between 0 and the given max.
Definition c_random.cpp:28
static u32 mConst1
Constant a from the ranqd1 algorithm.
Definition c_random.hpp:25
static u32 mConst2
Constant c from the ranqd1 algorithm.
Definition c_random.hpp:26
float ranqd2()
Generates a floating point number between 0 and 1.
Definition c_random.cpp:33