nim-stint/stint/private/uint_comparison.nim
Jacek Sieka 9027fbea3e
refactoring away for loop macros
* remove experimental for loop macro usage
* make implementation of several operations follow data structure
(recursive data -> recursive implementation)
* rename getSize -> bitsof to avoid bits vs bytes confusion
* fix potential 32-bit issue where asSignedWords cast to `int` even when
`uint64` was used as storage
* `hi` for signed ints now is signed - this replaces `asSignedWords` and
makes several int operations more natural
* fix bit size assert
2018-10-25 12:58:40 +02:00

43 lines
1.2 KiB
Nim

# 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 ./datatypes
func isZero*(n: SomeUnsignedInt): bool {.inline.} =
n == 0
func isZero*(n: UintImpl): bool {.inline.} =
n.hi.isZero and n.lo.isZero
func `<`*(x, y: UintImpl): bool {.inline.}=
# Lower comparison for multi-precision integers
x.hi < y.hi or
(x.hi == y.hi and x.lo < y.lo)
func `==`*(x, y: UintImpl): bool {.inline.}=
# Equal comparison for multi-precision integers
x.hi == y.hi and x.lo == y.lo
func `<=`*(x, y: UintImpl): bool {.inline.}=
# Lower or equal comparison for multi-precision integers
x.hi < y.hi or
(x.hi == y.hi and x.lo <= y.lo)
func isEven*(x: SomeUnsignedInt): bool {.inline.} =
(x and 1) == 0
func isEven*(x: UintImpl): bool {.inline.}=
x.lo.isEven
func isOdd*(x: SomeUnsignedInt): bool {.inline.} =
not x.isEven
func isOdd*(x: UintImpl): bool {.inline.}=
not x.isEven