Ticket #13352: trac_13352_bitset_len_v2.patch

File trac_13352_bitset_len_v2.patch, 1.2 KB (added by dcoudert, 8 years ago)
  • sage/misc/bitset.pxi

    # HG changeset patch
    # User dcoudert <david.coudert@inria.fr>
    # Date 1376770316 -7200
    # Node ID 64d54ad0eca1f26e81af292d1cca0a9122422253
    # Parent  995fbbf0196f8c00d0d66a64a425564a2bca0705
    trac #13352 -- Running time improvement of the bitset_len method
    
    diff --git a/sage/misc/bitset.pxi b/sage/misc/bitset.pxi
    a b  
    438438    Calculate the number of items in the set (i.e., the number of nonzero bits).
    439439    """
    440440    cdef long len = 0
    441     cdef long i = bitset_first(bits)
    442     while i >= 0:
    443         len += 1
    444         i = bitset_next(bits, i + 1)
     441    cdef long i
     442    for i from 0 <= i < bits.limbs:
     443        if bits.bits[i]:
     444            len += __builtin_popcountl(bits.bits[i])
    445445    return len
    446446
    447447cdef inline long bitset_hash(bitset_t bits):
  • sage/misc/bitset_pxd.pxi

    diff --git a/sage/misc/bitset_pxd.pxi b/sage/misc/bitset_pxd.pxi
    a b  
    2222    void *memcpy(void *, void *, size_t)
    2323    int memcmp(void *, void *, size_t)
    2424    size_t strlen(char *)
     25    int __builtin_popcountl(unsigned long)
    2526
    2627    # constant literals
    2728    int index_shift "(sizeof(unsigned long)==8 ? 6 : 5)"