diff options
author | Alex Ray <aray@google.com> | 2013-01-19 22:53:46 -0800 |
---|---|---|
committer | Alex Ray <aray@google.com> | 2013-02-22 14:27:37 -0800 |
commit | f90fd9eeb32469e41c8f4207afa44929f9ec070e (patch) | |
tree | 5de5341042d6943fe5b8d164d3d9738720491e35 /include/cutils/bitops.h | |
parent | 244814698203db9472ef69f13ba9a923aab0553f (diff) | |
download | system_core-f90fd9eeb32469e41c8f4207afa44929f9ec070e.zip system_core-f90fd9eeb32469e41c8f4207afa44929f9ec070e.tar.gz system_core-f90fd9eeb32469e41c8f4207afa44929f9ec070e.tar.bz2 |
cutils: bitops: Add Hamming weight to bitmask
Change-Id: I5c6b7adf711007edfffcb4fdf8e05b04bcffef54
Diffstat (limited to 'include/cutils/bitops.h')
-rw-r--r-- | include/cutils/bitops.h | 10 |
1 files changed, 10 insertions, 0 deletions
diff --git a/include/cutils/bitops.h b/include/cutils/bitops.h index eb44236..c26dc54 100644 --- a/include/cutils/bitops.h +++ b/include/cutils/bitops.h @@ -75,6 +75,16 @@ static inline int bitmask_ffz(unsigned int *bitmask, int num_bits) return -1; } +static inline int bitmask_weight(unsigned int *bitmask, int num_bits) +{ + int i; + int weight = 0; + + for (i = 0; i < BITS_TO_WORDS(num_bits); i++) + weight += __builtin_popcount(bitmask[i]); + return weight; +} + static inline void bitmask_set(unsigned int *bitmask, int bit) { bitmask[BIT_WORD(bit)] |= BIT_MASK(bit); |