//! Pure Rust CPU quantization primitives for Leleka preparation tools. //! No inference, GPU operation or complete model support is implied. pub mod iq2_s; mod iq2s_grid; pub mod iq3_s; pub mod iq3_xxs; mod iq3s_grid; mod iq3xxs_grid; pub mod iq4_nl; pub mod iq4_xs; mod k_scale_min; pub mod q2_0; pub mod q4_k; pub mod q5_k; pub mod q6_k; pub mod q8_0; pub fn f16_bits_to_f32(bits: u16) -> f32 { let sign = (u32::from(bits & 0x8000)) << 16; let exponent = (bits >> 10) & 0x1f; let mantissa = u32::from(bits & 0x03ff); let result = match exponent { 0 if mantissa == 0 => sign, 0 => { let leading = mantissa.leading_zeros() - 21; let normalized = mantissa << leading; sign | ((113 - leading) << 23) | ((normalized & 0x03ff) << 13) } 31 => sign | 0x7f80_0000 | (mantissa << 13), _ => sign | ((u32::from(exponent) + 112) << 23) | (mantissa << 13), }; f32::from_bits(result) } #[cfg(test)] mod tests { use super::f16_bits_to_f32; #[test] fn binary16_boundaries_preserve_exact_binary32_bits() { // Independent IEEE 754 encodings, including signed zero and NaN payloads. for (half, single) in [ (0x0000, 0x0000_0000), (0x8000, 0x8000_0000), (0x0001, 0x3380_0000), (0x03ff, 0x387f_c000), (0x0400, 0x3880_0000), (0x3c00, 0x3f80_0000), (0xc000, 0xc000_0000), (0x7bff, 0x477f_e000), (0x7c00, 0x7f80_0000), (0xfc00, 0xff80_0000), (0x7c01, 0x7f80_2000), (0xfe00, 0xffc0_0000), ] { assert_eq!(f16_bits_to_f32(half).to_bits(), single); } } }