🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

126 lines
3.1 KiB

  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2022 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. * \file SDL_bits.h
  20. *
  21. * Functions for fiddling with bits and bitmasks.
  22. */
  23. #ifndef SDL_bits_h_
  24. #define SDL_bits_h_
  25. #include "SDL_stdinc.h"
  26. #include "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. /**
  35. * Get the index of the most significant bit. Result is undefined when called
  36. * with 0. This operation can also be stated as "count leading zeroes" and
  37. * "log base 2".
  38. *
  39. * \return the index of the most significant bit, or -1 if the value is 0.
  40. */
  41. #if defined(__WATCOMC__) && defined(__386__)
  42. extern __inline int _SDL_bsr_watcom(Uint32);
  43. #pragma aux _SDL_bsr_watcom = \
  44. "bsr eax, eax" \
  45. parm [eax] nomemory \
  46. value [eax] \
  47. modify exact [eax] nomemory;
  48. #endif
  49. SDL_FORCE_INLINE int
  50. SDL_MostSignificantBitIndex32(Uint32 x)
  51. {
  52. #if defined(__GNUC__) && (__GNUC__ >= 4 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4))
  53. /* Count Leading Zeroes builtin in GCC.
  54. * http://gcc.gnu.org/onlinedocs/gcc-4.3.4/gcc/Other-Builtins.html
  55. */
  56. if (x == 0) {
  57. return -1;
  58. }
  59. return 31 - __builtin_clz(x);
  60. #elif defined(__WATCOMC__) && defined(__386__)
  61. if (x == 0) {
  62. return -1;
  63. }
  64. return _SDL_bsr_watcom(x);
  65. #elif defined(_MSC_VER)
  66. unsigned long index;
  67. if (_BitScanReverse(&index, x)) {
  68. return index;
  69. }
  70. return -1;
  71. #else
  72. /* Based off of Bit Twiddling Hacks by Sean Eron Anderson
  73. * <seander@cs.stanford.edu>, released in the public domain.
  74. * http://graphics.stanford.edu/~seander/bithacks.html#IntegerLog
  75. */
  76. const Uint32 b[] = {0x2, 0xC, 0xF0, 0xFF00, 0xFFFF0000};
  77. const int S[] = {1, 2, 4, 8, 16};
  78. int msbIndex = 0;
  79. int i;
  80. if (x == 0) {
  81. return -1;
  82. }
  83. for (i = 4; i >= 0; i--)
  84. {
  85. if (x & b[i])
  86. {
  87. x >>= S[i];
  88. msbIndex |= S[i];
  89. }
  90. }
  91. return msbIndex;
  92. #endif
  93. }
  94. SDL_FORCE_INLINE SDL_bool
  95. SDL_HasExactlyOneBitSet32(Uint32 x)
  96. {
  97. if (x && !(x & (x - 1))) {
  98. return SDL_TRUE;
  99. }
  100. return SDL_FALSE;
  101. }
  102. /* Ends C function definitions when using C++ */
  103. #ifdef __cplusplus
  104. }
  105. #endif
  106. #include "close_code.h"
  107. #endif /* SDL_bits_h_ */
  108. /* vi: set ts=4 sw=4 expandtab: */