Skip to content

Commit

Permalink
ensure unary minus propagates NaN payloads exactly
Browse files Browse the repository at this point in the history
  • Loading branch information
RalfJung committed Oct 9, 2023
1 parent 6796c57 commit 615d738
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
1 change: 1 addition & 0 deletions compiler/rustc_const_eval/src/interpret/operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
Ok((ImmTy::from_bool(res, *self.tcx), false))
}
ty::Float(fty) => {
// No NaN adjustment here, `-` is a bitwise operation!
let res = match (un_op, fty) {
(Neg, FloatTy::F32) => Scalar::from_f32(-val.to_f32()?),
(Neg, FloatTy::F64) => Scalar::from_f64(-val.to_f64()?),
Expand Down
8 changes: 8 additions & 0 deletions src/tools/miri/tests/pass/float_nan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,14 @@ fn test_f32() {
]),
|| F32::from(just1 % all1_snan),
);

// Unary `-` must preserve payloads exactly.
check_all_outcomes(HashSet::from_iter([F32::nan(Neg, Quiet, all1_payload)]), || {
F32::from(-all1)
});
check_all_outcomes(HashSet::from_iter([F32::nan(Neg, Signaling, all1_payload)]), || {
F32::from(-all1_snan)
});
}

fn test_f64() {
Expand Down

0 comments on commit 615d738

Please sign in to comment.