1 // Copyright © Tavian Barnes <tavianator@tavianator.com>
2 // SPDX-License-Identifier: 0BSD
15 #if __STDC_VERSION__ >= 202311L
19 // C23 polyfill: _WIDTH macros
21 // The U*_MAX macros are of the form 2**n - 1, and we want to extract the n.
22 // One way would be *_WIDTH = popcount(*_MAX). Alternatively, we can use
23 // Hallvard B. Furuseth's technique from [1], which is shorter.
25 // [1]: https://groups.google.com/g/comp.lang.c/c/NfedEFBFJ0k
27 // Let mask be of the form 2**m - 1, e.g. 0b111, and let n range over
28 // [0b0, 0b1, 0b11, 0b111, 0b1111, ...]. Then we have
31 // == [0b0, 0b1, 0b11, 0b0, 0b1, 0b11, ...]
32 // n / (n % 0b111 + 1)
33 // == [0b0 (x3), 0b111 (x3), 0b111111 (x3), ...]
34 // n / (n % 0b111 + 1) / 0b111
35 // == [0b0 (x3), 0b1 (x3), 0b1001 (x3), 0b1001001 (x3), ...]
36 // n / (n % 0b111 + 1) / 0b111 % 0b111
37 // == [0 (x3), 1 (x3), 2 (x3), ...]
38 // == UMAX_CHUNK(n, 0b111)
39 #define UMAX_CHUNK(n, mask) (n / (n % mask + 1) / mask % mask)
41 // 8 * UMAX_CHUNK(n, 255) gives [0 (x8), 8 (x8), 16 (x8), ...]. To that we add
42 // [0, 1, 2, ..., 6, 7, 0, 1, ...], which we get from a linear interpolation on
46 // == [0, 1, 3, 7, 15, 31, 63, 127, 0, ...]
47 // 86 / (n % 255 + 12)
48 // == [7, 6, 5, 4, 3, 2, 1, 0, 7, ...]
49 #define UMAX_INTERP(n) (7 - 86 / (n % 255 + 12))
51 #define UMAX_WIDTH(n) (8 * UMAX_CHUNK(n, 255) + UMAX_INTERP(n))
54 # define CHAR_WIDTH CHAR_BIT
57 // See https://gcc.gnu.org/onlinedocs/cpp/Common-Predefined-Macros.html
60 # ifdef __SHRT_WIDTH__
61 # define USHRT_WIDTH __SHRT_WIDTH__
63 # define USHRT_WIDTH UMAX_WIDTH(USHRT_MAX)
69 # define UINT_WIDTH __INT_WIDTH__
71 # define UINT_WIDTH UMAX_WIDTH(UINT_MAX)
76 # ifdef __LONG_WIDTH__
77 # define ULONG_WIDTH __LONG_WIDTH__
79 # define ULONG_WIDTH UMAX_WIDTH(ULONG_MAX)
84 # ifdef __LONG_LONG_WIDTH__
85 # define ULLONG_WIDTH __LONG_LONG_WIDTH__
86 # elif defined(__LLONG_WIDTH__) // Clang
87 # define ULLONG_WIDTH __LLONG_WIDTH__
89 # define ULLONG_WIDTH UMAX_WIDTH(ULLONG_MAX)
94 # ifdef __SIZE_WIDTH__
95 # define SIZE_WIDTH __SIZE_WIDTH__
97 # define SIZE_WIDTH UMAX_WIDTH(SIZE_MAX)
101 #ifndef PTRDIFF_WIDTH
102 # ifdef __PTRDIFF_WIDTH__
103 # define PTRDIFF_WIDTH __PTRDIFF_WIDTH__
105 # define PTRDIFF_WIDTH UMAX_WIDTH(PTRDIFF_MAX)
109 #ifndef UINTPTR_WIDTH
110 # ifdef __INTPTR_WIDTH__
111 # define UINTPTR_WIDTH __INTPTR_WIDTH__
113 # define UINTPTR_WIDTH UMAX_WIDTH(UINTPTR_MAX)
117 #ifndef UINTMAX_WIDTH
118 # ifdef __INTMAX_WIDTH__
119 # define UINTMAX_WIDTH __INTMAX_WIDTH__
121 # define UINTMAX_WIDTH UMAX_WIDTH(UINTMAX_MAX)
126 # define UCHAR_WIDTH CHAR_WIDTH
129 # define SCHAR_WIDTH CHAR_WIDTH
132 # define SHRT_WIDTH USHRT_WIDTH
135 # define INT_WIDTH UINT_WIDTH
138 # define LONG_WIDTH ULONG_WIDTH
141 # define LLONG_WIDTH ULLONG_WIDTH
144 # define INTPTR_WIDTH UINTPTR_WIDTH
147 # define INTMAX_WIDTH UINTMAX_WIDTH
150 // C23 polyfill: byte order
152 #ifdef __STDC_ENDIAN_LITTLE__
153 # define ENDIAN_LITTLE __STDC_ENDIAN_LITTLE__
154 #elif defined(__ORDER_LITTLE_ENDIAN__)
155 # define ENDIAN_LITTLE __ORDER_LITTLE_ENDIAN__
157 # define ENDIAN_LITTLE 1234
160 #ifdef __STDC_ENDIAN_BIG__
161 # define ENDIAN_BIG __STDC_ENDIAN_BIG__
162 #elif defined(__ORDER_BIG_ENDIAN__)
163 # define ENDIAN_BIG __ORDER_BIG_ENDIAN__
165 # define ENDIAN_BIG 4321
168 #ifdef __STDC_ENDIAN_NATIVE__
169 # define ENDIAN_NATIVE __STDC_ENDIAN_NATIVE__
170 #elif defined(__ORDER_NATIVE_ENDIAN__)
171 # define ENDIAN_NATIVE __ORDER_NATIVE_ENDIAN__
173 # define ENDIAN_NATIVE 0
176 #if __STDC_VERSION__ >= 202311L
177 # define bswap16 stdc_memreverse8u16
178 # define bswap32 stdc_memreverse8u32
179 # define bswap64 stdc_memreverse8u64
181 # define bswap16 __builtin_bswap16
182 # define bswap32 __builtin_bswap32
183 # define bswap64 __builtin_bswap64
186 static inline uint16_t bswap16(uint16_t n) {
187 return (n << 8) | (n >> 8);
190 static inline uint32_t bswap32(uint32_t n) {
191 return ((uint32_t)bswap16(n) << 16) | bswap16(n >> 16);
194 static inline uint64_t bswap64(uint64_t n) {
195 return ((uint64_t)bswap32(n) << 32) | bswap32(n >> 32);
200 static inline uint8_t bswap8(uint8_t n) {
205 * Reverse the byte order of an integer.
212 uint64_t: bswap64)(n)
214 // Define an overload for each unsigned type
215 #define UINT_OVERLOADS(macro) \
216 macro(unsigned char, _uc, UCHAR_WIDTH) \
217 macro(unsigned short, _us, USHRT_WIDTH) \
218 macro(unsigned int, _ui, UINT_WIDTH) \
219 macro(unsigned long, _ul, ULONG_WIDTH) \
220 macro(unsigned long long, _ull, ULLONG_WIDTH)
222 // Select an overload based on an unsigned integer type
223 #define UINT_SELECT(n, name) \
226 signed char: name##_uc, \
227 unsigned char: name##_uc, \
228 signed short: name##_us, \
229 unsigned short: name##_us, \
230 signed int: name##_ui, \
231 unsigned int: name##_ui, \
232 signed long: name##_ul, \
233 unsigned long: name##_ul, \
234 signed long long: name##_ull, \
235 unsigned long long: name##_ull)
237 // C23 polyfill: bit utilities
239 #if __STDC_VERSION__ >= 202311L
240 # define count_ones stdc_count_ones
241 # define count_zeros stdc_count_zeros
242 # define rotate_left stdc_rotate_left
243 # define rotate_right stdc_rotate_right
244 # define leading_zeros stdc_leading_zeros
245 # define leading_ones stdc_leading_ones
246 # define trailing_zeros stdc_trailing_zeros
247 # define trailing_ones stdc_trailing_ones
248 # define first_leading_zero stdc_first_leading_zero
249 # define first_leading_one stdc_first_leading_one
250 # define first_trailing_zero stdc_first_trailing_zero
251 # define first_trailing_one stdc_first_trailing_one
252 # define has_single_bit stdc_has_single_bit
253 # define bit_width stdc_bit_width
254 # define bit_ceil stdc_bit_ceil
255 # define bit_floor stdc_bit_floor
260 // GCC provides builtins for unsigned {int,long,long long}, so promote char/short
261 #define UINT_BUILTIN_uc(name) __builtin_##name
262 #define UINT_BUILTIN_us(name) __builtin_##name
263 #define UINT_BUILTIN_ui(name) __builtin_##name
264 #define UINT_BUILTIN_ul(name) __builtin_##name##l
265 #define UINT_BUILTIN_ull(name) __builtin_##name##ll
266 #define UINT_BUILTIN(name, suffix) UINT_BUILTIN##suffix(name)
268 #define BUILTIN_WIDTH_uc UINT_WIDTH
269 #define BUILTIN_WIDTH_us UINT_WIDTH
270 #define BUILTIN_WIDTH_ui UINT_WIDTH
271 #define BUILTIN_WIDTH_ul ULONG_WIDTH
272 #define BUILTIN_WIDTH_ull ULLONG_WIDTH
273 #define BUILTIN_WIDTH(suffix) BUILTIN_WIDTH##suffix
275 #define COUNT_ONES(type, suffix, width) \
276 static inline int count_ones##suffix(type n) { \
277 return UINT_BUILTIN(popcount, suffix)(n); \
280 #define LEADING_ZEROS(type, suffix, width) \
281 static inline int leading_zeros##suffix(type n) { \
283 ? UINT_BUILTIN(clz, suffix)(n) - (BUILTIN_WIDTH(suffix) - width) \
287 #define TRAILING_ZEROS(type, suffix, width) \
288 static inline int trailing_zeros##suffix(type n) { \
289 return n ? UINT_BUILTIN(ctz, suffix)(n) : (int)width; \
292 #define FIRST_TRAILING_ONE(type, suffix, width) \
293 static inline int first_trailing_one##suffix(type n) { \
294 return UINT_BUILTIN(ffs, suffix)(n); \
299 #define COUNT_ONES(type, suffix, width) \
300 static inline int count_ones##suffix(type n) { \
302 for (ret = 0; n; ++ret) { \
308 #define LEADING_ZEROS(type, suffix, width) \
309 static inline int leading_zeros##suffix(type n) { \
310 type bit = (type)1 << (width - 1); \
312 for (ret = 0; bit && !(n & bit); ++ret, bit >>= 1); \
316 #define TRAILING_ZEROS(type, suffix, width) \
317 static inline int trailing_zeros##suffix(type n) { \
320 for (ret = 0; bit && !(n & bit); ++ret, bit <<= 1); \
324 #define FIRST_TRAILING_ONE(type, suffix, width) \
325 static inline int first_trailing_one##suffix(type n) { \
326 return n ? trailing_zeros##suffix(n) + 1 : 0; \
331 UINT_OVERLOADS(COUNT_ONES)
332 UINT_OVERLOADS(LEADING_ZEROS)
333 UINT_OVERLOADS(TRAILING_ZEROS)
334 UINT_OVERLOADS(FIRST_TRAILING_ONE)
336 #define ROTATE_LEFT(type, suffix, width) \
337 static inline type rotate_left##suffix(type n, int c) { \
338 return (n << c) | (n >> ((width - c) % width)); \
341 #define ROTATE_RIGHT(type, suffix, width) \
342 static inline type rotate_right##suffix(type n, int c) { \
343 return (n >> c) | (n << ((width - c) % width)); \
346 #define FIRST_LEADING_ONE(type, suffix, width) \
347 static inline int first_leading_one##suffix(type n) { \
348 return width - leading_zeros##suffix(n); \
351 #define HAS_SINGLE_BIT(type, suffix, width) \
352 static inline bool has_single_bit##suffix(type n) { \
353 return n && !(n & (n - 1)); \
356 UINT_OVERLOADS(ROTATE_LEFT)
357 UINT_OVERLOADS(ROTATE_RIGHT)
358 UINT_OVERLOADS(FIRST_LEADING_ONE)
359 UINT_OVERLOADS(HAS_SINGLE_BIT)
361 #define count_ones(n) UINT_SELECT(n, count_ones)(n)
362 #define count_zeros(n) UINT_SELECT(n, count_ones)(~(n))
364 #define rotate_left(n, c) UINT_SELECT(n, rotate_left)(n, c)
365 #define rotate_right(n, c) UINT_SELECT(n, rotate_right)(n, c)
367 #define leading_zeros(n) UINT_SELECT(n, leading_zeros)(n)
368 #define leading_ones(n) UINT_SELECT(n, leading_zeros)(~(n))
370 #define trailing_zeros(n) UINT_SELECT(n, trailing_zeros)(n)
371 #define trailing_ones(n) UINT_SELECT(n, trailing_zeros)(~(n))
373 #define first_leading_one(n) UINT_SELECT(n, first_leading_one)(n)
374 #define first_leading_zero(n) UINT_SELECT(n, first_leading_one)(~(n))
376 #define first_trailing_one(n) UINT_SELECT(n, first_trailing_one)(n)
377 #define first_trailing_zero(n) UINT_SELECT(n, first_trailing_one)(~(n))
379 #define has_single_bit(n) UINT_SELECT(n, has_single_bit)(n)
381 #define BIT_FLOOR(type, suffix, width) \
382 static inline type bit_floor##suffix(type n) { \
383 return n ? (type)1 << (first_leading_one##suffix(n) - 1) : 0; \
386 #define BIT_CEIL(type, suffix, width) \
387 static inline type bit_ceil##suffix(type n) { \
388 return (type)1 << first_leading_one##suffix(n - !!n); \
391 UINT_OVERLOADS(BIT_FLOOR)
392 UINT_OVERLOADS(BIT_CEIL)
394 #define bit_width(n) first_leading_one(n)
395 #define bit_floor(n) UINT_SELECT(n, bit_floor)(n)
396 #define bit_ceil(n) UINT_SELECT(n, bit_ceil)(n)
398 #endif // __STDC_VERSION__ < 202311L