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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
extern crate rand;
extern crate extprim;
use rand::{Rng, Rand, SeedableRng};
use std::num::Wrapping;
use extprim::u128::u128;
pub mod stream;
pub mod multiplier;
pub mod outputmix;
pub mod numops;
pub mod extension;
use stream::{Stream, OneSeqStream, NoSeqStream, SpecificSeqStream, UniqueSeqStream};
use multiplier::{Multiplier, DefaultMultiplier, McgMultiplier};
use outputmix::{OutputMixin, XshRsMixin, XshRrMixin};
use numops::*;
use std::marker::PhantomData;
pub struct PcgEngine<Itype, Xtype,
StreamMix : Stream<Itype>,
MulMix : Multiplier<Itype>,
OutMix : OutputMixin<Itype, Xtype>>
{
state : Itype,
stream_mix : StreamMix,
mul_mix : PhantomData<MulMix>,
out_mix : PhantomData<OutMix>,
phantom : PhantomData<Xtype>
}
impl<Itype, Xtype, StreamMix, MulMix, OutMix> PcgEngine<Itype, Xtype, StreamMix, MulMix, OutMix>
where Itype: PcgOps + BitSize + AsSmaller<Xtype> + Clone, Xtype: PcgOps + BitSize,
StreamMix: Stream<Itype>, MulMix: Multiplier<Itype>, OutMix: OutputMixin<Itype, Xtype> {
pub fn new_unseeded() -> Self {
PcgEngine {
state : Itype::zero(),
stream_mix : StreamMix::build(),
mul_mix : PhantomData::<MulMix>,
out_mix : PhantomData::<OutMix>,
phantom : PhantomData::<Xtype>,
}
}
}
impl<Itype, StreamMix, MulMix, OutMix> Rng for PcgEngine<Itype, u32, StreamMix, MulMix, OutMix>
where Itype: PcgOps + BitSize + AsSmaller<u32> + Clone,
StreamMix: Stream<Itype>, MulMix: Multiplier<Itype>, OutMix: OutputMixin<Itype, u32> {
#[inline]
fn next_u32(&mut self) -> u32 {
let oldstate = self.state.clone();
self.state = self.stream_mix.increment().add(oldstate.mul(MulMix::multiplier()));
OutMix::output(oldstate)
}
}
impl<Itype, StreamMix, MulMix, OutMix> Rng for PcgEngine<Itype, u64, StreamMix, MulMix, OutMix>
where Itype: PcgOps + BitSize + AsSmaller<u64> + Clone,
StreamMix: Stream<Itype>, MulMix: Multiplier<Itype>, OutMix: OutputMixin<Itype, u64> {
#[inline]
fn next_u32(&mut self) -> u32 {
self.next_u64() as u32
}
#[inline]
fn next_u64(&mut self) -> u64 {
let oldstate = self.state.clone();
self.state = self.stream_mix.increment().add(oldstate.mul(MulMix::multiplier()));
OutMix::output(oldstate)
}
}
impl<Itype, Xtype, StreamMix, MulMix, OutMix> Rand for PcgEngine<Itype, Xtype, StreamMix, MulMix, OutMix>
where Itype: PcgOps + BitSize + AsSmaller<Xtype> + Clone + Rand, Xtype: PcgOps + BitSize,
StreamMix: Stream<Itype> + Rand, MulMix: Multiplier<Itype>, OutMix: OutputMixin<Itype, Xtype>
{
fn rand<R: Rng>(rng: &mut R) -> Self {
PcgEngine{
state: rng.gen(),
stream_mix : rng.gen(),
mul_mix : PhantomData::<MulMix>,
out_mix : PhantomData::<OutMix>,
phantom : PhantomData::<Xtype>,
}
}
}
pub type OneseqXshRs6432 = PcgEngine<u64, u32, OneSeqStream, DefaultMultiplier, XshRsMixin>;
pub type OneseqXshRr6432 = PcgEngine<u64, u32, OneSeqStream, DefaultMultiplier, XshRrMixin>;
pub type UniqueXshRs6432 = PcgEngine<u64, u32, UniqueSeqStream, DefaultMultiplier, XshRsMixin>;
pub type UniqueXshRr6432 = PcgEngine<u64, u32, UniqueSeqStream, DefaultMultiplier, XshRrMixin>;
pub type SetseqXshRs6432 = PcgEngine<u64, u32, SpecificSeqStream<u64>, DefaultMultiplier, XshRsMixin>;
pub type SetseqXshRr6432 = PcgEngine<u64, u32, SpecificSeqStream<u64>, DefaultMultiplier, XshRrMixin>;
pub type McgXshRs6432 = PcgEngine<u64, u32, NoSeqStream, McgMultiplier, XshRsMixin>;
pub type McgXshRr6432 = PcgEngine<u64, u32, NoSeqStream, McgMultiplier, XshRrMixin>;
pub type Pcg32 = SetseqXshRr6432;
pub type Pcg32Oneseq = OneseqXshRr6432;
pub type Pcg32Unique = UniqueXshRr6432;
pub type Pcg32Fast = McgXshRs6432;
pub type OneseqXshRs12832 = PcgEngine<u128, u32, OneSeqStream, DefaultMultiplier, XshRsMixin>;
pub type OneseqXshRr12832 = PcgEngine<u128, u32, OneSeqStream, DefaultMultiplier, XshRrMixin>;
pub type UniqueXshRs12832 = PcgEngine<u128, u32, UniqueSeqStream, DefaultMultiplier, XshRsMixin>;
pub type UniqueXshRr12832 = PcgEngine<u128, u32, UniqueSeqStream, DefaultMultiplier, XshRrMixin>;
pub type SetseqXshRs12832 = PcgEngine<u128, u32, SpecificSeqStream<u128>, DefaultMultiplier, XshRsMixin>;
pub type SetseqXshRr12832 = PcgEngine<u128, u32, SpecificSeqStream<u128>, DefaultMultiplier, XshRrMixin>;
pub type McgXshRs12832 = PcgEngine<u128, u32, NoSeqStream, McgMultiplier, XshRsMixin>;
pub type McgXshRr12832 = PcgEngine<u128, u32, NoSeqStream, McgMultiplier, XshRrMixin>;
pub type Pcg32L = SetseqXshRr12832;
pub type Pcg32LOneseq = OneseqXshRr12832;
pub type Pcg32LUnique = UniqueXshRr12832;
pub type Pcg32LFast = McgXshRs12832;
pub type OneseqXshRs12864 = PcgEngine<u128, u64, OneSeqStream, DefaultMultiplier, XshRsMixin>;
pub type OneseqXshRr12864 = PcgEngine<u128, u64, OneSeqStream, DefaultMultiplier, XshRrMixin>;
pub type UniqueXshRs12864 = PcgEngine<u128, u64, UniqueSeqStream, DefaultMultiplier, XshRsMixin>;
pub type UniqueXshRr12864 = PcgEngine<u128, u64, UniqueSeqStream, DefaultMultiplier, XshRrMixin>;
pub type SetseqXshRs12864 = PcgEngine<u128, u64, SpecificSeqStream<u128>, DefaultMultiplier, XshRsMixin>;
pub type SetseqXshRr12864 = PcgEngine<u128, u64, SpecificSeqStream<u128>, DefaultMultiplier, XshRrMixin>;
pub type McgXshRs12864 = PcgEngine<u128, u64, NoSeqStream, McgMultiplier, XshRsMixin>;
pub type McgXshRr12864 = PcgEngine<u128, u64, NoSeqStream, McgMultiplier, XshRrMixin>;
pub type Pcg64 = SetseqXshRr12864;
pub type Pcg64Oneseq = OneseqXshRr12864;
pub type Pcg64Unique = UniqueXshRr12864;
pub type Pcg64Fast = McgXshRs12864;
impl<Itype, Xtype, StreamMix, MulMix, OutMix> SeedableRng<Itype> for PcgEngine<Itype, Xtype, StreamMix, MulMix, OutMix>
where
Itype: PcgOps + BitSize,
Xtype: PcgOps + BitSize,
StreamMix: Stream<Itype>,
MulMix: Multiplier<Itype>,
OutMix: OutputMixin<Itype, Xtype>,
PcgEngine<Itype, Xtype, StreamMix, MulMix, OutMix> : Rng
{
fn reseed(&mut self, seed: Itype) {
self.state = seed;
}
fn from_seed(seed: Itype) -> Self {
PcgEngine{
state: seed,
stream_mix : StreamMix::build(),
mul_mix : PhantomData::<MulMix>,
out_mix : PhantomData::<OutMix>,
phantom : PhantomData::<Xtype>,
}
}
}
impl<Itype, Xtype, MulMix, OutMix> SeedableRng<[Itype;2]> for PcgEngine<Itype, Xtype, SpecificSeqStream<Itype>, MulMix, OutMix>
where Itype: PcgOps + BitSize + Clone,
Xtype: PcgOps + BitSize,
MulMix: Multiplier<Itype>,
OutMix: OutputMixin<Itype, Xtype>,
SpecificSeqStream<Itype>: Stream<Itype>,
PcgEngine<Itype, Xtype, SpecificSeqStream<Itype>, MulMix, OutMix> : Rng
{
fn reseed(&mut self, seed: [Itype;2]) {
self.state = seed[0].clone();
self.stream_mix.set_stream(seed[1].clone());
}
fn from_seed(seed: [Itype;2]) -> Self {
let mut stream = SpecificSeqStream::build();
stream.set_stream(seed[1].clone());
PcgEngine{
state: seed[0].clone(),
stream_mix : stream,
mul_mix : PhantomData::<MulMix>,
out_mix : PhantomData::<OutMix>,
phantom : PhantomData::<Xtype>,
}
}
}
impl<Xtype, StreamMix, MulMix, OutMix> SeedableRng<[u64;2]> for PcgEngine<u128, Xtype, StreamMix, MulMix, OutMix>
where Xtype: PcgOps + BitSize, StreamMix: Stream<u128>, MulMix: Multiplier<u128>, OutMix: OutputMixin<u128, Xtype>,
PcgEngine<u128, Xtype, StreamMix, MulMix, OutMix> : Rng
{
fn reseed(&mut self, seed: [u64;2]) {
self.state = u128::from_parts(seed[0], seed[1]);
}
fn from_seed(seed: [u64;2]) -> Self {
PcgEngine{
state: u128::from_parts(seed[0], seed[1]),
stream_mix : StreamMix::build(),
mul_mix : PhantomData::<MulMix>,
out_mix : PhantomData::<OutMix>,
phantom : PhantomData::<Xtype>,
}
}
}
impl<Xtype, MulMix, OutMix> SeedableRng<[u64;4]> for PcgEngine<u128, Xtype, SpecificSeqStream<u128>, MulMix, OutMix>
where Xtype: PcgOps + BitSize, MulMix: Multiplier<u128>, OutMix: OutputMixin<u128, Xtype>,
PcgEngine<u128, Xtype, SpecificSeqStream<u128>, MulMix, OutMix> : Rng
{
fn reseed(&mut self, seed: [u64;4]) {
self.state = u128::from_parts(seed[0], seed[1]);
self.stream_mix.set_stream(u128::from_parts(seed[2], seed[3]));
}
fn from_seed(seed: [u64;4]) -> Self {
let mut stream = SpecificSeqStream::build();
stream.set_stream(u128::from_parts(seed[2], seed[3]));
PcgEngine{
state: u128::from_parts(seed[0], seed[1]),
stream_mix : stream,
mul_mix : PhantomData::<MulMix>,
out_mix : PhantomData::<OutMix>,
phantom : PhantomData::<Xtype>,
}
}
}
pub struct Pcg32Basic {
state : u64,
inc : u64,
}
impl Pcg32Basic {
pub fn new_unseeded() -> Pcg32Basic {
Pcg32Basic{
state : 0,
inc : 1,
}
}
}
impl Rng for Pcg32Basic {
#[inline]
fn next_u32(&mut self) -> u32 {
let oldstate = Wrapping(self.state);
self.state = (oldstate * Wrapping(6364136223846793005u64) + Wrapping(self.inc | 1)).0;
let xorshifted : u32 = (((oldstate >> 18usize) ^ oldstate) >> 27usize).0 as u32;
let rot : u32 = (oldstate >> 59usize).0 as u32;
(xorshifted >> rot) | (xorshifted << ((-(rot as i32)) & 31))
}
}
impl SeedableRng<[u64; 2]> for Pcg32Basic {
fn reseed(&mut self, seed: [u64; 2]) {
self.state = seed[0];
self.inc = seed[1];
}
fn from_seed(seed: [u64; 2]) -> Pcg32Basic {
Pcg32Basic {
state : seed[0],
inc : seed[1],
}
}
}
impl Rand for Pcg32Basic {
fn rand<R: Rng>(other: &mut R) -> Pcg32Basic {
Pcg32Basic{
state : other.gen(),
inc : other.gen(),
}
}
}