SDL 3.0
SDL_bits.h
Go to the documentation of this file.
1/*
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
4
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
8
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
12
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
20*/
21
22/**
23 * # CategoryBits
24 *
25 * Functions for fiddling with bits and bitmasks.
26 */
27
28#ifndef SDL_bits_h_
29#define SDL_bits_h_
30
31#include <SDL3/SDL_stdinc.h>
32
33#include <SDL3/SDL_begin_code.h>
34/* Set up for C function definitions, even when using C++ */
35#ifdef __cplusplus
36extern "C" {
37#endif
38
39/**
40 * \file SDL_bits.h
41 */
42
43#if defined(__WATCOMC__) && defined(__386__)
44extern __inline int _SDL_bsr_watcom(Uint32);
45#pragma aux _SDL_bsr_watcom = \
46 "bsr eax, eax" \
47 parm [eax] nomemory \
48 value [eax] \
49 modify exact [eax] nomemory;
50#endif
51
52/**
53 * Get the index of the most significant (set) bit in a 32-bit number.
54 *
55 * Result is undefined when called with 0. This operation can also be stated
56 * as "count leading zeroes" and "log base 2".
57 *
58 * Note that this is a forced-inline function in a header, and not a public
59 * API function available in the SDL library (which is to say, the code is
60 * embedded in the calling program and the linker and dynamic loader will not
61 * be able to find this function inside SDL itself).
62 *
63 * \param x the 32-bit value to examine.
64 * \returns the index of the most significant bit, or -1 if the value is 0.
65 *
66 * \threadsafety It is safe to call this function from any thread.
67 *
68 * \since This function is available since SDL 3.1.3.
69 */
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 (int)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}
118
119/**
120 * Determine if a unsigned 32-bit value has exactly one bit set.
121 *
122 * If there are no bits set (`x` is zero), or more than one bit set, this
123 * returns false. If any one bit is exclusively set, this returns true.
124 *
125 * Note that this is a forced-inline function in a header, and not a public
126 * API function available in the SDL library (which is to say, the code is
127 * embedded in the calling program and the linker and dynamic loader will not
128 * be able to find this function inside SDL itself).
129 *
130 * \param x the 32-bit value to examine.
131 * \returns true if exactly one bit is set in `x`, false otherwise.
132 *
133 * \threadsafety It is safe to call this function from any thread.
134 *
135 * \since This function is available since SDL 3.1.3.
136 */
138{
139 if (x && !(x & (x - 1))) {
140 return true;
141 }
142 return false;
143}
144
145/* Ends C function definitions when using C++ */
146#ifdef __cplusplus
147}
148#endif
149#include <SDL3/SDL_close_code.h>
150
151#endif /* SDL_bits_h_ */
#define SDL_FORCE_INLINE
SDL_FORCE_INLINE int SDL_MostSignificantBitIndex32(Uint32 x)
Definition SDL_bits.h:70
SDL_FORCE_INLINE bool SDL_HasExactlyOneBitSet32(Uint32 x)
Definition SDL_bits.h:137
uint32_t Uint32
Definition SDL_stdinc.h:370