Skip to content

Commit

Permalink
native popcount
Browse files Browse the repository at this point in the history
  • Loading branch information
emkornfield committed Apr 13, 2016
1 parent 61b0481 commit a2e1e52
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions cpp/src/arrow/test-util.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,25 @@ void rand_uniform_int(int n, uint32_t seed, T min_value, T max_value, T* out) {
}

static inline int bitmap_popcount(const uint8_t* data, int length) {
// book keeping
constexpr int pop_len = sizeof(uint64_t);
const uint64_t* i64_data = reinterpret_cast<const uint64_t*>(data);
const int fast_counts = length / pop_len;
const uint64_t* end = i64_data + fast_counts;

int count = 0;
for (int i = 0; i < length; ++i) {
// TODO(wesm): accelerate this
// popcount as much as possible with the widest possible count
for (auto iter = i64_data; iter < end; ++iter) {
count += __builtin_popcountll(*iter);
}

// Account for left over bytes (in theory we could fall back to smaller
// versions of popcount but the code complexity is likely not worth it)
const int loop_tail_index = fast_counts * pop_len;
for (int i = loop_tail_index; i < length; ++i) {
if (util::get_bit(data, i)) { ++count; }
}

return count;
}

Expand Down

0 comments on commit a2e1e52

Please sign in to comment.