diff --git a/stint.nim b/stint.nim index 900d2b1..f13f275 100644 --- a/stint.nim +++ b/stint.nim @@ -7,8 +7,8 @@ # # at your option. This file may not be copied, modified, or distributed except according to those terms. -import stint/[uint_public, int_public, io, modular_arithmetic, literals_stint] -export uint_public, int_public, io, modular_arithmetic, literals_stint +import stint/[bitops2, endians2, intops, io, modular_arithmetic, literals_stint] +export bitops2, endians2, intops, io, modular_arithmetic, literals_stint type Int128* = Stint[128] diff --git a/stint/bitops2.nim b/stint/bitops2.nim new file mode 100644 index 0000000..9bad7c8 --- /dev/null +++ b/stint/bitops2.nim @@ -0,0 +1,16 @@ +# Stint +# Copyright 2018 Status Research & Development GmbH +# Licensed under either of +# +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0) +# * MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT) +# +# at your option. This file may not be copied, modified, or distributed except according to those terms. + +import ./private/[bitops2_priv, datatypes] + +func countOnes*(x: StUint): int {.inline.} = countOnes(x.data) +func parity*(x: StUint): int {.inline.} = parity(x.data) +func firstOne*(x: StUint): int {.inline.} = firstOne(x.data) +func leadingZeros*(x: StUint): int {.inline.} = leadingZeros(x.data) +func trailingZeros*(x: StUint): int {.inline.} = trailingZeros(x.data) diff --git a/stint/endians2.nim b/stint/endians2.nim new file mode 100644 index 0000000..e13f87e --- /dev/null +++ b/stint/endians2.nim @@ -0,0 +1,105 @@ +# Stint +# Copyright 2018 Status Research & Development GmbH +# Licensed under either of +# +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0) +# * MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT) +# +# 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 stew/endians2 +export endians2 + +func swapBytes*(x: StUint): StUint {.inline.} = StUint(data: swapBytes(x.data)) + +func toBytes*[bits: static int](x: StUint[bits], endian: Endianness = system.cpuEndian): + array[bits div 8, byte] {.inline.} = + toBytes(x.data, endian) + +func toBytesLE*[bits: static int](x: StUint[bits]): + array[bits div 8, byte] {.inline.} = + toBytes(x, littleEndian) + +func toBytesBE*[bits: static int](x: StUint[bits]): + array[bits div 8, byte] {.inline.} = + toBytes(x, bigEndian) + +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) + + if endian != system.cpuEndian: + result = swapBytes(result) + +func fromBytes*[bits: static int]( + T: typedesc[StUint[bits]], + x: openArray[byte], + endian: Endianness = system.cpuEndian): T {.inline.} = + # TODO fromBytesBE in io.nim handles this better, merge the two! + var tmp: array[bits div 8, byte] + if x.len < tmp.len: + let offset = if endian == bigEndian: tmp.len - x.len else: 0 + for i in 0..