pub const MAX_BF16: f32 = f32::from_bits(0x7f7f_0000);
pub const MAX_CBRT_BF16: f32 = f32::from_bits(0x7eaa_0000);
const _: () = {
assert!(MAX_CBRT_BF16 < f32::MAX / 3.0);
assert!(expand_bf16(floor_bf16(MAX_CBRT_BF16) + 1) > f32::MAX / 3.0);
};
#[inline(always)]
pub const fn expand_bf16(bits: u16) -> f32 {
f32::from_bits((bits as u32) << 16)
}
#[inline(always)]
pub const fn floor_bf16(x: f32) -> u16 {
if x == 0.0 {
return 0;
}
let b = x.to_bits();
if (b & 0x8000_0000) == 0 || (b & 0xffff) == 0 {
(b >> 16) as u16
} else {
((b >> 16) as u16) + 1
}
}
#[inline(always)]
pub const fn ceil_bf16(x: f32) -> u16 {
let neg = floor_bf16(-x);
if neg == 0 { 0 } else { neg ^ 0x8000 }
}
#[inline(always)]
pub(crate) const fn decode_bf16_finite(bits: u16) -> Option<f32> {
if bits == 0x8000 || (bits & 0x7f80) == 0x7f80 {
return None;
}
Some(expand_bf16(bits))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn exact_boundaries_round_trip_losslessly() {
for exact in [
0.0_f32,
f32::MIN_POSITIVE,
-f32::MIN_POSITIVE,
1.0 / 65_536.0,
0.25,
0.5,
107.0 / 128.0, 1.0,
-1.0,
2.0,
4.0,
8.0,
80.0,
88.0,
-87.0,
-126.0,
127.0,
127.5,
65_536.0,
MAX_CBRT_BF16,
-MAX_CBRT_BF16,
MAX_BF16,
-MAX_BF16,
] {
let lo = floor_bf16(exact);
let hi = ceil_bf16(exact);
assert_eq!(lo, hi, "exact bf16 {exact} diverged");
assert_eq!(expand_bf16(lo).to_bits(), exact.to_bits());
assert_eq!(decode_bf16_finite(lo), Some(exact));
}
assert_eq!(floor_bf16(-0.0), 0);
assert_eq!(ceil_bf16(-0.0), 0);
assert_eq!(decode_bf16_finite(0x8000), None);
assert_eq!(decode_bf16_finite(0x7f80), None);
assert_eq!(decode_bf16_finite(0xff80), None);
assert_eq!(decode_bf16_finite(0x7fc0), None);
assert!(core::hint::black_box(MAX_CBRT_BF16) < f32::MAX / 3.0);
assert!(expand_bf16(floor_bf16(MAX_CBRT_BF16) + 1) > f32::MAX / 3.0);
}
#[test]
fn outward_rounding_brackets_inexact_inputs_and_carries_across_exponents() {
for x in [
0.01_f32,
-0.01,
0.346_573_56,
-0.346_573_56,
0.99,
-0.999_999_94, 2.2,
2.4,
1.0 / 2.4,
127.49,
f32::from_bits(1),
-f32::from_bits(1),
] {
let lo = expand_bf16(floor_bf16(x));
let hi = expand_bf16(ceil_bf16(x));
assert!(lo <= x, "floor_bf16({x}) = {lo} > {x}");
assert!(hi >= x, "ceil_bf16({x}) = {hi} < {x}");
}
assert_eq!(expand_bf16(floor_bf16(-0.999_999_94)), -1.0);
assert_eq!(expand_bf16(ceil_bf16(127.49)), 127.5);
}
}