2018-04-25 19:21:25 +00:00
|
|
|
# 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.
|
2018-02-16 16:48:54 +00:00
|
|
|
|
2018-04-25 12:27:55 +00:00
|
|
|
import ./datatypes, ./as_words
|
2018-02-17 16:57:26 +00:00
|
|
|
|
2018-04-21 10:12:05 +00:00
|
|
|
func isZero*(n: SomeUnsignedInt): bool {.inline.} =
|
2018-03-26 10:45:10 +00:00
|
|
|
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".}
|
2018-06-18 10:54:25 +00:00
|
|
|
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".}
|
2018-06-18 10:54:25 +00:00
|
|
|
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.}=
|
2018-03-26 14:05:19 +00:00
|
|
|
# Equal comparison for multi-precision integers
|
2018-09-06 09:35:12 +00:00
|
|
|
{.push experimental: "forLoopMacros".}
|
2018-06-18 10:54:25 +00:00
|
|
|
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-03-26 14:05:19 +00:00
|
|
|
|
2018-04-25 10:52:00 +00:00
|
|
|
func `<=`*(x, y: UintImpl): bool {.inline.}=
|
2018-03-26 14:05:19 +00:00
|
|
|
# Lower or equal comparison for multi-precision integers
|
2018-09-06 09:35:12 +00:00
|
|
|
{.push experimental: "forLoopMacros".}
|
2018-06-18 10:54:25 +00:00
|
|
|
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 15:21:14 +00:00
|
|
|
return true # they're equal
|
2018-05-16 08:41:46 +00:00
|
|
|
|
|
|
|
func isOdd*(x: UintImpl): bool {.inline.}=
|
|
|
|
bool(x.least_significant_word and 1)
|
|
|
|
|
|
|
|
func isEven*(x: UintImpl): bool {.inline.}=
|
|
|
|
not x.isOdd
|