nim-stint/stint/private/int_comparison.nim

60 lines
1.7 KiB
Nim
Raw Normal View History

# 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, ./bithacks, ./as_words,
./bithacks
func isZero*(n: SomeSignedInt): bool {.inline.} =
n == 0
func isZero*(n: IntImpl): bool {.inline.} =
2018-09-06 09:35:12 +00:00
{.push experimental: "forLoopMacros".}
for word in asWords(n):
if word != 0:
return false
2018-09-06 09:35:12 +00:00
{.pop.}
return true
func isNegative*(n: IntImpl): bool {.inline.} =
## Returns true if a number is negative:
n.isMsbSet
2018-04-25 15:02:44 +00:00
func `<`*(x, y: IntImpl): bool {.inline.}=
# Lower comparison for multi-precision integers
2018-09-06 09:35:12 +00:00
{.push experimental: "forLoopMacros".}
for wx, wy in asSignedWords(x, y):
if wx != wy:
return wx < wy
2018-09-06 09:35:12 +00:00
{.pop.}
2018-04-25 15:02:44 +00:00
return false # they're equal
func `==`*(x, y: IntImpl): bool {.inline.}=
2018-04-25 15:02:44 +00:00
# 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-25 15:02:44 +00:00
return false
2018-09-06 09:35:12 +00:00
{.pop.}
2018-04-25 15:02:44 +00:00
return true # they're equal
func `<=`*(x, y: IntImpl): bool {.inline.}=
2018-04-25 15:02:44 +00:00
# Lower or equal comparison for multi-precision integers
2018-09-06 09:35:12 +00:00
{.push experimental: "forLoopMacros".}
for wx, wy in asSignedWords(x, y):
if wx != wy:
return wx < wy
2018-09-06 09:35:12 +00:00
{.pop.}
2018-04-25 15:02:44 +00:00
return true # they're equal
func isOdd*(x: IntImpl): bool {.inline.}=
2018-10-08 17:55:37 +00:00
bool(x.leastSignificantWord and 1)
func isEven*(x: IntImpl): bool {.inline.}=
not x.isOdd