Skip to main content

unsigned_float/
uf32.rs

1use core::cmp::Ordering;
2use core::fmt;
3use core::ops::{Add, Div, Mul, Sub};
4
5use crate::{ConversionError, dispatch};
6
7#[derive(Clone, Copy, Default, Hash, PartialEq, Eq)]
8#[repr(transparent)]
9/// A 32-bit unsigned float with 8 exponent bits and 24 mantissa bits.
10pub struct Uf32E8M24(u32);
11
12/// Default 32-bit unsigned float layout.
13pub type Uf32 = Uf32E8M24;
14
15impl Uf32E8M24 {
16    pub const EXPONENT_BITS: u32 = 8;
17    pub const MANTISSA_BITS: u32 = 24;
18    pub const EXPONENT_BIAS: i32 = 127;
19    pub const EXPONENT_MASK: u32 = 0xff00_0000;
20    pub const MANTISSA_MASK: u32 = 0x00ff_ffff;
21
22    pub const ZERO: Self = Self(0);
23    pub const ONE: Self = Self(0x7f00_0000);
24    pub const INFINITY: Self = Self(0xff00_0000);
25    pub const NAN: Self = Self(0xff80_0000);
26    pub const MAX: Self = Self(0xfeff_ffff);
27    pub const MIN_POSITIVE: Self = Self(0x0000_0001);
28    pub const MIN_NORMAL: Self = Self(0x0100_0000);
29
30    pub const fn from_bits(bits: u32) -> Self {
31        Self(bits)
32    }
33
34    pub const fn to_bits(self) -> u32 {
35        self.0
36    }
37
38    pub fn from_f64(value: f64) -> Self {
39        Self(dispatch::f64_to_uf32(value))
40    }
41
42    pub fn to_f64(self) -> f64 {
43        dispatch::uf32_to_f64(self.0)
44    }
45
46    pub fn try_from_f64(value: f64) -> Result<Self, ConversionError> {
47        crate::convert::check_finite_non_negative(value)?;
48
49        let encoded = Self::from_f64(value);
50        crate::convert::check_encoded(value, encoded.is_zero(), encoded.is_infinite())?;
51
52        Ok(encoded)
53    }
54
55    pub fn from_f32(value: f32) -> Self {
56        Self::from_f64(value as f64)
57    }
58
59    pub fn to_f32(self) -> f32 {
60        self.to_f64() as f32
61    }
62
63    #[cfg(feature = "f16")]
64    pub fn from_f16(value: f16) -> Self {
65        Self::from_f64(value as f64)
66    }
67
68    #[cfg(feature = "f16")]
69    pub fn to_f16(self) -> f16 {
70        self.to_f64() as f16
71    }
72
73    pub const fn exponent(self) -> u32 {
74        (self.0 & Self::EXPONENT_MASK) >> Self::MANTISSA_BITS
75    }
76
77    pub const fn mantissa(self) -> u32 {
78        self.0 & Self::MANTISSA_MASK
79    }
80
81    pub const fn is_zero(self) -> bool {
82        self.0 == 0
83    }
84
85    pub const fn is_nan(self) -> bool {
86        self.exponent() == 0xff && self.mantissa() != 0
87    }
88
89    pub const fn is_infinite(self) -> bool {
90        self.0 == Self::INFINITY.0
91    }
92
93    pub const fn is_finite(self) -> bool {
94        self.exponent() != 0xff
95    }
96
97    pub const fn is_subnormal(self) -> bool {
98        self.exponent() == 0 && self.mantissa() != 0
99    }
100}
101
102impl From<f32> for Uf32E8M24 {
103    fn from(value: f32) -> Self {
104        Self::from_f32(value)
105    }
106}
107
108#[cfg(feature = "f16")]
109impl From<f16> for Uf32E8M24 {
110    fn from(value: f16) -> Self {
111        Self::from_f16(value)
112    }
113}
114
115#[cfg(feature = "f16")]
116impl From<Uf32E8M24> for f16 {
117    fn from(value: Uf32E8M24) -> Self {
118        value.to_f16()
119    }
120}
121
122impl From<Uf32E8M24> for f32 {
123    fn from(value: Uf32E8M24) -> Self {
124        value.to_f32()
125    }
126}
127
128impl From<Uf32E8M24> for f64 {
129    fn from(value: Uf32E8M24) -> Self {
130        value.to_f64()
131    }
132}
133
134impl Ord for Uf32E8M24 {
135    fn cmp(&self, other: &Self) -> Ordering {
136        self.0.cmp(&other.0)
137    }
138}
139
140impl PartialOrd for Uf32E8M24 {
141    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
142        Some(self.cmp(other))
143    }
144}
145
146impl Add for Uf32E8M24 {
147    type Output = Self;
148
149    fn add(self, rhs: Self) -> Self::Output {
150        Self(dispatch::add_uf32(self.0, rhs.0))
151    }
152}
153
154impl Sub for Uf32E8M24 {
155    type Output = Self;
156
157    fn sub(self, rhs: Self) -> Self::Output {
158        Self(dispatch::sub_uf32(self.0, rhs.0))
159    }
160}
161
162impl Mul for Uf32E8M24 {
163    type Output = Self;
164
165    fn mul(self, rhs: Self) -> Self::Output {
166        Self(dispatch::mul_uf32(self.0, rhs.0))
167    }
168}
169
170impl Div for Uf32E8M24 {
171    type Output = Self;
172
173    fn div(self, rhs: Self) -> Self::Output {
174        Self(dispatch::div_uf32(self.0, rhs.0))
175    }
176}
177
178impl fmt::Debug for Uf32E8M24 {
179    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
180        f.debug_tuple("Uf32E8M24").field(&self.to_f64()).finish()
181    }
182}