simd_kernels/traits/
to_bits.rs

1//! # **ToBit trait** - *IEEE 754 bit conversion*
2
3// Copyright Peter Bower 2025. All Rights Reserved.
4// Licensed under Mozilla Public License (MPL) 2.0.
5
6/// Generic trait for converting floating-point types to their IEEE 754 bit representation.
7///
8/// Unifies access to a `to_bits` method across different floating-point types,
9/// enabling generic operation on floating-point bit patterns.
10///
11/// # Type Parameters
12/// The associated `Bits` type represents the unsigned integer type with equivalent
13/// bit width to the floating-point type, supporting equality comparison and hashing.
14///
15/// # Implementation Requirements
16/// Implementations must preserve IEEE 754 bit layout and handle special values
17/// (NaN, infinity, signed zero) according to standard specifications.
18pub trait ToBits {
19    /// The unsigned integer type representing the bit pattern.
20    ///
21    /// Must be the same bit width as the floating-point type and support
22    /// equality comparison and hashing for use in collections.
23    type Bits: Eq + std::hash::Hash + Copy;
24
25    /// Converts the floating-point value to its IEEE 754 bit representation.
26    ///
27    /// Returns the raw bit pattern as an unsigned integer, preserving all
28    /// floating-point special values and sign information.
29    ///
30    /// # Examples
31    /// ```rust,ignore
32    /// use simd_kernels::traits::to_bits::ToBits;
33    ///
34    /// let f = 3.14f32;
35    /// let bits = f.to_bits(); // Returns u32 bit representation
36    /// ```
37    fn to_bits(self) -> Self::Bits;
38}
39
40/// Implementation for 32-bit IEEE 754 single-precision floating-point values.
41///
42/// Maps to the corresponding 32-bit unsigned integer bit pattern, preserving
43/// all floating-point special values including NaN bit patterns and signed zeros.
44impl ToBits for f32 {
45    type Bits = u32;
46
47    #[inline(always)]
48    fn to_bits(self) -> u32 {
49        f32::to_bits(self)
50    }
51}
52
53/// Implementation for 64-bit IEEE 754 double-precision floating-point values.
54///
55/// Maps to the corresponding 64-bit unsigned integer bit pattern, preserving
56/// all floating-point special values including NaN bit patterns and signed zeros.
57impl ToBits for f64 {
58    type Bits = u64;
59
60    #[inline(always)]
61    fn to_bits(self) -> u64 {
62        f64::to_bits(self)
63    }
64}