Skip to content

handle NaN in unordered comparisons #627

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 44 additions & 1 deletion src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1284,7 +1284,50 @@ impl<'a, 'gcc, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'gcc, 'tcx> {
}

fn fcmp(&mut self, op: RealPredicate, lhs: RValue<'gcc>, rhs: RValue<'gcc>) -> RValue<'gcc> {
self.context.new_comparison(self.location, op.to_gcc_comparison(), lhs, rhs)
// LLVM has a concept of "unordered compares", where eg ULT returns true if either the two
// arguments are unordered (i.e. either is NaN), or the lhs is less than the rhs. GCC does
// not natively have this concept, so in some cases we must manually handle NaNs
let must_handle_nan = match op {
RealPredicate::RealPredicateFalse => unreachable!(),
RealPredicate::RealOEQ => false,
RealPredicate::RealOGT => false,
RealPredicate::RealOGE => false,
RealPredicate::RealOLT => false,
RealPredicate::RealOLE => false,
RealPredicate::RealONE => false,
RealPredicate::RealORD => unreachable!(),
RealPredicate::RealUNO => unreachable!(),
RealPredicate::RealUEQ => false,
RealPredicate::RealUGT => true,
RealPredicate::RealUGE => true,
RealPredicate::RealULT => true,
RealPredicate::RealULE => true,
RealPredicate::RealUNE => false,
RealPredicate::RealPredicateTrue => unreachable!(),
};

let cmp = self.context.new_comparison(self.location, op.to_gcc_comparison(), lhs, rhs);

if must_handle_nan {
let is_nan = self.context.new_binary_op(
self.location,
BinaryOp::LogicalOr,
self.cx.bool_type,
// compare a value to itself to check whether it is NaN
self.context.new_comparison(self.location, ComparisonOp::NotEquals, lhs, lhs),
self.context.new_comparison(self.location, ComparisonOp::NotEquals, rhs, rhs),
);

self.context.new_binary_op(
self.location,
BinaryOp::LogicalOr,
self.cx.bool_type,
is_nan,
cmp,
)
} else {
cmp
}
}

/* Miscellaneous instructions */
Expand Down
1 change: 0 additions & 1 deletion tests/failing-ui-tests.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ tests/ui/asm/x86_64/multiple-clobber-abi.rs
tests/ui/functions-closures/parallel-codegen-closures.rs
tests/ui/linkage-attr/linkage1.rs
tests/ui/lto/dylib-works.rs
tests/ui/numbers-arithmetic/saturating-float-casts.rs
tests/ui/sepcomp/sepcomp-cci.rs
tests/ui/sepcomp/sepcomp-extern.rs
tests/ui/sepcomp/sepcomp-fns-backwards.rs
Expand Down
32 changes: 32 additions & 0 deletions tests/run/float.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Compiler:
//
// Run-time:
// status: 0

#![feature(const_black_box)]

/*
* Code
*/

fn main() {
use std::hint::black_box;

macro_rules! check {
($ty:ty, $expr:expr) => {{
const EXPECTED: $ty = $expr;
assert_eq!($expr, EXPECTED);
}};
}

check!(i32, (black_box(0.0f32) as i32));

check!(u64, (black_box(f32::NAN) as u64));
check!(u128, (black_box(f32::NAN) as u128));

check!(i64, (black_box(f64::NAN) as i64));
check!(u64, (black_box(f64::NAN) as u64));

check!(i16, (black_box(f32::MIN) as i16));
check!(i16, (black_box(f32::MAX) as i16));
}