bitops_utils.nim 764 B

1234567891011121314151617181920212223
  1. template forwardImpl*(impl, arg) {.dirty.} =
  2. when sizeof(x) <= 4:
  3. when x is SomeSignedInt:
  4. impl(cast[uint32](x.int32))
  5. else:
  6. impl(x.uint32)
  7. else:
  8. when x is SomeSignedInt:
  9. impl(cast[uint64](x.int64))
  10. else:
  11. impl(x.uint64)
  12. # this could also be implemented via:
  13. # import std/typetraits
  14. # template castToUnsigned*(x: SomeInteger): auto = cast[toUnsigned(typeof(x))](x)
  15. template castToUnsigned*(x: int8): uint8 = cast[uint8](x)
  16. template castToUnsigned*(x: int16): uint16 = cast[uint16](x)
  17. template castToUnsigned*(x: int32): uint32 = cast[uint32](x)
  18. template castToUnsigned*(x: int64): uint64 = cast[uint64](x)
  19. template castToUnsigned*(x: int): uint = cast[uint](x)
  20. template castToUnsigned*[T: SomeUnsignedInt](x: T): T = x