4 * Compiler-specific definitions. Currently, only GCC and clang are supported.
6 * The following copying information applies to this specific source code file:
8 * Written in 2013-2016 by Eric Biggers <ebiggers3@gmail.com>
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").
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.
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/>.
23 #ifndef _WIMLIB_COMPILER_H
24 #define _WIMLIB_COMPILER_H
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)))
34 /* Feature-test macros defined by recent versions of clang. */
35 #ifndef __has_attribute
36 # define __has_attribute(attribute) 0
39 # define __has_feature(feature) 0
42 # define __has_builtin(builtin) 0
45 /* Declare that the annotated function should be exported from the shared
46 * library (or DLL). */
48 # define WIMLIBAPI __declspec(dllexport)
50 # define WIMLIBAPI __attribute__((visibility("default")))
53 /* Declare that the annotated function should be inlined. Currently, we force
54 * the compiler to honor this because we use 'inline' in highly tuned code, e.g.
55 * compression codecs. */
56 #define inline inline __attribute__((always_inline))
58 /* Declare that the annotated function should *not* be inlined. */
59 #define noinline __attribute__((noinline))
61 /* Functionally the same as 'noinline', but documents that the reason for not
62 * inlining is to prevent the annotated function from being inlined into a
63 * recursive function, thereby increasing its stack usage. */
64 #define noinline_for_stack noinline
66 /* Hint that the expression is usually true. */
67 #define likely(expr) __builtin_expect(!!(expr), 1)
69 /* Hint that the expression is usually false. */
70 #define unlikely(expr) __builtin_expect(!!(expr), 0)
72 /* Prefetch into L1 cache for read. */
73 #define prefetchr(addr) __builtin_prefetch((addr), 0)
75 /* Prefetch into L1 cache for write. */
76 #define prefetchw(addr) __builtin_prefetch((addr), 1)
78 /* Declare that the members of the annotated struct are tightly packed, and the
79 * struct itself may be misaligned. */
80 #define _packed_attribute __attribute__((packed))
82 /* Declare that the annotated variable, or variables of the annotated type, are
83 * to be aligned on n-byte boundaries. */
84 #define _aligned_attribute(n) __attribute__((aligned(n)))
86 /* Declare that pointers to the annotated type may alias other pointers. */
87 #define _may_alias_attribute __attribute__((may_alias))
89 /* Hint that the annotated function is rarely called. */
90 #if GCC_PREREQ(4, 4) || __has_attribute(cold)
91 # define _cold_attribute __attribute__((cold))
93 # define _cold_attribute
96 /* Hint that the annotated function is malloc-like: any non-null pointer it
97 * returns will not alias any pointer previously in use by the program. */
98 #define _malloc_attribute __attribute__((malloc))
100 /* Hint that the annotated function takes a printf()-like format string and
101 * arguments. This is currently disabled on Windows because MinGW does not
102 * support this attribute on functions taking wide-character strings. */
104 # define _format_attribute(type, format_str, format_start)
106 # define _format_attribute(type, format_str, format_start) \
107 __attribute__((format(type, format_str, format_start)))
110 /* Hint that the annotated function is intentionally not used. This might be
111 * the case if the function contains only static assertions. */
112 #define _unused_attribute __attribute__((unused))
114 /* Endianness definitions. Either CPU_IS_BIG_ENDIAN or CPU_IS_LITTLE_ENDIAN is
115 * set to 1. The other is set to 0. Note that newer gcc supports
116 * __BYTE_ORDER__ for easily determining the endianness; older gcc doesn't. In
117 * the latter case we fall back to a configure-time check. */
118 #ifdef __BYTE_ORDER__
119 # define CPU_IS_BIG_ENDIAN (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
120 #elif defined(HAVE_CONFIG_H)
122 # ifdef WORDS_BIGENDIAN
123 # define CPU_IS_BIG_ENDIAN 1
125 # define CPU_IS_BIG_ENDIAN 0
128 #define CPU_IS_LITTLE_ENDIAN (!CPU_IS_BIG_ENDIAN)
130 /* UNALIGNED_ACCESS_IS_FAST should be defined to 1 if unaligned memory accesses
131 * can be performed efficiently on the target platform. */
132 #if defined(__x86_64__) || defined(__i386__) || defined(__ARM_FEATURE_UNALIGNED)
133 # define UNALIGNED_ACCESS_IS_FAST 1
135 # define UNALIGNED_ACCESS_IS_FAST 0
138 /* Get the type of the specified expression. */
139 #define typeof __typeof__
141 /* Get the minimum of two variables, without multiple evaluation. */
143 # define min(a, b) ({ typeof(a) _a = (a); typeof(b) _b = (b); \
144 (_a < _b) ? _a : _b; })
147 /* Get the maximum of two variables, without multiple evaluation. */
149 # define max(a, b) ({ typeof(a) _a = (a); typeof(b) _b = (b); \
150 (_a > _b) ? _a : _b; })
153 /* Swap the values of two variables, without multiple evaluation. */
155 # define swap(a, b) ({ typeof(a) _a = (a); (a) = (b); (b) = _a; })
158 /* (Optional) Efficiently swap the bytes of a 16-bit integer. */
159 #if GCC_PREREQ(4, 8) || __has_builtin(__builtin_bswap16)
160 # define compiler_bswap16 __builtin_bswap16
163 /* (Optional) Efficiently swap the bytes of a 32-bit integer. */
164 #if GCC_PREREQ(4, 3) || __has_builtin(__builtin_bswap32)
165 # define compiler_bswap32 __builtin_bswap32
168 /* (Optional) Efficiently swap the bytes of a 64-bit integer. */
169 #if GCC_PREREQ(4, 3) || __has_builtin(__builtin_bswap64)
170 # define compiler_bswap64 __builtin_bswap64
173 /* (Optional) Find Last Set bit and Find First Set bit macros. */
174 #define compiler_fls32(n) (31 - __builtin_clz(n))
175 #define compiler_fls64(n) (63 - __builtin_clzll(n))
176 #define compiler_ffs32(n) __builtin_ctz(n)
177 #define compiler_ffs64(n) __builtin_ctzll(n)
179 /* Optional definitions for checking with 'sparse'. */
181 # define _bitwise_attr __attribute__((bitwise))
182 # define _force_attr __attribute__((force))
184 # define _bitwise_attr
188 /* STATIC_ASSERT() - verify the truth of an expression at compilation time. */
189 #if __STDC_VERSION__ >= 201112L
190 # define STATIC_ASSERT(expr) _Static_assert((expr), "")
192 # define STATIC_ASSERT(expr) ((void)sizeof(char[1 - 2 * !(expr)]))
195 #define CONCAT_IMPL(s1, s2) s1##s2
197 /* CONCAT() - concatenate two tokens at preprocessing time. */
198 #define CONCAT(s1, s2) CONCAT_IMPL(s1, s2)
200 #endif /* _WIMLIB_COMPILER_H */