Skip to content

Commit 662fef6

Browse files
committed
Move platform-specific implementation of libstd/f{32,64}.rs
This removes exceptions in the tidy lint 'pal'.
1 parent 5b8fe3e commit 662fef6

File tree

6 files changed

+484
-0
lines changed

6 files changed

+484
-0
lines changed

src/libstd/sys/redox/f32.rs

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![allow(dead_code)]
12+
13+
use intrinsics;
14+
15+
pub mod cmath {
16+
use libc::{c_float, c_int};
17+
18+
#[link_name = "m"]
19+
extern {
20+
pub fn lgammaf_r(n: c_float, sign: &mut c_int) -> c_float;
21+
pub fn hypotf(x: c_float, y: c_float) -> c_float;
22+
pub fn acosf(n: c_float) -> c_float;
23+
pub fn asinf(n: c_float) -> c_float;
24+
pub fn atan2f(a: c_float, b: c_float) -> c_float;
25+
pub fn atanf(n: c_float) -> c_float;
26+
pub fn coshf(n: c_float) -> c_float;
27+
pub fn frexpf(n: c_float, value: &mut c_int) -> c_float;
28+
pub fn ldexpf(x: c_float, n: c_int) -> c_float;
29+
pub fn sinhf(n: c_float) -> c_float;
30+
pub fn tanf(n: c_float) -> c_float;
31+
pub fn tanhf(n: c_float) -> c_float;
32+
}
33+
}
34+
35+
#[inline]
36+
pub fn floor(x: f32) -> f32 {
37+
unsafe { intrinsics::floorf32(x) }
38+
}
39+
40+
#[inline]
41+
pub fn ceil(x: f32) -> f32 {
42+
unsafe { intrinsics::ceilf32(x) }
43+
}
44+
45+
#[inline]
46+
pub fn powf(x: f32, n: f32) -> f32 {
47+
unsafe { intrinsics::powf32(x, n) }
48+
}
49+
50+
#[inline]
51+
pub fn exp(x: f32) -> f32 {
52+
unsafe { intrinsics::expf32(x) }
53+
}
54+
55+
#[inline]
56+
pub fn ln(x: f32) -> f32 {
57+
unsafe { intrinsics::logf32(x) }
58+
}
59+
60+
#[inline]
61+
pub fn log2(x: f32) -> f32 {
62+
unsafe { intrinsics::log2f32(x) }
63+
}
64+
65+
#[inline]
66+
pub fn log10(x: f32) -> f32 {
67+
unsafe { intrinsics::log10f32(x) }
68+
}
69+
70+
#[inline]
71+
pub fn sin(x: f32) -> f32 {
72+
unsafe { intrinsics::sinf32(x) }
73+
}
74+
75+
#[inline]
76+
pub fn cos(x: f32) -> f32 {
77+
unsafe { intrinsics::cosf32(x) }
78+
}

src/libstd/sys/redox/f64.rs

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![allow(dead_code)]
12+
13+
pub mod cmath {
14+
use libc::{c_double, c_int};
15+
16+
#[link_name = "m"]
17+
extern {
18+
pub fn lgamma_r(n: c_double, sign: &mut c_int) -> c_double;
19+
pub fn hypot(x: c_double, y: c_double) -> c_double;
20+
}
21+
}
22+
23+
pub fn ln(x: f64) -> f64 {
24+
unsafe { ::intrinsics::logf64(x) }
25+
}
26+
27+
pub fn log2(x: f64) -> f64 {
28+
unsafe { ::intrinsics::log2f64(x) }
29+
}
30+
31+
pub fn log10(x: f64) -> f64 {
32+
unsafe { ::intrinsics::log10f64(x) }
33+
}

src/libstd/sys/unix/f32.rs

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![allow(dead_code)]
12+
13+
use intrinsics;
14+
15+
pub mod cmath {
16+
use libc::{c_float, c_int};
17+
18+
#[link_name = "m"]
19+
extern {
20+
pub fn lgammaf_r(n: c_float, sign: &mut c_int) -> c_float;
21+
pub fn hypotf(x: c_float, y: c_float) -> c_float;
22+
pub fn acosf(n: c_float) -> c_float;
23+
pub fn asinf(n: c_float) -> c_float;
24+
pub fn atan2f(a: c_float, b: c_float) -> c_float;
25+
pub fn atanf(n: c_float) -> c_float;
26+
pub fn coshf(n: c_float) -> c_float;
27+
pub fn frexpf(n: c_float, value: &mut c_int) -> c_float;
28+
pub fn ldexpf(x: c_float, n: c_int) -> c_float;
29+
pub fn sinhf(n: c_float) -> c_float;
30+
pub fn tanf(n: c_float) -> c_float;
31+
pub fn tanhf(n: c_float) -> c_float;
32+
}
33+
}
34+
35+
#[inline]
36+
pub fn floor(x: f32) -> f32 {
37+
unsafe { intrinsics::floorf32(x) }
38+
}
39+
40+
#[inline]
41+
pub fn ceil(x: f32) -> f32 {
42+
unsafe { intrinsics::ceilf32(x) }
43+
}
44+
45+
#[inline]
46+
pub fn powf(x: f32, n: f32) -> f32 {
47+
unsafe { intrinsics::powf32(x, n) }
48+
}
49+
50+
#[inline]
51+
pub fn exp(x: f32) -> f32 {
52+
unsafe { intrinsics::expf32(x) }
53+
}
54+
55+
#[inline]
56+
pub fn ln(x: f32) -> f32 {
57+
unsafe { intrinsics::logf32(x) }
58+
}
59+
60+
#[inline]
61+
pub fn log2(x: f32) -> f32 {
62+
#[cfg(target_os = "android")]
63+
return ::sys::android::log2f32(x);
64+
#[cfg(not(target_os = "android"))]
65+
return unsafe { intrinsics::log2f32(x) };
66+
}
67+
68+
#[inline]
69+
pub fn log10(x: f32) -> f32 {
70+
unsafe { intrinsics::log10f32(x) }
71+
}
72+
73+
#[inline]
74+
pub fn sin(x: f32) -> f32 {
75+
unsafe { intrinsics::sinf32(x) }
76+
}
77+
78+
#[inline]
79+
pub fn cos(x: f32) -> f32 {
80+
unsafe { intrinsics::cosf32(x) }
81+
}

src/libstd/sys/unix/f64.rs

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![allow(dead_code)]
12+
13+
use core::f64::{NAN, NEG_INFINITY};
14+
15+
pub mod cmath {
16+
use libc::{c_double, c_int};
17+
18+
#[link_name = "m"]
19+
extern {
20+
pub fn lgamma_r(n: c_double, sign: &mut c_int) -> c_double;
21+
pub fn hypot(x: c_double, y: c_double) -> c_double;
22+
}
23+
}
24+
25+
pub fn ln(x: f64) -> f64 {
26+
log_wrapper(x, |n| { unsafe { ::intrinsics::logf64(n) } })
27+
}
28+
29+
pub fn log2(x: f64) -> f64 {
30+
log_wrapper(x,
31+
|n| {
32+
#[cfg(target_os = "android")]
33+
return ::sys::android::log2f64(n);
34+
#[cfg(not(target_os = "android"))]
35+
return unsafe { ::intrinsics::log2f64(n) };
36+
})
37+
}
38+
39+
pub fn log10(x: f64) -> f64 {
40+
log_wrapper(x, |n| { unsafe { ::intrinsics::log10f64(n) } })
41+
}
42+
43+
// Solaris/Illumos requires a wrapper around log, log2, and log10 functions
44+
// because of their non-standard behavior (e.g. log(-n) returns -Inf instead
45+
// of expected NaN).
46+
fn log_wrapper<F: Fn(f64) -> f64>(x: f64, log_fn: F) -> f64 {
47+
if !cfg!(target_os = "solaris") {
48+
log_fn(x)
49+
} else {
50+
if x.is_finite() {
51+
if x > 0.0 {
52+
log_fn(x)
53+
} else if x == 0.0 {
54+
NEG_INFINITY // log(0) = -Inf
55+
} else {
56+
NAN // log(-n) = NaN
57+
}
58+
} else if x.is_nan() {
59+
x // log(NaN) = NaN
60+
} else if x > 0.0 {
61+
x // log(Inf) = Inf
62+
} else {
63+
NAN // log(-Inf) = NaN
64+
}
65+
}
66+
}

0 commit comments

Comments
 (0)