# HG changeset patch
# User dcoudert <david.coudert@inria.fr>
# Date 1344500797 -7200
# Node ID 58e6cb67b43b1d657ee25512fcbba6f247d94c4b
# Parent a3a5bdf1e8c8c9f17f141150bf07043c8115ec4b
trac #13352 -- use builtin functions to count bits in integers
diff --git a/module_list.py b/module_list.py
a
|
b
|
|
1056 | 1056 | Extension('sage.misc.allocator', |
1057 | 1057 | sources = ['sage/misc/allocator.pyx']), |
1058 | 1058 | |
| 1059 | Extension('sage.misc.bitcount', |
| 1060 | sources = ['sage/misc/bitcount.c'], |
| 1061 | extra_compile_args = ['-mpopcnt']), |
| 1062 | |
1059 | 1063 | Extension('sage.misc.bitset', |
1060 | | sources = ['sage/misc/bitset.pyx']), |
| 1064 | sources = ['sage/misc/bitset.pyx'], |
| 1065 | extra_compile_args = ['-mpopcnt', '-msse4.2']), |
1061 | 1066 | |
1062 | 1067 | Extension('sage.misc.cachefunc', |
1063 | 1068 | sources = ['sage/misc/cachefunc.pyx']), |
diff --git a/sage/misc/bitcount.c b/sage/misc/bitcount.c
new file mode 100644
-
|
+
|
|
| 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
| 3 | int popcount32(int i){ return __builtin_popcount(i); } |
| 4 | int popcount64(unsigned long long i){ return __builtin_popcountll(i); } |
diff --git a/sage/misc/bitcount.pxd b/sage/misc/bitcount.pxd
new file mode 100644
-
|
+
|
|
| 1 | |
| 2 | cdef extern from "bitcount.c": |
| 3 | int popcount32(int i) |
| 4 | int popcount64(unsigned long long i) |
diff --git a/sage/misc/bitset.pxi b/sage/misc/bitset.pxi
a
|
b
|
|
402 | 402 | return -1 |
403 | 403 | |
404 | 404 | |
405 | | |
| 405 | from sage.misc.bitcount import popcount64 |
406 | 406 | cdef inline long bitset_len(bitset_t bits): |
407 | 407 | """ |
408 | 408 | Calculate the number of items in the set (i.e., the number of nonzero bits). |
409 | 409 | """ |
410 | 410 | cdef long len = 0 |
411 | | cdef long i = bitset_first(bits) |
412 | | while i>=0: |
413 | | len += 1 |
414 | | i=bitset_next(bits, i+1) |
| 411 | cdef long i |
| 412 | for i from 0 <= i < bits.limbs: |
| 413 | len += popcount64(bits.bits[i]) |
415 | 414 | return len |
416 | 415 | |
417 | 416 | cdef inline long bitset_hash(bitset_t bits): |