37

Given std::bitset<64> bits with any number of bits set and a bit position X (0-63)

What is the most efficient way to count bits at position X or lower or return 0 if the bit at X is not set

Note: If the bit is set the return will always be at least 1

The brute force way is very slow:

int countupto(std::bitset<64> bits, int X)
{
  if (!bits[X]) return 0;
  int total=1;
  for (int i=0; i < X; ++i)
  {
    total+=bits[i];
  }
  return total;
}

The count() methof of bitset will give you the popcount of all the bits, but bitset does not support ranges

Note: This is not a dup of How to count the number of set bits in a 32-bit integer? as that asks about all bits not the range 0 through X

Community
  • 1
  • 1
Glenn Teitelbaum
  • 10,108
  • 3
  • 36
  • 80

5 Answers5

46

This C++ gets g++ to emit very good x86 ASM (godbolt compiler explorer). I expect it will compile efficiently on other 64bit architectures, too (if there's a HW popcount for std::bitset::count to use, otherwise that'll always be the slow part; e.g. sure to use g++ -march=nehalem or higher, or -mpopcnt if you don't want to enable anything else, if you can limit your code to only running on CPUs that support that x86 instruction):

#include <bitset>

int popcount_subset(std::bitset<64> A, int pos) {
  int high_bits_to_eliminate = 63 - pos;
  A <<= (high_bits_to_eliminate & 63);  // puts A[pos] at A[63].

  return (A[63]? ~0ULL : 0) & A.count();  // most efficient way: great code with gcc and clang
  // see the godbolt link for some #ifdefs with other ways to do the check, like
    // return A[BSET_SIZE-1] ? A.count() : 0;
}

This probably isn't optimal on 32bit architectures, so compare other alternatives if you need to make a 32bit build.

This will work for other sizes of bitset, as long as you do something about the hard-coded 63s, and change the & 63 mask for the shift count into a more general range-check. For optimal performance with strange size bitsets, make a template function with a specialization for size <= register width of the target machine. In that case, extract the bitset to an unsigned type of the appropriate width, and shift to the top of the register instead of the top of the bitset.

You'd expect this to also generate ideal code for bitset<32>, but it doesn't quite. gcc/clang still use 64bit registers on x86-64.

For large bitsets, shifting the whole thing will be slower than just popcounting the words below the one containing pos, and using this on that word. (This is where a vectorized popcount really shines on x86 if you can assume SSSE3 but not the popcnt insn hardware support, or for 32bit targets. AVX2 256bit pshufb is the fastest way to do bulk popcounts, but without AVX2 I think 64bit popcnt is pretty close to a 128-bit pshufb implementation. See the comments for more discussion.)

If you have an array of 64-bit elements, and want to count bits below a certain position in each one separately, then you definitely should use SIMD. The shift parts of this algorithm vectorize, not just the popcnt part. Use psadbw against an all-zero register to horizontal-sum bytes in 64-bit chunks after a pshufb-based popcnt that produces counts for the bits in each byte separately. SSE/AVX doesn't have 64-bit arithmetic right shift, but you can use a different technique to blend on the high bit of each element.


How I came up with this:

The asm instructions you want to get the compiler to output will:

  1. remove the unwanted bits from the 64bit value
  2. test the highest of the wanted bits.
  3. popcount it.
  4. return 0 or popcount, depending on the result of the test. (Branchless or branching implementations both have advantages. If the branch is predictable, a branchless implementation tends to be slower.)

The obvious way to do 1 is to generate a mask ((1<<(pos+1)) -1) and & it. A more efficient way is to left-shift by 63-pos, leaving the bits you want packed at the top of a register.

This also has the interesting side-effect of putting the bit you want to test as the top bit in the register. Testing the sign bit, rather than any other arbitrary bit, takes slightly fewer instructions. An arithmetic right shift can broadcast the sign bit to the rest of the register, allowing for more-efficient-than-usual branchless code.


Doing the popcount is a much-discussed problem, but is actually the trickier part of the puzzle. On x86, there is extremely efficient hardware support for it, but only on recent-enough hardware. On Intel CPUs, the popcnt instruction is only available on Nehalem and newer. I forget when AMD added support.

So to use it safely, you need to either do CPU dispatching with a fallback that doesn't use popcnt. Or, make separate binaries that do/don't depend on some CPU features.

popcount without the popcnt instruction can be done a few ways. One uses SSSE3 pshufb to implement a 4-bit LUT. This is most effective when used on a whole array, rather than a single 64b at a time, though. Scalar bithacks might be best here, and wouldn't require SSSE3 (and so would be compatible with ancient AMD CPUs that have 64bit but not pshufb.)


The Bitbroadcast:

(A[63]? ~0ULL : 0) asks the compiler to broadcast the high bit to all other bit positions, allowing it to be used as an AND-mask to zero (or not) the popcount result. Note that even for large bitset sizes, it's still only masking the output of popcnt, not the bitset itself, so ~0ULL is fine I used ULL to make sure wasn't ever asking the compiler to broadcast the bit only to the low 32b of a register (with UL on Windows, for example).

This broadcast can be done with an arithmetic right shift by 63, which shifts in copies of the high bit.

clang generated this code from the original version. After some prodding from Glenn about different implementations for 4, I realized that I could lead gcc towards clang's optimal solution by writing the source more like the ASM I want. The obvious ((int64_t)something) >> 63 to more directly request an arithmetic right shift would not be strictly portable, because signed right-shifts are implementation-defined as either arithmetic or logical. The standard doesn't provide any portable arithmetic right-shift operator. (It's not undefined behaviour, though.) Anyway, fortunately compilers are smart enough: gcc sees the best way once you give it enough of a hint.

This source makes great code on x86-64 and ARM64 with gcc and clang. Both simply use an arithmetic right shift on the input to popcnt (so the shift can run in parallel with the popcnt). It also compiles great on 32bit x86 with gcc, because the masking only happens to a 32bit variable (after multiple popcnt results are added). It's the rest of the function that's nasty on 32bit (when the bitset is larger than a register).


Original ternary-operator version with gcc

Compiled with gcc 5.3.0 -O3 -march=nehalem -mtune=haswell (older gcc, like 4.9.2, also still emit this):

; the original ternary-operator version.  See below for the optimal version we can coax gcc into emitting.
popcount_subset(std::bitset<64ul>, int):
    ; input bitset in rdi, input count in esi (SysV ABI)
    mov     ecx, esi    ; x86 variable-count shift requires the count in cl
    xor     edx, edx    ; edx=0 
    xor     eax, eax    ; gcc's workaround for popcnt's false dependency on the old value of dest, on Intel
    not     ecx         ; two's complement bithack for 63-pos (in the low bits of the register)
    sal     rdi, cl     ; rdi << ((63-pos) & 63);  same insn as shl (arithmetic == logical left shift)
    popcnt  rdx, rdi
    test    rdi, rdi    ; sets SF if the high bit is set.
    cmovs   rax, rdx    ; conditional-move on the sign flag
    ret

See How to prove that the C statement -x, ~x+1, and ~(x-1) yield the same results? for background on gcc's use of the -x == ~x + 1 two's complement identity. (And Which 2's complement integer operations can be used without zeroing high bits in the inputs, if only the low part of the result is wanted? which tangentially mentions that shl masks the shift count, so we only need the low 6 bits of ecx to hold 63 - pos. Mostly linking that because I wrote it recently and anyone still reading this paragraph might find it interesting.)

Some of those instructions will go away when inlining. (e.g. gcc would generate the count in ecx in the first place.)

With Glenn's multiply instead of ternary operator idea (enabled by USE_mul), gcc does

    shr     rdi, 63
    imul    eax, edi

at the end instead of xor / test / cmovs.


Haswell perf analysis, using microarch data from Agner Fog (Multiply version):

  • mov r,r: 1 fused-domain uop, 0 latency, no execution unit
  • xor-zeroing: 1 fused-domain uop, no execution unit
  • not: 1 uop for p0/p1/p5/p6, 1c latency, 1 per 0.25c throughput
  • shl (aka sal) with count in cl: 3 uops for p0/p6: 2c latency, 1 per 2c throughput. (Agner Fog's data indicates that IvyBridge only takes 2 uops for this, strangely.)
  • popcnt: 1 uop for p1, 3c latency, 1 per 1c throughput
  • shr r,imm: 1 uop for p0/p6, 1c latency. 1 per 0.5c throughput.
  • imul r,r: 1uop for p1, 3c latency.
  • not counting the ret

Totals:

  • 9 fused-domain uops, can issue in 2.25 cycles (in theory; uop cache-line effects usually bottleneck the frontend slightly).
  • 4 uops (shifts) for p0/p6. 2 uops for p1. 1 any-ALU-port uop. Can execute at one per 2c (saturating the shift ports), so the frontend is the worst bottleneck.

Latency: Critical path from when the bitset is ready to when the result is: shl(2) -> popcnt(3) -> imul(3). Total 8 cycles. Or 9c from when pos is ready, because the not is an extra 1c latency for it.

The optimal bitbroadcast version replaces shr with sar (same perf), and imul with and (1c latency instead of 3c, runs on any port). So the only perf change is reducing the critical path latency to 6 cycles. Throughput is still bottlenecked on the frontend. and being able to run on any port doesn't make a difference, unless you're mixing this with code that bottlenecks on port1 (instead of looking at the throughput for running just this code in a tight loop).

cmov (ternary operator) version: 11 fused-domain uops (frontend: one per 2.75c). execution units: still bottlenecked on the shift ports (p0/p6) at one per 2c. Latency: 7c from bitset to result, 8c from pos to result. (cmov is 2c latency, 2 uops for any of p0/p1/p5/p6.)


Clang has some different tricks up its sleeve: Instead of test/cmovs, it generates a mask of either all-ones or all-zeros by using an arithmetic right-shift to broadcast the sign bit to all positions of a register. I love it: Using and instead of cmov is more efficient on Intel. It still has the data-dependency and does the work for both sides of the branch (which is the main downside to cmov in general), though. Update: with the right source code, gcc will use this method, too.

clang 3.7 -O3 -Wall -march=nehalem -mtune=haswell

popcount_subset(std::bitset<64ul>, int):
    mov     ecx, 63
    sub     ecx, esi      ; larger code size, but faster on CPUs without mov-elimination
    shl     rdi, cl       ; rdi << ((63-pos) & 63)
    popcnt  rax, rdi      ; doesn't start a fresh dep chain before this, like gcc does
    sar     rdi, 63       ; broadcast the sign bit
    and     eax, edi      ; eax = 0 or its previous value
    ret

sar / and replaces xor / test / cmov, and cmov is a 2-uop instruction on Intel CPUs, so that's really nice. (For the ternary-operator version).

Clang still does the sar / and trick instead of an actual imul when using the multiply source version, or the "bitbroadcast" source version. So those help gcc without hurting clang. (sar/and is definitely better than shr/imul: 2c less latency on the critical path.) The pow_of_two_sub version does hurt clang (see the first godbolt link: omitted from this answer to avoid clutter with ideas that didn't pan out).

The mov ecx, 63 / sub ecx, esi is actually faster on CPUs without mov-elimination for reg,reg moves (zero latency and no execution port, handled by register renaming). This includes Intel pre-IvyBridge, but not more recent Intel and AMD CPUs.

Clang's mov imm / sub method puts only one cycle of latency for pos onto the critical path (beyond the bitset->result latency), instead of two for a mov ecx, esi / not ecx on CPUs where mov r,r has 1c latency.


With BMI2 (Haswell and later), an optimal ASM version can save a mov to ecx. Everything else works the same, because shlx masks its shift-count input register down to the operand-size, just like shl.

x86 shift instructions have crazy CISC semantics where if the shift count is zero, the flags aren't affected. So variable-count shift instructions have a (potential) dependency on the old value of the flags. "Normal" x86 shl r, cl decodes to 3 uops on Haswell, but BMI2 shlx r, r, r is only 1. So it's too bad that gcc still emits sal with -march=haswell, instead of using shlx (which it does use in some other cases).

// hand-tuned BMI2 version using the NOT trick and the bitbroadcast
popcount_subset(std::bitset<64ul>, int):
    not     esi           ; The low 6 bits hold 63-pos.  gcc's two-s complement trick
    xor     eax, eax      ; break false dependency on Intel.  maybe not needed when inlined.
    shlx    rdi, rdi, rsi ; rdi << ((63-pos) & 63)
    popcnt  rax, rdi
    sar     rdi, 63       ; broadcast the sign bit: rdi=0 or -1
    and     eax, edi      ; eax = 0 or its previous value
    ret

Perf analysis for Intel Haswell: 6 fused-domain uops (frontend: one per 1.5c). Execution units: 2 p0/p6 shift uops. 1 p1 uop. 2 any-port uops: (one per 1.25c from total execution port limits). Critical path latency: shlx(1) -> popcnt(3) -> and(1) = 5c bitset->result. (or 6c from pos->result).

Note that when inlining, a human (or smart compiler) could avoid the need for the xor eax, eax. It's only there because of popcnt's false dependency on the output register (on Intel), and we need the output in eax (which the caller may have used recently for a long dep chain). With -mtune=bdver2 or something, gcc won't zero the register it's going to use for popcnt output.

When inlining, we could use an output register that already has to be ready at least as early as popcnt's source reg to avoid the problem. Compilers will do an in-place popcnt rdi,rdi when the source isn't needed later, but that's not the case here. Instead, we can pick another register that already has to be ready before the source. popcnt's input depends on 63-pos, and we can clobber it, so popcnt rsi,rdi's dependency on rsi can't delay it. Or if we had 63 in a register, we could popcnt rsi,rdi / sarx rax, rsi, reg_63 / and eax, esi. Or BMI2 3-operand shift instructions would also let us not clobber inputs in case they're needed afterwards.


This is so light-weight that loop overhead and setting up the input operands / storing the results are going to be major factors. (And the 63-pos can optimize away with a compile-time constant, or into wherever a variable count comes from.)


The Intel compiler amusingly shoots itself in the foot and doesn't take advantage of the fact that A[63] is the sign bit. shl / bt rdi, 63 / jc. It even sets up the branches in a really dumb way. It could zero eax, and then jump over popcnt or not based on the sign flag set by shl.

An optimal branching implementation, starting from ICC13 output from -O3 -march=corei7 on godbolt:

   // hand-tuned, not compiler output
        mov       ecx, esi    ; ICC uses neg/add/mov :/
        not       ecx
        xor       eax, eax    ; breaks the false dep, or is the return value in the taken-branch case
        shl       rdi, cl
        jns    .bit_not_set
        popcnt    rax, rdi
.bit_not_set:
        ret

That's pretty much optimal: The A[pos] == true case has one not-taken branch. It doesn't save very much over the branchless method, though.

If the A[pos] == false case is more common: jump over a ret instruction, to a popcnt / ret. (Or after inlining: jump to a block at the end that does the popcnt and jumps back).

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • isn't `high_bits_to_eliminate & 63` redundant? – Glenn Teitelbaum Dec 22 '15 at 12:02
  • @GlennTeitelbaum: No, because the compiler doesn't know the range of `pos` is `[0..63]`. Try it without on godbolt, and see what happens to the asm. It tests and branches on `(uint64_t) pos > 63U`. It's similar to http://stackoverflow.com/questions/776508/best-practices-for-circular-shift-rotate-operations-in-c, where the masking in the source lines up with how the x86 instruction works, allowing the compiler to use it *without* checks or undefined behaviour. `std::bitset::operator<<` looks like it saturates the count, producing a zero result when you shift out all the bits. – Peter Cordes Dec 22 '15 at 12:08
  • Apparently [ARM's shift instructions saturate the count](http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0497a/CIHDDCIF.html), so you might get more efficient code on ARM from not masking. (But then calling the function with an out-of-range `pos` would cause Undefined Behaviour. http://blog.llvm.org/2011/05/what-every-c-programmer-should-know.html mentions shifts.) – Peter Cordes Dec 22 '15 at 12:18
  • 1
    What are your thoughts on changing `return A[63] ? A.count() : 0;` to `return A[63] * A.count();` given no expectation of predictability – Glenn Teitelbaum Dec 22 '15 at 14:58
  • 1
    @GlennTeitelbaum: Interesting, to my surprise that does actually make arguably better code with gcc for x86-64. `xor/test/cmov` is replaced with `shr imm/imul r32,r32`. `imul` is 1 uop, 3 cycle latency, so it's slightly worse for latency, slightly better for throughput. Both ways were branchless on x86-64, but only the mul version is branchless on ARM64 (not counting the function call to `popcount`). **clang generates identical code either way**, because it sees through the multiply by a 0 or 1 value. – Peter Cordes Dec 22 '15 at 22:54
  • @Glenn: Are you targeting any particular architecture / compiler with your code? If you're targetting MSVC, for example, it might compile completely differently. If you're targetting x86-64 and can't assume `popcnt` support, you might still be able to assume `pshufb` support (SSSE3) and use it as a 4-bit LUT for popcount. Also, I checked with `-m32`, and it's ugly, but the multiply version doesn't seem any worse. There's still only one `imul` instruction, since `gcc` knows the value fits in 32bits. – Peter Cordes Dec 22 '15 at 22:58
  • I also like that this generically works with any `bitset` size (although possibly not as optimally) – Glenn Teitelbaum Dec 24 '15 at 16:39
  • @GlennTeitelbaum: interesting point. It will work fine with the natural width of a register. For an 8 or 16bit bitset, it would be best to left-shift up to a 32bit boundary, though. For one thing, the x86 popcnt instruction is only available in 32 and 64bit operand size. Writing a 16bit partial-register doesn't zero the upper bits. For arbitrary bitset sizes, it would have to mask off extraneous bits after shifting. (Optimal would be to still shift to a 64bit boundary, not just to the top of the bitset. To do that, get the uint64_t out of the bitset before shifting.) – Peter Cordes Dec 24 '15 at 17:22
  • What I noticed is that it will work with `bitset<256>`, its ugly, but the algorithm holds because it doesn't count on having a mask fit in a native integral type – Glenn Teitelbaum Dec 24 '15 at 18:04
  • @GlennTeitelbaum: yeah, I looked at 64bit bitsets compiled for 32bit. You could maybe make a template specialization for bitset sizes smaller than the `uintmax_t`, and in that case extract to a `uintmax_t` where you shift to the top of the register. Although I think on 32bit, `uintmax_t` is still 64bits, so that's not actually the right choice of type... – Peter Cordes Dec 24 '15 at 18:10
  • For `bitset<256>`, an SSE or AVX implementation should be good. You could broadcast your `pos` and compare (`vpcmpeqQ`) against a vector of `{192, 128, 64, 0}` to get all-ones in elements below pos. And then get the top element right-shifted to produce a mask ending at the right bit position... hrm. But yeah, this version that's great for 64bit at least works for other sizes (**if you take out the hard-coded `63`**), with performance that's probably not a lot worse than the other approach of generating a mask that wide. – Peter Cordes Dec 24 '15 at 18:13
  • another thought is `(128-A[63]) & A.count()` -- basically set a high bit, subtract the pos bit. If pos=1 you get a mask, if pos is 0 the bit is too high to match a bit in `count` -- so its a `mult` vs a `sub` `and` – Glenn Teitelbaum Dec 24 '15 at 20:19
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/98944/discussion-between-glenn-teitelbaum-and-peter-cordes). – Glenn Teitelbaum Dec 24 '15 at 20:23
  • Just taking a new look: https://godbolt.org/z/M6jEG6eeY – Glenn Teitelbaum Apr 08 '22 at 15:48
10

My immediate reaction would be to test the specified bit, and immediately return 0 of it's clear.

If you get past that, create a bit-mask with that bit (and the less-significant ones) set, and and that with the original input. Then use the count() member function to get the count of bits set in the result.

As for creating the mask: you can shift 1 left N places, then subtract 1.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • 1
    Hmmm, for 0: `(1<<0)-1==0` but I was looking for a 1 if it was set, this checks all bits below but not at. We could then just add 1. leaving `(bits[X]) ? bitset<64>((1UL << x) - 1)).count() +1 : 0` – Glenn Teitelbaum Dec 22 '15 at 02:24
  • @GlennTeitelbaum: I guess I should have been clear, but I was thinking in terms of 1-based bit-numbering, so for the least significant bit, it would be (1<<1)-1 = 1, exactly what you're looking for. The place you run into difficulty is if you want to count *all* bits, in which case you need a type that can hold at least one extra bit before the subtraction. – Jerry Coffin Dec 22 '15 at 04:09
  • @JerryCoffin in the latter case you can just return the `count` of the original :) – CompuChip Dec 22 '15 at 07:37
  • @CompuChip: You can, but if possible I'd prefer to avoid there being any special cases. – Jerry Coffin Dec 22 '15 at 13:37
  • `std::bitset` is 0 based, and I'm not sure how to get an extra bit from a `long long` – Glenn Teitelbaum Dec 22 '15 at 15:02
5

Assuming an unsigned long or unsigned long long is big enough to hold 64 bits, you can call bits.to_unlong() (or bits.to_ullong()) to get the bitset data as an integer, mask off the bits above X ((1 << X) - 1) then count those bits as given in the answer to the question you link to.

1201ProgramAlarm
  • 32,384
  • 7
  • 42
  • 56
5

It's easy to convert between a bit and a mask for bits below it, so something like this should work:

int popcnt(bitset<64> bs, int x) {
    // Early out when bit not set
    if (!bs[x]) return 0;
    // Otherwise, make mask from `x`, mask and count bits
    return (bs & bitset<64>((1UL << x) - 1)).count() + 1;
}

The assumption here is that bitset::count is implemented efficiently (using popcnt intrinsics or an efficient fallback); this isn't guaranteed, but the STL folks tend to optimize this sort of thing.

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
1

I've edited a problem I've seen before which would check if an odd or even number of bits are set in a number. It's for C but it shouldn't be too hard to massage it into C++. The crux of the solution is what's in the while loop. Try it out on paper to understand how it picks out the LSB and then removes it from x. The rest of the code is straight forward. The code runs in O(n), where n is the number of set bits in x. That's much better than linear time which I had also thought was only possible when first looking at this problem.

#include <stdio.h>

int
count(long x, int pos)
{
    /* if bit at location pos is not set, return 0 */
    if (!((x >> pos) & 1))
    {
        return 0;
    }

    /* prepare x by removing set bits after position pos */
    long tmp = x;
    tmp = tmp >> (pos + 1);
    tmp = tmp << (pos + 1);
    x ^= tmp;

    /* increment count every time the first set bit of x is removed (from the right) */
    int y;
    int count = 0;
    while (x != 0)
    {
        y = x & ~(x - 1);
        x ^= y;
        count++;
    }
    return count;
}

int
main(void)
{
    /* run tests */
    long num = 0b1010111;
    printf("%d\n", count(num, 0)); /* prints: 1 */
    printf("%d\n", count(num, 1)); /* prints: 2 */
    printf("%d\n", count(num, 2)); /* prints: 3 */
    printf("%d\n", count(num, 3)); /* prints: 0 */
    printf("%d\n", count(num, 4)); /* prints: 4 */
    printf("%d\n", count(num, 5)); /* prints: 0 */
    printf("%d\n", count(num, 6)); /* prints: 5 */
}
jigglypuff
  • 507
  • 1
  • 7
  • 20