diff options
author | Eliot Horowitz <eliot@10gen.com> | 2012-10-14 08:37:43 -0400 |
---|---|---|
committer | Eliot Horowitz <eliot@10gen.com> | 2012-10-14 08:42:32 -0400 |
commit | b184c111a62d27c49507e4042413fcc88f7f42d2 (patch) | |
tree | 8b9e7218bd0512d1efd51348164c569b9c847b08 /src/mongo/platform/bits.h | |
parent | 3a91169190b1876f6a5f74bbfb7ef1908e6cc5cd (diff) | |
download | mongo-b184c111a62d27c49507e4042413fcc88f7f42d2.tar.gz |
cross platform firstBitSet in prep for firstBitSet
Diffstat (limited to 'src/mongo/platform/bits.h')
-rw-r--r-- | src/mongo/platform/bits.h | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/src/mongo/platform/bits.h b/src/mongo/platform/bits.h index bce1e691d2e..7afc428556b 100644 --- a/src/mongo/platform/bits.h +++ b/src/mongo/platform/bits.h @@ -27,3 +27,34 @@ #error "unknown platform" #endif +namespace mongo { + // defined here so can test on linux + inline int mongo_firstBitSet( unsigned long long v ) { + if ( v == 0 ) + return 0; + + for ( int i = 0; i<8; i++ ) { + unsigned long long x = ( v >> ( 8 * i ) ) & 0xFF; + if ( x == 0 ) + continue; + + for ( int j = 0; j < 8; j++ ) { + if ( ( x >> j ) & 0x1 ) + return ( i * 8 ) + j + 1; + } + } + + return 0; + } +} + + +#if defined(__linux__) +#define firstBitSet ffsll +#define MONGO_SYSTEM_FFS 1 +#elif defined(__MACH__) && defined(MONGO_PLATFORM_64) +#define firstBitSet ffsl +#define MONGO_SYSTEM_FFS 1 +#else +#define firstBitSet mongo::mongo_firstBitSet +#endif |