From: Alex Converse Date: Wed, 8 Jan 2014 20:25:47 +0000 (-0800) Subject: Add a C fallback for get_msb() and change inline to INLINE. X-Git-Tag: v1.4.0~2724^2 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=ce7ff3b63d5a3259b9a414c67c9f27f89532fc39;p=libvpx Add a C fallback for get_msb() and change inline to INLINE. For systems without __builtin_clz() or _BitScanReverse(), taken from libwep Change-Id: Iead257efc1772c466c79e1dc0356ed571d38d43e --- diff --git a/vp9/common/vp9_systemdependent.h b/vp9/common/vp9_systemdependent.h index 7c277c32e..6f955ab56 100644 --- a/vp9/common/vp9_systemdependent.h +++ b/vp9/common/vp9_systemdependent.h @@ -12,7 +12,6 @@ #define VP9_COMMON_VP9_SYSTEMDEPENDENT_H_ #ifdef _MSC_VER -#include #include #define snprintf _snprintf #endif @@ -35,15 +34,40 @@ static int round(double x) { } #endif -static const inline int get_msb(int x) { -#ifdef _MSC_VER -int r = 0; -_BitScanReverse(&r, x); -return r; +// use GNU builtins where available. +#if defined(__GNUC__) && \ + ((__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || __GNUC__ >= 4) +static INLINE int get_msb(unsigned int n) { + return 31 ^ __builtin_clz(n); +} +#elif defined(_MSC_VER) && _MSC_VER > 1310 && \ + (defined(_M_X64) || defined(_M_IX86)) +#include +#pragma intrinsic(_BitScanReverse) + +static INLINE int get_msb(unsigned int n) { + unsigned long first_set_bit; + _BitScanReverse(&first_set_bit, n); + return first_set_bit; +} #else -return 31 ^ __builtin_clz(x); -#endif +// Returns (int)floor(log2(n)). n must be > 0. +static INLINE int get_msb(unsigned int n) { + int log = 0; + unsigned int value = n; + int i; + + for (i = 4; i >= 0; --i) { + const int shift = (1 << i); + const unsigned int x = value >> shift; + if (x != 0) { + value = x; + log += shift; + } + } + return log; } +#endif struct VP9Common; void vp9_machine_specific_config(struct VP9Common *cm);