rename imp to impl
This commit is contained in:
parent
747978e1e5
commit
13a9d31f9a
|
@ -254,28 +254,28 @@ func fromBytes*[bits: static int](
|
||||||
|
|
||||||
func toBytesLE*[bits: static int](src: StInt[bits]):
|
func toBytesLE*[bits: static int](src: StInt[bits]):
|
||||||
array[bits div 8, byte] {.inline.} =
|
array[bits div 8, byte] {.inline.} =
|
||||||
toBytesLE(src.imp)
|
toBytesLE(src.impl)
|
||||||
|
|
||||||
func toBytesBE*[bits: static int](src: StInt[bits]):
|
func toBytesBE*[bits: static int](src: StInt[bits]):
|
||||||
array[bits div 8, byte] {.inline.} =
|
array[bits div 8, byte] {.inline.} =
|
||||||
toBytesBE(src.imp)
|
toBytesBE(src.impl)
|
||||||
|
|
||||||
func toBytes*[bits: static int](x: StInt[bits], endian: Endianness = bigEndian):
|
func toBytes*[bits: static int](x: StInt[bits], endian: Endianness = bigEndian):
|
||||||
array[bits div 8, byte] {.inline.} =
|
array[bits div 8, byte] {.inline.} =
|
||||||
toBytes(x.imp, endian)
|
toBytes(x.impl, endian)
|
||||||
|
|
||||||
func fromBytesBE*[bits: static int](
|
func fromBytesBE*[bits: static int](
|
||||||
T: typedesc[StInt[bits]],
|
T: typedesc[StInt[bits]],
|
||||||
x: openArray[byte]): T {.raises: [], noinit, gcsafe, inline.} =
|
x: openArray[byte]): T {.raises: [], noinit, gcsafe, inline.} =
|
||||||
fromBytesBE(type result.imp, x)
|
fromBytesBE(type result.impl, x)
|
||||||
|
|
||||||
func fromBytesLE*[bits: static int](
|
func fromBytesLE*[bits: static int](
|
||||||
T: typedesc[StInt[bits]],
|
T: typedesc[StInt[bits]],
|
||||||
x: openArray[byte]): T {.inline.} =
|
x: openArray[byte]): T {.inline.} =
|
||||||
fromBytesLE(type result.imp, x)
|
fromBytesLE(type result.impl, x)
|
||||||
|
|
||||||
func fromBytes*[bits: static int](
|
func fromBytes*[bits: static int](
|
||||||
T: typedesc[StInt[bits]],
|
T: typedesc[StInt[bits]],
|
||||||
x: openArray[byte],
|
x: openArray[byte],
|
||||||
srcEndian: Endianness = bigEndian): T {.inline.} =
|
srcEndian: Endianness = bigEndian): T {.inline.} =
|
||||||
fromBytes(type result.imp, x, srcEndian)
|
fromBytes(type result.impl, x, srcEndian)
|
||||||
|
|
|
@ -28,7 +28,7 @@ const
|
||||||
func sign*(a: StInt): int =
|
func sign*(a: StInt): int =
|
||||||
## get the sign of `a`
|
## get the sign of `a`
|
||||||
## either -1, 0, or 1
|
## either -1, 0, or 1
|
||||||
if a.imp.isZero: return 0
|
if a.impl.isZero: return 0
|
||||||
if a.limbs[^1] < signMask: 1
|
if a.limbs[^1] < signMask: 1
|
||||||
else: -1
|
else: -1
|
||||||
|
|
||||||
|
@ -46,13 +46,13 @@ func setMSB(a: var StInt) =
|
||||||
|
|
||||||
func negate*(a: var StInt) =
|
func negate*(a: var StInt) =
|
||||||
## two complement negation
|
## two complement negation
|
||||||
a.imp.bitnot(a.imp)
|
a.impl.bitnot(a.impl)
|
||||||
a.imp.inc
|
a.impl.inc
|
||||||
|
|
||||||
func neg*(a: StInt): StInt =
|
func neg*(a: StInt): StInt =
|
||||||
## two complement negation
|
## two complement negation
|
||||||
result.imp.bitnot(a.imp)
|
result.impl.bitnot(a.impl)
|
||||||
result.imp.inc
|
result.impl.inc
|
||||||
|
|
||||||
func abs*(a: StInt): StInt =
|
func abs*(a: StInt): StInt =
|
||||||
## Returns the positive value of Stint
|
## Returns the positive value of Stint
|
||||||
|
@ -73,11 +73,11 @@ func `-`*(a: StInt): StInt =
|
||||||
|
|
||||||
func setZero*(a: var StInt) =
|
func setZero*(a: var StInt) =
|
||||||
## Set ``a`` to 0
|
## Set ``a`` to 0
|
||||||
a.imp.setZero
|
a.impl.setZero
|
||||||
|
|
||||||
func setOne*(a: var StInt) =
|
func setOne*(a: var StInt) =
|
||||||
## Set ``a`` to 1
|
## Set ``a`` to 1
|
||||||
a.imp.setOne
|
a.impl.setOne
|
||||||
|
|
||||||
func zero*[bits: static[int]](T: typedesc[StInt[bits]]): T =
|
func zero*[bits: static[int]](T: typedesc[StInt[bits]]): T =
|
||||||
## Returns the zero of the input type
|
## Returns the zero of the input type
|
||||||
|
@ -111,11 +111,11 @@ func low*[bits](_: typedesc[StInt[bits]]): StInt[bits] =
|
||||||
{.push raises: [], inline, noinit, gcsafe.}
|
{.push raises: [], inline, noinit, gcsafe.}
|
||||||
|
|
||||||
func isZero*(a: StInt): bool =
|
func isZero*(a: StInt): bool =
|
||||||
a.imp.isZero
|
a.impl.isZero
|
||||||
|
|
||||||
func `==`*(a, b: StInt): bool =
|
func `==`*(a, b: StInt): bool =
|
||||||
## Signed int `equal` comparison
|
## Signed int `equal` comparison
|
||||||
a.imp == b.imp
|
a.impl == b.impl
|
||||||
|
|
||||||
func `<`*(a, b: StInt): bool =
|
func `<`*(a, b: StInt): bool =
|
||||||
## Signed int `less than` comparison
|
## Signed int `less than` comparison
|
||||||
|
@ -126,7 +126,7 @@ func `<`*(a, b: StInt): bool =
|
||||||
if aNeg xor bNeg:
|
if aNeg xor bNeg:
|
||||||
return aNeg
|
return aNeg
|
||||||
|
|
||||||
a.imp < b.imp
|
a.impl < b.impl
|
||||||
|
|
||||||
func `<=`*(a, b: StInt): bool =
|
func `<=`*(a, b: StInt): bool =
|
||||||
## Signed int `less or equal` comparison
|
## Signed int `less or equal` comparison
|
||||||
|
@ -151,19 +151,19 @@ func isEven*(a: StInt): bool =
|
||||||
func `not`*(a: StInt): StInt =
|
func `not`*(a: StInt): StInt =
|
||||||
## Bitwise complement of signed integer a
|
## Bitwise complement of signed integer a
|
||||||
## i.e. flips all bits of the input
|
## i.e. flips all bits of the input
|
||||||
result.imp.bitnot(a.imp)
|
result.impl.bitnot(a.impl)
|
||||||
|
|
||||||
func `or`*(a, b: StInt): StInt =
|
func `or`*(a, b: StInt): StInt =
|
||||||
## `Bitwise or` of numbers a and b
|
## `Bitwise or` of numbers a and b
|
||||||
result.imp.bitor(a.imp, b.imp)
|
result.impl.bitor(a.impl, b.impl)
|
||||||
|
|
||||||
func `and`*(a, b: StInt): StInt =
|
func `and`*(a, b: StInt): StInt =
|
||||||
## `Bitwise and` of numbers a and b
|
## `Bitwise and` of numbers a and b
|
||||||
result.imp.bitand(a.imp, b.imp)
|
result.impl.bitand(a.impl, b.impl)
|
||||||
|
|
||||||
func `xor`*(a, b: StInt): StInt =
|
func `xor`*(a, b: StInt): StInt =
|
||||||
## `Bitwise xor` of numbers x and y
|
## `Bitwise xor` of numbers x and y
|
||||||
result.imp.bitxor(a.imp, b.imp)
|
result.impl.bitxor(a.impl, b.impl)
|
||||||
|
|
||||||
{.pop.} # End noInit
|
{.pop.} # End noInit
|
||||||
|
|
||||||
|
@ -174,30 +174,30 @@ func `shr`*(a: StInt, k: SomeInteger): StInt =
|
||||||
## value < 0 ? ~(~value >> amount) : value >> amount
|
## value < 0 ? ~(~value >> amount) : value >> amount
|
||||||
if a.isNegative:
|
if a.isNegative:
|
||||||
var tmp: type a
|
var tmp: type a
|
||||||
result.imp.bitnot(a.imp)
|
result.impl.bitnot(a.impl)
|
||||||
tmp.imp.shiftRight(result.imp, k)
|
tmp.impl.shiftRight(result.impl, k)
|
||||||
result.imp.bitnot(tmp.imp)
|
result.impl.bitnot(tmp.impl)
|
||||||
else:
|
else:
|
||||||
result.imp.shiftRight(a.imp, k)
|
result.impl.shiftRight(a.impl, k)
|
||||||
|
|
||||||
func `shl`*(a: StInt, k: SomeInteger): StInt =
|
func `shl`*(a: StInt, k: SomeInteger): StInt =
|
||||||
## Shift left by k bits
|
## Shift left by k bits
|
||||||
result.imp.shiftLeft(a.imp, k)
|
result.impl.shiftLeft(a.impl, k)
|
||||||
|
|
||||||
func setBit*(a: var StInt, k: Natural) =
|
func setBit*(a: var StInt, k: Natural) =
|
||||||
## set bit at position `k`
|
## set bit at position `k`
|
||||||
## k = 0..a.bits-1
|
## k = 0..a.bits-1
|
||||||
a.imp.setBit(k)
|
a.impl.setBit(k)
|
||||||
|
|
||||||
func clearBit*(a: var StInt, k: Natural) =
|
func clearBit*(a: var StInt, k: Natural) =
|
||||||
## set bit at position `k`
|
## set bit at position `k`
|
||||||
## k = 0..a.bits-1
|
## k = 0..a.bits-1
|
||||||
a.imp.clearBit(k)
|
a.impl.clearBit(k)
|
||||||
|
|
||||||
func getBit*(a: StInt, k: Natural): bool =
|
func getBit*(a: StInt, k: Natural): bool =
|
||||||
## set bit at position `k`
|
## set bit at position `k`
|
||||||
## k = 0..a.bits-1
|
## k = 0..a.bits-1
|
||||||
a.imp.getBit(k)
|
a.impl.getBit(k)
|
||||||
|
|
||||||
{.pop.}
|
{.pop.}
|
||||||
|
|
||||||
|
@ -207,32 +207,32 @@ func getBit*(a: StInt, k: Natural): bool =
|
||||||
|
|
||||||
func `+`*(a, b: StInt): StInt =
|
func `+`*(a, b: StInt): StInt =
|
||||||
## Addition for multi-precision signed int
|
## Addition for multi-precision signed int
|
||||||
result.imp.sum(a.imp, b.imp)
|
result.impl.sum(a.impl, b.impl)
|
||||||
|
|
||||||
func `+=`*(a: var StInt, b: StInt) =
|
func `+=`*(a: var StInt, b: StInt) =
|
||||||
## In-place addition for multi-precision signed int
|
## In-place addition for multi-precision signed int
|
||||||
a.imp.sum(a.imp, b.imp)
|
a.impl.sum(a.impl, b.impl)
|
||||||
|
|
||||||
func `-`*(a, b: StInt): StInt =
|
func `-`*(a, b: StInt): StInt =
|
||||||
## Substraction for multi-precision signed int
|
## Substraction for multi-precision signed int
|
||||||
result.imp.diff(a.imp, b.imp)
|
result.impl.diff(a.impl, b.impl)
|
||||||
|
|
||||||
func `-=`*(a: var StInt, b: StInt) =
|
func `-=`*(a: var StInt, b: StInt) =
|
||||||
## In-place substraction for multi-precision signed int
|
## In-place substraction for multi-precision signed int
|
||||||
a.imp.diff(a.imp, b.imp)
|
a.impl.diff(a.impl, b.impl)
|
||||||
|
|
||||||
func inc*(a: var StInt, w: Word = 1) =
|
func inc*(a: var StInt, w: Word = 1) =
|
||||||
a.imp.inc(w)
|
a.impl.inc(w)
|
||||||
|
|
||||||
func `+`*(a: StInt, b: SomeUnsignedInt): StInt =
|
func `+`*(a: StInt, b: SomeUnsignedInt): StInt =
|
||||||
## Addition for multi-precision signed int
|
## Addition for multi-precision signed int
|
||||||
## with an unsigned integer
|
## with an unsigned integer
|
||||||
result.imp.sum(a.imp, Word(b))
|
result.impl.sum(a.impl, Word(b))
|
||||||
|
|
||||||
func `+=`*(a: var StInt, b: SomeUnsignedInt) =
|
func `+=`*(a: var StInt, b: SomeUnsignedInt) =
|
||||||
## In-place addition for multi-precision signed int
|
## In-place addition for multi-precision signed int
|
||||||
## with an unsigned integer
|
## with an unsigned integer
|
||||||
a.imp.inc(Word(b))
|
a.impl.inc(Word(b))
|
||||||
|
|
||||||
{.pop.}
|
{.pop.}
|
||||||
|
|
||||||
|
@ -249,11 +249,11 @@ func pow*(a: StInt, e: Natural): StInt =
|
||||||
## ``e`` must be non-negative
|
## ``e`` must be non-negative
|
||||||
if a.isNegative:
|
if a.isNegative:
|
||||||
let base = a.neg
|
let base = a.neg
|
||||||
result.imp = base.imp.pow(e)
|
result.impl = base.impl.pow(e)
|
||||||
if e.isOdd:
|
if e.isOdd:
|
||||||
result.negate
|
result.negate
|
||||||
else:
|
else:
|
||||||
result.imp = a.imp.pow(e)
|
result.impl = a.impl.pow(e)
|
||||||
|
|
||||||
func pow*[aBits, eBits](a: StInt[aBits], e: StInt[eBits]): StInt[aBits] =
|
func pow*[aBits, eBits](a: StInt[aBits], e: StInt[eBits]): StInt[aBits] =
|
||||||
## Compute ``x`` to the power of ``y``,
|
## Compute ``x`` to the power of ``y``,
|
||||||
|
@ -262,11 +262,11 @@ func pow*[aBits, eBits](a: StInt[aBits], e: StInt[eBits]): StInt[aBits] =
|
||||||
|
|
||||||
if a.isNegative:
|
if a.isNegative:
|
||||||
let base = a.neg
|
let base = a.neg
|
||||||
result.imp = base.imp.pow(e.imp)
|
result.impl = base.impl.pow(e.impl)
|
||||||
if e.isOdd:
|
if e.isOdd:
|
||||||
result.negate
|
result.negate
|
||||||
else:
|
else:
|
||||||
result.imp = a.imp.pow(e.imp)
|
result.impl = a.impl.pow(e.impl)
|
||||||
|
|
||||||
{.pop.}
|
{.pop.}
|
||||||
|
|
||||||
|
@ -281,12 +281,12 @@ func `div`*(n, d: StInt): StInt =
|
||||||
if n.isPositive:
|
if n.isPositive:
|
||||||
if d.isPositive:
|
if d.isPositive:
|
||||||
# pos / pos
|
# pos / pos
|
||||||
result.imp = n.imp div d.imp
|
result.impl = n.impl div d.impl
|
||||||
return
|
return
|
||||||
else:
|
else:
|
||||||
# pos / neg
|
# pos / neg
|
||||||
tmp = d.neg
|
tmp = d.neg
|
||||||
result.imp = n.imp div tmp.imp
|
result.impl = n.impl div tmp.impl
|
||||||
result.negate
|
result.negate
|
||||||
return
|
return
|
||||||
|
|
||||||
|
@ -294,11 +294,11 @@ func `div`*(n, d: StInt): StInt =
|
||||||
if d.isNegative:
|
if d.isNegative:
|
||||||
# neg / neg
|
# neg / neg
|
||||||
tmp = d.neg
|
tmp = d.neg
|
||||||
result.imp = nneg.imp div tmp.imp
|
result.impl = nneg.impl div tmp.impl
|
||||||
return
|
return
|
||||||
|
|
||||||
# neg / pos
|
# neg / pos
|
||||||
result.imp = nneg.imp div d.imp
|
result.impl = nneg.impl div d.impl
|
||||||
result.negate
|
result.negate
|
||||||
|
|
||||||
func `mod`*(x, y: StInt): StInt =
|
func `mod`*(x, y: StInt): StInt =
|
||||||
|
@ -309,7 +309,7 @@ func `mod`*(x, y: StInt): StInt =
|
||||||
xIn = x.abs
|
xIn = x.abs
|
||||||
yIn = y.abs
|
yIn = y.abs
|
||||||
|
|
||||||
result.imp = xIn.imp mod yIn.imp
|
result.impl = xIn.impl mod yIn.impl
|
||||||
if x.isNegative:
|
if x.isNegative:
|
||||||
result.negate
|
result.negate
|
||||||
|
|
||||||
|
@ -360,7 +360,7 @@ func `*`*(a, b: StInt): StInt =
|
||||||
av = a.abs
|
av = a.abs
|
||||||
bv = b.abs
|
bv = b.abs
|
||||||
|
|
||||||
result.imp = av.imp * bv.imp
|
result.impl = av.impl * bv.impl
|
||||||
if a.isNegative xor b.isNegative:
|
if a.isNegative xor b.isNegative:
|
||||||
result.negate
|
result.negate
|
||||||
|
|
||||||
|
|
14
stint/io.nim
14
stint/io.nim
|
@ -41,17 +41,17 @@ func stuint*[T: SomeInteger](n: T, bits: static[int]): StUint[bits] {.inline.}=
|
||||||
func stint*[T: SomeInteger](n: T, bits: static[int]): StInt[bits] {.inline.}=
|
func stint*[T: SomeInteger](n: T, bits: static[int]): StInt[bits] {.inline.}=
|
||||||
## Converts an integer to an arbitrary precision signed integer.
|
## Converts an integer to an arbitrary precision signed integer.
|
||||||
when T is SomeUnsignedInt:
|
when T is SomeUnsignedInt:
|
||||||
result.imp = stuint(n, bits)
|
result.impl = stuint(n, bits)
|
||||||
else:
|
else:
|
||||||
if n < 0:
|
if n < 0:
|
||||||
if n == low(T):
|
if n == low(T):
|
||||||
# special case, bug #92 workaround
|
# special case, bug #92 workaround
|
||||||
result.imp = stuint(high(T), bits) + stuint(1, bits)
|
result.impl = stuint(high(T), bits) + stuint(1, bits)
|
||||||
else:
|
else:
|
||||||
result.imp = stuint(-n, bits)
|
result.impl = stuint(-n, bits)
|
||||||
result.negate
|
result.negate
|
||||||
else:
|
else:
|
||||||
result.imp = stuint(n, bits)
|
result.impl = stuint(n, bits)
|
||||||
|
|
||||||
func to*(a: SomeInteger, T: typedesc[StInt]): T =
|
func to*(a: SomeInteger, T: typedesc[StInt]): T =
|
||||||
stint(a, result.bits)
|
stint(a, result.bits)
|
||||||
|
@ -278,7 +278,7 @@ func parse*[bits: static[int]](input: string, T: typedesc[StInt[bits]], radix: s
|
||||||
noOverflow = noOverflow * base + input[curr].readHexChar.stuint(bits)
|
noOverflow = noOverflow * base + input[curr].readHexChar.stuint(bits)
|
||||||
nextNonBlank(curr, input)
|
nextNonBlank(curr, input)
|
||||||
|
|
||||||
result.imp = noOverflow
|
result.impl = noOverflow
|
||||||
if isNeg:
|
if isNeg:
|
||||||
result.negate
|
result.negate
|
||||||
|
|
||||||
|
@ -323,9 +323,9 @@ func toString*[bits: static[int]](num: StInt[bits], radix: static[int8] = 10): s
|
||||||
## - if not base 10, they are returned raw in two-complement form.
|
## - if not base 10, they are returned raw in two-complement form.
|
||||||
let isNeg = num.isNegative
|
let isNeg = num.isNegative
|
||||||
if radix == 10 and isNeg:
|
if radix == 10 and isNeg:
|
||||||
"-" & toString(num.neg.imp, radix)
|
"-" & toString(num.neg.impl, radix)
|
||||||
else:
|
else:
|
||||||
toString(num.imp, radix)
|
toString(num.impl, radix)
|
||||||
|
|
||||||
func `$`*(num: StInt or StUint): string {.inline.}=
|
func `$`*(num: StInt or StUint): string {.inline.}=
|
||||||
toString(num, 10)
|
toString(num, 10)
|
||||||
|
|
|
@ -40,7 +40,7 @@ type
|
||||||
StInt*[bits: static[int]] = object
|
StInt*[bits: static[int]] = object
|
||||||
## Stack-based integer
|
## Stack-based integer
|
||||||
## Signed, in two complement format
|
## Signed, in two complement format
|
||||||
imp*: StUint[bits]
|
impl*: StUint[bits]
|
||||||
|
|
||||||
Carry* = uint8 # distinct range[0'u8 .. 1]
|
Carry* = uint8 # distinct range[0'u8 .. 1]
|
||||||
Borrow* = uint8 # distinct range[0'u8 .. 1]
|
Borrow* = uint8 # distinct range[0'u8 .. 1]
|
||||||
|
@ -58,13 +58,13 @@ when sizeof(int) == 8 and GCC_Compatible:
|
||||||
# --------------------------------------------------------
|
# --------------------------------------------------------
|
||||||
|
|
||||||
template limbs*(a: StInt): untyped =
|
template limbs*(a: StInt): untyped =
|
||||||
a.imp.limbs
|
a.impl.limbs
|
||||||
|
|
||||||
template `[]`*(a: StInt, i: SomeInteger or BackwardsIndex): Word =
|
template `[]`*(a: StInt, i: SomeInteger or BackwardsIndex): Word =
|
||||||
a.imp.limbs[i]
|
a.impl.limbs[i]
|
||||||
|
|
||||||
template `[]=`*(a: var StInt, i: SomeInteger or BackwardsIndex, val: Word) =
|
template `[]=`*(a: var StInt, i: SomeInteger or BackwardsIndex, val: Word) =
|
||||||
a.imp.limbs[i] = val
|
a.impl.limbs[i] = val
|
||||||
|
|
||||||
template `[]`*(a: StUint, i: SomeInteger or BackwardsIndex): Word =
|
template `[]`*(a: StUint, i: SomeInteger or BackwardsIndex): Word =
|
||||||
a.limbs[i]
|
a.limbs[i]
|
||||||
|
|
|
@ -43,12 +43,12 @@ template testInitialization(chk, tst: untyped) =
|
||||||
let x = Int128.high
|
let x = Int128.high
|
||||||
var z = UInt128.high
|
var z = UInt128.high
|
||||||
z.clearBit(z.bits - 1)
|
z.clearBit(z.bits - 1)
|
||||||
chk x.imp == z
|
chk x.impl == z
|
||||||
|
|
||||||
let xx = Int128.low
|
let xx = Int128.low
|
||||||
var zz = UInt128.low
|
var zz = UInt128.low
|
||||||
zz.setBit(z.bits - 1)
|
zz.setBit(z.bits - 1)
|
||||||
chk xx.imp == zz
|
chk xx.impl == zz
|
||||||
|
|
||||||
static:
|
static:
|
||||||
testInitialization(ctCheck, ctTest)
|
testInitialization(ctCheck, ctTest)
|
||||||
|
|
Loading…
Reference in New Issue