Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit510dfa4

Browse files
committed
fix no_std + libm
1 parentb7780e6 commit510dfa4

File tree

2 files changed

+183
-4
lines changed

2 files changed

+183
-4
lines changed

‎src/float.rs‎

Lines changed: 166 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,22 @@ use core::f64;
1111

1212
usecrate::{Num,NumCast,ToPrimitive};
1313

14+
#[cfg(all(not(feature ="std"), feature ="libm"))]
15+
mod compiler_builtins{
16+
extern"C"{
17+
pubfnfloorf16(x:f16) ->f16;
18+
pubfnfloorf128(x:f128) ->f128;
19+
pubfnceilf16(x:f16) ->f16;
20+
pubfnceilf128(x:f128) ->f128;
21+
pubfnroundf16(x:f16) ->f16;
22+
pubfnroundf128(x:f128) ->f128;
23+
pubfntruncf16(x:f16) ->f16;
24+
pubfntruncf128(x:f128) ->f128;
25+
pubfnfdimf16(x:f16,y:f16) ->f16;
26+
pubfnfdimf128(x:f128,y:f128) ->f128;
27+
}
28+
}
29+
1430
/// Generic trait for floating point numbers that works with `no_std`.
1531
///
1632
/// This trait implements a subset of the `Float` trait.
@@ -852,8 +868,24 @@ impl FloatCore for f16 {
852868
Self::powi(self, n:i32) ->Self;
853869
}
854870

855-
// TODO:
856-
// use floor, ceil, round, trunc, abs and fract provided by libm
871+
#[cfg(all(not(feature ="std"), feature ="libm"))]
872+
forward!{
873+
unsafe compiler_builtins::floorf16as floor(self) ->Self;
874+
unsafe compiler_builtins::ceilf16as ceil(self) ->Self;
875+
unsafe compiler_builtins::roundf16as round(self) ->Self;
876+
unsafe compiler_builtins::truncf16as trunc(self) ->Self;
877+
}
878+
879+
#[cfg(all(not(feature ="std"), feature ="libm"))]
880+
forward!{
881+
f16::absas abs(self) ->Self;
882+
}
883+
884+
#[cfg(all(not(feature ="std"), feature ="libm"))]
885+
#[inline]
886+
fnfract(self) ->Self{
887+
self -unsafe{ compiler_builtins::truncf16(self)asSelf}
888+
}
857889
}
858890

859891
implFloatCoreforf32{
@@ -1026,8 +1058,24 @@ impl FloatCore for f128 {
10261058
Self::powi(self, n:i32) ->Self;
10271059
}
10281060

1029-
// TODO:
1030-
// use floor, ceil, round, trunc, abs and fract provided by libm
1061+
#[cfg(all(not(feature ="std"), feature ="libm"))]
1062+
forward!{
1063+
unsafe compiler_builtins::floorf128as floor(self) ->Self;
1064+
unsafe compiler_builtins::ceilf128as ceil(self) ->Self;
1065+
unsafe compiler_builtins::roundf128as round(self) ->Self;
1066+
unsafe compiler_builtins::truncf128as trunc(self) ->Self;
1067+
}
1068+
1069+
#[cfg(all(not(feature ="std"), feature ="libm"))]
1070+
forward!{
1071+
f128::absas abs(self) ->Self;
1072+
}
1073+
1074+
#[cfg(all(not(feature ="std"), feature ="libm"))]
1075+
#[inline]
1076+
fnfract(self) ->Self{
1077+
self -unsafe{ compiler_builtins::truncf128(self)asSelf}
1078+
}
10311079
}
10321080

10331081
// FIXME: these doctests aren't actually helpful, because they're using and
@@ -2232,6 +2280,63 @@ float_impl_std!(f64 integer_decode_f64);
22322280
#[cfg(feature ="std")]
22332281
float_impl_std!(f128 integer_decode_f128_truncated);
22342282

2283+
#[cfg(all(not(feature ="std"), feature ="libm"))]
2284+
implFloatforf16{
2285+
float_impl_libm!(f16 integer_decode_f16);
2286+
2287+
#[inline]
2288+
#[allow(deprecated)]
2289+
fnabs_sub(self,other:Self) ->Self{
2290+
unsafe{ compiler_builtins::fdimf16(self, other)asSelf}
2291+
}
2292+
2293+
forward!{
2294+
unsafe compiler_builtins::floorf16as floor(self) ->Self;
2295+
unsafe compiler_builtins::ceilf16as ceil(self) ->Self;
2296+
unsafe compiler_builtins::roundf16as round(self) ->Self;
2297+
unsafe compiler_builtins::truncf16as trunc(self) ->Self;
2298+
}
2299+
2300+
cast_forward_cast!{
2301+
[f32] libm::fmafas mul_add(self, a:Self, b:Self) ->Self;
2302+
[f32] libm::powfas powf(self, n:Self) ->Self;
2303+
[f32] libm::sqrtfas sqrt(self) ->Self;
2304+
[f32] libm::expfas exp(self) ->Self;
2305+
[f32] libm::exp2fas exp2(self) ->Self;
2306+
[f32] libm::logfas ln(self) ->Self;
2307+
[f32] libm::log2fas log2(self) ->Self;
2308+
[f32] libm::log10fas log10(self) ->Self;
2309+
[f32] libm::cbrtfas cbrt(self) ->Self;
2310+
[f32] libm::hypotfas hypot(self, other:Self) ->Self;
2311+
[f32] libm::sinfas sin(self) ->Self;
2312+
[f32] libm::cosfas cos(self) ->Self;
2313+
[f32] libm::tanfas tan(self) ->Self;
2314+
[f32] libm::asinfas asin(self) ->Self;
2315+
[f32] libm::acosfas acos(self) ->Self;
2316+
[f32] libm::atanfas atan(self) ->Self;
2317+
[f32] libm::atan2fas atan2(self, other:Self) ->Self;
2318+
[f32] libm::expm1fas exp_m1(self) ->Self;
2319+
[f32] libm::log1pfas ln_1p(self) ->Self;
2320+
[f32] libm::sinhfas sinh(self) ->Self;
2321+
[f32] libm::coshfas cosh(self) ->Self;
2322+
[f32] libm::tanhfas tanh(self) ->Self;
2323+
[f32] libm::asinhfas asinh(self) ->Self;
2324+
[f32] libm::acoshfas acosh(self) ->Self;
2325+
[f32] libm::atanhfas atanh(self) ->Self;
2326+
}
2327+
2328+
forward!{
2329+
f16::absas abs(self) ->Self;
2330+
f16::copysignas copysign(self, other:Self) ->Self;
2331+
}
2332+
2333+
#[inline]
2334+
fnsin_cos(self) ->(Self,Self){
2335+
let(x, y) = libm::sincosf(selfasf32);
2336+
(xasSelf, yasSelf)
2337+
}
2338+
}
2339+
22352340
#[cfg(all(not(feature ="std"), feature ="libm"))]
22362341
implFloatforf32{
22372342
float_impl_libm!(f32 integer_decode_f32);
@@ -2324,6 +2429,63 @@ impl Float for f64 {
23242429
}
23252430
}
23262431

2432+
#[cfg(all(not(feature ="std"), feature ="libm"))]
2433+
implFloatforf128{
2434+
float_impl_libm!(f128 integer_decode_f128_truncated);
2435+
2436+
#[inline]
2437+
#[allow(deprecated)]
2438+
fnabs_sub(self,other:Self) ->Self{
2439+
unsafe{ compiler_builtins::fdimf128(self, other)asSelf}
2440+
}
2441+
2442+
forward!{
2443+
unsafe compiler_builtins::floorf128as floor(self) ->Self;
2444+
unsafe compiler_builtins::ceilf128as ceil(self) ->Self;
2445+
unsafe compiler_builtins::roundf128as round(self) ->Self;
2446+
unsafe compiler_builtins::truncf128as trunc(self) ->Self;
2447+
}
2448+
2449+
cast_forward_cast!{
2450+
[f64] libm::fmaas mul_add(self, a:Self, b:Self) ->Self;
2451+
[f64] libm::powas powf(self, n:Self) ->Self;
2452+
[f64] libm::sqrtas sqrt(self) ->Self;
2453+
[f64] libm::expas exp(self) ->Self;
2454+
[f64] libm::exp2as exp2(self) ->Self;
2455+
[f64] libm::logas ln(self) ->Self;
2456+
[f64] libm::log2as log2(self) ->Self;
2457+
[f64] libm::log10as log10(self) ->Self;
2458+
[f64] libm::cbrtas cbrt(self) ->Self;
2459+
[f64] libm::hypotas hypot(self, other:Self) ->Self;
2460+
[f64] libm::sinas sin(self) ->Self;
2461+
[f64] libm::cosas cos(self) ->Self;
2462+
[f64] libm::tanas tan(self) ->Self;
2463+
[f64] libm::asinas asin(self) ->Self;
2464+
[f64] libm::acosas acos(self) ->Self;
2465+
[f64] libm::atanas atan(self) ->Self;
2466+
[f64] libm::atan2as atan2(self, other:Self) ->Self;
2467+
[f64] libm::expm1as exp_m1(self) ->Self;
2468+
[f64] libm::log1pas ln_1p(self) ->Self;
2469+
[f64] libm::sinhas sinh(self) ->Self;
2470+
[f64] libm::coshas cosh(self) ->Self;
2471+
[f64] libm::tanhas tanh(self) ->Self;
2472+
[f64] libm::asinhas asinh(self) ->Self;
2473+
[f64] libm::acoshas acosh(self) ->Self;
2474+
[f64] libm::atanhas atanh(self) ->Self;
2475+
}
2476+
2477+
forward!{
2478+
f128::absas abs(self) ->Self;
2479+
f128::copysignas copysign(self, other:Self) ->Self;
2480+
}
2481+
2482+
#[inline]
2483+
fnsin_cos(self) ->(Self,Self){
2484+
let(x, y) = libm::sincos(selfasf64);
2485+
(xasSelf, yasSelf)
2486+
}
2487+
}
2488+
23272489
macro_rules! float_const_impl{
23282490
($(#[$doc:meta] $constant:ident,)+) =>(
23292491
#[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:pathas $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:pathas $method:ident(self $(, $arg:ident: $ty:ty)*) -> $ret:ty;)*)
45+
=>{$(
46+
#[inline]
47+
fn $method(self $(, $arg: $ty)*) -> $ret{
48+
$imp(selfas $cast $(, $argas $cast)*)as $ret
49+
}
50+
)*};
51+
}
52+
3653
macro_rules! constant{
3754
($( $method:ident() -> $ret:expr;)*)
3855
=>{$(

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp