2010-10-27 21:50:40 +04:00
# include "util/bit_packing.hh"
# include "util/exception.hh"
2015-03-28 15:37:48 +03:00
# include <cstring>
2010-11-28 05:54:56 +03:00
2010-10-27 21:50:40 +04:00
namespace util {
namespace {
template < bool > struct StaticCheck { } ;
template < > struct StaticCheck < true > { typedef bool StaticAssertionPassed ; } ;
2015-04-30 08:05:11 +03:00
// If your float isn't 4 bytes, we're hosed.
2010-10-27 21:50:40 +04:00
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 ( ) {
2011-01-25 22:11:48 +03:00
const FloatEnc neg1 = { - 1.0 } , pos1 = { 1.0 } ;
2010-10-27 21:50:40 +04:00
if ( ( neg1 . i ^ pos1 . i ) ! = 0x80000000 ) UTIL_THROW ( Exception , " Sign bit is not 0x80000000 " ) ;
2010-11-28 05:54:56 +03:00
char mem [ 57 + 8 ] ;
memset ( mem , 0 , sizeof ( mem ) ) ;
const uint64_t test57 = 0x123456789abcdefULL ;
for ( uint64_t b = 0 ; b < 57 * 8 ; b + = 57 ) {
2011-06-27 02:21:44 +04:00
WriteInt57 ( mem , b , 57 , test57 ) ;
2010-11-28 05:54:56 +03:00
}
for ( uint64_t b = 0 ; b < 57 * 8 ; b + = 57 ) {
2011-06-27 02:21:44 +04:00
if ( test57 ! = ReadInt57 ( mem , b , 57 , ( 1ULL < < 57 ) - 1 ) )
2010-11-28 05:54:56 +03:00
UTIL_THROW ( Exception , " The bit packing routines are failing for your architecture. Please send a bug report with your architecture, operating system, and compiler. " ) ;
}
2015-04-30 08:05:11 +03:00
// TODO: more checks.
2010-10-27 21:50:40 +04:00
}
} // namespace util