summaryrefslogtreecommitdiff
path: root/vendor/github.com/mitjafelicijan/go-tree-sitter/bits.h
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/mitjafelicijan/go-tree-sitter/bits.h')
-rw-r--r--vendor/github.com/mitjafelicijan/go-tree-sitter/bits.h42
1 files changed, 42 insertions, 0 deletions
diff --git a/vendor/github.com/mitjafelicijan/go-tree-sitter/bits.h b/vendor/github.com/mitjafelicijan/go-tree-sitter/bits.h
new file mode 100644
index 0000000..ca8caf3
--- /dev/null
+++ b/vendor/github.com/mitjafelicijan/go-tree-sitter/bits.h
@@ -0,0 +1,42 @@
1#ifndef TREE_SITTER_BITS_H_
2#define TREE_SITTER_BITS_H_
3
4#include <stdint.h>
5
6static inline uint32_t bitmask_for_index(uint16_t id) {
7 return (1u << (31 - id));
8}
9
10#ifdef __TINYC__
11
12// Algorithm taken from the Hacker's Delight book
13// See also https://graphics.stanford.edu/~seander/bithacks.html
14static inline uint32_t count_leading_zeros(uint32_t x) {
15 int count = 0;
16 if (x == 0) return 32;
17 x = x - ((x >> 1) & 0x55555555);
18 x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
19 count = (((x + (x >> 4)) & 0x0f0f0f0f) * 0x01010101) >> 24;
20 return count;
21}
22
23#elif defined _WIN32 && !defined __GNUC__
24
25#include <intrin.h>
26
27static inline uint32_t count_leading_zeros(uint32_t x) {
28 if (x == 0) return 32;
29 uint32_t result;
30 _BitScanReverse(&result, x);
31 return 31 - result;
32}
33
34#else
35
36static inline uint32_t count_leading_zeros(uint32_t x) {
37 if (x == 0) return 32;
38 return __builtin_clz(x);
39}
40
41#endif
42#endif // TREE_SITTER_BITS_H_