macro_rules! unary {
($($double:ident $single:ident $method:ident),*) => {
$(
#[inline(always)]
pub fn $double(x: f64) -> f64 {
x.$method()
}
#[inline(always)]
pub fn $single(x: f32) -> f32 {
x.$method()
}
)*
const UNARY: &[&str] = &[$(stringify!($double), stringify!($single)),*];
};
}
unary!(
sin sinf sin, cos cosf cos, tan tanf tan, asin asinf asin, acos acosf acos, atan atanf atan,
sinh sinhf sinh, cosh coshf cosh, tanh tanhf tanh, asinh asinhf asinh, acosh acoshf acosh,
atanh atanhf atanh, exp expf exp, exp2 exp2f exp2, log logf ln, log2 log2f log2, log10 log10f log10,
sqrt sqrtf sqrt, ceil ceilf ceil, floor floorf floor, round roundf round,
fabs fabsf abs
);
#[inline(always)]
pub fn cbrt(x: f64) -> f64 {
c::cbrt(x)
}
#[inline(always)]
pub fn cbrtf(x: f32) -> f32 {
c::cbrtf(x)
}
#[cfg(all(target_os = "linux", target_env = "gnu"))]
mod c {
#[link(name = "m")]
unsafe extern "C" {
#[link_name = "cbrtf64"]
pub safe fn cbrt(x: f64) -> f64;
#[link_name = "cbrtf32"]
pub safe fn cbrtf(x: f32) -> f32;
}
}
#[cfg(not(all(target_os = "linux", target_env = "gnu")))]
mod c {
#[inline(always)]
pub fn cbrt(x: f64) -> f64 {
x.cbrt()
}
#[inline(always)]
pub fn cbrtf(x: f32) -> f32 {
x.cbrt()
}
}
#[inline(always)]
pub fn pow(x: f64, y: f64) -> f64 {
x.powf(y)
}
#[inline(always)]
pub fn powf(x: f32, y: f32) -> f32 {
x.powf(y)
}
#[inline(always)]
pub fn atan2(y: f64, x: f64) -> f64 {
y.atan2(x)
}
#[inline(always)]
pub fn atan2f(y: f32, x: f32) -> f32 {
y.atan2(x)
}
pub(crate) fn has(name: &str) -> bool {
UNARY.contains(&name) || matches!(name, "cbrt" | "cbrtf" | "pow" | "powf" | "atan2" | "atan2f")
}