-
Notifications
You must be signed in to change notification settings - Fork 1
/
compiler.h
193 lines (162 loc) · 6.29 KB
/
compiler.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
* compiler.h -- assisting macros for exploiting compiler capabilities
* Copyright (C) 2014 Daniel Santos <daniel.santos@pobox.com> and others
* Largely copied from Linux Kernel tree
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef _COMPILER_H_
#define _COMPILER_H_
#include <sys/types.h>
#include <inttypes.h>
/* a few helpful gcc-extension macros */
#ifndef likely
# define likely(x) __builtin_expect(!!(x), 1)
#endif
#ifndef unlikely
# define unlikely(x) __builtin_expect(!!(x), 0)
#endif
#ifndef __aligned
# define __aligned(n) __attribute__((aligned(n)))
#endif
#ifndef assume_aligned
# define assume_aligned(p, ...) __builtin_assume_aligned(p, ## __VA_ARGS__)
#endif
#ifndef __always_inline
# define __always_inline inline __attribute__((always_inline))
#endif
#ifndef __noinline
# define __noinline __attribute__((noinline))
#endif
#ifndef __flatten
# define __flatten __attribute__((flatten))
#endif
#ifndef __compiletime_error
# define __compiletime_error(msg) __attribute__((error(msg)))
#endif
#ifndef __compiletime_warning
# define __compiletime_warning(msg) __attribute__((warning(msg)))
#endif
#define __compiletime_assert(condition, msg, prefix, suffix) \
do { \
int __cond = !(condition); \
extern void prefix ## suffix(void) __compiletime_error(msg); \
if (__cond) \
prefix ## suffix(); \
} while (0)
#define _compiletime_assert(condition, msg, prefix, suffix) \
__compiletime_assert(condition, msg, prefix, suffix)
/**
* compiletime_assert - break build and emit msg if condition is false
* @condition: a compile-time constant condition to check
* @msg: a message to emit if condition is false
*
* In tradition of POSIX assert, this macro will break the build if the
* supplied condition is *false*, emitting the supplied error message if the
* compiler has support to do so.
*/
#define compiletime_assert(condition, msg) \
_compiletime_assert(condition, msg, __compiletime_assert_, __LINE__)
/* Force a compilation error if a constant expression is not a power of 2 */
#define BUILD_BUG_ON_NOT_POWER_OF_2(n) \
BUILD_BUG_ON((n) == 0 || (((n) & ((n) - 1)) != 0))
/* Force a compilation error if condition is true, but also produce a
result (of value 0 and type size_t), so the expression can be used
e.g. in a structure initializer (or where-ever else comma expressions
aren't permitted). */
#define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); }))
#define BUILD_BUG_ON_NULL(e) ((void *)sizeof(struct { int:-!!(e); }))
/*
* BUILD_BUG_ON_INVALID() permits the compiler to check the validity of the
* expression but avoids the generation of any code, even if that expression
* has side-effects.
*/
#define BUILD_BUG_ON_INVALID(e) ((void)(sizeof((__force long)(e))))
/**
* BUILD_BUG_ON_MSG - break compile if a condition is true & emit supplied
* error message.
* @condition: the condition which the compiler should know is false.
*
* See BUILD_BUG_ON for description.
*/
#ifndef __OPTIMIZE__
#define BUILD_BUG_ON_MSG(cond, msg) do {} while(0)
#else
#define BUILD_BUG_ON_MSG(cond, msg) compiletime_assert(!(cond), msg)
#endif
/**
* BUILD_BUG_ON - break compile if a condition is true.
* @condition: the condition which the compiler should know is false.
*
* If you have some code which relies on certain constants being equal, or
* some other compile-time-evaluated condition, you should use BUILD_BUG_ON to
* detect if someone changes it.
*
* The implementation uses gcc's reluctance to create a negative array, but gcc
* (as of 4.4) only emits that error for obvious cases (e.g. not arguments to
* inline functions). Luckily, in 4.3 they added the "error" function
* attribute just for this type of case. Thus, we use a negative sized array
* (should always create an error on gcc versions older than 4.4) and then call
* an undefined function with the error attribute (should always create an
* error on gcc 4.3 and later). If for some reason, neither creates a
* compile-time error, we'll still have a link-time error, which is harder to
* track down.
*/
#ifndef __OPTIMIZE__
#define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
#else
#define BUILD_BUG_ON(condition) \
BUILD_BUG_ON_MSG(condition, "BUILD_BUG_ON failed: " #condition)
#endif
/**
* BUILD_BUG - break compile if used.
*
* If you have some code that you expect the compiler to eliminate at
* build time, you should use BUILD_BUG to detect if it is
* unexpectedly used.
*/
#define BUILD_BUG() BUILD_BUG_ON_MSG(1, "BUILD_BUG failed")
#define assert_const(expr) BUILD_BUG_ON_MSG( \
!__builtin_constant_p(expr), "Not a constant expression: " #expr)
#define assert_early(expr) \
do { \
if (__builtin_constant_p(expr)) \
BUILD_BUG_ON_MSG(!(expr), "!(" #expr ")"); \
else \
assert(expr); \
} while (0)
#define __static_warn_msg(condition, msg, prefix, suffix) \
do { \
int __cond = 1 || !(condition); \
void __noinline __compiletime_warning(msg) \
prefix ## suffix(void) {__asm("");} \
if (__cond) \
prefix ## suffix(); \
} while (0)
#define _static_warn_msg(condition, msg, prefix, suffix) \
__static_warn_msg(condition, msg, prefix, suffix)
#define static_warn_msg(condition, msg) \
_static_warn_msg(condition, msg, __static_warn_, __LINE__)
static __always_inline /*__attribute__((alloc_align(2))) */void *
aligned_ptr(void *p, size_t align) {
return p;
//return (void*)((uintptr_t)p & (~((uintptr_t)0) & (align - 1)));
}
#define aligned_alloca(n, align) (void *) \
({ \
const size_t a = align - 1; \
return (void *) (((uintptr_t)alloca((n) + a) + a) & ~a);\
})
#endif /* _COMPILER_H_ */