diff --git a/stint/private/compiletime_helpers.nim b/stint/private/compiletime_helpers.nim index 96eaec3..8eaa992 100644 --- a/stint/private/compiletime_helpers.nim +++ b/stint/private/compiletime_helpers.nim @@ -1,5 +1,20 @@ import ./datatypes, ./uint_bitwise_ops +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 + +template convert*[T](x: 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) + func getByte*(x: SomeInteger, pos: int): byte {.compileTime.} = type DT = type x byte((x shr (pos * 8)) and 0xFF.DT) diff --git a/stint/private/endians2_priv.nim b/stint/private/endians2_priv.nim index 812be23..98f4c62 100644 --- a/stint/private/endians2_priv.nim +++ b/stint/private/endians2_priv.nim @@ -8,7 +8,7 @@ func swapBytes*(x: UintImpl): UintImpl {.inline.} = UintImpl(hi: hi, lo: lo) -func copyMem(x: UintImpl, ret: var openArray[byte]) {.compileTime.} = +func copyMem*(x: UintImpl, ret: var openArray[byte]) {.compileTime.} = const size = bitsof(x) div 8 type DT = type x.leastSignificantWord for i in 0 ..< size: diff --git a/stint/private/int_bitwise_ops.nim b/stint/private/int_bitwise_ops.nim index 4660079..eed62ec 100644 --- a/stint/private/int_bitwise_ops.nim +++ b/stint/private/int_bitwise_ops.nim @@ -7,7 +7,7 @@ # # at your option. This file may not be copied, modified, or distributed except according to those terms. -import ./datatypes, ./bitops2_priv, ./uint_bitwise_ops +import ./datatypes, ./bitops2_priv, ./uint_bitwise_ops, ./compiletime_helpers func `not`*(x: IntImpl): IntImpl {.inline.}= ## Bitwise complement of unsigned integer x @@ -25,21 +25,6 @@ func `xor`*(x, y: IntImpl): IntImpl {.inline.}= ## `Bitwise xor` of numbers x and y applyHiLo(x, y, `xor`) -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 - -template convert[T](x: 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) - func `shl`*(x: IntImpl, y: SomeInteger): IntImpl {.inline.}= ## Compute the `shift left` operation of x and y # Note: inlining this poses codegen/aliasing issue when doing `x = x shl 1`