diff --git a/src/libFLAC/bitmath.c b/src/libFLAC/bitmath.c index b9e8e85d..64b5eca3 100644 --- a/src/libFLAC/bitmath.c +++ b/src/libFLAC/bitmath.c @@ -35,7 +35,6 @@ #endif #include "private/bitmath.h" -#include "FLAC/assert.h" /* An example of what FLAC__bitmath_silog2() computes: * diff --git a/src/libFLAC/include/private/bitmath.h b/src/libFLAC/include/private/bitmath.h index fd65cca5..ae303147 100644 --- a/src/libFLAC/include/private/bitmath.h +++ b/src/libFLAC/include/private/bitmath.h @@ -34,6 +34,7 @@ #define FLAC__PRIVATE__BITMATH_H #include "FLAC/ordinals.h" +#include "FLAC/assert.h" /* for CHAR_BIT */ #include @@ -74,6 +75,7 @@ static inline unsigned int FLAC__clz_soft_uint32(unsigned int word) static inline unsigned int FLAC__clz_uint32(FLAC__uint32 v) { /* Never used with input 0 */ + FLAC__ASSERT(v > 0); #if defined(__INTEL_COMPILER) return _bit_scan_reverse(v) ^ 31U; #elif defined(__GNUC__) && (__GNUC__ >= 4 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)) @@ -81,9 +83,11 @@ static inline unsigned int FLAC__clz_uint32(FLAC__uint32 v) * -march= setting or to a software routine in exotic machines. */ return __builtin_clz(v); #elif defined(_MSC_VER) && (_MSC_VER >= 1400) - unsigned long idx; - _BitScanReverse(&idx, v); - return idx ^ 31U; + { + unsigned long idx; + _BitScanReverse(&idx, v); + return idx ^ 31U; + } #else return FLAC__clz_soft_uint32(v); #endif @@ -99,7 +103,7 @@ static inline unsigned int FLAC__clz2_uint32(FLAC__uint32 v) /* An example of what FLAC__bitmath_ilog2() computes: * - * ilog2( 0) = undefined + * ilog2( 0) = assertion failure * ilog2( 1) = 0 * ilog2( 2) = 1 * ilog2( 3) = 1 @@ -122,12 +126,15 @@ static inline unsigned int FLAC__clz2_uint32(FLAC__uint32 v) static inline unsigned FLAC__bitmath_ilog2(FLAC__uint32 v) { + FLAC__ASSERT(v > 0); #if defined(__INTEL_COMPILER) return _bit_scan_reverse(v); #elif defined(_MSC_VER) && (_MSC_VER >= 1400) - unsigned long idx; - _BitScanReverse(&idx, v); - return idx; + { + unsigned long idx; + _BitScanReverse(&idx, v); + return idx; + } #else return sizeof(FLAC__uint32) * CHAR_BIT - 1 - FLAC__clz_uint32(v); #endif @@ -138,8 +145,7 @@ static inline unsigned FLAC__bitmath_ilog2(FLAC__uint32 v) static inline unsigned FLAC__bitmath_ilog2_wide(FLAC__uint64 v) { - if (v == 0) - return 0; + FLAC__ASSERT(v > 0); #if defined(__GNUC__) && (__GNUC__ >= 4 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)) return sizeof(FLAC__uint64) * CHAR_BIT - 1 - __builtin_clzll(v); /* Sorry, only supported in x64/Itanium.. and both have fast FPU which makes integer-only encoder pointless */ @@ -150,7 +156,7 @@ static inline unsigned FLAC__bitmath_ilog2_wide(FLAC__uint64 v) return idx; } #else -/* Brain-damaged compilers will use the fastest possible way that is, +/* Brain-damaged compilers will use the fastest possible way that is, de Bruijn sequences (http://supertech.csail.mit.edu/papers/debruijn.pdf) (C) Timothy B. Terriberry (tterribe@xiph.org) 2001-2009 CC0 (Public domain). */