diff options
author | Brent Pedersen <bpederse@gmail.com> | 2019-09-09 14:04:28 -0600 |
---|---|---|
committer | Brent Pedersen <bpederse@gmail.com> | 2019-09-09 20:00:28 -0600 |
commit | 92f2d6880204666b2e4c786108eaf8a7432a4279 (patch) | |
tree | d286ba3f45315989ab7fa63f367ee9f5d1b7e0df /lib/system/sets.nim | |
parent | c2ba0ee144adfaea6f2e1cc93a20d022149f6ccf (diff) | |
download | Nim-92f2d6880204666b2e4c786108eaf8a7432a4279.tar.gz |
count 64 bits at a time
Diffstat (limited to 'lib/system/sets.nim')
-rw-r--r-- | lib/system/sets.nim | 10 |
1 files changed, 7 insertions, 3 deletions
diff --git a/lib/system/sets.nim b/lib/system/sets.nim index e48484da7..fe7df053d 100644 --- a/lib/system/sets.nim +++ b/lib/system/sets.nim @@ -21,7 +21,7 @@ proc countBits32(n: uint32): int {.compilerproc.} = 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.} = +proc countBits64(n: uint64): int {.compilerproc, inline.} = # 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) @@ -30,6 +30,10 @@ proc countBits64(n: uint64): int {.compilerproc.} = 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 + var j = -1 + for i in countup(0, len - 8, 8): + inc(result, countBits64((cast[ptr uint64](s[i].unsafeAddr))[])) + j = i + 7 + + for i in (j+1)..<len: inc(result, countBits32(uint32(s[i]))) |