Better bitsof implementation (#74)

See https://github.com/nim-lang/Nim/issues/9494
This commit is contained in:
Jacek Sieka 2019-01-07 13:11:45 -06:00 committed by Mamy Ratsimbazafy
parent 3a111d6a0e
commit 360d01d170
4 changed files with 11 additions and 27 deletions

View File

@ -9,7 +9,7 @@
import ./private/datatypes, macros
export StInt
export IntImpl, intImpl # TODO remove the need to export these
export IntImpl, intImpl, bitsof # TODO remove the need to export these
template make_unary(op, ResultTy): untyped =
func `op`*(x: Stint): ResultTy {.inline.} =

View File

@ -166,32 +166,13 @@ type
StInt*[bits: static[int]] = object
data*: intImpl(bits)
template bitsof*(x: SomeInteger): int = sizeof(x) * 8
# XXX: https://github.com/nim-lang/Nim/issues/9494 - the extra overloads
# can eventually be collapsed to one..
template bitsof*(x: UintImpl[SomeInteger]): untyped =
2 * bitsof(x.lo)
template bitsof*(x: UintImpl[UintImpl[SomeInteger]]): untyped =
2 * bitsof(x.lo)
template bitsof*(x: UintImpl[UintImpl[UintImpl[SomeInteger]]]): untyped =
2 * bitsof(x.lo)
template bitsof*(x: UintImpl[UintImpl[UintImpl[UintImpl[SomeInteger]]]]): untyped =
2 * bitsof(x.lo)
template bitsof*(x: UintImpl[UintImpl[UintImpl[UintImpl[UintImpl[SomeInteger]]]]]): untyped =
2 * bitsof(x.lo)
template bitsof*(x: UintImpl[UintImpl[UintImpl[UintImpl[UintImpl[UintImpl[SomeInteger]]]]]]): untyped =
# Uint2048 - eth-bloom
2 * bitsof(x.lo)
template bitsof*(x: UintImpl[UintImpl[UintImpl[UintImpl[UintImpl[UintImpl[UintImpl[SomeInteger]]]]]]]): untyped =
# Uint4096 - modExp in Nimbus
2 * bitsof(x.lo)
template bitsof*(x: UintImpl[UintImpl[UintImpl[UintImpl[UintImpl[UintImpl[UintImpl[UintImpl[SomeInteger]]]]]]]]): untyped =
# Uint8192 - modExp in Nimbus
2 * bitsof(x.lo)
template bitsof*(x: IntImpl): untyped =
2 * bitsof(x.lo)
template bitsof*(x: SomeInteger): untyped =
sizeof(x) * 8
template bitsof*(x: UintImpl|IntImpl): untyped =
# XXX: https://github.com/nim-lang/Nim/issues/9494
mixin bitsof
bitsof(x.lo) * 2
template applyHiLo*(a: UintImpl | IntImpl, c: untyped): untyped =
## Apply `c` to each of `hi` and `lo`

View File

@ -10,7 +10,7 @@
import ./private/datatypes
export StUint
export UintImpl, uintImpl # TODO: remove the need to export those
export UintImpl, uintImpl, bitsof # TODO: remove the need to export those
import ./private/uint_addsub

3
tests/nim.cfg Normal file
View File

@ -0,0 +1,3 @@
# Ensure code is ready for threads..
--threads:on