rust_extra/powersOfTwo/
PowerOfTwoThirtyTwoBit.rs

1// This file is part of dpdk. It is subject to the license terms in the COPYRIGHT file found in the top-level directory of this distribution and at https://raw.githubusercontent.com/lemonrock/dpdk/master/COPYRIGHT. No part of dpdk, including this file, may be copied, modified, propagated, or distributed except according to the terms contained in the COPYRIGHT file.
2// Copyright © 2016 The developers of dpdk. See the COPYRIGHT file in the top-level directory of this distribution and at https://raw.githubusercontent.com/lemonrock/dpdk/master/COPYRIGHT.
3
4
5#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
6#[repr(u32)]
7pub enum PowerOfTwoThirtyTwoBit
8{
9	_1 = 1,
10	_2 = 2,
11	_4 = 4,
12	_8 = 8,
13	_16 = 16,
14	_32 = 32,
15	_64 = 64,
16	_128 = 128,
17	_256 = 256,
18	_512 = 512,
19	_1024 = 1024,
20	_2048 = 2048,
21	_4096 = 4096,
22	_8192 = 8192,
23	_16384 = 16384,
24	_32768 = 32768,
25	_65536 = 65536,
26	_131072 = 131072,
27	_262144 = 262144,
28	_524288 = 524288,
29	_1048576 = 1048576,
30	_2097152 = 2097152,
31	_4194304 = 4194304,
32	_8388608 = 8388608,
33	_16777216 = 16777216,
34	_33554432 = 33554432,
35	_67108864 = 67108864,
36	_134217728 = 134217728,
37	_268435456 = 268435456,
38	_536870912 = 536870912,
39	_1073741824 = 1073741824,
40	_2147483648 = 2147483648,
41}
42
43impl AsU32 for Option<PowerOfTwoThirtyTwoBit>
44{
45	#[inline(always)]
46	fn as_u32(&self) -> u32
47	{
48		if self.is_none()
49		{
50			0
51		}
52		else
53		{
54			self.unwrap().as_u32()
55		}
56	}
57}
58
59impl AsU32 for PowerOfTwoThirtyTwoBit
60{
61	#[inline(always)]
62	fn as_u32(&self) -> u32
63	{
64		*self as u32
65	}
66}
67
68impl PowerOfTwoThirtyTwoBit
69{
70	// Does not check for value == 0 (that is not a power of two)
71	#[inline(always)]
72	pub fn isPowerOfTwo(value: u32) -> bool
73	{
74		debug_assert!(value != 0, "zero is not a valid power of two");
75		
76	    (value & (value - 1)) == 0
77	}
78	
79	#[inline(always)]
80	pub unsafe fn from_u32_unchecked(value: u32) -> PowerOfTwoThirtyTwoBit
81	{
82		debug_assert!(value != 0, "zero is not a valid power of two");
83		
84		transmute(value)
85	}
86	
87	#[inline(always)]
88	pub unsafe fn from_u32_panic(value: u32) -> PowerOfTwoThirtyTwoBit
89	{
90		match value
91		{
92			0 => panic!("Zero is not converted"),
93			
94			1 => PowerOfTwoThirtyTwoBit::_1,
95			2 => PowerOfTwoThirtyTwoBit::_2,
96			4 => PowerOfTwoThirtyTwoBit::_4,
97			8 => PowerOfTwoThirtyTwoBit::_8,
98			16 => PowerOfTwoThirtyTwoBit::_16,
99			32 => PowerOfTwoThirtyTwoBit::_32,
100			64 => PowerOfTwoThirtyTwoBit::_64,
101			128 => PowerOfTwoThirtyTwoBit::_128,
102			256 => PowerOfTwoThirtyTwoBit::_256,
103			512 => PowerOfTwoThirtyTwoBit::_512,
104			1024 => PowerOfTwoThirtyTwoBit::_1024,
105			2048 => PowerOfTwoThirtyTwoBit::_2048,
106			4096 => PowerOfTwoThirtyTwoBit::_4096,
107			8192 => PowerOfTwoThirtyTwoBit::_8192,
108			16384 => PowerOfTwoThirtyTwoBit::_16384,
109			32768 => PowerOfTwoThirtyTwoBit::_32768,
110			65536 => PowerOfTwoThirtyTwoBit::_65536,
111			131072 => PowerOfTwoThirtyTwoBit::_131072,
112			262144 => PowerOfTwoThirtyTwoBit::_262144,
113			524288 => PowerOfTwoThirtyTwoBit::_524288,
114			1048576 => PowerOfTwoThirtyTwoBit::_1048576,
115			2097152 => PowerOfTwoThirtyTwoBit::_2097152,
116			4194304 => PowerOfTwoThirtyTwoBit::_4194304,
117			8388608 => PowerOfTwoThirtyTwoBit::_8388608,
118			16777216 => PowerOfTwoThirtyTwoBit::_16777216,
119			33554432 => PowerOfTwoThirtyTwoBit::_33554432,
120			67108864 => PowerOfTwoThirtyTwoBit::_67108864,
121			134217728 => PowerOfTwoThirtyTwoBit::_134217728,
122			268435456 => PowerOfTwoThirtyTwoBit::_268435456,
123			536870912 => PowerOfTwoThirtyTwoBit::_536870912,
124			1073741824 => PowerOfTwoThirtyTwoBit::_1073741824,
125			2147483648 => PowerOfTwoThirtyTwoBit::_2147483648,
126			
127			_ => panic!("The value '{}' is not a power of two", value),
128		}
129	}
130	
131	#[inline(always)]
132	pub unsafe fn from_u32(value: u32) -> Option<PowerOfTwoThirtyTwoBit>
133	{
134		match value
135		{
136			0 => None,
137			
138			1 => Some(PowerOfTwoThirtyTwoBit::_1),
139			2 => Some(PowerOfTwoThirtyTwoBit::_2),
140			4 => Some(PowerOfTwoThirtyTwoBit::_4),
141			8 => Some(PowerOfTwoThirtyTwoBit::_8),
142			16 => Some(PowerOfTwoThirtyTwoBit::_16),
143			32 => Some(PowerOfTwoThirtyTwoBit::_32),
144			64 => Some(PowerOfTwoThirtyTwoBit::_64),
145			128 => Some(PowerOfTwoThirtyTwoBit::_128),
146			256 => Some(PowerOfTwoThirtyTwoBit::_256),
147			512 => Some(PowerOfTwoThirtyTwoBit::_512),
148			1024 => Some(PowerOfTwoThirtyTwoBit::_1024),
149			2048 => Some(PowerOfTwoThirtyTwoBit::_2048),
150			4096 => Some(PowerOfTwoThirtyTwoBit::_4096),
151			8192 => Some(PowerOfTwoThirtyTwoBit::_8192),
152			16384 => Some(PowerOfTwoThirtyTwoBit::_16384),
153			32768 => Some(PowerOfTwoThirtyTwoBit::_32768),
154			65536 => Some(PowerOfTwoThirtyTwoBit::_65536),
155			131072 => Some(PowerOfTwoThirtyTwoBit::_131072),
156			262144 => Some(PowerOfTwoThirtyTwoBit::_262144),
157			524288 => Some(PowerOfTwoThirtyTwoBit::_524288),
158			1048576 => Some(PowerOfTwoThirtyTwoBit::_1048576),
159			2097152 => Some(PowerOfTwoThirtyTwoBit::_2097152),
160			4194304 => Some(PowerOfTwoThirtyTwoBit::_4194304),
161			8388608 => Some(PowerOfTwoThirtyTwoBit::_8388608),
162			16777216 => Some(PowerOfTwoThirtyTwoBit::_16777216),
163			33554432 => Some(PowerOfTwoThirtyTwoBit::_33554432),
164			67108864 => Some(PowerOfTwoThirtyTwoBit::_67108864),
165			134217728 => Some(PowerOfTwoThirtyTwoBit::_134217728),
166			268435456 => Some(PowerOfTwoThirtyTwoBit::_268435456),
167			536870912 => Some(PowerOfTwoThirtyTwoBit::_536870912),
168			1073741824 => Some(PowerOfTwoThirtyTwoBit::_1073741824),
169			2147483648 => Some(PowerOfTwoThirtyTwoBit::_2147483648),
170			
171			_ => None,
172		}
173	}
174}