Skip to content

Commit badd513

Browse files
committed
Use platform-specific implementation for floats
Also remove exceptions in tidy lint 'pal'.
1 parent 662fef6 commit badd513

File tree

6 files changed

+28
-159
lines changed

6 files changed

+28
-159
lines changed

src/libstd/f32.rs

+14-118
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
#![stable(feature = "rust1", since = "1.0.0")]
1616
#![allow(missing_docs)]
1717

18+
#[allow(unused_imports)]
19+
use sys;
20+
1821
#[cfg(not(test))]
1922
use core::num;
2023
#[cfg(not(test))]
@@ -40,6 +43,8 @@ pub use core::f32::consts;
4043
mod cmath {
4144
use libc::{c_float, c_int};
4245

46+
pub use sys::f32::cmath::*;
47+
4348
extern {
4449
pub fn cbrtf(n: c_float) -> c_float;
4550
pub fn erff(n: c_float) -> c_float;
@@ -55,88 +60,6 @@ mod cmath {
5560
pub fn modff(n: c_float, iptr: &mut c_float) -> c_float;
5661
pub fn nextafterf(x: c_float, y: c_float) -> c_float;
5762
pub fn tgammaf(n: c_float) -> c_float;
58-
59-
#[cfg_attr(all(windows, target_env = "msvc"), link_name = "__lgammaf_r")]
60-
pub fn lgammaf_r(n: c_float, sign: &mut c_int) -> c_float;
61-
#[cfg_attr(all(windows, target_env = "msvc"), link_name = "_hypotf")]
62-
pub fn hypotf(x: c_float, y: c_float) -> c_float;
63-
}
64-
65-
// See the comments in the `floor` function for why MSVC is special
66-
// here.
67-
#[cfg(not(target_env = "msvc"))]
68-
extern {
69-
pub fn acosf(n: c_float) -> c_float;
70-
pub fn asinf(n: c_float) -> c_float;
71-
pub fn atan2f(a: c_float, b: c_float) -> c_float;
72-
pub fn atanf(n: c_float) -> c_float;
73-
pub fn coshf(n: c_float) -> c_float;
74-
pub fn frexpf(n: c_float, value: &mut c_int) -> c_float;
75-
pub fn ldexpf(x: c_float, n: c_int) -> c_float;
76-
pub fn sinhf(n: c_float) -> c_float;
77-
pub fn tanf(n: c_float) -> c_float;
78-
pub fn tanhf(n: c_float) -> c_float;
79-
}
80-
81-
#[cfg(target_env = "msvc")]
82-
pub use self::shims::*;
83-
#[cfg(target_env = "msvc")]
84-
mod shims {
85-
use libc::{c_float, c_int};
86-
87-
#[inline]
88-
pub unsafe fn acosf(n: c_float) -> c_float {
89-
f64::acos(n as f64) as c_float
90-
}
91-
92-
#[inline]
93-
pub unsafe fn asinf(n: c_float) -> c_float {
94-
f64::asin(n as f64) as c_float
95-
}
96-
97-
#[inline]
98-
pub unsafe fn atan2f(n: c_float, b: c_float) -> c_float {
99-
f64::atan2(n as f64, b as f64) as c_float
100-
}
101-
102-
#[inline]
103-
pub unsafe fn atanf(n: c_float) -> c_float {
104-
f64::atan(n as f64) as c_float
105-
}
106-
107-
#[inline]
108-
pub unsafe fn coshf(n: c_float) -> c_float {
109-
f64::cosh(n as f64) as c_float
110-
}
111-
112-
#[inline]
113-
#[allow(deprecated)]
114-
pub unsafe fn frexpf(x: c_float, value: &mut c_int) -> c_float {
115-
let (a, b) = f64::frexp(x as f64);
116-
*value = b as c_int;
117-
a as c_float
118-
}
119-
120-
#[inline]
121-
#[allow(deprecated)]
122-
pub unsafe fn ldexpf(x: c_float, n: c_int) -> c_float {
123-
f64::ldexp(x as f64, n as isize) as c_float
124-
}
125-
126-
#[inline]
127-
pub unsafe fn sinhf(n: c_float) -> c_float {
128-
f64::sinh(n as f64) as c_float
129-
}
130-
131-
#[inline]
132-
pub unsafe fn tanf(n: c_float) -> c_float {
133-
f64::tan(n as f64) as c_float
134-
}
135-
136-
#[inline]
137-
pub unsafe fn tanhf(n: c_float) -> c_float {
138-
f64::tanh(n as f64) as c_float
139-
}
14063
}
14164
}
14265

@@ -301,10 +224,7 @@ impl f32 {
301224
// Note that there are many MSVC-specific float operations which
302225
// redirect to this comment, so `floorf` is just one case of a missing
303226
// function on MSVC, but there are many others elsewhere.
304-
#[cfg(target_env = "msvc")]
305-
return (self as f64).floor() as f32;
306-
#[cfg(not(target_env = "msvc"))]
307-
return unsafe { intrinsics::floorf32(self) };
227+
sys::f32::floor(self)
308228
}
309229

310230
/// Returns the smallest integer greater than or equal to a number.
@@ -320,10 +240,7 @@ impl f32 {
320240
#[inline]
321241
pub fn ceil(self) -> f32 {
322242
// see notes above in `floor`
323-
#[cfg(target_env = "msvc")]
324-
return (self as f64).ceil() as f32;
325-
#[cfg(not(target_env = "msvc"))]
326-
return unsafe { intrinsics::ceilf32(self) };
243+
sys::f32::ceil(self)
327244
}
328245

329246
/// Returns the nearest integer to a number. Round half-way cases away from
@@ -519,10 +436,7 @@ impl f32 {
519436
#[inline]
520437
pub fn powf(self, n: f32) -> f32 {
521438
// see notes above in `floor`
522-
#[cfg(target_env = "msvc")]
523-
return (self as f64).powf(n as f64) as f32;
524-
#[cfg(not(target_env = "msvc"))]
525-
return unsafe { intrinsics::powf32(self, n) };
439+
sys::f32::powf(self, n)
526440
}
527441

528442
/// Takes the square root of a number.
@@ -568,10 +482,7 @@ impl f32 {
568482
#[inline]
569483
pub fn exp(self) -> f32 {
570484
// see notes above in `floor`
571-
#[cfg(target_env = "msvc")]
572-
return (self as f64).exp() as f32;
573-
#[cfg(not(target_env = "msvc"))]
574-
return unsafe { intrinsics::expf32(self) };
485+
sys::f32::exp(self)
575486
}
576487

577488
/// Returns `2^(self)`.
@@ -610,10 +521,7 @@ impl f32 {
610521
#[inline]
611522
pub fn ln(self) -> f32 {
612523
// see notes above in `floor`
613-
#[cfg(target_env = "msvc")]
614-
return (self as f64).ln() as f32;
615-
#[cfg(not(target_env = "msvc"))]
616-
return unsafe { intrinsics::logf32(self) };
524+
sys::f32::ln(self)
617525
}
618526

619527
/// Returns the logarithm of the number with respect to an arbitrary base.
@@ -652,10 +560,7 @@ impl f32 {
652560
#[stable(feature = "rust1", since = "1.0.0")]
653561
#[inline]
654562
pub fn log2(self) -> f32 {
655-
#[cfg(target_os = "android")]
656-
return ::sys::android::log2f32(self);
657-
#[cfg(not(target_os = "android"))]
658-
return unsafe { intrinsics::log2f32(self) };
563+
sys::f32::log2(self)
659564
}
660565

661566
/// Returns the base 10 logarithm of the number.
@@ -674,10 +579,7 @@ impl f32 {
674579
#[inline]
675580
pub fn log10(self) -> f32 {
676581
// see notes above in `floor`
677-
#[cfg(target_env = "msvc")]
678-
return (self as f64).log10() as f32;
679-
#[cfg(not(target_env = "msvc"))]
680-
return unsafe { intrinsics::log10f32(self) };
582+
sys::f32::log10(self)
681583
}
682584

683585
/// Converts radians to degrees.
@@ -908,10 +810,7 @@ impl f32 {
908810
#[inline]
909811
pub fn sin(self) -> f32 {
910812
// see notes in `core::f32::Float::floor`
911-
#[cfg(target_env = "msvc")]
912-
return (self as f64).sin() as f32;
913-
#[cfg(not(target_env = "msvc"))]
914-
return unsafe { intrinsics::sinf32(self) };
813+
sys::f32::sin(self)
915814
}
916815

917816
/// Computes the cosine of a number (in radians).
@@ -929,10 +828,7 @@ impl f32 {
929828
#[inline]
930829
pub fn cos(self) -> f32 {
931830
// see notes in `core::f32::Float::floor`
932-
#[cfg(target_env = "msvc")]
933-
return (self as f64).cos() as f32;
934-
#[cfg(not(target_env = "msvc"))]
935-
return unsafe { intrinsics::cosf32(self) };
831+
sys::f32::cos(self)
936832
}
937833

938834
/// Computes the tangent of a number (in radians).

src/libstd/f64.rs

+8-39
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,15 @@ pub use core::f64::{MIN, MIN_POSITIVE, MAX};
3535
#[stable(feature = "rust1", since = "1.0.0")]
3636
pub use core::f64::consts;
3737

38+
#[allow(unused_imports)]
39+
use sys;
40+
3841
#[allow(dead_code)]
3942
mod cmath {
4043
use libc::{c_double, c_int};
4144

45+
pub use sys::f64::cmath::*;
46+
4247
#[link_name = "m"]
4348
extern {
4449
pub fn acos(n: c_double) -> c_double;
@@ -75,12 +80,6 @@ mod cmath {
7580
pub fn y0(n: c_double) -> c_double;
7681
pub fn y1(n: c_double) -> c_double;
7782
pub fn yn(i: c_int, n: c_double) -> c_double;
78-
79-
#[cfg_attr(all(windows, target_env = "msvc"), link_name = "__lgamma_r")]
80-
pub fn lgamma_r(n: c_double, sign: &mut c_int) -> c_double;
81-
82-
#[cfg_attr(all(windows, target_env = "msvc"), link_name = "_hypot")]
83-
pub fn hypot(x: c_double, y: c_double) -> c_double;
8483
}
8584
}
8685

@@ -515,7 +514,7 @@ impl f64 {
515514
#[stable(feature = "rust1", since = "1.0.0")]
516515
#[inline]
517516
pub fn ln(self) -> f64 {
518-
self.log_wrapper(|n| { unsafe { intrinsics::logf64(n) } })
517+
sys::f64::ln(self)
519518
}
520519

521520
/// Returns the logarithm of the number with respect to an arbitrary base.
@@ -550,12 +549,7 @@ impl f64 {
550549
#[stable(feature = "rust1", since = "1.0.0")]
551550
#[inline]
552551
pub fn log2(self) -> f64 {
553-
self.log_wrapper(|n| {
554-
#[cfg(target_os = "android")]
555-
return ::sys::android::log2f64(n);
556-
#[cfg(not(target_os = "android"))]
557-
return unsafe { intrinsics::log2f64(n) };
558-
})
552+
sys::f64::log2(self)
559553
}
560554

561555
/// Returns the base 10 logarithm of the number.
@@ -571,7 +565,7 @@ impl f64 {
571565
#[stable(feature = "rust1", since = "1.0.0")]
572566
#[inline]
573567
pub fn log10(self) -> f64 {
574-
self.log_wrapper(|n| { unsafe { intrinsics::log10f64(n) } })
568+
sys::f64::log10(self)
575569
}
576570

577571
/// Converts radians to degrees.
@@ -1091,31 +1085,6 @@ impl f64 {
10911085
pub fn atanh(self) -> f64 {
10921086
0.5 * ((2.0 * self) / (1.0 - self)).ln_1p()
10931087
}
1094-
1095-
// Solaris/Illumos requires a wrapper around log, log2, and log10 functions
1096-
// because of their non-standard behavior (e.g. log(-n) returns -Inf instead
1097-
// of expected NaN).
1098-
fn log_wrapper<F: Fn(f64) -> f64>(self, log_fn: F) -> f64 {
1099-
if !cfg!(target_os = "solaris") {
1100-
log_fn(self)
1101-
} else {
1102-
if self.is_finite() {
1103-
if self > 0.0 {
1104-
log_fn(self)
1105-
} else if self == 0.0 {
1106-
NEG_INFINITY // log(0) = -Inf
1107-
} else {
1108-
NAN // log(-n) = NaN
1109-
}
1110-
} else if self.is_nan() {
1111-
self // log(NaN) = NaN
1112-
} else if self > 0.0 {
1113-
self // log(Inf) = Inf
1114-
} else {
1115-
NAN // log(-Inf) = NaN
1116-
}
1117-
}
1118-
}
11191088
}
11201089

11211090
#[cfg(test)]

src/libstd/sys/redox/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ pub mod backtrace;
1818
pub mod condvar;
1919
pub mod env;
2020
pub mod ext;
21+
pub mod f32;
22+
pub mod f64;
2123
pub mod fast_thread_local;
2224
pub mod fd;
2325
pub mod fs;

src/libstd/sys/unix/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ pub mod backtrace;
3838
pub mod condvar;
3939
pub mod env;
4040
pub mod ext;
41+
pub mod f32;
42+
pub mod f64;
4143
pub mod fast_thread_local;
4244
pub mod fd;
4345
pub mod fs;

src/libstd/sys/windows/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ pub mod condvar;
2525
pub mod dynamic_lib;
2626
pub mod env;
2727
pub mod ext;
28+
pub mod f32;
29+
pub mod f64;
2830
pub mod fs;
2931
pub mod handle;
3032
pub mod memchr;

src/tools/tidy/src/pal.rs

-2
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,6 @@ const EXCEPTION_PATHS: &'static [&'static str] = &[
6666

6767
// temporary exceptions
6868
"src/libstd/rtdeps.rs", // Until rustbuild replaces make
69-
"src/libstd/f32.rs",
70-
"src/libstd/f64.rs",
7169
"src/libstd/sys_common/mod.rs",
7270
"src/libstd/sys_common/net.rs",
7371
"src/libterm", // Not sure how to make this crate portable, but test needs it

0 commit comments

Comments
 (0)