Skip to content

Commit 7c83d9f

Browse files
committed
saves/loads float Inf, NaN and NegInf literals
1 parent 4157c4e commit 7c83d9f

File tree

3 files changed

+43
-5
lines changed

3 files changed

+43
-5
lines changed

compiler/icnif/nifdecoder.nim

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -293,9 +293,23 @@ proc fromNif(c: var DecodeContext; n: var Cursor): PNode =
293293
inc n
294294
of nkFloatLit .. nkFloat128Lit:
295295
c.withNode n, result, kind:
296-
expect n, FloatLit
297-
result.floatVal = pool.floats[n.floatId]
298-
inc n
296+
if n.kind == FloatLit:
297+
result.floatVal = pool.floats[n.floatId]
298+
inc n
299+
elif n.kind == ParLe:
300+
case pool.tags[n.tagId]
301+
of "inf":
302+
result.floatVal = Inf
303+
of "nan":
304+
result.floatVal = NaN
305+
of "neginf":
306+
result.floatVal = NegInf
307+
else:
308+
assert false, "expected float literal but got " & pool.tags[n.tagId]
309+
inc n
310+
skipParRi n
311+
else:
312+
assert false, "expected float literal but got " & $n.kind
299313
of nkStrLit .. nkTripleStrLit:
300314
c.withNode n, result, kind:
301315
expect n, StringLit

tests/icnif/tencode_node2node.nim

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import std/assertions
1+
import std/[assertions, math]
22
import "../../compiler/icnif" / [nifencoder, nifdecoder]
33
import "../../compiler" / [idents, ast, astalgo, options, pathutils, modulegraphs, modules, msgs, pipelines, syntaxes, sem, llstream, lineinfos]
44

@@ -294,8 +294,25 @@ proc eql(x, y: PNode; c: var EqlContext): bool =
294294
debug(y.sym)
295295
debug(x.sym.typ)
296296
debug(y.sym.typ)
297-
of nkCharLit .. nkTripleStrLit:
297+
of nkCharLit .. nkUInt64Lit, nkStrLit .. nkTripleStrLit:
298298
result = sameValue(x, y)
299+
of nkFloatLit .. nkFloat128Lit:
300+
# want to know if x and y are identical float value.
301+
# so x == y doesn't work if both x and y are NaN or x == 0 and y == -0.
302+
let xc = classify(x.floatVal)
303+
let yc = classify(y.floatVal)
304+
if xc == yc:
305+
if xc in {fcNormal, fcSubnormal}:
306+
if x.floatVal != y.floatVal:
307+
echo "float literal mismatch: ", x.floatVal, "/", y.floatVal
308+
result = false
309+
else:
310+
result = true
311+
else:
312+
result = true
313+
else:
314+
echo "float literal mismatch: ", xc, "/", yc
315+
result = false
299316
else:
300317
result = true
301318
for i in 0 ..< x.safeLen:

tests/icnif/testcode/modtestliterals.nim

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,14 @@ var uint32litH = 4294967295'u32
2323
var uint64litH = 18446744073709551615'u64
2424

2525
var floatlit = 1.25
26+
var floatlit2 = -1.25
2627
var float32lit = 1.25'f32
2728
var float64lit = 1.25'f64
29+
var floatZero = 0.0
30+
# Needs newer Nimony to save `-0.0` to NIF correclty
31+
#var floatNegZero = -0.0
32+
var floatInf = Inf
33+
var floatNaN = NaN
34+
var floatNegInf = NegInf
2835

2936
var nillit: ptr int = nil

0 commit comments

Comments
 (0)