Skip to content

Commit 3f65b14

Browse files
committed
Always have math functions but with weak linking attribute if we can
This is a replacement for rust-lang/libm#290 This fixes crashes during compilations for targets that don't have math symbols by default. So, we will provide them libm symbols, but mark it as `weak` (if its supported), so that the linker will choose the system builtin functions, since those are sometimes more optimized. If the linker couldn't find those, it will go with `libm` implementation.
1 parent 8ed91cd commit 3f65b14

File tree

1 file changed

+17
-67
lines changed

1 file changed

+17
-67
lines changed

src/math.rs

+17-67
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ macro_rules! no_mangle {
77
($(fn $fun:ident($($iid:ident : $ity:ty),+) -> $oty:ty;)+) => {
88
intrinsics! {
99
$(
10+
#[cfg_attr(all(not(windows), not(target_vendor = "apple")), weak)]
1011
pub extern "C" fn $fun($($iid: $ity),+) -> $oty {
1112
self::libm::$fun($($iid),+)
1213
}
@@ -15,17 +16,6 @@ macro_rules! no_mangle {
1516
}
1617
}
1718

18-
#[cfg(any(
19-
all(
20-
target_family = "wasm",
21-
target_os = "unknown",
22-
not(target_env = "wasi")
23-
),
24-
target_os = "xous",
25-
target_os = "uefi",
26-
all(target_arch = "xtensa", target_os = "none"),
27-
all(target_vendor = "fortanix", target_env = "sgx")
28-
))]
2919
no_mangle! {
3020
fn acos(x: f64) -> f64;
3121
fn asin(x: f64) -> f64;
@@ -41,19 +31,13 @@ no_mangle! {
4131
fn log10f(x: f32) -> f32;
4232
fn log(x: f64) -> f64;
4333
fn logf(x: f32) -> f32;
44-
fn fmin(x: f64, y: f64) -> f64;
45-
fn fminf(x: f32, y: f32) -> f32;
46-
fn fmax(x: f64, y: f64) -> f64;
47-
fn fmaxf(x: f32, y: f32) -> f32;
4834
fn round(x: f64) -> f64;
4935
fn roundf(x: f32) -> f32;
5036
fn rint(x: f64) -> f64;
5137
fn rintf(x: f32) -> f32;
5238
fn sin(x: f64) -> f64;
5339
fn pow(x: f64, y: f64) -> f64;
5440
fn powf(x: f32, y: f32) -> f32;
55-
fn fmod(x: f64, y: f64) -> f64;
56-
fn fmodf(x: f32, y: f32) -> f32;
5741
fn acosf(n: f32) -> f32;
5842
fn atan2f(a: f32, b: f32) -> f32;
5943
fn atanf(n: f32) -> f32;
@@ -85,67 +69,17 @@ no_mangle! {
8569
fn cbrtf(n: f32) -> f32;
8670
fn hypotf(x: f32, y: f32) -> f32;
8771
fn tanf(n: f32) -> f32;
88-
}
89-
90-
#[cfg(any(
91-
all(
92-
target_family = "wasm",
93-
target_os = "unknown",
94-
not(target_env = "wasi")
95-
),
96-
target_os = "xous",
97-
target_os = "uefi",
98-
all(target_arch = "xtensa", target_os = "none"),
99-
all(target_vendor = "fortanix", target_env = "sgx"),
100-
target_os = "windows"
101-
))]
102-
intrinsics! {
103-
pub extern "C" fn lgamma_r(x: f64, s: &mut i32) -> f64 {
104-
let r = self::libm::lgamma_r(x);
105-
*s = r.1;
106-
r.0
107-
}
108-
109-
pub extern "C" fn lgammaf_r(x: f32, s: &mut i32) -> f32 {
110-
let r = self::libm::lgammaf_r(x);
111-
*s = r.1;
112-
r.0
113-
}
114-
}
11572

116-
#[cfg(any(
117-
target_os = "xous",
118-
target_os = "uefi",
119-
all(target_arch = "xtensa", target_os = "none"),
120-
))]
121-
no_mangle! {
12273
fn sqrtf(x: f32) -> f32;
12374
fn sqrt(x: f64) -> f64;
124-
}
12575

126-
#[cfg(any(
127-
all(target_vendor = "fortanix", target_env = "sgx"),
128-
all(target_arch = "xtensa", target_os = "none"),
129-
target_os = "xous",
130-
target_os = "uefi"
131-
))]
132-
no_mangle! {
13376
fn ceil(x: f64) -> f64;
13477
fn ceilf(x: f32) -> f32;
13578
fn floor(x: f64) -> f64;
13679
fn floorf(x: f32) -> f32;
13780
fn trunc(x: f64) -> f64;
13881
fn truncf(x: f32) -> f32;
139-
}
14082

141-
// only for the thumb*-none-eabi*, riscv32*-none-elf, x86_64-unknown-none and mips*-unknown-none targets that lack the floating point instruction set
142-
#[cfg(any(
143-
all(target_arch = "arm", target_os = "none"),
144-
all(target_arch = "riscv32", not(target_feature = "f"), target_os = "none"),
145-
all(target_arch = "x86_64", target_os = "none"),
146-
all(target_arch = "mips", target_os = "none"),
147-
))]
148-
no_mangle! {
14983
fn fmin(x: f64, y: f64) -> f64;
15084
fn fminf(x: f32, y: f32) -> f32;
15185
fn fmax(x: f64, y: f64) -> f64;
@@ -155,3 +89,19 @@ no_mangle! {
15589
// `f32 % f32`
15690
fn fmodf(x: f32, y: f32) -> f32;
15791
}
92+
93+
intrinsics! {
94+
#[cfg_attr(all(not(windows), not(target_vendor = "apple")), weak)]
95+
pub extern "C" fn lgamma_r(x: f64, s: &mut i32) -> f64 {
96+
let r = self::libm::lgamma_r(x);
97+
*s = r.1;
98+
r.0
99+
}
100+
101+
#[cfg_attr(all(not(windows), not(target_vendor = "apple")), weak)]
102+
pub extern "C" fn lgammaf_r(x: f32, s: &mut i32) -> f32 {
103+
let r = self::libm::lgammaf_r(x);
104+
*s = r.1;
105+
r.0
106+
}
107+
}

0 commit comments

Comments
 (0)