Skip to content

Commit 615d738

Browse files
committed
ensure unary minus propagates NaN payloads exactly
1 parent 6796c57 commit 615d738

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

compiler/rustc_const_eval/src/interpret/operator.rs

+1
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
461461
Ok((ImmTy::from_bool(res, *self.tcx), false))
462462
}
463463
ty::Float(fty) => {
464+
// No NaN adjustment here, `-` is a bitwise operation!
464465
let res = match (un_op, fty) {
465466
(Neg, FloatTy::F32) => Scalar::from_f32(-val.to_f32()?),
466467
(Neg, FloatTy::F64) => Scalar::from_f64(-val.to_f64()?),

src/tools/miri/tests/pass/float_nan.rs

+8
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,14 @@ fn test_f32() {
241241
]),
242242
|| F32::from(just1 % all1_snan),
243243
);
244+
245+
// Unary `-` must preserve payloads exactly.
246+
check_all_outcomes(HashSet::from_iter([F32::nan(Neg, Quiet, all1_payload)]), || {
247+
F32::from(-all1)
248+
});
249+
check_all_outcomes(HashSet::from_iter([F32::nan(Neg, Signaling, all1_payload)]), || {
250+
F32::from(-all1_snan)
251+
});
244252
}
245253

246254
fn test_f64() {

0 commit comments

Comments
 (0)