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:
mratsim 2018-04-25 21:58:10 +02:00
parent 37ebd13dd4
commit ee02de16fd
3 changed files with 11 additions and 34 deletions

View File

@ -11,25 +11,13 @@
import
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
## i.e. a 1.uint64 will be 00000001
let bytes = cast[ptr array[T.sizeof, byte]](x.unsafeaddr)
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
## i.e.
## - 1.uint64 will be 00000001
## - (2.uint128)^64 + 1 will be 0000000100000001
const size = getSize(x) div 8
@ -42,3 +30,6 @@ func tohexBE*(x: UintImpl): string =
else:
for i in 0 ..< size:
result.add toHex(bytes[i])
func tohexBE*(x: Stint or StUint): string {.inline.}=
x.data.tohexBE

View File

@ -60,8 +60,4 @@ func stint*[T: SomeInteger](n: T, bits: static[int]): StInt[bits] {.inline.}=
else:
r_ptr[r_ptr[].len - 1] = n
else:
if n < 0:
result.data = (type result.data)(-n)
result = -result
else:
result.data = (type result.data)(n)
result.data = (type result.data)(n)

View File

@ -31,22 +31,12 @@ func initUintImpl*[InType, OutType](x: InType, _: typedesc[OutType]): OutType {.
func zero*[T: BaseUint](_: typedesc[T]): T {.inline.}=
discard
func one*[T: BaseUint](_: 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.}=
func one*[T: BaseUint or IntImpl](_: typedesc[T]): T {.inline.}=
when T is SomeInteger:
result = T(1)
else:
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
else:
r_ptr[r_ptr[].len - 1] = 1