2
0

strings.h 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #ifndef strings_h
  2. #define strings_h
  3. /* MSVC doesn't define ffs/ffsl. This dummy strings.h header is provided
  4. * for both */
  5. #ifdef _MSC_VER
  6. # include <intrin.h>
  7. # pragma intrinsic(_BitScanForward)
  8. static __forceinline int ffsl(long x) {
  9. unsigned long i;
  10. if (_BitScanForward(&i, x)) {
  11. return i + 1;
  12. }
  13. return 0;
  14. }
  15. static __forceinline int ffs(int x) {
  16. return ffsl(x);
  17. }
  18. # ifdef _M_X64
  19. # pragma intrinsic(_BitScanForward64)
  20. # endif
  21. static __forceinline int ffsll(unsigned __int64 x) {
  22. unsigned long i;
  23. #ifdef _M_X64
  24. if (_BitScanForward64(&i, x)) {
  25. return i + 1;
  26. }
  27. return 0;
  28. #else
  29. // Fallback for 32-bit build where 64-bit version not available
  30. // assuming little endian
  31. union {
  32. unsigned __int64 ll;
  33. unsigned long l[2];
  34. } s;
  35. s.ll = x;
  36. if (_BitScanForward(&i, s.l[0])) {
  37. return i + 1;
  38. } else if(_BitScanForward(&i, s.l[1])) {
  39. return i + 33;
  40. }
  41. return 0;
  42. #endif
  43. }
  44. #else
  45. # define ffsll(x) __builtin_ffsll(x)
  46. # define ffsl(x) __builtin_ffsl(x)
  47. # define ffs(x) __builtin_ffs(x)
  48. #endif
  49. #endif /* strings_h */