Advertisement
illwieckz

alternatie ifdef

Jun 20th, 2024
541
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.33 KB | None | 0 0
  1. inline int CountTrailingZeroes(unsigned int x)
  2. {
  3.     #if defined(DAEMON_USE_COMPILER_INTRINSICS) && defined(__GNUC__)
  4.         return __builtin_ctz(x);
  5.     #elif defined(DAEMON_USE_COMPILER_INTRINSICS) && defined(_MSC_VER)
  6.         unsigned long ans;
  7.         _BitScanForward(&ans, x);
  8.         return ans;
  9.     #else
  10.         int i = 0;
  11.         while (i < 32 && !(x & 1))
  12.         {
  13.             ++i;
  14.             x >>= 1;
  15.         }
  16.         return i;
  17.     #endif
  18. }
  19.  
  20. inline int CountTrailingZeroes(unsigned long x)
  21. {
  22.     #if defined(DAEMON_USE_COMPILER_INTRINSICS) && defined(__GNUC__)
  23.         return __builtin_ctzl(x);
  24.     #elif defined(DAEMON_USE_COMPILER_INTRINSICS) && defined(_MSC_VER)
  25.         unsigned long ans;
  26.         _BitScanForward(&ans, x);
  27.         return ans;
  28.     #else
  29.         int i = 0;
  30.         while (i < 64 && !(x & 1))
  31.         {
  32.             ++i;
  33.             x >>= 1;
  34.         }
  35.         return i;
  36.     #endif
  37. }
  38.  
  39. inline int CountTrailingZeroes(unsigned long long x)
  40. {
  41.     #if defined(DAEMON_USE_COMPILER_INTRINSICS) && defined(__GNUC__)
  42.         return __builtin_ctzll(x);
  43.     #elif defined(DAEMON_USE_COMPILER_INTRINSICS) && defined(_MSC_VER)
  44.         unsigned long ans;
  45.         #ifdef _WIN64
  46.             _BitScanForward64(&ans, x);
  47.         #else
  48.             bool nonzero = _BitScanForward(&ans, static_cast<unsigned long>(x));
  49.             if (!nonzero)
  50.             {
  51.                 _BitScanForward(&ans, x >> 32);
  52.             }
  53.         #endif
  54.         return ans;
  55.     #else
  56.         int i = 0;
  57.         while (i < 64 && !(x & 1))
  58.         {
  59.             ++i;
  60.             x >>= 1;
  61.         }
  62.         return i;
  63.     #endif
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement