Skip to content

Commit f64bc31

Browse files
authored
Rollup merge of #120740 - ChrisDenton:cmaths, r=Mark-Simulacrum
Make cmath.rs a single file It makes sense to have this all in one file. There's essentially only one target that has missing symbols and that's easy enough to handle inline. Note that the Windows definitions used to use `c_float` and `c_double` whereas the other platforms all used `f32` and `f64`. They've now been made consistent. However, `c_float` and `c_double` have the expected definitions on all Windows platforms we support.
2 parents 251a09e + be9ac56 commit f64bc31

File tree

4 files changed

+88
-140
lines changed

4 files changed

+88
-140
lines changed

library/std/src/sys/cmath.rs

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#![cfg(not(test))]
2+
3+
// These symbols are all defined by `libm`,
4+
// or by `compiler-builtins` on unsupported platforms.
5+
extern "C" {
6+
pub fn acos(n: f64) -> f64;
7+
pub fn asin(n: f64) -> f64;
8+
pub fn atan(n: f64) -> f64;
9+
pub fn atan2(a: f64, b: f64) -> f64;
10+
pub fn cbrt(n: f64) -> f64;
11+
pub fn cbrtf(n: f32) -> f32;
12+
pub fn cosh(n: f64) -> f64;
13+
pub fn expm1(n: f64) -> f64;
14+
pub fn expm1f(n: f32) -> f32;
15+
pub fn fdim(a: f64, b: f64) -> f64;
16+
pub fn fdimf(a: f32, b: f32) -> f32;
17+
#[cfg_attr(target_env = "msvc", link_name = "_hypot")]
18+
pub fn hypot(x: f64, y: f64) -> f64;
19+
#[cfg_attr(target_env = "msvc", link_name = "_hypotf")]
20+
pub fn hypotf(x: f32, y: f32) -> f32;
21+
pub fn log1p(n: f64) -> f64;
22+
pub fn log1pf(n: f32) -> f32;
23+
pub fn sinh(n: f64) -> f64;
24+
pub fn tan(n: f64) -> f64;
25+
pub fn tanh(n: f64) -> f64;
26+
pub fn tgamma(n: f64) -> f64;
27+
pub fn tgammaf(n: f32) -> f32;
28+
pub fn lgamma_r(n: f64, s: &mut i32) -> f64;
29+
pub fn lgammaf_r(n: f32, s: &mut i32) -> f32;
30+
31+
cfg_if::cfg_if! {
32+
if #[cfg(not(all(target_os = "windows", target_env = "msvc", target_arch = "x86")))] {
33+
pub fn acosf(n: f32) -> f32;
34+
pub fn asinf(n: f32) -> f32;
35+
pub fn atan2f(a: f32, b: f32) -> f32;
36+
pub fn atanf(n: f32) -> f32;
37+
pub fn coshf(n: f32) -> f32;
38+
pub fn sinhf(n: f32) -> f32;
39+
pub fn tanf(n: f32) -> f32;
40+
pub fn tanhf(n: f32) -> f32;
41+
}}
42+
}
43+
44+
// On 32-bit x86 MSVC these functions aren't defined, so we just define shims
45+
// which promote everything to f64, perform the calculation, and then demote
46+
// back to f32. While not precisely correct should be "correct enough" for now.
47+
cfg_if::cfg_if! {
48+
if #[cfg(all(target_os = "windows", target_env = "msvc", target_arch = "x86"))] {
49+
#[inline]
50+
pub unsafe fn acosf(n: f32) -> f32 {
51+
f64::acos(n as f64) as f32
52+
}
53+
54+
#[inline]
55+
pub unsafe fn asinf(n: f32) -> f32 {
56+
f64::asin(n as f64) as f32
57+
}
58+
59+
#[inline]
60+
pub unsafe fn atan2f(n: f32, b: f32) -> f32 {
61+
f64::atan2(n as f64, b as f64) as f32
62+
}
63+
64+
#[inline]
65+
pub unsafe fn atanf(n: f32) -> f32 {
66+
f64::atan(n as f64) as f32
67+
}
68+
69+
#[inline]
70+
pub unsafe fn coshf(n: f32) -> f32 {
71+
f64::cosh(n as f64) as f32
72+
}
73+
74+
#[inline]
75+
pub unsafe fn sinhf(n: f32) -> f32 {
76+
f64::sinh(n as f64) as f32
77+
}
78+
79+
#[inline]
80+
pub unsafe fn tanf(n: f32) -> f32 {
81+
f64::tan(n as f64) as f32
82+
}
83+
84+
#[inline]
85+
pub unsafe fn tanhf(n: f32) -> f32 {
86+
f64::tanh(n as f64) as f32
87+
}
88+
}}

library/std/src/sys/cmath/builtins.rs

-35
This file was deleted.

library/std/src/sys/cmath/mod.rs

-11
This file was deleted.

library/std/src/sys/cmath/windows.rs

-94
This file was deleted.

0 commit comments

Comments
 (0)