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
|
|
|
|
2019-05-09 19:46:40 +00:00
|
|
|
import ./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-10-05 17:28:50 +00:00
|
|
|
func `+`*(x, y: UintImpl): UintImpl {.inline.}
|
|
|
|
# Forward declaration
|
|
|
|
|
|
|
|
func `+=`*(x: var UintImpl, y: UintImpl) {.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
|
2018-10-05 17:28:50 +00:00
|
|
|
x.hi += (x.lo < y.lo).toSubtype(SubTy) + y.hi # This helps the compiler produce ADC (add with carry)
|
2018-04-21 10:12:05 +00:00
|
|
|
|
2018-10-05 17:28:50 +00:00
|
|
|
func `+`*(x, y: UintImpl): UintImpl {.inline.}=
|
2018-04-21 10:12:05 +00:00
|
|
|
# Addition for multi-precision unsigned int
|
|
|
|
result = x
|
|
|
|
result += y
|
|
|
|
|
2018-10-05 17:28:50 +00:00
|
|
|
func `-`*(x, y: UintImpl): UintImpl {.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
|
2018-10-05 17:28:50 +00:00
|
|
|
result.hi = x.hi - y.hi - (x.lo < y.lo).toSubtype(SubTy) # This might (?) help the compiler produce SBB (sub with borrow)
|
2018-04-21 10:12:05 +00:00
|
|
|
|
2018-10-05 17:28:50 +00:00
|
|
|
func `-=`*(x: var UintImpl, y: UintImpl) {.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)
|