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
//! Module for PCGs that use their own current memory address to pick the
//! stream.
//!
//! This works just like the `Pick` flavor of PCGs, except that instead of
//! storing a stream value along with the state the generator it just uses its
//! own `&mut` cast into a `usize`, then cast into the state type (`|1`, to make
//! it odd).
//!
//! Seeding these things is a little weird. Normally a PCG will run once or
//! twice as part of the seeding process, and when this generator is run as part
//! of seeding it has to use its position on the stack, but when it's returned
//! to the caller its position changes, which puts it into a new stream.
//! However, if that bugs you in the slightest I guess you'd have never selected
//! this PRNG in the first place.

use super::*;

const DEFAULT_MEM_SEED: u128 = 245720598905631564143578724636268694099;

/// A PCG with 8 bits of state, mem address stream
#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(transparent)]
pub struct PCGMemU8 {
  state: u8,
}
impl PCGMemU8 {
  /// Seeds the generator
  pub fn seed(seed: u8) -> Self {
    let mut out = Self { state: 0 };
    let inc = unsafe { NonZeroU8::new_unchecked(&mut out as *mut Self as usize as u8 | 1) };
    out.state = lcg8(out.state, PCG_DEFAULT_MULTIPLIER_8, inc);
    out.state = out.state.wrapping_add(seed);
    out.state = lcg8(out.state, PCG_DEFAULT_MULTIPLIER_8, inc);
    out
  }
  /// Gives a generator of the exact values you put in
  pub const fn exact(state: u8) -> Self {
    Self { state }
  }
  /// Gives the next same-size output.
  pub fn next_u8(&mut self) -> u8 {
    let output = rxs_m_xs_8_8(self.state);
    let inc = unsafe { NonZeroU8::new_unchecked(self as *mut Self as usize as u8 | 1) };
    let next_state = lcg8(self.state, PCG_DEFAULT_MULTIPLIER_8, inc);
    self.state = next_state;
    output
  }
}
impl From<u64> for PCGMemU8 {
  fn from(seed: u64) -> Self {
    Self::seed(seed as u8)
  }
}
impl Default for PCGMemU8 {
  fn default() -> Self {
    Self::seed(DEFAULT_MEM_SEED as u8)
  }
}

/// A PCG with 16 bits of state, mem address stream
#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(transparent)]
pub struct PCGMemU16 {
  state: u16,
}
impl PCGMemU16 {
  /// Seeds the generator
  pub fn seed(seed: u16) -> Self {
    let mut out = Self { state: 0 };
    let inc = unsafe { NonZeroU16::new_unchecked(&mut out as *mut Self as usize as u16 | 1) };
    out.state = lcg16(out.state, PCG_DEFAULT_MULTIPLIER_16, inc);
    out.state = out.state.wrapping_add(seed);
    out.state = lcg16(out.state, PCG_DEFAULT_MULTIPLIER_16, inc);
    out
  }
  /// Gives a generator of the exact values you put in
  pub const fn exact(state: u16) -> Self {
    Self { state }
  }
  /// Gives the next half-size output, using rotation
  pub fn next_u8(&mut self) -> u8 {
    let inc = unsafe { NonZeroU16::new_unchecked(self as *mut Self as usize as u16 | 1) };
    let output = xsh_rr_16_8(self.state);
    let next_state = lcg16(self.state, PCG_DEFAULT_MULTIPLIER_16, inc);
    self.state = next_state;
    output
  }
  /// Gives the next half-size output, using a shift
  pub fn next_u8shift(&mut self) -> u8 {
    let inc = unsafe { NonZeroU16::new_unchecked(self as *mut Self as usize as u16 | 1) };
    let output = xsh_rs_16_8(self.state);
    let next_state = lcg16(self.state, PCG_DEFAULT_MULTIPLIER_16, inc);
    self.state = next_state;
    output
  }
  /// Gives the next same-size output.
  pub fn next_u16(&mut self) -> u16 {
    let inc = unsafe { NonZeroU16::new_unchecked(self as *mut Self as usize as u16 | 1) };
    let output = rxs_m_xs_16_16(self.state);
    let next_state = lcg16(self.state, PCG_DEFAULT_MULTIPLIER_16, inc);
    self.state = next_state;
    output
  }
}
impl From<u64> for PCGMemU16 {
  fn from(seed: u64) -> Self {
    Self::seed(seed as u16)
  }
}
impl Default for PCGMemU16 {
  fn default() -> Self {
    Self::seed(DEFAULT_MEM_SEED as u16)
  }
}

/// A PCG with 32 bits of state, mem address stream
#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(transparent)]
pub struct PCGMemU32 {
  state: u32,
}
impl PCGMemU32 {
  /// Seeds the generator
  pub fn seed(seed: u32) -> Self {
    let mut out = Self { state: 0 };
    let inc = unsafe { NonZeroU32::new_unchecked(&mut out as *mut Self as usize as u32 | 1) };
    out.state = lcg32(out.state, PCG_DEFAULT_MULTIPLIER_32, inc);
    out.state = out.state.wrapping_add(seed);
    out.state = lcg32(out.state, PCG_DEFAULT_MULTIPLIER_32, inc);
    out
  }
  /// Gives a generator of the exact values you put in
  pub const fn exact(state: u32) -> Self {
    Self { state }
  }
  /// Gives the next half-size output, using rotation
  pub fn next_u16(&mut self) -> u16 {
    let inc = unsafe { NonZeroU32::new_unchecked(self as *mut Self as usize as u32 | 1) };
    let output = xsh_rr_32_16(self.state);
    let next_state = lcg32(self.state, PCG_DEFAULT_MULTIPLIER_32, inc);
    self.state = next_state;
    output
  }
  /// Gives the next half-size output, using a shift
  pub fn next_u16shift(&mut self) -> u16 {
    let inc = unsafe { NonZeroU32::new_unchecked(self as *mut Self as usize as u32 | 1) };
    let output = xsh_rs_32_16(self.state);
    let next_state = lcg32(self.state, PCG_DEFAULT_MULTIPLIER_32, inc);
    self.state = next_state;
    output
  }
  /// Gives the next same-size output.
  pub fn next_u32(&mut self) -> u32 {
    let inc = unsafe { NonZeroU32::new_unchecked(self as *mut Self as usize as u32 | 1) };
    let output = rxs_m_xs_32_32(self.state);
    let next_state = lcg32(self.state, PCG_DEFAULT_MULTIPLIER_32, inc);
    self.state = next_state;
    output
  }
}
impl From<u64> for PCGMemU32 {
  fn from(seed: u64) -> Self {
    Self::seed(seed as u32)
  }
}
impl Default for PCGMemU32 {
  fn default() -> Self {
    Self::seed(DEFAULT_MEM_SEED as u32)
  }
}

/// A PCG with 64 bits of state, mem address stream
#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(transparent)]
pub struct PCGMemU64 {
  state: u64,
}
impl PCGMemU64 {
  /// Seeds the generator
  pub fn seed(seed: u64) -> Self {
    let mut out = Self { state: 0 };
    let inc = unsafe { NonZeroU64::new_unchecked(&mut out as *mut Self as usize as u64 | 1) };
    out.state = lcg64(out.state, PCG_DEFAULT_MULTIPLIER_64, inc);
    out.state = out.state.wrapping_add(seed);
    out.state = lcg64(out.state, PCG_DEFAULT_MULTIPLIER_64, inc);
    out
  }
  /// Gives a generator of the exact values you put in
  pub const fn exact(state: u64) -> Self {
    Self { state }
  }
  /// Gives the next half-size output, using rotation
  pub fn next_u32(&mut self) -> u32 {
    let inc = unsafe { NonZeroU64::new_unchecked(self as *mut Self as usize as u64 | 1) };
    let output = xsh_rr_64_32(self.state);
    let next_state = lcg64(self.state, PCG_DEFAULT_MULTIPLIER_64, inc);
    self.state = next_state;
    output
  }
  /// Gives the next half-size output, using a shift
  pub fn next_u32shift(&mut self) -> u32 {
    let inc = unsafe { NonZeroU64::new_unchecked(self as *mut Self as usize as u64 | 1) };
    let output = xsh_rs_64_32(self.state);
    let next_state = lcg64(self.state, PCG_DEFAULT_MULTIPLIER_64, inc);
    self.state = next_state;
    output
  }
  /// Gives the next half-size output, using xsl rr
  pub fn next_u32xslrr(&mut self) -> u32 {
    let inc = unsafe { NonZeroU64::new_unchecked(self as *mut Self as usize as u64 | 1) };
    let output = xsl_rr_64_32(self.state);
    let next_state = lcg64(self.state, PCG_DEFAULT_MULTIPLIER_64, inc);
    self.state = next_state;
    output
  }
  /// Gives the next same-size output.
  pub fn next_u64(&mut self) -> u64 {
    let inc = unsafe { NonZeroU64::new_unchecked(self as *mut Self as usize as u64 | 1) };
    let output = rxs_m_xs_64_64(self.state);
    let next_state = lcg64(self.state, PCG_DEFAULT_MULTIPLIER_64, inc);
    self.state = next_state;
    output
  }
  /// Gives the next same-size output, using xsl rr rr
  pub fn next_u64xsl2rr(&mut self) -> u64 {
    let inc = unsafe { NonZeroU64::new_unchecked(self as *mut Self as usize as u64 | 1) };
    let output = xsl_rr_rr_64_64(self.state);
    let next_state = lcg64(self.state, PCG_DEFAULT_MULTIPLIER_64, inc);
    self.state = next_state;
    output
  }
}
impl From<u64> for PCGMemU64 {
  fn from(seed: u64) -> Self {
    Self::seed(seed)
  }
}
impl Default for PCGMemU64 {
  fn default() -> Self {
    Self::seed(DEFAULT_MEM_SEED as u64)
  }
}

/// A PCG with 128 bits of state, mem address stream
#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(transparent)]
pub struct PCGMemU128 {
  state: u128,
}
impl PCGMemU128 {
  /// Seeds the generator
  pub fn seed(seed: u128) -> Self {
    let mut out = Self { state: 0 };
    let inc = unsafe { NonZeroU128::new_unchecked(&mut out as *mut Self as usize as u128 | 1) };
    out.state = lcg128(out.state, PCG_DEFAULT_MULTIPLIER_128, inc);
    out.state = out.state.wrapping_add(seed);
    out.state = lcg128(out.state, PCG_DEFAULT_MULTIPLIER_128, inc);
    out
  }
  /// Gives a generator of the exact values you put in
  pub const fn exact(state: u128) -> Self {
    Self { state }
  }
  /// Gives the next half-size output, using rotation
  pub fn next_u64(&mut self) -> u64 {
    let inc = unsafe { NonZeroU128::new_unchecked(self as *mut Self as usize as u128 | 1) };
    let output = xsh_rr_128_64(self.state);
    let next_state = lcg128(self.state, PCG_DEFAULT_MULTIPLIER_128, inc);
    self.state = next_state;
    output
  }
  /// Gives the next half-size output, using a shift
  pub fn next_u64shift(&mut self) -> u64 {
    let inc = unsafe { NonZeroU128::new_unchecked(self as *mut Self as usize as u128 | 1) };
    let output = xsh_rs_128_64(self.state);
    let next_state = lcg128(self.state, PCG_DEFAULT_MULTIPLIER_128, inc);
    self.state = next_state;
    output
  }
  /// Gives the next half-size output, using xsl rr
  pub fn next_u64xslrr(&mut self) -> u64 {
    let inc = unsafe { NonZeroU128::new_unchecked(self as *mut Self as usize as u128 | 1) };
    let output = xsl_rr_128_64(self.state);
    let next_state = lcg128(self.state, PCG_DEFAULT_MULTIPLIER_128, inc);
    self.state = next_state;
    output
  }
  /// Gives the next same-size output.
  pub fn next_u128(&mut self) -> u128 {
    let inc = unsafe { NonZeroU128::new_unchecked(self as *mut Self as usize as u128 | 1) };
    let output = rxs_m_xs_128_128(self.state);
    let next_state = lcg128(self.state, PCG_DEFAULT_MULTIPLIER_128, inc);
    self.state = next_state;
    output
  }
  /// Gives the next same-size output, using xsl rr rr
  pub fn next_u128xsl2rr(&mut self) -> u128 {
    let inc = unsafe { NonZeroU128::new_unchecked(self as *mut Self as usize as u128 | 1) };
    let output = xsl_rr_rr_128_128(self.state);
    let next_state = lcg128(self.state, PCG_DEFAULT_MULTIPLIER_128, inc);
    self.state = next_state;
    output
  }
}
impl From<u64> for PCGMemU128 {
  fn from(seed: u64) -> Self {
    Self::seed(seed as u128)
  }
}
impl Default for PCGMemU128 {
  fn default() -> Self {
    Self::seed(DEFAULT_MEM_SEED)
  }
}