Permalink
Browse files

fix bits_required when min == max

add bits_required test
  • Loading branch information...
FredGithub committed Oct 27, 2018
1 parent f8a564e commit 26093d8b2ec5d7579a122112fd3605b34ab5beaa
Showing with 18 additions and 1 deletion.
  1. +17 −0 test.cpp
  2. +1 −1 yojimbo.h
@@ -228,6 +228,22 @@ void test_bitpacker()
check( reader.GetBitsRemaining() == bytesWritten * 8 - bitsWritten );
}
void test_bits_required()
{
check( bits_required( 0, 0 ) == 0 );
check( bits_required( 0, 1 ) == 1 );
check( bits_required( 0, 2 ) == 2 );
check( bits_required( 0, 3 ) == 2 );
check( bits_required( 0, 4 ) == 3 );
check( bits_required( 0, 5 ) == 3 );
check( bits_required( 0, 6 ) == 3 );
check( bits_required( 0, 7 ) == 3 );
check( bits_required( 0, 8 ) == 4 );
check( bits_required( 0, 255 ) == 8 );
check( bits_required( 0, 65535 ) == 16 );
check( bits_required( 0, 4294967295 ) == 32 );
}
const int MaxItems = 11;
struct TestData
@@ -2517,6 +2533,7 @@ int main()
RUN_TEST( test_base64 );
#endif // #if YOJIMBO_WITH_MBEDTLS
RUN_TEST( test_bitpacker );
RUN_TEST( test_bits_required );
RUN_TEST( test_stream );
RUN_TEST( test_address );
RUN_TEST( test_bit_array );
@@ -871,7 +871,7 @@ namespace yojimbo
inline int bits_required( uint32_t min, uint32_t max )
{
#ifdef __GNUC__
return 32 - __builtin_clz( max - min );
return ( min == max ) ? 0 : 32 - __builtin_clz( max - min );
#else // #ifdef __GNUC__
return ( min == max ) ? 0 : log2( max - min ) + 1;
#endif // #ifdef __GNUC__

0 comments on commit 26093d8

Please sign in to comment.