X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=turbopfor-encode.h;h=186d7fba89574bdc1a94f9af62e0f9ebe907f950;hb=e375887f911f81b8350183ac1ef37b59f3327120;hp=532aa19d07b01faf6643f54153ac019387907d38;hpb=0f7ca618fb8a2e501fe68e1760de9ee716e37c40;p=plocate diff --git a/turbopfor-encode.h b/turbopfor-encode.h index 532aa19..186d7fb 100644 --- a/turbopfor-encode.h +++ b/turbopfor-encode.h @@ -13,9 +13,12 @@ #include "turbopfor-common.h" +#include #include +#include #include #include +#include template void write_le(Docid val, void *out) @@ -27,7 +30,7 @@ void write_le(Docid val, void *out) } else if constexpr (sizeof(Docid) == 2) { val = htole16(val); } else if constexpr (sizeof(Docid) == 1) { - val = val; + // No change. } else { assert(false); } @@ -50,6 +53,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. } @@ -86,11 +95,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 {