Skip to content

Commit

Permalink
fix nim-lang#12884 VM float32
Browse files Browse the repository at this point in the history
  • Loading branch information
timotheecour committed Dec 16, 2019
1 parent e5ed4c1 commit 6aae864
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
5 changes: 4 additions & 1 deletion compiler/vm.nim
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,10 @@ proc putIntoReg(dest: var TFullReg; n: PNode) =
dest.intVal = n.intVal
of nkFloatLit..nkFloat128Lit:
dest.kind = rkFloat
dest.floatVal = n.floatVal
case n.typ.kind
of tyFloat32: dest.floatVal = n.floatVal.float32
of tyFloat: dest.floatVal = n.floatVal.float # handles 32bit float on 32 bit platforms
else: dest.floatVal = n.floatVal
else:
dest.kind = rkNode
dest.node = n
Expand Down
5 changes: 3 additions & 2 deletions compiler/vmgen.nim
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,8 @@ proc sameConstant*(a, b: PNode): bool =
of nkSym: result = a.sym == b.sym
of nkIdent: result = a.ident.id == b.ident.id
of nkCharLit..nkUInt64Lit: result = a.intVal == b.intVal
of nkFloatLit..nkFloat64Lit: result = a.floatVal == b.floatVal
of nkFloatLit..nkFloat64Lit:
result = a.floatVal == b.floatVal and a.typ.kind == b.typ.kind
of nkStrLit..nkTripleStrLit: result = a.strVal == b.strVal
of nkType, nkNilLit: result = a.typ == b.typ
of nkEmpty: result = true
Expand All @@ -462,7 +463,7 @@ proc sameConstant*(a, b: PNode): bool =
result = true

proc genLiteral(c: PCtx; n: PNode): int =
# types do not matter here:
# types do not matter here except for special cases eg float32, see #12884
for i in 0..<c.constants.len:
if sameConstant(c.constants[i], n): return i
result = rawGenLiteral(c, n)
Expand Down
25 changes: 25 additions & 0 deletions tests/vm/tfloat32.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
proc main(): string =
let x0 = float32(1.32)
let x1: float32 = 1.32
let x3 = 1.32'f32
let x4 = 1.32
let x2 = x1
let x5 = x3*x3 # will differ here
let x6 = 1.32'f64
let x7: float = 1.32
$(x0, x1, x2, x3, x4, x5, x6, x7, x3*x3,
x3 * 2.0, # will differ here
$type(x4), $type(x5))

const a1 = main()
let a2 = main()

#[
CT (a1) and RT (a2) values differ when float operations are involved (eg x3*x3),
but otherwise are the same
]#
doAssert a1 == """
(1.320000052452087, 1.320000052452087, 1.320000052452087, 1.320000052452087, 1.32, 1.742400138473513, 1.32, 1.32, 1.742400138473513, 2.640000104904175, "float64", "float32")"""

doAssert a2 == """
(1.320000052452087, 1.320000052452087, 1.320000052452087, 1.320000052452087, 1.32, 1.742400169372559, 1.32, 1.32, 1.742400169372559, 2.640000104904175, "float64", "float32")"""

0 comments on commit 6aae864

Please sign in to comment.