2018-04-25 19:21:25 +00:00
|
|
|
# Stint
|
2018-03-26 09:46:24 +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-04-25 12:27:55 +00:00
|
|
|
import ./private/datatypes, macros
|
|
|
|
export StUint, UintImpl, uintImpl # TODO remove the need to export UintImpl and this macro
|
2018-04-30 11:38:55 +00:00
|
|
|
{.pragma: fooPragma.}
|
2018-03-26 09:46:24 +00:00
|
|
|
type
|
2018-04-25 10:52:00 +00:00
|
|
|
UInt128* = StUint[128]
|
|
|
|
UInt256* = StUint[256]
|
2018-03-26 09:46:24 +00:00
|
|
|
|
2018-03-26 10:45:10 +00:00
|
|
|
template make_conv(conv_name: untyped, size: int): untyped =
|
2018-04-30 11:38:55 +00:00
|
|
|
func `convname`*(n: SomeInteger): StUint[size] {.inline, fooPragma.}=
|
2018-04-25 11:36:56 +00:00
|
|
|
n.stuint(size)
|
2018-03-26 10:45:10 +00:00
|
|
|
|
|
|
|
make_conv(u128, 128)
|
|
|
|
make_conv(u256, 256)
|
|
|
|
|
2018-03-26 09:46:24 +00:00
|
|
|
template make_unary(op, ResultTy): untyped =
|
2018-04-30 11:38:55 +00:00
|
|
|
func `op`*(x: StUint): ResultTy {.fooPragma, inline.} =
|
2018-04-25 10:52:00 +00:00
|
|
|
when ResultTy is StUint:
|
2018-03-26 09:46:24 +00:00
|
|
|
result.data = op(x.data)
|
|
|
|
else:
|
|
|
|
op(x.data)
|
|
|
|
export op
|
|
|
|
|
|
|
|
template make_binary(op, ResultTy): untyped =
|
2018-04-30 11:38:55 +00:00
|
|
|
func `op`*(x, y: StUint): ResultTy {.fooPragma, inline.} =
|
2018-04-25 10:52:00 +00:00
|
|
|
when ResultTy is StUint:
|
2018-03-26 09:46:24 +00:00
|
|
|
result.data = op(x.data, y.data)
|
|
|
|
else:
|
|
|
|
op(x.data, y.data)
|
|
|
|
export `op`
|
|
|
|
|
|
|
|
template make_binary_inplace(op): untyped =
|
2018-04-25 10:52:00 +00:00
|
|
|
func `op`*(x: var StUint, y: StUint) {.inline.} =
|
2018-03-26 09:46:24 +00:00
|
|
|
op(x.data, y.data)
|
|
|
|
export op
|
|
|
|
|
2018-04-21 10:12:05 +00:00
|
|
|
import ./private/uint_addsub
|
2018-03-26 09:46:24 +00:00
|
|
|
|
2018-04-25 10:52:00 +00:00
|
|
|
make_binary(`+`, StUint)
|
2018-03-26 09:46:24 +00:00
|
|
|
make_binary_inplace(`+=`)
|
2018-04-25 10:52:00 +00:00
|
|
|
make_binary(`-`, StUint)
|
2018-03-26 09:46:24 +00:00
|
|
|
make_binary_inplace(`-=`)
|
2018-04-21 10:12:05 +00:00
|
|
|
|
|
|
|
import ./private/uint_mul
|
2018-04-25 10:52:00 +00:00
|
|
|
make_binary(`*`, StUint)
|
2018-04-20 09:13:47 +00:00
|
|
|
|
2018-04-21 10:12:05 +00:00
|
|
|
import ./private/uint_div
|
2018-04-20 09:13:47 +00:00
|
|
|
|
2018-04-25 10:52:00 +00:00
|
|
|
make_binary(`div`, StUint)
|
|
|
|
make_binary(`mod`, StUint)
|
2018-04-30 11:38:55 +00:00
|
|
|
func divmod*(x, y: StUint): tuple[quot, rem: StUint] {.fooPragma, inline.} =
|
2018-03-26 09:46:24 +00:00
|
|
|
(result.quot.data, result.rem.data) = divmod(x.data, y.data)
|
|
|
|
|
|
|
|
import ./private/uint_comparison
|
|
|
|
|
|
|
|
make_binary(`<`, bool)
|
|
|
|
make_binary(`<=`, bool)
|
2018-04-21 10:12:05 +00:00
|
|
|
make_binary(`==`, bool)
|
2018-04-25 10:52:00 +00:00
|
|
|
func isZero*(x: StUint): bool {.inline.} = isZero x.data
|
2018-03-26 09:46:24 +00:00
|
|
|
|
|
|
|
import ./private/uint_bitwise_ops
|
|
|
|
|
2018-04-25 10:52:00 +00:00
|
|
|
make_unary(`not`, StUint)
|
|
|
|
make_binary(`or`, StUint)
|
|
|
|
make_binary(`and`, StUint)
|
|
|
|
make_binary(`xor`, StUint)
|
2018-04-30 11:38:55 +00:00
|
|
|
proc `shr`*(x: StUint, y: SomeInteger): StUint {.fooPragma, inline, noSideEffect.} =
|
2018-03-26 09:46:24 +00:00
|
|
|
result.data = x.data shr y
|
2018-04-30 11:38:55 +00:00
|
|
|
proc `shl`*(x: StUint, y: SomeInteger): StUint {.fooPragma, inline, noSideEffect.} =
|
2018-03-26 09:46:24 +00:00
|
|
|
result.data = x.data shl y
|
2018-05-06 20:19:00 +00:00
|
|
|
|
|
|
|
func high*[bits: static[int]](_: typedesc[Stuint[bits]]): Stuint[bits] {.inline.} =
|
|
|
|
result.data = high(type result.data)
|
|
|
|
|
|
|
|
func low*[bits: static[int]](_: typedesc[Stuint[bits]]): Stuint[bits] {.inline.} =
|
|
|
|
result.data = low(type result.data)
|