Skip to content

Commit 71f71a5

Browse files
committed
Auto merge of rust-lang#108485 - devsnek:float-pat-exception, r=workingjubilee
move pal cfgs in f32 and f64 to sys I'd like to push forward on `sys` being a separate crate. To start with, most of these PAL exception cases are very simple little bits of code like this, so I thought I would try tidying them up.
2 parents 02d1ee4 + 45b516c commit 71f71a5

File tree

4 files changed

+53
-38
lines changed

4 files changed

+53
-38
lines changed

library/std/src/f32.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -500,10 +500,7 @@ impl f32 {
500500
#[stable(feature = "rust1", since = "1.0.0")]
501501
#[inline]
502502
pub fn log2(self) -> f32 {
503-
#[cfg(target_os = "android")]
504-
return crate::sys::android::log2f32(self);
505-
#[cfg(not(target_os = "android"))]
506-
return unsafe { intrinsics::log2f32(self) };
503+
crate::sys::log2f32(self)
507504
}
508505

509506
/// Returns the base 10 logarithm of the number.

library/std/src/f64.rs

+3-32
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ impl f64 {
456456
#[stable(feature = "rust1", since = "1.0.0")]
457457
#[inline]
458458
pub fn ln(self) -> f64 {
459-
self.log_wrapper(|n| unsafe { intrinsics::logf64(n) })
459+
crate::sys::log_wrapper(self, |n| unsafe { intrinsics::logf64(n) })
460460
}
461461

462462
/// Returns the logarithm of the number with respect to an arbitrary base.
@@ -500,12 +500,7 @@ impl f64 {
500500
#[stable(feature = "rust1", since = "1.0.0")]
501501
#[inline]
502502
pub fn log2(self) -> f64 {
503-
self.log_wrapper(|n| {
504-
#[cfg(target_os = "android")]
505-
return crate::sys::android::log2f64(n);
506-
#[cfg(not(target_os = "android"))]
507-
return unsafe { intrinsics::log2f64(n) };
508-
})
503+
crate::sys::log_wrapper(self, crate::sys::log2f64)
509504
}
510505

511506
/// Returns the base 10 logarithm of the number.
@@ -525,7 +520,7 @@ impl f64 {
525520
#[stable(feature = "rust1", since = "1.0.0")]
526521
#[inline]
527522
pub fn log10(self) -> f64 {
528-
self.log_wrapper(|n| unsafe { intrinsics::log10f64(n) })
523+
crate::sys::log_wrapper(self, |n| unsafe { intrinsics::log10f64(n) })
529524
}
530525

531526
/// The positive difference of two numbers.
@@ -962,28 +957,4 @@ impl f64 {
962957
pub fn atanh(self) -> f64 {
963958
0.5 * ((2.0 * self) / (1.0 - self)).ln_1p()
964959
}
965-
966-
// Solaris/Illumos requires a wrapper around log, log2, and log10 functions
967-
// because of their non-standard behavior (e.g., log(-n) returns -Inf instead
968-
// of expected NaN).
969-
#[rustc_allow_incoherent_impl]
970-
fn log_wrapper<F: Fn(f64) -> f64>(self, log_fn: F) -> f64 {
971-
if !cfg!(any(target_os = "solaris", target_os = "illumos")) {
972-
log_fn(self)
973-
} else if self.is_finite() {
974-
if self > 0.0 {
975-
log_fn(self)
976-
} else if self == 0.0 {
977-
Self::NEG_INFINITY // log(0) = -Inf
978-
} else {
979-
Self::NAN // log(-n) = NaN
980-
}
981-
} else if self.is_nan() {
982-
self // log(NaN) = NaN
983-
} else if self > 0.0 {
984-
self // log(Inf) = Inf
985-
} else {
986-
Self::NAN // log(-Inf) = NaN
987-
}
988-
}
989960
}

library/std/src/sys/mod.rs

+49
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,52 @@ cfg_if::cfg_if! {
6060
pub const FULL_BACKTRACE_DEFAULT: bool = false;
6161
}
6262
}
63+
64+
#[cfg(not(test))]
65+
cfg_if::cfg_if! {
66+
if #[cfg(target_os = "android")] {
67+
pub use self::android::log2f32;
68+
pub use self::android::log2f64;
69+
} else {
70+
#[inline]
71+
pub fn log2f32(n: f32) -> f32 {
72+
unsafe { crate::intrinsics::log2f32(n) }
73+
}
74+
75+
#[inline]
76+
pub fn log2f64(n: f64) -> f64 {
77+
unsafe { crate::intrinsics::log2f64(n) }
78+
}
79+
}
80+
}
81+
82+
// Solaris/Illumos requires a wrapper around log, log2, and log10 functions
83+
// because of their non-standard behavior (e.g., log(-n) returns -Inf instead
84+
// of expected NaN).
85+
#[cfg(not(test))]
86+
#[cfg(any(target_os = "solaris", target_os = "illumos"))]
87+
#[inline]
88+
pub fn log_wrapper<F: Fn(f64) -> f64>(n: f64, log_fn: F) -> f64 {
89+
if n.is_finite() {
90+
if n > 0.0 {
91+
log_fn(n)
92+
} else if n == 0.0 {
93+
f64::NEG_INFINITY // log(0) = -Inf
94+
} else {
95+
f64::NAN // log(-n) = NaN
96+
}
97+
} else if n.is_nan() {
98+
n // log(NaN) = NaN
99+
} else if n > 0.0 {
100+
n // log(Inf) = Inf
101+
} else {
102+
f64::NAN // log(-Inf) = NaN
103+
}
104+
}
105+
106+
#[cfg(not(test))]
107+
#[cfg(not(any(target_os = "solaris", target_os = "illumos")))]
108+
#[inline]
109+
pub fn log_wrapper<F: Fn(f64) -> f64>(n: f64, log_fn: F) -> f64 {
110+
log_fn(n)
111+
}

src/tools/tidy/src/pal.rs

-2
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,6 @@ const EXCEPTION_PATHS: &[&str] = &[
5353
// FIXME: platform-specific code should be moved to `sys`
5454
"library/std/src/io/copy.rs",
5555
"library/std/src/io/stdio.rs",
56-
"library/std/src/f32.rs",
57-
"library/std/src/f64.rs",
5856
"library/std/src/path.rs",
5957
"library/std/src/sys_common", // Should only contain abstractions over platforms
6058
"library/std/src/net/test.rs", // Utility helpers for tests

0 commit comments

Comments
 (0)