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

Add f16, f128#333

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
usamoi wants to merge3 commits intorust-num:master
base:master
Choose a base branch
Loading
fromusamoi:master
Open
Show file tree
Hide file tree
Changes from1 commit
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
NextNext commit
add f16
  • Loading branch information
@usamoi
usamoi committedMay 31, 2025
commiteac01b9712a0a50648b4bf5728ed1f4d8ae27537
4 changes: 4 additions & 0 deletionsbuild.rs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -4,4 +4,8 @@ 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)");
}
6 changes: 5 additions & 1 deletionsrc/bounds.rs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
#[cfg(has_f16)]
use core::f16;
use core::num::Wrapping;
use core::{f32, f64};
use core::{i128, i16, i32, i64, i8, isize};
Expand DownExpand Up@@ -77,7 +79,10 @@ 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);

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

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

#[test]
fn wrapping_bounded() {
Expand Down
87 changes: 68 additions & 19 deletionssrc/cast.rs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
#[cfg(has_f16)]
use core::f16;
use core::mem::size_of;
use core::num::Wrapping;
use core::{f32, f64};
Expand DownExpand Up@@ -101,6 +103,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`.
Expand DownExpand Up@@ -177,6 +188,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)
Expand DownExpand Up@@ -247,6 +263,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)
Expand All@@ -267,8 +288,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.
Expand DownExpand Up@@ -364,13 +386,17 @@ 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_f16)]
impl_to_primitive_float!(f16);
impl_to_primitive_float!(f32);
impl_to_primitive_float!(f64);

Expand DownExpand Up@@ -469,6 +495,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]
Expand DownExpand Up@@ -545,6 +579,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()
Expand All@@ -569,6 +608,8 @@ 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);

Expand DownExpand Up@@ -598,6 +639,8 @@ 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;
}
Expand DownExpand Up@@ -629,6 +672,8 @@ 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);
}
Expand DownExpand Up@@ -692,6 +737,8 @@ 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);

Expand DownExpand Up@@ -742,29 +789,31 @@ 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 });
impl_as_primitive!(i8 => { #[cfg(has_f16)] f16, f32, f64 });
impl_as_primitive!(u16 => { #[cfg(has_f16)] f16, f32, f64 });
impl_as_primitive!(i16 => { #[cfg(has_f16)] f16, f32, f64 });
impl_as_primitive!(u32 => { #[cfg(has_f16)] f16, f32, f64 });
impl_as_primitive!(i32 => { #[cfg(has_f16)] f16, f32, f64 });
impl_as_primitive!(u64 => { #[cfg(has_f16)] f16, f32, f64 });
impl_as_primitive!(i64 => { #[cfg(has_f16)] f16, f32, f64 });
impl_as_primitive!(u128 => { #[cfg(has_f16)] f16, f32, f64 });
impl_as_primitive!(i128 => { #[cfg(has_f16)] f16, f32, f64 });
impl_as_primitive!(usize => { #[cfg(has_f16)] f16, f32, f64 });
impl_as_primitive!(isize => { #[cfg(has_f16)] f16, f32, f64 });
#[cfg(has_f16)]
impl_as_primitive!(f16 => { f16, f32, f64 });
impl_as_primitive!(f32 => { #[cfg(has_f16)] f16, f32, f64 });
impl_as_primitive!(f64 => { #[cfg(has_f16)] f16, f32, f64 });
impl_as_primitive!(char => { char });
impl_as_primitive!(bool => {});
Loading

[8]ページ先頭

©2009-2025 Movatter.jp