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.
|
|
|
|
|
2019-05-09 13:46:40 -06:00
|
|
|
import ./datatypes, ./uint_comparison
|
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-10-25 12:58:40 +02:00
|
|
|
n.hi.isZero and n.lo.isZero
|
|
|
|
|
|
|
|
func isNegative*(n: SomeSignedInt): bool {.inline.} =
|
|
|
|
n < 0
|
2018-04-25 14:43:24 +02:00
|
|
|
|
2018-04-25 15:41:59 +02:00
|
|
|
func isNegative*(n: IntImpl): bool {.inline.} =
|
|
|
|
## Returns true if a number is negative:
|
2018-10-25 12:58:40 +02:00
|
|
|
n.hi.isNegative
|
2018-04-25 17:02:44 +02:00
|
|
|
|
|
|
|
func `<`*(x, y: IntImpl): bool {.inline.}=
|
|
|
|
# Lower comparison for multi-precision integers
|
2018-10-25 12:58:40 +02:00
|
|
|
x.hi < y.hi or
|
|
|
|
(x.hi == y.hi and x.lo < y.lo)
|
2018-04-25 17:02:44 +02:00
|
|
|
|
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-10-25 12:58:40 +02:00
|
|
|
x.hi == y.hi and x.lo == y.lo
|
2018-04-25 17:02:44 +02:00
|
|
|
|
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-10-25 12:58:40 +02:00
|
|
|
x.hi < y.hi or
|
|
|
|
(x.hi == y.hi and x.lo <= y.lo)
|
2018-05-16 10:41:46 +02:00
|
|
|
|
2019-07-22 09:53:58 +02:00
|
|
|
func isOdd*(x: SomeSignedInt): bool {.inline.}=
|
|
|
|
bool(x and 1)
|
|
|
|
|
|
|
|
func isEven*(x: SomeSignedInt): bool {.inline.}=
|
|
|
|
not x.isOdd
|
|
|
|
|
2018-05-16 10:41:46 +02:00
|
|
|
func isEven*(x: IntImpl): bool {.inline.}=
|
2018-10-25 12:58:40 +02:00
|
|
|
x.lo.isEven
|
|
|
|
|
|
|
|
func isOdd*(x: IntImpl): bool {.inline.}=
|
|
|
|
not x.isEven
|