summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEliot Horowitz <eliot@10gen.com>2012-10-14 08:37:43 -0400
committerEliot Horowitz <eliot@10gen.com>2012-10-14 08:42:32 -0400
commitb184c111a62d27c49507e4042413fcc88f7f42d2 (patch)
tree8b9e7218bd0512d1efd51348164c569b9c847b08
parent3a91169190b1876f6a5f74bbfb7ef1908e6cc5cd (diff)
downloadmongo-b184c111a62d27c49507e4042413fcc88f7f42d2.tar.gz
cross platform firstBitSet in prep for firstBitSet
-rw-r--r--src/mongo/platform/bits.h31
-rw-r--r--src/mongo/platform/bits_test.cpp13
2 files changed, 44 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
diff --git a/src/mongo/platform/bits_test.cpp b/src/mongo/platform/bits_test.cpp
index eb675ef00fb..6a0f40fb8b0 100644
--- a/src/mongo/platform/bits_test.cpp
+++ b/src/mongo/platform/bits_test.cpp
@@ -31,4 +31,17 @@ namespace mongo {
#endif
}
+#if defined(MONGO_SYSTEM_FFS)
+ TEST( BitsTest, FIRST_BIT_SET ) {
+ ASSERT_EQUALS( firstBitSet(0), mongo_firstBitSet(0) );
+ ASSERT_EQUALS( firstBitSet(0x1234), mongo_firstBitSet(0x1234) );
+
+ for ( int i = 0; i < 64; i++ ) {
+ unsigned long long x = 1ULL << i;
+ ASSERT_EQUALS( firstBitSet(x), mongo_firstBitSet(x) );
+ x &= 0x5;
+ ASSERT_EQUALS( firstBitSet(x), mongo_firstBitSet(x) );
+ }
+ }
+#endif
}