Skip to content

Commit 70e6d69

Browse files
committed
fix no_std + libm
1 parent b7780e6 commit 70e6d69

File tree

2 files changed

+138
-4
lines changed

2 files changed

+138
-4
lines changed

src/float.rs

Lines changed: 128 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -852,8 +852,20 @@ impl FloatCore for f16 {
852852
Self::powi(self, n: i32) -> Self;
853853
}
854854

855-
// TODO:
856-
// use floor, ceil, round, trunc, abs and fract provided by libm
855+
#[cfg(all(not(feature = "std"), feature = "libm"))]
856+
cast_forward_cast! {
857+
[f32] libm::floorf as floor(self) -> Self;
858+
[f32] libm::ceilf as ceil(self) -> Self;
859+
[f32] libm::roundf as round(self) -> Self;
860+
[f32] libm::truncf as trunc(self) -> Self;
861+
[f32] libm::fabsf as abs(self) -> Self;
862+
}
863+
864+
#[cfg(all(not(feature = "std"), feature = "libm"))]
865+
#[inline]
866+
fn fract(self) -> Self {
867+
self - libm::truncf(self as f32) as Self
868+
}
857869
}
858870

859871
impl FloatCore for f32 {
@@ -1026,8 +1038,20 @@ impl FloatCore for f128 {
10261038
Self::powi(self, n: i32) -> Self;
10271039
}
10281040

1029-
// TODO:
1030-
// use floor, ceil, round, trunc, abs and fract provided by libm
1041+
#[cfg(all(not(feature = "std"), feature = "libm"))]
1042+
cast_forward_cast! {
1043+
[f64] libm::floor as floor(self) -> Self;
1044+
[f64] libm::ceil as ceil(self) -> Self;
1045+
[f64] libm::round as round(self) -> Self;
1046+
[f64] libm::trunc as trunc(self) -> Self;
1047+
[f64] libm::fabs as abs(self) -> Self;
1048+
}
1049+
1050+
#[cfg(all(not(feature = "std"), feature = "libm"))]
1051+
#[inline]
1052+
fn fract(self) -> Self {
1053+
self - libm::trunc(self as f64) as Self
1054+
}
10311055
}
10321056

10331057
// FIXME: these doctests aren't actually helpful, because they're using and
@@ -2232,6 +2256,56 @@ float_impl_std!(f64 integer_decode_f64);
22322256
#[cfg(feature = "std")]
22332257
float_impl_std!(f128 integer_decode_f128_truncated);
22342258

2259+
#[cfg(all(not(feature = "std"), feature = "libm"))]
2260+
impl Float for f16 {
2261+
float_impl_libm!(f16 integer_decode_f16);
2262+
2263+
#[inline]
2264+
#[allow(deprecated)]
2265+
fn abs_sub(self, other: Self) -> Self {
2266+
libm::fdimf(self as f32, other as f32) as Self
2267+
}
2268+
2269+
cast_forward_cast! {
2270+
[f32] libm::floorf as floor(self) -> Self;
2271+
[f32] libm::ceilf as ceil(self) -> Self;
2272+
[f32] libm::roundf as round(self) -> Self;
2273+
[f32] libm::truncf as trunc(self) -> Self;
2274+
[f32] libm::fabsf as abs(self) -> Self;
2275+
[f32] libm::fmaf as mul_add(self, a: Self, b: Self) -> Self;
2276+
[f32] libm::powf as powf(self, n: Self) -> Self;
2277+
[f32] libm::sqrtf as sqrt(self) -> Self;
2278+
[f32] libm::expf as exp(self) -> Self;
2279+
[f32] libm::exp2f as exp2(self) -> Self;
2280+
[f32] libm::logf as ln(self) -> Self;
2281+
[f32] libm::log2f as log2(self) -> Self;
2282+
[f32] libm::log10f as log10(self) -> Self;
2283+
[f32] libm::cbrtf as cbrt(self) -> Self;
2284+
[f32] libm::hypotf as hypot(self, other: Self) -> Self;
2285+
[f32] libm::sinf as sin(self) -> Self;
2286+
[f32] libm::cosf as cos(self) -> Self;
2287+
[f32] libm::tanf as tan(self) -> Self;
2288+
[f32] libm::asinf as asin(self) -> Self;
2289+
[f32] libm::acosf as acos(self) -> Self;
2290+
[f32] libm::atanf as atan(self) -> Self;
2291+
[f32] libm::atan2f as atan2(self, other: Self) -> Self;
2292+
[f32] libm::expm1f as exp_m1(self) -> Self;
2293+
[f32] libm::log1pf as ln_1p(self) -> Self;
2294+
[f32] libm::sinhf as sinh(self) -> Self;
2295+
[f32] libm::coshf as cosh(self) -> Self;
2296+
[f32] libm::tanhf as tanh(self) -> Self;
2297+
[f32] libm::asinhf as asinh(self) -> Self;
2298+
[f32] libm::acoshf as acosh(self) -> Self;
2299+
[f32] libm::atanhf as atanh(self) -> Self;
2300+
[f32] libm::copysignf as copysign(self, other: Self) -> Self;
2301+
}
2302+
#[inline]
2303+
fn sin_cos(self) -> (Self, Self) {
2304+
let (x, y) = libm::sincosf(self as f32);
2305+
(x as Self, y as Self)
2306+
}
2307+
}
2308+
22352309
#[cfg(all(not(feature = "std"), feature = "libm"))]
22362310
impl Float for f32 {
22372311
float_impl_libm!(f32 integer_decode_f32);
@@ -2324,6 +2398,56 @@ impl Float for f64 {
23242398
}
23252399
}
23262400

2401+
#[cfg(all(not(feature = "std"), feature = "libm"))]
2402+
impl Float for f128 {
2403+
float_impl_libm!(f128 integer_decode_f128_truncated);
2404+
2405+
#[inline]
2406+
#[allow(deprecated)]
2407+
fn abs_sub(self, other: Self) -> Self {
2408+
libm::fdimf(self as f32, other as f32) as Self
2409+
}
2410+
2411+
cast_forward_cast! {
2412+
[f64] libm::floor as floor(self) -> Self;
2413+
[f64] libm::ceil as ceil(self) -> Self;
2414+
[f64] libm::round as round(self) -> Self;
2415+
[f64] libm::trunc as trunc(self) -> Self;
2416+
[f64] libm::fabs as abs(self) -> Self;
2417+
[f64] libm::fma as mul_add(self, a: Self, b: Self) -> Self;
2418+
[f64] libm::pow as powf(self, n: Self) -> Self;
2419+
[f64] libm::sqrt as sqrt(self) -> Self;
2420+
[f64] libm::exp as exp(self) -> Self;
2421+
[f64] libm::exp2 as exp2(self) -> Self;
2422+
[f64] libm::log as ln(self) -> Self;
2423+
[f64] libm::log2 as log2(self) -> Self;
2424+
[f64] libm::log10 as log10(self) -> Self;
2425+
[f64] libm::cbrt as cbrt(self) -> Self;
2426+
[f64] libm::hypot as hypot(self, other: Self) -> Self;
2427+
[f64] libm::sin as sin(self) -> Self;
2428+
[f64] libm::cos as cos(self) -> Self;
2429+
[f64] libm::tan as tan(self) -> Self;
2430+
[f64] libm::asin as asin(self) -> Self;
2431+
[f64] libm::acos as acos(self) -> Self;
2432+
[f64] libm::atan as atan(self) -> Self;
2433+
[f64] libm::atan2 as atan2(self, other: Self) -> Self;
2434+
[f64] libm::expm1 as exp_m1(self) -> Self;
2435+
[f64] libm::log1p as ln_1p(self) -> Self;
2436+
[f64] libm::sinh as sinh(self) -> Self;
2437+
[f64] libm::cosh as cosh(self) -> Self;
2438+
[f64] libm::tanh as tanh(self) -> Self;
2439+
[f64] libm::asinh as asinh(self) -> Self;
2440+
[f64] libm::acosh as acosh(self) -> Self;
2441+
[f64] libm::atanh as atanh(self) -> Self;
2442+
[f64] libm::copysign as copysign(self, sign: Self) -> Self;
2443+
}
2444+
#[inline]
2445+
fn sin_cos(self) -> (Self, Self) {
2446+
let (x, y) = libm::sincos(self as f64);
2447+
(x as Self, y as Self)
2448+
}
2449+
}
2450+
23272451
macro_rules! float_const_impl {
23282452
($(#[$doc:meta] $constant:ident,)+) => (
23292453
#[allow(non_snake_case)]

src/macros.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,16 @@ macro_rules! forward {
3333
)*};
3434
}
3535

36+
macro_rules! cast_forward_cast {
37+
($( [$cast:ty] $imp:path as $method:ident ( self $( , $arg:ident : $ty:ty )* ) -> $ret:ty ; )*)
38+
=> {$(
39+
#[inline]
40+
fn $method(self $( , $arg : $ty )* ) -> $ret {
41+
$imp(self as $cast $( , $arg as $cast)* ) as $ret
42+
}
43+
)*};
44+
}
45+
3646
macro_rules! constant {
3747
($( $method:ident () -> $ret:expr ; )*)
3848
=> {$(

0 commit comments

Comments
 (0)