diff --git a/stint.nim b/stint.nim index c38335e..900d2b1 100644 --- a/stint.nim +++ b/stint.nim @@ -16,20 +16,14 @@ type UInt128* = StUint[128] UInt256* = StUint[256] -template make_conv(conv_name: untyped, size: int): untyped = - func `convname`*(n: SomeInteger): StUint[size] {.inline.}= - n.stuint(size) - func `convname`*(input: string): StUint[size] {.inline.}= - input.parse(Stuint[size]) +func u128*(n: SomeInteger): UInt128 {.inline.} = n.stuint(128) +func u128*(s: string): UInt128 {.inline.} = s.parse(UInt128) -make_conv(u128, 128) -make_conv(u256, 256) +func u256*(n: SomeInteger): UInt256 {.inline.} = n.stuint(256) +func u256*(s: string): UInt256 {.inline.} = s.parse(UInt256) -template make_conv(conv_name: untyped, size: int): untyped = - func `convname`*(n: SomeInteger): Stint[size] {.inline.}= - n.stint(size) - func `convname`*(input: string): Stint[size] {.inline.}= - input.parse(Stint[size]) +func i128*(n: SomeInteger): Int128 {.inline.} = n.stint(128) +func i128*(s: string): Int128 {.inline.} = s.parse(Int128) -make_conv(i128, 128) -make_conv(i256, 256) +func i256*(n: SomeInteger): Int256 {.inline.} = n.stint(256) +func i256*(s: string): Int256 {.inline.} = s.parse(Int256) diff --git a/stint/private/datatypes.nim b/stint/private/datatypes.nim index 1aed20a..7ecd3c2 100644 --- a/stint/private/datatypes.nim +++ b/stint/private/datatypes.nim @@ -175,6 +175,10 @@ 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 = + 2 * bitsof(x.lo) template applyHiLo*(a: UintImpl | IntImpl, c: untyped): untyped = ## Apply `c` to each of `hi` and `lo` diff --git a/tests/test_uint_bitwise.nim b/tests/test_uint_bitwise.nim index 406e74c..8014d8d 100644 --- a/tests/test_uint_bitwise.nim +++ b/tests/test_uint_bitwise.nim @@ -52,3 +52,9 @@ suite "Testing unsigned int bitwise operations": test "Shift right - by half the size of the integer": check: cast[uint16](b) == z # Sanity check 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