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
/*
 * PCG Random Number Generation for Rust
 *
 * Copyright 2015 John Brooks <jeb@robojeb.dev>
 *
 * 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::{One, PrimInt};
use numops::*;
use std::ops::{BitOr, 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> {
    const SERIALIZER_ID: &'static str;
    fn output(state: Itype, increment: Itype, multiplier: Itype) -> Xtype;
}

/// This output uses an Xor-shift followed by a right shift
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,
{
    const SERIALIZER_ID: &'static str = "XshRs";
    #[inline(always)]
    fn output(state: Itype, _increment: Itype, _multiplier: 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.
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,
{
    const SERIALIZER_ID: &'static str = "XshRr";
    #[inline(always)]
    fn output(state: Itype, _increment: Itype, _multiplier: 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)
    }
}

/// The Double Xor-shift multiply output
/// This is a new (added to the PCG C++ library in 2019) output which is meant to be more powerful.
pub struct DXsMMixin;

impl<Itype, Xtype> OutputMixin<Itype, Xtype> for DXsMMixin
where
    Itype: AsSmaller<Xtype> + Shr<usize, Output = Itype> + BitSize + Copy,
    Xtype: BitSize
        + PcgOps
        + Shr<usize, Output = Xtype>
        + BitXor<Xtype, Output = Xtype>
        + BitOr<Xtype, Output = Xtype>
        + One
        + Copy,
{
    const SERIALIZER_ID: &'static str = "DXsM";
    #[inline(always)]
    fn output(state: Itype, _increment: Itype, multiplier: Itype) -> Xtype {
        let hi: Xtype = (state >> (Itype::BITS - Xtype::BITS)).shrink();
        let low: Xtype = state.shrink();

        let low = low | Xtype::one();
        let hi = hi ^ (hi >> (Xtype::BITS / 2));

        let hi = hi.wrap_mul(multiplier.shrink());

        let hi = hi ^ (hi >> (3 * Xtype::BITS / 4));

        hi.wrap_mul(low)
    }
}