|
1 | 1 | #ifndef included_rand |
2 | 2 | #define included_rand |
| 3 | + |
3 | 4 | rand: |
4 | | -;Tested and passes all CAcert tests |
5 | | -;Uses a very simple 32-bit LCG and 32-bit LFSR |
6 | | -;it has a period of 18,446,744,069,414,584,320 |
7 | | -;roughly 18.4 quintillion. |
8 | | -;LFSR taps: 0,2,6,7 = 11000101 |
9 | | -;323cc |
10 | | -;Thanks to Runer112 for his help on optimizing the LCG and suggesting to try the much simpler LCG. On their own, the two are terrible, but together they are great. |
11 | | -;Uses 64 bits of state |
| 5 | +; This rand routine combines Patrik Rak's fantastic 32-bit xorshift |
| 6 | +; (https://gist.github.com/raxoft/c074743ea3f926db0037) with a simple lcg for |
| 7 | +; extra smoothing. |
| 8 | +; It has a period of 281,474,976,645,120 and uses 48 bits of state |
| 9 | +; 42 bytes |
| 10 | +; 210cc |
12 | 11 | ld hl,(seed0) |
13 | | - ld de,(seed0+2) |
14 | 12 | ld b,h |
15 | 13 | ld c,l |
16 | | - add hl,hl \ rl e \ rl d |
17 | | - add hl,hl \ rl e \ rl d |
| 14 | + add hl,hl |
| 15 | + add hl,hl |
18 | 16 | inc l |
19 | 17 | add hl,bc |
20 | 18 | ld (seed0),hl |
21 | | - ld hl,(seed0+2) |
22 | | - adc hl,de |
23 | | - ld (seed0+2),hl |
24 | | - ex de,hl |
25 | | -;lfsr |
26 | | - ld hl,(seed1) |
27 | | - ld bc,(seed1+2) |
28 | | - add hl,hl \ rl c \ rl b |
29 | | - ld (seed1+2),bc |
30 | | - sbc a,a |
31 | | - and %11000101 |
| 19 | + |
| 20 | +; xorshift |
| 21 | + ld hl,(seed1) ; yw -> zt |
| 22 | + ld de,(seed1+2) ; xz -> yw |
| 23 | + ld (seed1+2),hl ; x = y, z = w |
| 24 | + ld a,l ; w = w ^ ( w << 3 ) |
| 25 | + add a,a |
| 26 | + add a,a |
| 27 | + add a,a |
32 | 28 | xor l |
33 | 29 | ld l,a |
| 30 | + ld a,d ; t = x ^ (x << 1) |
| 31 | + add a,a |
| 32 | + xor d |
| 33 | + ld h,a |
| 34 | + rra ; t = t ^ (t >> 1) ^ w |
| 35 | + xor h |
| 36 | + xor l |
| 37 | + ld h,e ; y = z |
| 38 | + ld l,a ; w = t |
34 | 39 | ld (seed1),hl |
35 | | - ex de,hl |
| 40 | + |
| 41 | +; Mix the xorshift and the lcg |
36 | 42 | add hl,bc |
37 | 43 | ret |
38 | 44 | #endif |
0 commit comments