1//! Xoshiro256++ - http://xoroshiro.di.unimi.it/
2//!
3//! PRNG
4
5const std = @import("std");
6const math = std.math;
7const Xoshiro256 = @This();
8
9s: [4]u64,
10
11pub fn init(init_s: u64) Xoshiro256 {
12 var x = Xoshiro256{
13 .s = undefined,
14 };
15
16 x.seed(init_s);
17 return x;
18}
19
20pub fn random(self: *Xoshiro256) std.Random {
21 return std.Random.init(self, fill);
22}
23
24pub fn next(self: *Xoshiro256) u64 {
25 const r = math.rotl(u64, self.s[0] +% self.s[3], 23) +% self.s[0];
26
27 const t = self.s[1] << 17;
28
29 self.s[2] ^= self.s[0];
30 self.s[3] ^= self.s[1];
31 self.s[1] ^= self.s[2];
32 self.s[0] ^= self.s[3];
33
34 self.s[2] ^= t;
35
36 self.s[3] = math.rotl(u64, self.s[3], 45);
37
38 return r;
39}
40
41// Skip 2^128 places ahead in the sequence
42pub fn jump(self: *Xoshiro256) void {
43 var s: u256 = 0;
44
45 var table: u256 = 0x39abdc4529b1661ca9582618e03fc9aad5a61266f0c9392c180ec6d33cfd0aba;
46
47 while (table != 0) : (table >>= 1) {
48 if (@as(u1, @truncate(table)) != 0) {
49 s ^= @bitCast(self.s);
50 }
51 _ = self.next();
52 }
53
54 self.s = @bitCast(s);
55}
56
57pub fn seed(self: *Xoshiro256, init_s: u64) void {
58 // Xoshiro requires 256-bits of seed.
59 var gen = std.Random.SplitMix64.init(init_s);
60
61 self.s[0] = gen.next();
62 self.s[1] = gen.next();
63 self.s[2] = gen.next();
64 self.s[3] = gen.next();
65}
66
67pub fn fill(self: *Xoshiro256, buf: []u8) void {
68 var i: usize = 0;
69 const aligned_len = buf.len - (buf.len & 7);
70
71 // Complete 8 byte segments.
72 while (i < aligned_len) : (i += 8) {
73 var n = self.next();
74 comptime var j: usize = 0;
75 inline while (j < 8) : (j += 1) {
76 buf[i + j] = @as(u8, @truncate(n));
77 n >>= 8;
78 }
79 }
80
81 // Remaining. (cuts the stream)
82 if (i != buf.len) {
83 var n = self.next();
84 while (i < buf.len) : (i += 1) {
85 buf[i] = @as(u8, @truncate(n));
86 n >>= 8;
87 }
88 }
89}
90
91test "sequence" {
92 var r = Xoshiro256.init(0);
93
94 const seq1 = [_]u64{
95 0x53175d61490b23df,
96 0x61da6f3dc380d507,
97 0x5c0fdf91ec9a7bfc,
98 0x02eebf8c3bbe5e1a,
99 0x7eca04ebaf4a5eea,
100 0x0543c37757f08d9a,
101 };
102
103 for (seq1) |s| {
104 try std.testing.expect(s == r.next());
105 }
106
107 r.jump();
108
109 const seq2 = [_]u64{
110 0xae1db5c5e27807be,
111 0xb584c6a7fd8709fe,
112 0xc46a0ee9330fb6e,
113 0xdc0c9606f49ed76e,
114 0x1f5bb6540f6651fb,
115 0x72fa2ca734601488,
116 };
117
118 for (seq2) |s| {
119 try std.testing.expect(s == r.next());
120 }
121}
122
123test fill {
124 var r = Xoshiro256.init(0);
125
126 const seq = [_]u64{
127 0x53175d61490b23df,
128 0x61da6f3dc380d507,
129 0x5c0fdf91ec9a7bfc,
130 0x02eebf8c3bbe5e1a,
131 0x7eca04ebaf4a5eea,
132 0x0543c37757f08d9a,
133 };
134
135 for (seq) |s| {
136 var buf0: [8]u8 = undefined;
137 var buf1: [7]u8 = undefined;
138 std.mem.writeInt(u64, &buf0, s, .little);
139 r.fill(&buf1);
140 try std.testing.expect(std.mem.eql(u8, buf0[0..7], buf1[0..]));
141 }
142}