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
pub trait PcgOps {
fn wrap_mul(&self, rhs: Self) -> Self;
fn wrap_add(&self, rhs: Self) -> Self;
}
pub trait AsUsize {
fn as_usize(&self) -> usize;
}
pub trait BitSize {
const BITS: usize;
}
pub trait AsSmaller<T> {
fn shrink(self) -> T;
}
macro_rules! basic_ops {
( $( $t:ty, $bits:expr);*) => {
$(impl BitSize for $t {
const BITS: usize = $bits;
}
impl AsUsize for $t {
#[inline]
fn as_usize(&self) -> usize {
*self as usize
}
}
impl PcgOps for $t {
#[inline]
fn wrap_mul(&self, rhs : $t) -> $t {
self.wrapping_mul(rhs)
}
#[inline]
fn wrap_add(&self, rhs : $t) -> $t {
self.wrapping_add(rhs)
}
}
)*
}
}
basic_ops!(
u8, 8;
u16, 16;
u32, 32;
u64, 64;
u128, 128
);
macro_rules! smaller {
( $( $t:ty, $other:ty);*) => {
$(
impl AsSmaller<$other> for $t {
#[inline]
fn shrink(self) -> $other {
self as $other
}
}
)*
}
}
smaller!(
u128, u128;
u128, u64;
u128, u32;
u128, u16;
u128, u8;
u64, u64;
u64, u32;
u64, u16;
u64, u8;
u32, u32;
u32, u16;
u32, u8;
u16, u16;
u16, u8
);