2018-04-25 19:21:58 +00:00
|
|
|
# Stint
|
2018-04-25 12:43:24 +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-04-25 15:02:44 +00:00
|
|
|
import ./datatypes, ./bithacks, ./as_words, ./as_signed_words,
|
2018-04-25 13:41:59 +00:00
|
|
|
./bithacks
|
2018-04-25 12:43:24 +00:00
|
|
|
|
2018-04-25 13:41:59 +00:00
|
|
|
func isZero*(n: SomeSignedInt): bool {.inline.} =
|
2018-04-25 12:43:24 +00:00
|
|
|
n == 0
|
|
|
|
|
2018-04-25 13:41:59 +00:00
|
|
|
func isZero*(n: IntImpl): bool {.inline.} =
|
2018-04-25 12:43:24 +00:00
|
|
|
asWords(n, ignoreEndianness = true):
|
|
|
|
if n != 0:
|
|
|
|
return false
|
|
|
|
return true
|
|
|
|
|
2018-04-25 13:41:59 +00:00
|
|
|
func isNegative*(n: IntImpl): bool {.inline.} =
|
|
|
|
## Returns true if a number is negative:
|
|
|
|
n.msb.bool
|
2018-04-25 15:02:44 +00:00
|
|
|
|
|
|
|
func `<`*(x, y: IntImpl): bool {.inline.}=
|
|
|
|
# Lower comparison for multi-precision integers
|
|
|
|
asSignedWordsZip(x, y):
|
|
|
|
if x != y:
|
|
|
|
return x < y
|
|
|
|
return false # they're equal
|
|
|
|
|
2018-04-25 19:21:58 +00:00
|
|
|
func `==`*(x, y: IntImpl): bool {.inline.}=
|
2018-04-25 15:02:44 +00:00
|
|
|
# Equal comparison for multi-precision integers
|
|
|
|
asWordsZip(x, y, ignoreEndianness = true):
|
|
|
|
if x != y:
|
|
|
|
return false
|
|
|
|
return true # they're equal
|
|
|
|
|
2018-04-25 19:21:58 +00:00
|
|
|
func `<=`*(x, y: IntImpl): bool {.inline.}=
|
2018-04-25 15:02:44 +00:00
|
|
|
# Lower or equal comparison for multi-precision integers
|
|
|
|
asSignedWordsZip(x, y):
|
|
|
|
if x != y:
|
|
|
|
return x < y
|
|
|
|
return true # they're equal
|
2018-05-16 08:41:46 +00:00
|
|
|
|
|
|
|
func isOdd*(x: IntImpl): bool {.inline.}=
|
|
|
|
bool(x.least_significant_word and 1)
|
|
|
|
|
|
|
|
func isEven*(x: IntImpl): bool {.inline.}=
|
|
|
|
not x.isOdd
|