ssxre/bxprng.cpp

91 lines
1.9 KiB
C++

#include <cstdint>
#include <cstring>
struct bxPsuedoRng {
// Constant init state
constexpr static std::uint32_t InitState[6] {
0xf22d0e56,
0x883126e9,
0xc624dd2f,
0x702c49c,
0x9e353f7d,
0x6fdf3b64
};
std::uint32_t state[6];
explicit bxPsuedoRng() {
std::memcpy(&this->state[0], &InitState, sizeof(state));
}
void Init(std::uint32_t seed) {
state[0] = seed + InitState[0];
state[1] = seed + InitState[1];
state[2] = seed + InitState[2];
state[3] = seed + InitState[3];
state[4] = seed + InitState[4];
state[5] = seed + InitState[5];
}
std::uint32_t NextInt() {
std::uint32_t uVar1;
std::uint32_t uVar2;
std::uint32_t uVar3;
std::uint32_t uVar4;
int iVar5;
uVar3 = this->state[5] + this->state[4];
iVar5 = 0;
if ((uVar3 < this->state[5]) || (uVar3 < this->state[4]))
iVar5 = 1;
uVar2 = this->state[3];
// TODO: fix it up (I think i nabbed out one cycle.)
uVar1 = this->state[2];
this->state[4] = uVar3;
uVar4 = uVar3 + uVar2 + iVar5;
uVar3 = this->state[1];
this->state[3] = uVar4;
uVar4 = uVar4 + uVar1 + (std::uint32_t)(uVar4 < uVar2);
uVar2 = this->state[0];
this->state[2] = uVar4;
uVar4 = uVar4 + uVar3 + (std::uint32_t)(uVar4 < uVar1);
this->state[1] = uVar4;
uVar1 = this->state[5] + 1;
uVar3 = uVar4 + uVar2 + (std::uint32_t)(uVar4 < uVar3);
this->state[5] = uVar1;
if ((((uVar1 == 0) && (uVar2 = this->state[4] + 1, this->state[4] = uVar2, uVar2 == 0)) &&
(uVar2 = this->state[3] + 1, this->state[3] = uVar2, uVar2 == 0)) &&
((uVar2 = this->state[2] + 1, this->state[2] = uVar2, uVar2 == 0 &&
(uVar2 = this->state[1] + 1, this->state[1] = uVar2, uVar2 == 0)))) {
uVar3 = uVar3 + 1;
}
this->state[0] = uVar3;
return this->state[0];
}
};
#include <cstdio>
int main() {
bxPsuedoRng rng;
for(int i = 0; i < 1000; ++i) {
auto next = rng.NextInt();
std::printf("cycle %d: 0x%08x (decimal: %d / %u)\n", i + 1, next, next, next);
}
}