Skip to content

Commit 9f92a60

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 351d48e commit 9f92a60

File tree

1 file changed

+15
-52
lines changed

1 file changed

+15
-52
lines changed

src/math.rs

+15-52
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;
@@ -85,65 +75,38 @@ no_mangle! {
8575
fn cbrtf(n: f32) -> f32;
8676
fn hypotf(x: f32, y: f32) -> f32;
8777
fn tanf(n: f32) -> f32;
78+
fn sqrtf(x: f32) -> f32;
79+
fn sqrt(x: f64) -> f64;
80+
fn ceil(x: f64) -> f64;
81+
fn ceilf(x: f32) -> f32;
82+
fn floor(x: f64) -> f64;
83+
fn floorf(x: f32) -> f32;
84+
fn trunc(x: f64) -> f64;
85+
fn truncf(x: f32) -> f32;
8886
}
8987

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-
))]
10288
intrinsics! {
89+
#[cfg_attr(all(not(windows), not(target_vendor="apple")), weak)]
10390
pub extern "C" fn lgamma_r(x: f64, s: &mut i32) -> f64 {
10491
let r = self::libm::lgamma_r(x);
10592
*s = r.1;
10693
r.0
10794
}
10895

96+
#[cfg_attr(all(not(windows), not(target_vendor="apple")), weak)]
10997
pub extern "C" fn lgammaf_r(x: f32, s: &mut i32) -> f32 {
11098
let r = self::libm::lgammaf_r(x);
11199
*s = r.1;
112100
r.0
113101
}
114102
}
115103

116-
#[cfg(any(
117-
target_os = "xous",
118-
target_os = "uefi",
119-
all(target_arch = "xtensa", target_os = "none"),
120-
))]
121-
no_mangle! {
122-
fn sqrtf(x: f32) -> f32;
123-
fn sqrt(x: f64) -> f64;
124-
}
125-
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! {
133-
fn ceil(x: f64) -> f64;
134-
fn ceilf(x: f32) -> f32;
135-
fn floor(x: f64) -> f64;
136-
fn floorf(x: f32) -> f32;
137-
fn trunc(x: f64) -> f64;
138-
fn truncf(x: f32) -> f32;
139-
}
140-
141104
// 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
142105
#[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"),
106+
target_arch = "arm",
107+
all(target_arch = "riscv32", not(target_feature = "f")),
108+
target_arch = "x86_64",
109+
target_arch = "mips",
147110
))]
148111
no_mangle! {
149112
fn fmin(x: f64, y: f64) -> f64;

0 commit comments

Comments
 (0)