summary refs log tree commit diff stats
path: root/lib/pure/math.nim
diff options
context:
space:
mode:
authorAndreas Rumpf <andreasrumpf@noname>2009-09-15 23:22:22 +0200
committerAndreas Rumpf <andreasrumpf@noname>2009-09-15 23:22:22 +0200
commit66a7e3d37c0303997a6b1a3b7ec263dfb8c07748 (patch)
tree40ae1ab8aeb9086b7310ea73ab8a2ed6b597f88b /lib/pure/math.nim
parent300430fbba28b408f7ac86ca46b03d9d50839399 (diff)
downloadNim-66a7e3d37c0303997a6b1a3b7ec263dfb8c07748.tar.gz
added tools and web dirs
Diffstat (limited to 'lib/pure/math.nim')
-rwxr-xr-x[-rw-r--r--]lib/pure/math.nim10
1 files changed, 6 insertions, 4 deletions
diff --git a/lib/pure/math.nim b/lib/pure/math.nim
index a3e57e403..bca45894c 100644..100755
--- a/lib/pure/math.nim
+++ b/lib/pure/math.nim
@@ -1,7 +1,7 @@
 #
 #
 #            Nimrod's Runtime Library
-#        (c) Copyright 2008 Andreas Rumpf
+#        (c) Copyright 2009 Andreas Rumpf
 #
 #    See the file "copying.txt", included in this
 #    distribution, for details about the copyright.
@@ -83,10 +83,12 @@ proc nextPowerOfTwo*(x: int): int =
   result = result or (result shr 1)
   Inc(result)
 
-proc countBits*(n: int32): int {.noSideEffect.}
+proc countBits32*(n: int32): int {.noSideEffect.} =
   ## counts the set bits in `n`.
-
-include "system/cntbits"
+  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
 
 proc sum*[T](x: openarray[T]): T {.noSideEffect.} = 
   ## computes the sum of the elements in `x`.