code cleanup (#10874)

This commit is contained in:
Arne Döring
2019-03-28 12:32:02 +01:00
committed by Andreas Rumpf
parent fcd3b0c4d8
commit e904b3f952
6 changed files with 48 additions and 61 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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``.

View File

@@ -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)

View File

@@ -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..<len:
if likely(s[i] == 0): continue
inc(result, countBits32(int32(s[i])))
inc(result, countBits32(uint32(s[i])))

View File

@@ -17,6 +17,8 @@ type
Utf16Char* = distinct int16
WideCString* = ref UncheckedArray[Utf16Char]
proc ord(arg: Utf16Char): int = int(cast[uint16](arg))
proc len*(w: WideCString): int =
## returns the length of a widestring. This traverses the whole string to
## find the binary zero end marker!
@@ -46,7 +48,7 @@ template fastRuneAt(s: cstring, i, L: int, result: untyped, doInc = true) =
## `i` is incremented by the number of bytes that have been processed.
bind ones
if ord(s[i]) <=% 127:
if ord(s[i]) <= 127:
result = ord(s[i])
when doInc: inc(i)
elif ord(s[i]) shr 5 == 0b110:
@@ -98,20 +100,21 @@ proc newWideCString*(source: cstring, L: int): WideCString =
#result = cast[wideCString](alloc(L * 4 + 2))
var d = 0
for ch in runes(source, L):
if ch <=% UNI_MAX_BMP:
if ch >=% 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: