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 import ./private/datatypes, macros
export StInt 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 = template make_unary(op, ResultTy): untyped =
func `op`*(x: Stint): ResultTy {.inline.} = func `op`*(x: Stint): ResultTy {.inline.} =

View File

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

View File

@ -10,7 +10,7 @@
import ./private/datatypes import ./private/datatypes
export StUint 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 import ./private/uint_addsub

3
tests/nim.cfg Normal file
View File

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