1
2 /*
3 * Copyright (C) Igor Sysoev
4 * Copyright (C) NGINX, Inc.
5 */
6
7 #ifndef _NXT_CLANG_H_INCLUDED_
8 #define _NXT_CLANG_H_INCLUDED_
9
10
11 #define nxt_inline static inline __attribute__((always_inline))
12 #define nxt_noinline __attribute__((noinline))
13 #define nxt_cdecl
14
15
16 #if (NXT_CLANG)
17
18 /* Any __asm__ directive disables loop vectorization in GCC and Clang. */
19 #define nxt_pragma_loop_disable_vectorization \
20 __asm__("")
21
22 #else
23
24 #define nxt_pragma_loop_disable_vectorization
25
26 #endif
27
28
29 #if (NXT_HAVE_BUILTIN_EXPECT)
30
31 #define nxt_expect(c, x) \
32 __builtin_expect((long) (x), (c))
33
34 #define nxt_fast_path(x) \
35 nxt_expect(1, x)
36
37 #define nxt_slow_path(x) \
38 nxt_expect(0, x)
39
40
41 #else
42
43 #define nxt_expect(c, x) \
44 (x)
45
46 #define nxt_fast_path(x) \
47 (x)
48
49 #define nxt_slow_path(x) \
50 (x)
51
52 #endif
53
54
55 #if (NXT_HAVE_BUILTIN_UNREACHABLE)
56
57 #define nxt_unreachable() \
58 __builtin_unreachable()
59
60 #else
61
62 #define nxt_unreachable()
63
64 #endif
65
66
67 #if (NXT_HAVE_BUILTIN_PREFETCH)
68
69 #define nxt_prefetch(a) \
70 __builtin_prefetch(a)
71
72 #else
73
74 #define nxt_prefetch(a)
75
76 #endif
77
78
79 #if (NXT_HAVE_GCC_ATTRIBUTE_VISIBILITY)
80
81 #define NXT_EXPORT __attribute__((visibility("default")))
82
83 #else
84
85 #define NXT_EXPORT
86
87 #endif
88
89
90 #if (NXT_HAVE_GCC_ATTRIBUTE_MALLOC)
91
92 #define NXT_MALLOC_LIKE __attribute__((__malloc__))
93
94 #else
95
96 #define NXT_MALLOC_LIKE
97
98 #endif
99
100
101 #if (NXT_HAVE_GCC_ATTRIBUTE_ALIGNED)
102
103 #define nxt_aligned(x) __attribute__((aligned(x)))
104
105 #else
106
107 #define nxt_aligned(x)
108
109 #endif
110
111
112 #if (NXT_HAVE_GCC_ATTRIBUTE_PACKED)
113
114 #define nxt_packed __attribute__((__packed__))
115
116 #else
117
118 #define nxt_packed
119
120 #endif
121
122
123 #if (NXT_HAVE_GCC_ATTRIBUTE_UNUSED)
124
125 #define NXT_MAYBE_UNUSED __attribute__((__unused__))
126
127 #else
128
129 #define NXT_MAYBE_UNUSED
130
131 #endif
132
133
134 #if (NXT_HAVE_BUILTIN_POPCOUNT)
135
136 #define nxt_popcount __builtin_popcount
137
138 #else
139
140 nxt_inline int
nxt_popcount(unsigned int x)141 nxt_popcount(unsigned int x)
142 {
143 int count;
144
145 for (count = 0; x != 0; count++) {
146 x &= x - 1;
147 }
148
149 return count;
150 }
151
152 #endif
153
154
155 #ifndef NXT_ALIGNMENT
156
157 #if (NXT_SOLARIS)
158 #define NXT_ALIGNMENT _POINTER_ALIGNMENT /* x86_64: 8, i386: 4 */
159 /* sparcv9: 8, sparcv8: 4 */
160 #elif (__i386__ || __i386)
161 #define NXT_ALIGNMENT 4
162
163 #elif (__arm__)
164 #define NXT_ALIGNMENT 8 /* 32-bit ARM may use 64-bit load/store */
165
166 #elif (__ia64__)
167 #define NXT_ALIGNMENT 8 /* long long */
168
169 #else
170 #define NXT_ALIGNMENT NXT_PTR_SIZE
171 #endif
172
173 #endif
174
175
176 #ifndef NXT_MAX_ALIGNMENT
177
178 #if (NXT_SOLARIS)
179 #define NXT_MAX_ALIGNMENT _MAX_ALIGNMENT /* x86_64: 16, i386: 4 */
180 /* sparcv9: 16, sparcv8: 8 */
181 #elif (__i386__ || __i386)
182 #define NXT_MAX_ALIGNMENT 4
183
184 #elif (__arm__)
185 #define NXT_MAX_ALIGNMENT 16
186
187 #elif (__ia64__)
188 #define NXT_MAX_ALIGNMENT 16
189
190 #else
191 #define NXT_MAX_ALIGNMENT 16
192 #endif
193
194 #endif
195
196
197 #define nxt_alloca(size) \
198 alloca(size)
199
200
201 #define nxt_container_of(p, type, field) \
202 (type *) ((u_char *) (p) - offsetof(type, field))
203
204
205 #define nxt_pointer_to(p, offset) \
206 ((void *) ((char *) (p) + (offset)))
207
208
209 #define nxt_value_at(type, p, offset) \
210 *(type *) ((u_char *) p + offset)
211
212
213 #define nxt_nitems(x) \
214 (sizeof(x) / sizeof((x)[0]))
215
216
217 /* GCC and Clang use __builtin_abs() instead of libc abs(). */
218
219 #define nxt_abs(val) \
220 abs(val)
221
222
223 #define nxt_max(val1, val2) \
224 ((val1 < val2) ? (val2) : (val1))
225
226
227 #define nxt_min(val1, val2) \
228 ((val1 > val2) ? (val2) : (val1))
229
230
231 #define nxt_bswap32(val) \
232 ( ((val) >> 24) \
233 | (((val) & 0x00FF0000) >> 8) \
234 | (((val) & 0x0000FF00) << 8) \
235 | ((val) << 24))
236
237
238 #define nxt_is_power_of_two(value) \
239 ((((value) - 1) & (value)) == 0)
240
241
242 #define nxt_align_size(d, a) \
243 (((d) + ((size_t) (a) - 1)) & ~((size_t) (a) - 1))
244
245
246 #define nxt_align_ptr(p, a) \
247 (u_char *) (((uintptr_t) (p) + ((uintptr_t) (a) - 1)) \
248 & ~((uintptr_t) (a) - 1))
249
250 #define nxt_trunc_ptr(p, a) \
251 (u_char *) ((uintptr_t) (p) & ~((uintptr_t) (a) - 1))
252
253
254 #define nxt_length(s) \
255 (sizeof(s) - 1)
256
257
258 #endif /* _NXT_CLANG_H_INCLUDED_ */
259