Skip to content

Commit 013a71a

Browse files
aykevldeadprogram
authored andcommitted
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.
1 parent f0091b3 commit 013a71a

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

compiler/compiler.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2425,17 +2425,17 @@ func (c *Compiler) parseBinOp(op token.Token, typ types.Type, x, y llvm.Value, p
24252425
case token.QUO: // /
24262426
return c.builder.CreateFDiv(x, y, ""), nil
24272427
case token.EQL: // ==
2428-
return c.builder.CreateFCmp(llvm.FloatOEQ, x, y, ""), nil
2428+
return c.builder.CreateFCmp(llvm.FloatUEQ, x, y, ""), nil
24292429
case token.NEQ: // !=
2430-
return c.builder.CreateFCmp(llvm.FloatONE, x, y, ""), nil
2430+
return c.builder.CreateFCmp(llvm.FloatUNE, x, y, ""), nil
24312431
case token.LSS: // <
2432-
return c.builder.CreateFCmp(llvm.FloatOLT, x, y, ""), nil
2432+
return c.builder.CreateFCmp(llvm.FloatULT, x, y, ""), nil
24332433
case token.LEQ: // <=
2434-
return c.builder.CreateFCmp(llvm.FloatOLE, x, y, ""), nil
2434+
return c.builder.CreateFCmp(llvm.FloatULE, x, y, ""), nil
24352435
case token.GTR: // >
2436-
return c.builder.CreateFCmp(llvm.FloatOGT, x, y, ""), nil
2436+
return c.builder.CreateFCmp(llvm.FloatUGT, x, y, ""), nil
24372437
case token.GEQ: // >=
2438-
return c.builder.CreateFCmp(llvm.FloatOGE, x, y, ""), nil
2438+
return c.builder.CreateFCmp(llvm.FloatUGE, x, y, ""), nil
24392439
default:
24402440
panic("binop on float: " + op.String())
24412441
}

0 commit comments

Comments
 (0)