2012-04-30 18:38:31 +08:00
|
|
|
#ifndef strings_h
|
|
|
|
#define strings_h
|
|
|
|
|
|
|
|
/* MSVC doesn't define ffs/ffsl. This dummy strings.h header is provided
|
|
|
|
* for both */
|
2015-07-24 04:56:25 +08:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
# include <intrin.h>
|
|
|
|
# pragma intrinsic(_BitScanForward)
|
2017-01-16 08:56:30 +08:00
|
|
|
static __forceinline int ffsl(long x) {
|
2012-04-30 18:38:31 +08:00
|
|
|
unsigned long i;
|
|
|
|
|
2017-01-16 08:56:30 +08:00
|
|
|
if (_BitScanForward(&i, x)) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return i + 1;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2017-01-20 10:15:45 +08:00
|
|
|
return 0;
|
2012-04-30 18:38:31 +08:00
|
|
|
}
|
|
|
|
|
2017-01-16 08:56:30 +08:00
|
|
|
static __forceinline int ffs(int x) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return ffsl(x);
|
2012-04-30 18:38:31 +08:00
|
|
|
}
|
|
|
|
|
2016-02-24 03:39:02 +08:00
|
|
|
# ifdef _M_X64
|
|
|
|
# pragma intrinsic(_BitScanForward64)
|
|
|
|
# endif
|
|
|
|
|
2017-01-16 08:56:30 +08:00
|
|
|
static __forceinline int ffsll(unsigned __int64 x) {
|
2016-02-24 03:39:02 +08:00
|
|
|
unsigned long i;
|
|
|
|
#ifdef _M_X64
|
2017-01-16 08:56:30 +08:00
|
|
|
if (_BitScanForward64(&i, x)) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return i + 1;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2017-01-20 10:15:45 +08:00
|
|
|
return 0;
|
2016-02-24 03:39:02 +08:00
|
|
|
#else
|
|
|
|
// Fallback for 32-bit build where 64-bit version not available
|
|
|
|
// assuming little endian
|
|
|
|
union {
|
|
|
|
unsigned __int64 ll;
|
|
|
|
unsigned long l[2];
|
|
|
|
} s;
|
|
|
|
|
|
|
|
s.ll = x;
|
|
|
|
|
2017-01-16 08:56:30 +08:00
|
|
|
if (_BitScanForward(&i, s.l[0])) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return i + 1;
|
2017-01-16 08:56:30 +08:00
|
|
|
} else if(_BitScanForward(&i, s.l[1])) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return i + 33;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2017-01-20 10:15:45 +08:00
|
|
|
return 0;
|
2016-02-24 03:39:02 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2015-07-24 04:56:25 +08:00
|
|
|
#else
|
2016-02-24 03:39:02 +08:00
|
|
|
# define ffsll(x) __builtin_ffsll(x)
|
2015-07-24 04:56:25 +08:00
|
|
|
# define ffsl(x) __builtin_ffsl(x)
|
|
|
|
# define ffs(x) __builtin_ffs(x)
|
2012-04-30 18:38:31 +08:00
|
|
|
#endif
|
2015-07-24 04:56:25 +08:00
|
|
|
|
|
|
|
#endif /* strings_h */
|