diff --git a/src/mpint.nim b/src/mpint.nim index 7842cc2..4dd8f10 100644 --- a/src/mpint.nim +++ b/src/mpint.nim @@ -4,9 +4,11 @@ import ./uint_type, ./uint_init, ./uint_bitwise_ops, - ./uint_binary_ops + ./uint_binary_ops, + ./uint_comparison export uint_type, uint_init, uint_bitwise_ops, - uint_binary_ops \ No newline at end of file + uint_binary_ops, + uint_comparison \ No newline at end of file diff --git a/src/uint_binary_ops.nim b/src/uint_binary_ops.nim index c77a3ef..cd552a5 100644 --- a/src/uint_binary_ops.nim +++ b/src/uint_binary_ops.nim @@ -2,7 +2,8 @@ # Distributed under the MIT License (license terms are at http://opensource.org/licenses/MIT). import ./private/utils, - uint_type + uint_type, + uint_comparison proc `+=`*[T: MpUint](x: var T, y: T) {.noSideEffect.}= ## In-place addition for multi-precision unsigned int diff --git a/src/uint_bitwise_ops.nim b/src/uint_bitwise_ops.nim index 1e6fd84..3aafe8f 100644 --- a/src/uint_bitwise_ops.nim +++ b/src/uint_bitwise_ops.nim @@ -25,7 +25,7 @@ proc `xor`*(x, y: MpUint): MpUint {.noInit, noSideEffect, inline.}= result.lo = x.lo xor y.lo result.hi = x.hi xor y.hi -proc `shl`*[T: MpUint](x: T, y: SomeInteger): T {.noInit, noSideEffect.} +proc `shr`*[T: MpUint](x: T, y: SomeInteger): T {.noInit, noSideEffect.} # Forward declaration proc `shl`*[T: MpUint](x: T, y: SomeInteger): T {.noInit, noSideEffect.}= diff --git a/src/uint_comparison.nim b/src/uint_comparison.nim new file mode 100644 index 0000000..6206d4a --- /dev/null +++ b/src/uint_comparison.nim @@ -0,0 +1,13 @@ +# Copyright (c) 2018 Status Research & Development GmbH +# Distributed under the MIT License (license terms are at http://opensource.org/licenses/MIT). + +import ./uint_type + +proc `<`*[T: MpUint](x, y: T): bool {.noSideEffect, noInit, inline.}= + (x.hi < y.hi) or ((x.hi == y.hi) and x.lo < y.lo) + +proc `<=`*[T: MpUint](x, y: T): bool {.noSideEffect, noInit, inline.}= + # Lower or equal comparison for multi-precision integers + if x == y: + return true + x < y diff --git a/tests/all_tests.nim b/tests/all_tests.nim index 0a007da..a49e60d 100644 --- a/tests/all_tests.nim +++ b/tests/all_tests.nim @@ -3,4 +3,5 @@ import test_endianness, test_addsub, - test_mul \ No newline at end of file + test_mul, + test_comparison \ No newline at end of file diff --git a/tests/test_comparison.nim b/tests/test_comparison.nim new file mode 100644 index 0000000..4019826 --- /dev/null +++ b/tests/test_comparison.nim @@ -0,0 +1,34 @@ +# Copyright (c) 2018 Status Research & Development GmbH +# Distributed under the MIT License (license terms are at http://opensource.org/licenses/MIT). + +import ../src/mpint, unittest + +suite "Testing comparison operators": + let + a = initMpUint(10, uint8) + b = initMpUint(15, uint8) + c = 150'u16 + + test "< operator": + check: a < b + check: not (a + b < b) + check: not (a + a + a < b + b) + check: not (a * b < cast[Mpuint[uint8]](c)) + + test "<= operator": + check: a <= b + check: not (a + b <= b) + check: a + a + a <= b + b + check: a * b <= cast[Mpuint[uint8]](c) + + test "> operator": + check: b > a + check: not (b > a + b) + check: not (b + b > a + a + a) + check: not (cast[Mpuint[uint8]](c) > a * b) + + test ">= operator": + check: b >= a + check: not (b >= a + b) + check: b + b >= a + a + a + check: cast[Mpuint[uint8]](c) >= a * b \ No newline at end of file