ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
bits.h
Go to the documentation of this file.
1#pragma once
2
28
29#include <stdbool.h>
30#include <stddef.h>
31#include <stdint.h>
32
33#if defined(_MSC_VER) && !defined(__clang__)
34#include <intrin.h>
35
36#ifdef __cplusplus
37extern "C" {
38#endif
39#endif
40
41/* ============================================================================
42 * Power-of-Two Operations
43 * ============================================================================
44 */
45
65static inline bool is_power_of_two(size_t n) { return n && !(n & (n - 1)); }
66
87static inline size_t next_power_of_two(size_t n) {
88 if (n == 0)
89 return 1;
90 n--;
91 n |= n >> 1;
92 n |= n >> 2;
93 n |= n >> 4;
94 n |= n >> 8;
95 n |= n >> 16;
96#if SIZE_MAX > UINT32_MAX
97 n |= n >> 32;
98#endif
99 return n + 1;
100}
101
102/* ============================================================================
103 * Leading/Trailing Zero Detection
104 * ============================================================================
105 */
106
129static inline int find_first_set_bit(uint64_t mask) {
130#if defined(__GNUC__) || defined(__clang__)
131 // GCC/Clang builtin (fast hardware instruction on modern CPUs)
132 if (mask == 0)
133 return 64;
134 return __builtin_ctzll(mask);
135#elif defined(_MSC_VER) && defined(_M_X64)
136 // Microsoft Visual C++ x64 intrinsic
137 unsigned long index;
138 if (_BitScanForward64(&index, mask)) {
139 return (int)index;
140 }
141 return 64; // No bits set
142#elif defined(_MSC_VER)
143 // MSVC x86 fallback
144 unsigned long index;
145 if (_BitScanForward(&index, (unsigned long)mask)) {
146 return (int)index;
147 }
148 if (_BitScanForward(&index, (unsigned long)(mask >> 32))) {
149 return 32 + (int)index;
150 }
151 return 64; // No bits set
152#else
153 // De Bruijn sequence fallback: O(1) with ~4-5 CPU cycles
154 // Portable and production-tested (used in LLVM, GCC)
155 if (mask == 0)
156 return 64;
157
158 static const int debruijn_table[64] = {
159 0, 1, 2, 53, 3, 7, 54, 27, 4, 38, 41, 8, 34, 55, 48, 28,
160 62, 5, 39, 46, 44, 42, 22, 9, 24, 35, 59, 56, 49, 18, 29, 11,
161 63, 52, 6, 26, 37, 40, 33, 47, 61, 45, 43, 21, 23, 58, 17, 10,
162 51, 25, 36, 32, 60, 20, 57, 16, 50, 31, 19, 15, 30, 14, 13, 12};
163
164 // Isolate rightmost bit and hash using De Bruijn multiplication
165 return debruijn_table[((mask & -mask) * 0x022fdd63cc95386dULL) >> 58];
166#endif
167}
168
169/* ============================================================================
170 * Bit Counting Operations
171 * ============================================================================
172 */
173
191static inline int count_set_bits(uint64_t x) {
192#if defined(__GNUC__) || defined(__clang__)
193 return __builtin_popcountll(x);
194#elif defined(_MSC_VER) && defined(_M_X64)
195 return (int)__popcnt64(x);
196#else
197 // Software fallback
198 int count = 0;
199 while (x) {
200 count += x & 1;
201 x >>= 1;
202 }
203 return count;
204#endif
205}
206
208
209#ifdef __cplusplus
210}
211#endif
unsigned long long uint64_t
static bool size_t index
Definition queries.h:10