nim-stint/src/uint_public.nim

87 lines
2.5 KiB
Nim
Raw Normal View History

# Stint
# 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.
import ./private/datatypes, macros
export StUint, UintImpl, uintImpl # TODO remove the need to export UintImpl and this macro
{.pragma: fooPragma.}
type
2018-04-25 10:52:00 +00:00
UInt128* = StUint[128]
UInt256* = StUint[256]
template make_conv(conv_name: untyped, size: int): untyped =
func `convname`*(n: SomeInteger): StUint[size] {.inline, fooPragma.}=
n.stuint(size)
make_conv(u128, 128)
make_conv(u256, 256)
template make_unary(op, ResultTy): untyped =
func `op`*(x: StUint): ResultTy {.fooPragma, inline.} =
2018-04-25 10:52:00 +00:00
when ResultTy is StUint:
result.data = op(x.data)
else:
op(x.data)
export op
template make_binary(op, ResultTy): untyped =
func `op`*(x, y: StUint): ResultTy {.fooPragma, inline.} =
2018-04-25 10:52:00 +00:00
when ResultTy is StUint:
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.} =
op(x.data, y.data)
export op
import ./private/uint_addsub
2018-04-25 10:52:00 +00:00
make_binary(`+`, StUint)
make_binary_inplace(`+=`)
2018-04-25 10:52:00 +00:00
make_binary(`-`, StUint)
make_binary_inplace(`-=`)
import ./private/uint_mul
2018-04-25 10:52:00 +00:00
make_binary(`*`, StUint)
import ./private/uint_div
2018-04-25 10:52:00 +00:00
make_binary(`div`, StUint)
make_binary(`mod`, StUint)
func divmod*(x, y: StUint): tuple[quot, rem: StUint] {.fooPragma, inline.} =
(result.quot.data, result.rem.data) = divmod(x.data, y.data)
import ./private/uint_comparison
make_binary(`<`, bool)
make_binary(`<=`, bool)
make_binary(`==`, bool)
2018-04-25 10:52:00 +00:00
func isZero*(x: StUint): bool {.inline.} = isZero x.data
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)
proc `shr`*(x: StUint, y: SomeInteger): StUint {.fooPragma, inline, noSideEffect.} =
result.data = x.data shr y
proc `shl`*(x: StUint, y: SomeInteger): StUint {.fooPragma, inline, noSideEffect.} =
result.data = x.data shl y
2018-05-06 20:19:00 +00:00
2018-05-06 20:24:51 +00:00
import ./private/uint_highlow
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)