Skip to content

Commit ca591ce

Browse files
committed
make int->float conversion a transmute on x86/x64/ARM/aarch64
This is the mirror commit to rust-lang/rust#46012
1 parent fef2055 commit ca591ce

File tree

1 file changed

+40
-28
lines changed

1 file changed

+40
-28
lines changed

src/lib.rs

+40-28
Original file line numberDiff line numberDiff line change
@@ -2185,40 +2185,52 @@ impl ByteOrder for LittleEndian {
21852185

21862186
#[inline]
21872187
fn safe_u32_bits_to_f32(u: u32) -> f32 {
2188-
use core::f32::NAN;
2189-
2190-
const EXP_MASK: u32 = 0x7F800000;
2191-
const FRACT_MASK: u32 = 0x007FFFFF;
2192-
2193-
if u & EXP_MASK == EXP_MASK && u & FRACT_MASK != 0 {
2194-
// While IEEE 754-2008 specifies encodings for quiet NaNs and
2195-
// signaling ones, certains MIPS and PA-RISC CPUs treat signaling
2196-
// NaNs differently. Therefore, to be safe, we pass a known quiet
2197-
// NaN if u is any kind of NaN. The check above only assumes
2198-
// IEEE 754-1985 to be valid.
2199-
NAN
2200-
} else {
2201-
unsafe { transmute(u) }
2188+
if cfg!(not(any(
2189+
target_arch = "x86",
2190+
target_arch = "x64",
2191+
target_arch = "arm",
2192+
target_arch = "aarch64",
2193+
))) {
2194+
use core::f32::NAN;
2195+
2196+
const EXP_MASK: u32 = 0x7F800000;
2197+
const FRACT_MASK: u32 = 0x007FFFFF;
2198+
2199+
if u & EXP_MASK == EXP_MASK && u & FRACT_MASK != 0 {
2200+
// While IEEE 754-2008 specifies encodings for quiet NaNs and
2201+
// signaling ones, certains MIPS and PA-RISC CPUs treat signaling
2202+
// NaNs differently. Therefore, to be safe, we pass a known quiet
2203+
// NaN if u is any kind of NaN. The check above only assumes
2204+
// IEEE 754-1985 to be valid.
2205+
return NAN;
2206+
}
22022207
}
2208+
unsafe { transmute(u) }
22032209
}
22042210

22052211
#[inline]
22062212
fn safe_u64_bits_to_f64(u: u64) -> f64 {
2207-
use core::f64::NAN;
2208-
2209-
const EXP_MASK: u64 = 0x7FF0000000000000;
2210-
const FRACT_MASK: u64 = 0x000FFFFFFFFFFFFF;
2211-
2212-
if u & EXP_MASK == EXP_MASK && u & FRACT_MASK != 0 {
2213-
// While IEEE 754-2008 specifies encodings for quiet NaNs and
2214-
// signaling ones, certains MIPS and PA-RISC CPUs treat signaling
2215-
// NaNs differently. Therefore, to be safe, we pass a known quiet
2216-
// NaN if u is any kind of NaN. The check above only assumes
2217-
// IEEE 754-1985 to be valid.
2218-
NAN
2219-
} else {
2220-
unsafe { transmute(u) }
2213+
if cfg!(not(any(
2214+
target_arch = "x86",
2215+
target_arch = "x64",
2216+
target_arch = "arm",
2217+
target_arch = "aarch64",
2218+
))) {
2219+
use core::f64::NAN;
2220+
2221+
const EXP_MASK: u64 = 0x7FF0000000000000;
2222+
const FRACT_MASK: u64 = 0x000FFFFFFFFFFFFF;
2223+
2224+
if u & EXP_MASK == EXP_MASK && u & FRACT_MASK != 0 {
2225+
// While IEEE 754-2008 specifies encodings for quiet NaNs and
2226+
// signaling ones, certains MIPS and PA-RISC CPUs treat signaling
2227+
// NaNs differently. Therefore, to be safe, we pass a known quiet
2228+
// NaN if u is any kind of NaN. The check above only assumes
2229+
// IEEE 754-1985 to be valid.
2230+
return NAN;
2231+
}
22212232
}
2233+
unsafe { transmute(u) }
22222234
}
22232235

22242236
#[cfg(test)]

0 commit comments

Comments
 (0)