Skip to content

Commit

Permalink
Rollup merge of #77368 - est31:apfloat_fix, r=varkor
Browse files Browse the repository at this point in the history
Backport LLVM apfloat commit to rustc_apfloat

Backports LLVM commit: llvm/llvm-project@e34bd1e

Fixes #69532
  • Loading branch information
jonas-schievink authored Oct 4, 2020
2 parents 4ae7710 + d010809 commit 9ea462f
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 14 deletions.
15 changes: 10 additions & 5 deletions compiler/rustc_apfloat/src/ieee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1511,11 +1511,16 @@ impl<S: Semantics, T: Semantics> FloatConvert<IeeeFloat<T>> for IeeeFloat<S> {
sig::set_bit(&mut r.sig, T::PRECISION - 1);
}

// gcc forces the Quiet bit on, which means (float)(double)(float_sNan)
// does not give you back the same bits. This is dubious, and we
// don't currently do it. You're really supposed to get
// an invalid operation signal at runtime, but nobody does that.
status = Status::OK;
// Convert of sNaN creates qNaN and raises an exception (invalid op).
// This also guarantees that a sNaN does not become Inf on a truncation
// that loses all payload bits.
if self.is_signaling() {
// Quiet signaling NaN.
sig::set_bit(&mut r.sig, T::QNAN_BIT);
status = Status::INVALID_OP;
} else {
status = Status::OK;
}
} else {
*loses_info = false;
status = Status::OK;
Expand Down
34 changes: 25 additions & 9 deletions compiler/rustc_apfloat/tests/ieee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,17 @@ fn fma() {
}
}

#[test]
fn issue_69532() {
let f = Double::from_bits(0x7FF0_0000_0000_0001u64 as u128);
let mut loses_info = false;
let sta = f.convert(&mut loses_info);
let r: Single = sta.value;
assert!(loses_info);
assert!(r.is_nan());
assert_eq!(sta.status, Status::INVALID_OP);
}

#[test]
fn min_num() {
let f1 = Double::from_f64(1.0);
Expand Down Expand Up @@ -1492,27 +1503,32 @@ fn convert() {
assert_eq!(4294967295.0, test.to_f64());
assert!(!loses_info);

let test = Single::snan(None);
let x87_snan = X87DoubleExtended::snan(None);
let test: X87DoubleExtended = test.convert(&mut loses_info).value;
assert!(test.bitwise_eq(x87_snan));
assert!(!loses_info);

let test = Single::qnan(None);
let x87_qnan = X87DoubleExtended::qnan(None);
let test: X87DoubleExtended = test.convert(&mut loses_info).value;
assert!(test.bitwise_eq(x87_qnan));
assert!(!loses_info);

let test = X87DoubleExtended::snan(None);
let test: X87DoubleExtended = test.convert(&mut loses_info).value;
assert!(test.bitwise_eq(x87_snan));
let test = Single::snan(None);
let sta = test.convert(&mut loses_info);
let test: X87DoubleExtended = sta.value;
assert!(test.is_nan());
assert!(!test.is_signaling());
assert!(!loses_info);
assert_eq!(sta.status, Status::INVALID_OP);

let test = X87DoubleExtended::qnan(None);
let test: X87DoubleExtended = test.convert(&mut loses_info).value;
assert!(test.bitwise_eq(x87_qnan));
assert!(!loses_info);

let test = X87DoubleExtended::snan(None);
let sta = test.convert(&mut loses_info);
let test: X87DoubleExtended = sta.value;
assert!(test.is_nan());
assert!(!test.is_signaling());
assert!(!loses_info);
assert_eq!(sta.status, Status::INVALID_OP);
}

#[test]
Expand Down
24 changes: 24 additions & 0 deletions src/test/ui/issues/issue-69532.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// run-pass
#![feature(const_fn_transmute)]

const fn make_nans() -> (f64, f64, f32, f32) {
let nan1: f64 = unsafe { std::mem::transmute(0x7FF0_0001_0000_0001u64) };
let nan2: f64 = unsafe { std::mem::transmute(0x7FF0_0000_0000_0001u64) };

let nan1_32 = nan1 as f32;
let nan2_32 = nan2 as f32;

(nan1, nan2, nan1_32, nan2_32)
}

static NANS: (f64, f64, f32, f32) = make_nans();

fn main() {
let (nan1, nan2, nan1_32, nan2_32) = NANS;

assert!(nan1.is_nan());
assert!(nan2.is_nan());

assert!(nan1_32.is_nan());
assert!(nan2_32.is_nan());
}

0 comments on commit 9ea462f

Please sign in to comment.