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
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[repr(u16)]
pub enum PowerOfTwoSixteenBit
{
_1 = 1,
_2 = 2,
_4 = 4,
_8 = 8,
_16 = 16,
_32 = 32,
_64 = 64,
_128 = 128,
_256 = 256,
_512 = 512,
_1024 = 1024,
_2048 = 2048,
_4096 = 4096,
_8192 = 8192,
_16384 = 16384,
_32768 = 32768,
}
impl PowerOfTwoSixteenBit
{
#[inline(always)]
pub fn isPowerOfTwo(value: u16) -> bool
{
debug_assert!(value != 0, "zero is not a valid power of two");
(value & (value - 1)) == 0
}
#[inline(always)]
pub unsafe fn from_u16_unchecked(value: u16) -> PowerOfTwoSixteenBit
{
debug_assert!(value != 0, "zero is not a valid power of two");
transmute(value)
}
#[inline(always)]
pub unsafe fn from_u16_panic(value: u16) -> PowerOfTwoSixteenBit
{
match value
{
0 => panic!("Zero is not converted"),
1 => PowerOfTwoSixteenBit::_1,
2 => PowerOfTwoSixteenBit::_2,
4 => PowerOfTwoSixteenBit::_4,
8 => PowerOfTwoSixteenBit::_8,
16 => PowerOfTwoSixteenBit::_16,
32 => PowerOfTwoSixteenBit::_32,
64 => PowerOfTwoSixteenBit::_64,
128 => PowerOfTwoSixteenBit::_128,
256 => PowerOfTwoSixteenBit::_256,
512 => PowerOfTwoSixteenBit::_512,
1024 => PowerOfTwoSixteenBit::_1024,
2048 => PowerOfTwoSixteenBit::_2048,
4096 => PowerOfTwoSixteenBit::_4096,
8192 => PowerOfTwoSixteenBit::_8192,
16384 => PowerOfTwoSixteenBit::_16384,
32768 => PowerOfTwoSixteenBit::_32768,
_ => panic!("The value '{}' is not a power of two", value),
}
}
#[inline(always)]
pub unsafe fn from_u16_panic_withZeroAs<F>(value: u16, zeroIs: F) -> PowerOfTwoSixteenBit
where F: Fn() -> PowerOfTwoSixteenBit
{
match value
{
0 => zeroIs(),
1 => PowerOfTwoSixteenBit::_1,
2 => PowerOfTwoSixteenBit::_2,
4 => PowerOfTwoSixteenBit::_4,
8 => PowerOfTwoSixteenBit::_8,
16 => PowerOfTwoSixteenBit::_16,
32 => PowerOfTwoSixteenBit::_32,
64 => PowerOfTwoSixteenBit::_64,
128 => PowerOfTwoSixteenBit::_128,
256 => PowerOfTwoSixteenBit::_256,
512 => PowerOfTwoSixteenBit::_512,
1024 => PowerOfTwoSixteenBit::_1024,
2048 => PowerOfTwoSixteenBit::_2048,
4096 => PowerOfTwoSixteenBit::_4096,
8192 => PowerOfTwoSixteenBit::_8192,
16384 => PowerOfTwoSixteenBit::_16384,
32768 => PowerOfTwoSixteenBit::_32768,
_ => panic!("The value '{}' is not a power of two", value),
}
}
#[inline(always)]
pub unsafe fn from_u16(value: u16) -> Option<PowerOfTwoSixteenBit>
{
match value
{
0 => None,
1 => Some(PowerOfTwoSixteenBit::_1),
2 => Some(PowerOfTwoSixteenBit::_2),
4 => Some(PowerOfTwoSixteenBit::_4),
8 => Some(PowerOfTwoSixteenBit::_8),
16 => Some(PowerOfTwoSixteenBit::_16),
32 => Some(PowerOfTwoSixteenBit::_32),
64 => Some(PowerOfTwoSixteenBit::_64),
128 => Some(PowerOfTwoSixteenBit::_128),
256 => Some(PowerOfTwoSixteenBit::_256),
512 => Some(PowerOfTwoSixteenBit::_512),
1024 => Some(PowerOfTwoSixteenBit::_1024),
2048 => Some(PowerOfTwoSixteenBit::_2048),
4096 => Some(PowerOfTwoSixteenBit::_4096),
8192 => Some(PowerOfTwoSixteenBit::_8192),
16384 => Some(PowerOfTwoSixteenBit::_16384),
32768 => Some(PowerOfTwoSixteenBit::_32768),
_ => None,
}
}
}