From e904b3f952011ad9531b0923c2000398373af687 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arne=20D=C3=B6ring?= Date: Thu, 28 Mar 2019 12:32:02 +0100 Subject: [PATCH] code cleanup (#10874) --- compiler/ccgexprs.nim | 13 ++----------- lib/pure/bitops.nim | 24 ++++++------------------ lib/pure/math.nim | 10 ++++------ lib/system/repr.nim | 12 ++++++------ lib/system/sets.nim | 25 ++++++++++++++++--------- lib/system/widestrs.nim | 25 ++++++++++++++----------- 6 files changed, 48 insertions(+), 61 deletions(-) diff --git a/compiler/ccgexprs.nim b/compiler/ccgexprs.nim index 4026a429eb..7376dd374e 100644 --- a/compiler/ccgexprs.nim +++ b/compiler/ccgexprs.nim @@ -95,17 +95,8 @@ proc genLiteral(p: BProc, n: PNode): Rope = proc bitSetToWord(s: TBitSet, size: int): BiggestInt = result = 0 - when true: - for j in countup(0, size - 1): - if j < len(s): result = result or `shl`(ze64(s[j]), j * 8) - else: - # not needed, too complex thinking: - if CPU[platform.hostCPU].endian == CPU[targetCPU].endian: - for j in countup(0, size - 1): - if j < len(s): result = result or `shl`(Ze64(s[j]), j * 8) - else: - for j in countup(0, size - 1): - if j < len(s): result = result or `shl`(Ze64(s[j]), (Size - 1 - j) * 8) + for j in 0 ..< size: + if j < len(s): result = result or (ze64(s[j]) shl (j * 8)) proc genRawSetData(cs: TBitSet, size: int): Rope = var frmt: FormatStr diff --git a/lib/pure/bitops.nim b/lib/pure/bitops.nim index 19e4d38536..b32b5dc671 100644 --- a/lib/pure/bitops.nim +++ b/lib/pure/bitops.nim @@ -154,25 +154,13 @@ proc fastlog2_nim(x: uint64): int {.inline, nosideeffect.} = v = v or v shr 32 result = lookup[(v * 0x03F6EAF2CD271461'u64) shr 58].int +# sets.nim cannot import bitops, but bitops can use include +# system/sets to eleminate code duplication. sets.nim defines defines +# countBits32 and countBits64. +include system/sets -proc countSetBits_nim(n: uint32): int {.inline, noSideEffect.} = - ## Counts the set bits in integer. (also called Hamming weight.) - # generic formula is from: https://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel - - var v = uint32(n) - v = v - ((v shr 1) and 0x55555555) - v = (v and 0x33333333) + ((v shr 2) and 0x33333333) - result = (((v + (v shr 4) and 0xF0F0F0F) * 0x1010101) shr 24).int - -proc countSetBits_nim(n: uint64): int {.inline, noSideEffect.} = - ## Counts the set bits in integer. (also called Hamming weight.) - # generic formula is from: https://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel - var v = uint64(n) - v = v - ((v shr 1'u64) and 0x5555555555555555'u64) - v = (v and 0x3333333333333333'u64) + ((v shr 2'u64) and 0x3333333333333333'u64) - v = (v + (v shr 4'u64) and 0x0F0F0F0F0F0F0F0F'u64) - result = ((v * 0x0101010101010101'u64) shr 56'u64).int - +template countSetBits_nim(n: uint32): int = countBits32(n) +template countSetBits_nim(n: uint64): int = countBits64(n) template parity_impl[T](value: T): int = # formula id from: https://graphics.stanford.edu/%7Eseander/bithacks.html#ParityParallel diff --git a/lib/pure/math.nim b/lib/pure/math.nim index f94da043a5..05d5621827 100644 --- a/lib/pure/math.nim +++ b/lib/pure/math.nim @@ -188,18 +188,16 @@ proc nextPowerOfTwo*(x: int): int {.noSideEffect.} = result = result or (result shr 1) result += 1 + ord(x<=0) -proc countBits32*(n: int32): int {.noSideEffect.} = - ## Counts the set bits in ``n``. +proc countBits32*(n: int32): int {.noSideEffect, deprecated: "use bitops.countSetBits instead".} = + ## **Deprecated since version v0.20.0**: Use ``bitops.countSetBits`` instead. runnableExamples: doAssert countBits32(7) == 3 doAssert countBits32(8) == 1 doAssert countBits32(15) == 4 doAssert countBits32(16) == 1 doAssert countBits32(17) == 2 - var v = n - v = v -% ((v shr 1'i32) and 0x55555555'i32) - v = (v and 0x33333333'i32) +% ((v shr 2'i32) and 0x33333333'i32) - result = ((v +% (v shr 4'i32) and 0xF0F0F0F'i32) *% 0x1010101'i32) shr 24'i32 + + bitops.countSetBits(n) proc sum*[T](x: openArray[T]): T {.noSideEffect.} = ## Computes the sum of the elements in ``x``. diff --git a/lib/system/repr.nim b/lib/system/repr.nim index 68d316b73c..dc4b6d2842 100644 --- a/lib/system/repr.nim +++ b/lib/system/repr.nim @@ -95,12 +95,12 @@ proc reprSetAux(result: var string, p: pointer, typ: PNimType) = var elemCounter = 0 # we need this flag for adding the comma at # the right places add result, "{" - var u: int64 + var u: uint64 case typ.size - of 1: u = ze64(cast[ptr int8](p)[]) - of 2: u = ze64(cast[ptr int16](p)[]) - of 4: u = ze64(cast[ptr int32](p)[]) - of 8: u = cast[ptr int64](p)[] + of 1: u = cast[ptr uint8](p)[] + of 2: u = cast[ptr uint16](p)[] + of 4: u = cast[ptr uint32](p)[] + of 8: u = cast[ptr uint64](p)[] else: var a = cast[PByteArray](p) for i in 0 .. typ.size*8-1: @@ -110,7 +110,7 @@ proc reprSetAux(result: var string, p: pointer, typ: PNimType) = inc(elemCounter) if typ.size <= 8: for i in 0..sizeof(int64)*8-1: - if (u and (1'i64 shl int64(i))) != 0'i64: + if (u and (1'u64 shl uint64(i))) != 0'u64: if elemCounter > 0: add result, ", " addSetElem(result, i+typ.node.len, typ.base) inc(elemCounter) diff --git a/lib/system/sets.nim b/lib/system/sets.nim index 91dde0b815..e48484da76 100644 --- a/lib/system/sets.nim +++ b/lib/system/sets.nim @@ -12,17 +12,24 @@ type NimSet = array[0..4*2048-1, uint8] -proc countBits32(n: int32): int {.compilerproc.} = - var v = n - v = v -% ((v shr 1'i32) and 0x55555555'i32) - v = (v and 0x33333333'i32) +% ((v shr 2'i32) and 0x33333333'i32) - result = ((v +% (v shr 4'i32) and 0xF0F0F0F'i32) *% 0x1010101'i32) shr 24'i32 +# bitops can't be imported here, therefore the code duplication. -proc countBits64(n: int64): int {.compilerproc.} = - result = countBits32(toU32(n and 0xffffffff'i64)) + - countBits32(toU32(n shr 32'i64)) +proc countBits32(n: uint32): int {.compilerproc.} = + # generic formula is from: https://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel + var v = uint32(n) + v = v - ((v shr 1) and 0x55555555) + v = (v and 0x33333333) + ((v shr 2) and 0x33333333) + result = (((v + (v shr 4) and 0xF0F0F0F) * 0x1010101) shr 24).int + +proc countBits64(n: uint64): int {.compilerproc.} = + # generic formula is from: https://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel + var v = uint64(n) + v = v - ((v shr 1'u64) and 0x5555555555555555'u64) + v = (v and 0x3333333333333333'u64) + ((v shr 2'u64) and 0x3333333333333333'u64) + v = (v + (v shr 4'u64) and 0x0F0F0F0F0F0F0F0F'u64) + result = ((v * 0x0101010101010101'u64) shr 56'u64).int proc cardSet(s: NimSet, len: int): int {.compilerproc, inline.} = for i in 0..=% UNI_SUR_HIGH_START and ch <=% UNI_SUR_LOW_END: + + if ch <= UNI_MAX_BMP: + if ch >= UNI_SUR_HIGH_START and ch <= UNI_SUR_LOW_END: result[d] = UNI_REPLACEMENT_CHAR else: - result[d] = Utf16Char(toU16(ch)) - elif ch >% UNI_MAX_UTF16: + result[d] = cast[Utf16Char](uint16(ch)) + elif ch > UNI_MAX_UTF16: result[d] = UNI_REPLACEMENT_CHAR else: - let ch = ch -% halfBase - result[d] = Utf16Char(toU16((ch shr halfShift) +% UNI_SUR_HIGH_START)) + let ch = ch - halfBase + result[d] = cast[Utf16Char](uint16((ch shr halfShift) + UNI_SUR_HIGH_START)) inc d - result[d] = Utf16Char(toU16((ch and halfMask) +% UNI_SUR_LOW_START)) + result[d] = cast[Utf16Char](uint16((ch and halfMask) + UNI_SUR_LOW_START)) inc d - result[d] = Utf16Char(0'i16) + result[d] = Utf16Char(0) proc newWideCString*(s: cstring): WideCString = if s.isNil: return nil @@ -126,11 +129,11 @@ proc `$`*(w: WideCString, estimate: int, replacement: int = 0xFFFD): string = var i = 0 while w[i].int16 != 0'i16: - var ch = int(cast[uint16](w[i])) + var ch = ord(w[i]) inc i if ch >= UNI_SUR_HIGH_START and ch <= UNI_SUR_HIGH_END: # If the 16 bits following the high surrogate are in the source buffer... - let ch2 = int(cast[uint16](w[i])) + let ch2 = ord(w[i]) # If it's a low surrogate, convert to UTF32: if ch2 >= UNI_SUR_LOW_START and ch2 <= UNI_SUR_LOW_END: