COMMON: intLog2 uses _BitScanReverse on MSVC

This commit is contained in:
Bertrand Augereau 2011-07-25 13:24:03 +02:00 committed by Johannes Schickel
parent dd0ad3cba4
commit ff98725172
2 changed files with 34 additions and 0 deletions

View File

@ -48,6 +48,7 @@
#include "common/textconsole.h"
#include "common/tokenizer.h"
#include "common/translation.h"
#include "common/math.h"
#include "gui/gui-manager.h"
#include "gui/error.h"

View File

@ -26,6 +26,31 @@
#define COMMON_MATH_H
#include "common/scummsys.h"
#ifdef _MSC_VER
// HACK:
// intrin.h on MSVC includes setjmp.h, which will fail compiling due to our
// forbidden symbol colde. Since we also can not assure that defining
// FORBIDDEN_SYMBOL_EXCEPTION_setjmp and FORBIDDEN_SYMBOL_EXCEPTION_longjmp
// will actually allow the symbols, since forbidden.h might be included
// earlier already we need to undefine them here...
#undef setjmp
#undef longjmp
#include <intrin.h>
// ...and redefine them here so no code can actually use it.
// This could be resolved by including intrin.h on MSVC in scummsys.h before
// the forbidden.h include. This might make sense, in case we use MSVC
// extensions like _BitScanReverse in more places. But for now this hack should
// be ok...
#ifndef FORBIDDEN_SYMBOL_EXCEPTION_setjmp
#undef setjmp
#define setjmp(a) FORBIDDEN_SYMBOL_REPLACEMENT
#endif
#ifndef FORBIDDEN_SYMBOL_EXCEPTION_longjmp
#undef longjmp
#define longjmp(a,b) FORBIDDEN_SYMBOL_REPLACEMENT
#endif
#endif
#ifndef M_SQRT1_2
#define M_SQRT1_2 0.70710678118654752440 /* 1/sqrt(2) */
@ -63,6 +88,14 @@ inline int intLog2(uint32 v) {
// limits.h
return (sizeof(unsigned int) * 8 - 1) - __builtin_clz(v);
}
#elif defined(_MSC_VER)
inline int intLog2(uint32 v) {
unsigned long result = 0;
unsigned char nonZero = _BitScanReverse(&result, v);
// _BitScanReverse stores the position of the MSB set in case its result
// is non zero, thus we can just return it as is.
return nonZero ? result : -1;
}
#else
// See http://graphics.stanford.edu/~seander/bithacks.html#IntegerLogLookup
static const char LogTable256[256] = {