diff options
Diffstat (limited to 'src/binary16.rs')
-rw-r--r-- | src/binary16.rs | 106 |
1 files changed, 104 insertions, 2 deletions
diff --git a/src/binary16.rs b/src/binary16.rs index d5164f0..59ef1dc 100644 --- a/src/binary16.rs +++ b/src/binary16.rs @@ -1,9 +1,14 @@ #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; +#[cfg(feature = "bytemuck")] +use bytemuck::{Pod, Zeroable}; + use core::{ cmp::Ordering, - fmt::{Debug, Display, Error, Formatter, LowerExp, UpperExp}, + fmt::{ + Binary, Debug, Display, Error, Formatter, LowerExp, LowerHex, Octal, UpperExp, UpperHex, + }, num::{FpCategory, ParseFloatError}, str::FromStr, }; @@ -25,8 +30,81 @@ pub(crate) mod convert; #[derive(Clone, Copy, Default)] #[repr(transparent)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] +#[cfg_attr(feature = "bytemuck", derive(Zeroable, Pod))] pub struct f16(u16); +#[cfg(feature = "num-traits")] +mod impl_num_traits { + use super::f16; + use num_traits::{FromPrimitive, ToPrimitive}; + + impl ToPrimitive for f16 { + fn to_i64(&self) -> Option<i64> { + Self::to_f32(*self).to_i64() + } + fn to_u64(&self) -> Option<u64> { + Self::to_f32(*self).to_u64() + } + fn to_i8(&self) -> Option<i8> { + Self::to_f32(*self).to_i8() + } + fn to_u8(&self) -> Option<u8> { + Self::to_f32(*self).to_u8() + } + fn to_i16(&self) -> Option<i16> { + Self::to_f32(*self).to_i16() + } + fn to_u16(&self) -> Option<u16> { + Self::to_f32(*self).to_u16() + } + fn to_i32(&self) -> Option<i32> { + Self::to_f32(*self).to_i32() + } + fn to_u32(&self) -> Option<u32> { + Self::to_f32(*self).to_u32() + } + fn to_f32(&self) -> Option<f32> { + Some(Self::to_f32(*self)) + } + fn to_f64(&self) -> Option<f64> { + Some(Self::to_f64(*self)) + } + } + + impl FromPrimitive for f16 { + fn from_i64(n: i64) -> Option<Self> { + n.to_f32().map(|x| Self::from_f32(x)) + } + fn from_u64(n: u64) -> Option<Self> { + n.to_f32().map(|x| Self::from_f32(x)) + } + fn from_i8(n: i8) -> Option<Self> { + n.to_f32().map(|x| Self::from_f32(x)) + } + fn from_u8(n: u8) -> Option<Self> { + n.to_f32().map(|x| Self::from_f32(x)) + } + fn from_i16(n: i16) -> Option<Self> { + n.to_f32().map(|x| Self::from_f32(x)) + } + fn from_u16(n: u16) -> Option<Self> { + n.to_f32().map(|x| Self::from_f32(x)) + } + fn from_i32(n: i32) -> Option<Self> { + n.to_f32().map(|x| Self::from_f32(x)) + } + fn from_u32(n: u32) -> Option<Self> { + n.to_f32().map(|x| Self::from_f32(x)) + } + fn from_f32(n: f32) -> Option<Self> { + n.to_f32().map(|x| Self::from_f32(x)) + } + fn from_f64(n: f64) -> Option<Self> { + n.to_f64().map(|x| Self::from_f64(x)) + } + } +} + #[deprecated( since = "1.4.0", note = "all constants moved to associated constants of [`f16`](../struct.f16.html)" @@ -830,7 +908,7 @@ impl FromStr for f16 { impl Debug for f16 { fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> { - write!(f, "0x{:X}", self.0) + write!(f, "{:?}", self.to_f32()) } } @@ -852,6 +930,30 @@ impl UpperExp for f16 { } } +impl Binary for f16 { + fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> { + write!(f, "{:b}", self.0) + } +} + +impl Octal for f16 { + fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> { + write!(f, "{:o}", self.0) + } +} + +impl LowerHex for f16 { + fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> { + write!(f, "{:x}", self.0) + } +} + +impl UpperHex for f16 { + fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> { + write!(f, "{:X}", self.0) + } +} + #[allow( clippy::cognitive_complexity, clippy::float_cmp, |