| 1 | use Sourcerust-analyzer cargo random 0.12.2 source/Source#; |
| 2 | |
| 3 | /// An instance of the Xorshift128+ algorithm. |
| 4 | /// |
| 5 | /// ## References |
| 6 | /// |
| 7 | /// 1. Sebastiano Vigna, “Further Scramblings of Marsaglia’s Xorshift |
| 8 | /// Generators,” CoRR, 2014. |
| 9 | /// |
| 10 | /// 2. https://en.wikipedia.org/wiki/Xorshift#xorshift.2B |
| 11 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:)] |
| 12 | pub struct Xorshift128Plusrust-analyzer cargo random 0.12.2 xorshift/Xorshift128Plus#(u64, u64); |
| 13 | |
| 14 | impl Xorshift128Plusrust-analyzer cargo random 0.12.2 xorshift/Xorshift128Plus# { |
| 15 | /// Create an instance of the algorithm. |
| 16 | /// |
| 17 | /// At least one bit of the seed should be one. |
| 18 | #[inline(always)] |
| 19 | pub fn newrust-analyzer cargo random 0.12.2 xorshift/impl#[Xorshift128Plus]new().(seed: [u64; 2]) -> Xorshift128Plusrust-analyzer cargo random 0.12.2 xorshift/Xorshift128Plus# { |
| 20 | debug_assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/debug_assert!!(seed[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().0]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index(). |rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u64][`BitOr<Self>`]bitor(). seed[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().1]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index(). != 0, "at least one bit of the seed should be one"); |
| 21 | Xorshift128Plusrust-analyzer cargo random 0.12.2 xorshift/Xorshift128Plus#(seed[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().0]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index()., seed[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().1]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().) |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | impl Sourcerust-analyzer cargo random 0.12.2 source/Source# for Xorshift128Plusrust-analyzer cargo random 0.12.2 xorshift/Xorshift128Plus# { |
| 26 | #[inline(always)] |
| 27 | fn read_u64rust-analyzer cargo random 0.12.2 xorshift/impl#[Xorshift128Plus][Source]read_u64().(&mut self) -> u64 { |
| 28 | let (mut x, y) = (self.0rust-analyzer cargo random 0.12.2 xorshift/Xorshift128Plus#0., self.1rust-analyzer cargo random 0.12.2 xorshift/Xorshift128Plus#1.); |
| 29 | self.0rust-analyzer cargo random 0.12.2 xorshift/Xorshift128Plus#0. = y; |
| 30 | x = x rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u64][`BitXor<Self>`]bitxor().^rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u64][`BitXor<Self>`]bitxor(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u64][`BitXor<Self>`]bitxor().(x rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u64][`Shl<i32>`]shl().<<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u64][`Shl<i32>`]shl(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u64][`Shl<i32>`]shl().23); |
| 31 | x = x rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u64][`BitXor<Self>`]bitxor().^rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u64][`BitXor<Self>`]bitxor(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u64][`BitXor<Self>`]bitxor().(x rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u64][`Shr<i32>`]shr().>>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u64][`Shr<i32>`]shr(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u64][`Shr<i32>`]shr().17); |
| 32 | x = x rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u64][`BitXor<Self>`]bitxor().^rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u64][`BitXor<Self>`]bitxor(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u64][`BitXor<Self>`]bitxor().y rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u64][`BitXor<Self>`]bitxor().^rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u64][`BitXor<Self>`]bitxor(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u64][`BitXor<Self>`]bitxor().(y rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u64][`Shr<i32>`]shr().>>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u64][`Shr<i32>`]shr(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u64][`Shr<i32>`]shr().26); |
| 33 | self.1rust-analyzer cargo random 0.12.2 xorshift/Xorshift128Plus#1. = x; |
| 34 | x.wrapping_addrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u64]wrapping_add().(y) |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | #[cfg(test)] |
| 39 | mod testsrust-analyzer cargo random 0.12.2 xorshift/tests/ { |
| 40 | use Xorshift128Plusrust-analyzer cargo random 0.12.2 xorshift/Xorshift128Plus#; |
| 41 | |
| 42 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 43 | #[should_panic] |
| 44 | fn new_zero_seedrust-analyzer cargo random 0.12.2 xorshift/tests/new_zero_seed().() { |
| 45 | let _ = Xorshift128Plusrust-analyzer cargo random 0.12.2 xorshift/Xorshift128Plus#::newrust-analyzer cargo random 0.12.2 xorshift/impl#[Xorshift128Plus]new().([0, 0]); |
| 46 | } |
| 47 | } |