tzero_extend.nim 1.3 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 int(cast[uint8](i8)) == 0xFD
  7. doAssert int64(cast[uint8](i8)) == 0xFD
  8. doAssert int(cast[uint16](i16)) == 0xFFFD
  9. doAssert int64(cast[uint16](i16)) == 0xFFFD
  10. result[0] = @[]; result[1] = @[]; result[2] = @[]
  11. for offset in RANGE:
  12. let i8 = -(1'i64 shl 9) + offset
  13. let i16 = -(1'i64 shl 17) + offset
  14. let i32 = -(1'i64 shl 33) + offset
  15. # higher bits are masked. these should be exactly equal to offset.
  16. result[0].add cast[int8](cast[uint64](i8))
  17. result[1].add cast[int16](cast[uint64](i16))
  18. result[2].add cast[int32](cast[uint64](i32))
  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