summaryrefslogtreecommitdiff
path: root/klm/util/bit_packing.cc
blob: dd14ffe1ef70aa8173bdc3c5c0094261d222cf0c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include "util/bit_packing.hh"
#include "util/exception.hh"

namespace util {

namespace {
template <bool> struct StaticCheck {};
template <> struct StaticCheck<true> { typedef bool StaticAssertionPassed; };

typedef StaticCheck<sizeof(float) == 4>::StaticAssertionPassed FloatSize;

} // namespace

uint8_t RequiredBits(uint64_t max_value) {
  if (!max_value) return 0;
  uint8_t ret = 1;
  while (max_value >>= 1) ++ret;
  return ret;
}

void BitPackingSanity() {
  const detail::FloatEnc neg1 = { -1.0 }, pos1 = { 1.0 };
  if ((neg1.i ^ pos1.i) != 0x80000000) UTIL_THROW(Exception, "Sign bit is not 0x80000000");
  // TODO: more checks.  
}

} // namespace util