tidying up compiletime helpers
This commit is contained in:
parent
f5116945e6
commit
d67cee6f8d
|
@ -7,7 +7,7 @@
|
|||
#
|
||||
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
||||
|
||||
import private/[bitops2_priv, endians2_priv, datatypes]
|
||||
import private/[bitops2_priv, endians2_priv, datatypes, compiletime_helpers]
|
||||
|
||||
import stew/endians2
|
||||
export endians2
|
||||
|
@ -30,8 +30,11 @@ func fromBytes*[bits: static int](
|
|||
T: typedesc[StUint[bits]],
|
||||
x: array[bits div 8, byte],
|
||||
endian: Endianness = system.cpuEndian): T {.inline, noinit.} =
|
||||
# TODO compile-time version
|
||||
copyMem(addr result, unsafeAddr x[0], bits div 8)
|
||||
|
||||
when nimvm:
|
||||
copyFromArray(result.data, x)
|
||||
else:
|
||||
copyMem(addr result, unsafeAddr x[0], bits div 8)
|
||||
|
||||
if endian != system.cpuEndian:
|
||||
result = swapBytes(result)
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import ./datatypes, ./uint_bitwise_ops
|
||||
import ./datatypes, ./uint_bitwise_ops, ./bitops2_priv
|
||||
|
||||
func convertImpl[T: SomeInteger](x: SomeInteger): T {.compileTime.} =
|
||||
cast[T](x)
|
||||
|
@ -22,3 +22,38 @@ func getByte*(x: SomeInteger, pos: int): byte {.compileTime.} =
|
|||
func getByte*(x: UintImpl | IntImpl, pos: int): byte {.compileTime.} =
|
||||
type DT = type x.leastSignificantWord
|
||||
byte((x shr (pos * 8)).leastSignificantWord and 0xFF.DT)
|
||||
|
||||
proc setByte*(x: var SomeInteger, pos: int, b: byte) {.compileTime.} =
|
||||
type DT = type x
|
||||
x = x or (DT(b) shl (pos*8))
|
||||
|
||||
type SomeIntImpl = UintImpl | IntImpl
|
||||
func setByte*(x: var SomeIntImpl, pos: int, b: byte) {.compileTime.} =
|
||||
proc putFirstByte(x: var SomeInteger, b: byte) =
|
||||
type DT = type x
|
||||
x = x or b.DT
|
||||
|
||||
proc putFirstByte(x: var UintImpl, b: byte) =
|
||||
putFirstByte(x.lo, b)
|
||||
|
||||
var cx: type x
|
||||
cx.putFirstByte(b)
|
||||
x = x or (cx shl (pos*8))
|
||||
|
||||
func copyToArray*(ret: var openArray[byte], x: UintImpl) {.compileTime.} =
|
||||
const size = bitsof(x) div 8
|
||||
doAssert ret.len >= size
|
||||
for i in 0 ..< size:
|
||||
ret[i] = x.getByte(i)
|
||||
|
||||
func copyFromArray*(x: var UintImpl, data: openArray[byte]) {.compileTime.} =
|
||||
const size = bitsof(x) div 8
|
||||
doAssert data.len >= size
|
||||
for i in 0 ..< size:
|
||||
x.setByte(i, data[i])
|
||||
|
||||
func copyFromArray*(x: var SomeInteger, data: openArray[byte]) {.compileTime.} =
|
||||
const size = bitsof(x) div 8
|
||||
doAssert data.len >= size
|
||||
for i in 0 ..< size:
|
||||
x.setByte(i, data[i])
|
||||
|
|
|
@ -8,21 +8,15 @@ func swapBytes*(x: UintImpl): UintImpl {.inline.} =
|
|||
|
||||
UintImpl(hi: hi, lo: lo)
|
||||
|
||||
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:
|
||||
ret[i] = x.getByte(i)
|
||||
|
||||
func toBytes*(x: UintImpl, endian: Endianness = system.cpuEndian): auto {.inline.} =
|
||||
# TODO can't use bitsof in return type (compiler bug?), hence return auto
|
||||
var ret: array[bitsof(x) div 8, byte]
|
||||
when nimvm:
|
||||
if endian == system.cpuEndian:
|
||||
copyMem(x, ret)
|
||||
copyToArray(ret, x)
|
||||
else:
|
||||
let v = swapBytes(x)
|
||||
copyMem(v, ret)
|
||||
copyToArray(ret, v)
|
||||
else:
|
||||
if endian == system.cpuEndian:
|
||||
copyMem(addr ret[0], unsafeAddr x, ret.len)
|
||||
|
|
Loading…
Reference in New Issue