Skip to main content

polars_utils/
float16.rs

1#[cfg(feature = "python")]
2use std::convert::Infallible;
3use std::fmt::{Display, LowerExp};
4use std::iter::Sum;
5use std::ops::*;
6
7use bytemuck::{Pod, Zeroable};
8use half;
9use num_derive::*;
10use num_traits::real::Real;
11use num_traits::{AsPrimitive, Bounded, FromBytes, One, Pow, ToBytes, Zero};
12#[cfg(feature = "python")]
13use numpy::Element;
14#[cfg(feature = "python")]
15use pyo3::types::PyFloat;
16#[cfg(feature = "python")]
17use pyo3::{FromPyObject, IntoPyObject, PyErr, Python};
18#[cfg(feature = "serde")]
19use serde::{Deserialize, Serialize};
20
21use crate::nulls::IsNull;
22
23/// A portable float16 type.
24///
25/// This type is a newtype wrapper around `half::f16`.
26/// We intend to replace it by Rust's builtin `f16` type once it is stabilized.
27#[derive(
28    Debug,
29    Copy,
30    Clone,
31    Default,
32    PartialEq,
33    PartialOrd,
34    Zeroable,
35    Pod,
36    Float,
37    FromPrimitive,
38    Num,
39    NumCast,
40    NumOps,
41    One,
42    ToPrimitive,
43    Zero,
44)]
45#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
46#[allow(non_camel_case_types)]
47#[repr(transparent)]
48pub struct pf16(pub half::f16);
49
50#[cfg(feature = "dsl-schema")]
51impl schemars::JsonSchema for pf16 {
52    fn schema_name() -> std::borrow::Cow<'static, str> {
53        "f16".into()
54    }
55
56    fn schema_id() -> std::borrow::Cow<'static, str> {
57        std::borrow::Cow::Borrowed(concat!(module_path!(), "::", "pf16"))
58    }
59
60    fn json_schema(generator: &mut schemars::SchemaGenerator) -> schemars::Schema {
61        f32::json_schema(generator)
62    }
63}
64
65impl pf16 {
66    pub const NAN: Self = pf16(half::f16::NAN);
67    pub const INFINITY: Self = pf16(half::f16::INFINITY);
68    pub const NEG_INFINITY: Self = pf16(half::f16::NEG_INFINITY);
69
70    #[inline]
71    pub fn to_le_bytes(&self) -> [u8; 2] {
72        self.0.to_le_bytes()
73    }
74
75    #[inline]
76    pub fn is_nan(self) -> bool {
77        self.0.is_nan()
78    }
79
80    #[inline]
81    pub fn is_finite(self) -> bool {
82        self.0.is_finite()
83    }
84
85    #[inline]
86    pub fn abs(self) -> Self {
87        pf16(self.0.abs())
88    }
89
90    #[inline]
91    pub fn trunc(self) -> Self {
92        pf16(self.0.trunc())
93    }
94
95    #[inline]
96    pub fn round(self) -> Self {
97        pf16(self.0.round())
98    }
99
100    #[inline]
101    pub fn floor(self) -> Self {
102        pf16(self.0.floor())
103    }
104
105    #[inline]
106    pub fn ceil(self) -> Self {
107        pf16(self.0.ceil())
108    }
109
110    #[inline]
111    pub fn log(self, base: Self) -> Self {
112        pf16(self.0.log(base.0))
113    }
114
115    #[inline]
116    pub fn to_bits(self) -> u16 {
117        self.0.to_bits()
118    }
119
120    #[inline]
121    pub fn from_bits(b: u16) -> Self {
122        pf16(half::f16::from_bits(b))
123    }
124
125    /// The least value greater than `self`, as [`f32::next_up`].
126    #[inline]
127    pub fn next_up(self) -> Self {
128        self.step(true)
129    }
130
131    /// The greatest value less than `self`, as [`f32::next_down`].
132    #[inline]
133    pub fn next_down(self) -> Self {
134        self.step(false)
135    }
136
137    fn step(self, up: bool) -> Self {
138        let bits = self.to_bits();
139        let (positive, magnitude) = (bits & 0x8000 == 0, bits & 0x7FFF);
140        // A step away from zero increments the magnitude's bits, and toward zero decrements them.
141        let bits = if self.is_nan() || (magnitude == 0x7C00 && positive == up) {
142            bits
143        } else if magnitude == 0 {
144            if up { 0x0001 } else { 0x8001 }
145        } else if positive == up {
146            bits + 1
147        } else {
148            bits - 1
149        };
150        Self::from_bits(bits)
151    }
152
153    #[inline]
154    pub fn min(self, other: Self) -> Self {
155        pf16(self.0.min(other.0))
156    }
157
158    #[inline]
159    pub fn max(self, other: Self) -> Self {
160        pf16(self.0.max(other.0))
161    }
162}
163
164impl Display for pf16 {
165    #[inline]
166    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
167        Display::fmt(&self.0.to_f32(), f)
168    }
169}
170
171impl Neg for pf16 {
172    type Output = Self;
173
174    #[inline]
175    fn neg(self) -> Self::Output {
176        pf16(-self.0)
177    }
178}
179
180impl AddAssign for pf16 {
181    #[inline]
182    fn add_assign(&mut self, other: Self) {
183        self.0 = self.0 + other.0;
184    }
185}
186
187impl SubAssign for pf16 {
188    #[inline]
189    fn sub_assign(&mut self, other: Self) {
190        self.0 = self.0 - other.0;
191    }
192}
193
194impl MulAssign for pf16 {
195    #[inline]
196    fn mul_assign(&mut self, other: Self) {
197        self.0 = self.0 * other.0;
198    }
199}
200
201impl DivAssign for pf16 {
202    #[inline]
203    fn div_assign(&mut self, other: Self) {
204        self.0 = self.0 / other.0;
205    }
206}
207
208impl RemAssign for pf16 {
209    #[inline]
210    fn rem_assign(&mut self, other: Self) {
211        self.0 = self.0 % other.0;
212    }
213}
214
215impl Sum for pf16 {
216    fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
217        let mut acc = Zero::zero();
218        for v in iter {
219            acc += v.0;
220        }
221        pf16(acc)
222    }
223}
224
225impl Pow<pf16> for pf16 {
226    type Output = Self;
227
228    #[inline]
229    fn pow(self, rhs: pf16) -> Self::Output {
230        pf16(self.0.powf(rhs.0))
231    }
232}
233
234impl From<bool> for pf16 {
235    #[inline]
236    fn from(value: bool) -> Self {
237        if value { One::one() } else { Zero::zero() }
238    }
239}
240
241impl From<pf16> for f32 {
242    #[inline]
243    fn from(value: pf16) -> Self {
244        value.0.to_f32()
245    }
246}
247
248impl From<pf16> for f64 {
249    #[inline]
250    fn from(value: pf16) -> Self {
251        value.0.to_f64()
252    }
253}
254
255impl From<f32> for pf16 {
256    #[inline]
257    fn from(value: f32) -> Self {
258        pf16(half::f16::from_f32(value))
259    }
260}
261
262impl From<f64> for pf16 {
263    #[inline]
264    fn from(value: f64) -> Self {
265        pf16(half::f16::from_f64(value))
266    }
267}
268
269impl Bounded for pf16 {
270    fn min_value() -> Self {
271        pf16(half::f16::MIN)
272    }
273
274    fn max_value() -> Self {
275        pf16(half::f16::MAX)
276    }
277}
278
279macro_rules! impl_as_primitive {
280    ($ty:ty) => {
281        impl AsPrimitive<$ty> for pf16 {
282            #[inline]
283            fn as_(self) -> $ty {
284                self.0.as_()
285            }
286        }
287    };
288}
289
290impl_as_primitive!(u8);
291impl_as_primitive!(u16);
292impl_as_primitive!(u32);
293impl_as_primitive!(u64);
294impl_as_primitive!(i8);
295impl_as_primitive!(i16);
296impl_as_primitive!(i32);
297impl_as_primitive!(i64);
298impl_as_primitive!(f32);
299impl_as_primitive!(f64);
300
301impl AsPrimitive<pf16> for pf16 {
302    #[inline]
303    fn as_(self) -> pf16 {
304        self
305    }
306}
307
308impl AsPrimitive<u128> for pf16 {
309    #[inline]
310    fn as_(self) -> u128 {
311        self.0.to_f64() as u128
312    }
313}
314
315impl AsPrimitive<i128> for pf16 {
316    #[inline]
317    fn as_(self) -> i128 {
318        self.0.to_f64() as i128
319    }
320}
321
322macro_rules! impl_as_primitive_pf16_from {
323    ($ty:ty) => {
324        impl AsPrimitive<pf16> for $ty {
325            #[inline]
326            fn as_(self) -> pf16 {
327                pf16(<$ty>::as_(self))
328            }
329        }
330    };
331}
332impl_as_primitive_pf16_from!(usize);
333impl_as_primitive_pf16_from!(u8);
334impl_as_primitive_pf16_from!(u16);
335impl_as_primitive_pf16_from!(u32);
336impl_as_primitive_pf16_from!(u64);
337impl_as_primitive_pf16_from!(isize);
338impl_as_primitive_pf16_from!(i8);
339impl_as_primitive_pf16_from!(i16);
340impl_as_primitive_pf16_from!(i32);
341impl_as_primitive_pf16_from!(i64);
342impl_as_primitive_pf16_from!(f32);
343impl_as_primitive_pf16_from!(f64);
344
345impl AsPrimitive<pf16> for i128 {
346    #[inline]
347    fn as_(self) -> pf16 {
348        pf16(half::f16::from_f64(self as f64))
349    }
350}
351
352impl AsPrimitive<pf16> for u128 {
353    #[inline]
354    fn as_(self) -> pf16 {
355        pf16(half::f16::from_f64(self as f64))
356    }
357}
358
359impl IsNull for pf16 {
360    const HAS_NULLS: bool = false;
361    type Inner = pf16;
362
363    #[inline(always)]
364    fn is_null(&self) -> bool {
365        false
366    }
367    #[inline]
368    fn unwrap_inner(self) -> Self::Inner {
369        self
370    }
371}
372
373impl ToBytes for pf16 {
374    type Bytes = [u8; 2];
375
376    #[inline]
377    fn to_be_bytes(&self) -> Self::Bytes {
378        self.0.to_be_bytes()
379    }
380
381    #[inline]
382    fn to_le_bytes(&self) -> Self::Bytes {
383        self.0.to_le_bytes()
384    }
385
386    #[inline]
387    fn to_ne_bytes(&self) -> Self::Bytes {
388        self.0.to_ne_bytes()
389    }
390}
391
392impl FromBytes for pf16 {
393    type Bytes = [u8; 2];
394
395    #[inline]
396    fn from_be_bytes(bytes: &Self::Bytes) -> Self {
397        pf16(half::f16::from_be_bytes(*bytes))
398    }
399
400    #[inline]
401    fn from_le_bytes(bytes: &Self::Bytes) -> Self {
402        pf16(half::f16::from_le_bytes(*bytes))
403    }
404
405    #[inline]
406    fn from_ne_bytes(bytes: &Self::Bytes) -> Self {
407        pf16(half::f16::from_ne_bytes(*bytes))
408    }
409}
410
411impl LowerExp for pf16 {
412    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
413        LowerExp::fmt(&self.0.to_f32(), f)
414    }
415}
416
417#[cfg(feature = "python")]
418impl<'py> IntoPyObject<'py> for pf16 {
419    type Target = PyFloat;
420    type Output = pyo3::Bound<'py, Self::Target>;
421    type Error = Infallible;
422
423    #[inline]
424    fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self::Error> {
425        f32::into_pyobject(self.into(), py)
426    }
427}
428
429#[cfg(feature = "python")]
430impl<'a, 'py> FromPyObject<'a, 'py> for pf16 {
431    type Error = PyErr;
432
433    fn extract(ob: pyo3::Borrowed<'a, 'py, pyo3::PyAny>) -> pyo3::PyResult<Self> {
434        let v: f32 = ob.extract()?;
435        Ok(v.as_())
436    }
437}
438
439#[cfg(feature = "python")]
440unsafe impl Element for pf16 {
441    const IS_COPY: bool = half::f16::IS_COPY;
442
443    fn get_dtype(py: Python<'_>) -> pyo3::Bound<'_, numpy::PyArrayDescr> {
444        half::f16::get_dtype(py)
445    }
446    fn clone_ref(&self, py: Python<'_>) -> Self {
447        pf16(half::f16::clone_ref(&self.0, py))
448    }
449}