diff --git a/src/private/conversion.nim b/src/private/conversion.nim index 821184f..3f6847c 100644 --- a/src/private/conversion.nim +++ b/src/private/conversion.nim @@ -7,27 +7,7 @@ # # at your option. This file may not be copied, modified, or distributed except according to those terms. -import ./datatypes, - macros, typetraits - -func initUintImpl*[InType, OutType](x: InType, _: typedesc[OutType]): OutType {.inline.} = - - const - size_in = getSize(x) - size_out = getSize(result) - - static: - assert size_out >= size_in, "The result type size (" & $size_out & - " for " & $OutType.name & - ") should be equal or bigger than the input type size (" & $size_in & - " for " & $InType.name & ")." - - when OutType is SomeUnsignedInt: - result = x.OutType - elif size_in == size_out: - result = cast[type result](x) - else: - result.lo = initUintImpl(x, type result.lo) +import ./datatypes func toSubtype*[T: SomeInteger](b: bool, _: typedesc[T]): T {.inline.}= b.T @@ -36,19 +16,6 @@ func toSubtype*[T: UintImpl](b: bool, _: typedesc[T]): T {.inline.}= type SubTy = type result.lo result.lo = toSubtype(b, SubTy) -func zero*[T: BaseUint](_: typedesc[T]): T {.inline.}= - discard - -func one*[T: BaseUint](_: typedesc[T]): T {.inline.}= - when T is SomeUnsignedInt: - result = T(1) - else: - let r_ptr = cast[ptr array[getSize(result) div 8, byte]](result.addr) - when system.cpuEndian == bigEndian: - r_ptr[0] = 1 - else: - r_ptr[r_ptr[].len - 1] = 1 - func toUint*(n: UintImpl): auto {.inline.}= ## Casts a multiprecision integer to an uint of the same size diff --git a/src/private/initialization.nim b/src/private/initialization.nim new file mode 100644 index 0000000..46ccd13 --- /dev/null +++ b/src/private/initialization.nim @@ -0,0 +1,42 @@ +# Mpint +# 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 ./datatypes, typetraits + +func initUintImpl*[InType, OutType](x: InType, _: typedesc[OutType]): OutType {.inline.} = + + const + size_in = getSize(x) + size_out = getSize(result) + + static: + assert size_out >= size_in, "The result type size (" & $size_out & + " for " & $OutType.name & + ") should be equal or bigger than the input type size (" & $size_in & + " for " & $InType.name & ")." + + when OutType is SomeUnsignedInt: + result = x.OutType + elif size_in == size_out: + result = cast[type result](x) + else: + result.lo = initUintImpl(x, type result.lo) + +func zero*[T: BaseUint](_: typedesc[T]): T {.inline.}= + discard + +func one*[T: BaseUint](_: typedesc[T]): T {.inline.}= + when T is SomeUnsignedInt: + result = T(1) + else: + let r_ptr = cast[ptr array[getSize(result) div 8, byte]](result.addr) + when system.cpuEndian == bigEndian: + r_ptr[0] = 1 + else: + r_ptr[r_ptr[].len - 1] = 1 diff --git a/src/private/int_comparison.nim b/src/private/int_comparison.nim new file mode 100644 index 0000000..8087682 --- /dev/null +++ b/src/private/int_comparison.nim @@ -0,0 +1,40 @@ +# Mpint +# 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 ./datatypes, ./as_words, ./uint_comparison + +func isZero*(n: SomeUnsignedInt): bool {.inline.} = + n == 0 + +func isZero*(n: UintImpl): bool {.inline.} = + asWords(n, ignoreEndianness = true): + if n != 0: + return false + return true + +func `<`*(x, y: UintImpl): bool {.inline.}= + # Lower comparison for multi-precision integers + asWordsZip(x, y, ignoreEndianness = false): + if x != y: + return x < y + return false # they're equal + +func `==`*(x, y: UintImpl): bool {.inline.}= + # Equal comparison for multi-precision integers + asWordsZip(x, y, ignoreEndianness = true): + if x != y: + return false + return true # they're equal + +func `<=`*(x, y: UintImpl): bool {.inline.}= + # Lower or equal comparison for multi-precision integers + asWordsZip(x, y, ignoreEndianness = false): + if x != y: + return x < y + return true # they're equal diff --git a/src/private/uint_div.nim b/src/private/uint_div.nim index fb65639..d801dad 100644 --- a/src/private/uint_div.nim +++ b/src/private/uint_div.nim @@ -7,7 +7,7 @@ # # at your option. This file may not be copied, modified, or distributed except according to those terms. -import ./bithacks, ./conversion, +import ./bithacks, ./conversion, ./initialization, ./datatypes, ./uint_comparison, ./uint_bitwise_ops, diff --git a/src/private/uint_mul.nim b/src/private/uint_mul.nim index e60aad5..cf43162 100644 --- a/src/private/uint_mul.nim +++ b/src/private/uint_mul.nim @@ -8,6 +8,7 @@ # at your option. This file may not be copied, modified, or distributed except according to those terms. import ./conversion, + ./initialization, ./datatypes, ./uint_comparison, ./uint_addsub