SDL_bits.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2024 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. /**
  32. * \file SDL_bits.h
  33. */
  34. #if defined(__WATCOMC__) && defined(__386__)
  35. extern __inline int _SDL_bsr_watcom(Uint32);
  36. #pragma aux _SDL_bsr_watcom = \
  37. "bsr eax, eax" \
  38. parm [eax] nomemory \
  39. value [eax] \
  40. modify exact [eax] nomemory;
  41. #endif
  42. /**
  43. * Get the index of the most significant (set) bit in a 32-bit number.
  44. *
  45. * Result is undefined when called with 0. This operation can also be stated
  46. * as "count leading zeroes" and "log base 2".
  47. *
  48. * Note that this is a forced-inline function in a header, and not a public
  49. * API function available in the SDL library (which is to say, the code is
  50. * embedded in the calling program and the linker and dynamic loader will not
  51. * be able to find this function inside SDL itself).
  52. *
  53. * \param x the 32-bit value to examine.
  54. * \returns the index of the most significant bit, or -1 if the value is 0.
  55. *
  56. * \threadsafety It is safe to call this function from any thread.
  57. *
  58. * \since This function is available since SDL 3.1.3.
  59. */
  60. SDL_FORCE_INLINE int SDL_MostSignificantBitIndex32(Uint32 x)
  61. {
  62. #if defined(__GNUC__) && (__GNUC__ >= 4 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4))
  63. /* Count Leading Zeroes builtin in GCC.
  64. * http://gcc.gnu.org/onlinedocs/gcc-4.3.4/gcc/Other-Builtins.html
  65. */
  66. if (x == 0) {
  67. return -1;
  68. }
  69. return 31 - __builtin_clz(x);
  70. #elif defined(__WATCOMC__) && defined(__386__)
  71. if (x == 0) {
  72. return -1;
  73. }
  74. return _SDL_bsr_watcom(x);
  75. #elif defined(_MSC_VER)
  76. unsigned long index;
  77. if (_BitScanReverse(&index, x)) {
  78. return (int)index;
  79. }
  80. return -1;
  81. #else
  82. /* Based off of Bit Twiddling Hacks by Sean Eron Anderson
  83. * <seander@cs.stanford.edu>, released in the public domain.
  84. * http://graphics.stanford.edu/~seander/bithacks.html#IntegerLog
  85. */
  86. const Uint32 b[] = {0x2, 0xC, 0xF0, 0xFF00, 0xFFFF0000};
  87. const int S[] = {1, 2, 4, 8, 16};
  88. int msbIndex = 0;
  89. int i;
  90. if (x == 0) {
  91. return -1;
  92. }
  93. for (i = 4; i >= 0; i--)
  94. {
  95. if (x & b[i])
  96. {
  97. x >>= S[i];
  98. msbIndex |= S[i];
  99. }
  100. }
  101. return msbIndex;
  102. #endif
  103. }
  104. /**
  105. * Determine if a unsigned 32-bit value has exactly one bit set.
  106. *
  107. * If there are no bits set (`x` is zero), or more than one bit set, this
  108. * returns false. If any one bit is exclusively set, this returns true.
  109. *
  110. * Note that this is a forced-inline function in a header, and not a public
  111. * API function available in the SDL library (which is to say, the code is
  112. * embedded in the calling program and the linker and dynamic loader will not
  113. * be able to find this function inside SDL itself).
  114. *
  115. * \param x the 32-bit value to examine.
  116. * \returns true if exactly one bit is set in `x`, false otherwise.
  117. *
  118. * \threadsafety It is safe to call this function from any thread.
  119. *
  120. * \since This function is available since SDL 3.1.3.
  121. */
  122. SDL_FORCE_INLINE bool SDL_HasExactlyOneBitSet32(Uint32 x)
  123. {
  124. if (x && !(x & (x - 1))) {
  125. return true;
  126. }
  127. return false;
  128. }
  129. /* Ends C function definitions when using C++ */
  130. #ifdef __cplusplus
  131. }
  132. #endif
  133. #include <SDL3/SDL_close_code.h>
  134. #endif /* SDL_bits_h_ */