use bytemuck::{Pod, Zeroable};
use core::fmt::Display;
use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign};
use num_traits::{NumCast, ToPrimitive};
#[allow(non_camel_case_types)]
#[repr(transparent)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Clone, Copy, Default, Zeroable, Pod, PartialEq, PartialOrd, Debug)]
pub struct tf32(f32);
impl tf32 {
#[inline]
#[must_use]
pub const fn from_bits(bits: u32) -> tf32 {
tf32(f32::from_bits(bits))
}
#[inline]
#[must_use]
pub const fn from_f32(value: f32) -> tf32 {
tf32(value)
}
#[inline]
#[must_use]
pub const fn from_f64(value: f64) -> tf32 {
tf32(value as f32)
}
#[inline]
#[must_use]
pub const fn to_bits(self) -> u32 {
f32::to_bits(self.0)
}
#[inline]
#[must_use]
pub const fn to_f32(self) -> f32 {
self.0
}
#[inline]
#[must_use]
pub const fn to_f64(self) -> f64 {
self.0 as f64
}
}
impl Neg for tf32 {
type Output = Self;
fn neg(self) -> Self::Output {
Self::from_f32(self.to_f32().neg())
}
}
impl Mul for tf32 {
type Output = Self;
fn mul(self, rhs: Self) -> Self::Output {
Self::from_f32(self.to_f32() * rhs.to_f32())
}
}
impl MulAssign for tf32 {
fn mul_assign(&mut self, rhs: Self) {
*self = *self * rhs;
}
}
impl Div for tf32 {
type Output = Self;
fn div(self, rhs: Self) -> Self::Output {
Self::from_f32(self.to_f32() / rhs.to_f32())
}
}
impl DivAssign for tf32 {
fn div_assign(&mut self, rhs: Self) {
*self = *self / rhs;
}
}
impl Add for tf32 {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Self::from_f32(self.to_f32() + rhs.to_f32())
}
}
impl AddAssign for tf32 {
fn add_assign(&mut self, rhs: Self) {
*self = *self + rhs;
}
}
impl Sub for tf32 {
type Output = Self;
fn sub(self, rhs: Self) -> Self::Output {
Self::from_f32(self.to_f32() - rhs.to_f32())
}
}
impl SubAssign for tf32 {
fn sub_assign(&mut self, rhs: Self) {
*self = *self - rhs;
}
}
impl ToPrimitive for tf32 {
fn to_i64(&self) -> Option<i64> {
Some(tf32::to_f32(*self) as i64)
}
fn to_u64(&self) -> Option<u64> {
Some(tf32::to_f64(*self) as u64)
}
fn to_f32(&self) -> Option<f32> {
Some(tf32::to_f32(*self))
}
fn to_f64(&self) -> Option<f64> {
Some(tf32::to_f64(*self))
}
}
impl NumCast for tf32 {
fn from<T: num_traits::ToPrimitive>(n: T) -> Option<Self> {
Some(Self::from_f32(n.to_f32()?))
}
}
impl Display for tf32 {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "{}", self.0)
}
}