SDL_bits.h 730 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #ifndef _SDL_bits_h
  2. #define _SDL_bits_h
  3. #include "SDL_stdinc.h"
  4. #include "begin_code.h"
  5. #ifdef __cplusplus
  6. extern "C" {
  7. #endif
  8. SDL_FORCE_INLINE int
  9. SDL_MostSignificantBitIndex32(Uint32 x)
  10. {
  11. #if defined(__GNUC__) && __GNUC__ >= 4
  12. if (x == 0) {
  13. return -1;
  14. }
  15. return 31 - __builtin_clz(x);
  16. #else
  17. const Uint32 b[] = {0x2, 0xC, 0xF0, 0xFF00, 0xFFFF0000};
  18. const int S[] = {1, 2, 4, 8, 16};
  19. int msbIndex = 0;
  20. int i;
  21. if (x == 0) {
  22. return -1;
  23. }
  24. for (i = 4; i >= 0; i--)
  25. {
  26. if (x & b[i])
  27. {
  28. x >>= S[i];
  29. msbIndex |= S[i];
  30. }
  31. }
  32. return msbIndex;
  33. #endif
  34. }
  35. #ifdef __cplusplus
  36. }
  37. #endif
  38. #include "close_code.h"
  39. #endif