summary refs log tree commit diff stats
path: root/lib/pure
diff options
context:
space:
mode:
authorArne Döring <arne.doering@gmx.net>2019-03-28 12:32:02 +0100
committerAndreas Rumpf <rumpf_a@web.de>2019-03-28 12:32:02 +0100
commite904b3f952011ad9531b0923c2000398373af687 (patch)
treeecc07a898ae2e264b9d7600e2518548328a4803d /lib/pure
parentfcd3b0c4d8f266ca60aa0b2afe1f00bb68b4a9a4 (diff)
downloadNim-e904b3f952011ad9531b0923c2000398373af687.tar.gz
code cleanup (#10874)
Diffstat (limited to 'lib/pure')
-rw-r--r--lib/pure/bitops.nim24
-rw-r--r--lib/pure/math.nim10
2 files changed, 10 insertions, 24 deletions
diff --git a/lib/pure/bitops.nim b/lib/pure/bitops.nim
index 19e4d3853..b32b5dc67 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 f94da043a..05d562182 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``.