Skip to content

Commit c0cdb3a

Browse files
committed
add f128
1 parent 20b84d0 commit c0cdb3a

12 files changed

Lines changed: 535 additions & 19 deletions

File tree

build.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,6 @@ fn main() {
1010
// FIXME: use autocfg to emit it
1111
println!("cargo:rustc-cfg=has_f16");
1212
println!("cargo:rustc-check-cfg=cfg(has_f16)");
13+
println!("cargo:rustc-cfg=has_f128");
14+
println!("cargo:rustc-check-cfg=cfg(has_f128)");
1315
}

src/bounds.rs

Lines changed: 4 additions & 0 deletions
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;
@@ -130,6 +132,8 @@ impl<T: Bounded> Bounded for Wrapping<T> {
130132
bounded_impl!(f16, f16::MIN, f16::MAX);
131133
bounded_impl!(f32, f32::MIN, f32::MAX);
132134
bounded_impl!(f64, f64::MIN, f64::MAX);
135+
#[cfg(has_f128)]
136+
bounded_impl!(f128, f128::MIN, f128::MAX);
133137

134138
macro_rules! for_each_tuple_ {
135139
( $m:ident !! ) => (

src/cast.rs

Lines changed: 63 additions & 15 deletions
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;
@@ -138,6 +140,15 @@ pub trait ToPrimitive {
138140
None => self.to_u64().as_ref().and_then(ToPrimitive::to_f64),
139141
}
140142
}
143+
144+
/// Converts the value of `self` to an `f128`. Overflows may map to positive
145+
/// or negative inifinity, otherwise `None` is returned if the value cannot
146+
/// be represented by an `f128`.
147+
#[cfg(has_f128)]
148+
#[inline]
149+
fn to_f128(&self) -> Option<f128> {
150+
self.to_f64().as_ref().and_then(ToPrimitive::to_f128)
151+
}
141152
}
142153

143154
macro_rules! impl_to_primitive_int_to_int {
@@ -205,6 +216,11 @@ macro_rules! impl_to_primitive_int {
205216
fn to_f64(&self) -> Option<f64> {
206217
Some(*self as f64)
207218
}
219+
#[cfg(has_f128)]
220+
#[inline]
221+
fn to_f128(&self) -> Option<f128> {
222+
Some(*self as f128)
223+
}
208224
}
209225
};
210226
}
@@ -280,6 +296,11 @@ macro_rules! impl_to_primitive_uint {
280296
fn to_f64(&self) -> Option<f64> {
281297
Some(*self as f64)
282298
}
299+
#[cfg(has_f128)]
300+
#[inline]
301+
fn to_f128(&self) -> Option<f128> {
302+
Some(*self as f128)
303+
}
283304
}
284305
};
285306
}
@@ -442,6 +463,8 @@ macro_rules! impl_to_primitive_float {
442463
fn to_f16 -> f16;
443464
fn to_f32 -> f32;
444465
fn to_f64 -> f64;
466+
#[cfg(has_f128)]
467+
fn to_f128 -> f128;
445468
}
446469
}
447470
};
@@ -451,6 +474,8 @@ macro_rules! impl_to_primitive_float {
451474
impl_to_primitive_float!(f16);
452475
impl_to_primitive_float!(f32);
453476
impl_to_primitive_float!(f64);
477+
#[cfg(has_f128)]
478+
impl_to_primitive_float!(f128);
454479

455480
/// A generic trait for converting a number to a value.
456481
///
@@ -575,6 +600,14 @@ pub trait FromPrimitive: Sized {
575600
None => n.to_u64().and_then(FromPrimitive::from_u64),
576601
}
577602
}
603+
604+
/// Converts a `f128` to return an optional value of this type. If the
605+
/// value cannot be represented by this type, then `None` is returned.
606+
#[cfg(has_f128)]
607+
#[inline]
608+
fn from_f128(n: f128) -> Option<Self> {
609+
FromPrimitive::from_f64(n as f64)
610+
}
578611
}
579612

580613
macro_rules! impl_from_primitive {
@@ -643,6 +676,11 @@ macro_rules! impl_from_primitive {
643676
fn from_f64(n: f64) -> Option<$T> {
644677
n.$to_ty()
645678
}
679+
#[cfg(has_f128)]
680+
#[inline]
681+
fn from_f128(n: f128) -> Option<$T> {
682+
n.$to_ty()
683+
}
646684
}
647685
};
648686
}
@@ -663,6 +701,8 @@ impl_from_primitive!(u128, to_u128);
663701
impl_from_primitive!(f16, to_f16);
664702
impl_from_primitive!(f32, to_f32);
665703
impl_from_primitive!(f64, to_f64);
704+
#[cfg(has_f128)]
705+
impl_from_primitive!(f128, to_f128);
666706

667707
macro_rules! impl_from_primitive_nonzero {
668708
($T:ty, $to_ty:ident) => {
@@ -772,6 +812,8 @@ impl<T: ToPrimitive> ToPrimitive for Wrapping<T> {
772812
fn to_f16 -> f16;
773813
fn to_f32 -> f32;
774814
fn to_f64 -> f64;
815+
#[cfg(has_f128)]
816+
fn to_f128 -> f128;
775817
}
776818
}
777819

@@ -805,6 +847,8 @@ impl<T: FromPrimitive> FromPrimitive for Wrapping<T> {
805847
fn from_f16(f16);
806848
fn from_f32(f32);
807849
fn from_f64(f64);
850+
#[cfg(has_f128)]
851+
fn from_f128(f128);
808852
}
809853
}
810854

@@ -867,6 +911,8 @@ impl_num_cast!(isize, to_isize);
867911
impl_num_cast!(f16, to_f16);
868912
impl_num_cast!(f32, to_f32);
869913
impl_num_cast!(f64, to_f64);
914+
#[cfg(has_f128)]
915+
impl_num_cast!(f128, to_f128);
870916

871917
macro_rules! impl_num_cast_nonzero {
872918
($T:ty, $conv:ident) => {
@@ -950,21 +996,23 @@ macro_rules! impl_as_primitive {
950996
};
951997
}
952998

953-
impl_as_primitive!(u8 => { char, #[cfg(has_f16)] f16, f32, f64 });
954-
impl_as_primitive!(i8 => { #[cfg(has_f16)] f16, f32, f64 });
955-
impl_as_primitive!(u16 => { #[cfg(has_f16)] f16, f32, f64 });
956-
impl_as_primitive!(i16 => { #[cfg(has_f16)] f16, f32, f64 });
957-
impl_as_primitive!(u32 => { #[cfg(has_f16)] f16, f32, f64 });
958-
impl_as_primitive!(i32 => { #[cfg(has_f16)] f16, f32, f64 });
959-
impl_as_primitive!(u64 => { #[cfg(has_f16)] f16, f32, f64 });
960-
impl_as_primitive!(i64 => { #[cfg(has_f16)] f16, f32, f64 });
961-
impl_as_primitive!(u128 => { #[cfg(has_f16)] f16, f32, f64 });
962-
impl_as_primitive!(i128 => { #[cfg(has_f16)] f16, f32, f64 });
963-
impl_as_primitive!(usize => { #[cfg(has_f16)] f16, f32, f64 });
964-
impl_as_primitive!(isize => { #[cfg(has_f16)] f16, f32, f64 });
999+
impl_as_primitive!(u8 => { char, #[cfg(has_f16)] f16, f32, f64, #[cfg(has_f128)] f128 });
1000+
impl_as_primitive!(i8 => { #[cfg(has_f16)] f16, f32, f64, #[cfg(has_f128)] f128 });
1001+
impl_as_primitive!(u16 => { #[cfg(has_f16)] f16, f32, f64, #[cfg(has_f128)] f128 });
1002+
impl_as_primitive!(i16 => { #[cfg(has_f16)] f16, f32, f64, #[cfg(has_f128)] f128 });
1003+
impl_as_primitive!(u32 => { #[cfg(has_f16)] f16, f32, f64, #[cfg(has_f128)] f128 });
1004+
impl_as_primitive!(i32 => { #[cfg(has_f16)] f16, f32, f64, #[cfg(has_f128)] f128 });
1005+
impl_as_primitive!(u64 => { #[cfg(has_f16)] f16, f32, f64, #[cfg(has_f128)] f128 });
1006+
impl_as_primitive!(i64 => { #[cfg(has_f16)] f16, f32, f64, #[cfg(has_f128)] f128 });
1007+
impl_as_primitive!(u128 => { #[cfg(has_f16)] f16, f32, f64, #[cfg(has_f128)] f128 });
1008+
impl_as_primitive!(i128 => { #[cfg(has_f16)] f16, f32, f64, #[cfg(has_f128)] f128 });
1009+
impl_as_primitive!(usize => { #[cfg(has_f16)] f16, f32, f64, #[cfg(has_f128)] f128 });
1010+
impl_as_primitive!(isize => { #[cfg(has_f16)] f16, f32, f64, #[cfg(has_f128)] f128 });
9651011
#[cfg(has_f16)]
966-
impl_as_primitive!(f16 => { f16, f32, f64 });
967-
impl_as_primitive!(f32 => { #[cfg(has_f16)] f16, f32, f64 });
968-
impl_as_primitive!(f64 => { #[cfg(has_f16)] f16, f32, f64 });
1012+
impl_as_primitive!(f16 => { f16, f32, f64, #[cfg(has_f128)] f128 });
1013+
impl_as_primitive!(f32 => { #[cfg(has_f16)] f16, f32, f64, #[cfg(has_f128)] f128 });
1014+
impl_as_primitive!(f64 => { #[cfg(has_f16)] f16, f32, f64, #[cfg(has_f128)] f128 });
1015+
#[cfg(has_f128)]
1016+
impl_as_primitive!(f128 => { #[cfg(has_f16)] f16, f32, f64, f128 });
9691017
impl_as_primitive!(char => { char });
9701018
impl_as_primitive!(bool => {});

0 commit comments

Comments
 (0)