SDL 3.0
SDL_bits.h File Reference
+ Include dependency graph for SDL_bits.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

SDL_FORCE_INLINE int SDL_MostSignificantBitIndex32 (Uint32 x)
 
SDL_FORCE_INLINE SDL_bool SDL_HasExactlyOneBitSet32 (Uint32 x)
 

Detailed Description

CategoryBits

Functions for fiddling with bits and bitmasks.

Definition in file SDL_bits.h.

Function Documentation

◆ SDL_HasExactlyOneBitSet32()

SDL_FORCE_INLINE SDL_bool SDL_HasExactlyOneBitSet32 ( Uint32  x)

Determine if a unsigned 32-bit value has exactly one bit set.

If there are no bits set (x is zero), or more than one bit set, this returns SDL_FALSE. If any one bit is exclusively set, this returns SDL_TRUE.

Note that this is a forced-inline function in a header, and not a public API function available in the SDL library (which is to say, the code is embedded in the calling program and the linker and dynamic loader will not be able to find this function inside SDL itself).

Parameters
xthe 32-bit value to examine.
Returns
SDL_TRUE if exactly one bit is set in x, SDL_FALSE otherwise.

\threadsafety It is safe to call this function from any thread.

Since
This function is available since SDL 3.0.0.

Definition at line 138 of file SDL_bits.h.

139{
140 if (x && !(x & (x - 1))) {
141 return SDL_TRUE;
142 }
143 return SDL_FALSE;
144}
#define SDL_TRUE
Definition SDL_stdinc.h:203
#define SDL_FALSE
Definition SDL_stdinc.h:194

References SDL_FALSE, and SDL_TRUE.

◆ SDL_MostSignificantBitIndex32()

SDL_FORCE_INLINE int SDL_MostSignificantBitIndex32 ( Uint32  x)

Get the index of the most significant (set) bit in a 32-bit number.

Result is undefined when called with 0. This operation can also be stated as "count leading zeroes" and "log base 2".

Note that this is a forced-inline function in a header, and not a public API function available in the SDL library (which is to say, the code is embedded in the calling program and the linker and dynamic loader will not be able to find this function inside SDL itself).

Parameters
xthe 32-bit value to examine.
Returns
the index of the most significant bit, or -1 if the value is 0.

\threadsafety It is safe to call this function from any thread.

Since
This function is available since SDL 3.0.0.

Definition at line 70 of file SDL_bits.h.

71{
72#if defined(__GNUC__) && (__GNUC__ >= 4 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4))
73 /* Count Leading Zeroes builtin in GCC.
74 * http://gcc.gnu.org/onlinedocs/gcc-4.3.4/gcc/Other-Builtins.html
75 */
76 if (x == 0) {
77 return -1;
78 }
79 return 31 - __builtin_clz(x);
80#elif defined(__WATCOMC__) && defined(__386__)
81 if (x == 0) {
82 return -1;
83 }
84 return _SDL_bsr_watcom(x);
85#elif defined(_MSC_VER)
86 unsigned long index;
87 if (_BitScanReverse(&index, x)) {
88 return index;
89 }
90 return -1;
91#else
92 /* Based off of Bit Twiddling Hacks by Sean Eron Anderson
93 * <seander@cs.stanford.edu>, released in the public domain.
94 * http://graphics.stanford.edu/~seander/bithacks.html#IntegerLog
95 */
96 const Uint32 b[] = {0x2, 0xC, 0xF0, 0xFF00, 0xFFFF0000};
97 const int S[] = {1, 2, 4, 8, 16};
98
99 int msbIndex = 0;
100 int i;
101
102 if (x == 0) {
103 return -1;
104 }
105
106 for (i = 4; i >= 0; i--)
107 {
108 if (x & b[i])
109 {
110 x >>= S[i];
111 msbIndex |= S[i];
112 }
113 }
114
115 return msbIndex;
116#endif
117}
uint32_t Uint32
Definition SDL_stdinc.h:265