Calculates next 32-bits state for a Marsaglia’s XOR-Shift pseudo-random 8-bits generator, with the tuple (1,1,3) .
u32 cpct_nextRandom_mxor_u8 (u32 seed) __z88dk_fastcall;
(4B DE:HL) *state* | Previous state that the XOR-Shift algorithm will use to calculate its follower in the sequence. |
call cpct_nextRandom_mxor_u8_asm
<u32> | Next internal state of the pseudo-random generator. The 8 Least Significant bits of this state are the 8 pseudo-random bits generated by this call. |
This function implements a sequence of 32-bits states with period (2^32)-1. For each produced state, a random sequence of 8-bits is returned. This means that it produces consecutive 8-bits values that do not repeat until 4.294.967.295 numbers have been generated. To do this, the function receives a 32-bit state as parameter (that should be different from 0) and returns the next 32-bit value in the sequence. In each returned state, the 8 Least Significant bits are the 8 pseudo-random bits produced.
The sequence calculated by this function is based on a modified version of Marsaglia’s XOR-shift generator using the tuple (3, 1, 1) for 8-bit values. This tuple is implemented really fast on a Z80 (Patrik Rak originally showed a similar implementation for tuple (1, 1, 3)). Assuming that the 32-bits state s is composed of 4 8-bits numbers s=(x, z, y, w), this algorithm produces a new state s’=(x’,z’,y’,w’) doing these operations:
x' = y; y' = z; z' = w; t = x ^ (x << 3); t = t ^ (t >> 1); w' = w ^ (w << 1) ^ t;
where <</>> are left/right bit shifting operations, and ^ is the exclusive OR (XOR) bitwise operation.
AF, BC, DE, HL
17 bytes
Case | microSecs (us) | CPU Cycles ----------------------------------------- Any | 19 | 76 -----------------------------------------