mirror of
https://gitlab.freedesktop.org/mesa/mesa.git
synced 2026-01-07 17:20:21 +01:00
util: Improve bitcount.
Sorry for not pushing this before, it got lost in stashes.
This commit is contained in:
parent
cad14c2542
commit
c93dcbfea7
1 changed files with 8 additions and 4 deletions
|
|
@ -499,11 +499,15 @@ util_bitcount(unsigned n)
|
|||
#if defined(PIPE_CC_GCC)
|
||||
return __builtin_popcount(n);
|
||||
#else
|
||||
/* XXX there are more clever ways of doing this */
|
||||
/* K&R classic bitcount.
|
||||
*
|
||||
* For each iteration, clear the LSB from the bitfield.
|
||||
* Requires only one iteration per set bit, instead of
|
||||
* one iteration per bit less than highest set bit.
|
||||
*/
|
||||
unsigned bits = 0;
|
||||
while (n) {
|
||||
bits += (n & 1);
|
||||
n = n >> 1;
|
||||
for (bits, n, bits++) {
|
||||
n &= n - 1;
|
||||
}
|
||||
return bits;
|
||||
#endif
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue