I need to convert stored half floats (16 bit) to standard 32 bit floats. I currently use the code below, but it relies on libc. I want to use only std
and it should work on stable Rust.
#[inline]
fn decode_f16(half: u16) -> f32 {
let exp: u16 = half >> 10 & 0x1f;
let mant: u16 = half & 0x3ff;
let val: f32 = if exp == 0 {
ffi::c_ldexpf(mant as f32, -24)
} else if exp != 31 {
ffi::c_ldexpf(mant as f32 + 1024f32, exp as isize - 25)
} else if mant == 0 {
::std::f32::INFINITY
} else {
::std::f32::NAN
};
if half & 0x8000 != 0 {
-val
} else {
val
}
}
You can just replace ffi::c_ldexpf(x, y)
with x * (2.0).powi(y)
. This works on all u16
s, according to my exhaustive test.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With