* Fixes #7079 * Fix handling of neg zero during constant folding
This commit is contained in:
parent
b5538990a2
commit
94038545be
4 changed files with 31 additions and 11 deletions
|
|
@ -2284,11 +2284,17 @@ proc gen(p: PProc, n: PNode, r: var TCompRes) =
|
||||||
r.kind = resExpr
|
r.kind = resExpr
|
||||||
of nkFloatLit..nkFloat64Lit:
|
of nkFloatLit..nkFloat64Lit:
|
||||||
let f = n.floatVal
|
let f = n.floatVal
|
||||||
if f != f: r.res = rope"NaN"
|
case classify(f)
|
||||||
elif f == 0.0: r.res = rope"0.0"
|
of fcNaN:
|
||||||
elif f == 0.5 * f:
|
r.res = rope"NaN"
|
||||||
if f > 0.0: r.res = rope"Infinity"
|
of fcNegZero:
|
||||||
else: r.res = rope"-Infinity"
|
r.res = rope"-0.0"
|
||||||
|
of fcZero:
|
||||||
|
r.res = rope"0.0"
|
||||||
|
of fcInf:
|
||||||
|
r.res = rope"Infinity"
|
||||||
|
of fcNegInf:
|
||||||
|
r.res = rope"-Infinity"
|
||||||
else: r.res = rope(f.toStrMaxPrecision)
|
else: r.res = rope(f.toStrMaxPrecision)
|
||||||
r.kind = resExpr
|
r.kind = resExpr
|
||||||
of nkCallKinds:
|
of nkCallKinds:
|
||||||
|
|
|
||||||
|
|
@ -8,18 +8,22 @@
|
||||||
#
|
#
|
||||||
|
|
||||||
## Serialization utilities for the compiler.
|
## Serialization utilities for the compiler.
|
||||||
import strutils
|
import strutils, math
|
||||||
|
|
||||||
proc c_snprintf(s: cstring; n:uint; frmt: cstring): cint {.importc: "snprintf", header: "<stdio.h>", nodecl, varargs.}
|
proc c_snprintf(s: cstring; n:uint; frmt: cstring): cint {.importc: "snprintf", header: "<stdio.h>", nodecl, varargs.}
|
||||||
|
|
||||||
proc toStrMaxPrecision*(f: BiggestFloat, literalPostfix = ""): string =
|
proc toStrMaxPrecision*(f: BiggestFloat, literalPostfix = ""): string =
|
||||||
if f != f:
|
case classify(f)
|
||||||
|
of fcNaN:
|
||||||
result = "NAN"
|
result = "NAN"
|
||||||
elif f == 0.0:
|
of fcNegZero:
|
||||||
|
result = "-0.0" & literalPostfix
|
||||||
|
of fcZero:
|
||||||
result = "0.0" & literalPostfix
|
result = "0.0" & literalPostfix
|
||||||
elif f == 0.5 * f:
|
of fcInf:
|
||||||
if f > 0.0: result = "INF"
|
result = "INF"
|
||||||
else: result = "-INF"
|
of fcNegInf:
|
||||||
|
result = "-INF"
|
||||||
else:
|
else:
|
||||||
when defined(nimNoArrayToCstringConversion):
|
when defined(nimNoArrayToCstringConversion):
|
||||||
result = newString(81)
|
result = newString(81)
|
||||||
|
|
|
||||||
|
|
@ -225,6 +225,7 @@ proc evalOp(m: TMagic, n, a, b, c: PNode): PNode =
|
||||||
of mDivF64:
|
of mDivF64:
|
||||||
if getFloat(b) == 0.0:
|
if getFloat(b) == 0.0:
|
||||||
if getFloat(a) == 0.0: result = newFloatNodeT(NaN, n)
|
if getFloat(a) == 0.0: result = newFloatNodeT(NaN, n)
|
||||||
|
elif getFloat(b).classify == fcNegZero: result = newFloatNodeT(-Inf, n)
|
||||||
else: result = newFloatNodeT(Inf, n)
|
else: result = newFloatNodeT(Inf, n)
|
||||||
else:
|
else:
|
||||||
result = newFloatNodeT(getFloat(a) / getFloat(b), n)
|
result = newFloatNodeT(getFloat(a) / getFloat(b), n)
|
||||||
|
|
|
||||||
9
tests/ccgbugs/t7079.nim
Normal file
9
tests/ccgbugs/t7079.nim
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
discard """
|
||||||
|
action: run
|
||||||
|
targets: '''c js'''
|
||||||
|
"""
|
||||||
|
|
||||||
|
import math
|
||||||
|
let x = -0.0
|
||||||
|
doAssert classify(x) == fcNegZero
|
||||||
|
doAssert classify(1 / -0.0) == fcNegInf
|
||||||
Loading…
Add table
Add a link
Reference in a new issue