aboutsummaryrefslogtreecommitdiff
path: root/src/nvim/math.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/nvim/math.c')
-rw-r--r--src/nvim/math.c17
1 files changed, 17 insertions, 0 deletions
diff --git a/src/nvim/math.c b/src/nvim/math.c
index 47a667416c..1ccf4d7806 100644
--- a/src/nvim/math.c
+++ b/src/nvim/math.c
@@ -77,6 +77,23 @@ int xctz(uint64_t x)
#endif
}
+/// Count number of set bits in bit field.
+int popcount(uint64_t x)
+{
+ // Use compiler builtin if possible.
+#if defined(__clang__) || defined(__GNUC__)
+ return __builtin_popcountll(x);
+#else
+ int count = 0;
+ for (; x != 0; x >>= 1) {
+ if (x & 1) {
+ count++;
+ }
+ }
+ return count;
+#endif
+}
+
/// For overflow detection, add a digit safely to an int value.
int vim_append_digit_int(int *value, int digit)
{