Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add f16, f128 #333

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -4,4 +4,10 @@ fn main() {
ac.emit_expression_cfg("1f64.total_cmp(&2f64)", "has_total_cmp"); // 1.62

autocfg::rerun_path("build.rs");

// FIXME: use autocfg to emit it
println!("cargo:rustc-cfg=has_f16");
println!("cargo:rustc-check-cfg=cfg(has_f16)");
println!("cargo:rustc-cfg=has_f128");
println!("cargo:rustc-check-cfg=cfg(has_f128)");
}
10 changes: 9 additions & 1 deletion src/bounds.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
#[cfg(has_f128)]
use core::f128;
#[cfg(has_f16)]
use core::f16;
use core::num::Wrapping;
use core::{f32, f64};
use core::{i128, i16, i32, i64, i8, isize};
@@ -77,7 +81,12 @@ impl<T: Bounded> Bounded for Wrapping<T> {
}
}

#[cfg(has_f16)]
bounded_impl!(f16, f16::MIN, f16::MAX);
bounded_impl!(f32, f32::MIN, f32::MAX);
bounded_impl!(f64, f64::MIN, f64::MAX);
#[cfg(has_f128)]
bounded_impl!(f128, f128::MIN, f128::MAX);

macro_rules! for_each_tuple_ {
( $m:ident !! ) => (
@@ -110,7 +119,6 @@ macro_rules! bounded_tuple {
}

for_each_tuple!(bounded_tuple);
bounded_impl!(f64, f64::MIN, f64::MAX);

#[test]
fn wrapping_bounded() {
135 changes: 116 additions & 19 deletions src/cast.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
#[cfg(has_f128)]
use core::f128;
#[cfg(has_f16)]
use core::f16;
use core::mem::size_of;
use core::num::Wrapping;
use core::{f32, f64};
@@ -101,6 +105,15 @@ pub trait ToPrimitive {
self.to_u64().map(From::from)
}

/// Converts the value of `self` to an `f16`. Overflows may map to positive
/// or negative inifinity, otherwise `None` is returned if the value cannot
/// be represented by an `f16`.
#[cfg(has_f16)]
#[inline]
fn to_f16(&self) -> Option<f16> {
self.to_f64().as_ref().and_then(ToPrimitive::to_f16)
}

/// Converts the value of `self` to an `f32`. Overflows may map to positive
/// or negative inifinity, otherwise `None` is returned if the value cannot
/// be represented by an `f32`.
@@ -123,6 +136,15 @@ pub trait ToPrimitive {
None => self.to_u64().as_ref().and_then(ToPrimitive::to_f64),
}
}

/// Converts the value of `self` to an `f128`. Overflows may map to positive
/// or negative inifinity, otherwise `None` is returned if the value cannot
/// be represented by an `f128`.
#[cfg(has_f128)]
#[inline]
fn to_f128(&self) -> Option<f128> {
self.to_f64().as_ref().and_then(ToPrimitive::to_f128)
}
}

macro_rules! impl_to_primitive_int_to_int {
@@ -177,6 +199,11 @@ macro_rules! impl_to_primitive_int {
fn to_u128 -> u128;
}

#[cfg(has_f16)]
#[inline]
fn to_f16(&self) -> Option<f16> {
Some(*self as f16)
}
#[inline]
fn to_f32(&self) -> Option<f32> {
Some(*self as f32)
@@ -185,6 +212,11 @@ macro_rules! impl_to_primitive_int {
fn to_f64(&self) -> Option<f64> {
Some(*self as f64)
}
#[cfg(has_f128)]
#[inline]
fn to_f128(&self) -> Option<f128> {
Some(*self as f128)
}
}
};
}
@@ -247,6 +279,11 @@ macro_rules! impl_to_primitive_uint {
fn to_u128 -> u128;
}

#[cfg(has_f16)]
#[inline]
fn to_f16(&self) -> Option<f16> {
Some(*self as f16)
}
#[inline]
fn to_f32(&self) -> Option<f32> {
Some(*self as f32)
@@ -255,6 +292,11 @@ macro_rules! impl_to_primitive_uint {
fn to_f64(&self) -> Option<f64> {
Some(*self as f64)
}
#[cfg(has_f128)]
#[inline]
fn to_f128(&self) -> Option<f128> {
Some(*self as f128)
}
}
};
}
@@ -267,8 +309,9 @@ impl_to_primitive_uint!(u64);
impl_to_primitive_uint!(u128);

macro_rules! impl_to_primitive_float_to_float {
($SrcT:ident : $( fn $method:ident -> $DstT:ident ; )*) => {$(
($SrcT:ident : $( $(#[$cfg:meta])* fn $method:ident -> $DstT:ident ; )*) => {$(
#[inline]
$(#[$cfg])*
fn $method(&self) -> Option<$DstT> {
// We can safely cast all values, whether NaN, +-inf, or finite.
// Finite values that are reducing size may saturate to +-inf.
@@ -364,15 +407,23 @@ macro_rules! impl_to_primitive_float {
}

impl_to_primitive_float_to_float! { $T:
#[cfg(has_f16)]
fn to_f16 -> f16;
fn to_f32 -> f32;
fn to_f64 -> f64;
#[cfg(has_f128)]
fn to_f128 -> f128;
}
}
};
}

#[cfg(has_f16)]
impl_to_primitive_float!(f16);
impl_to_primitive_float!(f32);
impl_to_primitive_float!(f64);
#[cfg(has_f128)]
impl_to_primitive_float!(f128);

/// A generic trait for converting a number to a value.
///
@@ -469,6 +520,14 @@ pub trait FromPrimitive: Sized {
n.to_u64().and_then(FromPrimitive::from_u64)
}

/// Converts a `f16` to return an optional value of this type. If the
/// value cannot be represented by this type, then `None` is returned.
#[cfg(has_f16)]
#[inline]
fn from_f16(n: f16) -> Option<Self> {
FromPrimitive::from_f64(n as f64)
}

/// Converts a `f32` to return an optional value of this type. If the
/// value cannot be represented by this type, then `None` is returned.
#[inline]
@@ -489,6 +548,14 @@ pub trait FromPrimitive: Sized {
None => n.to_u64().and_then(FromPrimitive::from_u64),
}
}

/// Converts a `f128` to return an optional value of this type. If the
/// value cannot be represented by this type, then `None` is returned.
#[cfg(has_f128)]
#[inline]
fn from_f128(n: f128) -> Option<Self> {
FromPrimitive::from_f64(n as f64)
}
}

macro_rules! impl_from_primitive {
@@ -545,6 +612,11 @@ macro_rules! impl_from_primitive {
n.$to_ty()
}

#[cfg(has_f16)]
#[inline]
fn from_f16(n: f16) -> Option<$T> {
n.$to_ty()
}
#[inline]
fn from_f32(n: f32) -> Option<$T> {
n.$to_ty()
@@ -553,6 +625,11 @@ macro_rules! impl_from_primitive {
fn from_f64(n: f64) -> Option<$T> {
n.$to_ty()
}
#[cfg(has_f128)]
#[inline]
fn from_f128(n: f128) -> Option<$T> {
n.$to_ty()
}
}
};
}
@@ -569,8 +646,12 @@ impl_from_primitive!(u16, to_u16);
impl_from_primitive!(u32, to_u32);
impl_from_primitive!(u64, to_u64);
impl_from_primitive!(u128, to_u128);
#[cfg(has_f16)]
impl_from_primitive!(f16, to_f16);
impl_from_primitive!(f32, to_f32);
impl_from_primitive!(f64, to_f64);
#[cfg(has_f128)]
impl_from_primitive!(f128, to_f128);

macro_rules! impl_to_primitive_wrapping {
($( $(#[$cfg:meta])* fn $method:ident -> $i:ident ; )*) => {$(
@@ -598,8 +679,12 @@ impl<T: ToPrimitive> ToPrimitive for Wrapping<T> {
fn to_u64 -> u64;
fn to_u128 -> u128;

#[cfg(has_f16)]
fn to_f16 -> f16;
fn to_f32 -> f32;
fn to_f64 -> f64;
#[cfg(has_f128)]
fn to_f128 -> f128;
}
}

@@ -629,8 +714,12 @@ impl<T: FromPrimitive> FromPrimitive for Wrapping<T> {
fn from_u64(u64);
fn from_u128(u128);

#[cfg(has_f16)]
fn from_f16(f16);
fn from_f32(f32);
fn from_f64(f64);
#[cfg(has_f128)]
fn from_f128(f128);
}
}

@@ -692,8 +781,12 @@ impl_num_cast!(i32, to_i32);
impl_num_cast!(i64, to_i64);
impl_num_cast!(i128, to_i128);
impl_num_cast!(isize, to_isize);
#[cfg(has_f16)]
impl_num_cast!(f16, to_f16);
impl_num_cast!(f32, to_f32);
impl_num_cast!(f64, to_f64);
#[cfg(has_f128)]
impl_num_cast!(f128, to_f128);

impl<T: NumCast> NumCast for Wrapping<T> {
fn from<U: ToPrimitive>(n: U) -> Option<Self> {
@@ -742,29 +835,33 @@ macro_rules! impl_as_primitive {
#[inline] fn as_(self) -> $U { self as $U }
}
};
(@ $T: ty => { $( $U: ty ),* } ) => {$(
impl_as_primitive!(@ $T => impl $U);
(@ $T: ty => { $( $(#[$cfg:meta])* $U: ty ),* } ) => {$(
impl_as_primitive!(@ $T => $(#[$cfg])* impl $U);
)*};
($T: ty => { $( $U: ty ),* } ) => {
impl_as_primitive!(@ $T => { $( $U ),* });
($T: ty => { $( $(#[$cfg:meta])* $U: ty ),* } ) => {
impl_as_primitive!(@ $T => { $( $(#[$cfg])* $U ),* });
impl_as_primitive!(@ $T => { u8, u16, u32, u64, u128, usize });
impl_as_primitive!(@ $T => { i8, i16, i32, i64, i128, isize });
};
}

impl_as_primitive!(u8 => { char, f32, f64 });
impl_as_primitive!(i8 => { f32, f64 });
impl_as_primitive!(u16 => { f32, f64 });
impl_as_primitive!(i16 => { f32, f64 });
impl_as_primitive!(u32 => { f32, f64 });
impl_as_primitive!(i32 => { f32, f64 });
impl_as_primitive!(u64 => { f32, f64 });
impl_as_primitive!(i64 => { f32, f64 });
impl_as_primitive!(u128 => { f32, f64 });
impl_as_primitive!(i128 => { f32, f64 });
impl_as_primitive!(usize => { f32, f64 });
impl_as_primitive!(isize => { f32, f64 });
impl_as_primitive!(f32 => { f32, f64 });
impl_as_primitive!(f64 => { f32, f64 });
impl_as_primitive!(u8 => { char, #[cfg(has_f16)] f16, f32, f64, #[cfg(has_f128)] f128 });
impl_as_primitive!(i8 => { #[cfg(has_f16)] f16, f32, f64, #[cfg(has_f128)] f128 });
impl_as_primitive!(u16 => { #[cfg(has_f16)] f16, f32, f64, #[cfg(has_f128)] f128 });
impl_as_primitive!(i16 => { #[cfg(has_f16)] f16, f32, f64, #[cfg(has_f128)] f128 });
impl_as_primitive!(u32 => { #[cfg(has_f16)] f16, f32, f64, #[cfg(has_f128)] f128 });
impl_as_primitive!(i32 => { #[cfg(has_f16)] f16, f32, f64, #[cfg(has_f128)] f128 });
impl_as_primitive!(u64 => { #[cfg(has_f16)] f16, f32, f64, #[cfg(has_f128)] f128 });
impl_as_primitive!(i64 => { #[cfg(has_f16)] f16, f32, f64, #[cfg(has_f128)] f128 });
impl_as_primitive!(u128 => { #[cfg(has_f16)] f16, f32, f64, #[cfg(has_f128)] f128 });
impl_as_primitive!(i128 => { #[cfg(has_f16)] f16, f32, f64, #[cfg(has_f128)] f128 });
impl_as_primitive!(usize => { #[cfg(has_f16)] f16, f32, f64, #[cfg(has_f128)] f128 });
impl_as_primitive!(isize => { #[cfg(has_f16)] f16, f32, f64, #[cfg(has_f128)] f128 });
#[cfg(has_f16)]
impl_as_primitive!(f16 => { f16, f32, f64, #[cfg(has_f128)] f128 });
impl_as_primitive!(f32 => { #[cfg(has_f16)] f16, f32, f64, #[cfg(has_f128)] f128 });
impl_as_primitive!(f64 => { #[cfg(has_f16)] f16, f32, f64, #[cfg(has_f128)] f128 });
#[cfg(has_f128)]
impl_as_primitive!(f128 => { #[cfg(has_f16)] f16, f32, f64, f128 });
impl_as_primitive!(char => { char });
impl_as_primitive!(bool => {});
Loading