jorgen@6866: /* jorgen@6866: Simple DirectMedia Layer jorgen@6866: Copyright (C) 1997-2013 Sam Lantinga jorgen@6866: jorgen@6866: This software is provided 'as-is', without any express or implied jorgen@6866: warranty. In no event will the authors be held liable for any damages jorgen@6866: arising from the use of this software. jorgen@6866: jorgen@6866: Permission is granted to anyone to use this software for any purpose, jorgen@6866: including commercial applications, and to alter it and redistribute it jorgen@6866: freely, subject to the following restrictions: jorgen@6866: jorgen@6866: 1. The origin of this software must not be misrepresented; you must not jorgen@6866: claim that you wrote the original software. If you use this software jorgen@6866: in a product, an acknowledgment in the product documentation would be jorgen@6866: appreciated but is not required. jorgen@6866: 2. Altered source versions must be plainly marked as such, and must not be jorgen@6866: misrepresented as being the original software. jorgen@6866: 3. This notice may not be removed or altered from any source distribution. jorgen@6866: */ jorgen@6866: jorgen@6866: /** jorgen@6866: * \file SDL_bits.h jorgen@6866: * jorgen@6866: * Functions for fiddling with bits and bitmasks. jorgen@6866: */ jorgen@6866: jorgen@6866: #ifndef _SDL_bits_h jorgen@6866: #define _SDL_bits_h jorgen@6866: jorgen@6866: #include "SDL_stdinc.h" jorgen@6866: jorgen@6866: #include "begin_code.h" jorgen@6866: /* Set up for C function definitions, even when using C++ */ jorgen@6866: #ifdef __cplusplus jorgen@6866: /* *INDENT-OFF* */ jorgen@6866: extern "C" { jorgen@6866: /* *INDENT-ON* */ jorgen@6866: #endif jorgen@6866: jorgen@6866: /** jorgen@6866: * \file SDL_bits.h jorgen@6866: */ jorgen@6866: jorgen@6866: /** jorgen@6866: * Get the index of the most significant bit. Result is undefined when called jorgen@6866: * with 0. This operation can also be stated as "count leading zeroes" and jorgen@6866: * "log base 2". jorgen@6866: * jorgen@6866: * \return Index of the most significant bit. jorgen@6866: */ icculus@7004: SDL_FORCE_INLINE Sint8 jorgen@6866: SDL_MostSignificantBitIndex32(Uint32 x) jorgen@6866: { icculus@7033: #if defined(__GNUC__) && __GNUC__ >= 4 jorgen@6866: /* Count Leading Zeroes builtin in GCC. jorgen@6866: * http://gcc.gnu.org/onlinedocs/gcc-4.3.4/gcc/Other-Builtins.html jorgen@6866: */ jorgen@6866: return 31 - __builtin_clz(x); jorgen@6866: #else jorgen@6866: /* Based off of Bit Twiddling Hacks by Sean Eron Anderson jorgen@6866: * , released in the public domain. jorgen@6873: * http://graphics.stanford.edu/~seander/bithacks.html#IntegerLog jorgen@6866: */ jorgen@6873: const Uint32 b[] = {0x2, 0xC, 0xF0, 0xFF00, 0xFFFF0000}; jorgen@6873: const Uint8 S[] = {1, 2, 4, 8, 16}; jorgen@6873: jorgen@6873: Uint8 msbIndex = 0; jorgen@6873: int i; jorgen@6873: jorgen@6873: for (i = 4; i >= 0; i--) jorgen@6866: { jorgen@6873: if (x & b[i]) jorgen@6873: { jorgen@6873: x >>= S[i]; jorgen@6873: msbIndex |= S[i]; jorgen@6873: } jorgen@6873: } jorgen@6866: jorgen@6873: return msbIndex; jorgen@6866: #endif jorgen@6866: } jorgen@6866: jorgen@6866: /* Ends C function definitions when using C++ */ jorgen@6866: #ifdef __cplusplus jorgen@6866: /* *INDENT-OFF* */ jorgen@6866: } jorgen@6866: /* *INDENT-ON* */ jorgen@6866: #endif jorgen@6866: #include "close_code.h" jorgen@6866: jorgen@6866: #endif /* _SDL_bits_h */ jorgen@6866: jorgen@6866: /* vi: set ts=4 sw=4 expandtab: */