Skip to content

Commit

Permalink
make int->float conversion a transmute
Browse files Browse the repository at this point in the history
This is the mirror commit to rust-lang/rust#46012
  • Loading branch information
Gankra authored and BurntSushi committed Nov 29, 2017
1 parent 098064b commit 1b1f206
Showing 1 changed file with 2 additions and 30 deletions.
32 changes: 2 additions & 30 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2185,40 +2185,12 @@ impl ByteOrder for LittleEndian {

#[inline]
fn safe_u32_bits_to_f32(u: u32) -> f32 {
use core::f32::NAN;

const EXP_MASK: u32 = 0x7F800000;
const FRACT_MASK: u32 = 0x007FFFFF;

if u & EXP_MASK == EXP_MASK && u & FRACT_MASK != 0 {
// While IEEE 754-2008 specifies encodings for quiet NaNs and
// signaling ones, certains MIPS and PA-RISC CPUs treat signaling
// NaNs differently. Therefore, to be safe, we pass a known quiet
// NaN if u is any kind of NaN. The check above only assumes
// IEEE 754-1985 to be valid.
NAN
} else {
unsafe { transmute(u) }
}
unsafe { transmute(u) }
}

#[inline]
fn safe_u64_bits_to_f64(u: u64) -> f64 {
use core::f64::NAN;

const EXP_MASK: u64 = 0x7FF0000000000000;
const FRACT_MASK: u64 = 0x000FFFFFFFFFFFFF;

if u & EXP_MASK == EXP_MASK && u & FRACT_MASK != 0 {
// While IEEE 754-2008 specifies encodings for quiet NaNs and
// signaling ones, certains MIPS and PA-RISC CPUs treat signaling
// NaNs differently. Therefore, to be safe, we pass a known quiet
// NaN if u is any kind of NaN. The check above only assumes
// IEEE 754-1985 to be valid.
NAN
} else {
unsafe { transmute(u) }
}
unsafe { transmute(u) }
}

#[cfg(test)]
Expand Down

0 comments on commit 1b1f206

Please sign in to comment.