2018-04-25 19:21:25 +00:00
|
|
|
# Stint
|
2018-04-21 10:12:05 +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-05-06 20:29:08 +00:00
|
|
|
|
2018-04-30 11:38:55 +00:00
|
|
|
import ./bithacks, ./conversion, ./initialization,
|
2018-04-25 12:27:55 +00:00
|
|
|
./datatypes,
|
2018-04-21 10:12:05 +00:00
|
|
|
./uint_comparison,
|
|
|
|
./uint_bitwise_ops
|
|
|
|
|
|
|
|
# ############ Addition & Substraction ############ #
|
|
|
|
|
2018-04-25 10:52:00 +00:00
|
|
|
proc `+=`*(x: var UintImpl, y: UintImpl) {.noSideEffect, inline.}=
|
2018-04-21 10:12:05 +00:00
|
|
|
## In-place addition for multi-precision unsigned int
|
|
|
|
|
|
|
|
type SubTy = type x.lo
|
|
|
|
x.lo += y.lo
|
|
|
|
x.hi += (x.lo < y.lo).toSubtype(SubTy) + y.hi
|
|
|
|
|
2018-05-06 20:29:08 +00:00
|
|
|
proc `+`*(x, y: UintImpl): UintImpl {.noSideEffect, inline.}=
|
2018-04-21 10:12:05 +00:00
|
|
|
# Addition for multi-precision unsigned int
|
|
|
|
result = x
|
|
|
|
result += y
|
|
|
|
|
2018-05-06 20:29:08 +00:00
|
|
|
proc `-`*(x, y: UintImpl): UintImpl {.noSideEffect, inline.}=
|
2018-04-21 10:12:05 +00:00
|
|
|
# Substraction for multi-precision unsigned int
|
|
|
|
|
|
|
|
type SubTy = type x.lo
|
|
|
|
result.lo = x.lo - y.lo
|
|
|
|
result.hi = x.hi - y.hi - (x.lo < y.lo).toSubtype(SubTy)
|
|
|
|
|
2018-04-25 10:52:00 +00:00
|
|
|
proc `-=`*(x: var UintImpl, y: UintImpl) {.noSideEffect, inline.}=
|
2018-04-21 10:12:05 +00:00
|
|
|
## In-place substraction for multi-precision unsigned int
|
|
|
|
x = x - y
|
2018-04-30 11:38:55 +00:00
|
|
|
|
|
|
|
func inc*(x: var UintImpl){.inline.}=
|
|
|
|
x += one(type x)
|