summaryrefslogtreecommitdiffstats
path: root/libglusterfs
diff options
context:
space:
mode:
Diffstat (limited to 'libglusterfs')
-rw-r--r--libglusterfs/src/common-utils.c24
-rw-r--r--libglusterfs/src/common-utils.h11
2 files changed, 35 insertions, 0 deletions
diff --git a/libglusterfs/src/common-utils.c b/libglusterfs/src/common-utils.c
index 9a5f90b02f1..b62e69cf102 100644
--- a/libglusterfs/src/common-utils.c
+++ b/libglusterfs/src/common-utils.c
@@ -4494,3 +4494,27 @@ gf_zero_fill_stat (struct iatt *buf)
buf->ia_nlink = 0;
buf->ia_ctime = 0;
}
+
+int
+gf_bits_count (uint64_t n)
+{
+ int val = 0;
+#ifdef _GNU_SOURCE
+ val = __builtin_popcountll (n);
+#else
+ n -= (n >> 1) & 0x5555555555555555ULL;
+ n = ((n >> 2) & 0x3333333333333333ULL) + (n & 0x3333333333333333ULL);
+ n = (n + (n >> 4)) & 0x0F0F0F0F0F0F0F0FULL;
+ n += n >> 8;
+ n += n >> 16;
+ n += n >> 32;
+ val = n & 0xFF;
+#endif
+ return val;
+}
+
+int
+gf_bits_index (uint64_t n)
+{
+ return ffsll(n) - 1;
+}
diff --git a/libglusterfs/src/common-utils.h b/libglusterfs/src/common-utils.h
index f1c26a2d0c5..93dee58b079 100644
--- a/libglusterfs/src/common-utils.h
+++ b/libglusterfs/src/common-utils.h
@@ -25,6 +25,10 @@
#include <limits.h>
#include <fnmatch.h>
+#ifndef ffsll
+#define ffsll(x) __builtin_ffsll(x)
+#endif
+
void trap (void);
#define GF_UNIVERSAL_ANSWER 42 /* :O */
@@ -835,4 +839,11 @@ is_virtual_xattr (const char *k);
const char *
gf_inode_type_to_str (ia_type_t type);
+
+int32_t
+gf_bits_count (uint64_t n);
+
+int32_t
+gf_bits_index (uint64_t n);
+
#endif /* _COMMON_UTILS_H */