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
36
extern
"C"
{
37
#endif
38
39
/**
40
* \file SDL_bits.h
41
*/
42
43
#if defined(__WATCOMC__) && defined(__386__)
44
extern
__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.0.0.
69
*/
70
SDL_FORCE_INLINE
int
SDL_MostSignificantBitIndex32
(
Uint32
x)
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
}
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 SDL_FALSE. If any one bit is exclusively set, this returns
124
* SDL_TRUE.
125
*
126
* Note that this is a forced-inline function in a header, and not a public
127
* API function available in the SDL library (which is to say, the code is
128
* embedded in the calling program and the linker and dynamic loader will not
129
* be able to find this function inside SDL itself).
130
*
131
* \param x the 32-bit value to examine.
132
* \returns SDL_TRUE if exactly one bit is set in `x`, SDL_FALSE otherwise.
133
*
134
* \threadsafety It is safe to call this function from any thread.
135
*
136
* \since This function is available since SDL 3.0.0.
137
*/
138
SDL_FORCE_INLINE
SDL_bool
SDL_HasExactlyOneBitSet32
(
Uint32
x)
139
{
140
if
(x && !(x & (x - 1))) {
141
return
SDL_TRUE
;
142
}
143
return
SDL_FALSE
;
144
}
145
146
/* Ends C function definitions when using C++ */
147
#ifdef __cplusplus
148
}
149
#endif
150
#include <
SDL3/SDL_close_code.h
>
151
152
#endif
/* SDL_bits_h_ */
SDL_begin_code.h
SDL_FORCE_INLINE
#define SDL_FORCE_INLINE
Definition
SDL_begin_code.h:128
SDL_HasExactlyOneBitSet32
SDL_FORCE_INLINE SDL_bool SDL_HasExactlyOneBitSet32(Uint32 x)
Definition
SDL_bits.h:138
SDL_MostSignificantBitIndex32
SDL_FORCE_INLINE int SDL_MostSignificantBitIndex32(Uint32 x)
Definition
SDL_bits.h:70
SDL_close_code.h
SDL_stdinc.h
SDL_TRUE
#define SDL_TRUE
Definition
SDL_stdinc.h:203
SDL_FALSE
#define SDL_FALSE
Definition
SDL_stdinc.h:194
SDL_bool
int SDL_bool
Definition
SDL_stdinc.h:213
Uint32
uint32_t Uint32
Definition
SDL_stdinc.h:265
include
SDL3
SDL_bits.h
Generated by
1.9.8