Skip to content

Commit 4732818

Browse files
authored
Rollup 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 431840e + 27ee670 commit 4732818

File tree

4 files changed

+53
-38
lines changed

4 files changed

+53
-38
lines changed

library/std/src/f32.rs

Lines changed: 1 addition & 4 deletions
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

Lines changed: 3 additions & 32 deletions
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.
@@ -960,28 +955,4 @@ impl f64 {
960955
pub fn atanh(self) -> f64 {
961956
0.5 * ((2.0 * self) / (1.0 - self)).ln_1p()
962957
}
963-
964-
// Solaris/Illumos requires a wrapper around log, log2, and log10 functions
965-
// because of their non-standard behavior (e.g., log(-n) returns -Inf instead
966-
// of expected NaN).
967-
#[rustc_allow_incoherent_impl]
968-
fn log_wrapper<F: Fn(f64) -> f64>(self, log_fn: F) -> f64 {
969-
if !cfg!(any(target_os = "solaris", target_os = "illumos")) {
970-
log_fn(self)
971-
} else if self.is_finite() {
972-
if self > 0.0 {
973-
log_fn(self)
974-
} else if self == 0.0 {
975-
Self::NEG_INFINITY // log(0) = -Inf
976-
} else {
977-
Self::NAN // log(-n) = NaN
978-
}
979-
} else if self.is_nan() {
980-
self // log(NaN) = NaN
981-
} else if self > 0.0 {
982-
self // log(Inf) = Inf
983-
} else {
984-
Self::NAN // log(-Inf) = NaN
985-
}
986-
}
987958
}

library/std/src/sys/mod.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,52 @@ cfg_if::cfg_if! {
7676
pub mod c;
7777
}
7878
}
79+
80+
#[cfg(not(test))]
81+
cfg_if::cfg_if! {
82+
if #[cfg(target_os = "android")] {
83+
pub use crate::android::log2f32;
84+
pub use crate::android::log2f64;
85+
} else {
86+
#[inline]
87+
pub fn log2f32(n: f32) -> f32 {
88+
unsafe { crate::intrinsics::log2f32(n) }
89+
}
90+
91+
#[inline]
92+
pub fn log2f64(n: f64) -> f64 {
93+
unsafe { crate::intrinsics::log2f64(n) }
94+
}
95+
}
96+
}
97+
98+
// Solaris/Illumos requires a wrapper around log, log2, and log10 functions
99+
// because of their non-standard behavior (e.g., log(-n) returns -Inf instead
100+
// of expected NaN).
101+
#[cfg(not(test))]
102+
#[cfg(any(target_os = "solaris", target_os = "illumos"))]
103+
#[inline]
104+
pub fn log_wrapper<F: Fn(f64) -> f64>(n: f64, log_fn: F) -> f64 {
105+
if n.is_finite() {
106+
if n > 0.0 {
107+
log_fn(n)
108+
} else if n == 0.0 {
109+
f64::NEG_INFINITY // log(0) = -Inf
110+
} else {
111+
f64::NAN // log(-n) = NaN
112+
}
113+
} else if n.is_nan() {
114+
n // log(NaN) = NaN
115+
} else if n > 0.0 {
116+
n // log(Inf) = Inf
117+
} else {
118+
f64::NAN // log(-Inf) = NaN
119+
}
120+
}
121+
122+
#[cfg(not(test))]
123+
#[cfg(not(any(target_os = "solaris", target_os = "illumos")))]
124+
#[inline]
125+
pub fn log_wrapper<F: Fn(f64) -> f64>(n: f64, log_fn: F) -> f64 {
126+
log_fn(n)
127+
}

src/tools/tidy/src/pal.rs

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

0 commit comments

Comments
 (0)