Skip to content

Commit 348930e

Browse files
committed
std: Move the cmath module into the sys module
This commit moves the `f32::cmath` and `f64::cmath` modules into the `sys` module. Note that these are not publicly exported modules, simply implementation details. These modules are already platform-specific with shims on MSVC and this is mostly just a reflection of that reality. This should also help cut down on `#[cfg]` traffic if platforms are brought on which don't directly support these functions.
1 parent 1ccb50e commit 348930e

File tree

8 files changed

+196
-133
lines changed

8 files changed

+196
-133
lines changed

src/libstd/f32.rs

+2-86
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ use core::num;
2323
use intrinsics;
2424
#[cfg(not(test))]
2525
use num::FpCategory;
26+
#[cfg(not(test))]
27+
use sys::cmath;
2628

2729

2830
#[stable(feature = "rust1", since = "1.0.0")]
@@ -36,92 +38,6 @@ pub use core::f32::{MIN, MIN_POSITIVE, MAX};
3638
#[stable(feature = "rust1", since = "1.0.0")]
3739
pub use core::f32::consts;
3840

39-
#[allow(dead_code)]
40-
mod cmath {
41-
use libc::{c_float, c_int};
42-
43-
extern {
44-
pub fn cbrtf(n: c_float) -> c_float;
45-
pub fn erff(n: c_float) -> c_float;
46-
pub fn erfcf(n: c_float) -> c_float;
47-
pub fn expm1f(n: c_float) -> c_float;
48-
pub fn fdimf(a: c_float, b: c_float) -> c_float;
49-
pub fn fmodf(a: c_float, b: c_float) -> c_float;
50-
pub fn ilogbf(n: c_float) -> c_int;
51-
pub fn logbf(n: c_float) -> c_float;
52-
pub fn log1pf(n: c_float) -> c_float;
53-
pub fn modff(n: c_float, iptr: &mut c_float) -> c_float;
54-
pub fn nextafterf(x: c_float, y: c_float) -> c_float;
55-
pub fn tgammaf(n: c_float) -> c_float;
56-
57-
#[cfg_attr(all(windows, target_env = "msvc"), link_name = "__lgammaf_r")]
58-
pub fn lgammaf_r(n: c_float, sign: &mut c_int) -> c_float;
59-
#[cfg_attr(all(windows, target_env = "msvc"), link_name = "_hypotf")]
60-
pub fn hypotf(x: c_float, y: c_float) -> c_float;
61-
}
62-
63-
// See the comments in the `floor` function for why MSVC is special
64-
// here.
65-
#[cfg(not(target_env = "msvc"))]
66-
extern {
67-
pub fn acosf(n: c_float) -> c_float;
68-
pub fn asinf(n: c_float) -> c_float;
69-
pub fn atan2f(a: c_float, b: c_float) -> c_float;
70-
pub fn atanf(n: c_float) -> c_float;
71-
pub fn coshf(n: c_float) -> c_float;
72-
pub fn sinhf(n: c_float) -> c_float;
73-
pub fn tanf(n: c_float) -> c_float;
74-
pub fn tanhf(n: c_float) -> c_float;
75-
}
76-
77-
#[cfg(target_env = "msvc")]
78-
pub use self::shims::*;
79-
#[cfg(target_env = "msvc")]
80-
mod shims {
81-
use libc::c_float;
82-
83-
#[inline]
84-
pub unsafe fn acosf(n: c_float) -> c_float {
85-
f64::acos(n as f64) as c_float
86-
}
87-
88-
#[inline]
89-
pub unsafe fn asinf(n: c_float) -> c_float {
90-
f64::asin(n as f64) as c_float
91-
}
92-
93-
#[inline]
94-
pub unsafe fn atan2f(n: c_float, b: c_float) -> c_float {
95-
f64::atan2(n as f64, b as f64) as c_float
96-
}
97-
98-
#[inline]
99-
pub unsafe fn atanf(n: c_float) -> c_float {
100-
f64::atan(n as f64) as c_float
101-
}
102-
103-
#[inline]
104-
pub unsafe fn coshf(n: c_float) -> c_float {
105-
f64::cosh(n as f64) as c_float
106-
}
107-
108-
#[inline]
109-
pub unsafe fn sinhf(n: c_float) -> c_float {
110-
f64::sinh(n as f64) as c_float
111-
}
112-
113-
#[inline]
114-
pub unsafe fn tanf(n: c_float) -> c_float {
115-
f64::tan(n as f64) as c_float
116-
}
117-
118-
#[inline]
119-
pub unsafe fn tanhf(n: c_float) -> c_float {
120-
f64::tanh(n as f64) as c_float
121-
}
122-
}
123-
}
124-
12541
#[cfg(not(test))]
12642
#[lang = "f32"]
12743
impl f32 {

src/libstd/f64.rs

+2-47
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ use core::num;
2323
use intrinsics;
2424
#[cfg(not(test))]
2525
use num::FpCategory;
26+
#[cfg(not(test))]
27+
use sys::cmath;
2628

2729
#[stable(feature = "rust1", since = "1.0.0")]
2830
pub use core::f64::{RADIX, MANTISSA_DIGITS, DIGITS, EPSILON};
@@ -35,53 +37,6 @@ pub use core::f64::{MIN, MIN_POSITIVE, MAX};
3537
#[stable(feature = "rust1", since = "1.0.0")]
3638
pub use core::f64::consts;
3739

38-
#[allow(dead_code)]
39-
mod cmath {
40-
use libc::{c_double, c_int};
41-
42-
#[link_name = "m"]
43-
extern {
44-
pub fn acos(n: c_double) -> c_double;
45-
pub fn asin(n: c_double) -> c_double;
46-
pub fn atan(n: c_double) -> c_double;
47-
pub fn atan2(a: c_double, b: c_double) -> c_double;
48-
pub fn cbrt(n: c_double) -> c_double;
49-
pub fn cosh(n: c_double) -> c_double;
50-
pub fn erf(n: c_double) -> c_double;
51-
pub fn erfc(n: c_double) -> c_double;
52-
pub fn expm1(n: c_double) -> c_double;
53-
pub fn fdim(a: c_double, b: c_double) -> c_double;
54-
pub fn fmod(a: c_double, b: c_double) -> c_double;
55-
pub fn frexp(n: c_double, value: &mut c_int) -> c_double;
56-
pub fn ilogb(n: c_double) -> c_int;
57-
pub fn ldexp(x: c_double, n: c_int) -> c_double;
58-
pub fn logb(n: c_double) -> c_double;
59-
pub fn log1p(n: c_double) -> c_double;
60-
pub fn nextafter(x: c_double, y: c_double) -> c_double;
61-
pub fn modf(n: c_double, iptr: &mut c_double) -> c_double;
62-
pub fn sinh(n: c_double) -> c_double;
63-
pub fn tan(n: c_double) -> c_double;
64-
pub fn tanh(n: c_double) -> c_double;
65-
pub fn tgamma(n: c_double) -> c_double;
66-
67-
// These are commonly only available for doubles
68-
69-
pub fn j0(n: c_double) -> c_double;
70-
pub fn j1(n: c_double) -> c_double;
71-
pub fn jn(i: c_int, n: c_double) -> c_double;
72-
73-
pub fn y0(n: c_double) -> c_double;
74-
pub fn y1(n: c_double) -> c_double;
75-
pub fn yn(i: c_int, n: c_double) -> c_double;
76-
77-
#[cfg_attr(all(windows, target_env = "msvc"), link_name = "__lgamma_r")]
78-
pub fn lgamma_r(n: c_double, sign: &mut c_int) -> c_double;
79-
80-
#[cfg_attr(all(windows, target_env = "msvc"), link_name = "_hypot")]
81-
pub fn hypot(x: c_double, y: c_double) -> c_double;
82-
}
83-
}
84-
8540
#[cfg(not(test))]
8641
#[lang = "f64"]
8742
impl f64 {

src/libstd/sys/redox/cmath.rs

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright 2017 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+
#![cfg(not(test))]
12+
13+
use libc::{c_float, c_double};
14+
15+
#[link_name = "m"]
16+
extern {
17+
pub fn acos(n: c_double) -> c_double;
18+
pub fn acosf(n: c_float) -> c_float;
19+
pub fn asin(n: c_double) -> c_double;
20+
pub fn asinf(n: c_float) -> c_float;
21+
pub fn atan(n: c_double) -> c_double;
22+
pub fn atan2(a: c_double, b: c_double) -> c_double;
23+
pub fn atan2f(a: c_float, b: c_float) -> c_float;
24+
pub fn atanf(n: c_float) -> c_float;
25+
pub fn cbrt(n: c_double) -> c_double;
26+
pub fn cbrtf(n: c_float) -> c_float;
27+
pub fn cosh(n: c_double) -> c_double;
28+
pub fn coshf(n: c_float) -> c_float;
29+
pub fn expm1(n: c_double) -> c_double;
30+
pub fn expm1f(n: c_float) -> c_float;
31+
pub fn fdim(a: c_double, b: c_double) -> c_double;
32+
pub fn fdimf(a: c_float, b: c_float) -> c_float;
33+
pub fn hypot(x: c_double, y: c_double) -> c_double;
34+
pub fn hypotf(x: c_float, y: c_float) -> c_float;
35+
pub fn log1p(n: c_double) -> c_double;
36+
pub fn log1pf(n: c_float) -> c_float;
37+
pub fn sinh(n: c_double) -> c_double;
38+
pub fn sinhf(n: c_float) -> c_float;
39+
pub fn tan(n: c_double) -> c_double;
40+
pub fn tanf(n: c_float) -> c_float;
41+
pub fn tanh(n: c_double) -> c_double;
42+
pub fn tanhf(n: c_float) -> c_float;
43+
}

src/libstd/sys/redox/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ pub use self::rand::hashmap_random_keys;
1717
pub mod args;
1818
#[cfg(feature = "backtrace")]
1919
pub mod backtrace;
20+
pub mod cmath;
2021
pub mod condvar;
2122
pub mod env;
2223
pub mod ext;

src/libstd/sys/unix/cmath.rs

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright 2017 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+
#![cfg(not(test))]
12+
13+
use libc::{c_float, c_double};
14+
15+
#[link_name = "m"]
16+
extern {
17+
pub fn acos(n: c_double) -> c_double;
18+
pub fn acosf(n: c_float) -> c_float;
19+
pub fn asin(n: c_double) -> c_double;
20+
pub fn asinf(n: c_float) -> c_float;
21+
pub fn atan(n: c_double) -> c_double;
22+
pub fn atan2(a: c_double, b: c_double) -> c_double;
23+
pub fn atan2f(a: c_float, b: c_float) -> c_float;
24+
pub fn atanf(n: c_float) -> c_float;
25+
pub fn cbrt(n: c_double) -> c_double;
26+
pub fn cbrtf(n: c_float) -> c_float;
27+
pub fn cosh(n: c_double) -> c_double;
28+
pub fn coshf(n: c_float) -> c_float;
29+
pub fn expm1(n: c_double) -> c_double;
30+
pub fn expm1f(n: c_float) -> c_float;
31+
pub fn fdim(a: c_double, b: c_double) -> c_double;
32+
pub fn fdimf(a: c_float, b: c_float) -> c_float;
33+
pub fn hypot(x: c_double, y: c_double) -> c_double;
34+
pub fn hypotf(x: c_float, y: c_float) -> c_float;
35+
pub fn log1p(n: c_double) -> c_double;
36+
pub fn log1pf(n: c_float) -> c_float;
37+
pub fn sinh(n: c_double) -> c_double;
38+
pub fn sinhf(n: c_float) -> c_float;
39+
pub fn tan(n: c_double) -> c_double;
40+
pub fn tanf(n: c_float) -> c_float;
41+
pub fn tanh(n: c_double) -> c_double;
42+
pub fn tanhf(n: c_float) -> c_float;
43+
}

src/libstd/sys/unix/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ pub mod args;
3838
pub mod android;
3939
#[cfg(feature = "backtrace")]
4040
pub mod backtrace;
41+
pub mod cmath;
4142
pub mod condvar;
4243
pub mod env;
4344
pub mod ext;

src/libstd/sys/windows/cmath.rs

+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
// Copyright 2017 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+
#![cfg(not(test))]
12+
13+
use libc::{c_float, c_double};
14+
15+
#[link_name = "m"]
16+
extern {
17+
pub fn acos(n: c_double) -> c_double;
18+
pub fn asin(n: c_double) -> c_double;
19+
pub fn atan(n: c_double) -> c_double;
20+
pub fn atan2(a: c_double, b: c_double) -> c_double;
21+
pub fn cbrt(n: c_double) -> c_double;
22+
pub fn cbrtf(n: c_float) -> c_float;
23+
pub fn cosh(n: c_double) -> c_double;
24+
pub fn expm1(n: c_double) -> c_double;
25+
pub fn expm1f(n: c_float) -> c_float;
26+
pub fn fdim(a: c_double, b: c_double) -> c_double;
27+
pub fn fdimf(a: c_float, b: c_float) -> c_float;
28+
#[cfg_attr(target_env = "msvc", link_name = "_hypot")]
29+
pub fn hypot(x: c_double, y: c_double) -> c_double;
30+
#[cfg_attr(target_env = "msvc", link_name = "_hypotf")]
31+
pub fn hypotf(x: c_float, y: c_float) -> c_float;
32+
pub fn log1p(n: c_double) -> c_double;
33+
pub fn log1pf(n: c_float) -> c_float;
34+
pub fn sinh(n: c_double) -> c_double;
35+
pub fn tan(n: c_double) -> c_double;
36+
pub fn tanh(n: c_double) -> c_double;
37+
}
38+
39+
pub use self::shims::*;
40+
41+
#[cfg(not(target_env = "msvc"))]
42+
mod shims {
43+
use libc::c_float;
44+
45+
extern {
46+
pub fn acosf(n: c_float) -> c_float;
47+
pub fn asinf(n: c_float) -> c_float;
48+
pub fn atan2f(a: c_float, b: c_float) -> c_float;
49+
pub fn atanf(n: c_float) -> c_float;
50+
pub fn coshf(n: c_float) -> c_float;
51+
pub fn sinhf(n: c_float) -> c_float;
52+
pub fn tanf(n: c_float) -> c_float;
53+
pub fn tanhf(n: c_float) -> c_float;
54+
}
55+
}
56+
57+
// On MSVC these functions aren't defined, so we just define shims which promote
58+
// everything fo f64, perform the calculation, and then demote back to f32.
59+
// While not precisely correct should be "correct enough" for now.
60+
#[cfg(target_env = "msvc")]
61+
mod shims {
62+
use libc::c_float;
63+
64+
#[inline]
65+
pub unsafe fn acosf(n: c_float) -> c_float {
66+
f64::acos(n as f64) as c_float
67+
}
68+
69+
#[inline]
70+
pub unsafe fn asinf(n: c_float) -> c_float {
71+
f64::asin(n as f64) as c_float
72+
}
73+
74+
#[inline]
75+
pub unsafe fn atan2f(n: c_float, b: c_float) -> c_float {
76+
f64::atan2(n as f64, b as f64) as c_float
77+
}
78+
79+
#[inline]
80+
pub unsafe fn atanf(n: c_float) -> c_float {
81+
f64::atan(n as f64) as c_float
82+
}
83+
84+
#[inline]
85+
pub unsafe fn coshf(n: c_float) -> c_float {
86+
f64::cosh(n as f64) as c_float
87+
}
88+
89+
#[inline]
90+
pub unsafe fn sinhf(n: c_float) -> c_float {
91+
f64::sinh(n as f64) as c_float
92+
}
93+
94+
#[inline]
95+
pub unsafe fn tanf(n: c_float) -> c_float {
96+
f64::tan(n as f64) as c_float
97+
}
98+
99+
#[inline]
100+
pub unsafe fn tanhf(n: c_float) -> c_float {
101+
f64::tanh(n as f64) as c_float
102+
}
103+
}

src/libstd/sys/windows/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ pub mod args;
2525
#[cfg(feature = "backtrace")]
2626
pub mod backtrace;
2727
pub mod c;
28+
pub mod cmath;
2829
pub mod condvar;
2930
#[cfg(feature = "backtrace")]
3031
pub mod dynamic_lib;

0 commit comments

Comments
 (0)