xref: /unit/src/nxt_clang.h (revision 0:a63ceefd6ab0)
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                                                                       \
20 nxt_pragma_loop_disable_vectorization                                         \
21     __asm__("")
22 
23 #else
24 
25 #define                                                                       \
26 nxt_pragma_loop_disable_vectorization
27 
28 #endif
29 
30 
31 #if (NXT_HAVE_BUILTIN_EXPECT)
32 
33 #define                                                                       \
34 nxt_fast_path(x)                                                              \
35     __builtin_expect((long) (x), 1)
36 
37 #define                                                                       \
38 nxt_slow_path(x)                                                              \
39     __builtin_expect((long) (x), 0)
40 
41 
42 #else
43 
44 #define                                                                       \
45 nxt_fast_path(x)                                                              \
46     (x)
47 
48 #define                                                                       \
49 nxt_slow_path(x)                                                              \
50     (x)
51 
52 #endif
53 
54 
55 #if (NXT_HAVE_BUILTIN_UNREACHABLE)
56 
57 #define                                                                       \
58 nxt_unreachable()                                                             \
59     __builtin_unreachable()
60 
61 #else
62 
63 #define                                                                       \
64 nxt_unreachable()
65 
66 #endif
67 
68 
69 #if (NXT_HAVE_BUILTIN_PREFETCH)
70 
71 #define                                                                       \
72 nxt_prefetch(a)                                                               \
73     __builtin_prefetch(a)
74 
75 #else
76 
77 #define                                                                       \
78 nxt_prefetch(a)
79 
80 #endif
81 
82 
83 #if (NXT_HAVE_GCC_ATTRIBUTE_VISIBILITY)
84 
85 #define NXT_EXPORT         __attribute__((visibility("default")))
86 
87 #else
88 
89 #define NXT_EXPORT
90 
91 #endif
92 
93 
94 #if (NXT_HAVE_GCC_ATTRIBUTE_MALLOC)
95 
96 #define NXT_MALLOC_LIKE    __attribute__((__malloc__))
97 
98 #else
99 
100 #define NXT_MALLOC_LIKE
101 
102 #endif
103 
104 
105 #if (NXT_HAVE_GCC_ATTRIBUTE_ALIGNED)
106 
107 #define nxt_aligned(x)     __attribute__((aligned(x)))
108 
109 #else
110 
111 #define nxt_aligned(x)
112 
113 #endif
114 
115 
116 #ifndef NXT_ALIGNMENT
117 
118 #if (NXT_SOLARIS)
119 #define NXT_ALIGNMENT      _POINTER_ALIGNMENT     /* x86_64: 8,   i386: 4    */
120                                                   /* sparcv9: 8,  sparcv8: 4 */
121 #elif (__i386__ || __i386)
122 #define NXT_ALIGNMENT      4
123 
124 #elif (__arm__)
125 #define NXT_ALIGNMENT      8         /* 32-bit ARM may use 64-bit load/store */
126 
127 #elif (__ia64__)
128 #define NXT_ALIGNMENT      8         /* long long */
129 
130 #else
131 #define NXT_ALIGNMENT      NXT_PTR_SIZE
132 #endif
133 
134 #endif
135 
136 
137 #ifndef NXT_MAX_ALIGNMENT
138 
139 #if (NXT_SOLARIS)
140 #define NXT_MAX_ALIGNMENT  _MAX_ALIGNMENT        /* x86_64: 16,   i386: 4    */
141                                                  /* sparcv9: 16,  sparcv8: 8 */
142 #elif (__i386__ || __i386)
143 #define NXT_MAX_ALIGNMENT  4
144 
145 #elif (__arm__)
146 #define NXT_MAX_ALIGNMENT  16
147 
148 #elif (__ia64__)
149 #define NXT_MAX_ALIGNMENT  16
150 
151 #else
152 #define NXT_MAX_ALIGNMENT  16
153 #endif
154 
155 #endif
156 
157 
158 #define                                                                       \
159 nxt_alloca(size)                                                              \
160     alloca(size)
161 
162 
163 #define                                                                       \
164 nxt_container_of(p, type, field)                                              \
165     (type *) ((u_char *) (p) - offsetof(type, field))
166 
167 
168 #define                                                                       \
169 nxt_nitems(x)                                                                 \
170     (sizeof(x) / sizeof((x)[0]))
171 
172 
173 /* GCC and Clang use __builtin_abs() instead of libc abs(). */
174 
175 #define                                                                       \
176 nxt_abs(val)                                                                  \
177     abs(val)
178 
179 
180 #define                                                                       \
181 nxt_max(val1, val2)                                                           \
182     ((val1 < val2) ? (val2) : (val1))
183 
184 
185 #define                                                                       \
186 nxt_min(val1, val2)                                                           \
187     ((val1 > val2) ? (val2) : (val1))
188 
189 
190 #define                                                                       \
191 nxt_bswap32(val)                                                              \
192     (   ((val)               >> 24)                                           \
193      | (((val) & 0x00ff0000) >>  8)                                           \
194      | (((val) & 0x0000ff00) <<  8)                                           \
195      |  ((val)               << 24))
196 
197 
198 
199 #define                                                                       \
200 nxt_align_size(d, a)                                                          \
201     (((d) + ((size_t) (a) - 1)) & ~((size_t) (a) - 1))
202 
203 
204 #define                                                                       \
205 nxt_align_ptr(p, a)                                                           \
206     (u_char *) (((uintptr_t) (p) + ((uintptr_t) (a) - 1))                     \
207                   & ~((uintptr_t) (a) - 1))
208 
209 #define                                                                       \
210 nxt_trunc_ptr(p, a)                                                           \
211     (u_char *) ((uintptr_t) (p) & ~((uintptr_t) (a) - 1))
212 
213 
214 #endif /* _NXT_CLANG_H_INCLUDED_ */
215