X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=turbopfor-encode.h;h=24e007cdc070e902bb9ea4b250b9d8bbb1d33505;hb=b52042bc4952d32cd5930fbb844bea302ae646f8;hp=532aa19d07b01faf6643f54153ac019387907d38;hpb=0f7ca618fb8a2e501fe68e1760de9ee716e37c40;p=plocate diff --git a/turbopfor-encode.h b/turbopfor-encode.h index 532aa19..24e007c 100644 --- a/turbopfor-encode.h +++ b/turbopfor-encode.h @@ -13,9 +13,14 @@ #include "turbopfor-common.h" +#include #include +#ifdef HAS_ENDIAN_H +#include +#endif #include #include +#include template void write_le(Docid val, void *out) @@ -27,7 +32,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 +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. } @@ -86,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 {