mirror of
https://github.com/status-im/nim-stint.git
synced 2025-02-22 11:58:18 +00:00
Fix one, it impacted signed int negation and uint division (why was it not catched?). Signed int pass the full test suite
This commit is contained in:
parent
37ebd13dd4
commit
ee02de16fd
@ -11,25 +11,13 @@
|
|||||||
|
|
||||||
import
|
import
|
||||||
strutils,
|
strutils,
|
||||||
../private/[datatypes, getSize]
|
../private/datatypes
|
||||||
|
|
||||||
func tohexBE*[T: uint8 or uint16 or uint32 or uint64](x: T): string =
|
func tohexBE*(x: UintImpl or IntImpl or SomeInteger): string =
|
||||||
## Stringify an uint to hex, Most significant byte on the left
|
## Stringify an uint to hex, Most significant byte on the left
|
||||||
## i.e. a 1.uint64 will be 00000001
|
## i.e.
|
||||||
|
## - 1.uint64 will be 00000001
|
||||||
let bytes = cast[ptr array[T.sizeof, byte]](x.unsafeaddr)
|
## - (2.uint128)^64 + 1 will be 0000000100000001
|
||||||
|
|
||||||
result = ""
|
|
||||||
when system.cpuEndian == littleEndian:
|
|
||||||
for i in countdown(T.sizeof - 1, 0):
|
|
||||||
result.add toHex(bytes[i])
|
|
||||||
else:
|
|
||||||
for i in 0 ..< T.sizeof:
|
|
||||||
result.add toHex(bytes[i])
|
|
||||||
|
|
||||||
func tohexBE*(x: UintImpl): string =
|
|
||||||
## Stringify an uint to hex, Most significant byte on the left
|
|
||||||
## i.e. a (2.uint128)^64 + 1 will be 0000000100000001
|
|
||||||
|
|
||||||
const size = getSize(x) div 8
|
const size = getSize(x) div 8
|
||||||
|
|
||||||
@ -42,3 +30,6 @@ func tohexBE*(x: UintImpl): string =
|
|||||||
else:
|
else:
|
||||||
for i in 0 ..< size:
|
for i in 0 ..< size:
|
||||||
result.add toHex(bytes[i])
|
result.add toHex(bytes[i])
|
||||||
|
|
||||||
|
func tohexBE*(x: Stint or StUint): string {.inline.}=
|
||||||
|
x.data.tohexBE
|
||||||
|
@ -59,9 +59,5 @@ func stint*[T: SomeInteger](n: T, bits: static[int]): StInt[bits] {.inline.}=
|
|||||||
result = -result
|
result = -result
|
||||||
else:
|
else:
|
||||||
r_ptr[r_ptr[].len - 1] = n
|
r_ptr[r_ptr[].len - 1] = n
|
||||||
else:
|
|
||||||
if n < 0:
|
|
||||||
result.data = (type result.data)(-n)
|
|
||||||
result = -result
|
|
||||||
else:
|
else:
|
||||||
result.data = (type result.data)(n)
|
result.data = (type result.data)(n)
|
||||||
|
@ -31,22 +31,12 @@ func initUintImpl*[InType, OutType](x: InType, _: typedesc[OutType]): OutType {.
|
|||||||
func zero*[T: BaseUint](_: typedesc[T]): T {.inline.}=
|
func zero*[T: BaseUint](_: typedesc[T]): T {.inline.}=
|
||||||
discard
|
discard
|
||||||
|
|
||||||
func one*[T: BaseUint](_: typedesc[T]): T {.inline.}=
|
func one*[T: BaseUint or IntImpl](_: typedesc[T]): T {.inline.}=
|
||||||
when T is SomeUnsignedInt:
|
|
||||||
result = T(1)
|
|
||||||
else:
|
|
||||||
let r_ptr = cast[ptr array[getSize(result) div 8, byte]](result.addr)
|
|
||||||
when system.cpuEndian == bigEndian:
|
|
||||||
r_ptr[0] = 1
|
|
||||||
else:
|
|
||||||
r_ptr[r_ptr[].len - 1] = 1
|
|
||||||
|
|
||||||
func one*[T: IntImpl](_: typedesc[T]): T {.inline.}=
|
|
||||||
when T is SomeInteger:
|
when T is SomeInteger:
|
||||||
result = T(1)
|
result = T(1)
|
||||||
else:
|
else:
|
||||||
let r_ptr = cast[ptr array[getSize(result) div 8, byte]](result.addr)
|
let r_ptr = cast[ptr array[getSize(result) div 8, byte]](result.addr)
|
||||||
when system.cpuEndian == bigEndian:
|
when system.cpuEndian == littleEndian:
|
||||||
r_ptr[0] = 1
|
r_ptr[0] = 1
|
||||||
else:
|
else:
|
||||||
r_ptr[r_ptr[].len - 1] = 1
|
r_ptr[r_ptr[].len - 1] = 1
|
||||||
|
Loading…
x
Reference in New Issue
Block a user