compiler_builtins\libm\src\math\support/
float_traits.rs

1#![allow(unknown_lints)] // FIXME(msrv) we shouldn't need this
2
3use core::{fmt, mem, ops};
4
5use super::int_traits::{CastFrom, Int, MinInt};
6
7/// Trait for some basic operations on floats
8// #[allow(dead_code)]
9#[allow(dead_code)] // Some constants are only used with tests
10pub trait Float:
11    Copy
12    + fmt::Debug
13    + PartialEq
14    + PartialOrd
15    + ops::AddAssign
16    + ops::MulAssign
17    + ops::Add<Output = Self>
18    + ops::Sub<Output = Self>
19    + ops::Mul<Output = Self>
20    + ops::Div<Output = Self>
21    + ops::Rem<Output = Self>
22    + ops::Neg<Output = Self>
23    + 'static
24{
25    /// A uint of the same width as the float
26    type Int: Int<OtherSign = Self::SignedInt, Unsigned = Self::Int>;
27
28    /// A int of the same width as the float
29    type SignedInt: Int
30        + MinInt<OtherSign = Self::Int, Unsigned = Self::Int>
31        + ops::Neg<Output = Self::SignedInt>;
32
33    const ZERO: Self;
34    const NEG_ZERO: Self;
35    const ONE: Self;
36    const NEG_ONE: Self;
37    const INFINITY: Self;
38    const NEG_INFINITY: Self;
39    const NAN: Self;
40    const NEG_NAN: Self;
41    const MAX: Self;
42    const MIN: Self;
43    const EPSILON: Self;
44    const PI: Self;
45    const NEG_PI: Self;
46    const FRAC_PI_2: Self;
47
48    const MIN_POSITIVE_NORMAL: Self;
49
50    /// The bitwidth of the float type
51    const BITS: u32;
52
53    /// The bitwidth of the significand
54    const SIG_BITS: u32;
55
56    /// The bitwidth of the exponent
57    const EXP_BITS: u32 = Self::BITS - Self::SIG_BITS - 1;
58
59    /// The saturated (maximum bitpattern) value of the exponent, i.e. the infinite
60    /// representation.
61    ///
62    /// This shifted fully right, use `EXP_MASK` for the shifted value.
63    const EXP_SAT: u32 = (1 << Self::EXP_BITS) - 1;
64
65    /// The exponent bias value
66    const EXP_BIAS: u32 = Self::EXP_SAT >> 1;
67
68    /// Maximum unbiased exponent value.
69    const EXP_MAX: i32 = Self::EXP_BIAS as i32;
70
71    /// Minimum *NORMAL* unbiased exponent value.
72    const EXP_MIN: i32 = -(Self::EXP_MAX - 1);
73
74    /// Minimum subnormal exponent value.
75    const EXP_MIN_SUBNORM: i32 = Self::EXP_MIN - Self::SIG_BITS as i32;
76
77    /// A mask for the sign bit
78    const SIGN_MASK: Self::Int;
79
80    /// A mask for the significand
81    const SIG_MASK: Self::Int;
82
83    /// A mask for the exponent
84    const EXP_MASK: Self::Int;
85
86    /// The implicit bit of the float format
87    const IMPLICIT_BIT: Self::Int;
88
89    /// Returns `self` transmuted to `Self::Int`
90    fn to_bits(self) -> Self::Int;
91
92    /// Returns `self` transmuted to `Self::SignedInt`
93    #[allow(dead_code)]
94    fn to_bits_signed(self) -> Self::SignedInt {
95        self.to_bits().signed()
96    }
97
98    /// Check bitwise equality.
99    #[allow(dead_code)]
100    fn biteq(self, rhs: Self) -> bool {
101        self.to_bits() == rhs.to_bits()
102    }
103
104    /// Checks if two floats have the same bit representation. *Except* for NaNs! NaN can be
105    /// represented in multiple different ways.
106    ///
107    /// This method returns `true` if two NaNs are compared. Use [`biteq`](Self::biteq) instead
108    /// if `NaN` should not be treated separately.
109    #[allow(dead_code)]
110    fn eq_repr(self, rhs: Self) -> bool {
111        if self.is_nan() && rhs.is_nan() {
112            true
113        } else {
114            self.biteq(rhs)
115        }
116    }
117
118    /// Returns true if the value is NaN.
119    fn is_nan(self) -> bool;
120
121    /// Returns true if the value is +inf or -inf.
122    fn is_infinite(self) -> bool;
123
124    /// Returns true if the sign is negative. Extracts the sign bit regardless of zero or NaN.
125    fn is_sign_negative(self) -> bool;
126
127    /// Returns true if the sign is positive. Extracts the sign bit regardless of zero or NaN.
128    fn is_sign_positive(self) -> bool {
129        !self.is_sign_negative()
130    }
131
132    /// Returns if `self` is subnormal.
133    #[allow(dead_code)]
134    fn is_subnormal(self) -> bool {
135        (self.to_bits() & Self::EXP_MASK) == Self::Int::ZERO
136    }
137
138    /// Returns the exponent, not adjusting for bias, not accounting for subnormals or zero.
139    fn ex(self) -> u32 {
140        u32::cast_from(self.to_bits() >> Self::SIG_BITS) & Self::EXP_SAT
141    }
142
143    /// Extract the exponent and adjust it for bias, not accounting for subnormals or zero.
144    fn exp_unbiased(self) -> i32 {
145        self.ex().signed() - (Self::EXP_BIAS as i32)
146    }
147
148    /// Returns the significand with no implicit bit (or the "fractional" part)
149    #[allow(dead_code)]
150    fn frac(self) -> Self::Int {
151        self.to_bits() & Self::SIG_MASK
152    }
153
154    /// Returns a `Self::Int` transmuted back to `Self`
155    fn from_bits(a: Self::Int) -> Self;
156
157    /// Constructs a `Self` from its parts. Inputs are treated as bits and shifted into position.
158    fn from_parts(negative: bool, exponent: u32, significand: Self::Int) -> Self {
159        let sign = if negative {
160            Self::Int::ONE
161        } else {
162            Self::Int::ZERO
163        };
164        Self::from_bits(
165            (sign << (Self::BITS - 1))
166                | (Self::Int::cast_from(exponent & Self::EXP_SAT) << Self::SIG_BITS)
167                | (significand & Self::SIG_MASK),
168        )
169    }
170
171    #[allow(dead_code)]
172    fn abs(self) -> Self;
173
174    /// Returns a number composed of the magnitude of self and the sign of sign.
175    fn copysign(self, other: Self) -> Self;
176
177    /// Fused multiply add, rounding once.
178    fn fma(self, y: Self, z: Self) -> Self;
179
180    /// Returns (normalized exponent, normalized significand)
181    #[allow(dead_code)]
182    fn normalize(significand: Self::Int) -> (i32, Self::Int);
183
184    /// Returns a number that represents the sign of self.
185    #[allow(dead_code)]
186    fn signum(self) -> Self {
187        if self.is_nan() {
188            self
189        } else {
190            Self::ONE.copysign(self)
191        }
192    }
193
194    /// Make a best-effort attempt to canonicalize the number. Note that this is allowed
195    /// to be a nop and does not always quiet sNaNs.
196    fn canonicalize(self) -> Self {
197        // FIXME: LLVM often removes this. We should determine whether we can remove the operation,
198        // or switch to something based on `llvm.canonicalize` (which has crashes,
199        // <https://github.com/llvm/llvm-project/issues/32650>).
200        self * Self::ONE
201    }
202}
203
204/// Access the associated `Int` type from a float (helper to avoid ambiguous associated types).
205pub type IntTy<F> = <F as Float>::Int;
206
207macro_rules! float_impl {
208    (
209        $ty:ident,
210        $ity:ident,
211        $sity:ident,
212        $bits:expr,
213        $significand_bits:expr,
214        $from_bits:path,
215        $to_bits:path,
216        $fma_fn:ident,
217        $fma_intrinsic:ident
218    ) => {
219        impl Float for $ty {
220            type Int = $ity;
221            type SignedInt = $sity;
222
223            const ZERO: Self = 0.0;
224            const NEG_ZERO: Self = -0.0;
225            const ONE: Self = 1.0;
226            const NEG_ONE: Self = -1.0;
227            const INFINITY: Self = Self::INFINITY;
228            const NEG_INFINITY: Self = Self::NEG_INFINITY;
229            const NAN: Self = Self::NAN;
230            // NAN isn't guaranteed to be positive but it usually is. We only use this for
231            // tests.
232            const NEG_NAN: Self = $from_bits($to_bits(Self::NAN) | Self::SIGN_MASK);
233            const MAX: Self = -Self::MIN;
234            // Sign bit set, saturated mantissa, saturated exponent with last bit zeroed
235            const MIN: Self = $from_bits(Self::Int::MAX & !(1 << Self::SIG_BITS));
236            const EPSILON: Self = <$ty>::EPSILON;
237
238            // Exponent is a 1 in the LSB
239            const MIN_POSITIVE_NORMAL: Self = $from_bits(1 << Self::SIG_BITS);
240
241            const PI: Self = core::$ty::consts::PI;
242            const NEG_PI: Self = -Self::PI;
243            const FRAC_PI_2: Self = core::$ty::consts::FRAC_PI_2;
244
245            const BITS: u32 = $bits;
246            const SIG_BITS: u32 = $significand_bits;
247
248            const SIGN_MASK: Self::Int = 1 << (Self::BITS - 1);
249            const SIG_MASK: Self::Int = (1 << Self::SIG_BITS) - 1;
250            const EXP_MASK: Self::Int = !(Self::SIGN_MASK | Self::SIG_MASK);
251            const IMPLICIT_BIT: Self::Int = 1 << Self::SIG_BITS;
252
253            fn to_bits(self) -> Self::Int {
254                self.to_bits()
255            }
256            fn is_nan(self) -> bool {
257                self.is_nan()
258            }
259            fn is_infinite(self) -> bool {
260                self.is_infinite()
261            }
262            fn is_sign_negative(self) -> bool {
263                self.is_sign_negative()
264            }
265            fn from_bits(a: Self::Int) -> Self {
266                Self::from_bits(a)
267            }
268            fn abs(self) -> Self {
269                cfg_if! {
270                    // FIXME(msrv): `abs` is available in `core` starting with 1.85.
271                    if #[cfg(intrinsics_enabled)] {
272                        self.abs()
273                    } else {
274                        super::super::generic::fabs(self)
275                    }
276                }
277            }
278            fn copysign(self, other: Self) -> Self {
279                cfg_if! {
280                    // FIXME(msrv): `copysign` is available in `core` starting with 1.85.
281                    if #[cfg(intrinsics_enabled)] {
282                        self.copysign(other)
283                    } else {
284                        super::super::generic::copysign(self, other)
285                    }
286                }
287            }
288            fn fma(self, y: Self, z: Self) -> Self {
289                cfg_if! {
290                    // fma is not yet available in `core`
291                    if #[cfg(intrinsics_enabled)] {
292                        unsafe{ core::intrinsics::$fma_intrinsic(self, y, z) }
293                    } else {
294                        super::super::$fma_fn(self, y, z)
295                    }
296                }
297            }
298            fn normalize(significand: Self::Int) -> (i32, Self::Int) {
299                let shift = significand.leading_zeros().wrapping_sub(Self::EXP_BITS);
300                (
301                    1i32.wrapping_sub(shift as i32),
302                    significand << shift as Self::Int,
303                )
304            }
305        }
306    };
307}
308
309#[cfg(f16_enabled)]
310float_impl!(
311    f16,
312    u16,
313    i16,
314    16,
315    10,
316    f16::from_bits,
317    f16::to_bits,
318    fmaf16,
319    fmaf16
320);
321float_impl!(
322    f32,
323    u32,
324    i32,
325    32,
326    23,
327    f32_from_bits,
328    f32_to_bits,
329    fmaf,
330    fmaf32
331);
332float_impl!(
333    f64,
334    u64,
335    i64,
336    64,
337    52,
338    f64_from_bits,
339    f64_to_bits,
340    fma,
341    fmaf64
342);
343#[cfg(f128_enabled)]
344float_impl!(
345    f128,
346    u128,
347    i128,
348    128,
349    112,
350    f128::from_bits,
351    f128::to_bits,
352    fmaf128,
353    fmaf128
354);
355
356/* FIXME(msrv): vendor some things that are not const stable at our MSRV */
357
358/// `f32::from_bits`
359#[allow(unnecessary_transmutes)] // lint appears in newer versions of Rust
360pub const fn f32_from_bits(bits: u32) -> f32 {
361    // SAFETY: POD cast with no preconditions
362    unsafe { mem::transmute::<u32, f32>(bits) }
363}
364
365/// `f32::to_bits`
366#[allow(unnecessary_transmutes)] // lint appears in newer versions of Rust
367pub const fn f32_to_bits(x: f32) -> u32 {
368    // SAFETY: POD cast with no preconditions
369    unsafe { mem::transmute::<f32, u32>(x) }
370}
371
372/// `f64::from_bits`
373#[allow(unnecessary_transmutes)] // lint appears in newer versions of Rust
374pub const fn f64_from_bits(bits: u64) -> f64 {
375    // SAFETY: POD cast with no preconditions
376    unsafe { mem::transmute::<u64, f64>(bits) }
377}
378
379/// `f64::to_bits`
380#[allow(unnecessary_transmutes)] // lint appears in newer versions of Rust
381pub const fn f64_to_bits(x: f64) -> u64 {
382    // SAFETY: POD cast with no preconditions
383    unsafe { mem::transmute::<f64, u64>(x) }
384}
385
386/// Trait for floats twice the bit width of another integer.
387pub trait DFloat: Float {
388    /// Float that is half the bit width of the floatthis trait is implemented for.
389    type H: HFloat<D = Self>;
390
391    /// Narrow the float type.
392    fn narrow(self) -> Self::H;
393}
394
395/// Trait for floats half the bit width of another float.
396pub trait HFloat: Float {
397    /// Float that is double the bit width of the float this trait is implemented for.
398    type D: DFloat<H = Self>;
399
400    /// Widen the float type.
401    fn widen(self) -> Self::D;
402}
403
404macro_rules! impl_d_float {
405    ($($X:ident $D:ident),*) => {
406        $(
407            impl DFloat for $D {
408                type H = $X;
409
410                fn narrow(self) -> Self::H {
411                    self as $X
412                }
413            }
414        )*
415    };
416}
417
418macro_rules! impl_h_float {
419    ($($H:ident $X:ident),*) => {
420        $(
421            impl HFloat for $H {
422                type D = $X;
423
424                fn widen(self) -> Self::D {
425                    self as $X
426                }
427            }
428        )*
429    };
430}
431
432impl_d_float!(f32 f64);
433#[cfg(f16_enabled)]
434impl_d_float!(f16 f32);
435#[cfg(f128_enabled)]
436impl_d_float!(f64 f128);
437
438impl_h_float!(f32 f64);
439#[cfg(f16_enabled)]
440impl_h_float!(f16 f32);
441#[cfg(f128_enabled)]
442impl_h_float!(f64 f128);
443
444#[cfg(test)]
445mod tests {
446    use super::*;
447
448    #[test]
449    #[cfg(f16_enabled)]
450    fn check_f16() {
451        // Constants
452        assert_eq!(f16::EXP_SAT, 0b11111);
453        assert_eq!(f16::EXP_BIAS, 15);
454        assert_eq!(f16::EXP_MAX, 15);
455        assert_eq!(f16::EXP_MIN, -14);
456        assert_eq!(f16::EXP_MIN_SUBNORM, -24);
457
458        // `exp_unbiased`
459        assert_eq!(f16::FRAC_PI_2.exp_unbiased(), 0);
460        assert_eq!((1.0f16 / 2.0).exp_unbiased(), -1);
461        assert_eq!(f16::MAX.exp_unbiased(), 15);
462        assert_eq!(f16::MIN.exp_unbiased(), 15);
463        assert_eq!(f16::MIN_POSITIVE.exp_unbiased(), -14);
464        // This is a convenience method and not ldexp, `exp_unbiased` does not return correct
465        // results for zero and subnormals.
466        assert_eq!(f16::ZERO.exp_unbiased(), -15);
467        assert_eq!(f16::from_bits(0x1).exp_unbiased(), -15);
468        assert_eq!(f16::MIN_POSITIVE, f16::MIN_POSITIVE_NORMAL);
469
470        // `from_parts`
471        assert_biteq!(f16::from_parts(true, f16::EXP_BIAS, 0), -1.0f16);
472        assert_biteq!(f16::from_parts(false, 0, 1), f16::from_bits(0x1));
473    }
474
475    #[test]
476    fn check_f32() {
477        // Constants
478        assert_eq!(f32::EXP_SAT, 0b11111111);
479        assert_eq!(f32::EXP_BIAS, 127);
480        assert_eq!(f32::EXP_MAX, 127);
481        assert_eq!(f32::EXP_MIN, -126);
482        assert_eq!(f32::EXP_MIN_SUBNORM, -149);
483
484        // `exp_unbiased`
485        assert_eq!(f32::FRAC_PI_2.exp_unbiased(), 0);
486        assert_eq!((1.0f32 / 2.0).exp_unbiased(), -1);
487        assert_eq!(f32::MAX.exp_unbiased(), 127);
488        assert_eq!(f32::MIN.exp_unbiased(), 127);
489        assert_eq!(f32::MIN_POSITIVE.exp_unbiased(), -126);
490        // This is a convenience method and not ldexp, `exp_unbiased` does not return correct
491        // results for zero and subnormals.
492        assert_eq!(f32::ZERO.exp_unbiased(), -127);
493        assert_eq!(f32::from_bits(0x1).exp_unbiased(), -127);
494        assert_eq!(f32::MIN_POSITIVE, f32::MIN_POSITIVE_NORMAL);
495
496        // `from_parts`
497        assert_biteq!(f32::from_parts(true, f32::EXP_BIAS, 0), -1.0f32);
498        assert_biteq!(
499            f32::from_parts(false, 10 + f32::EXP_BIAS, 0),
500            hf32!("0x1p10")
501        );
502        assert_biteq!(f32::from_parts(false, 0, 1), f32::from_bits(0x1));
503    }
504
505    #[test]
506    fn check_f64() {
507        // Constants
508        assert_eq!(f64::EXP_SAT, 0b11111111111);
509        assert_eq!(f64::EXP_BIAS, 1023);
510        assert_eq!(f64::EXP_MAX, 1023);
511        assert_eq!(f64::EXP_MIN, -1022);
512        assert_eq!(f64::EXP_MIN_SUBNORM, -1074);
513
514        // `exp_unbiased`
515        assert_eq!(f64::FRAC_PI_2.exp_unbiased(), 0);
516        assert_eq!((1.0f64 / 2.0).exp_unbiased(), -1);
517        assert_eq!(f64::MAX.exp_unbiased(), 1023);
518        assert_eq!(f64::MIN.exp_unbiased(), 1023);
519        assert_eq!(f64::MIN_POSITIVE.exp_unbiased(), -1022);
520        // This is a convenience method and not ldexp, `exp_unbiased` does not return correct
521        // results for zero and subnormals.
522        assert_eq!(f64::ZERO.exp_unbiased(), -1023);
523        assert_eq!(f64::from_bits(0x1).exp_unbiased(), -1023);
524        assert_eq!(f64::MIN_POSITIVE, f64::MIN_POSITIVE_NORMAL);
525
526        // `from_parts`
527        assert_biteq!(f64::from_parts(true, f64::EXP_BIAS, 0), -1.0f64);
528        assert_biteq!(
529            f64::from_parts(false, 10 + f64::EXP_BIAS, 0),
530            hf64!("0x1p10")
531        );
532        assert_biteq!(f64::from_parts(false, 0, 1), f64::from_bits(0x1));
533    }
534
535    #[test]
536    #[cfg(f128_enabled)]
537    fn check_f128() {
538        // Constants
539        assert_eq!(f128::EXP_SAT, 0b111111111111111);
540        assert_eq!(f128::EXP_BIAS, 16383);
541        assert_eq!(f128::EXP_MAX, 16383);
542        assert_eq!(f128::EXP_MIN, -16382);
543        assert_eq!(f128::EXP_MIN_SUBNORM, -16494);
544
545        // `exp_unbiased`
546        assert_eq!(f128::FRAC_PI_2.exp_unbiased(), 0);
547        assert_eq!((1.0f128 / 2.0).exp_unbiased(), -1);
548        assert_eq!(f128::MAX.exp_unbiased(), 16383);
549        assert_eq!(f128::MIN.exp_unbiased(), 16383);
550        assert_eq!(f128::MIN_POSITIVE.exp_unbiased(), -16382);
551        // This is a convenience method and not ldexp, `exp_unbiased` does not return correct
552        // results for zero and subnormals.
553        assert_eq!(f128::ZERO.exp_unbiased(), -16383);
554        assert_eq!(f128::from_bits(0x1).exp_unbiased(), -16383);
555        assert_eq!(f128::MIN_POSITIVE, f128::MIN_POSITIVE_NORMAL);
556
557        // `from_parts`
558        assert_biteq!(f128::from_parts(true, f128::EXP_BIAS, 0), -1.0f128);
559        assert_biteq!(f128::from_parts(false, 0, 1), f128::from_bits(0x1));
560    }
561}