Skip to content

Commit 9bdedec

Browse files
committed
Refactor float builtins to use associated consts
1 parent c9a2618 commit 9bdedec

File tree

4 files changed

+56
-80
lines changed

4 files changed

+56
-80
lines changed

src/float/add.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ macro_rules! add {
1010
let one = Wrapping(1 as <$ty as Float>::Int);
1111
let zero = Wrapping(0 as <$ty as Float>::Int);
1212

13-
let bits = Wrapping(<$ty>::bits() as <$ty as Float>::Int);
14-
let significand_bits = Wrapping(<$ty>::significand_bits() as <$ty as Float>::Int);
13+
let bits = Wrapping(<$ty>::BITS as <$ty as Float>::Int);
14+
let significand_bits = Wrapping(<$ty>::SIGNIFICAND_BITS as <$ty as Float>::Int);
1515
let exponent_bits = bits - significand_bits - one;
1616
let max_exponent = (one << exponent_bits.0 as usize) - one;
1717

src/float/conv.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ macro_rules! int_to_float {
88
return 0.0
99
}
1010

11-
let mant_dig = <$fty>::significand_bits() + 1;
12-
let exponent_bias = <$fty>::exponent_bias();
11+
let mant_dig = <$fty>::SIGNIFICAND_BITS + 1;
12+
let exponent_bias = <$fty>::EXPONENT_BIAS;
1313

1414
let n = <$ity>::BITS;
1515
let (s, a) = i.extract_sign();
@@ -145,9 +145,9 @@ macro_rules! float_to_int {
145145
let fixint_bits = <$ity>::BITS as usize;
146146
let fixint_unsigned = fixint_min == 0;
147147

148-
let sign_bit = <$fty>::sign_mask();
149-
let significand_bits = <$fty>::significand_bits() as usize;
150-
let exponent_bias = <$fty>::exponent_bias() as usize;
148+
let sign_bit = <$fty>::SIGN_MASK;
149+
let significand_bits = <$fty>::SIGNIFICAND_BITS as usize;
150+
let exponent_bias = <$fty>::EXPONENT_BIAS as usize;
151151
//let exponent_max = <$fty>::exponent_max() as usize;
152152

153153
// Break a into sign, exponent, significand
@@ -157,7 +157,7 @@ macro_rules! float_to_int {
157157
// this is used to work around -1 not being available for unsigned
158158
let sign = if (a_rep & sign_bit) == 0 { Sign::Positive } else { Sign::Negative };
159159
let mut exponent = (a_abs >> significand_bits) as usize;
160-
let significand = (a_abs & <$fty>::significand_mask()) | <$fty>::implicit_bit();
160+
let significand = (a_abs & <$fty>::SIGNIFICAND_MASK) | <$fty>::IMPLICIT_BIT;
161161

162162
// if < 1 or unsigned & negative
163163
if exponent < exponent_bias ||

src/float/mod.rs

Lines changed: 46 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use core::mem;
22

3+
use super::int::Int;
4+
35
pub mod conv;
46
pub mod add;
57
pub mod pow;
@@ -8,39 +10,34 @@ pub mod sub;
810
/// Trait for some basic operations on floats
911
pub trait Float: Sized + Copy {
1012
/// A uint of the same with as the float
11-
type Int;
13+
type Int: Int;
1214

13-
/// Returns the bitwidth of the float type
14-
fn bits() -> u32;
15+
/// The bitwidth of the float type
16+
const BITS: u32;
1517

16-
/// Returns the bitwidth of the significand
17-
fn significand_bits() -> u32;
18+
/// The bitwidth of the significand
19+
const SIGNIFICAND_BITS: u32;
1820

19-
/// Returns the bitwidth of the exponent
20-
fn exponent_bits() -> u32 {
21-
Self::bits() - Self::significand_bits() - 1
22-
}
23-
/// Returns the maximum value of the exponent
24-
fn exponent_max() -> u32 {
25-
(1 << Self::exponent_bits()) - 1
26-
}
21+
/// The bitwidth of the exponent
22+
const EXPONENT_BITS: u32 = Self::BITS - Self::SIGNIFICAND_BITS - 1;
2723

28-
/// Returns the exponent bias value
29-
fn exponent_bias() -> u32 {
30-
Self::exponent_max() >> 1
31-
}
24+
/// The maximum value of the exponent
25+
const EXPONENT_MAX: u32 = (1 << Self::EXPONENT_BITS) - 1;
26+
27+
/// The exponent bias value
28+
const EXPONENT_BIAS: u32 = Self::EXPONENT_MAX >> 1;
3229

33-
/// Returns a mask for the sign bit
34-
fn sign_mask() -> Self::Int;
30+
/// A mask for the sign bit
31+
const SIGN_MASK: Self::Int;
3532

36-
/// Returns a mask for the significand
37-
fn significand_mask() -> Self::Int;
33+
/// A mask for the significand
34+
const SIGNIFICAND_MASK: Self::Int;
3835

39-
// Returns the implicit bit of the float format
40-
fn implicit_bit() -> Self::Int;
36+
// The implicit bit of the float format
37+
const IMPLICIT_BIT: Self::Int;
4138

42-
/// Returns a mask for the exponent
43-
fn exponent_mask() -> Self::Int;
39+
/// A mask for the exponent
40+
const EXPONENT_MASK: Self::Int;
4441

4542
/// Returns `self` transmuted to `Self::Int`
4643
fn repr(self) -> Self::Int;
@@ -65,24 +62,14 @@ pub trait Float: Sized + Copy {
6562
// https://github.com/rust-lang/rfcs/issues/1424
6663
impl Float for f32 {
6764
type Int = u32;
68-
fn bits() -> u32 {
69-
32
70-
}
71-
fn significand_bits() -> u32 {
72-
23
73-
}
74-
fn implicit_bit() -> Self::Int {
75-
1 << Self::significand_bits()
76-
}
77-
fn sign_mask() -> Self::Int {
78-
1 << (Self::bits() - 1)
79-
}
80-
fn significand_mask() -> Self::Int {
81-
(1 << Self::significand_bits()) - 1
82-
}
83-
fn exponent_mask() -> Self::Int {
84-
!(Self::sign_mask() | Self::significand_mask())
85-
}
65+
const BITS: u32 = 32;
66+
const SIGNIFICAND_BITS: u32 = 23;
67+
68+
const SIGN_MASK: Self::Int = 1 << (Self::BITS - 1);
69+
const SIGNIFICAND_MASK: Self::Int = (1 << Self::SIGNIFICAND_BITS) - 1;
70+
const IMPLICIT_BIT: Self::Int = 1 << Self::SIGNIFICAND_BITS;
71+
const EXPONENT_MASK: Self::Int = !(Self::SIGN_MASK | Self::SIGNIFICAND_MASK);
72+
8673
fn repr(self) -> Self::Int {
8774
unsafe { mem::transmute(self) }
8875
}
@@ -98,37 +85,26 @@ impl Float for f32 {
9885
unsafe { mem::transmute(a) }
9986
}
10087
fn from_parts(sign: bool, exponent: Self::Int, significand: Self::Int) -> Self {
101-
Self::from_repr(((sign as Self::Int) << (Self::bits() - 1)) |
102-
((exponent << Self::significand_bits()) & Self::exponent_mask()) |
103-
(significand & Self::significand_mask()))
88+
Self::from_repr(((sign as Self::Int) << (Self::BITS - 1)) |
89+
((exponent << Self::SIGNIFICAND_BITS) & Self::EXPONENT_MASK) |
90+
(significand & Self::SIGNIFICAND_MASK))
10491
}
10592
fn normalize(significand: Self::Int) -> (i32, Self::Int) {
10693
let shift = significand.leading_zeros()
107-
.wrapping_sub((1u32 << Self::significand_bits()).leading_zeros());
94+
.wrapping_sub((1u32 << Self::SIGNIFICAND_BITS).leading_zeros());
10895
(1i32.wrapping_sub(shift as i32), significand << shift as Self::Int)
10996
}
11097
}
11198
impl Float for f64 {
11299
type Int = u64;
113-
fn bits() -> u32 {
114-
64
115-
}
116-
fn significand_bits() -> u32 {
117-
52
118-
}
119-
// Returns the implicit bit of the float format
120-
fn implicit_bit() -> Self::Int {
121-
1 << Self::significand_bits()
122-
}
123-
fn sign_mask() -> Self::Int {
124-
1 << (Self::bits() - 1)
125-
}
126-
fn significand_mask() -> Self::Int {
127-
(1 << Self::significand_bits()) - 1
128-
}
129-
fn exponent_mask() -> Self::Int {
130-
!(Self::sign_mask() | Self::significand_mask())
131-
}
100+
const BITS: u32 = 64;
101+
const SIGNIFICAND_BITS: u32 = 52;
102+
103+
const SIGN_MASK: Self::Int = 1 << (Self::BITS - 1);
104+
const SIGNIFICAND_MASK: Self::Int = (1 << Self::SIGNIFICAND_BITS) - 1;
105+
const IMPLICIT_BIT: Self::Int = 1 << Self::SIGNIFICAND_BITS;
106+
const EXPONENT_MASK: Self::Int = !(Self::SIGN_MASK | Self::SIGNIFICAND_MASK);
107+
132108
fn repr(self) -> Self::Int {
133109
unsafe { mem::transmute(self) }
134110
}
@@ -144,13 +120,13 @@ impl Float for f64 {
144120
unsafe { mem::transmute(a) }
145121
}
146122
fn from_parts(sign: bool, exponent: Self::Int, significand: Self::Int) -> Self {
147-
Self::from_repr(((sign as Self::Int) << (Self::bits() - 1)) |
148-
((exponent << Self::significand_bits()) & Self::exponent_mask()) |
149-
(significand & Self::significand_mask()))
123+
Self::from_repr(((sign as Self::Int) << (Self::BITS - 1)) |
124+
((exponent << Self::SIGNIFICAND_BITS) & Self::EXPONENT_MASK) |
125+
(significand & Self::SIGNIFICAND_MASK))
150126
}
151127
fn normalize(significand: Self::Int) -> (i32, Self::Int) {
152128
let shift = significand.leading_zeros()
153-
.wrapping_sub((1u64 << Self::significand_bits()).leading_zeros());
129+
.wrapping_sub((1u64 << Self::SIGNIFICAND_BITS).leading_zeros());
154130
(1i32.wrapping_sub(shift as i32), significand << shift as Self::Int)
155131
}
156132
}

src/float/sub.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ use float::Float;
33
intrinsics! {
44
#[arm_aeabi_alias = __aeabi_fsub]
55
pub extern "C" fn __subsf3(a: f32, b: f32) -> f32 {
6-
a + f32::from_repr(b.repr() ^ f32::sign_mask())
6+
a + f32::from_repr(b.repr() ^ f32::SIGN_MASK)
77
}
88

99
#[arm_aeabi_alias = __aeabi_dsub]
1010
pub extern "C" fn __subdf3(a: f64, b: f64) -> f64 {
11-
a + f64::from_repr(b.repr() ^ f64::sign_mask())
11+
a + f64::from_repr(b.repr() ^ f64::SIGN_MASK)
1212
}
1313
}

0 commit comments

Comments
 (0)