diff options
author | Josh Rahm <joshuarahm@gmail.com> | 2024-05-24 19:18:11 +0000 |
---|---|---|
committer | Josh Rahm <joshuarahm@gmail.com> | 2024-05-24 19:18:11 +0000 |
commit | ff7ed8f586589d620a806c3758fac4a47a8e7e15 (patch) | |
tree | 729bbcb92231538fa61dab6c3d890b025484b7f5 /src/nvim/math.c | |
parent | 376914f419eb08fdf4c1a63a77e1f035898a0f10 (diff) | |
parent | 28c04948a1c887a1cc0cb64de79fa32631700466 (diff) | |
download | rneovim-ff7ed8f586589d620a806c3758fac4a47a8e7e15.tar.gz rneovim-ff7ed8f586589d620a806c3758fac4a47a8e7e15.tar.bz2 rneovim-ff7ed8f586589d620a806c3758fac4a47a8e7e15.zip |
Merge remote-tracking branch 'upstream/master' into mix_20240309
Diffstat (limited to 'src/nvim/math.c')
-rw-r--r-- | src/nvim/math.c | 38 |
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; +} |