Skip to content

Commit

Permalink
bitop
Browse files Browse the repository at this point in the history
  • Loading branch information
joncampbell123 committed Jun 7, 2018
1 parent 6e59811 commit 090b658
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 0 deletions.
51 changes: 51 additions & 0 deletions include/bitop.h
Expand Up @@ -2,6 +2,8 @@
#include <limits.h>
#include <stdint.h>

#include <utility>

namespace bitop {

/* Return the number of bits of the data type.
Expand Down Expand Up @@ -338,6 +340,55 @@ template <typename T=unsigned int> static inline unsigned int log2(T v) {
return ~0u;
}


/* return type, pair */
class bitseqlengthandpos_ret_t {
public:
bitseqlengthandpos_ret_t(const unsigned int _start,const unsigned int _length) : start(_start), length(_length) { }
public:
bool operator==(const bitseqlengthandpos_ret_t &n) const {
return (n.start == start) &&
(n.length == length);
}
bool empty(void) const {
return (start == 0u && length == 0u);
}
public:
unsigned int start, length;
};

/* return a pair struct representing start and length of a series of 1 bits */
template <typename T=unsigned int> static inline bitseqlengthandpos_ret_t bitseqlengthandpos(T v) {
if (v != bitop::allzero<T>()) {
unsigned int start=0,length=0;

/* count zeros */
while ((v & (T)0xFFUL) == (T)0x00UL) {
start += (T)8;
v >>= (T)8UL;
}
while (!(v & (T)1u)) {
start++;
v >>= (T)1UL;
}

/* count ones */
while ((v & (T)0xFFUL) == (T)0xFFUL) {
length += (T)8;
v >>= (T)8UL;
}
while (v & (T)1u) {
length++;
v >>= (T)1UL;
}

return bitseqlengthandpos_ret_t(start,length);
}

return bitseqlengthandpos_ret_t(0u,0u);
}


void self_test(void);

}
Expand Down
38 changes: 38 additions & 0 deletions src/gui/bitop.cpp
Expand Up @@ -245,6 +245,44 @@ void self_test(void) {
assert(negate<unsigned long>(1) == ~1ul);
assert(negate<unsigned long long>(0) == ~0ull);
assert(negate<unsigned long long>(1) == ~1ull);

assert(bitseqlengthandpos(0) == bitseqlengthandpos_ret_t(0,0));
assert(bitseqlengthandpos(1) == bitseqlengthandpos_ret_t(0,1));
assert(bitseqlengthandpos(3) == bitseqlengthandpos_ret_t(0,2));
assert(bitseqlengthandpos(7) == bitseqlengthandpos_ret_t(0,3));
assert(bitseqlengthandpos(15) == bitseqlengthandpos_ret_t(0,4));
assert(bitseqlengthandpos(31) == bitseqlengthandpos_ret_t(0,5));
assert(bitseqlengthandpos(63) == bitseqlengthandpos_ret_t(0,6));
assert(bitseqlengthandpos(127) == bitseqlengthandpos_ret_t(0,7));
assert(bitseqlengthandpos(255) == bitseqlengthandpos_ret_t(0,8));

assert(bitseqlengthandpos(1) == bitseqlengthandpos_ret_t(0,1));
assert(bitseqlengthandpos(2) == bitseqlengthandpos_ret_t(1,1));
assert(bitseqlengthandpos(4) == bitseqlengthandpos_ret_t(2,1));
assert(bitseqlengthandpos(8) == bitseqlengthandpos_ret_t(3,1));
assert(bitseqlengthandpos(16) == bitseqlengthandpos_ret_t(4,1));
assert(bitseqlengthandpos(32) == bitseqlengthandpos_ret_t(5,1));
assert(bitseqlengthandpos(64) == bitseqlengthandpos_ret_t(6,1));
assert(bitseqlengthandpos(128) == bitseqlengthandpos_ret_t(7,1));
assert(bitseqlengthandpos(256) == bitseqlengthandpos_ret_t(8,1));

assert(bitseqlengthandpos(2) == bitseqlengthandpos_ret_t(1,1));
assert(bitseqlengthandpos(6) == bitseqlengthandpos_ret_t(1,2));
assert(bitseqlengthandpos(14) == bitseqlengthandpos_ret_t(1,3));
assert(bitseqlengthandpos(30) == bitseqlengthandpos_ret_t(1,4));
assert(bitseqlengthandpos(62) == bitseqlengthandpos_ret_t(1,5));
assert(bitseqlengthandpos(126) == bitseqlengthandpos_ret_t(1,6));
assert(bitseqlengthandpos(254) == bitseqlengthandpos_ret_t(1,7));
assert(bitseqlengthandpos(510) == bitseqlengthandpos_ret_t(1,8));

assert(bitseqlengthandpos(4) == bitseqlengthandpos_ret_t(2,1));
assert(bitseqlengthandpos(12) == bitseqlengthandpos_ret_t(2,2));
assert(bitseqlengthandpos(28) == bitseqlengthandpos_ret_t(2,3));
assert(bitseqlengthandpos(60) == bitseqlengthandpos_ret_t(2,4));
assert(bitseqlengthandpos(124) == bitseqlengthandpos_ret_t(2,5));
assert(bitseqlengthandpos(252) == bitseqlengthandpos_ret_t(2,6));
assert(bitseqlengthandpos(508) == bitseqlengthandpos_ret_t(2,7));
assert(bitseqlengthandpos(1020)== bitseqlengthandpos_ret_t(2,8));
}

}
Expand Down

0 comments on commit 090b658

Please sign in to comment.