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.c38
1 files changed, 38 insertions, 0 deletions
diff --git a/src/nvim/math.c b/src/nvim/math.c
index 9a0825823c..1ccf4d7806 100644
--- a/src/nvim/math.c
+++ b/src/nvim/math.c
@@ -1,10 +1,16 @@
// uncrustify:off
#include <math.h>
// uncrustify:on
+#include <limits.h>
#include <stdint.h>
#include <string.h>
+#ifdef HAVE_BITSCANFORWARD64
+# include <intrin.h> // Required for _BitScanForward64
+#endif
+
#include "nvim/math.h"
+#include "nvim/vim_defs.h"
#ifdef INCLUDE_GENERATED_DECLARATIONS
# include "math.c.generated.h"
@@ -52,6 +58,10 @@ int xctz(uint64_t x)
// Use compiler builtin if possible.
#if defined(__clang__) || (defined(__GNUC__) && (__GNUC__ >= 4))
return __builtin_ctzll(x);
+#elif defined(HAVE_BITSCANFORWARD64)
+ unsigned long index;
+ _BitScanForward64(&index, x);
+ return (int)index;
#else
int count = 0;
// Set x's trailing zeroes to ones and zero the rest.
@@ -66,3 +76,31 @@ int xctz(uint64_t x)
return count;
#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)
+{
+ int x = *value;
+ if (x > ((INT_MAX - digit) / 10)) {
+ return FAIL;
+ }
+ *value = x * 10 + digit;
+ return OK;
+}