Skip to content

Commit

Permalink
Make count_set_bits use the intrinsic (#287)
Browse files Browse the repository at this point in the history
This doesn't actually seem to affect the timings much at the top level, but this version of `count_set_bits` seemed faster in a contrived benchmark,

Closes gh-54
  • Loading branch information
eric-wieser committed Mar 23, 2020
1 parent 5b3be93 commit e43810c
Showing 1 changed file with 34 additions and 15 deletions.
49 changes: 34 additions & 15 deletions clifford/_layout_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,41 @@
import operator

from . import _numba_utils
import numba
import numba.extending
import numba.types
import numba.config


@numba.extending.intrinsic
def __builtin_popcnt(tyctx, x):
""" Emulate clang and GCC's `__builtin_popcnt` """
if isinstance(x, numba.types.Integer):
def impl(cgctx, builder, sig, args):
x, = args
return builder.ctpop(x)
sig = x(x)
return sig, impl


if numba.config.DISABLE_JIT:
def count_set_bits(bitmap):
""" Counts the number of bits set to 1 in bitmap """
bmp = bitmap
count = 0
n = 1
while bmp > 0:
if bmp & 1:
count += 1
bmp = bmp >> 1
n = n + 1
return count


@_numba_utils.njit
def count_set_bits(bitmap):
"""
Counts the number of bits set to 1 in bitmap
"""
bmp = bitmap
count = 0
n = 1
while bmp > 0:
if bmp & 1:
count += 1
bmp = bmp >> 1
n = n + 1
return count
else:
@numba.njit
def count_set_bits(x):
""" Counts the number of bits set to 1 in bitmap """
return __builtin_popcnt(x)


@_numba_utils.njit
Expand Down

0 comments on commit e43810c

Please sign in to comment.