Skip to content

Commit d251717

Browse files
committed
Change f32::midpoint to upcast to f64
This has been verified by kani as a correct optimization see: rust-lang#110840 (comment) The new implementation is branchless, and only differs in which NaN values are produced (if any are produced at all). Which is fine to change. Aside from NaN handling, this implementation produces bitwise identical results to the original implementation.
1 parent a84bb95 commit d251717

File tree

1 file changed

+1
-20
lines changed

1 file changed

+1
-20
lines changed

library/core/src/num/f32.rs

+1-20
Original file line numberDiff line numberDiff line change
@@ -1016,26 +1016,7 @@ impl f32 {
10161016
/// ```
10171017
#[unstable(feature = "num_midpoint", issue = "110840")]
10181018
pub fn midpoint(self, other: f32) -> f32 {
1019-
const LO: f32 = f32::MIN_POSITIVE * 2.;
1020-
const HI: f32 = f32::MAX / 2.;
1021-
1022-
let (a, b) = (self, other);
1023-
let abs_a = a.abs_private();
1024-
let abs_b = b.abs_private();
1025-
1026-
if abs_a <= HI && abs_b <= HI {
1027-
// Overflow is impossible
1028-
(a + b) / 2.
1029-
} else if abs_a < LO {
1030-
// Not safe to halve a
1031-
a + (b / 2.)
1032-
} else if abs_b < LO {
1033-
// Not safe to halve b
1034-
(a / 2.) + b
1035-
} else {
1036-
// Not safe to halve a and b
1037-
(a / 2.) + (b / 2.)
1038-
}
1019+
((f64::from(self) + f64::from(other)) / 2.0) as f32
10391020
}
10401021

10411022
/// Rounds toward zero and converts to any primitive integer type,

0 commit comments

Comments
 (0)