fallback_builtins.h 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #ifndef FALLBACK_BUILTINS_H
  2. #define FALLBACK_BUILTINS_H
  3. #if defined(_MSC_VER) && !defined(__clang__)
  4. #if defined(_M_IX86) || defined(_M_AMD64) || defined(_M_IA64) || defined(_M_ARM) || defined(_M_ARM64) || defined(_M_ARM64EC)
  5. #include <intrin.h>
  6. /* This is not a general purpose replacement for __builtin_ctz. The function expects that value is != 0.
  7. * Because of that assumption trailing_zero is not initialized and the return value is not checked.
  8. * Tzcnt and bsf give identical results except when input value is 0, therefore this can not be allowed.
  9. * If tzcnt instruction is not supported, the cpu will itself execute bsf instead.
  10. * Performance tzcnt/bsf is identical on Intel cpu, tzcnt is faster than bsf on AMD cpu.
  11. */
  12. static __forceinline int __builtin_ctz(unsigned int value) {
  13. Assert(value != 0, "Invalid input value: 0");
  14. # if defined(X86_FEATURES) && !(_MSC_VER < 1700)
  15. return (int)_tzcnt_u32(value);
  16. # else
  17. unsigned long trailing_zero;
  18. _BitScanForward(&trailing_zero, value);
  19. return (int)trailing_zero;
  20. # endif
  21. }
  22. #define HAVE_BUILTIN_CTZ
  23. #ifdef _M_AMD64
  24. /* This is not a general purpose replacement for __builtin_ctzll. The function expects that value is != 0.
  25. * Because of that assumption trailing_zero is not initialized and the return value is not checked.
  26. */
  27. static __forceinline int __builtin_ctzll(unsigned long long value) {
  28. Assert(value != 0, "Invalid input value: 0");
  29. # if defined(X86_FEATURES) && !(_MSC_VER < 1700)
  30. return (int)_tzcnt_u64(value);
  31. # else
  32. unsigned long trailing_zero;
  33. _BitScanForward64(&trailing_zero, value);
  34. return (int)trailing_zero;
  35. # endif
  36. }
  37. #define HAVE_BUILTIN_CTZLL
  38. #endif // Microsoft AMD64
  39. #endif // Microsoft AMD64/IA64/x86/ARM/ARM64 test
  40. #endif // _MSC_VER & !clang
  41. #endif // include guard FALLBACK_BUILTINS_H