Split initialization and conversion proc
This commit is contained in:
parent
40c2215804
commit
b3dedf7824
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
|
@ -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
|
|
@ -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,
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue