Skip to content

Commit f66e0f1

Browse files
committed
Rename EXP_MAX to EXP_SAT
`EXP_MAX` sounds like it would be the maximum value representable by that float type's exponent, rather than the maximum unsigned value of its bits. Clarify this by renaming to `EXP_SAT`, the "saturated" exponent representation.
1 parent 45870ef commit f66e0f1

File tree

3 files changed

+15
-12
lines changed

3 files changed

+15
-12
lines changed

src/math/generic/fmod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub fn fmod<F: Float>(x: F, y: F) -> F {
1313
let mut ey = y.exp().signed();
1414
let sx = ix & F::SIGN_MASK;
1515

16-
if iy << 1 == zero || y.is_nan() || ex == F::EXP_MAX as i32 {
16+
if iy << 1 == zero || y.is_nan() || ex == F::EXP_SAT as i32 {
1717
return (x * y) / (x * y);
1818
}
1919

src/math/generic/sqrt.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ where
6868
(Exp::NoShift(()), special_case)
6969
} else {
7070
let top = u32::cast_from(ix >> F::SIG_BITS);
71-
let special_case = top.wrapping_sub(1) >= F::EXP_MAX - 1;
71+
let special_case = top.wrapping_sub(1) >= F::EXP_SAT - 1;
7272
(Exp::Shifted(top), special_case)
7373
};
7474

@@ -119,7 +119,7 @@ where
119119
if even {
120120
m_u2 >>= 1;
121121
}
122-
e = (e.wrapping_add(F::EXP_MAX >> 1)) >> 1;
122+
e = (e.wrapping_add(F::EXP_SAT >> 1)) >> 1;
123123
(m_u2, Exp::Shifted(e))
124124
}
125125
Exp::NoShift(()) => {

src/math/support/float_traits.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,14 @@ pub trait Float:
4848
/// The bitwidth of the exponent
4949
const EXP_BITS: u32 = Self::BITS - Self::SIG_BITS - 1;
5050

51-
/// The saturated value of the exponent (infinite representation), in the rightmost postiion.
52-
const EXP_MAX: u32 = (1 << Self::EXP_BITS) - 1;
51+
/// The saturated (maximum bitpattern) value of the exponent, i.e. the infinite
52+
/// representation.
53+
///
54+
/// This shifted fully right, use `EXP_MASK` for the shifted value.
55+
const EXP_SAT: u32 = (1 << Self::EXP_BITS) - 1;
5356

5457
/// The exponent bias value
55-
const EXP_BIAS: u32 = Self::EXP_MAX >> 1;
58+
const EXP_BIAS: u32 = Self::EXP_SAT >> 1;
5659

5760
/// A mask for the sign bit
5861
const SIGN_MASK: Self::Int;
@@ -109,7 +112,7 @@ pub trait Float:
109112

110113
/// Returns the exponent, not adjusting for bias, not accounting for subnormals or zero.
111114
fn exp(self) -> u32 {
112-
u32::cast_from(self.to_bits() >> Self::SIG_BITS) & Self::EXP_MAX
115+
u32::cast_from(self.to_bits() >> Self::SIG_BITS) & Self::EXP_SAT
113116
}
114117

115118
/// Extract the exponent and adjust it for bias, not accounting for subnormals or zero.
@@ -135,7 +138,7 @@ pub trait Float:
135138
let sign = if negative { Self::Int::ONE } else { Self::Int::ZERO };
136139
Self::from_bits(
137140
(sign << (Self::BITS - 1))
138-
| (Self::Int::cast_from(exponent & Self::EXP_MAX) << Self::SIG_BITS)
141+
| (Self::Int::cast_from(exponent & Self::EXP_SAT) << Self::SIG_BITS)
139142
| (significand & Self::SIG_MASK),
140143
)
141144
}
@@ -267,7 +270,7 @@ mod tests {
267270
#[cfg(f16_enabled)]
268271
fn check_f16() {
269272
// Constants
270-
assert_eq!(f16::EXP_MAX, 0b11111);
273+
assert_eq!(f16::EXP_SAT, 0b11111);
271274
assert_eq!(f16::EXP_BIAS, 15);
272275

273276
// `exp_unbiased`
@@ -289,7 +292,7 @@ mod tests {
289292
#[test]
290293
fn check_f32() {
291294
// Constants
292-
assert_eq!(f32::EXP_MAX, 0b11111111);
295+
assert_eq!(f32::EXP_SAT, 0b11111111);
293296
assert_eq!(f32::EXP_BIAS, 127);
294297

295298
// `exp_unbiased`
@@ -312,7 +315,7 @@ mod tests {
312315
#[test]
313316
fn check_f64() {
314317
// Constants
315-
assert_eq!(f64::EXP_MAX, 0b11111111111);
318+
assert_eq!(f64::EXP_SAT, 0b11111111111);
316319
assert_eq!(f64::EXP_BIAS, 1023);
317320

318321
// `exp_unbiased`
@@ -336,7 +339,7 @@ mod tests {
336339
#[cfg(f128_enabled)]
337340
fn check_f128() {
338341
// Constants
339-
assert_eq!(f128::EXP_MAX, 0b111111111111111);
342+
assert_eq!(f128::EXP_SAT, 0b111111111111111);
340343
assert_eq!(f128::EXP_BIAS, 16383);
341344

342345
// `exp_unbiased`

0 commit comments

Comments
 (0)