Skip to content

Commit

Permalink
Add implementations of num_traits::{FromBytes, ToBytes}.
Browse files Browse the repository at this point in the history
  • Loading branch information
kpreid committed Jan 22, 2025
1 parent 2c4122d commit 4da7f73
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 3 deletions.
12 changes: 11 additions & 1 deletion src/bfloat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down Expand Up @@ -1367,6 +1367,16 @@ mod test {
assert_eq!(<bf16 as FromPrimitive>::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!(<bf16 as ToBytes>::to_le_bytes(&two), [0, 64]);
assert_eq!(<bf16 as FromBytes>::from_le_bytes(&[0, 64]), two);
assert_eq!(<bf16 as ToBytes>::to_be_bytes(&two), [64, 0]);
assert_eq!(<bf16 as FromBytes>::from_be_bytes(&[64, 0]), two);
}

#[test]
fn test_bf16_consts_from_f32() {
let one = bf16::from_f32(1.0);
Expand Down
12 changes: 11 additions & 1 deletion src/binary16.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down Expand Up @@ -1383,6 +1383,16 @@ mod test {
assert_eq!(<f16 as FromPrimitive>::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!(<f16 as ToBytes>::to_le_bytes(&two), [0, 64]);
assert_eq!(<f16 as FromBytes>::from_le_bytes(&[0, 64]), two);
assert_eq!(<f16 as ToBytes>::to_be_bytes(&two), [64, 0]);
assert_eq!(<f16 as FromBytes>::from_be_bytes(&[64, 0]), two);
}

#[test]
fn test_f16_consts() {
// DIGITS
Expand Down
67 changes: 66 additions & 1 deletion src/num_traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<i64> {
Expand Down Expand Up @@ -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)
}
}

0 comments on commit 4da7f73

Please sign in to comment.