Skip to content

Commit 520e2a0

Browse files
authored
Merge pull request #738 from tgross35/float-trait-renaming
Float trait: shorten prefixes, rename EXP_MAX to EXP_SAT
2 parents ff0ca1f + 73b6429 commit 520e2a0

File tree

12 files changed

+126
-128
lines changed

12 files changed

+126
-128
lines changed

examples/intrinsics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -640,7 +640,7 @@ fn run() {
640640
fn something_with_a_dtor(f: &dyn Fn()) {
641641
struct A<'a>(&'a (dyn Fn() + 'a));
642642

643-
impl<'a> Drop for A<'a> {
643+
impl Drop for A<'_> {
644644
fn drop(&mut self) {
645645
(self.0)();
646646
}

src/float/add.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ where
1313
let zero = F::Int::ZERO;
1414

1515
let bits = F::BITS.cast();
16-
let significand_bits = F::SIGNIFICAND_BITS;
17-
let max_exponent = F::EXPONENT_MAX;
16+
let significand_bits = F::SIG_BITS;
17+
let max_exponent = F::EXP_SAT;
1818

1919
let implicit_bit = F::IMPLICIT_BIT;
20-
let significand_mask = F::SIGNIFICAND_MASK;
20+
let significand_mask = F::SIG_MASK;
2121
let sign_bit = F::SIGN_MASK as F::Int;
2222
let abs_mask = sign_bit - one;
23-
let exponent_mask = F::EXPONENT_MASK;
23+
let exponent_mask = F::EXP_MASK;
2424
let inf_rep = exponent_mask;
2525
let quiet_bit = implicit_bit >> 1;
2626
let qnan_rep = exponent_mask | quiet_bit;
@@ -143,9 +143,9 @@ where
143143

144144
// If the addition carried up, we need to right-shift the result and
145145
// adjust the exponent:
146-
if a_significand & implicit_bit << 4 != MinInt::ZERO {
146+
if a_significand & (implicit_bit << 4) != MinInt::ZERO {
147147
let sticky = F::Int::from_bool(a_significand & one != MinInt::ZERO);
148-
a_significand = a_significand >> 1 | sticky;
148+
a_significand = (a_significand >> 1) | sticky;
149149
a_exponent += 1;
150150
}
151151
}
@@ -161,7 +161,7 @@ where
161161
let shift = (1 - a_exponent).cast();
162162
let sticky =
163163
F::Int::from_bool((a_significand << bits.wrapping_sub(shift).cast()) != MinInt::ZERO);
164-
a_significand = a_significand >> shift.cast() | sticky;
164+
a_significand = (a_significand >> shift.cast()) | sticky;
165165
a_exponent = 0;
166166
}
167167

@@ -170,7 +170,7 @@ where
170170
let round_guard_sticky: i32 = a_significand_i32 & 0x7;
171171

172172
// Shift the significand into place, and mask off the implicit bit.
173-
let mut result = a_significand >> 3 & significand_mask;
173+
let mut result = (a_significand >> 3) & significand_mask;
174174

175175
// Insert the exponent and sign.
176176
result |= a_exponent.cast() << significand_bits;

src/float/cmp.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ fn cmp<F: Float>(a: F, b: F) -> Result {
3838

3939
let sign_bit = F::SIGN_MASK as F::Int;
4040
let abs_mask = sign_bit - one;
41-
let exponent_mask = F::EXPONENT_MASK;
41+
let exponent_mask = F::EXP_MASK;
4242
let inf_rep = exponent_mask;
4343

4444
let a_rep = a.to_bits();
@@ -87,7 +87,7 @@ fn unord<F: Float>(a: F, b: F) -> bool {
8787

8888
let sign_bit = F::SIGN_MASK as F::Int;
8989
let abs_mask = sign_bit - one;
90-
let exponent_mask = F::EXPONENT_MASK;
90+
let exponent_mask = F::EXP_MASK;
9191
let inf_rep = exponent_mask;
9292

9393
let a_rep = a.to_bits();

src/float/conv.rs

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ mod int_to_float {
3232
/// Usually 1 is subtracted from this function's result, so that a mantissa with the implicit
3333
/// bit set can be added back later.
3434
fn exp<I: Int, F: Float<Int: CastFrom<u32>>>(n: u32) -> F::Int {
35-
F::Int::cast_from(F::EXPONENT_BIAS - 1 + I::BITS - n)
35+
F::Int::cast_from(F::EXP_BIAS - 1 + I::BITS - n)
3636
}
3737

3838
/// Adjust a mantissa with dropped bits to perform correct rounding.
@@ -42,7 +42,7 @@ mod int_to_float {
4242
fn m_adj<F: Float>(m_base: F::Int, dropped_bits: F::Int) -> F::Int {
4343
// Branchlessly extract a `1` if rounding up should happen, 0 otherwise
4444
// This accounts for rounding to even.
45-
let adj = (dropped_bits - (dropped_bits >> (F::BITS - 1) & !m_base)) >> (F::BITS - 1);
45+
let adj = (dropped_bits - ((dropped_bits >> (F::BITS - 1)) & !m_base)) >> (F::BITS - 1);
4646

4747
// Add one when we need to round up. Break ties to even.
4848
m_base + adj
@@ -54,17 +54,17 @@ mod int_to_float {
5454
/// value to cancel it out.
5555
fn repr<F: Float>(e: F::Int, m: F::Int) -> F::Int {
5656
// + rather than | so the mantissa can overflow into the exponent
57-
(e << F::SIGNIFICAND_BITS) + m
57+
(e << F::SIG_BITS) + m
5858
}
5959

6060
/// Shift distance from a left-aligned integer to a smaller float.
6161
fn shift_f_lt_i<I: Int, F: Float>() -> u32 {
62-
(I::BITS - F::BITS) + F::EXPONENT_BITS
62+
(I::BITS - F::BITS) + F::EXP_BITS
6363
}
6464

6565
/// Shift distance from an integer with `n` leading zeros to a smaller float.
6666
fn shift_f_gt_i<I: Int, F: Float>(n: u32) -> u32 {
67-
F::SIGNIFICAND_BITS - I::BITS + 1 + n
67+
F::SIG_BITS - I::BITS + 1 + n
6868
}
6969

7070
/// Perform a signed operation as unsigned, then add the sign back.
@@ -85,9 +85,9 @@ mod int_to_float {
8585
}
8686
let n = i.leading_zeros();
8787
// Mantissa with implicit bit set (significant bits)
88-
let m_base = (i << n) >> f32::EXPONENT_BITS;
88+
let m_base = (i << n) >> f32::EXP_BITS;
8989
// Bits that will be dropped (insignificant bits)
90-
let adj = (i << n) << (f32::SIGNIFICAND_BITS + 1);
90+
let adj = (i << n) << (f32::SIG_BITS + 1);
9191
let m = m_adj::<f32>(m_base, adj);
9292
let e = exp::<u32, f32>(n) - 1;
9393
repr::<f32>(e, m)
@@ -116,7 +116,7 @@ mod int_to_float {
116116
let m = (i as u64) << (shift_f_gt_i::<u32, f128>(n) - 64);
117117
let e = exp::<u32, f128>(n) as u64 - 1;
118118
// High 64 bits of f128 representation.
119-
let h = (e << (f128::SIGNIFICAND_BITS - 64)) + m;
119+
let h = (e << (f128::SIG_BITS - 64)) + m;
120120

121121
// Shift back to the high bits, the rest of the mantissa will always be 0.
122122
(h as u128) << 64
@@ -128,8 +128,8 @@ mod int_to_float {
128128
// Mantissa with implicit bit set
129129
let m_base: u32 = (i_m >> shift_f_lt_i::<u64, f32>()) as u32;
130130
// The entire lower half of `i` will be truncated (masked portion), plus the
131-
// next `EXPONENT_BITS` bits.
132-
let adj = (i_m >> f32::EXPONENT_BITS | i_m & 0xFFFF) as u32;
131+
// next `EXP_BITS` bits.
132+
let adj = ((i_m >> f32::EXP_BITS) | i_m & 0xFFFF) as u32;
133133
let m = m_adj::<f32>(m_base, adj);
134134
let e = if i == 0 { 0 } else { exp::<u64, f32>(n) - 1 };
135135
repr::<f32>(e, m)
@@ -141,8 +141,8 @@ mod int_to_float {
141141
}
142142
let n = i.leading_zeros();
143143
// Mantissa with implicit bit set
144-
let m_base = (i << n) >> f64::EXPONENT_BITS;
145-
let adj = (i << n) << (f64::SIGNIFICAND_BITS + 1);
144+
let m_base = (i << n) >> f64::EXP_BITS;
145+
let adj = (i << n) << (f64::SIG_BITS + 1);
146146
let m = m_adj::<f64>(m_base, adj);
147147
let e = exp::<u64, f64>(n) - 1;
148148
repr::<f64>(e, m)
@@ -167,7 +167,7 @@ mod int_to_float {
167167

168168
// Within the upper `F::BITS`, everything except for the signifcand
169169
// gets truncated
170-
let d1: u32 = (i_m >> (u128::BITS - f32::BITS - f32::SIGNIFICAND_BITS - 1)).cast();
170+
let d1: u32 = (i_m >> (u128::BITS - f32::BITS - f32::SIG_BITS - 1)).cast();
171171

172172
// The entire rest of `i_m` gets truncated. Zero the upper `F::BITS` then just
173173
// check if it is nonzero.
@@ -186,8 +186,8 @@ mod int_to_float {
186186
// Mantissa with implicit bit set
187187
let m_base: u64 = (i_m >> shift_f_lt_i::<u128, f64>()) as u64;
188188
// The entire lower half of `i` will be truncated (masked portion), plus the
189-
// next `EXPONENT_BITS` bits.
190-
let adj = (i_m >> f64::EXPONENT_BITS | i_m & 0xFFFF_FFFF) as u64;
189+
// next `EXP_BITS` bits.
190+
let adj = ((i_m >> f64::EXP_BITS) | i_m & 0xFFFF_FFFF) as u64;
191191
let m = m_adj::<f64>(m_base, adj);
192192
let e = if i == 0 { 0 } else { exp::<u128, f64>(n) - 1 };
193193
repr::<f64>(e, m)
@@ -200,8 +200,8 @@ mod int_to_float {
200200
}
201201
let n = i.leading_zeros();
202202
// Mantissa with implicit bit set
203-
let m_base = (i << n) >> f128::EXPONENT_BITS;
204-
let adj = (i << n) << (f128::SIGNIFICAND_BITS + 1);
203+
let m_base = (i << n) >> f128::EXP_BITS;
204+
let adj = (i << n) << (f128::SIG_BITS + 1);
205205
let m = m_adj::<f128>(m_base, adj);
206206
let e = exp::<u128, f128>(n) - 1;
207207
repr::<f128>(e, m)
@@ -362,29 +362,29 @@ where
362362
F::Int: CastFrom<u32>,
363363
u32: CastFrom<F::Int>,
364364
{
365-
let int_max_exp = F::EXPONENT_BIAS + I::MAX.ilog2() + 1;
366-
let foobar = F::EXPONENT_BIAS + I::UnsignedInt::BITS - 1;
365+
let int_max_exp = F::EXP_BIAS + I::MAX.ilog2() + 1;
366+
let foobar = F::EXP_BIAS + I::UnsignedInt::BITS - 1;
367367

368368
if fbits < F::ONE.to_bits() {
369369
// < 0 gets rounded to 0
370370
I::ZERO
371-
} else if fbits < F::Int::cast_from(int_max_exp) << F::SIGNIFICAND_BITS {
371+
} else if fbits < F::Int::cast_from(int_max_exp) << F::SIG_BITS {
372372
// >= 1, < integer max
373373
let m_base = if I::UnsignedInt::BITS >= F::Int::BITS {
374-
I::UnsignedInt::cast_from(fbits) << (I::BITS - F::SIGNIFICAND_BITS - 1)
374+
I::UnsignedInt::cast_from(fbits) << (I::BITS - F::SIG_BITS - 1)
375375
} else {
376-
I::UnsignedInt::cast_from(fbits >> (F::SIGNIFICAND_BITS - I::BITS + 1))
376+
I::UnsignedInt::cast_from(fbits >> (F::SIG_BITS - I::BITS + 1))
377377
};
378378

379379
// Set the implicit 1-bit.
380-
let m: I::UnsignedInt = I::UnsignedInt::ONE << (I::BITS - 1) | m_base;
380+
let m: I::UnsignedInt = (I::UnsignedInt::ONE << (I::BITS - 1)) | m_base;
381381

382382
// Shift based on the exponent and bias.
383-
let s: u32 = (foobar) - u32::cast_from(fbits >> F::SIGNIFICAND_BITS);
383+
let s: u32 = (foobar) - u32::cast_from(fbits >> F::SIG_BITS);
384384

385385
let unsigned = m >> s;
386386
map_inbounds(I::from_unsigned(unsigned))
387-
} else if fbits <= F::EXPONENT_MASK {
387+
} else if fbits <= F::EXP_MASK {
388388
// >= max (incl. inf)
389389
out_of_bounds()
390390
} else {

src/float/div.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,16 +105,16 @@ where
105105
let hw = F::BITS / 2;
106106
let lo_mask = F::Int::MAX >> hw;
107107

108-
let significand_bits = F::SIGNIFICAND_BITS;
108+
let significand_bits = F::SIG_BITS;
109109
// Saturated exponent, representing infinity
110-
let exponent_sat: F::Int = F::EXPONENT_MAX.cast();
110+
let exponent_sat: F::Int = F::EXP_SAT.cast();
111111

112-
let exponent_bias = F::EXPONENT_BIAS;
112+
let exponent_bias = F::EXP_BIAS;
113113
let implicit_bit = F::IMPLICIT_BIT;
114-
let significand_mask = F::SIGNIFICAND_MASK;
114+
let significand_mask = F::SIG_MASK;
115115
let sign_bit = F::SIGN_MASK;
116116
let abs_mask = sign_bit - one;
117-
let exponent_mask = F::EXPONENT_MASK;
117+
let exponent_mask = F::EXP_MASK;
118118
let inf_rep = exponent_mask;
119119
let quiet_bit = implicit_bit >> 1;
120120
let qnan_rep = exponent_mask | quiet_bit;
@@ -261,7 +261,7 @@ where
261261
let c_hw = c_hw::<F>();
262262

263263
// Check that the top bit is set, i.e. value is within `[1, 2)`.
264-
debug_assert!(b_uq1_hw & one_hw << (HalfRep::<F>::BITS - 1) > zero_hw);
264+
debug_assert!(b_uq1_hw & (one_hw << (HalfRep::<F>::BITS - 1)) > zero_hw);
265265

266266
// b >= 1, thus an upper bound for 3/4 + 1/sqrt(2) - b/2 is about 0.9572,
267267
// so x0 fits to UQ0.HW without wrapping.

src/float/extend.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,19 @@ where
1515
let src_zero = F::Int::ZERO;
1616
let src_one = F::Int::ONE;
1717
let src_bits = F::BITS;
18-
let src_sign_bits = F::SIGNIFICAND_BITS;
19-
let src_exp_bias = F::EXPONENT_BIAS;
18+
let src_sign_bits = F::SIG_BITS;
19+
let src_exp_bias = F::EXP_BIAS;
2020
let src_min_normal = F::IMPLICIT_BIT;
21-
let src_infinity = F::EXPONENT_MASK;
21+
let src_infinity = F::EXP_MASK;
2222
let src_sign_mask = F::SIGN_MASK as F::Int;
2323
let src_abs_mask = src_sign_mask - src_one;
24-
let src_qnan = F::SIGNIFICAND_MASK;
24+
let src_qnan = F::SIG_MASK;
2525
let src_nan_code = src_qnan - src_one;
2626

2727
let dst_bits = R::BITS;
28-
let dst_sign_bits = R::SIGNIFICAND_BITS;
29-
let dst_inf_exp = R::EXPONENT_MAX;
30-
let dst_exp_bias = R::EXPONENT_BIAS;
28+
let dst_sign_bits = R::SIG_BITS;
29+
let dst_inf_exp = R::EXP_SAT;
30+
let dst_exp_bias = R::EXP_BIAS;
3131
let dst_min_normal = R::IMPLICIT_BIT;
3232

3333
let sign_bits_delta = dst_sign_bits - src_sign_bits;

0 commit comments

Comments
 (0)