Skip to content

Commit

Permalink
Speed up Blob highzerobits implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
fwojcik committed Aug 21, 2023
1 parent 72e194c commit 22d5502
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 25 deletions.
20 changes: 1 addition & 19 deletions util/Blob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,4 @@
#include "Blob.h"

//-----------------------------------------------------------------------------
// For highzerobits()
const uint8_t hzb[256] = {
8, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
};
// Someday this will have Blob unit tests
36 changes: 30 additions & 6 deletions util/Blob.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@
*/
#include <algorithm>

extern const uint8_t hzb[256];

//-----------------------------------------------------------------------------
#define _bytes ((size_t)(_bits + 7) / 8)
template <unsigned _bits>
Expand Down Expand Up @@ -283,13 +281,39 @@ class Blob {

static FORCE_INLINE uint32_t _highzerobits( const uint8_t * bytes, const size_t len ) {
uint32_t zb = 0;
size_t i = _bytes;

for (ssize_t i = len - 1; i >= 0; i--) {
zb += hzb[bytes[i]];
if (bytes[i] != 0) {
break;
while (i >= 8) {
uint64_t a;
i -= 8;
memcpy(&a, &bytes[i], 8); a = COND_BSWAP(a, isBE());
if (a != 0) {
zb += clz8(a);
return zb;
}
zb += 64;
}
while (i >= 4) {
uint32_t a;
i -= 4;
memcpy(&a, &bytes[i], 4); a = COND_BSWAP(a, isBE());
if (a != 0) {
zb += clz4(a);
return zb;
}
zb += 32;
}
while (i >= 1) {
uint32_t a;
i -= 1;
a = bytes[i];
if (a != 0) {
zb += clz4(a);
return zb;
}
zb += 8;
}

return zb;
}

Expand Down

0 comments on commit 22d5502

Please sign in to comment.