add a few more overloads for calculating bitset size (for eth-bloom)
This commit is contained in:
parent
0f53325c08
commit
edb1ade373
22
stint.nim
22
stint.nim
|
@ -16,20 +16,14 @@ type
|
||||||
UInt128* = StUint[128]
|
UInt128* = StUint[128]
|
||||||
UInt256* = StUint[256]
|
UInt256* = StUint[256]
|
||||||
|
|
||||||
template make_conv(conv_name: untyped, size: int): untyped =
|
func u128*(n: SomeInteger): UInt128 {.inline.} = n.stuint(128)
|
||||||
func `convname`*(n: SomeInteger): StUint[size] {.inline.}=
|
func u128*(s: string): UInt128 {.inline.} = s.parse(UInt128)
|
||||||
n.stuint(size)
|
|
||||||
func `convname`*(input: string): StUint[size] {.inline.}=
|
|
||||||
input.parse(Stuint[size])
|
|
||||||
|
|
||||||
make_conv(u128, 128)
|
func u256*(n: SomeInteger): UInt256 {.inline.} = n.stuint(256)
|
||||||
make_conv(u256, 256)
|
func u256*(s: string): UInt256 {.inline.} = s.parse(UInt256)
|
||||||
|
|
||||||
template make_conv(conv_name: untyped, size: int): untyped =
|
func i128*(n: SomeInteger): Int128 {.inline.} = n.stint(128)
|
||||||
func `convname`*(n: SomeInteger): Stint[size] {.inline.}=
|
func i128*(s: string): Int128 {.inline.} = s.parse(Int128)
|
||||||
n.stint(size)
|
|
||||||
func `convname`*(input: string): Stint[size] {.inline.}=
|
|
||||||
input.parse(Stint[size])
|
|
||||||
|
|
||||||
make_conv(i128, 128)
|
func i256*(n: SomeInteger): Int256 {.inline.} = n.stint(256)
|
||||||
make_conv(i256, 256)
|
func i256*(s: string): Int256 {.inline.} = s.parse(Int256)
|
||||||
|
|
|
@ -175,6 +175,10 @@ template bitsof*(x: UintImpl[UintImpl[UintImpl[SomeInteger]]]): untyped =
|
||||||
2 * bitsof(x.lo)
|
2 * bitsof(x.lo)
|
||||||
template bitsof*(x: UintImpl[UintImpl[UintImpl[UintImpl[SomeInteger]]]]): untyped =
|
template bitsof*(x: UintImpl[UintImpl[UintImpl[UintImpl[SomeInteger]]]]): untyped =
|
||||||
2 * bitsof(x.lo)
|
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 =
|
||||||
|
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`
|
||||||
|
|
|
@ -52,3 +52,9 @@ suite "Testing unsigned int bitwise operations":
|
||||||
test "Shift right - by half the size of the integer":
|
test "Shift right - by half the size of the integer":
|
||||||
check: cast[uint16](b) == z # Sanity check
|
check: cast[uint16](b) == z # Sanity check
|
||||||
check: cast[uint16](b shr 8) == z shr 8
|
check: cast[uint16](b shr 8) == z shr 8
|
||||||
|
|
||||||
|
test "leading zero bits":
|
||||||
|
var x: StUint[2048]
|
||||||
|
check: x.countLeadingZeroBits() == 2048
|
||||||
|
x = StUint[2048].one()
|
||||||
|
check: x.countLeadingZeroBits() == 2047
|
||||||
|
|
Loading…
Reference in New Issue