Skip to content

Commit 85604d9

Browse files
committed
Shorten prefixes for float constants
Change `SIGNIFICAND_*` to `SIG_*` and `EXPONENT_*` to `EXP_*`. This makes things more consistent with `libm`, and terseness is convenient here since there isn't anything to confuse.
1 parent ff0ca1f commit 85604d9

File tree

10 files changed

+111
-116
lines changed

10 files changed

+111
-116
lines changed

src/float/add.rs

Lines changed: 4 additions & 4 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_MAX;
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;

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: 23 additions & 23 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.
@@ -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.
380380
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: 5 additions & 5 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_MAX.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;

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_MAX;
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;

src/float/mod.rs

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -42,32 +42,32 @@ pub(crate) trait Float:
4242
const ZERO: Self;
4343
const ONE: Self;
4444

45-
/// The bitwidth of the float type
45+
/// The bitwidth of the float type.
4646
const BITS: u32;
4747

48-
/// The bitwidth of the significand
49-
const SIGNIFICAND_BITS: u32;
48+
/// The bitwidth of the significand.
49+
const SIG_BITS: u32;
5050

51-
/// The bitwidth of the exponent
52-
const EXPONENT_BITS: u32 = Self::BITS - Self::SIGNIFICAND_BITS - 1;
51+
/// The bitwidth of the exponent.
52+
const EXP_BITS: u32 = Self::BITS - Self::SIG_BITS - 1;
5353

5454
/// The saturated value of the exponent (infinite representation), in the rightmost postiion.
55-
const EXPONENT_MAX: u32 = (1 << Self::EXPONENT_BITS) - 1;
55+
const EXP_MAX: u32 = (1 << Self::EXP_BITS) - 1;
5656

57-
/// The exponent bias value
58-
const EXPONENT_BIAS: u32 = Self::EXPONENT_MAX >> 1;
57+
/// The exponent bias value.
58+
const EXP_BIAS: u32 = Self::EXP_MAX >> 1;
5959

60-
/// A mask for the sign bit
60+
/// A mask for the sign bit.
6161
const SIGN_MASK: Self::Int;
6262

63-
/// A mask for the significand
64-
const SIGNIFICAND_MASK: Self::Int;
63+
/// A mask for the significand.
64+
const SIG_MASK: Self::Int;
6565

66-
/// The implicit bit of the float format
66+
/// The implicit bit of the float format.
6767
const IMPLICIT_BIT: Self::Int;
6868

69-
/// A mask for the exponent
70-
const EXPONENT_MASK: Self::Int;
69+
/// A mask for the exponent.
70+
const EXP_MASK: Self::Int;
7171

7272
/// Returns `self` transmuted to `Self::Int`
7373
fn to_bits(self) -> Self::Int;
@@ -122,12 +122,12 @@ macro_rules! float_impl {
122122
const ONE: Self = 1.0;
123123

124124
const BITS: u32 = $bits;
125-
const SIGNIFICAND_BITS: u32 = $significand_bits;
125+
const SIG_BITS: u32 = $significand_bits;
126126

127127
const SIGN_MASK: Self::Int = 1 << (Self::BITS - 1);
128-
const SIGNIFICAND_MASK: Self::Int = (1 << Self::SIGNIFICAND_BITS) - 1;
129-
const IMPLICIT_BIT: Self::Int = 1 << Self::SIGNIFICAND_BITS;
130-
const EXPONENT_MASK: Self::Int = !(Self::SIGN_MASK | Self::SIGNIFICAND_MASK);
128+
const SIG_MASK: Self::Int = (1 << Self::SIG_BITS) - 1;
129+
const IMPLICIT_BIT: Self::Int = 1 << Self::SIG_BITS;
130+
const EXP_MASK: Self::Int = !(Self::SIGN_MASK | Self::SIG_MASK);
131131

132132
fn to_bits(self) -> Self::Int {
133133
self.to_bits()
@@ -142,8 +142,7 @@ macro_rules! float_impl {
142142
// necessary builtin (__unordtf2) to test whether `f128` is NaN.
143143
// FIXME(f16_f128): Remove once the nightly toolchain has the __unordtf2 builtin
144144
// x is NaN if all the bits of the exponent are set and the significand is non-0
145-
x.to_bits() & $ty::EXPONENT_MASK == $ty::EXPONENT_MASK
146-
&& x.to_bits() & $ty::SIGNIFICAND_MASK != 0
145+
x.to_bits() & $ty::EXP_MASK == $ty::EXP_MASK && x.to_bits() & $ty::SIG_MASK != 0
147146
}
148147
#[cfg(not(feature = "mangled-names"))]
149148
fn is_nan(x: $ty) -> bool {
@@ -159,10 +158,10 @@ macro_rules! float_impl {
159158
self.is_sign_negative()
160159
}
161160
fn exp(self) -> Self::ExpInt {
162-
((self.to_bits() & Self::EXPONENT_MASK) >> Self::SIGNIFICAND_BITS) as Self::ExpInt
161+
((self.to_bits() & Self::EXP_MASK) >> Self::SIG_BITS) as Self::ExpInt
163162
}
164163
fn frac(self) -> Self::Int {
165-
self.to_bits() & Self::SIGNIFICAND_MASK
164+
self.to_bits() & Self::SIG_MASK
166165
}
167166
fn imp_frac(self) -> Self::Int {
168167
self.frac() | Self::IMPLICIT_BIT
@@ -173,21 +172,19 @@ macro_rules! float_impl {
173172
fn from_parts(negative: bool, exponent: Self::Int, significand: Self::Int) -> Self {
174173
Self::from_bits(
175174
((negative as Self::Int) << (Self::BITS - 1))
176-
| ((exponent << Self::SIGNIFICAND_BITS) & Self::EXPONENT_MASK)
177-
| (significand & Self::SIGNIFICAND_MASK),
175+
| ((exponent << Self::SIG_BITS) & Self::EXP_MASK)
176+
| (significand & Self::SIG_MASK),
178177
)
179178
}
180179
fn normalize(significand: Self::Int) -> (i32, Self::Int) {
181-
let shift = significand
182-
.leading_zeros()
183-
.wrapping_sub(Self::EXPONENT_BITS);
180+
let shift = significand.leading_zeros().wrapping_sub(Self::EXP_BITS);
184181
(
185182
1i32.wrapping_sub(shift as i32),
186183
significand << shift as Self::Int,
187184
)
188185
}
189186
fn is_subnormal(self) -> bool {
190-
(self.to_bits() & Self::EXPONENT_MASK) == Self::Int::ZERO
187+
(self.to_bits() & Self::EXP_MASK) == Self::Int::ZERO
191188
}
192189
}
193190
};

src/float/mul.rs

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

1515
let bits = F::BITS;
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_MAX;
1818

19-
let exponent_bias = F::EXPONENT_BIAS;
19+
let exponent_bias = F::EXP_BIAS;
2020

2121
let implicit_bit = F::IMPLICIT_BIT;
22-
let significand_mask = F::SIGNIFICAND_MASK;
23-
let sign_bit = F::SIGN_MASK as F::Int;
22+
let significand_mask = F::SIG_MASK;
23+
let sign_bit = F::SIGN_MASK;
2424
let abs_mask = sign_bit - one;
25-
let exponent_mask = F::EXPONENT_MASK;
25+
let exponent_mask = F::EXP_MASK;
2626
let inf_rep = exponent_mask;
2727
let quiet_bit = implicit_bit >> 1;
2828
let qnan_rep = exponent_mask | quiet_bit;
29-
let exponent_bits = F::EXPONENT_BITS;
29+
let exponent_bits = F::EXP_BITS;
3030

3131
let a_rep = a.to_bits();
3232
let b_rep = b.to_bits();

0 commit comments

Comments
 (0)