-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Expand file tree
/
Copy pathRandom.Xoshiro128StarStarImpl.cs
More file actions
270 lines (234 loc) · 10.1 KB
/
Random.Xoshiro128StarStarImpl.cs
File metadata and controls
270 lines (234 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Diagnostics;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Internal.Runtime.CompilerServices;
namespace System
{
public partial class Random
{
/// <summary>
/// Provides an implementation of the xoshiro128** algorithm. This implementation is used
/// on 32-bit when no seed is specified and an instance of the base Random class is constructed.
/// As such, we are free to implement however we see fit, without back compat concerns around
/// the sequence of numbers generated or what methods call what other methods.
/// </summary>
internal sealed class XoshiroImpl : ImplBase
{
// NextUInt32 is based on the algorithm from http://prng.di.unimi.it/xoshiro128starstar.c:
//
// Written in 2018 by David Blackman and Sebastiano Vigna (vigna@acm.org)
//
// To the extent possible under law, the author has dedicated all copyright
// and related and neighboring rights to this software to the public domain
// worldwide. This software is distributed without any warranty.
//
// See <http://creativecommons.org/publicdomain/zero/1.0/>.
private uint _s0, _s1, _s2, _s3;
public unsafe XoshiroImpl()
{
uint* ptr = stackalloc uint[4];
do
{
Interop.GetRandomBytes((byte*)ptr, 4 * sizeof(uint));
_s0 = ptr[0];
_s1 = ptr[1];
_s2 = ptr[2];
_s3 = ptr[3];
}
while ((_s0 | _s1 | _s2 | _s3) == 0); // at least one value must be non-zero
}
/// <summary>Produces a value in the range [0, uint.MaxValue].</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)] // small-ish hot path used by a handful of "next" methods
internal uint NextUInt32()
{
uint s0 = _s0, s1 = _s1, s2 = _s2, s3 = _s3;
uint result = BitOperations.RotateLeft(s1 * 5, 7) * 9;
uint t = s1 << 9;
s2 ^= s0;
s3 ^= s1;
s1 ^= s2;
s0 ^= s3;
s2 ^= t;
s3 = BitOperations.RotateLeft(s3, 11);
_s0 = s0;
_s1 = s1;
_s2 = s2;
_s3 = s3;
return result;
}
/// <summary>Produces a value in the range [0, ulong.MaxValue].</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)] // small-ish hot path used by a handful of "next" methods
internal ulong NextUInt64() => (((ulong)NextUInt32()) << 32) | NextUInt32();
public override int Next()
{
while (true)
{
// Get top 31 bits to get a value in the range [0, int.MaxValue], but try again
// if the value is actually int.MaxValue, as the method is defined to return a value
// in the range [0, int.MaxValue).
uint result = NextUInt32() >> 1;
if (result != int.MaxValue)
{
return (int)result;
}
}
}
public override int Next(int maxValue)
{
if (maxValue > 1)
{
// Narrow down to the smallest range [0, 2^bits] that contains maxValue.
// Then repeatedly generate a value in that outer range until we get one within the inner range.
int bits = BitOperations.Log2Ceiling((uint)maxValue);
while (true)
{
uint result = NextUInt32() >> (sizeof(uint) * 8 - bits);
if (result < (uint)maxValue)
{
return (int)result;
}
}
}
Debug.Assert(maxValue == 0 || maxValue == 1);
return 0;
}
public override int Next(int minValue, int maxValue)
{
uint exclusiveRange = (uint)(maxValue - minValue);
if (exclusiveRange > 1)
{
// Narrow down to the smallest range [0, 2^bits] that contains maxValue.
// Then repeatedly generate a value in that outer range until we get one within the inner range.
int bits = BitOperations.Log2Ceiling(exclusiveRange);
while (true)
{
uint result = NextUInt32() >> (sizeof(uint) * 8 - bits);
if (result < exclusiveRange)
{
return (int)result + minValue;
}
}
}
Debug.Assert(minValue == maxValue || minValue + 1 == maxValue);
return minValue;
}
public override long NextInt64()
{
while (true)
{
// Get top 63 bits to get a value in the range [0, long.MaxValue], but try again
// if the value is actually long.MaxValue, as the method is defined to return a value
// in the range [0, long.MaxValue).
ulong result = NextUInt64() >> 1;
if (result != long.MaxValue)
{
return (long)result;
}
}
}
public override long NextInt64(long maxValue)
{
if (maxValue <= int.MaxValue)
{
return Next((int)maxValue);
}
if (maxValue > 1)
{
// Narrow down to the smallest range [0, 2^bits] that contains maxValue.
// Then repeatedly generate a value in that outer range until we get one within the inner range.
int bits = BitOperations.Log2Ceiling((ulong)maxValue);
while (true)
{
ulong result = NextUInt64() >> (sizeof(ulong) * 8 - bits);
if (result < (ulong)maxValue)
{
return (long)result;
}
}
}
Debug.Assert(maxValue == 0 || maxValue == 1);
return 0;
}
public override long NextInt64(long minValue, long maxValue)
{
ulong exclusiveRange = (ulong)(maxValue - minValue);
if (exclusiveRange <= int.MaxValue)
{
return Next((int)exclusiveRange) + minValue;
}
if (exclusiveRange > 1)
{
// Narrow down to the smallest range [0, 2^bits] that contains maxValue.
// Then repeatedly generate a value in that outer range until we get one within the inner range.
int bits = BitOperations.Log2Ceiling(exclusiveRange);
while (true)
{
ulong result = NextUInt64() >> (sizeof(ulong) * 8 - bits);
if (result < exclusiveRange)
{
return (long)result + minValue;
}
}
}
Debug.Assert(minValue == maxValue || minValue + 1 == maxValue);
return minValue;
}
public override void NextBytes(byte[] buffer) => NextBytes((Span<byte>)buffer);
public override unsafe void NextBytes(Span<byte> buffer)
{
uint s0 = _s0, s1 = _s1, s2 = _s2, s3 = _s3;
while (buffer.Length >= sizeof(uint))
{
Unsafe.WriteUnaligned(
ref MemoryMarshal.GetReference(buffer),
BitOperations.RotateLeft(s1 * 5, 7) * 9);
// Update PRNG state.
uint t = s1 << 9;
s2 ^= s0;
s3 ^= s1;
s1 ^= s2;
s0 ^= s3;
s2 ^= t;
s3 = BitOperations.RotateLeft(s3, 11);
buffer = buffer.Slice(sizeof(uint));
}
if (!buffer.IsEmpty)
{
uint next = BitOperations.RotateLeft(s1 * 5, 7) * 9;
byte* remainingBytes = (byte*)&next;
Debug.Assert(buffer.Length < sizeof(uint));
for (int i = 0; i < buffer.Length; i++)
{
buffer[i] = remainingBytes[i];
}
// Update PRNG state.
uint t = s1 << 9;
s2 ^= s0;
s3 ^= s1;
s1 ^= s2;
s0 ^= s3;
s2 ^= t;
s3 = BitOperations.RotateLeft(s3, 11);
}
_s0 = s0;
_s1 = s1;
_s2 = s2;
_s3 = s3;
}
public override double NextDouble() =>
// See comment in Xoshiro256StarStarImpl.
(NextUInt64() >> 11) * (1.0 / (1ul << 53));
public override float NextSingle() =>
// See comment in Xoshiro256StarStarImpl.
(NextUInt32() >> 8) * (1.0f / (1u << 24));
public override double Sample()
{
Debug.Fail("Not used or called for this implementation.");
throw new NotSupportedException();
}
}
}
}