Skip to content

Commit c6e7689

Browse files
committed
add f128
1 parent 157cd4f commit c6e7689

File tree

12 files changed

+302
-26
lines changed

12 files changed

+302
-26
lines changed

build.rs

+2
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,6 @@ fn main() {
88
// FIXME: use autocfg to emit it
99
println!("cargo:rustc-cfg=has_f16");
1010
println!("cargo:rustc-check-cfg=cfg(has_f16)");
11+
println!("cargo:rustc-cfg=has_f128");
12+
println!("cargo:rustc-check-cfg=cfg(has_f128)");
1113
}

src/bounds.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#[cfg(has_f128)]
2+
use core::f128;
13
#[cfg(has_f16)]
24
use core::f16;
35
use core::num::Wrapping;
@@ -83,6 +85,8 @@ impl<T: Bounded> Bounded for Wrapping<T> {
8385
bounded_impl!(f16, f16::MIN, f16::MAX);
8486
bounded_impl!(f32, f32::MIN, f32::MAX);
8587
bounded_impl!(f64, f64::MIN, f64::MAX);
88+
#[cfg(has_f128)]
89+
bounded_impl!(f128, f128::MIN, f128::MAX);
8690

8791
macro_rules! for_each_tuple_ {
8892
( $m:ident !! ) => (

src/cast.rs

+63-15
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#[cfg(has_f128)]
2+
use core::f128;
13
#[cfg(has_f16)]
24
use core::f16;
35
use core::mem::size_of;
@@ -134,6 +136,15 @@ pub trait ToPrimitive {
134136
None => self.to_u64().as_ref().and_then(ToPrimitive::to_f64),
135137
}
136138
}
139+
140+
/// Converts the value of `self` to an `f128`. Overflows may map to positive
141+
/// or negative inifinity, otherwise `None` is returned if the value cannot
142+
/// be represented by an `f128`.
143+
#[cfg(has_f128)]
144+
#[inline]
145+
fn to_f128(&self) -> Option<f128> {
146+
self.to_f64().as_ref().and_then(ToPrimitive::to_f128)
147+
}
137148
}
138149

139150
macro_rules! impl_to_primitive_int_to_int {
@@ -201,6 +212,11 @@ macro_rules! impl_to_primitive_int {
201212
fn to_f64(&self) -> Option<f64> {
202213
Some(*self as f64)
203214
}
215+
#[cfg(has_f128)]
216+
#[inline]
217+
fn to_f128(&self) -> Option<f128> {
218+
Some(*self as f128)
219+
}
204220
}
205221
};
206222
}
@@ -276,6 +292,11 @@ macro_rules! impl_to_primitive_uint {
276292
fn to_f64(&self) -> Option<f64> {
277293
Some(*self as f64)
278294
}
295+
#[cfg(has_f128)]
296+
#[inline]
297+
fn to_f128(&self) -> Option<f128> {
298+
Some(*self as f128)
299+
}
279300
}
280301
};
281302
}
@@ -390,6 +411,8 @@ macro_rules! impl_to_primitive_float {
390411
fn to_f16 -> f16;
391412
fn to_f32 -> f32;
392413
fn to_f64 -> f64;
414+
#[cfg(has_f128)]
415+
fn to_f128 -> f128;
393416
}
394417
}
395418
};
@@ -399,6 +422,8 @@ macro_rules! impl_to_primitive_float {
399422
impl_to_primitive_float!(f16);
400423
impl_to_primitive_float!(f32);
401424
impl_to_primitive_float!(f64);
425+
#[cfg(has_f128)]
426+
impl_to_primitive_float!(f128);
402427

403428
/// A generic trait for converting a number to a value.
404429
///
@@ -523,6 +548,14 @@ pub trait FromPrimitive: Sized {
523548
None => n.to_u64().and_then(FromPrimitive::from_u64),
524549
}
525550
}
551+
552+
/// Converts a `f128` to return an optional value of this type. If the
553+
/// value cannot be represented by this type, then `None` is returned.
554+
#[cfg(has_f128)]
555+
#[inline]
556+
fn from_f128(n: f128) -> Option<Self> {
557+
FromPrimitive::from_f64(n as f64)
558+
}
526559
}
527560

528561
macro_rules! impl_from_primitive {
@@ -592,6 +625,11 @@ macro_rules! impl_from_primitive {
592625
fn from_f64(n: f64) -> Option<$T> {
593626
n.$to_ty()
594627
}
628+
#[cfg(has_f128)]
629+
#[inline]
630+
fn from_f128(n: f128) -> Option<$T> {
631+
n.$to_ty()
632+
}
595633
}
596634
};
597635
}
@@ -612,6 +650,8 @@ impl_from_primitive!(u128, to_u128);
612650
impl_from_primitive!(f16, to_f16);
613651
impl_from_primitive!(f32, to_f32);
614652
impl_from_primitive!(f64, to_f64);
653+
#[cfg(has_f128)]
654+
impl_from_primitive!(f128, to_f128);
615655

616656
macro_rules! impl_to_primitive_wrapping {
617657
($( $(#[$cfg:meta])* fn $method:ident -> $i:ident ; )*) => {$(
@@ -643,6 +683,8 @@ impl<T: ToPrimitive> ToPrimitive for Wrapping<T> {
643683
fn to_f16 -> f16;
644684
fn to_f32 -> f32;
645685
fn to_f64 -> f64;
686+
#[cfg(has_f128)]
687+
fn to_f128 -> f128;
646688
}
647689
}
648690

@@ -676,6 +718,8 @@ impl<T: FromPrimitive> FromPrimitive for Wrapping<T> {
676718
fn from_f16(f16);
677719
fn from_f32(f32);
678720
fn from_f64(f64);
721+
#[cfg(has_f128)]
722+
fn from_f128(f128);
679723
}
680724
}
681725

@@ -741,6 +785,8 @@ impl_num_cast!(isize, to_isize);
741785
impl_num_cast!(f16, to_f16);
742786
impl_num_cast!(f32, to_f32);
743787
impl_num_cast!(f64, to_f64);
788+
#[cfg(has_f128)]
789+
impl_num_cast!(f128, to_f128);
744790

745791
impl<T: NumCast> NumCast for Wrapping<T> {
746792
fn from<U: ToPrimitive>(n: U) -> Option<Self> {
@@ -799,21 +845,23 @@ macro_rules! impl_as_primitive {
799845
};
800846
}
801847

802-
impl_as_primitive!(u8 => { char, #[cfg(has_f16)] f16, f32, f64 });
803-
impl_as_primitive!(i8 => { #[cfg(has_f16)] f16, f32, f64 });
804-
impl_as_primitive!(u16 => { #[cfg(has_f16)] f16, f32, f64 });
805-
impl_as_primitive!(i16 => { #[cfg(has_f16)] f16, f32, f64 });
806-
impl_as_primitive!(u32 => { #[cfg(has_f16)] f16, f32, f64 });
807-
impl_as_primitive!(i32 => { #[cfg(has_f16)] f16, f32, f64 });
808-
impl_as_primitive!(u64 => { #[cfg(has_f16)] f16, f32, f64 });
809-
impl_as_primitive!(i64 => { #[cfg(has_f16)] f16, f32, f64 });
810-
impl_as_primitive!(u128 => { #[cfg(has_f16)] f16, f32, f64 });
811-
impl_as_primitive!(i128 => { #[cfg(has_f16)] f16, f32, f64 });
812-
impl_as_primitive!(usize => { #[cfg(has_f16)] f16, f32, f64 });
813-
impl_as_primitive!(isize => { #[cfg(has_f16)] f16, f32, f64 });
848+
impl_as_primitive!(u8 => { char, #[cfg(has_f16)] f16, f32, f64, #[cfg(has_f128)] f128 });
849+
impl_as_primitive!(i8 => { #[cfg(has_f16)] f16, f32, f64, #[cfg(has_f128)] f128 });
850+
impl_as_primitive!(u16 => { #[cfg(has_f16)] f16, f32, f64, #[cfg(has_f128)] f128 });
851+
impl_as_primitive!(i16 => { #[cfg(has_f16)] f16, f32, f64, #[cfg(has_f128)] f128 });
852+
impl_as_primitive!(u32 => { #[cfg(has_f16)] f16, f32, f64, #[cfg(has_f128)] f128 });
853+
impl_as_primitive!(i32 => { #[cfg(has_f16)] f16, f32, f64, #[cfg(has_f128)] f128 });
854+
impl_as_primitive!(u64 => { #[cfg(has_f16)] f16, f32, f64, #[cfg(has_f128)] f128 });
855+
impl_as_primitive!(i64 => { #[cfg(has_f16)] f16, f32, f64, #[cfg(has_f128)] f128 });
856+
impl_as_primitive!(u128 => { #[cfg(has_f16)] f16, f32, f64, #[cfg(has_f128)] f128 });
857+
impl_as_primitive!(i128 => { #[cfg(has_f16)] f16, f32, f64, #[cfg(has_f128)] f128 });
858+
impl_as_primitive!(usize => { #[cfg(has_f16)] f16, f32, f64, #[cfg(has_f128)] f128 });
859+
impl_as_primitive!(isize => { #[cfg(has_f16)] f16, f32, f64, #[cfg(has_f128)] f128 });
814860
#[cfg(has_f16)]
815-
impl_as_primitive!(f16 => { f16, f32, f64 });
816-
impl_as_primitive!(f32 => { #[cfg(has_f16)] f16, f32, f64 });
817-
impl_as_primitive!(f64 => { #[cfg(has_f16)] f16, f32, f64 });
861+
impl_as_primitive!(f16 => { f16, f32, f64, #[cfg(has_f128)] f128 });
862+
impl_as_primitive!(f32 => { #[cfg(has_f16)] f16, f32, f64, #[cfg(has_f128)] f128 });
863+
impl_as_primitive!(f64 => { #[cfg(has_f16)] f16, f32, f64, #[cfg(has_f128)] f128 });
864+
#[cfg(has_f128)]
865+
impl_as_primitive!(f128 => { #[cfg(has_f16)] f16, f32, f64, f128 });
818866
impl_as_primitive!(char => { char });
819867
impl_as_primitive!(bool => {});

0 commit comments

Comments
 (0)