diff options
author | Laurent Bercot <ska-skaware@skarnet.org> | 2015-05-16 00:26:24 +0000 |
---|---|---|
committer | Laurent Bercot <ska-skaware@skarnet.org> | 2015-05-16 00:26:24 +0000 |
commit | efd2b41c35d427c78c685b9a543578df3641135d (patch) | |
tree | 2c4e5af483e5a2b3d7bb9ec23e62ec09d71acbf3 /src/libstddjb/bitarray_count.c | |
parent | d6d44bb45053f0dbf866f355d1e5a4b0b3eeaf3d (diff) | |
download | skalibs-efd2b41c35d427c78c685b9a543578df3641135d.tar.xz |
- add bitarray_count
- add openwritevnclose_at
- rc for 2.3.5.0
Diffstat (limited to 'src/libstddjb/bitarray_count.c')
-rw-r--r-- | src/libstddjb/bitarray_count.c | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/src/libstddjb/bitarray_count.c b/src/libstddjb/bitarray_count.c new file mode 100644 index 0000000..724f2b3 --- /dev/null +++ b/src/libstddjb/bitarray_count.c @@ -0,0 +1,20 @@ +/* ISC license. */ + +#include <skalibs/bitarray.h> + +#define B0(n) n, n+1, n+1, n+2 +#define B1(n) B0(n), B0(n+1), B0(n+1), B0(n+2) +#define B2(n) B1(n), B1(n+1), B1(n+1), B1(n+2) +#define B3(n) B2(n), B2(n+1), B2(n+1), B2(n+2) + +unsigned int bitarray_countones (unsigned char const *c, unsigned int n) +{ + static unsigned char const table[256] = { B3(0) } ; + unsigned int len = bitarray_div8(n) ; + unsigned int total = 0 ; + register unsigned int i = 0 ; + if (n & 7) len-- ; + for (; i < len ; i++) total += table[c[i]] ; + if (n & 7) total += table[c[i] & ((1 << (n & 7)) - 1)] ; + return total ; +} |