2019-10-09 08:51:53 +07:00
|
|
|
import ./bitops2_priv, ./datatypes, ./compiletime_helpers
|
2019-07-22 09:53:58 +02:00
|
|
|
import stew/endians2
|
|
|
|
export endians2
|
|
|
|
|
|
|
|
func swapBytes*(x: UintImpl): UintImpl {.inline.} =
|
|
|
|
let lo = swapBytes(x.hi)
|
|
|
|
let hi = swapBytes(x.lo)
|
|
|
|
|
|
|
|
UintImpl(hi: hi, lo: lo)
|
|
|
|
|
|
|
|
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]
|
2019-10-08 20:40:27 +07:00
|
|
|
when nimvm:
|
|
|
|
if endian == system.cpuEndian:
|
2019-10-09 10:24:34 +07:00
|
|
|
copyToArray(ret, x)
|
2019-10-08 20:40:27 +07:00
|
|
|
else:
|
|
|
|
let v = swapBytes(x)
|
2019-10-09 10:24:34 +07:00
|
|
|
copyToArray(ret, v)
|
2019-07-22 09:53:58 +02:00
|
|
|
else:
|
2019-10-08 20:40:27 +07:00
|
|
|
if endian == system.cpuEndian:
|
|
|
|
copyMem(addr ret[0], unsafeAddr x, ret.len)
|
|
|
|
else:
|
|
|
|
let v = swapBytes(x)
|
|
|
|
copyMem(addr ret[0], unsafeAddr v, ret.len)
|
2019-07-22 09:53:58 +02:00
|
|
|
ret
|