2018-01-16 17:05:20 +00:00
|
|
|
import constants, strformat, macros
|
|
|
|
|
|
|
|
proc intToBigEndian*(value: Int256): cstring =
|
2018-01-15 18:42:40 +00:00
|
|
|
result = cstring""
|
|
|
|
|
2018-01-16 17:05:20 +00:00
|
|
|
proc bigEndianToInt*(value: cstring): Int256 =
|
|
|
|
result = 0.Int256
|
|
|
|
|
2018-01-16 18:42:38 +00:00
|
|
|
proc unsignedToSigned*(value: Int256): Int256 =
|
2018-01-16 17:05:20 +00:00
|
|
|
if value <= UINT_255_MAX:
|
|
|
|
return value
|
|
|
|
else:
|
|
|
|
return value - UINT_256_CEILING
|
|
|
|
|
2018-01-16 18:42:38 +00:00
|
|
|
proc signedToUnsigned*(value: Int256): Int256 =
|
2018-01-16 17:05:20 +00:00
|
|
|
if value < 0:
|
|
|
|
return value + UINT_256_CEILING
|
|
|
|
else:
|
|
|
|
return value
|
|
|
|
|
|
|
|
macro ceilXX(ceiling: static[int]): untyped =
|
|
|
|
var name = ident(%"ceil{ceiling}")
|
|
|
|
result = quote:
|
|
|
|
proc `name`*(value: Int256): Int256 =
|
|
|
|
var remainder = value mod `ceiling`.Int256
|
|
|
|
if remainder == 0:
|
|
|
|
return value
|
|
|
|
else:
|
|
|
|
return value + `ceiling`.Int256 - remainder
|
2018-01-16 18:42:38 +00:00
|
|
|
|
2018-01-16 17:05:20 +00:00
|
|
|
|
|
|
|
ceilXX(32)
|
|
|
|
ceilXX(8)
|