tzero_extend.nim 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. const RANGE = -384.. -127
  2. proc get_values(): (seq[int8], seq[int16], seq[int32]) =
  3. let i8 = -3'i8
  4. let i16 = -3'i16
  5. let i32 = -3'i32
  6. doAssert i8.ze == 0xFD
  7. doAssert i8.ze64 == 0xFD
  8. doAssert i16.ze == 0xFFFD
  9. doAssert i16.ze64 == 0xFFFD
  10. result[0] = @[]; result[1] = @[]; result[2] = @[]
  11. for offset in RANGE:
  12. let i8 = -(1 shl 9) + offset
  13. let i16 = -(1 shl 17) + offset
  14. let i32 = -(1 shl 33) + offset
  15. # higher bits are masked. these should be exactly equal to offset.
  16. result[0].add i8.toU8
  17. result[1].add i16.toU16
  18. result[2].add i32.toU32
  19. # these values this computed by VM
  20. const COMPILETIME_VALUES = get_values()
  21. # these values this computed by compiler
  22. let RUNTIME_VALUES = get_values()
  23. template check_values(int_type: static[int]) =
  24. var index = 0
  25. let cvalues = COMPILETIME_VALUES[int_type]
  26. let rvalues = RUNTIME_VALUES[int_type]
  27. for offset in RANGE:
  28. let moffset = cast[type(rvalues[0])](offset)
  29. doAssert(moffset == rvalues[index] and moffset == cvalues[index],
  30. "expected: " & $moffset & " got runtime: " & $rvalues[index] & " && compiletime: " & $cvalues[index] )
  31. inc(index)
  32. check_values(0) # uint8
  33. check_values(1) # uint16
  34. check_values(2) # uint32