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.
 
 
 
 
 
 

113 line
2.9 KiB

  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2018 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 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_clz_watcom (Uint32);
  43. #pragma aux _SDL_clz_watcom = \
  44. "bsr eax, eax" \
  45. "xor eax, 31" \
  46. parm [eax] nomemory \
  47. value [eax] \
  48. modify exact [eax] nomemory;
  49. #endif
  50. SDL_FORCE_INLINE int
  51. SDL_MostSignificantBitIndex32(Uint32 x)
  52. {
  53. #if defined(__GNUC__) && (__GNUC__ >= 4 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4))
  54. /* Count Leading Zeroes builtin in GCC.
  55. * http://gcc.gnu.org/onlinedocs/gcc-4.3.4/gcc/Other-Builtins.html
  56. */
  57. if (x == 0) {
  58. return -1;
  59. }
  60. return 31 - __builtin_clz(x);
  61. #elif defined(__WATCOMC__) && defined(__386__)
  62. if (x == 0) {
  63. return -1;
  64. }
  65. return 31 - _SDL_clz_watcom(x);
  66. #else
  67. /* Based off of Bit Twiddling Hacks by Sean Eron Anderson
  68. * <seander@cs.stanford.edu>, released in the public domain.
  69. * http://graphics.stanford.edu/~seander/bithacks.html#IntegerLog
  70. */
  71. const Uint32 b[] = {0x2, 0xC, 0xF0, 0xFF00, 0xFFFF0000};
  72. const int S[] = {1, 2, 4, 8, 16};
  73. int msbIndex = 0;
  74. int i;
  75. if (x == 0) {
  76. return -1;
  77. }
  78. for (i = 4; i >= 0; i--)
  79. {
  80. if (x & b[i])
  81. {
  82. x >>= S[i];
  83. msbIndex |= S[i];
  84. }
  85. }
  86. return msbIndex;
  87. #endif
  88. }
  89. /* Ends C function definitions when using C++ */
  90. #ifdef __cplusplus
  91. }
  92. #endif
  93. #include "close_code.h"
  94. #endif /* SDL_bits_h_ */
  95. /* vi: set ts=4 sw=4 expandtab: */