dynamic_weighted_index/
numeric.rs1pub trait FloatingPointParts {
2 const BITS_MANTISSA: usize;
3 const BITS_EXPONENT: usize;
4 type BitsType;
5
6 fn get_mantissa(&self) -> Self::BitsType;
7 fn get_exponent(&self) -> Self::BitsType;
8 fn get_sign(&self) -> bool;
9
10 fn compose(mantissa: Self::BitsType, exponent: Self::BitsType, sign: bool) -> Self;
11}
12
13macro_rules! fpp_impl {
14 () => {
15 fn get_mantissa(&self) -> Self::BitsType {
16 self.to_bits() & ((1 << Self::BITS_MANTISSA) - 1)
17 }
18
19 fn get_exponent(&self) -> Self::BitsType {
20 (self.to_bits() >> Self::BITS_MANTISSA) & ((1 << Self::BITS_EXPONENT) - 1)
21 }
22
23 fn get_sign(&self) -> bool {
24 (self.to_bits() >> (Self::BITS_EXPONENT + Self::BITS_MANTISSA)) == 1
25 }
26
27 fn compose(mantissa: Self::BitsType, exponent: Self::BitsType, sign: bool) -> Self {
28 Self::from_bits(
29 mantissa
30 | (exponent << Self::BITS_MANTISSA)
31 | ((sign as Self::BitsType) << (Self::BITS_EXPONENT + Self::BITS_MANTISSA)),
32 )
33 }
34 };
35}
36
37impl FloatingPointParts for f32 {
38 const BITS_MANTISSA: usize = 23;
39 const BITS_EXPONENT: usize = 8;
40 type BitsType = u32;
41
42 fpp_impl!();
43}
44
45impl FloatingPointParts for f64 {
46 const BITS_MANTISSA: usize = 52;
47 const BITS_EXPONENT: usize = 11;
48 type BitsType = u64;
49
50 fpp_impl!();
51}