[go: up one dir, main page]

float16/
bfloat.rs

1use core::{
2    cmp::Ordering,
3    iter::{Product, Sum},
4    num::FpCategory,
5    ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Rem, RemAssign, Sub, SubAssign},
6};
7#[cfg(not(target_arch = "spirv"))]
8use core::{
9    fmt::{
10        Binary,
11        Debug,
12        Display,
13        Error,
14        Formatter,
15        LowerExp,
16        LowerHex,
17        Octal,
18        UpperExp,
19        UpperHex,
20    },
21    num::ParseFloatError,
22    str::FromStr,
23};
24
25use crate::error::TryFromFloatError;
26use crate::try_from::try_from_lossless;
27
28pub(crate) mod convert;
29
30/// A 16-bit floating point type implementing the [`bfloat16`] format.
31///
32/// The [`bfloat16`] floating point format is a truncated 16-bit version of the
33/// IEEE 754 standard `binary32`, a.k.a [`f32`]. [`struct@bf16`] has
34/// approximately the same dynamic range as [`f32`] by having a lower precision
35/// than [`f16`][crate::f16]. While [`f16`][crate::f16] has a precision of
36/// 11 bits, [`struct@bf16`] has a precision of only 8 bits.
37///
38/// [`bfloat16`]: https://en.wikipedia.org/wiki/Bfloat16_floating-point_format
39#[repr(C)]
40#[allow(non_camel_case_types)]
41#[derive(Clone, Copy, Default)]
42#[cfg_attr(kani, derive(kani::Arbitrary))]
43pub struct bf16(u16);
44
45impl bf16 {
46    /// Constructs a [`struct@bf16`] value from the raw bits.
47    #[inline]
48    #[must_use]
49    pub const fn from_bits(bits: u16) -> bf16 {
50        bf16(bits)
51    }
52
53    /// Constructs a [`struct@bf16`] value from a 32-bit floating point value.
54    ///
55    /// This operation is lossy. If the 32-bit value is too large to fit, ±∞
56    /// will result. NaN values are preserved. Subnormal values that are too
57    /// tiny to be represented will result in ±0. All other values are
58    /// truncated and rounded to the nearest representable value.
59    #[inline]
60    #[must_use]
61    pub fn from_f32(value: f32) -> bf16 {
62        Self::from_f32_const(value)
63    }
64
65    /// Constructs a [`struct@bf16`] value from a 32-bit floating point value.
66    ///
67    /// This function is identical to [`from_f32`][Self::from_f32] except it
68    /// never uses hardware intrinsics, which allows it to be `const`.
69    /// [`from_f32`][Self::from_f32] should be preferred in any non-`const`
70    /// context.
71    ///
72    /// This operation is lossy. If the 32-bit value is too large to fit, ±∞
73    /// will result. NaN values are preserved. Subnormal values that are too
74    /// tiny to be represented will result in ±0. All other values are
75    /// truncated and rounded to the nearest representable value.
76    #[inline]
77    #[must_use]
78    pub const fn from_f32_const(value: f32) -> bf16 {
79        bf16(convert::f32_to_bf16(value))
80    }
81
82    /// Create a [`struct@bf16`] loslessly from an [`f32`].
83    ///
84    /// This is only true if the [`f32`] is non-finite
85    /// (infinite or NaN), or no non-zero bits would
86    /// be truncated.
87    ///
88    /// "Lossless" does not mean the data is represented the
89    /// same as a decimal number. For example, an [`f32`]
90    /// and [`f64`] have the significant digits (excluding the
91    /// hidden bit) for a value closest to `1e35` of:
92    /// - `f32`: `110100001001100001100`
93    /// - `f64`: `11010000100110000110000000000000000000000000000000`
94    ///
95    /// However, the [`f64`] is displayed as `1.0000000409184788e+35`,
96    /// while the value closest to `1e35` in [`f64`] is
97    /// `11010000100110000101110010110001110100110110000010`. This
98    /// makes it look like precision has been lost but this is
99    /// due to the approximations used to represent binary values as
100    /// a decimal.
101    ///
102    /// This does not respect signalling NaNs: if the value
103    /// is NaN or inf, then it will return that value.
104    ///
105    /// Since [`struct@bf16`] has the same number of exponent
106    /// bits  as [`f32`], this is effectively just checking if the
107    /// value is non-finite (infinite or NaN) or the value
108    /// is normal and the lower 16 bits are 0.
109    #[inline]
110    pub const fn from_f32_lossless(value: f32) -> Option<bf16> {
111        // NOTE: This logic is effectively just getting the top 16 bits
112        // and the bottom 16 bits, but it's done explicitly with mantissa
113        // digits for this reason. For explicit clarity, we remove the
114        // hidden bit in our exponent logic
115        const BF16_MANT_BITS: u32 = bf16::MANTISSA_DIGITS - 1;
116        const F32_MANT_BITS: u32 = f32::MANTISSA_DIGITS - 1;
117        const EXP_MASK: u32 = (f32::MAX_EXP as u32 * 2 - 1) << F32_MANT_BITS;
118        const TRUNCATED: u32 = F32_MANT_BITS - BF16_MANT_BITS;
119        const TRUNC_MASK: u32 = (1 << TRUNCATED) - 1;
120
121        // SAFETY: safe since it's plain old data
122        let bits: u32 = unsafe { core::mem::transmute(value) };
123
124        // `bits & exp_mask == exp_mask` -> infinite or NaN
125        // `truncated == 0` -> no bits truncated
126        // since the exp ranges are the same, any denormal handling
127        // is already implicit.
128        let exp = bits & EXP_MASK;
129        let is_special = exp == EXP_MASK;
130        if is_special || bits & TRUNC_MASK == 0 {
131            Some(Self::from_f32_const(value))
132        } else {
133            None
134        }
135    }
136
137    /// Constructs a [`struct@bf16`] value from a 64-bit floating point value.
138    ///
139    /// This operation is lossy. If the 64-bit value is to large to fit, ±∞ will
140    /// result. NaN values are preserved. 64-bit subnormal values are too
141    /// tiny to be represented and result in ±0. Exponents that underflow
142    /// the minimum exponent will result in subnormals or ±0. All other
143    /// values are truncated and rounded to the nearest representable value.
144    #[inline]
145    #[must_use]
146    pub fn from_f64(value: f64) -> bf16 {
147        Self::from_f64_const(value)
148    }
149
150    /// Constructs a [`struct@bf16`] value from a 64-bit floating point value.
151    ///
152    /// This function is identical to [`from_f64`][Self::from_f64] except it
153    /// never uses hardware intrinsics, which allows it to be `const`.
154    /// [`from_f64`][Self::from_f64] should be preferred in any non-`const`
155    /// context.
156    ///
157    /// This operation is lossy. If the 64-bit value is to large to fit, ±∞ will
158    /// result. NaN values are preserved. 64-bit subnormal values are too
159    /// tiny to be represented and result in ±0. Exponents that underflow
160    /// the minimum exponent will result in subnormals or ±0. All other
161    /// values are truncated and rounded to the nearest representable value.
162    #[inline]
163    #[must_use]
164    pub const fn from_f64_const(value: f64) -> bf16 {
165        bf16(convert::f64_to_bf16(value))
166    }
167
168    /// Create a [`struct@bf16`] loslessly from an [`f64`].
169    ///
170    /// This is only true if the [`f64`] is non-finite
171    /// (infinite or NaN), zero, or the exponent can be
172    /// represented by a normal [`struct@bf16`] and no
173    /// non-zero bits would be truncated.
174    ///
175    /// "Lossless" does not mean the data is represented the
176    /// same as a decimal number. For example, an [`f32`]
177    /// and [`f64`] have the significant digits (excluding the
178    /// hidden bit) for a value closest to `1e35` of:
179    /// - `f32`: `110100001001100001100`
180    /// - `f64`: `11010000100110000110000000000000000000000000000000`
181    ///
182    /// However, the [`f64`] is displayed as `1.0000000409184788e+35`,
183    /// while the value closest to `1e35` in [`f64`] is
184    /// `11010000100110000101110010110001110100110110000010`. This
185    /// makes it look like precision has been lost but this is
186    /// due to the approximations used to represent binary values as
187    /// a decimal.
188    ///
189    /// This does not respect signalling NaNs: if the value
190    /// is NaN or inf, then it will return that value.
191    #[inline]
192    pub const fn from_f64_lossless(value: f64) -> Option<bf16> {
193        try_from_lossless!(
194            value => value,
195            half => bf16,
196            full => f64,
197            half_bits => u16,
198            full_bits => u64,
199            to_half => from_f64
200        )
201    }
202
203    /// Converts a [`struct@bf16`] into the underlying bit representation.
204    #[inline]
205    #[must_use]
206    pub const fn to_bits(self) -> u16 {
207        self.0
208    }
209
210    /// Returns the memory representation of the underlying bit representation
211    /// as a byte array in little-endian byte order.
212    ///
213    /// # Examples
214    ///
215    /// ```rust
216    /// # use float16::*;
217    /// let bytes = bf16::from_f32(12.5).to_le_bytes();
218    /// assert_eq!(bytes, [0x48, 0x41]);
219    /// ```
220    #[inline]
221    #[must_use]
222    pub const fn to_le_bytes(self) -> [u8; 2] {
223        self.0.to_le_bytes()
224    }
225
226    /// Returns the memory representation of the underlying bit representation
227    /// as a byte array in big-endian (network) byte order.
228    ///
229    /// # Examples
230    ///
231    /// ```rust
232    /// # use float16::*;
233    /// let bytes = bf16::from_f32(12.5).to_be_bytes();
234    /// assert_eq!(bytes, [0x41, 0x48]);
235    /// ```
236    #[inline]
237    #[must_use]
238    pub const fn to_be_bytes(self) -> [u8; 2] {
239        self.0.to_be_bytes()
240    }
241
242    /// Returns the memory representation of the underlying bit representation
243    /// as a byte array in native byte order.
244    ///
245    /// As the target platform's native endianness is used, portable code should
246    /// use [`to_be_bytes`][bf16::to_be_bytes] or
247    /// [`to_le_bytes`][bf16::to_le_bytes], as appropriate, instead.
248    ///
249    /// # Examples
250    ///
251    /// ```rust
252    /// # use float16::*;
253    /// let bytes = bf16::from_f32(12.5).to_ne_bytes();
254    /// assert_eq!(bytes, if cfg!(target_endian = "big") {
255    ///     [0x41, 0x48]
256    /// } else {
257    ///     [0x48, 0x41]
258    /// });
259    /// ```
260    #[inline]
261    #[must_use]
262    pub const fn to_ne_bytes(self) -> [u8; 2] {
263        self.0.to_ne_bytes()
264    }
265
266    /// Creates a floating point value from its representation as a byte array
267    /// in little endian.
268    ///
269    /// # Examples
270    ///
271    /// ```rust
272    /// # use float16::*;
273    /// let value = bf16::from_le_bytes([0x48, 0x41]);
274    /// assert_eq!(value, bf16::from_f32(12.5));
275    /// ```
276    #[inline]
277    #[must_use]
278    pub const fn from_le_bytes(bytes: [u8; 2]) -> bf16 {
279        bf16::from_bits(u16::from_le_bytes(bytes))
280    }
281
282    /// Creates a floating point value from its representation as a byte array
283    /// in big endian.
284    ///
285    /// # Examples
286    ///
287    /// ```rust
288    /// # use float16::*;
289    /// let value = bf16::from_be_bytes([0x41, 0x48]);
290    /// assert_eq!(value, bf16::from_f32(12.5));
291    /// ```
292    #[inline]
293    #[must_use]
294    pub const fn from_be_bytes(bytes: [u8; 2]) -> bf16 {
295        bf16::from_bits(u16::from_be_bytes(bytes))
296    }
297
298    /// Creates a floating point value from its representation as a byte array
299    /// in native endian.
300    ///
301    /// As the target platform's native endianness is used, portable code likely
302    /// wants to use [`from_be_bytes`][bf16::from_be_bytes] or
303    /// [`from_le_bytes`][bf16::from_le_bytes], as appropriate instead.
304    ///
305    /// # Examples
306    ///
307    /// ```rust
308    /// # use float16::*;
309    /// let value = bf16::from_ne_bytes(if cfg!(target_endian = "big") {
310    ///     [0x41, 0x48]
311    /// } else {
312    ///     [0x48, 0x41]
313    /// });
314    /// assert_eq!(value, bf16::from_f32(12.5));
315    /// ```
316    #[inline]
317    #[must_use]
318    pub const fn from_ne_bytes(bytes: [u8; 2]) -> bf16 {
319        bf16::from_bits(u16::from_ne_bytes(bytes))
320    }
321
322    /// Converts a [`struct@bf16`] value into an [`f32`] value.
323    ///
324    /// This conversion is lossless as all values can be represented exactly in
325    /// [`f32`].
326    #[inline]
327    #[must_use]
328    pub fn to_f32(self) -> f32 {
329        self.to_f32_const()
330    }
331
332    /// Converts a [`struct@bf16`] value into an [`f32`] value.
333    ///
334    /// This function is identical to [`to_f32`][Self::to_f32] except it never
335    /// uses hardware intrinsics, which allows it to be `const`.
336    /// [`to_f32`][Self::to_f32] should be preferred in any non-`const`
337    /// context.
338    ///
339    /// This conversion is lossless as all values can be represented exactly in
340    /// [`f32`].
341    #[inline]
342    #[must_use]
343    pub const fn to_f32_const(self) -> f32 {
344        convert::bf16_to_f32(self.0)
345    }
346
347    /// Convert the data to an `f32` type, used for numerical operations.
348    #[inline(always)]
349    pub fn as_f32(self) -> f32 {
350        self.to_f32_const()
351    }
352
353    /// Convert the data to an `f32` type, used for numerical operations.
354    #[inline(always)]
355    pub const fn as_f32_const(self) -> f32 {
356        self.to_f32_const()
357    }
358
359    /// Converts a [`struct@bf16`] value into an [`f64`] value.
360    ///
361    /// This conversion is lossless as all values can be represented exactly in
362    /// [`f64`].
363    #[inline]
364    #[must_use]
365    pub fn to_f64(self) -> f64 {
366        self.to_f64_const()
367    }
368
369    /// Converts a [`struct@bf16`] value into an [`f64`] value.
370    ///
371    /// This function is identical to [`to_f64`][Self::to_f64] except it never
372    /// uses hardware intrinsics, which allows it to be `const`.
373    /// [`to_f64`][Self::to_f64] should be preferred in any non-`const`
374    /// context.
375    ///
376    /// This conversion is lossless as all values can be represented exactly in
377    /// [`f64`].
378    #[inline]
379    #[must_use]
380    pub const fn to_f64_const(self) -> f64 {
381        convert::bf16_to_f64(self.0)
382    }
383
384    /// Convert the data to an `f64` type, used for numerical operations.
385    #[inline(always)]
386    pub fn as_f64(self) -> f64 {
387        self.to_f64_const()
388    }
389
390    /// Convert the data to an `f64` type, used for numerical operations.
391    #[inline(always)]
392    pub const fn as_f64_const(self) -> f64 {
393        self.to_f64_const()
394    }
395
396    /// Returns `true` if this value is NaN and `false` otherwise.
397    ///
398    /// # Examples
399    ///
400    /// ```rust
401    /// # use float16::*;
402    ///
403    /// let nan = bf16::NAN;
404    /// let f = bf16::from_f32(7.0_f32);
405    ///
406    /// assert!(nan.is_nan());
407    /// assert!(!f.is_nan());
408    /// ```
409    #[inline]
410    #[must_use]
411    pub const fn is_nan(self) -> bool {
412        self.0 & Self::NOT_SIGN > Self::EXP_MASK
413    }
414
415    /// Computes the absolute value of `self`.
416    #[must_use]
417    #[inline(always)]
418    pub const fn abs(self) -> Self {
419        Self(self.0 & !Self::SIGN_MASK)
420    }
421
422    /// Returns `true` if this value is ±∞ and `false` otherwise.
423    ///
424    /// # Examples
425    ///
426    /// ```rust
427    /// # use float16::*;
428    ///
429    /// let f = bf16::from_f32(7.0f32);
430    /// let inf = bf16::INFINITY;
431    /// let neg_inf = bf16::NEG_INFINITY;
432    /// let nan = bf16::NAN;
433    ///
434    /// assert!(!f.is_infinite());
435    /// assert!(!nan.is_infinite());
436    ///
437    /// assert!(inf.is_infinite());
438    /// assert!(neg_inf.is_infinite());
439    /// ```
440    #[inline]
441    #[must_use]
442    pub const fn is_infinite(self) -> bool {
443        self.0 & Self::NOT_SIGN == Self::EXP_MASK
444    }
445
446    /// Returns `true` if this number is neither infinite nor NaN.
447    ///
448    /// # Examples
449    ///
450    /// ```rust
451    /// # use float16::*;
452    ///
453    /// let f = bf16::from_f32(7.0f32);
454    /// let inf = bf16::INFINITY;
455    /// let neg_inf = bf16::NEG_INFINITY;
456    /// let nan = bf16::NAN;
457    ///
458    /// assert!(f.is_finite());
459    ///
460    /// assert!(!nan.is_finite());
461    /// assert!(!inf.is_finite());
462    /// assert!(!neg_inf.is_finite());
463    /// ```
464    #[inline]
465    #[must_use]
466    pub const fn is_finite(self) -> bool {
467        self.0 & Self::EXP_MASK != Self::EXP_MASK
468    }
469
470    /// Returns `true` if the number is [subnormal].
471    ///
472    /// [subnormal]: https://en.wikipedia.org/wiki/Denormal_number
473    #[must_use]
474    #[inline(always)]
475    pub const fn is_subnormal(self) -> bool {
476        matches!(self.classify(), FpCategory::Subnormal)
477    }
478
479    /// Returns `true` if the number is neither zero, infinite, subnormal, or
480    /// NaN.
481    ///
482    /// # Examples
483    ///
484    /// ```rust
485    /// # use float16::*;
486    ///
487    /// let min = bf16::MIN_POSITIVE;
488    /// let max = bf16::MAX;
489    /// let lower_than_min = bf16::from_f32(1.0e-39_f32);
490    /// let zero = bf16::from_f32(0.0_f32);
491    ///
492    /// assert!(min.is_normal());
493    /// assert!(max.is_normal());
494    ///
495    /// assert!(!zero.is_normal());
496    /// assert!(!bf16::NAN.is_normal());
497    /// assert!(!bf16::INFINITY.is_normal());
498    /// // Values between 0 and `min` are subnormal.
499    /// assert!(!lower_than_min.is_normal());
500    /// ```
501    #[inline]
502    #[must_use]
503    pub const fn is_normal(self) -> bool {
504        let exp = self.0 & Self::EXP_MASK;
505        exp != Self::EXP_MASK && exp != 0
506    }
507
508    /// Returns the floating point category of the number.
509    ///
510    /// If only one property is going to be tested, it is generally faster to
511    /// use the specific predicate instead.
512    ///
513    /// # Examples
514    ///
515    /// ```rust
516    /// use std::num::FpCategory;
517    /// # use float16::*;
518    ///
519    /// let num = bf16::from_f32(12.4_f32);
520    /// let inf = bf16::INFINITY;
521    ///
522    /// assert_eq!(num.classify(), FpCategory::Normal);
523    /// assert_eq!(inf.classify(), FpCategory::Infinite);
524    /// ```
525    #[inline]
526    #[must_use]
527    pub const fn classify(self) -> FpCategory {
528        let exp = self.0 & Self::EXP_MASK;
529        let man = self.0 & Self::MAN_MASK;
530        match (exp, man) {
531            (0, 0) => FpCategory::Zero,
532            (0, _) => FpCategory::Subnormal,
533            (Self::EXP_MASK, 0) => FpCategory::Infinite,
534            (Self::EXP_MASK, _) => FpCategory::Nan,
535            _ => FpCategory::Normal,
536        }
537    }
538
539    /// Returns a number that represents the sign of `self`.
540    ///
541    /// * 1.0 if the number is positive, +0.0 or [`INFINITY`][bf16::INFINITY]
542    /// * −1.0 if the number is negative, −0.0` or
543    ///   [`NEG_INFINITY`][bf16::NEG_INFINITY]
544    /// * [`NAN`][bf16::NAN] if the number is NaN
545    ///
546    /// # Examples
547    ///
548    /// ```rust
549    /// # use float16::*;
550    ///
551    /// let f = bf16::from_f32(3.5_f32);
552    ///
553    /// assert_eq!(f.signum(), bf16::from_f32(1.0));
554    /// assert_eq!(bf16::NEG_INFINITY.signum(), bf16::from_f32(-1.0));
555    ///
556    /// assert!(bf16::NAN.signum().is_nan());
557    /// ```
558    #[inline]
559    #[must_use]
560    pub const fn signum(self) -> bf16 {
561        if self.is_nan() {
562            self
563        } else if self.0 & Self::SIGN_MASK != 0 {
564            Self::NEG_ONE
565        } else {
566            Self::ONE
567        }
568    }
569
570    /// Returns `true` if and only if `self` has a positive sign, including
571    /// +0.0, NaNs with a positive sign bit and +∞.
572    ///
573    /// # Examples
574    ///
575    /// ```rust
576    /// # use float16::*;
577    ///
578    /// let nan = bf16::NAN;
579    /// let f = bf16::from_f32(7.0_f32);
580    /// let g = bf16::from_f32(-7.0_f32);
581    ///
582    /// assert!(f.is_sign_positive());
583    /// assert!(!g.is_sign_positive());
584    /// // NaN can be either positive or negative
585    /// assert!(nan.is_sign_positive() != nan.is_sign_negative());
586    /// ```
587    #[inline]
588    #[must_use]
589    pub const fn is_sign_positive(self) -> bool {
590        self.0 & Self::SIGN_MASK == 0
591    }
592
593    /// Returns `true` if and only if `self` has a negative sign, including
594    /// −0.0, NaNs with a negative sign bit and −∞.
595    ///
596    /// # Examples
597    ///
598    /// ```rust
599    /// # use float16::*;
600    ///
601    /// let nan = bf16::NAN;
602    /// let f = bf16::from_f32(7.0f32);
603    /// let g = bf16::from_f32(-7.0f32);
604    ///
605    /// assert!(!f.is_sign_negative());
606    /// assert!(g.is_sign_negative());
607    /// // NaN can be either positive or negative
608    /// assert!(nan.is_sign_positive() != nan.is_sign_negative());
609    /// ```
610    #[inline]
611    #[must_use]
612    pub const fn is_sign_negative(self) -> bool {
613        self.0 & Self::SIGN_MASK != 0
614    }
615
616    /// Returns a number composed of the magnitude of `self` and the sign of
617    /// `sign`.
618    ///
619    /// Equal to `self` if the sign of `self` and `sign` are the same, otherwise
620    /// equal to `-self`. If `self` is NaN, then NaN with the sign of `sign`
621    /// is returned.
622    ///
623    /// # Examples
624    ///
625    /// ```
626    /// # use float16::*;
627    /// let f = bf16::from_f32(3.5);
628    ///
629    /// assert_eq!(f.copysign(bf16::from_f32(0.42)), bf16::from_f32(3.5));
630    /// assert_eq!(f.copysign(bf16::from_f32(-0.42)), bf16::from_f32(-3.5));
631    /// assert_eq!((-f).copysign(bf16::from_f32(0.42)), bf16::from_f32(3.5));
632    /// assert_eq!((-f).copysign(bf16::from_f32(-0.42)), bf16::from_f32(-3.5));
633    ///
634    /// assert!(bf16::NAN.copysign(bf16::from_f32(1.0)).is_nan());
635    /// ```
636    #[inline]
637    #[must_use]
638    pub const fn copysign(self, sign: bf16) -> bf16 {
639        bf16((sign.0 & Self::SIGN_MASK) | (self.0 & Self::NOT_SIGN))
640    }
641
642    /// Takes the reciprocal (inverse) of a number, `1/x`.
643    #[must_use]
644    #[inline(always)]
645    pub fn recip(self) -> Self {
646        Self::ONE / self
647    }
648
649    /// Converts radians to degrees.
650    #[must_use]
651    #[inline(always)]
652    pub fn to_degrees(self) -> Self {
653        self * Self::from(180u8) / Self::PI
654    }
655
656    /// Converts degrees to radians.
657    #[must_use]
658    #[inline(always)]
659    pub fn to_radians(self) -> Self {
660        self * Self::PI / Self::from(180u8)
661    }
662
663    /// Returns the maximum of the two numbers.
664    ///
665    /// If one of the arguments is NaN, then the other argument is returned.
666    ///
667    /// # Examples
668    ///
669    /// ```
670    /// # use float16::*;
671    /// let x = bf16::from_f32(1.0);
672    /// let y = bf16::from_f32(2.0);
673    ///
674    /// assert_eq!(x.max(y), y);
675    /// ```
676    #[inline]
677    #[must_use]
678    pub const fn max(self, other: bf16) -> bf16 {
679        if self.is_nan() || gt(other, self) {
680            other
681        } else {
682            self
683        }
684    }
685
686    /// Returns the minimum of the two numbers.
687    ///
688    /// If one of the arguments is NaN, then the other argument is returned.
689    ///
690    /// # Examples
691    ///
692    /// ```
693    /// # use float16::*;
694    /// let x = bf16::from_f32(1.0);
695    /// let y = bf16::from_f32(2.0);
696    ///
697    /// assert_eq!(x.min(y), x);
698    /// ```
699    #[inline]
700    #[must_use]
701    pub const fn min(self, other: bf16) -> bf16 {
702        if self.is_nan() || lt(other, self) {
703            other
704        } else {
705            self
706        }
707    }
708
709    /// Restrict a value to a certain interval unless it is NaN.
710    ///
711    /// Returns `max` if `self` is greater than `max`, and `min` if `self` is
712    /// less than `min`. Otherwise this returns `self`.
713    ///
714    /// Note that this function returns NaN if the initial value was NaN as
715    /// well.
716    ///
717    /// # Panics
718    /// Panics if `min > max`, `min` is NaN, or `max` is NaN.
719    ///
720    /// # Examples
721    ///
722    /// ```
723    /// # use float16::*;
724    /// assert!(bf16::from_f32(-3.0).clamp(bf16::from_f32(-2.0), bf16::from_f32(1.0)) == bf16::from_f32(-2.0));
725    /// assert!(bf16::from_f32(0.0).clamp(bf16::from_f32(-2.0), bf16::from_f32(1.0)) == bf16::from_f32(0.0));
726    /// assert!(bf16::from_f32(2.0).clamp(bf16::from_f32(-2.0), bf16::from_f32(1.0)) == bf16::from_f32(1.0));
727    /// assert!(bf16::NAN.clamp(bf16::from_f32(-2.0), bf16::from_f32(1.0)).is_nan());
728    /// ```
729    #[inline]
730    #[must_use]
731    pub const fn clamp(self, min: bf16, max: bf16) -> bf16 {
732        assert!(le(min, max));
733        let mut x = self;
734        if lt(x, min) {
735            x = min;
736        }
737        if gt(x, max) {
738            x = max;
739        }
740        x
741    }
742
743    /// Returns the ordering between `self` and `other`.
744    ///
745    /// Unlike the standard partial comparison between floating point numbers,
746    /// this comparison always produces an ordering in accordance to
747    /// the `totalOrder` predicate as defined in the IEEE 754 (2008 revision)
748    /// floating point standard. The values are ordered in the following
749    /// sequence:
750    ///
751    /// - negative quiet NaN
752    /// - negative signaling NaN
753    /// - negative infinity
754    /// - negative numbers
755    /// - negative subnormal numbers
756    /// - negative zero
757    /// - positive zero
758    /// - positive subnormal numbers
759    /// - positive numbers
760    /// - positive infinity
761    /// - positive signaling NaN
762    /// - positive quiet NaN.
763    ///
764    /// The ordering established by this function does not always agree with the
765    /// [`PartialOrd`] and [`PartialEq`] implementations of `bf16`. For example,
766    /// they consider negative and positive zero equal, while `total_cmp`
767    /// doesn't.
768    ///
769    /// The interpretation of the signaling NaN bit follows the definition in
770    /// the IEEE 754 standard, which may not match the interpretation by some of
771    /// the older, non-conformant (e.g. MIPS) hardware implementations.
772    ///
773    /// # Examples
774    /// ```
775    /// # use float16::bf16;
776    /// let mut v: Vec<bf16> = vec![];
777    /// v.push(bf16::ONE);
778    /// v.push(bf16::INFINITY);
779    /// v.push(bf16::NEG_INFINITY);
780    /// v.push(bf16::NAN);
781    /// v.push(bf16::MAX_SUBNORMAL);
782    /// v.push(-bf16::MAX_SUBNORMAL);
783    /// v.push(bf16::ZERO);
784    /// v.push(bf16::NEG_ZERO);
785    /// v.push(bf16::NEG_ONE);
786    /// v.push(bf16::MIN_POSITIVE);
787    ///
788    /// v.sort_by(|a, b| a.total_cmp(&b));
789    ///
790    /// assert!(v
791    ///     .into_iter()
792    ///     .zip(
793    ///         [
794    ///             bf16::NEG_INFINITY,
795    ///             bf16::NEG_ONE,
796    ///             -bf16::MAX_SUBNORMAL,
797    ///             bf16::NEG_ZERO,
798    ///             bf16::ZERO,
799    ///             bf16::MAX_SUBNORMAL,
800    ///             bf16::MIN_POSITIVE,
801    ///             bf16::ONE,
802    ///             bf16::INFINITY,
803    ///             bf16::NAN
804    ///         ]
805    ///         .iter()
806    ///     )
807    ///     .all(|(a, b)| a.to_bits() == b.to_bits()));
808    /// ```
809    // Implementation based on: https://doc.rust-lang.org/std/primitive.f32.html#method.total_cmp
810    #[inline]
811    #[must_use]
812    pub fn total_cmp(&self, other: &Self) -> Ordering {
813        let mut left = self.to_bits() as i16;
814        let mut right = other.to_bits() as i16;
815        left ^= (((left >> 15) as u16) >> 1) as i16;
816        right ^= (((right >> 15) as u16) >> 1) as i16;
817        left.cmp(&right)
818    }
819
820    /// Approximate number of [`struct@bf16`] significant digits in base 10
821    pub const DIGITS: u32 = 2;
822    /// [`struct@bf16`]
823    /// [machine epsilon](https://en.wikipedia.org/wiki/Machine_epsilon) value
824    ///
825    /// This is the difference between 1.0 and the next largest representable
826    /// number.
827    pub const EPSILON: bf16 = bf16(0x3C00u16);
828    /// [`struct@bf16`] positive Infinity (+∞)
829    pub const INFINITY: bf16 = bf16(0x7F80u16);
830    /// Number of [`struct@bf16`] significant digits in base 2
831    pub const MANTISSA_DIGITS: u32 = 8;
832    /// Largest finite [`struct@bf16`] value
833    pub const MAX: bf16 = bf16(0x7F7F);
834    /// Maximum possible [`struct@bf16`] power of 10 exponent
835    pub const MAX_10_EXP: i32 = 38;
836    /// Maximum possible [`struct@bf16`] power of 2 exponent
837    pub const MAX_EXP: i32 = 128;
838    /// Smallest finite [`struct@bf16`] value
839    pub const MIN: bf16 = bf16(0xFF7F);
840    /// Minimum possible normal [`struct@bf16`] power of 10 exponent
841    pub const MIN_10_EXP: i32 = -37;
842    /// One greater than the minimum possible normal [`struct@bf16`] power of 2
843    /// exponent
844    pub const MIN_EXP: i32 = -125;
845    /// Smallest positive normal [`struct@bf16`] value
846    pub const MIN_POSITIVE: bf16 = bf16(0x0080u16);
847    /// [`struct@bf16`] Not a Number (NaN)
848    pub const NAN: bf16 = bf16(0x7FC0u16);
849    /// [`struct@bf16`] negative infinity (-∞).
850    pub const NEG_INFINITY: bf16 = bf16(0xFF80u16);
851    /// The radix or base of the internal representation of [`struct@bf16`]
852    pub const RADIX: u32 = 2;
853
854    /// Minimum positive subnormal [`struct@bf16`] value
855    pub const MIN_POSITIVE_SUBNORMAL: bf16 = bf16(0x0001u16);
856    /// Maximum subnormal [`struct@bf16`] value
857    pub const MAX_SUBNORMAL: bf16 = bf16(0x007Fu16);
858
859    /// [`struct@bf16`] 1
860    pub const ONE: bf16 = bf16(0x3F80u16);
861    /// [`struct@bf16`] 0
862    pub const ZERO: bf16 = bf16(0x0000u16);
863    /// [`struct@bf16`] -0
864    pub const NEG_ZERO: bf16 = bf16(0x8000u16);
865    /// [`struct@bf16`] -1
866    pub const NEG_ONE: bf16 = bf16(0xBF80u16);
867
868    /// [`struct@bf16`] Euler's number (ℯ)
869    pub const E: bf16 = bf16(0x402Eu16);
870    /// [`struct@bf16`] Archimedes' constant (π)
871    pub const PI: bf16 = bf16(0x4049u16);
872    /// [`struct@bf16`] 1/π
873    pub const FRAC_1_PI: bf16 = bf16(0x3EA3u16);
874    /// [`struct@bf16`] 1/√2
875    pub const FRAC_1_SQRT_2: bf16 = bf16(0x3F35u16);
876    /// [`struct@bf16`] 2/π
877    pub const FRAC_2_PI: bf16 = bf16(0x3F23u16);
878    /// [`struct@bf16`] 2/√π
879    pub const FRAC_2_SQRT_PI: bf16 = bf16(0x3F90u16);
880    /// [`struct@bf16`] π/2
881    pub const FRAC_PI_2: bf16 = bf16(0x3FC9u16);
882    /// [`struct@bf16`] π/3
883    pub const FRAC_PI_3: bf16 = bf16(0x3F86u16);
884    /// [`struct@bf16`] π/4
885    pub const FRAC_PI_4: bf16 = bf16(0x3F49u16);
886    /// [`struct@bf16`] π/6
887    pub const FRAC_PI_6: bf16 = bf16(0x3F06u16);
888    /// [`struct@bf16`] π/8
889    pub const FRAC_PI_8: bf16 = bf16(0x3EC9u16);
890    /// [`struct@bf16`] 𝗅𝗇 10
891    pub const LN_10: bf16 = bf16(0x4013u16);
892    /// [`struct@bf16`] 𝗅𝗇 2
893    pub const LN_2: bf16 = bf16(0x3F31u16);
894    /// [`struct@bf16`] 𝗅𝗈𝗀₁₀ℯ
895    pub const LOG10_E: bf16 = bf16(0x3EDEu16);
896    /// [`struct@bf16`] 𝗅𝗈𝗀₁₀2
897    pub const LOG10_2: bf16 = bf16(0x3E9Au16);
898    /// [`struct@bf16`] 𝗅𝗈𝗀₂ℯ
899    pub const LOG2_E: bf16 = bf16(0x3FB9u16);
900    /// [`struct@bf16`] 𝗅𝗈𝗀₂10
901    pub const LOG2_10: bf16 = bf16(0x4055u16);
902    /// [`struct@bf16`] √2
903    pub const SQRT_2: bf16 = bf16(0x3FB5u16);
904
905    /// Sign bit
906    pub const SIGN_MASK: u16 = 0x8000;
907    // Private helper for comparisons.
908    const NOT_SIGN: u16 = !Self::SIGN_MASK;
909
910    /// Exponent mask
911    pub const EXP_MASK: u16 = 0x7F80;
912
913    /// Mask for the hidden bit.
914    pub const HIDDEN_BIT_MASK: u16 = 0x0080;
915
916    /// Mantissa mask
917    pub const MAN_MASK: u16 = 0x007F;
918
919    /// Minimum representable positive value (min subnormal)
920    pub const TINY_BITS: u16 = 0x1;
921
922    /// Minimum representable negative value (min negative subnormal)
923    pub const NEG_TINY_BITS: u16 = Self::TINY_BITS | Self::SIGN_MASK;
924}
925
926macro_rules! from_int_impl {
927    ($t:ty, $func:ident) => {
928        /// Create from the integral type, as if by an `as` cast.
929        #[inline(always)]
930        pub const fn $func(value: $t) -> Self {
931            Self::from_f32_const(value as f32)
932        }
933    };
934}
935
936impl bf16 {
937    from_int_impl!(u8, from_u8);
938    from_int_impl!(u16, from_u16);
939    from_int_impl!(u32, from_u32);
940    from_int_impl!(u64, from_u64);
941    from_int_impl!(u128, from_u128);
942    from_int_impl!(i8, from_i8);
943    from_int_impl!(i16, from_i16);
944    from_int_impl!(i32, from_i32);
945    from_int_impl!(i64, from_i64);
946    from_int_impl!(i128, from_i128);
947}
948
949impl From<bf16> for f32 {
950    #[inline]
951    fn from(x: bf16) -> f32 {
952        x.to_f32()
953    }
954}
955
956impl From<bf16> for f64 {
957    #[inline]
958    fn from(x: bf16) -> f64 {
959        x.to_f64()
960    }
961}
962
963impl From<i8> for bf16 {
964    #[inline]
965    fn from(x: i8) -> bf16 {
966        // Convert to f32, then to bf16
967        bf16::from_f32(f32::from(x))
968    }
969}
970
971impl From<u8> for bf16 {
972    #[inline]
973    fn from(x: u8) -> bf16 {
974        // Convert to f32, then to f16
975        bf16::from_f32(f32::from(x))
976    }
977}
978
979impl TryFrom<f32> for bf16 {
980    type Error = TryFromFloatError;
981
982    #[inline]
983    fn try_from(x: f32) -> Result<Self, Self::Error> {
984        Self::from_f32_lossless(x).ok_or(TryFromFloatError(()))
985    }
986}
987
988impl TryFrom<f64> for bf16 {
989    type Error = TryFromFloatError;
990
991    #[inline]
992    fn try_from(x: f64) -> Result<Self, Self::Error> {
993        Self::from_f64_lossless(x).ok_or(TryFromFloatError(()))
994    }
995}
996
997impl PartialEq for bf16 {
998    fn eq(&self, other: &bf16) -> bool {
999        eq(*self, *other)
1000    }
1001}
1002
1003impl PartialOrd for bf16 {
1004    fn partial_cmp(&self, other: &bf16) -> Option<Ordering> {
1005        if self.is_nan() || other.is_nan() {
1006            None
1007        } else {
1008            let neg = self.0 & Self::SIGN_MASK != 0;
1009            let other_neg = other.0 & Self::SIGN_MASK != 0;
1010            match (neg, other_neg) {
1011                (false, false) => Some(self.0.cmp(&other.0)),
1012                (false, true) => {
1013                    if (self.0 | other.0) & Self::NOT_SIGN == 0 {
1014                        Some(Ordering::Equal)
1015                    } else {
1016                        Some(Ordering::Greater)
1017                    }
1018                },
1019                (true, false) => {
1020                    if (self.0 | other.0) & Self::NOT_SIGN == 0 {
1021                        Some(Ordering::Equal)
1022                    } else {
1023                        Some(Ordering::Less)
1024                    }
1025                },
1026                (true, true) => Some(other.0.cmp(&self.0)),
1027            }
1028        }
1029    }
1030
1031    fn lt(&self, other: &bf16) -> bool {
1032        lt(*self, *other)
1033    }
1034
1035    fn le(&self, other: &bf16) -> bool {
1036        le(*self, *other)
1037    }
1038
1039    fn gt(&self, other: &bf16) -> bool {
1040        gt(*self, *other)
1041    }
1042
1043    fn ge(&self, other: &bf16) -> bool {
1044        ge(*self, *other)
1045    }
1046}
1047
1048#[cfg(not(target_arch = "spirv"))]
1049impl FromStr for bf16 {
1050    type Err = ParseFloatError;
1051
1052    #[inline]
1053    fn from_str(src: &str) -> Result<bf16, ParseFloatError> {
1054        f32::from_str(src).map(bf16::from_f32)
1055    }
1056}
1057
1058#[cfg(not(target_arch = "spirv"))]
1059impl Debug for bf16 {
1060    #[inline]
1061    fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
1062        Debug::fmt(&self.to_f32(), f)
1063    }
1064}
1065
1066#[cfg(not(target_arch = "spirv"))]
1067impl Display for bf16 {
1068    #[inline]
1069    fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
1070        Display::fmt(&self.to_f32(), f)
1071    }
1072}
1073
1074#[cfg(not(target_arch = "spirv"))]
1075impl LowerExp for bf16 {
1076    #[inline]
1077    fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
1078        write!(f, "{:e}", self.to_f32())
1079    }
1080}
1081
1082#[cfg(not(target_arch = "spirv"))]
1083impl UpperExp for bf16 {
1084    #[inline]
1085    fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
1086        write!(f, "{:E}", self.to_f32())
1087    }
1088}
1089
1090#[cfg(not(target_arch = "spirv"))]
1091impl Binary for bf16 {
1092    #[inline]
1093    fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
1094        write!(f, "{:b}", self.0)
1095    }
1096}
1097
1098#[cfg(not(target_arch = "spirv"))]
1099impl Octal for bf16 {
1100    #[inline]
1101    fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
1102        write!(f, "{:o}", self.0)
1103    }
1104}
1105
1106#[cfg(not(target_arch = "spirv"))]
1107impl LowerHex for bf16 {
1108    #[inline]
1109    fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
1110        write!(f, "{:x}", self.0)
1111    }
1112}
1113
1114#[cfg(not(target_arch = "spirv"))]
1115impl UpperHex for bf16 {
1116    #[inline]
1117    fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
1118        write!(f, "{:X}", self.0)
1119    }
1120}
1121
1122impl Neg for bf16 {
1123    type Output = Self;
1124
1125    #[inline]
1126    fn neg(self) -> Self::Output {
1127        Self(self.0 ^ Self::SIGN_MASK)
1128    }
1129}
1130
1131impl Neg for &bf16 {
1132    type Output = <bf16 as Neg>::Output;
1133
1134    #[inline]
1135    fn neg(self) -> Self::Output {
1136        Neg::neg(*self)
1137    }
1138}
1139
1140impl Add for bf16 {
1141    type Output = Self;
1142
1143    #[inline]
1144    fn add(self, rhs: Self) -> Self::Output {
1145        Self::from_f32(Self::to_f32(self) + Self::to_f32(rhs))
1146    }
1147}
1148
1149impl Add<&bf16> for bf16 {
1150    type Output = <bf16 as Add<bf16>>::Output;
1151
1152    #[inline]
1153    fn add(self, rhs: &bf16) -> Self::Output {
1154        self.add(*rhs)
1155    }
1156}
1157
1158impl Add<&bf16> for &bf16 {
1159    type Output = <bf16 as Add<bf16>>::Output;
1160
1161    #[inline]
1162    fn add(self, rhs: &bf16) -> Self::Output {
1163        (*self).add(*rhs)
1164    }
1165}
1166
1167impl Add<bf16> for &bf16 {
1168    type Output = <bf16 as Add<bf16>>::Output;
1169
1170    #[inline]
1171    fn add(self, rhs: bf16) -> Self::Output {
1172        (*self).add(rhs)
1173    }
1174}
1175
1176impl AddAssign for bf16 {
1177    #[inline]
1178    fn add_assign(&mut self, rhs: Self) {
1179        *self = (*self).add(rhs);
1180    }
1181}
1182
1183impl AddAssign<&bf16> for bf16 {
1184    #[inline]
1185    fn add_assign(&mut self, rhs: &bf16) {
1186        *self = (*self).add(rhs);
1187    }
1188}
1189
1190impl Sub for bf16 {
1191    type Output = Self;
1192
1193    #[inline]
1194    fn sub(self, rhs: Self) -> Self::Output {
1195        Self::from_f32(Self::to_f32(self) - Self::to_f32(rhs))
1196    }
1197}
1198
1199impl Sub<&bf16> for bf16 {
1200    type Output = <bf16 as Sub<bf16>>::Output;
1201
1202    #[inline]
1203    fn sub(self, rhs: &bf16) -> Self::Output {
1204        self.sub(*rhs)
1205    }
1206}
1207
1208impl Sub<&bf16> for &bf16 {
1209    type Output = <bf16 as Sub<bf16>>::Output;
1210
1211    #[inline]
1212    fn sub(self, rhs: &bf16) -> Self::Output {
1213        (*self).sub(*rhs)
1214    }
1215}
1216
1217impl Sub<bf16> for &bf16 {
1218    type Output = <bf16 as Sub<bf16>>::Output;
1219
1220    #[inline]
1221    fn sub(self, rhs: bf16) -> Self::Output {
1222        (*self).sub(rhs)
1223    }
1224}
1225
1226impl SubAssign for bf16 {
1227    #[inline]
1228    fn sub_assign(&mut self, rhs: Self) {
1229        *self = (*self).sub(rhs);
1230    }
1231}
1232
1233impl SubAssign<&bf16> for bf16 {
1234    #[inline]
1235    fn sub_assign(&mut self, rhs: &bf16) {
1236        *self = (*self).sub(rhs);
1237    }
1238}
1239
1240impl Mul for bf16 {
1241    type Output = Self;
1242
1243    #[inline]
1244    fn mul(self, rhs: Self) -> Self::Output {
1245        Self::from_f32(Self::to_f32(self) * Self::to_f32(rhs))
1246    }
1247}
1248
1249impl Mul<&bf16> for bf16 {
1250    type Output = <bf16 as Mul<bf16>>::Output;
1251
1252    #[inline]
1253    fn mul(self, rhs: &bf16) -> Self::Output {
1254        self.mul(*rhs)
1255    }
1256}
1257
1258impl Mul<&bf16> for &bf16 {
1259    type Output = <bf16 as Mul<bf16>>::Output;
1260
1261    #[inline]
1262    fn mul(self, rhs: &bf16) -> Self::Output {
1263        (*self).mul(*rhs)
1264    }
1265}
1266
1267impl Mul<bf16> for &bf16 {
1268    type Output = <bf16 as Mul<bf16>>::Output;
1269
1270    #[inline]
1271    fn mul(self, rhs: bf16) -> Self::Output {
1272        (*self).mul(rhs)
1273    }
1274}
1275
1276impl MulAssign for bf16 {
1277    #[inline]
1278    fn mul_assign(&mut self, rhs: Self) {
1279        *self = (*self).mul(rhs);
1280    }
1281}
1282
1283impl MulAssign<&bf16> for bf16 {
1284    #[inline]
1285    fn mul_assign(&mut self, rhs: &bf16) {
1286        *self = (*self).mul(rhs);
1287    }
1288}
1289
1290impl Div for bf16 {
1291    type Output = Self;
1292
1293    #[inline]
1294    fn div(self, rhs: Self) -> Self::Output {
1295        Self::from_f32(Self::to_f32(self) / Self::to_f32(rhs))
1296    }
1297}
1298
1299impl Div<&bf16> for bf16 {
1300    type Output = <bf16 as Div<bf16>>::Output;
1301
1302    #[inline]
1303    fn div(self, rhs: &bf16) -> Self::Output {
1304        self.div(*rhs)
1305    }
1306}
1307
1308impl Div<&bf16> for &bf16 {
1309    type Output = <bf16 as Div<bf16>>::Output;
1310
1311    #[inline]
1312    fn div(self, rhs: &bf16) -> Self::Output {
1313        (*self).div(*rhs)
1314    }
1315}
1316
1317impl Div<bf16> for &bf16 {
1318    type Output = <bf16 as Div<bf16>>::Output;
1319
1320    #[inline]
1321    fn div(self, rhs: bf16) -> Self::Output {
1322        (*self).div(rhs)
1323    }
1324}
1325
1326impl DivAssign for bf16 {
1327    #[inline]
1328    fn div_assign(&mut self, rhs: Self) {
1329        *self = (*self).div(rhs);
1330    }
1331}
1332
1333impl DivAssign<&bf16> for bf16 {
1334    #[inline]
1335    fn div_assign(&mut self, rhs: &bf16) {
1336        *self = (*self).div(rhs);
1337    }
1338}
1339
1340impl Rem for bf16 {
1341    type Output = Self;
1342
1343    fn rem(self, rhs: Self) -> Self::Output {
1344        Self::from_f32(Self::to_f32(self) % Self::to_f32(rhs))
1345    }
1346}
1347
1348impl Rem<&bf16> for bf16 {
1349    type Output = <bf16 as Rem<bf16>>::Output;
1350
1351    #[inline]
1352    fn rem(self, rhs: &bf16) -> Self::Output {
1353        self.rem(*rhs)
1354    }
1355}
1356
1357impl Rem<&bf16> for &bf16 {
1358    type Output = <bf16 as Rem<bf16>>::Output;
1359
1360    #[inline]
1361    fn rem(self, rhs: &bf16) -> Self::Output {
1362        (*self).rem(*rhs)
1363    }
1364}
1365
1366impl Rem<bf16> for &bf16 {
1367    type Output = <bf16 as Rem<bf16>>::Output;
1368
1369    #[inline]
1370    fn rem(self, rhs: bf16) -> Self::Output {
1371        (*self).rem(rhs)
1372    }
1373}
1374
1375impl RemAssign for bf16 {
1376    #[inline]
1377    fn rem_assign(&mut self, rhs: Self) {
1378        *self = (*self).rem(rhs);
1379    }
1380}
1381
1382impl RemAssign<&bf16> for bf16 {
1383    #[inline]
1384    fn rem_assign(&mut self, rhs: &bf16) {
1385        *self = (*self).rem(rhs);
1386    }
1387}
1388
1389impl Product for bf16 {
1390    #[inline]
1391    fn product<I: Iterator<Item = Self>>(iter: I) -> Self {
1392        bf16::from_f32(iter.map(|f| f.to_f32()).product())
1393    }
1394}
1395
1396impl<'a> Product<&'a bf16> for bf16 {
1397    #[inline]
1398    fn product<I: Iterator<Item = &'a bf16>>(iter: I) -> Self {
1399        bf16::from_f32(iter.map(|f| f.to_f32()).product())
1400    }
1401}
1402
1403impl Sum for bf16 {
1404    #[inline]
1405    fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
1406        bf16::from_f32(iter.map(|f| f.to_f32()).sum())
1407    }
1408}
1409
1410impl<'a> Sum<&'a bf16> for bf16 {
1411    #[inline]
1412    fn sum<I: Iterator<Item = &'a bf16>>(iter: I) -> Self {
1413        bf16::from_f32(iter.map(|f| f.to_f32()).sum())
1414    }
1415}
1416
1417#[inline]
1418const fn eq(lhs: bf16, rhs: bf16) -> bool {
1419    if lhs.is_nan() || rhs.is_nan() {
1420        false
1421    } else {
1422        (lhs.0 == rhs.0) || ((lhs.0 | rhs.0) & bf16::NOT_SIGN == 0)
1423    }
1424}
1425
1426#[inline]
1427const fn lt(lhs: bf16, rhs: bf16) -> bool {
1428    if lhs.is_nan() || rhs.is_nan() {
1429        false
1430    } else {
1431        let neg = lhs.0 & bf16::SIGN_MASK != 0;
1432        let rhs_neg = rhs.0 & bf16::SIGN_MASK != 0;
1433        match (neg, rhs_neg) {
1434            (false, false) => lhs.0 < rhs.0,
1435            (false, true) => false,
1436            (true, false) => (lhs.0 | rhs.0) & bf16::NOT_SIGN != 0,
1437            (true, true) => lhs.0 > rhs.0,
1438        }
1439    }
1440}
1441
1442#[inline]
1443const fn le(lhs: bf16, rhs: bf16) -> bool {
1444    if lhs.is_nan() || rhs.is_nan() {
1445        false
1446    } else {
1447        let neg = lhs.0 & bf16::SIGN_MASK != 0;
1448        let rhs_neg = rhs.0 & bf16::SIGN_MASK != 0;
1449        match (neg, rhs_neg) {
1450            (false, false) => lhs.0 <= rhs.0,
1451            (false, true) => (lhs.0 | rhs.0) & bf16::NOT_SIGN == 0,
1452            (true, false) => true,
1453            (true, true) => lhs.0 >= rhs.0,
1454        }
1455    }
1456}
1457
1458#[inline]
1459const fn gt(lhs: bf16, rhs: bf16) -> bool {
1460    if lhs.is_nan() || rhs.is_nan() {
1461        false
1462    } else {
1463        let neg = lhs.0 & bf16::SIGN_MASK != 0;
1464        let rhs_neg = rhs.0 & bf16::SIGN_MASK != 0;
1465        match (neg, rhs_neg) {
1466            (false, false) => lhs.0 > rhs.0,
1467            (false, true) => (lhs.0 | rhs.0) & bf16::NOT_SIGN != 0,
1468            (true, false) => false,
1469            (true, true) => lhs.0 < rhs.0,
1470        }
1471    }
1472}
1473
1474#[inline]
1475const fn ge(lhs: bf16, rhs: bf16) -> bool {
1476    if lhs.is_nan() || rhs.is_nan() {
1477        false
1478    } else {
1479        let neg = lhs.0 & bf16::SIGN_MASK != 0;
1480        let rhs_neg = rhs.0 & bf16::SIGN_MASK != 0;
1481        match (neg, rhs_neg) {
1482            (false, false) => lhs.0 >= rhs.0,
1483            (false, true) => true,
1484            (true, false) => (lhs.0 | rhs.0) & bf16::NOT_SIGN == 0,
1485            (true, true) => lhs.0 <= rhs.0,
1486        }
1487    }
1488}
1489
1490#[allow(clippy::cognitive_complexity, clippy::float_cmp, clippy::neg_cmp_op_on_partial_ord)]
1491#[cfg(test)]
1492mod test {
1493    use core::cmp::Ordering;
1494
1495    use super::*;
1496
1497    #[test]
1498    fn test_bf16_consts_from_f32() {
1499        let  class="number">1.0);
1500        let zero = bf16::from_f32(0.0);
1501        let neg_zero = bf16::from_f32(-0.0);
1502        let neg_one = bf16::from_f32(-1.0);
1503        let inf = bf16::from_f32(core::f32::INFINITY);
1504        let neg_inf = bf16::from_f32(core::f32::NEG_INFINITY);
1505        let nan = bf16::from_f32(core::f32::NAN);
1506
1507        assert_eq!(bf16::ONE, one);
1508        assert_eq!(bf16::ZERO, zero);
1509        assert!(zero.is_sign_positive());
1510        assert_eq!(bf16::NEG_ZERO, neg_zero);
1511        assert!(neg_zero.is_sign_negative());
1512        assert_eq!(bf16::NEG_ONE, neg_one);
1513        assert!(neg_one.is_sign_negative());
1514        assert_eq!(bf16::INFINITY, inf);
1515        assert_eq!(bf16::NEG_INFINITY, neg_inf);
1516        assert!(nan.is_nan());
1517        assert!(bf16::NAN.is_nan());
1518
1519        let e = bf16::from_f32(core::f32::consts::E);
1520        let pi = bf16::from_f32(core::f32::consts::PI);
1521        let frac_1_pi = bf16::from_f32(core::f32::consts::FRAC_1_PI);
1522        let frac_1_sqrt_2 = bf16::from_f32(core::f32::consts::FRAC_1_SQRT_2);
1523        let frac_2_pi = bf16::from_f32(core::f32::consts::FRAC_2_PI);
1524        let frac_2_sqrt_pi = bf16::from_f32(core::f32::consts::FRAC_2_SQRT_PI);
1525        let frac_pi_2 = bf16::from_f32(core::f32::consts::FRAC_PI_2);
1526        let frac_pi_3 = bf16::from_f32(core::f32::consts::FRAC_PI_3);
1527        let frac_pi_4 = bf16::from_f32(core::f32::consts::FRAC_PI_4);
1528        let frac_pi_6 = bf16::from_f32(core::f32::consts::FRAC_PI_6);
1529        let frac_pi_8 = bf16::from_f32(core::f32::consts::FRAC_PI_8);
1530        let ln_10 = bf16::from_f32(core::f32::consts::LN_10);
1531        let ln_2 = bf16::from_f32(core::f32::consts::LN_2);
1532        let log10_e = bf16::from_f32(core::f32::consts::LOG10_E);
1533        // core::f32::consts::LOG10_2 requires rustc 1.43.0
1534        let log10_2 = bf16::from_f32(2f32.log10());
1535        let log2_e = bf16::from_f32(core::f32::consts::LOG2_E);
1536        // core::f32::consts::LOG2_10 requires rustc 1.43.0
1537        let log2_10 = bf16::from_f32(10f32.log2());
1538        let sqrt_2 = bf16::from_f32(core::f32::consts::SQRT_2);
1539
1540        assert_eq!(bf16::E, e);
1541        assert_eq!(bf16::PI, pi);
1542        assert_eq!(bf16::FRAC_1_PI, frac_1_pi);
1543        assert_eq!(bf16::FRAC_1_SQRT_2, frac_1_sqrt_2);
1544        assert_eq!(bf16::FRAC_2_PI, frac_2_pi);
1545        assert_eq!(bf16::FRAC_2_SQRT_PI, frac_2_sqrt_pi);
1546        assert_eq!(bf16::FRAC_PI_2, frac_pi_2);
1547        assert_eq!(bf16::FRAC_PI_3, frac_pi_3);
1548        assert_eq!(bf16::FRAC_PI_4, frac_pi_4);
1549        assert_eq!(bf16::FRAC_PI_6, frac_pi_6);
1550        assert_eq!(bf16::FRAC_PI_8, frac_pi_8);
1551        assert_eq!(bf16::LN_10, ln_10);
1552        assert_eq!(bf16::LN_2, ln_2);
1553        assert_eq!(bf16::LOG10_E, log10_e);
1554        assert_eq!(bf16::LOG10_2, log10_2);
1555        assert_eq!(bf16::LOG2_E, log2_e);
1556        assert_eq!(bf16::LOG2_10, log2_10);
1557        assert_eq!(bf16::SQRT_2, sqrt_2);
1558    }
1559
1560    #[test]
1561    fn test_bf16_consts_from_f64() {
1562        let  class="number">1.0);
1563        let zero = bf16::from_f64(0.0);
1564        let neg_zero = bf16::from_f64(-0.0);
1565        let inf = bf16::from_f64(core::f64::INFINITY);
1566        let neg_inf = bf16::from_f64(core::f64::NEG_INFINITY);
1567        let nan = bf16::from_f64(core::f64::NAN);
1568
1569        assert_eq!(bf16::ONE, one);
1570        assert_eq!(bf16::ZERO, zero);
1571        assert_eq!(bf16::NEG_ZERO, neg_zero);
1572        assert_eq!(bf16::INFINITY, inf);
1573        assert_eq!(bf16::NEG_INFINITY, neg_inf);
1574        assert!(nan.is_nan());
1575        assert!(bf16::NAN.is_nan());
1576
1577        let e = bf16::from_f64(core::f64::consts::E);
1578        let pi = bf16::from_f64(core::f64::consts::PI);
1579        let frac_1_pi = bf16::from_f64(core::f64::consts::FRAC_1_PI);
1580        let frac_1_sqrt_2 = bf16::from_f64(core::f64::consts::FRAC_1_SQRT_2);
1581        let frac_2_pi = bf16::from_f64(core::f64::consts::FRAC_2_PI);
1582        let frac_2_sqrt_pi = bf16::from_f64(core::f64::consts::FRAC_2_SQRT_PI);
1583        let frac_pi_2 = bf16::from_f64(core::f64::consts::FRAC_PI_2);
1584        let frac_pi_3 = bf16::from_f64(core::f64::consts::FRAC_PI_3);
1585        let frac_pi_4 = bf16::from_f64(core::f64::consts::FRAC_PI_4);
1586        let frac_pi_6 = bf16::from_f64(core::f64::consts::FRAC_PI_6);
1587        let frac_pi_8 = bf16::from_f64(core::f64::consts::FRAC_PI_8);
1588        let ln_10 = bf16::from_f64(core::f64::consts::LN_10);
1589        let ln_2 = bf16::from_f64(core::f64::consts::LN_2);
1590        let log10_e = bf16::from_f64(core::f64::consts::LOG10_E);
1591        // core::f64::consts::LOG10_2 requires rustc 1.43.0
1592        let log10_2 = bf16::from_f64(2f64.log10());
1593        let log2_e = bf16::from_f64(core::f64::consts::LOG2_E);
1594        // core::f64::consts::LOG2_10 requires rustc 1.43.0
1595        let log2_10 = bf16::from_f64(10f64.log2());
1596        let sqrt_2 = bf16::from_f64(core::f64::consts::SQRT_2);
1597
1598        assert_eq!(bf16::E, e);
1599        assert_eq!(bf16::PI, pi);
1600        assert_eq!(bf16::FRAC_1_PI, frac_1_pi);
1601        assert_eq!(bf16::FRAC_1_SQRT_2, frac_1_sqrt_2);
1602        assert_eq!(bf16::FRAC_2_PI, frac_2_pi);
1603        assert_eq!(bf16::FRAC_2_SQRT_PI, frac_2_sqrt_pi);
1604        assert_eq!(bf16::FRAC_PI_2, frac_pi_2);
1605        assert_eq!(bf16::FRAC_PI_3, frac_pi_3);
1606        assert_eq!(bf16::FRAC_PI_4, frac_pi_4);
1607        assert_eq!(bf16::FRAC_PI_6, frac_pi_6);
1608        assert_eq!(bf16::FRAC_PI_8, frac_pi_8);
1609        assert_eq!(bf16::LN_10, ln_10);
1610        assert_eq!(bf16::LN_2, ln_2);
1611        assert_eq!(bf16::LOG10_E, log10_e);
1612        assert_eq!(bf16::LOG10_2, log10_2);
1613        assert_eq!(bf16::LOG2_E, log2_e);
1614        assert_eq!(bf16::LOG2_10, log2_10);
1615        assert_eq!(bf16::SQRT_2, sqrt_2);
1616    }
1617
1618    #[test]
1619    fn test_nan_conversion_to_smaller() {
1620        let nan64 = f64::from_bits(0x7FF0_0000_0000_0001u64);
1621        let neg_nan64 = f64::from_bits(0xFFF0_0000_0000_0001u64);
1622        let nan32 = f32::from_bits(0x7F80_0001u32);
1623        let neg_nan32 = f32::from_bits(0xFF80_0001u32);
1624        let nan32_from_64 = nan64 as f32;
1625        let neg_nan32_from_64 = neg_nan64 as f32;
1626        let nan16_from_64 = bf16::from_f64(nan64);
1627        let neg_nan16_from_64 = bf16::from_f64(neg_nan64);
1628        let nan16_from_32 = bf16::from_f32(nan32);
1629        let neg_nan16_from_32 = bf16::from_f32(neg_nan32);
1630
1631        assert!(nan64.is_nan() && nan64.is_sign_positive());
1632        assert!(neg_nan64.is_nan() && neg_nan64.is_sign_negative());
1633        assert!(nan32.is_nan() && nan32.is_sign_positive());
1634        assert!(neg_nan32.is_nan() && neg_nan32.is_sign_negative());
1635
1636        // f32/f64 NaN conversion sign is non-deterministic: https://github.com/VoidStarKat/half-rs/issues/103
1637        assert!(neg_nan32_from_64.is_nan());
1638        assert!(nan32_from_64.is_nan());
1639        assert!(nan16_from_64.is_nan());
1640        assert!(neg_nan16_from_64.is_nan());
1641        assert!(nan16_from_32.is_nan());
1642        assert!(neg_nan16_from_32.is_nan());
1643    }
1644
1645    #[test]
1646    fn test_nan_conversion_to_larger() {
1647        let nan16 = bf16::from_bits(0x7F81u16);
1648        let neg_nan16 = bf16::from_bits(0xFF81u16);
1649        let nan32 = f32::from_bits(0x7F80_0001u32);
1650        let neg_nan32 = f32::from_bits(0xFF80_0001u32);
1651        let nan32_from_16 = f32::from(nan16);
1652        let neg_nan32_from_16 = f32::from(neg_nan16);
1653        let nan64_from_16 = f64::from(nan16);
1654        let neg_nan64_from_16 = f64::from(neg_nan16);
1655        let nan64_from_32 = f64::from(nan32);
1656        let neg_nan64_from_32 = f64::from(neg_nan32);
1657
1658        assert!(nan16.is_nan() && nan16.is_sign_positive());
1659        assert!(neg_nan16.is_nan() && neg_nan16.is_sign_negative());
1660        assert!(nan32.is_nan() && nan32.is_sign_positive());
1661        assert!(neg_nan32.is_nan() && neg_nan32.is_sign_negative());
1662
1663        // // f32/f64 NaN conversion sign is non-deterministic: https://github.com/VoidStarKat/half-rs/issues/103
1664        assert!(nan32_from_16.is_nan());
1665        assert!(neg_nan32_from_16.is_nan());
1666        assert!(nan64_from_16.is_nan());
1667        assert!(neg_nan64_from_16.is_nan());
1668        assert!(nan64_from_32.is_nan());
1669        assert!(neg_nan64_from_32.is_nan());
1670    }
1671
1672    #[test]
1673    fn test_bf16_to_f32() {
1674        let f = bf16::from_f32(7.0);
1675        assert_eq!(f.to_f32(), 7.0f32);
1676
1677        // 7.1 is NOT exactly representable in 16-bit, it's rounded
1678        let f = bf16::from_f32(7.1);
1679        let diff = (f.to_f32() - 7.1f32).abs();
1680        // diff must be <= 4 * EPSILON, as 7 has two more significant bits than 1
1681        assert!(diff <= 4.0 * bf16::EPSILON.to_f32());
1682
1683        let tiny32 = f32::from_bits(0x0001_0000u32);
1684        assert_eq!(bf16::from_bits(0x0001).to_f32(), tiny32);
1685        assert_eq!(bf16::from_bits(0x0005).to_f32(), 5.0 * tiny32);
1686
1687        assert_eq!(bf16::from_bits(0x0001), bf16::from_f32(tiny32));
1688        assert_eq!(bf16::from_bits(0x0005), bf16::from_f32(5.0 * tiny32));
1689    }
1690
1691    #[test]
1692    #[cfg_attr(miri, ignore)]
1693    fn test_bf16_to_f64() {
1694        let f = bf16::from_f64(7.0);
1695        assert_eq!(f.to_f64(), 7.0f64);
1696
1697        // 7.1 is NOT exactly representable in 16-bit, it's rounded
1698        let f = bf16::from_f64(7.1);
1699        let diff = (f.to_f64() - 7.1f64).abs();
1700        // diff must be <= 4 * EPSILON, as 7 has two more significant bits than 1
1701        assert!(diff <= 4.0 * bf16::EPSILON.to_f64());
1702
1703        let tiny64 = 2.0f64.powi(-133);
1704        assert_eq!(bf16::from_bits(0x0001).to_f64(), tiny64);
1705        assert_eq!(bf16::from_bits(0x0005).to_f64(), 5.0 * tiny64);
1706
1707        assert_eq!(bf16::from_bits(0x0001), bf16::from_f64(tiny64));
1708        assert_eq!(bf16::from_bits(0x0005), bf16::from_f64(5.0 * tiny64));
1709    }
1710
1711    #[test]
1712    fn test_comparisons() {
1713        let zero = bf16::from_f64(0.0);
1714        let  class="number">1.0);
1715        let neg_zero = bf16::from_f64(-0.0);
1716        let neg_one = bf16::from_f64(-1.0);
1717
1718        assert_eq!(zero.partial_cmp(&neg_zero), Some(Ordering::Equal));
1719        assert_eq!(neg_zero.partial_cmp(&zero), Some(Ordering::Equal));
1720        assert!(zero == neg_zero);
1721        assert!(neg_zero == zero);
1722        assert!(!(zero != neg_zero));
1723        assert!(!(neg_zero != zero));
1724        assert!(!(zero < neg_zero));
1725        assert!(!(neg_zero < zero));
1726        assert!(zero <= neg_zero);
1727        assert!(neg_zero <= zero);
1728        assert!(!(zero > neg_zero));
1729        assert!(!(neg_zero > zero));
1730        assert!(zero >= neg_zero);
1731        assert!(neg_zero >= zero);
1732
1733        assert_eq!(one.partial_cmp(&neg_zero), Some(Ordering::Greater));
1734        assert_eq!(neg_zero.partial_cmp(&one), Some(Ordering::Less));
1735        assert!(!( neg_zero));
1736        assert!(!(neg_zero == one));
1737        assert!(one != neg_zero);
1738        assert!(neg_zero != one);
1739        assert!(!(one < neg_zero));
1740        assert!(neg_zero < one);
1741        assert!(!(one <= neg_zero));
1742        assert!(neg_zero <= one);
1743        assert!(one > neg_zero);
1744        assert!(!(neg_zero > one));
1745        assert!(one >= neg_zero);
1746        assert!(!(neg_zero >= one));
1747
1748        assert_eq!(one.partial_cmp(&neg_one), Some(Ordering::Greater));
1749        assert_eq!(neg_one.partial_cmp(&one), Some(Ordering::Less));
1750        assert!(!( neg_one));
1751        assert!(!(neg_one == one));
1752        assert!(one != neg_one);
1753        assert!(neg_one != one);
1754        assert!(!(one < neg_one));
1755        assert!(neg_one < one);
1756        assert!(!(one <= neg_one));
1757        assert!(neg_one <= one);
1758        assert!(one > neg_one);
1759        assert!(!(neg_one > one));
1760        assert!(one >= neg_one);
1761        assert!(!(neg_one >= one));
1762    }
1763
1764    #[test]
1765    #[allow(clippy::erasing_op, clippy::identity_op)]
1766    #[cfg_attr(miri, ignore)]
1767    fn round_to_even_f32() {
1768        // smallest positive subnormal = 0b0.0000_001 * 2^-126 = 2^-133
1769        let min_sub = bf16::from_bits(1);
1770        let min_sub_f = (-133f32).exp2();
1771        assert_eq!(bf16::from_f32(min_sub_f).to_bits(), min_sub.to_bits());
1772        assert_eq!(f32::from(min_sub).to_bits(), min_sub_f.to_bits());
1773
1774        // 0.0000000_011111 rounded to 0.0000000 (< tie, no rounding)
1775        // 0.0000000_100000 rounded to 0.0000000 (tie and even, remains at even)
1776        // 0.0000000_100001 rounded to 0.0000001 (> tie, rounds up)
1777        assert_eq!(bf16::from_f32(min_sub_f * 0.49).to_bits(), min_sub.to_bits() * 0);
1778        assert_eq!(bf16::from_f32(min_sub_f * 0.50).to_bits(), min_sub.to_bits() * 0);
1779        assert_eq!(bf16::from_f32(min_sub_f * 0.51).to_bits(), min_sub.to_bits() * 1);
1780
1781        // 0.0000001_011111 rounded to 0.0000001 (< tie, no rounding)
1782        // 0.0000001_100000 rounded to 0.0000010 (tie and odd, rounds up to even)
1783        // 0.0000001_100001 rounded to 0.0000010 (> tie, rounds up)
1784        assert_eq!(bf16::from_f32(min_sub_f * 1.49).to_bits(), min_sub.to_bits() * 1);
1785        assert_eq!(bf16::from_f32(min_sub_f * 1.50).to_bits(), min_sub.to_bits() * 2);
1786        assert_eq!(bf16::from_f32(min_sub_f * 1.51).to_bits(), min_sub.to_bits() * 2);
1787
1788        // 0.0000010_011111 rounded to 0.0000010 (< tie, no rounding)
1789        // 0.0000010_100000 rounded to 0.0000010 (tie and even, remains at even)
1790        // 0.0000010_100001 rounded to 0.0000011 (> tie, rounds up)
1791        assert_eq!(bf16::from_f32(min_sub_f * 2.49).to_bits(), min_sub.to_bits() * 2);
1792        assert_eq!(bf16::from_f32(min_sub_f * 2.50).to_bits(), min_sub.to_bits() * 2);
1793        assert_eq!(bf16::from_f32(min_sub_f * 2.51).to_bits(), min_sub.to_bits() * 3);
1794
1795        assert_eq!(bf16::from_f32(250.49f32).to_bits(), bf16::from_f32(250.0).to_bits());
1796        assert_eq!(bf16::from_f32(250.50f32).to_bits(), bf16::from_f32(250.0).to_bits());
1797        assert_eq!(bf16::from_f32(250.51f32).to_bits(), bf16::from_f32(251.0).to_bits());
1798        assert_eq!(bf16::from_f32(251.49f32).to_bits(), bf16::from_f32(251.0).to_bits());
1799        assert_eq!(bf16::from_f32(251.50f32).to_bits(), bf16::from_f32(252.0).to_bits());
1800        assert_eq!(bf16::from_f32(251.51f32).to_bits(), bf16::from_f32(252.0).to_bits());
1801        assert_eq!(bf16::from_f32(252.49f32).to_bits(), bf16::from_f32(252.0).to_bits());
1802        assert_eq!(bf16::from_f32(252.50f32).to_bits(), bf16::from_f32(252.0).to_bits());
1803        assert_eq!(bf16::from_f32(252.51f32).to_bits(), bf16::from_f32(253.0).to_bits());
1804    }
1805
1806    #[test]
1807    #[allow(clippy::erasing_op, clippy::identity_op)]
1808    #[cfg_attr(miri, ignore)]
1809    fn round_to_even_f64() {
1810        // smallest positive subnormal = 0b0.0000_001 * 2^-126 = 2^-133
1811        let min_sub = bf16::from_bits(1);
1812        let min_sub_f = (-133f64).exp2();
1813        assert_eq!(bf16::from_f64(min_sub_f).to_bits(), min_sub.to_bits());
1814        assert_eq!(f64::from(min_sub).to_bits(), min_sub_f.to_bits());
1815
1816        // 0.0000000_011111 rounded to 0.0000000 (< tie, no rounding)
1817        // 0.0000000_100000 rounded to 0.0000000 (tie and even, remains at even)
1818        // 0.0000000_100001 rounded to 0.0000001 (> tie, rounds up)
1819        assert_eq!(bf16::from_f64(min_sub_f * 0.49).to_bits(), min_sub.to_bits() * 0);
1820        assert_eq!(bf16::from_f64(min_sub_f * 0.50).to_bits(), min_sub.to_bits() * 0);
1821        assert_eq!(bf16::from_f64(min_sub_f * 0.51).to_bits(), min_sub.to_bits() * 1);
1822
1823        // 0.0000001_011111 rounded to 0.0000001 (< tie, no rounding)
1824        // 0.0000001_100000 rounded to 0.0000010 (tie and odd, rounds up to even)
1825        // 0.0000001_100001 rounded to 0.0000010 (> tie, rounds up)
1826        assert_eq!(bf16::from_f64(min_sub_f * 1.49).to_bits(), min_sub.to_bits() * 1);
1827        assert_eq!(bf16::from_f64(min_sub_f * 1.50).to_bits(), min_sub.to_bits() * 2);
1828        assert_eq!(bf16::from_f64(min_sub_f * 1.51).to_bits(), min_sub.to_bits() * 2);
1829
1830        // 0.0000010_011111 rounded to 0.0000010 (< tie, no rounding)
1831        // 0.0000010_100000 rounded to 0.0000010 (tie and even, remains at even)
1832        // 0.0000010_100001 rounded to 0.0000011 (> tie, rounds up)
1833        assert_eq!(bf16::from_f64(min_sub_f * 2.49).to_bits(), min_sub.to_bits() * 2);
1834        assert_eq!(bf16::from_f64(min_sub_f * 2.50).to_bits(), min_sub.to_bits() * 2);
1835        assert_eq!(bf16::from_f64(min_sub_f * 2.51).to_bits(), min_sub.to_bits() * 3);
1836
1837        assert_eq!(bf16::from_f64(250.49f64).to_bits(), bf16::from_f64(250.0).to_bits());
1838        assert_eq!(bf16::from_f64(250.50f64).to_bits(), bf16::from_f64(250.0).to_bits());
1839        assert_eq!(bf16::from_f64(250.51f64).to_bits(), bf16::from_f64(251.0).to_bits());
1840        assert_eq!(bf16::from_f64(251.49f64).to_bits(), bf16::from_f64(251.0).to_bits());
1841        assert_eq!(bf16::from_f64(251.50f64).to_bits(), bf16::from_f64(252.0).to_bits());
1842        assert_eq!(bf16::from_f64(251.51f64).to_bits(), bf16::from_f64(252.0).to_bits());
1843        assert_eq!(bf16::from_f64(252.49f64).to_bits(), bf16::from_f64(252.0).to_bits());
1844        assert_eq!(bf16::from_f64(252.50f64).to_bits(), bf16::from_f64(252.0).to_bits());
1845        assert_eq!(bf16::from_f64(252.51f64).to_bits(), bf16::from_f64(253.0).to_bits());
1846    }
1847
1848    #[test]
1849    fn from_f32_lossless() {
1850        let from_f32 = |v: f32| bf16::from_f32_lossless(v);
1851        let roundtrip = |v: f32, expected: Option<bf16>| {
1852            let half = from_f32(v);
1853            assert_eq!(half, expected);
1854            if !expected.is_none() {
1855                let as_f32 = expected.unwrap().to_f32_const();
1856                assert_eq!(v, as_f32);
1857            }
1858        };
1859
1860        assert_eq!(from_f32(f32::NAN).map(bf16::is_nan), Some(true));
1861        roundtrip(f32::INFINITY, Some(bf16::INFINITY));
1862        roundtrip(f32::NEG_INFINITY, Some(bf16::NEG_INFINITY));
1863        roundtrip(f32::from_bits(0b0_00000000_00000000000000000000000), Some(bf16(0)));
1864        roundtrip(
1865            f32::from_bits(0b1_00000000_00000000000000000000000),
1866            Some(bf16(bf16::SIGN_MASK)),
1867        );
1868        roundtrip(f32::from_bits(1), None);
1869        roundtrip(f32::from_bits(0b0_00001010_10101001010110100101110), None);
1870        roundtrip(f32::from_bits(0b0_00001010_10101001010110100101110), None);
1871        roundtrip(f32::from_bits(0b0_00001010_10101011000000000000000), None);
1872        roundtrip(
1873            f32::from_bits(0b0_00001010_10101010000000000000000),
1874            Some(bf16(0b0_00001010_1010101)),
1875        );
1876        roundtrip(f32::from_bits(0b0_00000000_10000000000000000000000), Some(bf16(0x40)));
1877        // special truncation with denormals, etc.
1878        roundtrip(f32::from_bits(0b0_00000000_00000001000000000000000), None);
1879        roundtrip(f32::from_bits(0b0_00000000_00000010000000000000000), Some(bf16(1)));
1880        roundtrip(f32::from_bits(0b0_00000000_00000100000000000000000), Some(bf16(2)));
1881        roundtrip(f32::from_bits(0b0_00000000_00000110000000000000000), Some(bf16(3)));
1882        roundtrip(f32::from_bits(0b0_00000000_00000111000000000000000), None);
1883        roundtrip(f32::from_bits(0b0_00001011_10100111101101101001001), None);
1884        // 1.99170198e-35 and has bits until 16 to the end, so truncated 2
1885        roundtrip(f32::from_bits(0b0_00001011_10100111100000000000000), None);
1886        // 1.99170198e-35 and has bits until 15 to the end, so truncated 1
1887        roundtrip(f32::from_bits(0b0_00001011_10100111000000000000000), None);
1888        // 1.99170198e-35 and has bits until 15 to the end, so truncated 1
1889        roundtrip(f32::from_bits(0b0_00001011_10100110000000000000000), Some(bf16(0x05d3)));
1890    }
1891
1892    #[test]
1893    fn from_f64_lossless() {
1894        let from_f64 = |v: f64| bf16::from_f64_lossless(v);
1895        let roundtrip = |v: f64, expected: Option<bf16>| {
1896            let half = from_f64(v);
1897            assert_eq!(half, expected);
1898            if !expected.is_none() {
1899                let as_f64 = expected.unwrap().to_f64_const();
1900                assert_eq!(v, as_f64);
1901            }
1902        };
1903
1904        assert_eq!(from_f64(f64::NAN).map(bf16::is_nan), Some(true));
1905        roundtrip(f64::INFINITY, Some(bf16::INFINITY));
1906        roundtrip(f64::NEG_INFINITY, Some(bf16::NEG_INFINITY));
1907        roundtrip(
1908            f64::from_bits(0b0_00000000000_0000000000000000000000000000000000000000000000000000),
1909            Some(bf16(0)),
1910        );
1911        roundtrip(
1912            f64::from_bits(0b1_00000000000_0000000000000000000000000000000000000000000000000000),
1913            Some(bf16(bf16::SIGN_MASK)),
1914        );
1915        roundtrip(
1916            f64::from_bits(0b0_01110001010_1010100101011010010110110111111110000111101000001111),
1917            None,
1918        );
1919        // 1.99170198e-35 and has bits until 44 to the end, so truncated 1
1920        roundtrip(
1921            f64::from_bits(0b0_01110001010_1010100100000000000000000000000000000000000000000000),
1922            None,
1923        );
1924        roundtrip(
1925            f64::from_bits(0b0_01110001010_1010100000000000000000000000000000000000000000000000),
1926            Some(bf16(0x0554)),
1927        );
1928        roundtrip(
1929            f64::from_bits(0b0_01110001010_1010101000000000000000000000000000000000000000000000),
1930            Some(bf16(0x0555)),
1931        );
1932        roundtrip(
1933            f64::from_bits(0b0_01110001010_1010110000000000000000000000000000000000000000000000),
1934            Some(bf16(0x0556)),
1935        );
1936        roundtrip(
1937            f64::from_bits(0b0_01110001010_1010111000000000000000000000000000000000000000000000),
1938            Some(bf16(0x0557)),
1939        );
1940        roundtrip(
1941            f64::from_bits(0b0_01110001010_1010101100000000000000000000000000000000000000000000),
1942            None,
1943        );
1944        roundtrip(
1945            f64::from_bits(0b0_01110001010_1010100110000000000000000000000000000000000000000000),
1946            None,
1947        );
1948        roundtrip(
1949            f64::from_bits(0b1_01110001010_1010100000000000000000000000000000000000000000000000),
1950            Some(bf16(0x8554)),
1951        );
1952        roundtrip(
1953            f64::from_bits(0b1_01110001010_1010101000000000000000000000000000000000000000000000),
1954            Some(bf16(0x8555)),
1955        );
1956        // exp out of range but finite
1957        roundtrip(
1958            f64::from_bits(0b1_11110001010_1010101000000000000000000000000000000000000000000000),
1959            None,
1960        );
1961        // explicitly check denormals
1962        roundtrip(
1963            f64::from_bits(0b0_01101111010_0000000000000000000000000000000000000000000000000000),
1964            Some(bf16(1)),
1965        );
1966        roundtrip(
1967            f64::from_bits(0b0_01101111011_1000000000000000000000000000000000000000000000000000),
1968            Some(bf16(3)),
1969        );
1970        roundtrip(
1971            f64::from_bits(0b0_01101111011_1100000000000000000000000000000000000000000000000000),
1972            None,
1973        );
1974        // Due to being denormal, this is truncated out
1975        roundtrip(
1976            f64::from_bits(0b0_01101111010_0001000000000000000000000000000000000000000000000000),
1977            None,
1978        );
1979        roundtrip(
1980            f64::from_bits(0b0_01101111010_1000000000000000000000000000000000000000000000000000),
1981            None,
1982        );
1983    }
1984
1985    #[test]
1986    fn test_max() {
1987        let a = bf16::from_f32(0.0);
1988        let b = bf16::from_f32(42.0);
1989        assert_eq!(a.max(b), b);
1990
1991        let a = bf16::from_f32(42.0);
1992        let b = bf16::from_f32(0.0);
1993        assert_eq!(a.max(b), a);
1994
1995        let a = bf16::NAN;
1996        let b = bf16::from_f32(42.0);
1997        assert_eq!(a.max(b), b);
1998
1999        let a = bf16::from_f32(42.0);
2000        let b = bf16::NAN;
2001        assert_eq!(a.max(b), a);
2002
2003        let a = bf16::NAN;
2004        let b = bf16::NAN;
2005        assert!(a.max(b).is_nan());
2006    }
2007
2008    #[test]
2009    fn test_min() {
2010        let a = bf16::from_f32(0.0);
2011        let b = bf16::from_f32(42.0);
2012        assert_eq!(a.min(b), a);
2013
2014        let a = bf16::from_f32(42.0);
2015        let b = bf16::from_f32(0.0);
2016        assert_eq!(a.min(b), b);
2017
2018        let a = bf16::NAN;
2019        let b = bf16::from_f32(42.0);
2020        assert_eq!(a.min(b), b);
2021
2022        let a = bf16::from_f32(42.0);
2023        let b = bf16::NAN;
2024        assert_eq!(a.min(b), a);
2025
2026        let a = bf16::NAN;
2027        let b = bf16::NAN;
2028        assert!(a.min(b).is_nan());
2029    }
2030}