Skip to content

Commit 7f5008c

Browse files
committed
Backport LLVM apfloat commit to rustc_apfloat
Backports LLVM commit: llvm/llvm-project@e34bd1e Fixes #69532
1 parent c6e4db6 commit 7f5008c

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

compiler/rustc_apfloat/src/ieee.rs

+12
Original file line numberDiff line numberDiff line change
@@ -1511,6 +1511,18 @@ impl<S: Semantics, T: Semantics> FloatConvert<IeeeFloat<T>> for IeeeFloat<S> {
15111511
sig::set_bit(&mut r.sig, T::PRECISION - 1);
15121512
}
15131513

1514+
// If we are truncating NaN, it is possible that we shifted out all of the
1515+
// set bits in a signalling NaN payload. But NaN must remain NaN, so some
1516+
// bit in the significand must be set (otherwise it is Inf).
1517+
// This can only happen with sNaN. Set the 1st bit after the quiet bit,
1518+
// so that we still have an sNaN.
1519+
if r.sig[0] == 0 {
1520+
assert!(shift < 0, "Should not lose NaN payload on extend");
1521+
assert!(T::PRECISION >= 3, "Unexpectedly narrow significand");
1522+
assert!(*loses_info, "Missing payload should have set lost info");
1523+
sig::set_bit(&mut r.sig, T::PRECISION - 3);
1524+
}
1525+
15141526
// gcc forces the Quiet bit on, which means (float)(double)(float_sNan)
15151527
// does not give you back the same bits. This is dubious, and we
15161528
// don't currently do it. You're really supposed to get

compiler/rustc_apfloat/tests/ieee.rs

+9
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,15 @@ fn fma() {
566566
}
567567
}
568568

569+
#[test]
570+
fn issue_69532() {
571+
let f = Double::from_bits(0x7FF0_0000_0000_0001u64 as u128);
572+
let mut loses_info = false;
573+
let r: Single = f.convert(&mut loses_info).value;
574+
assert!(loses_info);
575+
assert!(r.is_nan());
576+
}
577+
569578
#[test]
570579
fn min_num() {
571580
let f1 = Double::from_f64(1.0);

src/test/ui/issues/issue-69532.rs

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// run-pass
2+
#![feature(const_fn_transmute)]
3+
4+
const fn make_nans() -> (f64, f64, f32, f32) {
5+
let nan1: f64 = unsafe { std::mem::transmute(0x7FF0_0001_0000_0001u64) };
6+
let nan2: f64 = unsafe { std::mem::transmute(0x7FF0_0000_0000_0001u64) };
7+
8+
let nan1_32 = nan1 as f32;
9+
let nan2_32 = nan2 as f32;
10+
11+
(nan1, nan2, nan1_32, nan2_32)
12+
}
13+
14+
static NANS: (f64, f64, f32, f32) = make_nans();
15+
16+
fn main() {
17+
let (nan1, nan2, nan1_32, nan2_32) = NANS;
18+
19+
assert!(nan1.is_nan());
20+
assert!(nan2.is_nan());
21+
22+
assert!(nan1_32.is_nan());
23+
assert!(nan2_32.is_nan());
24+
}

0 commit comments

Comments
 (0)