From ee02de16fd63cde7730b3b81aa85d8cde4c4a0d0 Mon Sep 17 00:00:00 2001 From: mratsim Date: Wed, 25 Apr 2018 21:58:10 +0200 Subject: [PATCH] Fix one, it impacted signed int negation and uint division (why was it not catched?). Signed int pass the full test suite --- src/debug/debugutils.nim | 25 ++++++++----------------- src/init.nim | 6 +----- src/private/initialization.nim | 14 ++------------ 3 files changed, 11 insertions(+), 34 deletions(-) diff --git a/src/debug/debugutils.nim b/src/debug/debugutils.nim index 81cd7c4..62394bd 100644 --- a/src/debug/debugutils.nim +++ b/src/debug/debugutils.nim @@ -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 diff --git a/src/init.nim b/src/init.nim index dd05f8f..5c8d5f6 100644 --- a/src/init.nim +++ b/src/init.nim @@ -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) diff --git a/src/private/initialization.nim b/src/private/initialization.nim index 71cc99e..f58c137 100644 --- a/src/private/initialization.nim +++ b/src/private/initialization.nim @@ -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