2018-04-25 21:21:58 +02:00
|
|
|
# Stint
|
2018-04-25 14:43:24 +02: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-06-18 16:44:29 +02:00
|
|
|
import ./datatypes, ./bithacks, ./as_words,
|
2018-04-25 15:41:59 +02:00
|
|
|
./bithacks
|
2018-04-25 14:43:24 +02:00
|
|
|
|
2018-04-25 15:41:59 +02:00
|
|
|
func isZero*(n: SomeSignedInt): bool {.inline.} =
|
2018-04-25 14:43:24 +02:00
|
|
|
n == 0
|
|
|
|
|
2018-04-25 15:41:59 +02:00
|
|
|
func isZero*(n: IntImpl): bool {.inline.} =
|
2018-09-06 12:35:12 +03:00
|
|
|
{.push experimental: "forLoopMacros".}
|
2018-06-18 12:54:25 +02:00
|
|
|
for word in asWords(n):
|
|
|
|
if word != 0:
|
2018-04-25 14:43:24 +02:00
|
|
|
return false
|
2018-09-06 12:35:12 +03:00
|
|
|
{.pop.}
|
2018-04-25 14:43:24 +02:00
|
|
|
return true
|
|
|
|
|
2018-04-25 15:41:59 +02:00
|
|
|
func isNegative*(n: IntImpl): bool {.inline.} =
|
|
|
|
## Returns true if a number is negative:
|
2018-06-18 12:54:25 +02:00
|
|
|
n.isMsbSet
|
2018-04-25 17:02:44 +02:00
|
|
|
|
|
|
|
func `<`*(x, y: IntImpl): bool {.inline.}=
|
|
|
|
# Lower comparison for multi-precision integers
|
2018-09-06 12:35:12 +03:00
|
|
|
{.push experimental: "forLoopMacros".}
|
2018-06-18 16:44:29 +02:00
|
|
|
for wx, wy in asSignedWords(x, y):
|
|
|
|
if wx != wy:
|
|
|
|
return wx < wy
|
2018-09-06 12:35:12 +03:00
|
|
|
{.pop.}
|
2018-04-25 17:02:44 +02:00
|
|
|
return false # they're equal
|
|
|
|
|
2018-04-25 21:21:58 +02:00
|
|
|
func `==`*(x, y: IntImpl): bool {.inline.}=
|
2018-04-25 17:02:44 +02:00
|
|
|
# Equal comparison for multi-precision integers
|
2018-09-06 12:35:12 +03:00
|
|
|
{.push experimental: "forLoopMacros".}
|
2018-06-18 12:54:25 +02:00
|
|
|
for wx, wy in asWords(x, y):
|
|
|
|
if wx != wy:
|
2018-04-25 17:02:44 +02:00
|
|
|
return false
|
2018-09-06 12:35:12 +03:00
|
|
|
{.pop.}
|
2018-04-25 17:02:44 +02:00
|
|
|
return true # they're equal
|
|
|
|
|
2018-04-25 21:21:58 +02:00
|
|
|
func `<=`*(x, y: IntImpl): bool {.inline.}=
|
2018-04-25 17:02:44 +02:00
|
|
|
# Lower or equal comparison for multi-precision integers
|
2018-09-06 12:35:12 +03:00
|
|
|
{.push experimental: "forLoopMacros".}
|
2018-06-18 16:44:29 +02:00
|
|
|
for wx, wy in asSignedWords(x, y):
|
|
|
|
if wx != wy:
|
|
|
|
return wx < wy
|
2018-09-06 12:35:12 +03:00
|
|
|
{.pop.}
|
2018-04-25 17:02:44 +02:00
|
|
|
return true # they're equal
|
2018-05-16 10:41:46 +02:00
|
|
|
|
|
|
|
func isOdd*(x: IntImpl): bool {.inline.}=
|
|
|
|
bool(x.least_significant_word and 1)
|
|
|
|
|
|
|
|
func isEven*(x: IntImpl): bool {.inline.}=
|
|
|
|
not x.isOdd
|