Skip to content

Commit 27d64ee

Browse files
authored
Merge pull request #718 from tgross35/float-trait-refactor
Rename Float::repr and Float::from_repr
2 parents adaef32 + 64f131a commit 27d64ee

File tree

10 files changed

+76
-72
lines changed

10 files changed

+76
-72
lines changed

src/float/add.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -25,26 +25,26 @@ where
2525
let quiet_bit = implicit_bit >> 1;
2626
let qnan_rep = exponent_mask | quiet_bit;
2727

28-
let mut a_rep = a.repr();
29-
let mut b_rep = b.repr();
28+
let mut a_rep = a.to_bits();
29+
let mut b_rep = b.to_bits();
3030
let a_abs = a_rep & abs_mask;
3131
let b_abs = b_rep & abs_mask;
3232

3333
// Detect if a or b is zero, infinity, or NaN.
3434
if a_abs.wrapping_sub(one) >= inf_rep - one || b_abs.wrapping_sub(one) >= inf_rep - one {
3535
// NaN + anything = qNaN
3636
if a_abs > inf_rep {
37-
return F::from_repr(a_abs | quiet_bit);
37+
return F::from_bits(a_abs | quiet_bit);
3838
}
3939
// anything + NaN = qNaN
4040
if b_abs > inf_rep {
41-
return F::from_repr(b_abs | quiet_bit);
41+
return F::from_bits(b_abs | quiet_bit);
4242
}
4343

4444
if a_abs == inf_rep {
4545
// +/-infinity + -/+infinity = qNaN
46-
if (a.repr() ^ b.repr()) == sign_bit {
47-
return F::from_repr(qnan_rep);
46+
if (a.to_bits() ^ b.to_bits()) == sign_bit {
47+
return F::from_bits(qnan_rep);
4848
} else {
4949
// +/-infinity + anything remaining = +/- infinity
5050
return a;
@@ -60,7 +60,7 @@ where
6060
if a_abs == MinInt::ZERO {
6161
// but we need to get the sign right for zero + zero
6262
if b_abs == MinInt::ZERO {
63-
return F::from_repr(a.repr() & b.repr());
63+
return F::from_bits(a.to_bits() & b.to_bits());
6464
} else {
6565
return b;
6666
}
@@ -126,7 +126,7 @@ where
126126
a_significand = a_significand.wrapping_sub(b_significand);
127127
// If a == -b, return +zero.
128128
if a_significand == MinInt::ZERO {
129-
return F::from_repr(MinInt::ZERO);
129+
return F::from_bits(MinInt::ZERO);
130130
}
131131

132132
// If partial cancellation occured, we need to left-shift the result
@@ -152,7 +152,7 @@ where
152152

153153
// If we have overflowed the type, return +/- infinity:
154154
if a_exponent >= max_exponent as i32 {
155-
return F::from_repr(inf_rep | result_sign);
155+
return F::from_bits(inf_rep | result_sign);
156156
}
157157

158158
if a_exponent <= 0 {
@@ -185,7 +185,7 @@ where
185185
result += result & one;
186186
}
187187

188-
F::from_repr(result)
188+
F::from_bits(result)
189189
}
190190

191191
intrinsics! {

src/float/cmp.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ fn cmp<F: Float>(a: F, b: F) -> Result {
4141
let exponent_mask = F::EXPONENT_MASK;
4242
let inf_rep = exponent_mask;
4343

44-
let a_rep = a.repr();
45-
let b_rep = b.repr();
44+
let a_rep = a.to_bits();
45+
let b_rep = b.to_bits();
4646
let a_abs = a_rep & abs_mask;
4747
let b_abs = b_rep & abs_mask;
4848

@@ -56,8 +56,8 @@ fn cmp<F: Float>(a: F, b: F) -> Result {
5656
return Result::Equal;
5757
}
5858

59-
let a_srep = a.signed_repr();
60-
let b_srep = b.signed_repr();
59+
let a_srep = a.to_bits_signed();
60+
let b_srep = b.to_bits_signed();
6161

6262
// If at least one of a and b is positive, we get the same result comparing
6363
// a and b as signed integers as we would with a fp_ting-point compare.
@@ -90,8 +90,8 @@ fn unord<F: Float>(a: F, b: F) -> bool {
9090
let exponent_mask = F::EXPONENT_MASK;
9191
let inf_rep = exponent_mask;
9292

93-
let a_rep = a.repr();
94-
let b_rep = b.repr();
93+
let a_rep = a.to_bits();
94+
let b_rep = b.to_bits();
9595
let a_abs = a_rep & abs_mask;
9696
let b_abs = b_rep & abs_mask;
9797

src/float/conv.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ where
158158
F::Int: CastInto<U::UnsignedInt>,
159159
u32: CastFrom<F::Int>,
160160
{
161-
float_to_int_inner::<F, U, _, _>(f.repr(), |i: U| i, || U::MAX)
161+
float_to_int_inner::<F, U, _, _>(f.to_bits(), |i: U| i, || U::MAX)
162162
}
163163

164164
/// Generic float to signed int conversions.
@@ -172,7 +172,7 @@ where
172172
u32: CastFrom<F::Int>,
173173
{
174174
float_to_int_inner::<F, I, _, _>(
175-
f.repr() & !F::SIGN_MASK,
175+
f.to_bits() & !F::SIGN_MASK,
176176
|i: I| if f.is_sign_negative() { -i } else { i },
177177
|| if f.is_sign_negative() { I::MIN } else { I::MAX },
178178
)
@@ -203,7 +203,7 @@ where
203203
let int_max_exp = F::EXPONENT_BIAS + I::MAX.ilog2() + 1;
204204
let foobar = F::EXPONENT_BIAS + I::UnsignedInt::BITS - 1;
205205

206-
if fbits < F::ONE.repr() {
206+
if fbits < F::ONE.to_bits() {
207207
// < 0 gets rounded to 0
208208
I::ZERO
209209
} else if fbits < F::Int::cast_from(int_max_exp) << F::SIGNIFICAND_BITS {

src/float/div.rs

+13-13
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,8 @@ where
126126
half_iterations += 1;
127127
}
128128

129-
let a_rep = a.repr();
130-
let b_rep = b.repr();
129+
let a_rep = a.to_bits();
130+
let b_rep = b.to_bits();
131131

132132
// Exponent numeric representationm not accounting for bias
133133
let a_exponent = (a_rep >> significand_bits) & exponent_sat;
@@ -150,42 +150,42 @@ where
150150

151151
// NaN / anything = qNaN
152152
if a_abs > inf_rep {
153-
return F::from_repr(a_rep | quiet_bit);
153+
return F::from_bits(a_rep | quiet_bit);
154154
}
155155

156156
// anything / NaN = qNaN
157157
if b_abs > inf_rep {
158-
return F::from_repr(b_rep | quiet_bit);
158+
return F::from_bits(b_rep | quiet_bit);
159159
}
160160

161161
if a_abs == inf_rep {
162162
if b_abs == inf_rep {
163163
// infinity / infinity = NaN
164-
return F::from_repr(qnan_rep);
164+
return F::from_bits(qnan_rep);
165165
} else {
166166
// infinity / anything else = +/- infinity
167-
return F::from_repr(a_abs | quotient_sign);
167+
return F::from_bits(a_abs | quotient_sign);
168168
}
169169
}
170170

171171
// anything else / infinity = +/- 0
172172
if b_abs == inf_rep {
173-
return F::from_repr(quotient_sign);
173+
return F::from_bits(quotient_sign);
174174
}
175175

176176
if a_abs == zero {
177177
if b_abs == zero {
178178
// zero / zero = NaN
179-
return F::from_repr(qnan_rep);
179+
return F::from_bits(qnan_rep);
180180
} else {
181181
// zero / anything else = +/- zero
182-
return F::from_repr(quotient_sign);
182+
return F::from_bits(quotient_sign);
183183
}
184184
}
185185

186186
// anything else / zero = +/- infinity
187187
if b_abs == zero {
188-
return F::from_repr(inf_rep | quotient_sign);
188+
return F::from_bits(inf_rep | quotient_sign);
189189
}
190190

191191
// a is denormal. Renormalize it and set the scale to include the necessary exponent
@@ -463,7 +463,7 @@ where
463463
//
464464
// If we have overflowed the exponent, return infinity
465465
if res_exponent >= i32::cast_from(exponent_sat) {
466-
return F::from_repr(inf_rep | quotient_sign);
466+
return F::from_bits(inf_rep | quotient_sign);
467467
}
468468

469469
// Now, quotient <= the correctly-rounded result
@@ -476,7 +476,7 @@ where
476476
ret
477477
} else {
478478
if ((significand_bits as i32) + res_exponent) < 0 {
479-
return F::from_repr(quotient_sign);
479+
return F::from_bits(quotient_sign);
480480
}
481481

482482
let ret = quotient.wrapping_shr(u32::cast_from(res_exponent.wrapping_neg()) + 1);
@@ -501,7 +501,7 @@ where
501501
u8::from(abs_result < inf_rep && residual_lo > (4 + 1).cast() * b_significand).into();
502502
}
503503

504-
F::from_repr(abs_result | quotient_sign)
504+
F::from_bits(abs_result | quotient_sign)
505505
}
506506

507507
/// Calculate the number of iterations required for a float type's precision.

src/float/extend.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ where
3232

3333
let sign_bits_delta = dst_sign_bits - src_sign_bits;
3434
let exp_bias_delta = dst_exp_bias - src_exp_bias;
35-
let a_abs = a.repr() & src_abs_mask;
35+
let a_abs = a.to_bits() & src_abs_mask;
3636
let mut abs_result = R::Int::ZERO;
3737

3838
if a_abs.wrapping_sub(src_min_normal) < src_infinity.wrapping_sub(src_min_normal) {
@@ -65,8 +65,8 @@ where
6565
abs_result = (abs_result ^ dst_min_normal) | (bias_dst.wrapping_shl(dst_sign_bits));
6666
}
6767

68-
let sign_result: R::Int = (a.repr() & src_sign_mask).cast();
69-
R::from_repr(abs_result | (sign_result.wrapping_shl(dst_bits - src_bits)))
68+
let sign_result: R::Int = (a.to_bits() & src_sign_mask).cast();
69+
R::from_bits(abs_result | (sign_result.wrapping_shl(dst_bits - src_bits)))
7070
}
7171

7272
intrinsics! {

src/float/mod.rs

+19-14
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,10 @@ pub(crate) trait Float:
7070
const EXPONENT_MASK: Self::Int;
7171

7272
/// Returns `self` transmuted to `Self::Int`
73-
fn repr(self) -> Self::Int;
73+
fn to_bits(self) -> Self::Int;
7474

7575
/// Returns `self` transmuted to `Self::SignedInt`
76-
fn signed_repr(self) -> Self::SignedInt;
76+
fn to_bits_signed(self) -> Self::SignedInt;
7777

7878
/// Checks if two floats have the same bit representation. *Except* for NaNs! NaN can be
7979
/// represented in multiple different ways. This method returns `true` if two NaNs are
@@ -93,10 +93,15 @@ pub(crate) trait Float:
9393
fn imp_frac(self) -> Self::Int;
9494

9595
/// Returns a `Self::Int` transmuted back to `Self`
96-
fn from_repr(a: Self::Int) -> Self;
96+
fn from_bits(a: Self::Int) -> Self;
9797

9898
/// Constructs a `Self` from its parts. Inputs are treated as bits and shifted into position.
99-
fn from_parts(sign: bool, exponent: Self::Int, significand: Self::Int) -> Self;
99+
fn from_parts(negative: bool, exponent: Self::Int, significand: Self::Int) -> Self;
100+
101+
fn abs(self) -> Self {
102+
let abs_mask = !Self::SIGN_MASK ;
103+
Self::from_bits(self.to_bits() & abs_mask)
104+
}
100105

101106
/// Returns (normalized exponent, normalized significand)
102107
fn normalize(significand: Self::Int) -> (i32, Self::Int);
@@ -124,10 +129,10 @@ macro_rules! float_impl {
124129
const IMPLICIT_BIT: Self::Int = 1 << Self::SIGNIFICAND_BITS;
125130
const EXPONENT_MASK: Self::Int = !(Self::SIGN_MASK | Self::SIGNIFICAND_MASK);
126131

127-
fn repr(self) -> Self::Int {
132+
fn to_bits(self) -> Self::Int {
128133
self.to_bits()
129134
}
130-
fn signed_repr(self) -> Self::SignedInt {
135+
fn to_bits_signed(self) -> Self::SignedInt {
131136
self.to_bits() as Self::SignedInt
132137
}
133138
fn eq_repr(self, rhs: Self) -> bool {
@@ -137,8 +142,8 @@ macro_rules! float_impl {
137142
// necessary builtin (__unordtf2) to test whether `f128` is NaN.
138143
// FIXME(f16_f128): Remove once the nightly toolchain has the __unordtf2 builtin
139144
// x is NaN if all the bits of the exponent are set and the significand is non-0
140-
x.repr() & $ty::EXPONENT_MASK == $ty::EXPONENT_MASK
141-
&& x.repr() & $ty::SIGNIFICAND_MASK != 0
145+
x.to_bits() & $ty::EXPONENT_MASK == $ty::EXPONENT_MASK
146+
&& x.to_bits() & $ty::SIGNIFICAND_MASK != 0
142147
}
143148
#[cfg(not(feature = "mangled-names"))]
144149
fn is_nan(x: $ty) -> bool {
@@ -147,7 +152,7 @@ macro_rules! float_impl {
147152
if is_nan(self) && is_nan(rhs) {
148153
true
149154
} else {
150-
self.repr() == rhs.repr()
155+
self.to_bits() == rhs.to_bits()
151156
}
152157
}
153158
fn is_sign_negative(self) -> bool {
@@ -162,12 +167,12 @@ macro_rules! float_impl {
162167
fn imp_frac(self) -> Self::Int {
163168
self.frac() | Self::IMPLICIT_BIT
164169
}
165-
fn from_repr(a: Self::Int) -> Self {
170+
fn from_bits(a: Self::Int) -> Self {
166171
Self::from_bits(a)
167172
}
168-
fn from_parts(sign: bool, exponent: Self::Int, significand: Self::Int) -> Self {
169-
Self::from_repr(
170-
((sign as Self::Int) << (Self::BITS - 1))
173+
fn from_parts(negative: bool, exponent: Self::Int, significand: Self::Int) -> Self {
174+
Self::from_bits(
175+
((negative as Self::Int) << (Self::BITS - 1))
171176
| ((exponent << Self::SIGNIFICAND_BITS) & Self::EXPONENT_MASK)
172177
| (significand & Self::SIGNIFICAND_MASK),
173178
)
@@ -182,7 +187,7 @@ macro_rules! float_impl {
182187
)
183188
}
184189
fn is_subnormal(self) -> bool {
185-
(self.repr() & Self::EXPONENT_MASK) == Self::Int::ZERO
190+
(self.to_bits() & Self::EXPONENT_MASK) == Self::Int::ZERO
186191
}
187192
}
188193
};

0 commit comments

Comments
 (0)