1 // SPDX-License-Identifier: MPL-2.0
2 // Copyright (c) 2018 Yuxuan Shui <yshuiv7@gmail.com>
3 #pragma once
4 
5 #ifdef HAS_STDC_PREDEF_H
6 #include <stdc-predef.h>
7 #endif
8 
9 #define auto           __auto_type
10 #define likely(x)      __builtin_expect(!!(x), 1)
11 #define unlikely(x)    __builtin_expect(!!(x), 0)
12 #define likely_if(x)   if (likely(x))
13 #define unlikely_if(x) if (unlikely(x))
14 
15 #ifndef __has_attribute
16 # if __GNUC__ >= 4
17 #  define __has_attribute(x) 1
18 # else
19 #  define __has_attribute(x) 0
20 # endif
21 #endif
22 
23 #if __has_attribute(const)
24 # define attr_const __attribute__((const))
25 #else
26 # define attr_const
27 #endif
28 
29 #if __has_attribute(format)
30 # define attr_printf(a, b) __attribute__((format(printf, a, b)))
31 #else
32 # define attr_printf(a, b)
33 #endif
34 
35 #if __has_attribute(pure)
36 # define attr_pure __attribute__((pure))
37 #else
38 # define attr_pure
39 #endif
40 
41 #if __has_attribute(unused)
42 # define attr_unused __attribute__((unused))
43 #else
44 # define attr_unused
45 #endif
46 
47 #if __has_attribute(warn_unused_result)
48 # define attr_warn_unused_result __attribute__((warn_unused_result))
49 #else
50 # define attr_warn_unused_result
51 #endif
52 // An alias for conveninence
53 #define must_use attr_warn_unused_result
54 
55 #if __has_attribute(nonnull)
56 # define attr_nonnull(...) __attribute__((nonnull(__VA_ARGS__)))
57 # define attr_nonnull_all __attribute__((nonnull))
58 #else
59 # define attr_nonnull(...)
60 # define attr_nonnull_all
61 #endif
62 
63 #if __has_attribute(returns_nonnull)
64 # define attr_ret_nonnull __attribute__((returns_nonnull))
65 #else
66 # define attr_ret_nonnull
67 #endif
68 
69 #if __has_attribute(deprecated)
70 # define attr_deprecated __attribute__((deprecated))
71 #else
72 # define attr_deprecated
73 #endif
74 
75 #if __has_attribute(malloc)
76 # define attr_malloc __attribute__((malloc))
77 #else
78 # define attr_malloc
79 #endif
80 
81 #if __STDC_VERSION__ >= 201112L
82 # define attr_noret _Noreturn
83 #else
84 # if __has_attribute(noreturn)
85 #  define attr_noret __attribute__((noreturn))
86 # else
87 #  define attr_noret
88 # endif
89 #endif
90 
91 #if defined(__GNUC__) || defined(__clang__)
92 # define unreachable __builtin_unreachable()
93 #else
94 # define unreachable do {} while(0)
95 #endif
96 
97 #ifndef __has_include
98 #define __has_include(x) 0
99 #endif
100 
101 #if !defined(__STDC_NO_THREADS__) && __has_include(<threads.h>)
102 # include <threads.h>
103 #elif __STDC_VERSION__ >= 201112L
104 # define thread_local _Thread_local
105 #elif defined(__GNUC__) || defined(__clang__)
106 # define thread_local __thread
107 #else
108 # define thread_local _Pragma("GCC error \"No thread local storage support\"") __error__
109 #endif
110 
111 typedef unsigned long ulong;
112 typedef unsigned int uint;
113 
popcntul(unsigned long a)114 static inline int attr_const popcntul(unsigned long a) {
115 	return __builtin_popcountl(a);
116 }
117