nim-stint/stint/private/compiletime_cast.nim

24 lines
737 B
Nim

import ./datatypes
# this module should be in compiletime_helpers
# but the cyclic dependency of compiletime_helpers
# and int_bitwise_ops make things complicated
func convertImpl[T: SomeInteger](x: SomeInteger): T {.compileTime.} =
cast[T](x)
func convertImpl[T: IntImpl|UintImpl](x: IntImpl|UintImpl): T {.compileTime.} =
result.hi = convertImpl[type(result.hi)](x.hi)
result.lo = x.lo
func convertImpl[T: StUint|StInt](x: StUint|StInt): T {.compileTime.} =
result.data = convertImpl[type(result.data)](x.data)
template convert*[T](x: StUint|StInt|UintImpl|IntImpl|SomeInteger): T =
when nimvm:
# this is a workaround Nim VM inability to cast
# something non integer
convertImpl[T](x)
else:
cast[T](x)