]> wimlib.net Git - wimlib/blob - include/wimlib/compiler.h
matchfinder_common: sync with libdeflate
[wimlib] / include / wimlib / compiler.h
1 /*
2  * compiler.h
3  *
4  * Compiler-specific definitions.  Currently, only GCC and clang are supported.
5  *
6  * Copyright 2022 Eric Biggers
7  *
8  * Permission is hereby granted, free of charge, to any person
9  * obtaining a copy of this software and associated documentation
10  * files (the "Software"), to deal in the Software without
11  * restriction, including without limitation the rights to use,
12  * copy, modify, merge, publish, distribute, sublicense, and/or sell
13  * copies of the Software, and to permit persons to whom the
14  * Software is furnished to do so, subject to the following
15  * conditions:
16  *
17  * The above copyright notice and this permission notice shall be
18  * included in all copies or substantial portions of the Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
22  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
24  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
25  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
27  * OTHER DEALINGS IN THE SOFTWARE.
28  */
29
30 #ifndef _WIMLIB_COMPILER_H
31 #define _WIMLIB_COMPILER_H
32
33 /* Is the compiler GCC of the specified version or later?  This always returns
34  * false for clang, since clang is "frozen" at GNUC 4.2.  The __has_*
35  * feature-test macros should be used to detect clang functionality instead.  */
36 #define GCC_PREREQ(major, minor)                                        \
37         (!defined(__clang__) && !defined(__INTEL_COMPILER) &&           \
38          (__GNUC__ > major ||                                           \
39           (__GNUC__ == major && __GNUC_MINOR__ >= minor)))
40
41 /* Feature-test macros defined by recent versions of clang.  */
42 #ifndef __has_attribute
43 #  define __has_attribute(attribute)    0
44 #endif
45 #ifndef __has_feature
46 #  define __has_feature(feature)        0
47 #endif
48 #ifndef __has_builtin
49 #  define __has_builtin(builtin)        0
50 #endif
51
52 /* Declare that the annotated function should be exported from the shared
53  * library (or DLL).  */
54 #ifdef __WIN32__
55 #  define WIMLIBAPI __declspec(dllexport)
56 #else
57 #  define WIMLIBAPI __attribute__((visibility("default")))
58 #endif
59
60 /* Declare that the annotated function should always be inlined.  This might be
61  * desirable in highly tuned code, e.g. compression codecs.  */
62 #define forceinline             inline __attribute__((always_inline))
63
64 /* Declare that the annotated function should *not* be inlined.  */
65 #define noinline                __attribute__((noinline))
66
67 /* Functionally the same as 'noinline', but documents that the reason for not
68  * inlining is to prevent the annotated function from being inlined into a
69  * recursive function, thereby increasing its stack usage.  */
70 #define noinline_for_stack      noinline
71
72 /* Hint that the expression is usually true.  */
73 #define likely(expr)            __builtin_expect(!!(expr), 1)
74
75 /* Hint that the expression is usually false.  */
76 #define unlikely(expr)          __builtin_expect(!!(expr), 0)
77
78 /* Prefetch into L1 cache for read.  */
79 #define prefetchr(addr)         __builtin_prefetch((addr), 0)
80
81 /* Prefetch into L1 cache for write.  */
82 #define prefetchw(addr)         __builtin_prefetch((addr), 1)
83
84 /* Declare that the members of the annotated struct are tightly packed, and the
85  * struct itself may be misaligned.  */
86 #define _packed_attribute       __attribute__((packed))
87
88 /* Declare that the annotated variable, or variables of the annotated type, are
89  * to be aligned on n-byte boundaries.  */
90 #define _aligned_attribute(n)   __attribute__((aligned(n)))
91
92 /* Declare that pointers to the annotated type may alias other pointers.  */
93 #define _may_alias_attribute    __attribute__((may_alias))
94
95 /* Hint that the annotated function is rarely called.  */
96 #if GCC_PREREQ(4, 4) || __has_attribute(cold)
97 #  define _cold_attribute       __attribute__((cold))
98 #else
99 #  define _cold_attribute
100 #endif
101
102 /* Hint that the annotated function is malloc-like: any non-null pointer it
103  * returns will not alias any pointer previously in use by the program.  */
104 #define _malloc_attribute       __attribute__((malloc))
105
106 /* Hint that the annotated function takes a printf()-like format string and
107  * arguments.  This is currently disabled on Windows because MinGW does not
108  * support this attribute on functions taking wide-character strings.  */
109 #ifdef __WIN32__
110 #  define _format_attribute(type, format_str, format_start)
111 #else
112 #  define _format_attribute(type, format_str, format_start)     \
113                         __attribute__((format(type, format_str, format_start)))
114 #endif
115
116 /* Hint that the annotated function is intentionally not used.  This might be
117  * the case if the function contains only static assertions.  */
118 #define _unused_attribute       __attribute__((unused))
119
120 /* Endianness definitions.  Either CPU_IS_BIG_ENDIAN() or CPU_IS_LITTLE_ENDIAN()
121  * evaluates to 1.  The other evaluates to 0.  Note that newer gcc supports
122  * __BYTE_ORDER__ for easily determining the endianness; older gcc doesn't.  In
123  * the latter case we fall back to a configure-time check.  */
124 #ifdef __BYTE_ORDER__
125 #  define CPU_IS_BIG_ENDIAN()   (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
126 #elif defined(HAVE_CONFIG_H)
127 #  include "config.h"
128 #  ifdef WORDS_BIGENDIAN
129 #    define CPU_IS_BIG_ENDIAN() 1
130 #  else
131 #    define CPU_IS_BIG_ENDIAN() 0
132 #  endif
133 #endif
134 #define CPU_IS_LITTLE_ENDIAN() (!CPU_IS_BIG_ENDIAN())
135
136 /* UNALIGNED_ACCESS_IS_FAST should be defined to 1 if unaligned memory accesses
137  * can be performed efficiently on the target platform.  */
138 #if defined(__x86_64__) || defined(__i386__) || defined(__ARM_FEATURE_UNALIGNED)
139 #  define UNALIGNED_ACCESS_IS_FAST 1
140 #else
141 #  define UNALIGNED_ACCESS_IS_FAST 0
142 #endif
143
144 /* Get the type of the specified expression.  */
145 #define typeof     __typeof__
146
147 /* Get the minimum of two variables, without multiple evaluation.  */
148 #ifndef min
149 #  define min(a, b)  ({ typeof(a) _a = (a); typeof(b) _b = (b); \
150                         (_a < _b) ? _a : _b; })
151 #endif
152 #define MIN(a, b)       min((a), (b))
153
154 /* Get the maximum of two variables, without multiple evaluation.  */
155 #ifndef max
156 #  define max(a, b)  ({ typeof(a) _a = (a); typeof(b) _b = (b); \
157                         (_a > _b) ? _a : _b; })
158 #endif
159 #define MAX(a, b)       max((a), (b))
160
161 /* Swap the values of two variables, without multiple evaluation.  */
162 #ifndef swap
163 #  define swap(a, b) ({ typeof(a) _a = (a); (a) = (b); (b) = _a; })
164 #endif
165 #define SWAP(a, b)      swap((a), (b))
166
167 /* (Optional) Efficiently swap the bytes of a 16-bit integer.  */
168 #if GCC_PREREQ(4, 8) || __has_builtin(__builtin_bswap16)
169 #  define compiler_bswap16 __builtin_bswap16
170 #endif
171
172 /* (Optional) Efficiently swap the bytes of a 32-bit integer.  */
173 #if GCC_PREREQ(4, 3) || __has_builtin(__builtin_bswap32)
174 #  define compiler_bswap32 __builtin_bswap32
175 #endif
176
177 /* (Optional) Efficiently swap the bytes of a 64-bit integer.  */
178 #if GCC_PREREQ(4, 3) || __has_builtin(__builtin_bswap64)
179 #  define compiler_bswap64 __builtin_bswap64
180 #endif
181
182 /* (Optional) Find Last Set bit and Find First Set bit macros.  */
183 #define compiler_bsr32(n)       (31 - __builtin_clz(n))
184 #define compiler_bsr64(n)       (63 - __builtin_clzll(n))
185 #define compiler_bsf32(n)       __builtin_ctz(n)
186 #define compiler_bsf64(n)       __builtin_ctzll(n)
187
188 /* Optional definitions for checking with 'sparse'.  */
189 #ifdef __CHECKER__
190 #  define _bitwise_attr __attribute__((bitwise))
191 #  define _force_attr   __attribute__((force))
192 #else
193 #  define _bitwise_attr
194 #  define _force_attr
195 #endif
196
197 /* STATIC_ASSERT() - verify the truth of an expression at compilation time.  */
198 #ifdef __CHECKER__
199 #  define STATIC_ASSERT(expr)
200 #elif __STDC_VERSION__ >= 201112L
201 #  define STATIC_ASSERT(expr)   _Static_assert((expr), "")
202 #else
203 #  define STATIC_ASSERT(expr)   ((void)sizeof(char[1 - 2 * !(expr)]))
204 #endif
205
206 /* STATIC_ASSERT_ZERO() - verify the truth of an expression at compilation time
207  * and also produce a result of value '0' to be used in constant expressions */
208 #define STATIC_ASSERT_ZERO(expr) ((int)sizeof(char[-!(expr)]))
209
210 #define CONCAT_IMPL(s1, s2)     s1##s2
211
212 /* CONCAT() - concatenate two tokens at preprocessing time.  */
213 #define CONCAT(s1, s2)          CONCAT_IMPL(s1, s2)
214
215 #endif /* _WIMLIB_COMPILER_H */