]> wimlib.net Git - wimlib/blob - include/wimlib/compiler.h
6a87e8912338a759b7516db26109a99d2529cb0f
[wimlib] / include / wimlib / compiler.h
1 /*
2  * compiler.h
3  *
4  * Compiler-specific definitions.  Currently, only GCC and clang are supported.
5  *
6  * The following copying information applies to this specific source code file:
7  *
8  * Written in 2013-2016 by Eric Biggers <ebiggers3@gmail.com>
9  *
10  * To the extent possible under law, the author(s) have dedicated all copyright
11  * and related and neighboring rights to this software to the public domain
12  * worldwide via the Creative Commons Zero 1.0 Universal Public Domain
13  * Dedication (the "CC0").
14  *
15  * This software is distributed in the hope that it will be useful, but WITHOUT
16  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17  * FOR A PARTICULAR PURPOSE. See the CC0 for more details.
18  *
19  * You should have received a copy of the CC0 along with this software; if not
20  * see <http://creativecommons.org/publicdomain/zero/1.0/>.
21  */
22
23 #ifndef _WIMLIB_COMPILER_H
24 #define _WIMLIB_COMPILER_H
25
26 /* Is the compiler GCC of the specified version or later?  This always returns
27  * false for clang, since clang is "frozen" at GNUC 4.2.  The __has_*
28  * feature-test macros should be used to detect clang functionality instead.  */
29 #define GCC_PREREQ(major, minor)                                        \
30         (!defined(__clang__) && !defined(__INTEL_COMPILER) &&           \
31          (__GNUC__ > major ||                                           \
32           (__GNUC__ == major && __GNUC_MINOR__ >= minor)))
33
34 /* Feature-test macros defined by recent versions of clang.  */
35 #ifndef __has_attribute
36 #  define __has_attribute(attribute)    0
37 #endif
38 #ifndef __has_feature
39 #  define __has_feature(feature)        0
40 #endif
41 #ifndef __has_builtin
42 #  define __has_builtin(builtin)        0
43 #endif
44
45 /* Declare that the annotated function should be exported from the shared
46  * library (or DLL).  */
47 #ifdef __WIN32__
48 #  define WIMLIBAPI __declspec(dllexport)
49 #else
50 #  define WIMLIBAPI __attribute__((visibility("default")))
51 #endif
52
53 /* Declare that the annotated function should always be inlined.  This might be
54  * desirable in highly tuned code, e.g. compression codecs.  */
55 #define forceinline             inline __attribute__((always_inline))
56
57 /* Declare that the annotated function should *not* be inlined.  */
58 #define noinline                __attribute__((noinline))
59
60 /* Functionally the same as 'noinline', but documents that the reason for not
61  * inlining is to prevent the annotated function from being inlined into a
62  * recursive function, thereby increasing its stack usage.  */
63 #define noinline_for_stack      noinline
64
65 /* Hint that the expression is usually true.  */
66 #define likely(expr)            __builtin_expect(!!(expr), 1)
67
68 /* Hint that the expression is usually false.  */
69 #define unlikely(expr)          __builtin_expect(!!(expr), 0)
70
71 /* Prefetch into L1 cache for read.  */
72 #define prefetchr(addr)         __builtin_prefetch((addr), 0)
73
74 /* Prefetch into L1 cache for write.  */
75 #define prefetchw(addr)         __builtin_prefetch((addr), 1)
76
77 /* Declare that the members of the annotated struct are tightly packed, and the
78  * struct itself may be misaligned.  */
79 #define _packed_attribute       __attribute__((packed))
80
81 /* Declare that the annotated variable, or variables of the annotated type, are
82  * to be aligned on n-byte boundaries.  */
83 #define _aligned_attribute(n)   __attribute__((aligned(n)))
84
85 /* Declare that pointers to the annotated type may alias other pointers.  */
86 #define _may_alias_attribute    __attribute__((may_alias))
87
88 /* Hint that the annotated function is rarely called.  */
89 #if GCC_PREREQ(4, 4) || __has_attribute(cold)
90 #  define _cold_attribute       __attribute__((cold))
91 #else
92 #  define _cold_attribute
93 #endif
94
95 /* Hint that the annotated function is malloc-like: any non-null pointer it
96  * returns will not alias any pointer previously in use by the program.  */
97 #define _malloc_attribute       __attribute__((malloc))
98
99 /* Hint that the annotated function takes a printf()-like format string and
100  * arguments.  This is currently disabled on Windows because MinGW does not
101  * support this attribute on functions taking wide-character strings.  */
102 #ifdef __WIN32__
103 #  define _format_attribute(type, format_str, format_start)
104 #else
105 #  define _format_attribute(type, format_str, format_start)     \
106                         __attribute__((format(type, format_str, format_start)))
107 #endif
108
109 /* Hint that the annotated function is intentionally not used.  This might be
110  * the case if the function contains only static assertions.  */
111 #define _unused_attribute       __attribute__((unused))
112
113 /* Endianness definitions.  Either CPU_IS_BIG_ENDIAN or CPU_IS_LITTLE_ENDIAN is
114  * set to 1.  The other is set to 0.  Note that newer gcc supports
115  * __BYTE_ORDER__ for easily determining the endianness; older gcc doesn't.  In
116  * the latter case we fall back to a configure-time check.  */
117 #ifdef __BYTE_ORDER__
118 #  define CPU_IS_BIG_ENDIAN     (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
119 #elif defined(HAVE_CONFIG_H)
120 #  include "config.h"
121 #  ifdef WORDS_BIGENDIAN
122 #    define CPU_IS_BIG_ENDIAN 1
123 #  else
124 #    define CPU_IS_BIG_ENDIAN 0
125 #  endif
126 #endif
127 #define CPU_IS_LITTLE_ENDIAN (!CPU_IS_BIG_ENDIAN)
128
129 /* UNALIGNED_ACCESS_IS_FAST should be defined to 1 if unaligned memory accesses
130  * can be performed efficiently on the target platform.  */
131 #if defined(__x86_64__) || defined(__i386__) || defined(__ARM_FEATURE_UNALIGNED)
132 #  define UNALIGNED_ACCESS_IS_FAST 1
133 #else
134 #  define UNALIGNED_ACCESS_IS_FAST 0
135 #endif
136
137 /* Get the type of the specified expression.  */
138 #define typeof     __typeof__
139
140 /* Get the minimum of two variables, without multiple evaluation.  */
141 #ifndef min
142 #  define min(a, b)  ({ typeof(a) _a = (a); typeof(b) _b = (b); \
143                         (_a < _b) ? _a : _b; })
144 #endif
145
146 /* Get the maximum of two variables, without multiple evaluation.  */
147 #ifndef max
148 #  define max(a, b)  ({ typeof(a) _a = (a); typeof(b) _b = (b); \
149                         (_a > _b) ? _a : _b; })
150 #endif
151
152 /* Swap the values of two variables, without multiple evaluation.  */
153 #ifndef swap
154 #  define swap(a, b) ({ typeof(a) _a = (a); (a) = (b); (b) = _a; })
155 #endif
156
157 /* (Optional) Efficiently swap the bytes of a 16-bit integer.  */
158 #if GCC_PREREQ(4, 8) || __has_builtin(__builtin_bswap16)
159 #  define compiler_bswap16 __builtin_bswap16
160 #endif
161
162 /* (Optional) Efficiently swap the bytes of a 32-bit integer.  */
163 #if GCC_PREREQ(4, 3) || __has_builtin(__builtin_bswap32)
164 #  define compiler_bswap32 __builtin_bswap32
165 #endif
166
167 /* (Optional) Efficiently swap the bytes of a 64-bit integer.  */
168 #if GCC_PREREQ(4, 3) || __has_builtin(__builtin_bswap64)
169 #  define compiler_bswap64 __builtin_bswap64
170 #endif
171
172 /* (Optional) Find Last Set bit and Find First Set bit macros.  */
173 #define compiler_bsr32(n)       (31 - __builtin_clz(n))
174 #define compiler_bsr64(n)       (63 - __builtin_clzll(n))
175 #define compiler_bsf32(n)       __builtin_ctz(n)
176 #define compiler_bsf64(n)       __builtin_ctzll(n)
177
178 /* Optional definitions for checking with 'sparse'.  */
179 #ifdef __CHECKER__
180 #  define _bitwise_attr __attribute__((bitwise))
181 #  define _force_attr   __attribute__((force))
182 #else
183 #  define _bitwise_attr
184 #  define _force_attr
185 #endif
186
187 /* STATIC_ASSERT() - verify the truth of an expression at compilation time.  */
188 #ifdef __CHECKER__
189 #  define STATIC_ASSERT(expr)
190 #elif __STDC_VERSION__ >= 201112L
191 #  define STATIC_ASSERT(expr)   _Static_assert((expr), "")
192 #else
193 #  define STATIC_ASSERT(expr)   ((void)sizeof(char[1 - 2 * !(expr)]))
194 #endif
195
196 /* STATIC_ASSERT_ZERO() - verify the truth of an expression at compilation time
197  * and also produce a result of value '0' to be used in constant expressions */
198 #define STATIC_ASSERT_ZERO(expr) ((int)sizeof(char[-!(expr)]))
199
200 #define CONCAT_IMPL(s1, s2)     s1##s2
201
202 /* CONCAT() - concatenate two tokens at preprocessing time.  */
203 #define CONCAT(s1, s2)          CONCAT_IMPL(s1, s2)
204
205 #endif /* _WIMLIB_COMPILER_H */