Skip to content

Commit 078fbe4

Browse files
committedNov 24, 2017
Auto merge of #46012 - Gankro:float-conv-transmute, r=sfackler
Make float::from_bits transmute See commit message for details. See also this discussion here: #40470 (comment) (may require libs team discussion before merging)
2 parents eb44c89 + 439576f commit 078fbe4

File tree

2 files changed

+86
-76
lines changed

2 files changed

+86
-76
lines changed
 

‎src/libstd/f32.rs

+43-48
Original file line numberDiff line numberDiff line change
@@ -998,10 +998,13 @@ impl f32 {
998998

999999
/// Raw transmutation to `u32`.
10001000
///
1001-
/// Converts the `f32` into its raw memory representation,
1002-
/// similar to the `transmute` function.
1001+
/// This is currently identical to `transmute::<f32, u32>(self)` on all platforms.
10031002
///
1004-
/// Note that this function is distinct from casting.
1003+
/// See `from_bits` for some discussion of the portability of this operation
1004+
/// (there are almost no issues).
1005+
///
1006+
/// Note that this function is distinct from `as` casting, which attempts to
1007+
/// preserve the *numeric* value, and not the bitwise value.
10051008
///
10061009
/// # Examples
10071010
///
@@ -1018,17 +1021,33 @@ impl f32 {
10181021

10191022
/// Raw transmutation from `u32`.
10201023
///
1021-
/// Converts the given `u32` containing the float's raw memory
1022-
/// representation into the `f32` type, similar to the
1023-
/// `transmute` function.
1024+
/// This is currently identical to `transmute::<u32, f32>(v)` on all platforms.
1025+
/// It turns out this is incredibly portable, for two reasons:
1026+
///
1027+
/// * Floats and Ints have the same endianess on all supported platforms.
1028+
/// * IEEE-754 very precisely specifies the bit layout of floats.
1029+
///
1030+
/// However there is one caveat: prior to the 2008 version of IEEE-754, how
1031+
/// to interpret the NaN signaling bit wasn't actually specified. Most platforms
1032+
/// (notably x86 and ARM) picked the interpretation that was ultimately
1033+
/// standardized in 2008, but some didn't (notably MIPS). As a result, all
1034+
/// signaling NaNs on MIPS are quiet NaNs on x86, and vice-versa.
1035+
///
1036+
/// Rather than trying to preserve signaling-ness cross-platform, this
1037+
/// implementation favours preserving the exact bits. This means that
1038+
/// any payloads encoded in NaNs will be preserved even if the result of
1039+
/// this method is sent over the network from an x86 machine to a MIPS one.
1040+
///
1041+
/// If the results of this method are only manipulated by the same
1042+
/// architecture that produced them, then there is no portability concern.
1043+
///
1044+
/// If the input isn't NaN, then there is no portability concern.
10241045
///
1025-
/// There is only one difference to a bare `transmute`:
1026-
/// Due to the implications onto Rust's safety promises being
1027-
/// uncertain, if the representation of a signaling NaN "sNaN" float
1028-
/// is passed to the function, the implementation is allowed to
1029-
/// return a quiet NaN instead.
1046+
/// If you don't care about signalingness (very likely), then there is no
1047+
/// portability concern.
10301048
///
1031-
/// Note that this function is distinct from casting.
1049+
/// Note that this function is distinct from `as` casting, which attempts to
1050+
/// preserve the *numeric* value, and not the bitwise value.
10321051
///
10331052
/// # Examples
10341053
///
@@ -1037,25 +1056,11 @@ impl f32 {
10371056
/// let v = f32::from_bits(0x41480000);
10381057
/// let difference = (v - 12.5).abs();
10391058
/// assert!(difference <= 1e-5);
1040-
/// // Example for a signaling NaN value:
1041-
/// let snan = 0x7F800001;
1042-
/// assert_ne!(f32::from_bits(snan).to_bits(), snan);
10431059
/// ```
10441060
#[stable(feature = "float_bits_conv", since = "1.20.0")]
10451061
#[inline]
1046-
pub fn from_bits(mut v: u32) -> Self {
1047-
const EXP_MASK: u32 = 0x7F800000;
1048-
const FRACT_MASK: u32 = 0x007FFFFF;
1049-
if v & EXP_MASK == EXP_MASK && v & FRACT_MASK != 0 {
1050-
// While IEEE 754-2008 specifies encodings for quiet NaNs
1051-
// and signaling ones, certain MIPS and PA-RISC
1052-
// CPUs treat signaling NaNs differently.
1053-
// Therefore to be safe, we pass a known quiet NaN
1054-
// if v is any kind of NaN.
1055-
// The check above only assumes IEEE 754-1985 to be
1056-
// valid.
1057-
v = unsafe { ::mem::transmute(NAN) };
1058-
}
1062+
pub fn from_bits(v: u32) -> Self {
1063+
// It turns out the safety issues with sNaN were overblown! Hooray!
10591064
unsafe { ::mem::transmute(v) }
10601065
}
10611066
}
@@ -1646,25 +1651,15 @@ mod tests {
16461651
assert_approx_eq!(f32::from_bits(0x41480000), 12.5);
16471652
assert_approx_eq!(f32::from_bits(0x44a72000), 1337.0);
16481653
assert_approx_eq!(f32::from_bits(0xc1640000), -14.25);
1649-
}
1650-
#[test]
1651-
fn test_snan_masking() {
1652-
// NOTE: this test assumes that our current platform
1653-
// implements IEEE 754-2008 that specifies the difference
1654-
// in encoding of quiet and signaling NaNs.
1655-
// If you are porting Rust to a platform that does not
1656-
// implement IEEE 754-2008 (but e.g. IEEE 754-1985, which
1657-
// only says that "Signaling NaNs shall be reserved operands"
1658-
// but doesn't specify the actual setup), feel free to
1659-
// cfg out this test.
1660-
let snan: u32 = 0x7F801337;
1661-
const QNAN_MASK: u32 = 0x00400000;
1662-
let nan_masked_fl = f32::from_bits(snan);
1663-
let nan_masked = nan_masked_fl.to_bits();
1664-
// Ensure that signaling NaNs don't stay the same
1665-
assert_ne!(nan_masked, snan);
1666-
// Ensure that we have a quiet NaN
1667-
assert_ne!(nan_masked & QNAN_MASK, 0);
1668-
assert!(nan_masked_fl.is_nan());
1654+
1655+
// Check that NaNs roundtrip their bits regardless of signalingness
1656+
// 0xA is 0b1010; 0x5 is 0b0101 -- so these two together clobbers all the mantissa bits
1657+
let masked_nan1 = f32::NAN.to_bits() ^ 0x002A_AAAA;
1658+
let masked_nan2 = f32::NAN.to_bits() ^ 0x0055_5555;
1659+
assert!(f32::from_bits(masked_nan1).is_nan());
1660+
assert!(f32::from_bits(masked_nan2).is_nan());
1661+
1662+
assert_eq!(f32::from_bits(masked_nan1).to_bits(), masked_nan1);
1663+
assert_eq!(f32::from_bits(masked_nan2).to_bits(), masked_nan2);
16691664
}
16701665
}

‎src/libstd/f64.rs

+43-28
Original file line numberDiff line numberDiff line change
@@ -953,10 +953,13 @@ impl f64 {
953953

954954
/// Raw transmutation to `u64`.
955955
///
956-
/// Converts the `f64` into its raw memory representation,
957-
/// similar to the `transmute` function.
956+
/// This is currently identical to `transmute::<f64, u64>(self)` on all platforms.
958957
///
959-
/// Note that this function is distinct from casting.
958+
/// See `from_bits` for some discussion of the portability of this operation
959+
/// (there are almost no issues).
960+
///
961+
/// Note that this function is distinct from `as` casting, which attempts to
962+
/// preserve the *numeric* value, and not the bitwise value.
960963
///
961964
/// # Examples
962965
///
@@ -973,17 +976,33 @@ impl f64 {
973976

974977
/// Raw transmutation from `u64`.
975978
///
976-
/// Converts the given `u64` containing the float's raw memory
977-
/// representation into the `f64` type, similar to the
978-
/// `transmute` function.
979+
/// This is currently identical to `transmute::<u64, f64>(v)` on all platforms.
980+
/// It turns out this is incredibly portable, for two reasons:
981+
///
982+
/// * Floats and Ints have the same endianess on all supported platforms.
983+
/// * IEEE-754 very precisely specifies the bit layout of floats.
984+
///
985+
/// However there is one caveat: prior to the 2008 version of IEEE-754, how
986+
/// to interpret the NaN signaling bit wasn't actually specified. Most platforms
987+
/// (notably x86 and ARM) picked the interpretation that was ultimately
988+
/// standardized in 2008, but some didn't (notably MIPS). As a result, all
989+
/// signaling NaNs on MIPS are quiet NaNs on x86, and vice-versa.
990+
///
991+
/// Rather than trying to preserve signaling-ness cross-platform, this
992+
/// implementation favours preserving the exact bits. This means that
993+
/// any payloads encoded in NaNs will be preserved even if the result of
994+
/// this method is sent over the network from an x86 machine to a MIPS one.
995+
///
996+
/// If the results of this method are only manipulated by the same
997+
/// architecture that produced them, then there is no portability concern.
979998
///
980-
/// There is only one difference to a bare `transmute`:
981-
/// Due to the implications onto Rust's safety promises being
982-
/// uncertain, if the representation of a signaling NaN "sNaN" float
983-
/// is passed to the function, the implementation is allowed to
984-
/// return a quiet NaN instead.
999+
/// If the input isn't NaN, then there is no portability concern.
9851000
///
986-
/// Note that this function is distinct from casting.
1001+
/// If you don't care about signalingness (very likely), then there is no
1002+
/// portability concern.
1003+
///
1004+
/// Note that this function is distinct from `as` casting, which attempts to
1005+
/// preserve the *numeric* value, and not the bitwise value.
9871006
///
9881007
/// # Examples
9891008
///
@@ -992,25 +1011,11 @@ impl f64 {
9921011
/// let v = f64::from_bits(0x4029000000000000);
9931012
/// let difference = (v - 12.5).abs();
9941013
/// assert!(difference <= 1e-5);
995-
/// // Example for a signaling NaN value:
996-
/// let snan = 0x7FF0000000000001;
997-
/// assert_ne!(f64::from_bits(snan).to_bits(), snan);
9981014
/// ```
9991015
#[stable(feature = "float_bits_conv", since = "1.20.0")]
10001016
#[inline]
1001-
pub fn from_bits(mut v: u64) -> Self {
1002-
const EXP_MASK: u64 = 0x7FF0000000000000;
1003-
const FRACT_MASK: u64 = 0x000FFFFFFFFFFFFF;
1004-
if v & EXP_MASK == EXP_MASK && v & FRACT_MASK != 0 {
1005-
// While IEEE 754-2008 specifies encodings for quiet NaNs
1006-
// and signaling ones, certain MIPS and PA-RISC
1007-
// CPUs treat signaling NaNs differently.
1008-
// Therefore to be safe, we pass a known quiet NaN
1009-
// if v is any kind of NaN.
1010-
// The check above only assumes IEEE 754-1985 to be
1011-
// valid.
1012-
v = unsafe { ::mem::transmute(NAN) };
1013-
}
1017+
pub fn from_bits(v: u64) -> Self {
1018+
// It turns out the safety issues with sNaN were overblown! Hooray!
10141019
unsafe { ::mem::transmute(v) }
10151020
}
10161021
}
@@ -1597,5 +1602,15 @@ mod tests {
15971602
assert_approx_eq!(f64::from_bits(0x4029000000000000), 12.5);
15981603
assert_approx_eq!(f64::from_bits(0x4094e40000000000), 1337.0);
15991604
assert_approx_eq!(f64::from_bits(0xc02c800000000000), -14.25);
1605+
1606+
// Check that NaNs roundtrip their bits regardless of signalingness
1607+
// 0xA is 0b1010; 0x5 is 0b0101 -- so these two together clobbers all the mantissa bits
1608+
let masked_nan1 = f64::NAN.to_bits() ^ 0x000A_AAAA_AAAA_AAAA;
1609+
let masked_nan2 = f64::NAN.to_bits() ^ 0x0005_5555_5555_5555;
1610+
assert!(f64::from_bits(masked_nan1).is_nan());
1611+
assert!(f64::from_bits(masked_nan2).is_nan());
1612+
1613+
assert_eq!(f64::from_bits(masked_nan1).to_bits(), masked_nan1);
1614+
assert_eq!(f64::from_bits(masked_nan2).to_bits(), masked_nan2);
16001615
}
16011616
}

0 commit comments

Comments
 (0)