summary refs log tree commit diff stats
path: root/tests/vm/tzero_extend.nim
diff options
context:
space:
mode:
authorParashurama <Rhagdamaziel@ymail.com>2017-08-03 10:58:45 +0200
committerAndreas Rumpf <rumpf_a@web.de>2017-08-03 10:58:45 +0200
commitf063943d5f9015b0222b5d3c3ff9322c47b5ec6f (patch)
tree00e2a6b4dcd8a4a34a583b1d17e666e5881a7160 /tests/vm/tzero_extend.nim
parent0755f902ddedab9427ac6930e8597114a4f69e71 (diff)
downloadNim-f063943d5f9015b0222b5d3c3ff9322c47b5ec6f.tar.gz
Vm fix zero extend proc ze/ze64 && toU32/toU16/toU8 (#5988)
* fixes ze/ze64 procs in VM.
* fixes toU8/toU16/toU32.
* add tests for ze/ze64 toU32/toU16/toU8 procs
Diffstat (limited to 'tests/vm/tzero_extend.nim')
-rw-r--r--tests/vm/tzero_extend.nim44
1 files changed, 44 insertions, 0 deletions
diff --git a/tests/vm/tzero_extend.nim b/tests/vm/tzero_extend.nim
new file mode 100644
index 000000000..a79105531
--- /dev/null
+++ b/tests/vm/tzero_extend.nim
@@ -0,0 +1,44 @@
+
+const RANGE = -384.. -127
+
+proc get_values(): (seq[int8], seq[int16], seq[int32]) =
+  let i8 = -3'i8
+  let i16 = -3'i16
+  let i32 = -3'i32
+  doAssert i8.ze == 0xFD
+  doAssert i8.ze64 == 0xFD
+  doAssert i16.ze == 0xFFFD
+  doAssert i16.ze64 == 0xFFFD
+
+  result[0] = @[]; result[1] = @[]; result[2] = @[]
+
+  for offset in RANGE:
+    let i8 = -(1 shl 9) + offset
+    let i16 = -(1 shl 17) + offset
+    let i32 = -(1 shl 33) + offset
+
+    # higher bits are masked. these should be exactly equal to offset.
+    result[0].add i8.toU8
+    result[1].add i16.toU16
+    result[2].add i32.toU32
+
+
+# these values this computed by VM
+const COMPILETIME_VALUES = get_values()
+
+# these values this computed by compiler
+let RUNTIME_VALUES = get_values()
+
+template check_values(int_type: static[int]) =
+  var index = 0
+  let cvalues = COMPILETIME_VALUES[int_type]
+  let rvalues = RUNTIME_VALUES[int_type]
+  for offset in RANGE:
+    let moffset = cast[type(rvalues[0])](offset)
+    doAssert(moffset == rvalues[index] and moffset == cvalues[index],
+      "expected: " & $moffset & " got runtime: " & $rvalues[index] & " && compiletime: " & $cvalues[index] )
+    inc(index)
+
+check_values(0) # uint8
+check_values(1) # uint16
+check_values(2) # uint32