diff --git a/compiler/icnif/nifdecoder.nim b/compiler/icnif/nifdecoder.nim index f10e49c168..03b008690c 100644 --- a/compiler/icnif/nifdecoder.nim +++ b/compiler/icnif/nifdecoder.nim @@ -293,9 +293,23 @@ proc fromNif(c: var DecodeContext; n: var Cursor): PNode = inc n of nkFloatLit .. nkFloat128Lit: c.withNode n, result, kind: - expect n, FloatLit - result.floatVal = pool.floats[n.floatId] - inc n + if n.kind == FloatLit: + result.floatVal = pool.floats[n.floatId] + inc n + elif n.kind == ParLe: + case pool.tags[n.tagId] + of "inf": + result.floatVal = Inf + of "nan": + result.floatVal = NaN + of "neginf": + result.floatVal = NegInf + else: + assert false, "expected float literal but got " & pool.tags[n.tagId] + inc n + skipParRi n + else: + assert false, "expected float literal but got " & $n.kind of nkStrLit .. nkTripleStrLit: c.withNode n, result, kind: expect n, StringLit diff --git a/tests/icnif/tencode_node2node.nim b/tests/icnif/tencode_node2node.nim index 564ea8606a..a6c183a928 100644 --- a/tests/icnif/tencode_node2node.nim +++ b/tests/icnif/tencode_node2node.nim @@ -1,4 +1,4 @@ -import std/assertions +import std/[assertions, math] import "../../compiler/icnif" / [nifencoder, nifdecoder] import "../../compiler" / [idents, ast, astalgo, options, pathutils, modulegraphs, modules, msgs, pipelines, syntaxes, sem, llstream, lineinfos] @@ -294,8 +294,25 @@ proc eql(x, y: PNode; c: var EqlContext): bool = debug(y.sym) debug(x.sym.typ) debug(y.sym.typ) - of nkCharLit .. nkTripleStrLit: + of nkCharLit .. nkUInt64Lit, nkStrLit .. nkTripleStrLit: result = sameValue(x, y) + of nkFloatLit .. nkFloat128Lit: + # want to know if x and y are identical float value. + # so x == y doesn't work if both x and y are NaN or x == 0 and y == -0. + let xc = classify(x.floatVal) + let yc = classify(y.floatVal) + if xc == yc: + if xc in {fcNormal, fcSubnormal}: + if x.floatVal != y.floatVal: + echo "float literal mismatch: ", x.floatVal, "/", y.floatVal + result = false + else: + result = true + else: + result = true + else: + echo "float literal mismatch: ", xc, "/", yc + result = false else: result = true for i in 0 ..< x.safeLen: diff --git a/tests/icnif/testcode/modtestliterals.nim b/tests/icnif/testcode/modtestliterals.nim index 2d11cf8fa9..bb63ceb265 100644 --- a/tests/icnif/testcode/modtestliterals.nim +++ b/tests/icnif/testcode/modtestliterals.nim @@ -23,7 +23,14 @@ var uint32litH = 4294967295'u32 var uint64litH = 18446744073709551615'u64 var floatlit = 1.25 +var floatlit2 = -1.25 var float32lit = 1.25'f32 var float64lit = 1.25'f64 +var floatZero = 0.0 +# Needs newer Nimony to save `-0.0` to NIF correclty +#var floatNegZero = -0.0 +var floatInf = Inf +var floatNaN = NaN +var floatNegInf = NegInf var nillit: ptr int = nil