00001 #include "util/bit_packing.hh"
00002 #include "util/exception.hh"
00003
00004 #include <cstring>
00005
00006 namespace util {
00007
00008 namespace {
00009 template <bool> struct StaticCheck {};
00010 template <> struct StaticCheck<true> { typedef bool StaticAssertionPassed; };
00011
00012
00013 typedef StaticCheck<sizeof(float) == 4>::StaticAssertionPassed FloatSize;
00014
00015 }
00016
00017 uint8_t RequiredBits(uint64_t max_value) {
00018 if (!max_value) return 0;
00019 uint8_t ret = 1;
00020 while (max_value >>= 1) ++ret;
00021 return ret;
00022 }
00023
00024 void BitPackingSanity() {
00025 const FloatEnc neg1 = { -1.0 }, pos1 = { 1.0 };
00026 if ((neg1.i ^ pos1.i) != 0x80000000) UTIL_THROW(Exception, "Sign bit is not 0x80000000");
00027 char mem[57+8];
00028 memset(mem, 0, sizeof(mem));
00029 const uint64_t test57 = 0x123456789abcdefULL;
00030 for (uint64_t b = 0; b < 57 * 8; b += 57) {
00031 WriteInt57(mem, b, 57, test57);
00032 }
00033 for (uint64_t b = 0; b < 57 * 8; b += 57) {
00034 if (test57 != ReadInt57(mem, b, 57, (1ULL << 57) - 1))
00035 UTIL_THROW(Exception, "The bit packing routines are failing for your architecture. Please send a bug report with your architecture, operating system, and compiler.");
00036 }
00037
00038 }
00039
00040 }