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.
|
# 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
|
import stew/endians2
|
||||||
export endians2
|
export endians2
|
||||||
|
@ -30,7 +30,10 @@ func fromBytes*[bits: static int](
|
||||||
T: typedesc[StUint[bits]],
|
T: typedesc[StUint[bits]],
|
||||||
x: array[bits div 8, byte],
|
x: array[bits div 8, byte],
|
||||||
endian: Endianness = system.cpuEndian): T {.inline, noinit.} =
|
endian: Endianness = system.cpuEndian): T {.inline, noinit.} =
|
||||||
# TODO compile-time version
|
|
||||||
|
when nimvm:
|
||||||
|
copyFromArray(result.data, x)
|
||||||
|
else:
|
||||||
copyMem(addr result, unsafeAddr x[0], bits div 8)
|
copyMem(addr result, unsafeAddr x[0], bits div 8)
|
||||||
|
|
||||||
if endian != system.cpuEndian:
|
if endian != system.cpuEndian:
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import ./datatypes, ./uint_bitwise_ops
|
import ./datatypes, ./uint_bitwise_ops, ./bitops2_priv
|
||||||
|
|
||||||
func convertImpl[T: SomeInteger](x: SomeInteger): T {.compileTime.} =
|
func convertImpl[T: SomeInteger](x: SomeInteger): T {.compileTime.} =
|
||||||
cast[T](x)
|
cast[T](x)
|
||||||
|
@ -22,3 +22,38 @@ func getByte*(x: SomeInteger, pos: int): byte {.compileTime.} =
|
||||||
func getByte*(x: UintImpl | IntImpl, pos: int): byte {.compileTime.} =
|
func getByte*(x: UintImpl | IntImpl, pos: int): byte {.compileTime.} =
|
||||||
type DT = type x.leastSignificantWord
|
type DT = type x.leastSignificantWord
|
||||||
byte((x shr (pos * 8)).leastSignificantWord and 0xFF.DT)
|
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)
|
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.} =
|
func toBytes*(x: UintImpl, endian: Endianness = system.cpuEndian): auto {.inline.} =
|
||||||
# TODO can't use bitsof in return type (compiler bug?), hence return auto
|
# TODO can't use bitsof in return type (compiler bug?), hence return auto
|
||||||
var ret: array[bitsof(x) div 8, byte]
|
var ret: array[bitsof(x) div 8, byte]
|
||||||
when nimvm:
|
when nimvm:
|
||||||
if endian == system.cpuEndian:
|
if endian == system.cpuEndian:
|
||||||
copyMem(x, ret)
|
copyToArray(ret, x)
|
||||||
else:
|
else:
|
||||||
let v = swapBytes(x)
|
let v = swapBytes(x)
|
||||||
copyMem(v, ret)
|
copyToArray(ret, v)
|
||||||
else:
|
else:
|
||||||
if endian == system.cpuEndian:
|
if endian == system.cpuEndian:
|
||||||
copyMem(addr ret[0], unsafeAddr x, ret.len)
|
copyMem(addr ret[0], unsafeAddr x, ret.len)
|
||||||
|
|
Loading…
Reference in New Issue