From 013a71aa3dfdb470865b5a6d9a22067b8374885f Mon Sep 17 00:00:00 2001 From: Ayke van Laethem Date: Fri, 1 Feb 2019 16:22:58 +0100 Subject: [PATCH] compiler: support NaN in float comparisons LLVM supports both "ordered" and "unordered" floating point comparisons. The difference is that unordered comparisons ignore NaN and produce incorrect results when presented with a NaN value. This commit switches these comparisons from ordered to unordered. --- compiler/compiler.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/compiler/compiler.go b/compiler/compiler.go index 0d9a3273..5fa6c217 100644 --- a/compiler/compiler.go +++ b/compiler/compiler.go @@ -2425,17 +2425,17 @@ func (c *Compiler) parseBinOp(op token.Token, typ types.Type, x, y llvm.Value, p case token.QUO: // / return c.builder.CreateFDiv(x, y, ""), nil case token.EQL: // == - return c.builder.CreateFCmp(llvm.FloatOEQ, x, y, ""), nil + return c.builder.CreateFCmp(llvm.FloatUEQ, x, y, ""), nil case token.NEQ: // != - return c.builder.CreateFCmp(llvm.FloatONE, x, y, ""), nil + return c.builder.CreateFCmp(llvm.FloatUNE, x, y, ""), nil case token.LSS: // < - return c.builder.CreateFCmp(llvm.FloatOLT, x, y, ""), nil + return c.builder.CreateFCmp(llvm.FloatULT, x, y, ""), nil case token.LEQ: // <= - return c.builder.CreateFCmp(llvm.FloatOLE, x, y, ""), nil + return c.builder.CreateFCmp(llvm.FloatULE, x, y, ""), nil case token.GTR: // > - return c.builder.CreateFCmp(llvm.FloatOGT, x, y, ""), nil + return c.builder.CreateFCmp(llvm.FloatUGT, x, y, ""), nil case token.GEQ: // >= - return c.builder.CreateFCmp(llvm.FloatOGE, x, y, ""), nil + return c.builder.CreateFCmp(llvm.FloatUGE, x, y, ""), nil default: panic("binop on float: " + op.String()) }