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