|
3 | 3 | #ifndef JL_DTYPES_H |
4 | 4 | #define JL_DTYPES_H |
5 | 5 |
|
| 6 | +#include <assert.h> |
6 | 7 | #include <stddef.h> |
7 | 8 | #include <stddef.h> // double include of stddef.h fixes #3421 |
8 | 9 | #include <stdint.h> |
@@ -142,52 +143,87 @@ typedef intptr_t ssize_t; |
142 | 143 | # define JL_ATTRIBUTE_ALIGN_PTRSIZE(x) |
143 | 144 | #endif |
144 | 145 |
|
145 | | -#ifdef __has_builtin |
| 146 | +#if defined(__has_builtin) |
146 | 147 | # define jl_has_builtin(x) __has_builtin(x) |
147 | 148 | #else |
148 | 149 | # define jl_has_builtin(x) 0 |
149 | 150 | #endif |
150 | 151 |
|
151 | | -#if jl_has_builtin(__builtin_assume) |
152 | | -#define jl_assume(cond) (__extension__ ({ \ |
153 | | - __typeof__(cond) cond_ = (cond); \ |
154 | | - __builtin_assume(!!(cond_)); \ |
155 | | - cond_; \ |
156 | | - })) |
157 | | -#elif defined(__GNUC__) |
| 152 | +#ifdef JL_NDEBUG |
| 153 | +# if jl_has_builtin(__builtin_assume) |
| 154 | +# define jl_assume(cond) \ |
| 155 | + (__extension__({ \ |
| 156 | + __typeof__(cond) cond_ = (cond); \ |
| 157 | + __builtin_assume(!!(cond_)); \ |
| 158 | + cond_; \ |
| 159 | + })) |
| 160 | +# elif defined(__GNUC__) |
158 | 161 | static inline void jl_assume_(int cond) |
159 | 162 | { |
160 | 163 | if (!cond) { |
161 | 164 | __builtin_unreachable(); |
162 | 165 | } |
163 | 166 | } |
164 | | -#define jl_assume(cond) (__extension__ ({ \ |
165 | | - __typeof__(cond) cond_ = (cond); \ |
166 | | - jl_assume_(!!(cond_)); \ |
167 | | - cond_; \ |
168 | | - })) |
169 | | -#else |
170 | | -#define jl_assume(cond) (cond) |
171 | | -#endif |
172 | | - |
173 | | -#if jl_has_builtin(__builtin_assume_aligned) || defined(_COMPILER_GCC_) |
174 | | -#define jl_assume_aligned(ptr, align) __builtin_assume_aligned(ptr, align) |
175 | | -#elif defined(__GNUC__) |
176 | | -#define jl_assume_aligned(ptr, align) (__extension__ ({ \ |
177 | | - __typeof__(ptr) ptr_ = (ptr); \ |
178 | | - jl_assume(((uintptr_t)ptr) % (align) == 0); \ |
179 | | - ptr_; \ |
180 | | - })) |
181 | | -#elif defined(__cplusplus) |
| 167 | +# define jl_assume(cond) \ |
| 168 | + (__extension__({ \ |
| 169 | + __typeof__(cond) cond_ = (cond); \ |
| 170 | + jl_assume_(!!(cond_)); \ |
| 171 | + cond_; \ |
| 172 | + })) |
| 173 | +# else |
| 174 | +# define jl_assume(cond) (cond) |
| 175 | +# endif |
| 176 | +# if jl_has_builtin(__builtin_assume_aligned) || defined(_COMPILER_GCC_) |
| 177 | +# define jl_assume_aligned(ptr, align) __builtin_assume_aligned(ptr, align) |
| 178 | +# elif defined(__GNUC__) |
| 179 | +# define jl_assume_aligned(ptr, align) \ |
| 180 | + (__extension__({ \ |
| 181 | + __typeof__(ptr) ptr_ = (ptr); \ |
| 182 | + jl_assume(((uintptr_t)ptr_) % (align) == 0); \ |
| 183 | + ptr_; \ |
| 184 | + })) |
| 185 | +# elif defined(__cplusplus) |
182 | 186 | template<typename T> |
183 | 187 | static inline T |
184 | 188 | jl_assume_aligned(T ptr, unsigned align) |
185 | 189 | { |
186 | 190 | (void)jl_assume(((uintptr_t)ptr) % align == 0); |
187 | 191 | return ptr; |
188 | 192 | } |
| 193 | +# else |
| 194 | +# define jl_assume_aligned(ptr, align) (ptr) |
| 195 | +# endif |
189 | 196 | #else |
190 | | -#define jl_assume_aligned(ptr, align) (ptr) |
| 197 | +# if defined(__GNUC__) |
| 198 | +# define jl_assume(cond) \ |
| 199 | + (__extension__({ \ |
| 200 | + __typeof__(cond) cond_ = (cond); \ |
| 201 | + assert(!!(cond_)); \ |
| 202 | + cond_; \ |
| 203 | + })) |
| 204 | +# define jl_assume_aligned(ptr, align) \ |
| 205 | + (__extension__({ \ |
| 206 | + __typeof__(ptr) ptr_ = (ptr); \ |
| 207 | + assert(((uintptr_t)ptr_) % (align) == 0); \ |
| 208 | + ptr_; \ |
| 209 | + })) |
| 210 | +# elif defined(__cplusplus) |
| 211 | +# define jl_assume(cond) \ |
| 212 | + (([&]() { \ |
| 213 | + auto cond_ = (cond); \ |
| 214 | + assert(!!(cond_)); \ |
| 215 | + return cond_; \ |
| 216 | + })()) |
| 217 | +# define jl_assume_aligned(ptr, align) \ |
| 218 | + (([&]() { \ |
| 219 | + auto ptr_ = (ptr); \ |
| 220 | + assert(((uintptr_t)ptr_) % (align) == 0); \ |
| 221 | + return ptr_; \ |
| 222 | + })()) |
| 223 | +# else |
| 224 | +# define jl_assume(cond) (cond) |
| 225 | +# define jl_assume_aligned(ptr, align) (ptr) |
| 226 | +# endif |
191 | 227 | #endif |
192 | 228 |
|
193 | 229 | typedef int bool_t; |
|
0 commit comments