Skip to content

Commit f7f041f

Browse files
matticoMatthew Ickstadt
authored and
Matthew Ickstadt
committed
Add floating point deconstruction helpers
1 parent 69e93de commit f7f041f

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

src/float/add.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ macro_rules! add {
1414

1515
let bits = Wrapping(<$ty>::bits() as <$ty as Float>::Int);
1616
let significand_bits = Wrapping(<$ty>::significand_bits() as <$ty as Float>::Int);
17-
let exponent_bits = bits - significand_bits - one;
17+
let exponent_bits = Wrapping(<$ty>::exponent_bits() as <$ty as Float>::Int);
1818
let max_exponent = (one << exponent_bits.0 as usize) - one;
1919

2020
let implicit_bit = one << significand_bits.0 as usize;

src/float/mod.rs

+38
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ pub trait Float: Sized + Copy {
1010
/// Returns the bitwidth of the float type
1111
fn bits() -> u32;
1212

13+
/// Returns the bitwidth of the exponent
14+
fn exponent_bits() -> u32;
15+
1316
/// Returns the bitwidth of the significand
1417
fn significand_bits() -> u32;
1518

@@ -25,6 +28,15 @@ pub trait Float: Sized + Copy {
2528
/// Returns a `Self::Int` transmuted back to `Self`
2629
fn from_repr(a: Self::Int) -> Self;
2730

31+
/// Returns the sign bit of `self`
32+
fn sign(self) -> bool;
33+
34+
/// Returns the exponent portion of `self`, shifted to the right
35+
fn exponent(self) -> Self::Int;
36+
37+
/// Returns the significand portion of `self`
38+
fn significand(self) -> Self::Int;
39+
2840
/// Returns (normalized exponent, normalized significand)
2941
fn normalize(significand: Self::Int) -> (i32, Self::Int);
3042
}
@@ -34,6 +46,9 @@ impl Float for f32 {
3446
fn bits() -> u32 {
3547
32
3648
}
49+
fn exponent_bits() -> u32 {
50+
8
51+
}
3752
fn significand_bits() -> u32 {
3853
23
3954
}
@@ -51,6 +66,16 @@ impl Float for f32 {
5166
fn from_repr(a: Self::Int) -> Self {
5267
unsafe { mem::transmute(a) }
5368
}
69+
fn sign(self) -> bool {
70+
(self.repr() & 1 << Self::bits()) != 0
71+
}
72+
fn exponent(self) -> Self::Int {
73+
self.repr() >> Self::significand_bits()
74+
& ((1 << Self::exponent_bits()) - 1)
75+
}
76+
fn significand(self) -> Self::Int {
77+
self.repr() & ((1 << Self::significand_bits()) - 1)
78+
}
5479
fn normalize(significand: Self::Int) -> (i32, Self::Int) {
5580
let shift = significand.leading_zeros()
5681
.wrapping_sub((1u32 << Self::significand_bits()).leading_zeros());
@@ -62,6 +87,9 @@ impl Float for f64 {
6287
fn bits() -> u32 {
6388
64
6489
}
90+
fn exponent_bits() -> u32 {
91+
11
92+
}
6593
fn significand_bits() -> u32 {
6694
52
6795
}
@@ -79,6 +107,16 @@ impl Float for f64 {
79107
fn from_repr(a: Self::Int) -> Self {
80108
unsafe { mem::transmute(a) }
81109
}
110+
fn sign(self) -> bool {
111+
(self.repr() & 1 << Self::bits()) != 0
112+
}
113+
fn exponent(self) -> Self::Int {
114+
self.repr() >> Self::significand_bits()
115+
& ((1 << Self::exponent_bits()) - 1)
116+
}
117+
fn significand(self) -> Self::Int {
118+
self.repr() & ((1 << Self::significand_bits()) - 1)
119+
}
82120
fn normalize(significand: Self::Int) -> (i32, Self::Int) {
83121
let shift = significand.leading_zeros()
84122
.wrapping_sub((1u64 << Self::significand_bits()).leading_zeros());

0 commit comments

Comments
 (0)