Skip to content

Commit 4df53c6

Browse files
committed
Add floating point deconstruction helpers
1 parent da53b70 commit 4df53c6

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
@@ -12,7 +12,7 @@ macro_rules! add {
1212

1313
let bits = Wrapping(<$ty>::bits() as <$ty as Float>::Int);
1414
let significand_bits = Wrapping(<$ty>::significand_bits() as <$ty as Float>::Int);
15-
let exponent_bits = bits - significand_bits - one;
15+
let exponent_bits = Wrapping(<$ty>::exponent_bits() as <$ty as Float>::Int);
1616
let max_exponent = (one << exponent_bits.0 as usize) - one;
1717

1818
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 {
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

@@ -19,6 +22,15 @@ pub trait Float: Sized {
1922
/// Returns a `Self::Int` transmuted back to `Self`
2023
fn from_repr(a: Self::Int) -> Self;
2124

25+
/// Returns the sign bit of `self`
26+
fn sign(self) -> bool;
27+
28+
/// Returns the exponent portion of `self`, shifted to the right
29+
fn exponent(self) -> Self::Int;
30+
31+
/// Returns the significand portion of `self`
32+
fn significand(self) -> Self::Int;
33+
2234
/// Returns (normalized exponent, normalized significand)
2335
fn normalize(significand: Self::Int) -> (i32, Self::Int);
2436
}
@@ -28,6 +40,9 @@ impl Float for f32 {
2840
fn bits() -> u32 {
2941
32
3042
}
43+
fn exponent_bits() -> u32 {
44+
8
45+
}
3146
fn significand_bits() -> u32 {
3247
23
3348
}
@@ -37,6 +52,16 @@ impl Float for f32 {
3752
fn from_repr(a: Self::Int) -> Self {
3853
unsafe { mem::transmute(a) }
3954
}
55+
fn sign(self) -> bool {
56+
(self.repr() & 1 << Self::bits()) != 0
57+
}
58+
fn exponent(self) -> Self::Int {
59+
self.repr() >> Self::significand_bits()
60+
& ((1 << Self::exponent_bits()) - 1)
61+
}
62+
fn significand(self) -> Self::Int {
63+
self.repr() & ((1 << Self::significand_bits()) - 1)
64+
}
4065
fn normalize(significand: Self::Int) -> (i32, Self::Int) {
4166
let shift = significand.leading_zeros()
4267
.wrapping_sub((1u32 << Self::significand_bits()).leading_zeros());
@@ -48,6 +73,9 @@ impl Float for f64 {
4873
fn bits() -> u32 {
4974
64
5075
}
76+
fn exponent_bits() -> u32 {
77+
11
78+
}
5179
fn significand_bits() -> u32 {
5280
52
5381
}
@@ -57,6 +85,16 @@ impl Float for f64 {
5785
fn from_repr(a: Self::Int) -> Self {
5886
unsafe { mem::transmute(a) }
5987
}
88+
fn sign(self) -> bool {
89+
(self.repr() & 1 << Self::bits()) != 0
90+
}
91+
fn exponent(self) -> Self::Int {
92+
self.repr() >> Self::significand_bits()
93+
& ((1 << Self::exponent_bits()) - 1)
94+
}
95+
fn significand(self) -> Self::Int {
96+
self.repr() & ((1 << Self::significand_bits()) - 1)
97+
}
6098
fn normalize(significand: Self::Int) -> (i32, Self::Int) {
6199
let shift = significand.leading_zeros()
62100
.wrapping_sub((1u64 << Self::significand_bits()).leading_zeros());

0 commit comments

Comments
 (0)