]> wimlib.net Git - wimlib/blob - include/wimlib/compiler.h
compiler.h: remove _aligned_attribute
[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 always be inlined.  This might be
53  * desirable in highly tuned code, e.g. compression codecs.  */
54 #define forceinline             inline __attribute__((always_inline))
55
56 /* Declare that the annotated function should *not* be inlined.  */
57 #define noinline                __attribute__((noinline))
58
59 /* Functionally the same as 'noinline', but documents that the reason for not
60  * inlining is to prevent the annotated function from being inlined into a
61  * recursive function, thereby increasing its stack usage.  */
62 #define noinline_for_stack      noinline
63
64 /* Hint that the expression is usually true.  */
65 #define likely(expr)            __builtin_expect(!!(expr), 1)
66
67 /* Hint that the expression is usually false.  */
68 #define unlikely(expr)          __builtin_expect(!!(expr), 0)
69
70 /* Prefetch into L1 cache for read.  */
71 #define prefetchr(addr)         __builtin_prefetch((addr), 0)
72
73 /* Prefetch into L1 cache for write.  */
74 #define prefetchw(addr)         __builtin_prefetch((addr), 1)
75
76 /* Declare that the members of the annotated struct are tightly packed, and the
77  * struct itself may be misaligned.  */
78 #define _packed_attribute       __attribute__((packed))
79
80 /* Declare that pointers to the annotated type may alias other pointers.  */
81 #define _may_alias_attribute    __attribute__((may_alias))
82
83 /* Hint that the annotated function is rarely called.  */
84 #if GCC_PREREQ(4, 4) || __has_attribute(cold)
85 #  define _cold_attribute       __attribute__((cold))
86 #else
87 #  define _cold_attribute
88 #endif
89
90 /* Hint that the annotated function takes a printf()-like format string and
91  * arguments.  This is currently disabled on Windows because MinGW does not
92  * support this attribute on functions taking wide-character strings.  */
93 #ifdef _WIN32
94 #  define _format_attribute(type, format_str, format_start)
95 #else
96 #  define _format_attribute(type, format_str, format_start)     \
97                         __attribute__((format(type, format_str, format_start)))
98 #endif
99
100 /* Hint that the annotated function is intentionally not used.  This might be
101  * the case if the function contains only static assertions.  */
102 #define _unused_attribute       __attribute__((unused))
103
104 /* Endianness definitions.  Either CPU_IS_BIG_ENDIAN() or CPU_IS_LITTLE_ENDIAN()
105  * evaluates to 1.  The other evaluates to 0.  Note that newer gcc supports
106  * __BYTE_ORDER__ for easily determining the endianness; older gcc doesn't.  In
107  * the latter case we fall back to a configure-time check.  */
108 #ifdef __BYTE_ORDER__
109 #  define CPU_IS_BIG_ENDIAN()   (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
110 #elif defined(HAVE_CONFIG_H)
111 #  include "config.h"
112 #  ifdef WORDS_BIGENDIAN
113 #    define CPU_IS_BIG_ENDIAN() 1
114 #  else
115 #    define CPU_IS_BIG_ENDIAN() 0
116 #  endif
117 #endif
118 #define CPU_IS_LITTLE_ENDIAN() (!CPU_IS_BIG_ENDIAN())
119
120 /* UNALIGNED_ACCESS_IS_FAST should be defined to 1 if unaligned memory accesses
121  * can be performed efficiently on the target platform.  */
122 #if defined(__x86_64__) || defined(__i386__) || \
123         defined(__ARM_FEATURE_UNALIGNED) || defined(__powerpc64__)
124 #  define UNALIGNED_ACCESS_IS_FAST 1
125 #else
126 #  define UNALIGNED_ACCESS_IS_FAST 0
127 #endif
128
129 /* Get the minimum of two variables, without multiple evaluation.  */
130 #undef min
131 #define min(a, b)  ({ typeof(a) _a = (a); typeof(b) _b = (b); \
132                     (_a < _b) ? _a : _b; })
133 #undef MIN
134 #define MIN(a, b)       min((a), (b))
135
136 /* Get the maximum of two variables, without multiple evaluation.  */
137 #undef max
138 #define max(a, b)  ({ typeof(a) _a = (a); typeof(b) _b = (b); \
139                     (_a > _b) ? _a : _b; })
140 #undef MAX
141 #define MAX(a, b)       max((a), (b))
142
143 /* Swap the values of two variables, without multiple evaluation.  */
144 #ifndef swap
145 #  define swap(a, b) ({ typeof(a) _a = (a); (a) = (b); (b) = _a; })
146 #endif
147 #define SWAP(a, b)      swap((a), (b))
148
149 /* Optional definitions for checking with 'sparse'.  */
150 #ifdef __CHECKER__
151 #  define _bitwise_attr __attribute__((bitwise))
152 #  define _force_attr   __attribute__((force))
153 #else
154 #  define _bitwise_attr
155 #  define _force_attr
156 #endif
157
158 /* STATIC_ASSERT() - verify the truth of an expression at compilation time.  */
159 #ifdef __CHECKER__
160 #  define STATIC_ASSERT(expr)
161 #elif __STDC_VERSION__ >= 201112L
162 #  define STATIC_ASSERT(expr)   _Static_assert((expr), "")
163 #else
164 #  define STATIC_ASSERT(expr)   ((void)sizeof(char[1 - 2 * !(expr)]))
165 #endif
166
167 /* STATIC_ASSERT_ZERO() - verify the truth of an expression at compilation time
168  * and also produce a result of value '0' to be used in constant expressions */
169 #define STATIC_ASSERT_ZERO(expr) ((int)sizeof(char[-!(expr)]))
170
171 #define CONCAT_IMPL(s1, s2)     s1##s2
172
173 /* CONCAT() - concatenate two tokens at preprocessing time.  */
174 #define CONCAT(s1, s2)          CONCAT_IMPL(s1, s2)
175
176 #endif /* _WIMLIB_COMPILER_H */