move compile time convertImpl to compiletile helpers

This commit is contained in:
andri lim 2019-10-09 09:00:42 +07:00 committed by zah
parent affbe30b41
commit f5116945e6
3 changed files with 17 additions and 17 deletions

View File

@ -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)

View File

@ -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:

View File

@ -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`