Skip to content

Commit 3deee50

Browse files
committed
fix no_std + libm
1 parent b7780e6 commit 3deee50

File tree

3 files changed

+164
-5
lines changed

3 files changed

+164
-5
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ features = ["std"]
2020
rustdoc-args = ["--generate-link-to-definition"]
2121

2222
[dependencies]
23-
libm = { version = "0.2.0", optional = true }
23+
libm = { version = "0.2.0", optional = true, features = ["unstable-float"] }
2424

2525
[features]
2626
default = ["std"]

src/float.rs

Lines changed: 146 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+
forward! {
857+
libm::floorf16 as floor(self) -> Self;
858+
libm::ceilf16 as ceil(self) -> Self;
859+
libm::roundf16 as round(self) -> Self;
860+
libm::truncf16 as trunc(self) -> Self;
861+
libm::fabsf16 as abs(self) -> Self;
862+
}
863+
864+
#[cfg(all(not(feature = "std"), feature = "libm"))]
865+
#[inline]
866+
fn fract(self) -> Self {
867+
self - libm::truncf16(self)
868+
}
857869
}
858870

859871
impl FloatCore for f32 {
@@ -1026,8 +1038,24 @@ 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+
forward! {
1043+
libm::floorf128 as floor(self) -> Self;
1044+
libm::ceilf128 as ceil(self) -> Self;
1045+
libm::roundf128 as round(self) -> Self;
1046+
libm::truncf128 as trunc(self) -> Self;
1047+
}
1048+
1049+
#[cfg(all(not(feature = "std"), feature = "libm"))]
1050+
forward! {
1051+
f128::abs as abs(self) -> Self;
1052+
}
1053+
1054+
#[cfg(all(not(feature = "std"), feature = "libm"))]
1055+
#[inline]
1056+
fn fract(self) -> Self {
1057+
self - libm::truncf128(self)
1058+
}
10311059
}
10321060

10331061
// FIXME: these doctests aren't actually helpful, because they're using and
@@ -2232,6 +2260,63 @@ float_impl_std!(f64 integer_decode_f64);
22322260
#[cfg(feature = "std")]
22332261
float_impl_std!(f128 integer_decode_f128_truncated);
22342262

2263+
#[cfg(all(not(feature = "std"), feature = "libm"))]
2264+
impl Float for f16 {
2265+
float_impl_libm!(f16 integer_decode_f16);
2266+
2267+
#[inline]
2268+
#[allow(deprecated)]
2269+
fn abs_sub(self, other: Self) -> Self {
2270+
libm::fdimf16(self, other)
2271+
}
2272+
2273+
forward! {
2274+
libm::floorf16 as floor(self) -> Self;
2275+
libm::ceilf16 as ceil(self) -> Self;
2276+
libm::roundf16 as round(self) -> Self;
2277+
libm::truncf16 as trunc(self) -> Self;
2278+
}
2279+
2280+
cast_forward_cast! {
2281+
[f32] libm::fmaf as mul_add(self, a: Self, b: Self) -> Self;
2282+
[f32] libm::powf as powf(self, n: Self) -> Self;
2283+
[f32] libm::sqrtf as sqrt(self) -> Self;
2284+
[f32] libm::expf as exp(self) -> Self;
2285+
[f32] libm::exp2f as exp2(self) -> Self;
2286+
[f32] libm::logf as ln(self) -> Self;
2287+
[f32] libm::log2f as log2(self) -> Self;
2288+
[f32] libm::log10f as log10(self) -> Self;
2289+
[f32] libm::cbrtf as cbrt(self) -> Self;
2290+
[f32] libm::hypotf as hypot(self, other: Self) -> Self;
2291+
[f32] libm::sinf as sin(self) -> Self;
2292+
[f32] libm::cosf as cos(self) -> Self;
2293+
[f32] libm::tanf as tan(self) -> Self;
2294+
[f32] libm::asinf as asin(self) -> Self;
2295+
[f32] libm::acosf as acos(self) -> Self;
2296+
[f32] libm::atanf as atan(self) -> Self;
2297+
[f32] libm::atan2f as atan2(self, other: Self) -> Self;
2298+
[f32] libm::expm1f as exp_m1(self) -> Self;
2299+
[f32] libm::log1pf as ln_1p(self) -> Self;
2300+
[f32] libm::sinhf as sinh(self) -> Self;
2301+
[f32] libm::coshf as cosh(self) -> Self;
2302+
[f32] libm::tanhf as tanh(self) -> Self;
2303+
[f32] libm::asinhf as asinh(self) -> Self;
2304+
[f32] libm::acoshf as acosh(self) -> Self;
2305+
[f32] libm::atanhf as atanh(self) -> Self;
2306+
}
2307+
2308+
forward! {
2309+
f16::abs as abs(self) -> Self;
2310+
f16::copysign as copysign(self, other: Self) -> Self;
2311+
}
2312+
2313+
#[inline]
2314+
fn sin_cos(self) -> (Self, Self) {
2315+
let (x, y) = libm::sincosf(self as f32);
2316+
(x as Self, y as Self)
2317+
}
2318+
}
2319+
22352320
#[cfg(all(not(feature = "std"), feature = "libm"))]
22362321
impl Float for f32 {
22372322
float_impl_libm!(f32 integer_decode_f32);
@@ -2324,6 +2409,63 @@ impl Float for f64 {
23242409
}
23252410
}
23262411

2412+
#[cfg(all(not(feature = "std"), feature = "libm"))]
2413+
impl Float for f128 {
2414+
float_impl_libm!(f128 integer_decode_f128_truncated);
2415+
2416+
#[inline]
2417+
#[allow(deprecated)]
2418+
fn abs_sub(self, other: Self) -> Self {
2419+
libm::fdimf128(self, other)
2420+
}
2421+
2422+
forward! {
2423+
libm::floorf128 as floor(self) -> Self;
2424+
libm::ceilf128 as ceil(self) -> Self;
2425+
libm::roundf128 as round(self) -> Self;
2426+
libm::truncf128 as trunc(self) -> Self;
2427+
}
2428+
2429+
cast_forward_cast! {
2430+
[f64] libm::pow as powf(self, n: Self) -> Self;
2431+
[f64] libm::exp as exp(self) -> Self;
2432+
[f64] libm::exp2 as exp2(self) -> Self;
2433+
[f64] libm::log as ln(self) -> Self;
2434+
[f64] libm::log2 as log2(self) -> Self;
2435+
[f64] libm::log10 as log10(self) -> Self;
2436+
[f64] libm::cbrt as cbrt(self) -> Self;
2437+
[f64] libm::hypot as hypot(self, other: Self) -> Self;
2438+
[f64] libm::sin as sin(self) -> Self;
2439+
[f64] libm::cos as cos(self) -> Self;
2440+
[f64] libm::tan as tan(self) -> Self;
2441+
[f64] libm::asin as asin(self) -> Self;
2442+
[f64] libm::acos as acos(self) -> Self;
2443+
[f64] libm::atan as atan(self) -> Self;
2444+
[f64] libm::atan2 as atan2(self, other: Self) -> Self;
2445+
[f64] libm::expm1 as exp_m1(self) -> Self;
2446+
[f64] libm::log1p as ln_1p(self) -> Self;
2447+
[f64] libm::sinh as sinh(self) -> Self;
2448+
[f64] libm::cosh as cosh(self) -> Self;
2449+
[f64] libm::tanh as tanh(self) -> Self;
2450+
[f64] libm::asinh as asinh(self) -> Self;
2451+
[f64] libm::acosh as acosh(self) -> Self;
2452+
[f64] libm::atanh as atanh(self) -> Self;
2453+
}
2454+
2455+
forward! {
2456+
libm::fmaf128 as mul_add(self, a: Self, b: Self) -> Self;
2457+
libm::sqrtf128 as sqrt(self) -> Self;
2458+
f128::abs as abs(self) -> Self;
2459+
f128::copysign as copysign(self, other: Self) -> Self;
2460+
}
2461+
2462+
#[inline]
2463+
fn sin_cos(self) -> (Self, Self) {
2464+
let (x, y) = libm::sincos(self as f64);
2465+
(x as Self, y as Self)
2466+
}
2467+
}
2468+
23272469
macro_rules! float_const_impl {
23282470
($(#[$doc:meta] $constant:ident,)+) => (
23292471
#[allow(non_snake_case)]

src/macros.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@
33

44
/// Forward a method to an inherent method or a base trait method.
55
macro_rules! forward {
6+
($( unsafe $imp:path as $method:ident ( self $( , $arg:ident : $ty:ty )* ) -> $ret:ty ; )*)
7+
=> {$(
8+
#[inline]
9+
fn $method(self $( , $arg : $ty )* ) -> $ret {
10+
unsafe { $imp(self $( , $arg )* ) }
11+
}
12+
)*};
613
($( Self :: $method:ident ( self $( , $arg:ident : $ty:ty )* ) -> $ret:ty ; )*)
714
=> {$(
815
#[inline]
@@ -33,6 +40,16 @@ macro_rules! forward {
3340
)*};
3441
}
3542

43+
macro_rules! cast_forward_cast {
44+
($( [$cast:ty] $imp:path as $method:ident ( self $( , $arg:ident : $ty:ty )* ) -> $ret:ty ; )*)
45+
=> {$(
46+
#[inline]
47+
fn $method(self $( , $arg : $ty )* ) -> $ret {
48+
$imp(self as $cast $( , $arg as $cast)* ) as $ret
49+
}
50+
)*};
51+
}
52+
3653
macro_rules! constant {
3754
($( $method:ident () -> $ret:expr ; )*)
3855
=> {$(

0 commit comments

Comments
 (0)