SDL_bits.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. /**
  19. * # CategoryBits
  20. *
  21. * Functions for fiddling with bits and bitmasks.
  22. */
  23. #ifndef SDL_bits_h_
  24. #define SDL_bits_h_
  25. #include <SDL3/SDL_stdinc.h>
  26. #include <SDL3/SDL_begin_code.h>
  27. /* Set up for C function definitions, even when using C++ */
  28. #ifdef __cplusplus
  29. extern "C" {
  30. #endif
  31. #if defined(__WATCOMC__) && defined(__386__)
  32. extern __inline int _SDL_bsr_watcom(Uint32);
  33. #pragma aux _SDL_bsr_watcom = \
  34. "bsr eax, eax" \
  35. parm [eax] nomemory \
  36. value [eax] \
  37. modify exact [eax] nomemory;
  38. #endif
  39. /**
  40. * Get the index of the most significant (set) bit in a 32-bit number.
  41. *
  42. * Result is undefined when called with 0. This operation can also be stated
  43. * as "count leading zeroes" and "log base 2".
  44. *
  45. * Note that this is a forced-inline function in a header, and not a public
  46. * API function available in the SDL library (which is to say, the code is
  47. * embedded in the calling program and the linker and dynamic loader will not
  48. * be able to find this function inside SDL itself).
  49. *
  50. * \param x the 32-bit value to examine.
  51. * \returns the index of the most significant bit, or -1 if the value is 0.
  52. *
  53. * \threadsafety It is safe to call this function from any thread.
  54. *
  55. * \since This function is available since SDL 3.2.0.
  56. */
  57. SDL_FORCE_INLINE int SDL_MostSignificantBitIndex32(Uint32 x)
  58. {
  59. #if defined(__GNUC__) && (__GNUC__ >= 4 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4))
  60. /* Count Leading Zeroes builtin in GCC.
  61. * http://gcc.gnu.org/onlinedocs/gcc-4.3.4/gcc/Other-Builtins.html
  62. */
  63. if (x == 0) {
  64. return -1;
  65. }
  66. return 31 - __builtin_clz(x);
  67. #elif defined(__WATCOMC__) && defined(__386__)
  68. if (x == 0) {
  69. return -1;
  70. }
  71. return _SDL_bsr_watcom(x);
  72. #elif defined(_MSC_VER) && _MSC_VER >= 1400
  73. unsigned long index;
  74. if (_BitScanReverse(&index, x)) {
  75. return (int)index;
  76. }
  77. return -1;
  78. #else
  79. /* Based off of Bit Twiddling Hacks by Sean Eron Anderson
  80. * <seander@cs.stanford.edu>, released in the public domain.
  81. * http://graphics.stanford.edu/~seander/bithacks.html#IntegerLog
  82. */
  83. const Uint32 b[] = {0x2, 0xC, 0xF0, 0xFF00, 0xFFFF0000};
  84. const int S[] = {1, 2, 4, 8, 16};
  85. int msbIndex = 0;
  86. int i;
  87. if (x == 0) {
  88. return -1;
  89. }
  90. for (i = 4; i >= 0; i--)
  91. {
  92. if (x & b[i])
  93. {
  94. x >>= S[i];
  95. msbIndex |= S[i];
  96. }
  97. }
  98. return msbIndex;
  99. #endif
  100. }
  101. /**
  102. * Determine if a unsigned 32-bit value has exactly one bit set.
  103. *
  104. * If there are no bits set (`x` is zero), or more than one bit set, this
  105. * returns false. If any one bit is exclusively set, this returns true.
  106. *
  107. * Note that this is a forced-inline function in a header, and not a public
  108. * API function available in the SDL library (which is to say, the code is
  109. * embedded in the calling program and the linker and dynamic loader will not
  110. * be able to find this function inside SDL itself).
  111. *
  112. * \param x the 32-bit value to examine.
  113. * \returns true if exactly one bit is set in `x`, false otherwise.
  114. *
  115. * \threadsafety It is safe to call this function from any thread.
  116. *
  117. * \since This function is available since SDL 3.2.0.
  118. */
  119. SDL_FORCE_INLINE bool SDL_HasExactlyOneBitSet32(Uint32 x)
  120. {
  121. if (x && !(x & (x - 1))) {
  122. return true;
  123. }
  124. return false;
  125. }
  126. /* Ends C function definitions when using C++ */
  127. #ifdef __cplusplus
  128. }
  129. #endif
  130. #include <SDL3/SDL_close_code.h>
  131. #endif /* SDL_bits_h_ */