nim-stint/stint/private/uint_comparison.nim

55 lines
1.5 KiB
Nim
Raw Normal View History

# Stint
2018-03-02 10:48:08 +00:00
# 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
2018-02-17 16:57:26 +00:00
func isZero*(n: SomeUnsignedInt): bool {.inline.} =
n == 0
2018-02-17 16:57:26 +00:00
2018-04-25 10:52:00 +00:00
func isZero*(n: UintImpl): bool {.inline.} =
2018-09-06 09:35:12 +00:00
{.push experimental: "forLoopMacros".}
for word in asWords(n):
if word != 0:
2018-04-21 11:50:43 +00:00
return false
2018-09-06 09:35:12 +00:00
{.pop.}
2018-04-21 11:50:43 +00:00
return true
2018-04-25 10:52:00 +00:00
func `<`*(x, y: UintImpl): bool {.inline.}=
2018-04-21 11:50:43 +00:00
# Lower comparison for multi-precision integers
2018-09-06 09:35:12 +00:00
{.push experimental: "forLoopMacros".}
for wx, wy in asWords(x, y):
if wx != wy:
return wx < wy
2018-09-06 09:35:12 +00:00
{.pop.}
2018-04-21 11:50:43 +00:00
return false # they're equal
2018-04-25 10:52:00 +00:00
func `==`*(x, y: UintImpl): bool {.inline.}=
# Equal comparison for multi-precision integers
2018-09-06 09:35:12 +00:00
{.push experimental: "forLoopMacros".}
for wx, wy in asWords(x, y):
if wx != wy:
2018-04-21 11:50:43 +00:00
return false
2018-09-06 09:35:12 +00:00
{.pop.}
2018-04-21 11:50:43 +00:00
return true # they're equal
2018-04-25 10:52:00 +00:00
func `<=`*(x, y: UintImpl): bool {.inline.}=
# Lower or equal comparison for multi-precision integers
2018-09-06 09:35:12 +00:00
{.push experimental: "forLoopMacros".}
for wx, wy in asWords(x, y):
if wx != wy:
return wx < wy
2018-09-06 09:35:12 +00:00
{.pop.}
return true # they're equal
func isOdd*(x: UintImpl): bool {.inline.}=
bool(x.least_significant_word and 1)
func isEven*(x: UintImpl): bool {.inline.}=
not x.isOdd