SDL standard library
Embed:
(wiki syntax)
Show/hide line numbers
SDL_bits.h
Go to the documentation of this file.
00001 /* 00002 Simple DirectMedia Layer 00003 Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org> 00004 00005 This software is provided 'as-is', without any express or implied 00006 warranty. In no event will the authors be held liable for any damages 00007 arising from the use of this software. 00008 00009 Permission is granted to anyone to use this software for any purpose, 00010 including commercial applications, and to alter it and redistribute it 00011 freely, subject to the following restrictions: 00012 00013 1. The origin of this software must not be misrepresented; you must not 00014 claim that you wrote the original software. If you use this software 00015 in a product, an acknowledgment in the product documentation would be 00016 appreciated but is not required. 00017 2. Altered source versions must be plainly marked as such, and must not be 00018 misrepresented as being the original software. 00019 3. This notice may not be removed or altered from any source distribution. 00020 */ 00021 00022 /** 00023 * \file SDL_bits.h 00024 * 00025 * Functions for fiddling with bits and bitmasks. 00026 */ 00027 00028 #ifndef _SDL_bits_h 00029 #define _SDL_bits_h 00030 00031 #include "SDL_stdinc.h" 00032 00033 #include "begin_code.h" 00034 /* Set up for C function definitions, even when using C++ */ 00035 #ifdef __cplusplus 00036 extern "C" { 00037 #endif 00038 00039 /** 00040 * \file SDL_bits.h 00041 */ 00042 00043 /** 00044 * Get the index of the most significant bit. Result is undefined when called 00045 * with 0. This operation can also be stated as "count leading zeroes" and 00046 * "log base 2". 00047 * 00048 * \return Index of the most significant bit, or -1 if the value is 0. 00049 */ 00050 SDL_FORCE_INLINE int 00051 SDL_MostSignificantBitIndex32(Uint32 x) 00052 { 00053 #if defined(__GNUC__) && __GNUC__ >= 4 00054 /* Count Leading Zeroes builtin in GCC. 00055 * http://gcc.gnu.org/onlinedocs/gcc-4.3.4/gcc/Other-Builtins.html 00056 */ 00057 if (x == 0) { 00058 return -1; 00059 } 00060 return 31 - __builtin_clz(x); 00061 #else 00062 /* Based off of Bit Twiddling Hacks by Sean Eron Anderson 00063 * <seander@cs.stanford.edu>, released in the public domain. 00064 * http://graphics.stanford.edu/~seander/bithacks.html#IntegerLog 00065 */ 00066 const Uint32 b[] = {0x2, 0xC, 0xF0, 0xFF00, 0xFFFF0000}; 00067 const int S[] = {1, 2, 4, 8, 16}; 00068 00069 int msbIndex = 0; 00070 int i; 00071 00072 if (x == 0) { 00073 return -1; 00074 } 00075 00076 for (i = 4; i >= 0; i--) 00077 { 00078 if (x & b[i]) 00079 { 00080 x >>= S[i]; 00081 msbIndex |= S[i]; 00082 } 00083 } 00084 00085 return msbIndex; 00086 #endif 00087 } 00088 00089 /* Ends C function definitions when using C++ */ 00090 #ifdef __cplusplus 00091 } 00092 #endif 00093 #include "close_code.h" 00094 00095 #endif /* _SDL_bits_h */ 00096 00097 /* vi: set ts=4 sw=4 expandtab: */
Generated on Tue Jul 12 2022 13:56:24 by 1.7.2