Skip to content

Commit

Permalink
Always have math functions but with weak linking attribute if we can
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Amjad50 authored and Amanieu committed Apr 10, 2024
1 parent 63976cb commit 018616e
Showing 1 changed file with 17 additions and 67 deletions.
84 changes: 17 additions & 67 deletions src/math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ macro_rules! no_mangle {
($(fn $fun:ident($($iid:ident : $ity:ty),+) -> $oty:ty;)+) => {
intrinsics! {
$(
#[cfg_attr(all(not(windows), not(target_vendor = "apple")), weak)]
pub extern "C" fn $fun($($iid: $ity),+) -> $oty {
self::libm::$fun($($iid),+)
}
Expand All @@ -15,17 +16,6 @@ macro_rules! no_mangle {
}
}

#[cfg(any(
all(
target_family = "wasm",
target_os = "unknown",
not(target_env = "wasi")
),
target_os = "xous",
target_os = "uefi",
all(target_arch = "xtensa", target_os = "none"),
all(target_vendor = "fortanix", target_env = "sgx")
))]
no_mangle! {
fn acos(x: f64) -> f64;
fn asin(x: f64) -> f64;
Expand All @@ -41,19 +31,13 @@ no_mangle! {
fn log10f(x: f32) -> f32;
fn log(x: f64) -> f64;
fn logf(x: f32) -> f32;
fn fmin(x: f64, y: f64) -> f64;
fn fminf(x: f32, y: f32) -> f32;
fn fmax(x: f64, y: f64) -> f64;
fn fmaxf(x: f32, y: f32) -> f32;
fn round(x: f64) -> f64;
fn roundf(x: f32) -> f32;
fn rint(x: f64) -> f64;
fn rintf(x: f32) -> f32;
fn sin(x: f64) -> f64;
fn pow(x: f64, y: f64) -> f64;
fn powf(x: f32, y: f32) -> f32;
fn fmod(x: f64, y: f64) -> f64;
fn fmodf(x: f32, y: f32) -> f32;
fn acosf(n: f32) -> f32;
fn atan2f(a: f32, b: f32) -> f32;
fn atanf(n: f32) -> f32;
Expand Down Expand Up @@ -85,67 +69,17 @@ no_mangle! {
fn cbrtf(n: f32) -> f32;
fn hypotf(x: f32, y: f32) -> f32;
fn tanf(n: f32) -> f32;
}

#[cfg(any(
all(
target_family = "wasm",
target_os = "unknown",
not(target_env = "wasi")
),
target_os = "xous",
target_os = "uefi",
all(target_arch = "xtensa", target_os = "none"),
all(target_vendor = "fortanix", target_env = "sgx"),
target_os = "windows"
))]
intrinsics! {
pub extern "C" fn lgamma_r(x: f64, s: &mut i32) -> f64 {
let r = self::libm::lgamma_r(x);
*s = r.1;
r.0
}

pub extern "C" fn lgammaf_r(x: f32, s: &mut i32) -> f32 {
let r = self::libm::lgammaf_r(x);
*s = r.1;
r.0
}
}

#[cfg(any(
target_os = "xous",
target_os = "uefi",
all(target_arch = "xtensa", target_os = "none"),
))]
no_mangle! {
fn sqrtf(x: f32) -> f32;
fn sqrt(x: f64) -> f64;
}

#[cfg(any(
all(target_vendor = "fortanix", target_env = "sgx"),
all(target_arch = "xtensa", target_os = "none"),
target_os = "xous",
target_os = "uefi"
))]
no_mangle! {
fn ceil(x: f64) -> f64;
fn ceilf(x: f32) -> f32;
fn floor(x: f64) -> f64;
fn floorf(x: f32) -> f32;
fn trunc(x: f64) -> f64;
fn truncf(x: f32) -> f32;
}

// 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
#[cfg(any(
all(target_arch = "arm", target_os = "none"),
all(target_arch = "riscv32", not(target_feature = "f"), target_os = "none"),
all(target_arch = "x86_64", target_os = "none"),
all(target_arch = "mips", target_os = "none"),
))]
no_mangle! {
fn fmin(x: f64, y: f64) -> f64;
fn fminf(x: f32, y: f32) -> f32;
fn fmax(x: f64, y: f64) -> f64;
Expand All @@ -155,3 +89,19 @@ no_mangle! {
// `f32 % f32`
fn fmodf(x: f32, y: f32) -> f32;
}

intrinsics! {
#[cfg_attr(all(not(windows), not(target_vendor = "apple")), weak)]
pub extern "C" fn lgamma_r(x: f64, s: &mut i32) -> f64 {
let r = self::libm::lgamma_r(x);
*s = r.1;
r.0
}

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

0 comments on commit 018616e

Please sign in to comment.