pub struct PowerOfTwo<T> { /* private fields */ }Expand description
Proof that a value is a power of two (2^k, k ≥ 0), for an unsigned
integer type T.
Representation is the exponent k, not the value — so the consuming
operations in PowerOfTwoOps are pure shifts and masks with nothing
recomputed per call, and a (future) big-integer backend carries a tiny
u32 proof regardless of its width. The field is private, so the
representation is an implementation detail. Consequently PowerOfTwo does
not deref to T; use get (which reconstructs 1 << k)
or exp.
PowerOfTwo<T> is always Copy (it stores only a u32), independent of
whether T is.
§Examples
use const_num_traits::{PowerOfTwo, PowerOfTwoOps};
let p = PowerOfTwo::<u32>::new(16).unwrap();
assert_eq!(p.exp(), 4);
assert_eq!(p.get(), 16);
assert_eq!(100u32.div_pow2(p), 6); // 100 / 16
assert_eq!(100u32.rem_pow2(p), 4); // 100 % 16
assert!(PowerOfTwo::<u32>::new(6).is_none());Implementations§
Source§impl PowerOfTwo<u8>
impl PowerOfTwo<u8>
Source§impl PowerOfTwo<u16>
impl PowerOfTwo<u16>
Source§impl PowerOfTwo<u32>
impl PowerOfTwo<u32>
Source§impl PowerOfTwo<u64>
impl PowerOfTwo<u64>
Source§impl PowerOfTwo<u128>
impl PowerOfTwo<u128>
Source§impl PowerOfTwo<usize>
impl PowerOfTwo<usize>
Source§impl<T> PowerOfTwo<T>
impl<T> PowerOfTwo<T>
Sourcepub const unsafe fn from_exp_unchecked(exp: u32) -> Self
pub const unsafe fn from_exp_unchecked(exp: u32) -> Self
Constructs the proof directly from an exponent, without checking.
§Safety
exp must be < T::BITS (so that 1 << exp is a valid power of two of
T). Passing a too-large exponent makes the consuming ops produce
nonsense or overflow.
Source§impl<T> PowerOfTwo<T>
impl<T> PowerOfTwo<T>
Sourcepub fn new_checked(value: T) -> Option<Self>where
T: IsPowerOfTwo + PrimBits,
pub fn new_checked(value: T) -> Option<Self>where
T: IsPowerOfTwo + PrimBits,
Safe constructor for any carrier, not just the primitives: Some iff
value is a power of two. Keeps the unsafe crate-internal; the
per-primitive new stays the const fast path.
Source§impl PowerOfTwo<u8>
impl PowerOfTwo<u8>
Source§impl PowerOfTwo<u16>
impl PowerOfTwo<u16>
Source§impl PowerOfTwo<u32>
impl PowerOfTwo<u32>
Source§impl PowerOfTwo<u64>
impl PowerOfTwo<u64>
Sourcepub const fn new(value: u64) -> Option<Self>
pub const fn new(value: u64) -> Option<Self>
Checked constructor: Some iff value is a power of two.
const fn on stable and nightly (delegates to the inherent
is_power_of_two/ilog2, both const since ≤ 1.67).
Examples found in repository?
20fn power_of_two() {
21 let radix = 256u64;
22 let p = PowerOfTwo::<u64>::new(radix).expect("256 is 2^8");
23
24 // `x % radix` and `x / radix` without a division instruction
25 assert_eq!(700u64.rem_pow2(p), 700 % 256); // & 0xFF
26 assert_eq!(700u64.div_pow2(p), 700 / 256); // >> 8
27
28 // align-up to the next multiple, branch-free; checked form for the edge
29 assert_eq!(700u64.next_multiple_of_pow2(p), 768);
30 assert_eq!(u64::MAX.checked_next_multiple_of_pow2(p), None);
31
32 // built once from a runtime value, spent in a loop with no per-iter recheck
33 let mut folded = 0u64;
34 for limb in [10u64, 600, 70_000] {
35 folded ^= limb.rem_pow2(p);
36 }
37 assert_eq!(folded, (10 ^ 600 ^ 70_000) & 0xFF);
38}Source§impl PowerOfTwo<u128>
impl PowerOfTwo<u128>
Source§impl PowerOfTwo<usize>
impl PowerOfTwo<usize>
Trait Implementations§
Source§impl<T> Clone for PowerOfTwo<T>
impl<T> Clone for PowerOfTwo<T>
impl<T> Copy for PowerOfTwo<T>
Source§impl TryFrom<u8> for PowerOfTwo<u8>
Checked construction by value; mirrors PowerOfTwo::new. (The
generic carrier path stays PowerOfTwo::new_checked.)
impl TryFrom<u8> for PowerOfTwo<u8>
Checked construction by value; mirrors PowerOfTwo::new. (The
generic carrier path stays PowerOfTwo::new_checked.)
Source§type Error = TypestateError
type Error = TypestateError
Source§impl TryFrom<u16> for PowerOfTwo<u16>
Checked construction by value; mirrors PowerOfTwo::new. (The
generic carrier path stays PowerOfTwo::new_checked.)
impl TryFrom<u16> for PowerOfTwo<u16>
Checked construction by value; mirrors PowerOfTwo::new. (The
generic carrier path stays PowerOfTwo::new_checked.)
Source§type Error = TypestateError
type Error = TypestateError
Source§impl TryFrom<u32> for PowerOfTwo<u32>
Checked construction by value; mirrors PowerOfTwo::new. (The
generic carrier path stays PowerOfTwo::new_checked.)
impl TryFrom<u32> for PowerOfTwo<u32>
Checked construction by value; mirrors PowerOfTwo::new. (The
generic carrier path stays PowerOfTwo::new_checked.)
Source§type Error = TypestateError
type Error = TypestateError
Source§impl TryFrom<u64> for PowerOfTwo<u64>
Checked construction by value; mirrors PowerOfTwo::new. (The
generic carrier path stays PowerOfTwo::new_checked.)
impl TryFrom<u64> for PowerOfTwo<u64>
Checked construction by value; mirrors PowerOfTwo::new. (The
generic carrier path stays PowerOfTwo::new_checked.)
Source§type Error = TypestateError
type Error = TypestateError
Source§impl TryFrom<u128> for PowerOfTwo<u128>
Checked construction by value; mirrors PowerOfTwo::new. (The
generic carrier path stays PowerOfTwo::new_checked.)
impl TryFrom<u128> for PowerOfTwo<u128>
Checked construction by value; mirrors PowerOfTwo::new. (The
generic carrier path stays PowerOfTwo::new_checked.)
Source§type Error = TypestateError
type Error = TypestateError
Source§impl TryFrom<usize> for PowerOfTwo<usize>
Checked construction by value; mirrors PowerOfTwo::new. (The
generic carrier path stays PowerOfTwo::new_checked.)
impl TryFrom<usize> for PowerOfTwo<usize>
Checked construction by value; mirrors PowerOfTwo::new. (The
generic carrier path stays PowerOfTwo::new_checked.)