Skip to content
This repository was archived by the owner on Apr 28, 2025. It is now read-only.

Commit 830ddc5

Browse files
committed
Change prefixes used by the Float trait
Change `EXPONENT_` to `EXP_` and `SIGNIFICAND_` to `SIG_`. These are pretty unambiguous, and just makes for less to type once these get used.
1 parent 6b2a1f3 commit 830ddc5

File tree

2 files changed

+26
-21
lines changed

2 files changed

+26
-21
lines changed

crates/libm-test/src/mpfloat.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use crate::Float;
1515

1616
/// Create a multiple-precision float with the correct number of bits for a concrete float type.
1717
fn new_mpfloat<F: Float>() -> MpFloat {
18-
MpFloat::new(F::SIGNIFICAND_BITS + 1)
18+
MpFloat::new(F::SIG_BITS + 1)
1919
}
2020

2121
/// Set subnormal emulation and convert to a concrete float type.

src/math/support/float_traits.rs

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -27,34 +27,37 @@ pub trait Float:
2727

2828
const ZERO: Self;
2929
const ONE: Self;
30+
const INFINITY: Self;
31+
const NEG_INFINITY: Self;
32+
const NAN: Self;
3033

3134
/// The bitwidth of the float type
3235
const BITS: u32;
3336

3437
/// The bitwidth of the significand
35-
const SIGNIFICAND_BITS: u32;
38+
const SIG_BITS: u32;
3639

3740
/// The bitwidth of the exponent
38-
const EXPONENT_BITS: u32 = Self::BITS - Self::SIGNIFICAND_BITS - 1;
41+
const EXP_BITS: u32 = Self::BITS - Self::SIG_BITS - 1;
3942

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

4346
/// The exponent bias value
44-
const EXPONENT_BIAS: u32 = Self::EXPONENT_MAX >> 1;
47+
const EXP_BIAS: u32 = Self::EXP_MAX >> 1;
4548

4649
/// A mask for the sign bit
4750
const SIGN_MASK: Self::Int;
4851

4952
/// A mask for the significand
50-
const SIGNIFICAND_MASK: Self::Int;
53+
const SIG_MASK: Self::Int;
54+
55+
/// A mask for the exponent
56+
const EXP_MASK: Self::Int;
5157

5258
/// The implicit bit of the float format
5359
const IMPLICIT_BIT: Self::Int;
5460

55-
/// A mask for the exponent
56-
const EXPONENT_MASK: Self::Int;
57-
5861
/// Returns `self` transmuted to `Self::Int`
5962
fn to_bits(self) -> Self::Int;
6063

@@ -105,14 +108,17 @@ macro_rules! float_impl {
105108

106109
const ZERO: Self = 0.0;
107110
const ONE: Self = 1.0;
111+
const INFINITY: Self = Self::INFINITY;
112+
const NEG_INFINITY: Self = Self::NEG_INFINITY;
113+
const NAN: Self = Self::NAN;
108114

109115
const BITS: u32 = $bits;
110-
const SIGNIFICAND_BITS: u32 = $significand_bits;
116+
const SIG_BITS: u32 = $significand_bits;
111117

112118
const SIGN_MASK: Self::Int = 1 << (Self::BITS - 1);
113-
const SIGNIFICAND_MASK: Self::Int = (1 << Self::SIGNIFICAND_BITS) - 1;
114-
const IMPLICIT_BIT: Self::Int = 1 << Self::SIGNIFICAND_BITS;
115-
const EXPONENT_MASK: Self::Int = !(Self::SIGN_MASK | Self::SIGNIFICAND_MASK);
119+
const SIG_MASK: Self::Int = (1 << Self::SIG_BITS) - 1;
120+
const EXP_MASK: Self::Int = !(Self::SIGN_MASK | Self::SIG_MASK);
121+
const IMPLICIT_BIT: Self::Int = 1 << Self::SIG_BITS;
116122

117123
fn to_bits(self) -> Self::Int {
118124
self.to_bits()
@@ -126,19 +132,18 @@ macro_rules! float_impl {
126132
// necessary builtin (__unordtf2) to test whether `f128` is NaN.
127133
// FIXME(f16_f128): Remove once the nightly toolchain has the __unordtf2 builtin
128134
// x is NaN if all the bits of the exponent are set and the significand is non-0
129-
x.to_bits() & $ty::EXPONENT_MASK == $ty::EXPONENT_MASK
130-
&& x.to_bits() & $ty::SIGNIFICAND_MASK != 0
135+
x.to_bits() & $ty::EXP_MASK == $ty::EXP_MASK && x.to_bits() & $ty::SIG_MASK != 0
131136
}
132137
if is_nan(self) && is_nan(rhs) { true } else { self.to_bits() == rhs.to_bits() }
133138
}
134139
fn is_sign_negative(self) -> bool {
135140
self.is_sign_negative()
136141
}
137142
fn exp(self) -> Self::ExpInt {
138-
((self.to_bits() & Self::EXPONENT_MASK) >> Self::SIGNIFICAND_BITS) as Self::ExpInt
143+
((self.to_bits() & Self::EXP_MASK) >> Self::SIG_BITS) as Self::ExpInt
139144
}
140145
fn frac(self) -> Self::Int {
141-
self.to_bits() & Self::SIGNIFICAND_MASK
146+
self.to_bits() & Self::SIG_MASK
142147
}
143148
fn imp_frac(self) -> Self::Int {
144149
self.frac() | Self::IMPLICIT_BIT
@@ -149,16 +154,16 @@ macro_rules! float_impl {
149154
fn from_parts(negative: bool, exponent: Self::Int, significand: Self::Int) -> Self {
150155
Self::from_bits(
151156
((negative as Self::Int) << (Self::BITS - 1))
152-
| ((exponent << Self::SIGNIFICAND_BITS) & Self::EXPONENT_MASK)
153-
| (significand & Self::SIGNIFICAND_MASK),
157+
| ((exponent << Self::SIG_BITS) & Self::EXP_MASK)
158+
| (significand & Self::SIG_MASK),
154159
)
155160
}
156161
fn normalize(significand: Self::Int) -> (i32, Self::Int) {
157-
let shift = significand.leading_zeros().wrapping_sub(Self::EXPONENT_BITS);
162+
let shift = significand.leading_zeros().wrapping_sub(Self::EXP_BITS);
158163
(1i32.wrapping_sub(shift as i32), significand << shift as Self::Int)
159164
}
160165
fn is_subnormal(self) -> bool {
161-
(self.to_bits() & Self::EXPONENT_MASK) == Self::Int::ZERO
166+
(self.to_bits() & Self::EXP_MASK) == Self::Int::ZERO
162167
}
163168
}
164169
};

0 commit comments

Comments
 (0)