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