f32_to_bf16

Function f32_to_bf16 

Source
pub fn f32_to_bf16(val: f32) -> u16
Expand description

2026-09-25: Convert f32 to BF16 with IEEE-754 round-to-nearest-even.

Truncation (bits >> 16) is biased toward zero; the tests below pin the cases where the two differ.

NaN maps to the canonical quiet-NaN pattern with the sign preserved.

METRALE_DISABLE_RNE is a bisect escape hatch that reverts to truncation. It is a presence check, not a value check: =0 disables RNE just as =1 does.

The escape hatch is read once per process. Weight loaders call this once per element (weight_map::quant_helpers, weight_map::fp8_lut), and a std::env::var per call would allocate a String and take the process-wide environment lock each time.

Caching is safe because the test that varies the variable re-execs the test binary as a child process with it set (disable_rne_presence_uses_truncation below), so each process resolves it once at its own start. Do not convert that test to an in-process set_var.