How can I decode f16 to f32 using only the stable standard library?

Viewed 593

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
    }
}
1 Answers
Related