X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=turbopfor-encode.h;h=24e007cdc070e902bb9ea4b250b9d8bbb1d33505;hb=ddc5817ee81be45ce5eab251e5773284150da416;hp=e6e3cd5f13129767fef24bda95420b5cf1be9c35;hpb=e10f92d401f89b610a091623a7005aeeb243e4f9;p=plocate diff --git a/turbopfor-encode.h b/turbopfor-encode.h index e6e3cd5..24e007c 100644 --- a/turbopfor-encode.h +++ b/turbopfor-encode.h @@ -15,6 +15,9 @@ #include #include +#ifdef HAS_ENDIAN_H +#include +#endif #include #include #include @@ -52,6 +55,12 @@ unsigned char *write_baseval(Docid in, unsigned char *out) out[1] = in & 0xff; out[2] = (in >> 8) & 0xff; return out + 3; + } else if (in < 0x10000000) { + out[0] = (in >> 24) | 0xe0; + out[1] = (in >> 16) & 0xff; + out[2] = (in >> 8) & 0xff; + out[3] = in & 0xff; + return out + 4; } else { assert(false); // Not implemented. } @@ -88,11 +97,20 @@ unsigned char *write_vb(Docid val, unsigned char *out) template inline unsigned num_bits(Docid x) { +#ifdef __GNUC__ if (x == 0) { return 0; } else { return sizeof(Docid) * CHAR_BIT - __builtin_clz(x); } +#else + for (int i = sizeof(Docid) * CHAR_BIT; i-- > 0;) { + if (x & (Docid{ 1 } << i)) { + return i; + } + } + return 0; +#endif } struct BitWriter {