diff --git a/src/bfloat.rs b/src/bfloat.rs index 86d96f3..7fe54d5 100644 --- a/src/bfloat.rs +++ b/src/bfloat.rs @@ -1332,7 +1332,7 @@ mod test { #[allow(unused_imports)] use core::cmp::Ordering; #[cfg(feature = "num-traits")] - use num_traits::{AsPrimitive, FromPrimitive, ToPrimitive}; + use num_traits::{AsPrimitive, FromBytes, FromPrimitive, ToBytes, ToPrimitive}; use quickcheck_macros::quickcheck; #[cfg(feature = "num-traits")] @@ -1367,6 +1367,16 @@ mod test { assert_eq!(::from_f64(2.0).unwrap(), two); } + #[cfg(feature = "num-traits")] + #[test] + fn to_and_from_bytes() { + let two = bf16::from_f32(2.0); + assert_eq!(::to_le_bytes(&two), [0, 64]); + assert_eq!(::from_le_bytes(&[0, 64]), two); + assert_eq!(::to_be_bytes(&two), [64, 0]); + assert_eq!(::from_be_bytes(&[64, 0]), two); + } + #[test] fn test_bf16_consts_from_f32() { let one = bf16::from_f32(1.0); diff --git a/src/binary16.rs b/src/binary16.rs index 5a23bd6..00afe2f 100644 --- a/src/binary16.rs +++ b/src/binary16.rs @@ -1348,7 +1348,7 @@ mod test { #[allow(unused_imports)] use core::cmp::Ordering; #[cfg(feature = "num-traits")] - use num_traits::{AsPrimitive, FromPrimitive, ToPrimitive}; + use num_traits::{AsPrimitive, FromBytes, FromPrimitive, ToBytes, ToPrimitive}; use quickcheck_macros::quickcheck; #[cfg(feature = "num-traits")] @@ -1383,6 +1383,16 @@ mod test { assert_eq!(::from_f64(2.0).unwrap(), two); } + #[cfg(feature = "num-traits")] + #[test] + fn to_and_from_bytes() { + let two = f16::from_f32(2.0); + assert_eq!(::to_le_bytes(&two), [0, 64]); + assert_eq!(::from_le_bytes(&[0, 64]), two); + assert_eq!(::to_be_bytes(&two), [64, 0]); + assert_eq!(::from_be_bytes(&[64, 0]), two); + } + #[test] fn test_f16_consts() { // DIGITS diff --git a/src/num_traits.rs b/src/num_traits.rs index 7ffd4a6..d4ef6c8 100644 --- a/src/num_traits.rs +++ b/src/num_traits.rs @@ -2,7 +2,8 @@ use crate::{bf16, f16}; use core::cmp::Ordering; use core::{num::FpCategory, ops::Div}; use num_traits::{ - AsPrimitive, Bounded, FloatConst, FromPrimitive, Num, NumCast, One, ToPrimitive, Zero, + AsPrimitive, Bounded, FloatConst, FromBytes, FromPrimitive, Num, NumCast, One, ToBytes, + ToPrimitive, Zero, }; impl ToPrimitive for f16 { @@ -744,6 +745,38 @@ impl_as_primitive_f16_from!(usize, from_f32); impl_as_primitive_f16_from!(f32, from_f32); impl_as_primitive_f16_from!(f64, from_f64); +impl ToBytes for f16 { + type Bytes = [u8; 2]; + + fn to_be_bytes(&self) -> Self::Bytes { + Self::to_be_bytes(*self) + } + + fn to_le_bytes(&self) -> Self::Bytes { + Self::to_le_bytes(*self) + } + + fn to_ne_bytes(&self) -> Self::Bytes { + Self::to_ne_bytes(*self) + } +} + +impl FromBytes for f16 { + type Bytes = [u8; 2]; + + fn from_be_bytes(bytes: &Self::Bytes) -> Self { + Self::from_be_bytes(*bytes) + } + + fn from_le_bytes(bytes: &Self::Bytes) -> Self { + Self::from_le_bytes(*bytes) + } + + fn from_ne_bytes(bytes: &Self::Bytes) -> Self { + Self::from_ne_bytes(*bytes) + } +} + impl ToPrimitive for bf16 { #[inline] fn to_i64(&self) -> Option { @@ -1483,3 +1516,35 @@ impl_as_primitive_bf16_from!(isize, from_f32); impl_as_primitive_bf16_from!(usize, from_f32); impl_as_primitive_bf16_from!(f32, from_f32); impl_as_primitive_bf16_from!(f64, from_f64); + +impl ToBytes for bf16 { + type Bytes = [u8; 2]; + + fn to_be_bytes(&self) -> Self::Bytes { + Self::to_be_bytes(*self) + } + + fn to_le_bytes(&self) -> Self::Bytes { + Self::to_le_bytes(*self) + } + + fn to_ne_bytes(&self) -> Self::Bytes { + Self::to_ne_bytes(*self) + } +} + +impl FromBytes for bf16 { + type Bytes = [u8; 2]; + + fn from_be_bytes(bytes: &Self::Bytes) -> Self { + Self::from_be_bytes(*bytes) + } + + fn from_le_bytes(bytes: &Self::Bytes) -> Self { + Self::from_le_bytes(*bytes) + } + + fn from_ne_bytes(bytes: &Self::Bytes) -> Self { + Self::from_ne_bytes(*bytes) + } +}