Skip to content

Commit 2332290

Browse files
committed
Fix *_exponent representation
1 parent cc28bb1 commit 2332290

File tree

2 files changed

+17
-17
lines changed

2 files changed

+17
-17
lines changed

src/float/add.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ macro_rules! add {
8080
}
8181

8282
// Extract the exponent and significand from the (possibly swapped) a and b.
83-
let mut a_exponent = a_rep >> significand_bits.0 as usize & max_exponent;
84-
let mut b_exponent = b_rep >> significand_bits.0 as usize & max_exponent;
83+
let mut a_exponent = Wrapping((a_rep >> significand_bits.0 as usize & max_exponent).0 as i32);
84+
let mut b_exponent = Wrapping((b_rep >> significand_bits.0 as usize & max_exponent).0 as i32);
8585
let mut a_significand = a_rep & significand_mask;
8686
let mut b_significand = b_rep & significand_mask;
8787

@@ -111,7 +111,7 @@ macro_rules! add {
111111

112112
// Shift the significand of b by the difference in exponents, with a sticky
113113
// bottom bit to get rounding correct.
114-
let align = a_exponent - b_exponent;
114+
let align = Wrapping((a_exponent - b_exponent).0 as <$ty as Float>::Int);
115115
if align.0 != 0 {
116116
if align < bits {
117117
let sticky = ((b_significand << (bits - align).0 as usize).0 != 0) as <$ty as Float>::Int;
@@ -131,7 +131,7 @@ macro_rules! add {
131131
// and adjust the exponent:
132132
if a_significand < implicit_bit << 3 {
133133
let shift = (a_significand.0.leading_zeros()
134-
- (implicit_bit << 3).0.leading_zeros()) as <$ty as Float>::Int;
134+
- (implicit_bit << 3).0.leading_zeros()) as i32;
135135
a_significand <<= shift as usize;
136136
a_exponent -= Wrapping(shift);
137137
}
@@ -143,38 +143,38 @@ macro_rules! add {
143143
if (a_significand & implicit_bit << 4).0 != 0 {
144144
let sticky = ((a_significand & one).0 != 0) as <$ty as Float>::Int;
145145
a_significand = a_significand >> 1 | Wrapping(sticky);
146-
a_exponent += one;
146+
a_exponent += Wrapping(1);
147147
}
148148
}
149149

150150
// If we have overflowed the type, return +/- infinity:
151-
if a_exponent >= max_exponent {
151+
if a_exponent >= Wrapping(max_exponent.0 as i32) {
152152
return (<$ty>::from_repr((inf_rep | result_sign).0));
153153
}
154154

155155
if a_exponent.0 <= 0 {
156156
// Result is denormal before rounding; the exponent is zero and we
157157
// need to shift the significand.
158-
let shift = one - a_exponent;
158+
let shift = Wrapping((Wrapping(1) - a_exponent).0 as <$ty as Float>::Int);
159159
let sticky = ((a_significand << (bits - shift).0 as usize).0 != 0) as <$ty as Float>::Int;
160160
a_significand = a_significand >> shift.0 as usize | Wrapping(sticky);
161-
a_exponent = zero;
161+
a_exponent = Wrapping(0);
162162
}
163163

164164
// Low three bits are round, guard, and sticky.
165-
let round_guard_sticky = a_significand & Wrapping(0x7);
165+
let round_guard_sticky: i32 = (a_significand.0 & 0x7) as i32;
166166

167167
// Shift the significand into place, and mask off the implicit bit.
168168
let mut result = a_significand >> 3 & significand_mask;
169169

170170
// Insert the exponent and sign.
171-
result |= a_exponent << significand_bits.0 as usize;
171+
result |= Wrapping(a_exponent.0 as <$ty as Float>::Int) << significand_bits.0 as usize;
172172
result |= result_sign;
173173

174174
// Final rounding. The result may overflow to infinity, but that is the
175175
// correct result in that case.
176-
if round_guard_sticky.0 > 0x4 { result += one; }
177-
if round_guard_sticky.0 == 0x4 { result += result & one; }
176+
if round_guard_sticky > 0x4 { result += one; }
177+
if round_guard_sticky == 0x4 { result += result & one; }
178178
return (<$ty>::from_repr(result.0));
179179
}
180180
}

src/float/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ pub trait Float: Sized {
2020
fn from_repr(a: Self::Int) -> Self;
2121

2222
/// Returns (normalized exponent, normalized significand)
23-
fn normalize(significand: Self::Int) -> (Self::Int, Self::Int);
23+
fn normalize(significand: Self::Int) -> (i32, Self::Int);
2424
}
2525

2626
impl Float for f32 {
@@ -37,10 +37,10 @@ impl Float for f32 {
3737
fn from_repr(a: Self::Int) -> Self {
3838
unsafe { mem::transmute(a) }
3939
}
40-
fn normalize(significand: Self::Int) -> (Self::Int, Self::Int) {
40+
fn normalize(significand: Self::Int) -> (i32, Self::Int) {
4141
let shift = significand.leading_zeros()
4242
.wrapping_sub((1u32 << Self::significand_bits()).leading_zeros());
43-
(1u32.wrapping_sub(shift as Self::Int), significand << shift as Self::Int)
43+
(1i32.wrapping_sub(shift as i32), significand << shift as Self::Int)
4444
}
4545
}
4646
impl Float for f64 {
@@ -57,9 +57,9 @@ impl Float for f64 {
5757
fn from_repr(a: Self::Int) -> Self {
5858
unsafe { mem::transmute(a) }
5959
}
60-
fn normalize(significand: Self::Int) -> (Self::Int, Self::Int) {
60+
fn normalize(significand: Self::Int) -> (i32, Self::Int) {
6161
let shift = significand.leading_zeros()
6262
.wrapping_sub((1u64 << Self::significand_bits()).leading_zeros());
63-
(1u64.wrapping_sub(shift as Self::Int), significand << shift as Self::Int)
63+
(1i32.wrapping_sub(shift as i32), significand << shift as Self::Int)
6464
}
6565
}

0 commit comments

Comments
 (0)