struct HalfTables {
base: [u32; 512],
shift: [u32; 512],
}
fn tables() -> &'static HalfTables {
use std::sync::OnceLock;
static TABLES: OnceLock<HalfTables> = OnceLock::new();
TABLES.get_or_init(|| {
let mut base = [0u32; 512];
let mut shift = [0u32; 512];
for i in 0..256usize {
let e = i as i32 - 127;
let (b, nb, s) = if e < -27 {
(0x0000, 0x8000, 24)
} else if e < -14 {
let b = 0x0400u32 >> (-e - 14);
(b, b | 0x8000, (-e - 1) as u32)
} else if e <= 15 {
let b = ((e + 15) as u32) << 10;
(b, b | 0x8000, 13)
} else if e < 128 {
(0x7c00, 0xfc00, 24)
} else {
(0x7c00, 0xfc00, 13)
};
base[i] = b;
base[i | 0x100] = nb;
shift[i] = s;
shift[i | 0x100] = s;
}
HalfTables { base, shift }
})
}
pub fn to_half_float(val: f64) -> u16 {
let clamped = val.clamp(-65504.0, 65504.0) as f32;
let f = clamped.to_bits();
let e = ((f >> 23) & 0x1ff) as usize;
let tables = tables();
(tables.base[e] + ((f & 0x007f_ffff) >> tables.shift[e])) as u16
}
pub fn from_half_float(val: u16) -> f32 {
let sign = (val as u32) >> 15;
let exponent = ((val >> 10) & 0x1f) as u32;
let mantissa = (val & 0x3ff) as u32;
let bits = if exponent == 0 {
if mantissa == 0 {
sign << 31
} else {
let mut m = mantissa;
let mut e: i32 = 0;
while m & 0x400 == 0 {
m <<= 1;
e -= 1;
}
(sign << 31) | (((127 - 15 + 1 + e) as u32) << 23) | ((m & 0x3ff) << 13)
}
} else if exponent == 31 {
(sign << 31) | 0x7f80_0000 | (mantissa << 13)
} else {
(sign << 31) | ((exponent + 127 - 15) << 23) | (mantissa << 13)
};
f32::from_bits(bits)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_obvious_values() {
assert_eq!(to_half_float(0.0), 0x0000);
assert_eq!(to_half_float(-0.0), 0x8000);
assert_eq!(to_half_float(1.0), 0x3c00);
assert_eq!(to_half_float(-1.0), 0xbc00);
assert_eq!(to_half_float(2.0), 0x4000);
assert_eq!(to_half_float(65504.0), 0x7bff);
assert_eq!(to_half_float(1.0e30), 0x7bff);
assert_eq!(to_half_float(-1.0e30), 0xfbff);
}
#[test]
fn denormals_and_underflow() {
assert_eq!(to_half_float(5.960_464_477_539_063e-8), 0x0001);
assert_eq!(to_half_float(6.097_555_2e-5), 0x03ff);
assert_eq!(to_half_float(6.103_515_625e-5), 0x0400);
assert_eq!(to_half_float(1.0e-12), 0x0000);
}
#[test]
fn every_half_round_trips() {
for bits in 0..=u16::MAX {
let exponent = (bits >> 10) & 0x1f;
let value = from_half_float(bits);
if exponent == 31 {
continue;
}
assert_eq!(to_half_float(value as f64), bits, "half {bits:#06x}");
}
}
#[test]
fn the_non_finite_halves() {
assert!(from_half_float(0x7c00).is_infinite() && from_half_float(0x7c00) > 0.0);
assert!(from_half_float(0xfc00).is_infinite() && from_half_float(0xfc00) < 0.0);
assert!(from_half_float(0x7e00).is_nan());
assert!(to_half_float(f64::NAN) & 0x7c00 == 0x7c00);
}
#[test]
fn the_mantissa_is_truncated_not_rounded() {
assert_eq!(to_half_float(1.0 / 3.0), 0x3555);
assert!(from_half_float(0x3555) < 1.0 / 3.0);
}
}