Commit a8567d4b authored by Erik de Castro Lopo's avatar Erik de Castro Lopo
Browse files

libFLAC/bitmath : Restore an ASSERT that was removed some time after 1.2.1.

Restore a FLAC__ASSERT() to bitmath functions FLAC__bitmath_ilog2 and
FLAC__bitmath_ilog2_wide functions. This prevents the return of an
"undefined" value.

Patch-from: lvqcl <lvqcl.mail@gmail.com>
parent 9aa15464
......@@ -35,7 +35,6 @@
#endif
#include "private/bitmath.h"
#include "FLAC/assert.h"
/* An example of what FLAC__bitmath_silog2() computes:
*
......
......@@ -34,6 +34,7 @@
#define FLAC__PRIVATE__BITMATH_H
#include "FLAC/ordinals.h"
#include "FLAC/assert.h"
/* for CHAR_BIT */
#include <limits.h>
......@@ -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).
*/
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment