From 5a8dbc6347b3541e84fe669b22c17ad3b715e258 Mon Sep 17 00:00:00 2001 From: Mitja Felicijan Date: Wed, 21 Jan 2026 20:22:09 +0100 Subject: Engage! --- .../mitjafelicijan/go-tree-sitter/bits.h | 42 ++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 vendor/github.com/mitjafelicijan/go-tree-sitter/bits.h (limited to 'vendor/github.com/mitjafelicijan/go-tree-sitter/bits.h') 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 @@ +#ifndef TREE_SITTER_BITS_H_ +#define TREE_SITTER_BITS_H_ + +#include + +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 + +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_ -- cgit v1.2.3