forked from smacker/go-tree-sitter
-
Notifications
You must be signed in to change notification settings - Fork 2
/
bits.h
42 lines (32 loc) · 941 Bytes
/
bits.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#ifndef TREE_SITTER_BITS_H_
#define TREE_SITTER_BITS_H_
#include <stdint.h>
static inline uint32_t bitmask_for_index(uint16_t id) {
return (1u << (31 - id));
}
#ifdef __TINYC__
// Algorithm taken from the Hacker's Delight book
// See also https://graphics.stanford.edu/~seander/bithacks.html
static inline uint32_t count_leading_zeros(uint32_t x) {
int count = 0;
if (x == 0) return 32;
x = x - ((x >> 1) & 0x55555555);
x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
count = (((x + (x >> 4)) & 0x0f0f0f0f) * 0x01010101) >> 24;
return count;
}
#elif defined _WIN32 && !defined __GNUC__
#include <intrin.h>
static inline uint32_t count_leading_zeros(uint32_t x) {
if (x == 0) return 32;
uint32_t result;
_BitScanReverse(&result, x);
return 31 - result;
}
#else
static inline uint32_t count_leading_zeros(uint32_t x) {
if (x == 0) return 32;
return __builtin_clz(x);
}
#endif
#endif // TREE_SITTER_BITS_H_