Simplify {f16, f32, f54, f128}::midpoint()

`(float_ty::MAX / 2) - (float_ty::MIN_POSITIVE * 2)` equals
`(float_ty::MAX / 2) + (float_ty::MIN_POSITIVE * 2)` equals
`(float_ty::MAX / 2)`. So these branches are pointless
This commit is contained in:
Jules Bertholet 2025-09-05 02:01:18 -04:00
parent 91edc3ebcc
commit bc17bcdef0
No known key found for this signature in database
GPG key ID: 32034DAFC38C1BFC
4 changed files with 0 additions and 32 deletions

View file

@ -832,7 +832,6 @@ impl f128 {
#[unstable(feature = "f128", issue = "116909")]
#[rustc_const_unstable(feature = "f128", issue = "116909")]
pub const fn midpoint(self, other: f128) -> f128 {
const LO: f128 = f128::MIN_POSITIVE * 2.;
const HI: f128 = f128::MAX / 2.;
let (a, b) = (self, other);
@ -842,14 +841,7 @@ impl f128 {
if abs_a <= HI && abs_b <= HI {
// Overflow is impossible
(a + b) / 2.
} else if abs_a < LO {
// Not safe to halve `a` (would underflow)
a + (b / 2.)
} else if abs_b < LO {
// Not safe to halve `b` (would underflow)
(a / 2.) + b
} else {
// Safe to halve `a` and `b`
(a / 2.) + (b / 2.)
}
}

View file

@ -820,7 +820,6 @@ impl f16 {
#[unstable(feature = "f16", issue = "116909")]
#[rustc_const_unstable(feature = "f16", issue = "116909")]
pub const fn midpoint(self, other: f16) -> f16 {
const LO: f16 = f16::MIN_POSITIVE * 2.;
const HI: f16 = f16::MAX / 2.;
let (a, b) = (self, other);
@ -830,14 +829,7 @@ impl f16 {
if abs_a <= HI && abs_b <= HI {
// Overflow is impossible
(a + b) / 2.
} else if abs_a < LO {
// Not safe to halve `a` (would underflow)
a + (b / 2.)
} else if abs_b < LO {
// Not safe to halve `b` (would underflow)
(a / 2.) + b
} else {
// Safe to halve `a` and `b`
(a / 2.) + (b / 2.)
}
}

View file

@ -1025,7 +1025,6 @@ impl f32 {
((self as f64 + other as f64) / 2.0) as f32
}
_ => {
const LO: f32 = f32::MIN_POSITIVE * 2.;
const HI: f32 = f32::MAX / 2.;
let (a, b) = (self, other);
@ -1035,14 +1034,7 @@ impl f32 {
if abs_a <= HI && abs_b <= HI {
// Overflow is impossible
(a + b) / 2.
} else if abs_a < LO {
// Not safe to halve `a` (would underflow)
a + (b / 2.)
} else if abs_b < LO {
// Not safe to halve `b` (would underflow)
(a / 2.) + b
} else {
// Safe to halve `a` and `b`
(a / 2.) + (b / 2.)
}
}

View file

@ -1026,7 +1026,6 @@ impl f64 {
#[stable(feature = "num_midpoint", since = "1.85.0")]
#[rustc_const_stable(feature = "num_midpoint", since = "1.85.0")]
pub const fn midpoint(self, other: f64) -> f64 {
const LO: f64 = f64::MIN_POSITIVE * 2.;
const HI: f64 = f64::MAX / 2.;
let (a, b) = (self, other);
@ -1036,14 +1035,7 @@ impl f64 {
if abs_a <= HI && abs_b <= HI {
// Overflow is impossible
(a + b) / 2.
} else if abs_a < LO {
// Not safe to halve `a` (would underflow)
a + (b / 2.)
} else if abs_b < LO {
// Not safe to halve `b` (would underflow)
(a / 2.) + b
} else {
// Safe to halve `a` and `b`
(a / 2.) + (b / 2.)
}
}