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
/*
 * PCG Random Number Generation for Rust
 *
 * Copyright 2015 John Brooks <robojeb@robojeb.xyz>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * This work is derived from the implementation PCG RNG for C++ by
 * Melissa O'Neill.
 *
 * For additional information about the PCG random number generation scheme,
 * including its license and other licensing options, visit
 *
 *     http://www.pcg-random.org
 */

use num_traits::PrimInt;
use numops::*;
use std::ops::{BitXor, Shr};

/// The output mixin trait provides the permutation function for the output
/// of the PCG. After the LCG state is advanced the state is run through
/// the `output(...)` function to produce the output.
pub trait OutputMixin<Itype, Xtype> {
    fn output(state: Itype) -> Xtype;
}

/// This output uses an Xor-shift followed by a right shift
#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
pub struct XshRsMixin;

impl<Itype, Xtype> OutputMixin<Itype, Xtype> for XshRsMixin
where
    Itype: Shr<usize, Output = Itype>
        + BitXor<Itype, Output = Itype>
        + AsSmaller<Xtype>
        + BitSize
        + AsUsize
        + Copy,
    Xtype: BitSize,
{
    #[inline(always)]
    fn output(state: Itype) -> Xtype {
        let mut state = state;
        let sparebits = Itype::BITS - Xtype::BITS;

        let opbits: usize = if sparebits - 5 >= 64 {
            5
        } else if sparebits - 4 >= 32 {
            4
        } else if sparebits - 3 >= 16 {
            3
        } else if sparebits - 2 >= 4 {
            2
        } else if sparebits > 1 {
            1
        } else {
            0
        };
        let mask = (1 << opbits) - 1;
        let maxrandshift = mask;
        let topspare = opbits;
        let bottomspare = sparebits - topspare;
        let xshift = topspare + (Xtype::BITS + maxrandshift) / 2;

        let rshift = if opbits != 0 {
            (state >> (Itype::BITS - opbits)).as_usize() & mask
        } else {
            0
        };

        state = state ^ (state >> xshift);
        (state >> (bottomspare - maxrandshift + rshift)).shrink()
    }
}

/// This output uses an xor-shift followed by a random rotation.
#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
pub struct XshRrMixin;

impl<Itype, Xtype> OutputMixin<Itype, Xtype> for XshRrMixin
where
    Itype: Shr<usize, Output = Itype>
        + BitXor<Itype, Output = Itype>
        + AsUsize
        + AsSmaller<Xtype>
        + BitSize
        + Copy,
    Xtype: BitSize + PrimInt,
{
    #[inline(always)]
    fn output(state: Itype) -> Xtype {
        let mut state = state;

        let sparebits = Itype::BITS - Xtype::BITS;
        let xtypebits = Xtype::BITS;
        let wantedopbits: usize = if xtypebits >= 128 {
            7
        } else if xtypebits >= 64 {
            6
        } else if xtypebits >= 32 {
            5
        } else if xtypebits >= 16 {
            4
        } else {
            3
        };

        let opbits: usize = if sparebits >= wantedopbits {
            wantedopbits
        } else {
            sparebits
        };

        let amplifier = wantedopbits - opbits;
        let mask = (1 << opbits) - 1;
        let topspare = opbits;
        let bottomspare = sparebits - topspare;
        let xshift = (topspare + xtypebits) / 2;

        let rot = if opbits != 0 {
            (state >> (Itype::BITS - opbits)).as_usize() & mask
        } else {
            0
        };

        let amprot = (rot << amplifier) & mask;
        state = state ^ (state >> xshift);

        let result: Xtype = (state >> bottomspare).shrink();
        result.rotate_right(amprot as u32)
    }
}