1 /* Common header file for the CADO project 2 3 Copyright 2007, 2008, 2009, 2010, 2011 Pierrick Gaudry, Alexander Kruppa, 4 Emmanuel Thome, Paul Zimmermann 5 6 This file is part of the CADO project. 7 8 This library is free software; you can redistribute it and/or 9 modify it under the terms of the GNU Lesser General Public 10 License as published by the Free Software Foundation; either 11 version 2.1 of the License, or (at your option) any later version. 12 13 This library is distributed in the hope that it will be useful, 14 but WITHOUT ANY WARRANTY; without even the implied warranty of 15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 16 Lesser General Public License for more details. 17 18 You should have received a copy of the GNU Lesser General Public 19 License along with this library; if not, write to the Free Software 20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 21 22 */ 23 24 #ifndef CADO_MACROS_H_ 25 #define CADO_MACROS_H_ 26 // pragma no prototypes 27 28 /**********************************************************************/ 29 /* Common asserting/debugging defines */ 30 /* See README.macro_usage */ 31 32 #include <assert.h> 33 #include <stdio.h> 34 #include <stdlib.h> 35 #include <string.h> 36 #include <errno.h> 37 #include <gmp.h> /* for __GNU_MP_VERSION */ 38 39 #define ASSERT(x) assert(x) 40 41 /* Even simple assertions are relatively expensive in very simple functions. 42 If we want them anyway to hunt a bug, define WANT_ASSERT_EXPENSIVE */ 43 #ifdef WANT_ASSERT_EXPENSIVE 44 #define ASSERT_EXPENSIVE(x) ASSERT(x) 45 #else 46 #define ASSERT_EXPENSIVE(x) 47 #endif 48 49 #ifndef CPP_STRINGIFY 50 #define CPP_STRINGIFY0(x) #x 51 #define CPP_STRINGIFY(x) CPP_STRINGIFY0(x) 52 #endif 53 #ifndef CPP_PAD 54 #define CPP_PAD(x,y) x ## y 55 #endif 56 57 #define croak__(x,y) do { \ 58 fprintf(stderr,"%s in %s at %s:%d -- %s\n", \ 59 (x),__func__,__FILE__,__LINE__,(y)); \ 60 } while (0) 61 #define croak_throw__(e, x) do { \ 62 throw e("code BUG() : condition " x \ 63 " failed at " __FILE__ ":" CPP_STRINGIFY(__LINE__)); \ 64 } while (0) 65 66 /* In C++ dtors which are not allowed to throw, use this variant instead. 67 */ 68 #define ASSERT_ALWAYS_NOTHROW(x) \ 69 do { \ 70 if (!(x)) { \ 71 croak__("code BUG() : condition " #x " failed", \ 72 "Abort"); \ 73 abort(); \ 74 } \ 75 } while (0) 76 #ifdef __cplusplus 77 #include <stdexcept> 78 #define ASSERT_ALWAYS_OR_THROW(x, e) \ 79 do { \ 80 if (!(x)) \ 81 croak_throw__(e, #x); \ 82 } while (0) 83 #define ASSERT_ALWAYS(x) ASSERT_ALWAYS_OR_THROW(x, std::runtime_error) 84 #else 85 #define ASSERT_ALWAYS(x) ASSERT_ALWAYS_NOTHROW(x) 86 #endif 87 88 /* never throw exceptions in that case, just exit */ 89 #define FATAL_ERROR_CHECK(cond, msg) \ 90 do { \ 91 if (UNLIKELY((cond))) { \ 92 croak__("Fatal error: ", msg); \ 93 abort(); \ 94 } \ 95 } while (0) 96 97 /* Note that string.h must be #included in order to use this macro */ 98 #define DIE_ERRNO_DIAG(tst, fmt, ...) do { \ 99 if (UNLIKELY(tst)) { \ 100 fprintf(stderr, fmt ": %s\n", __VA_ARGS__, strerror(errno)); \ 101 exit(1); \ 102 } \ 103 } while (0) 104 105 /* Note that string.h must be #included in order to use this macro */ 106 #define WARN_ERRNO_DIAG(tst, fmt, ...) do { \ 107 if (UNLIKELY(tst)) { \ 108 fprintf(stderr, fmt ": %s\n", __VA_ARGS__, strerror(errno)); \ 109 } \ 110 } while (0) 111 112 /* This macro is used to guard against some trivial false positives 113 * returned by static analyzer */ 114 #if defined(__COVERITY__) 115 #define ASSERT_FOR_STATIC_ANALYZER(x) do { \ 116 if (!(x)) { \ 117 abort(); \ 118 } \ 119 } while (0) 120 #else 121 #define ASSERT_FOR_STATIC_ANALYZER(x) 122 #endif 123 124 /*********************************************************************/ 125 /* Helper macros */ 126 /* See README.macro_usage */ 127 128 #ifndef ABS 129 #define ABS(x) ((x) >= 0 ? (x) : -(x)) 130 #endif 131 132 #ifndef MIN 133 #define MIN(l,o) ((l) < (o) ? (l) : (o)) 134 #endif 135 136 #ifndef MAX 137 #define MAX(h,i) ((h) > (i) ? (h) : (i)) 138 #endif 139 140 /* Handy, and does not require libm */ 141 #ifndef iceildiv 142 /* C99 defines division as truncating, i.e. rounding to 0. To get ceil in all 143 cases, we have to distinguish by sign of arguments. Note that (afaik) the 144 sign and truncation direction of the quotient with any negative operads was 145 undefined in C89. */ 146 /* iceildiv requires *unsigned* operands in spite of the name suggesting 147 (signed) integer type. For negative operands, the result is wrong. */ 148 #define iceildiv(x,y) ((x) == 0 ? 0 : ((x)-1)/(y)+1) 149 /* siceildiv requires signed operands, or the compiler will throw warnings 150 with -Wtype-limits */ 151 #define siceildiv(x,y) ((x) == 0 ? 0 : ((x)<0) + ((y)<0) == 1 ? (x)/(y) : ((x)-1+2*((y)<0))/(y)+1) 152 #endif 153 154 #define LEXGE2(X,Y,A,B) (X>A || (X == A && Y >= B)) 155 #define LEXGE3(X,Y,Z,A,B,C) (X>A || (X == A && LEXGE2(Y,Z,B,C))) 156 #define LEXLE2(X,Y,A,B) LEXGE2(A,B,X,Y) 157 #define LEXLE3(X,Y,Z,A,B,C) LEXGE3(A,B,C,X,Y,Z) 158 159 #ifndef GNUC_VERSION 160 #ifndef __GNUC__ 161 #define GNUC_VERSION(X,Y,Z) 0 162 #else 163 #define GNUC_VERSION(X,Y,Z) \ 164 (__GNUC__ == X && __GNUC_MINOR__ == Y && __GNUC_PATCHLEVEL__ == Z) 165 #endif 166 #endif 167 168 #ifndef GNUC_VERSION_ATLEAST 169 #ifndef __GNUC__ 170 #define GNUC_VERSION_ATLEAST(X,Y,Z) 0 171 #else 172 #define GNUC_VERSION_ATLEAST(X,Y,Z) \ 173 LEXGE3(__GNUC__,__GNUC_MINOR__,__GNUC_PATCHLEVEL__,X,Y,Z) 174 #endif 175 #endif 176 177 #ifndef GNUC_VERSION_ATMOST 178 #ifndef __GNUC__ 179 #define GNUC_VERSION_ATMOST(X,Y,Z) 0 180 #else 181 #define GNUC_VERSION_ATMOST(X,Y,Z) \ 182 LEXLE3(__GNUC__,__GNUC_MINOR__,__GNUC_PATCHLEVEL__,X,Y,Z) 183 #endif 184 #endif 185 186 #ifndef GMP_VERSION_ATLEAST 187 #ifndef __GNU_MP_VERSION 188 #define GMP_VERSION_ATLEAST(X,Y,Z) 0 189 #else 190 #define GMP_VERSION_ATLEAST(X,Y,Z) \ 191 LEXGE3(__GNU_MP_VERSION,__GNU_MP_VERSION_MINOR,__GNU_MP_VERSION_PATCHLEVEL,X,Y,Z) 192 #endif 193 #endif 194 195 #ifndef GMP_VERSION_ATMOST 196 #ifndef __GNU_MP_VERSION 197 #define GMP_VERSION_ATMOST(X,Y,Z) 0 198 #else 199 #define GMP_VERSION_ATMOST(X,Y,Z) \ 200 LEXLE3(__GNU_MP_VERSION,__GNU_MP_VERSION_MINOR,__GNU_MP_VERSION_PATCHLEVEL,X,Y,Z) 201 #endif 202 #endif 203 204 /* Intel icc and Clang try to imitate all predefined macros present in 205 FSF's GCC which can make it tricky to detect whether the compiler is 206 genuine GCC. Thus we define GENUINE_GNUC by explicitly testing that 207 the compiler is neither icc nor Clang. */ 208 #ifndef GENUINE_GNUC 209 #if defined(__GNUC__) && !defined(__INTEL_COMPILER) && !defined(__clang__) 210 #define GENUINE_GNUC 1 211 #endif 212 #endif 213 214 #ifndef MPI_VERSION_ATLEAST 215 #define MPI_VERSION_ATLEAST(X,Y) LEXGE2(MPI_VERSION,MPI_SUBVERSION,X,Y) 216 #endif /* MPI_VERSION_ATLEAST */ 217 218 #ifndef MPI_VERSION_ATMOST 219 #define MPI_VERSION_ATMOST(X,Y) LEXLE2(MPI_VERSION,MPI_SUBVERSION,X,Y) 220 #endif /* MPI_VERSION_ATMOST */ 221 222 #ifndef MPI_VERSION_IS 223 #define MPI_VERSION_IS(X,Y) (((X) == MPI_VERSION) && ((Y) == MPI_SUBVERSION)) 224 #endif /* MPI_VERSION_IS */ 225 226 #ifndef OMPI_VERSION_ATLEAST 227 #ifdef OPEN_MPI 228 #define OMPI_VERSION_ATLEAST(X,Y,Z) \ 229 (LEXGE3(OMPI_MAJOR_VERSION,OMPI_MINOR_VERSION,OMPI_RELEASE_VERSION,X,Y,Z)) 230 #else 231 #define OMPI_VERSION_ATLEAST(X,Y,Z) 0 232 #endif 233 #endif /* OMPI_VERSION_ATLEAST */ 234 235 #ifndef OMPI_VERSION_ATMOST 236 #ifdef OPEN_MPI 237 #define OMPI_VERSION_ATMOST(X,Y,Z) \ 238 (LEXLE3(OMPI_MAJOR_VERSION,OMPI_MINOR_VERSION,OMPI_RELEASE_VERSION,X,Y,Z)) 239 #else 240 #define OMPI_VERSION_ATMOST(X,Y,Z) 0 241 #endif 242 #endif /* OMPI_VERSION_ATMOST */ 243 244 #ifndef OMPI_VERSION_IS 245 #ifdef OPEN_MPI 246 #define OMPI_VERSION_IS(X,Y,Z) \ 247 (((X) == OMPI_MAJOR_VERSION) && \ 248 ((Y) == OMPI_MINOR_VERSION) && \ 249 ((Z) == OMPI_RELEASE_VERSION)) 250 #else 251 #define OMPI_VERSION_IS(X,Y,Z) 0 252 #endif 253 #endif /* OMPI_VERSION_IS */ 254 255 256 #ifndef MAYBE_UNUSED 257 #if GNUC_VERSION_ATLEAST(3,4,0) 258 /* according to 259 * http://gcc.gnu.org/onlinedocs/gcc-3.1.1/gcc/Variable-Attributes.html#Variable%20Attributes 260 * the 'unused' attribute already existed in 3.1.1 ; however the rules 261 * for its usage remained quirky until 3.4.0, so we prefer to stick to 262 * the more modern way of using the unused attribute, and recommend 263 * setting the -Wno-unused flag for pre-3.4 versions of gcc 264 */ 265 #define MAYBE_UNUSED __attribute__ ((unused)) 266 #else 267 #define MAYBE_UNUSED 268 #endif 269 #endif 270 271 #if __STDC_VERSION__ >= 201112L 272 #define STATIC_ASSERT(COND,MSG) _Static_assert(COND, #MSG) 273 #else 274 /* Does not work in structs */ 275 #define STATIC_ASSERT(COND,MSG) \ 276 typedef char static_assertion_##MSG[(COND)?1:-1] MAYBE_UNUSED 277 #endif 278 279 #ifndef ATTRIBUTE_WARN_UNUSED_RESULT 280 /* https://gcc.gnu.org/onlinedocs/gcc-3.3.6/gcc/Function-Attributes.html#Function-Attributes 281 * https://gcc.gnu.org/onlinedocs/gcc-3.4.6/gcc/Function-Attributes.html#Function-Attributes 282 */ 283 #if GNUC_VERSION_ATLEAST(3,4,0) 284 #define ATTRIBUTE_WARN_UNUSED_RESULT __attribute__ ((warn_unused_result)) 285 #elif defined(__clang__) 286 #if __has_attribute(warn_unused_result) 287 #define ATTRIBUTE_WARN_UNUSED_RESULT __attribute__((warn_unused_result)) 288 #else 289 #define ATTRIBUTE_WARN_UNUSED_RESULT 290 #endif 291 #else 292 #define ATTRIBUTE_WARN_UNUSED_RESULT 293 #endif 294 #endif 295 296 #ifndef ATTRIBUTE_DEPRECATED 297 #if GNUC_VERSION_ATLEAST(3,1,1) 298 #define ATTRIBUTE_DEPRECATED __attribute__ ((deprecated)) 299 #elif defined(__clang__) 300 #if __has_attribute(deprecated) 301 #define ATTRIBUTE_DEPRECATED __attribute__((deprecated)) 302 #else 303 #define ATTRIBUTE_DEPRECATED 304 #endif 305 #else 306 #define ATTRIBUTE_DEPRECATED 307 #endif 308 #endif 309 310 #ifndef ATTRIBUTE_ARTIFICIAL 311 #if GNUC_VERSION_ATLEAST(4,3,0) 312 #define ATTRIBUTE_ARTIFICIAL __attribute__ ((__artificial__)) 313 #elif defined(__clang__) 314 #if __has_attribute(artificial) 315 #define ATTRIBUTE_ARTIFICIAL __attribute__((artificial)) 316 #else 317 #define ATTRIBUTE_ARTIFICIAL 318 #endif 319 #else 320 #define ATTRIBUTE_ARTIFICIAL 321 #endif 322 #endif 323 324 #ifndef ATTRIBUTE_ALWAYS_INLINE 325 #if GNUC_VERSION_ATLEAST(3,1,0) 326 #define ATTRIBUTE_ALWAYS_INLINE __attribute__ ((__always_inline__)) 327 #elif defined(__clang__) 328 #if __has_attribute(always_inline) 329 #define ATTRIBUTE_ALWAYS_INLINE __attribute__((always_inline)) 330 #else 331 #define ATTRIBUTE_ALWAYS_INLINE 332 #endif 333 #else 334 #define ATTRIBUTE_ALWAYS_INLINE 335 #endif 336 #endif 337 338 #if defined(__GNUC__) 339 340 #ifndef NO_INLINE 341 #define NO_INLINE __attribute__ ((noinline)) 342 #endif 343 #ifndef PACKED 344 #define PACKED __attribute__ ((packed)) 345 #endif 346 #ifndef EXPECT 347 #define EXPECT(x,val) __builtin_expect(x,val) 348 #endif 349 #ifndef ATTR_ALIGNED 350 #define ATTR_ALIGNED(x) __attribute__((aligned(x))) 351 #endif 352 #ifndef HAVE_MINGW 353 #ifndef ATTR_PRINTF 354 #define ATTR_PRINTF(a,b) __attribute__((format(printf,a,b))) 355 #endif 356 #ifndef CONSTANT_P 357 #define CONSTANT_P(x) __builtin_constant_p(x) 358 #endif 359 #else 360 /* mingw's gcc is apparently unaware that the c99 format strings _may_ be 361 * recognized by the win32 printf, for who asks nicely... */ 362 #define ATTR_PRINTF(a,b) /**/ 363 #endif /* HAVE_MINGW */ 364 /* Note that ATTRIBUTE is sort of a catch-all, but its use should be 365 * discouraged, or at least limited to attributes which have been in gcc 366 * versions for a very long time. For a newly introduced gcc version, it 367 * is crucial to *NOT* use ATTRIBUTE() here, and instead define a macro 368 * which is set depending on the GCC version (see further down in this 369 * file). 370 */ 371 #ifndef ATTRIBUTE 372 #define ATTRIBUTE(x) __attribute__ (x) 373 #endif 374 #else 375 #ifndef NO_INLINE 376 #define NO_INLINE 377 #endif 378 #ifndef PACKED 379 #define PACKED 380 #endif 381 #ifndef EXPECT 382 #define EXPECT(x,val) (x) 383 #endif 384 #ifndef ATTR_ALIGNED 385 #define ATTR_ALIGNED(x) 386 #endif 387 #ifndef ATTR_PRINTF 388 #define ATTR_PRINTF(a,b) /**/ 389 #endif 390 #ifndef ATTRIBUTE 391 #define ATTRIBUTE(x) 392 #ifndef CONSTANT_P 393 #define CONSTANT_P(x) 0 394 #endif 395 #endif 396 #endif /* if defined(__GNUC__) */ 397 398 /* These warnings are a nuisance, really. Not only do we have now to 399 * add no_break() statements when we want switch cases fall through one 400 * another, but on top of that, coverity wants the corresponding lines to 401 * be preceded by a coverity[unterminated_case] comment... 402 */ 403 #if GNUC_VERSION_ATLEAST(7,0,0) && !defined(__ICC) 404 #define no_break() __attribute__ ((fallthrough)) 405 #else 406 #define no_break() 407 #endif 408 409 /* as of version __INTEL_COMPILER==_ICC==1800, attribute assume_aligned 410 * is not supported, even though the underlying gcc is 6.3... 411 * 412 * I'm flagging this as unsupported overall by icc. Maybe if someone 413 * cares to check at some later point, we could have a finer grain test 414 * case. 415 */ 416 #if GNUC_VERSION_ATLEAST(4,9,0) && !defined(__ICC) 417 #define ATTR_ASSUME_ALIGNED(x) __attribute__((assume_aligned(x))) 418 #else 419 #define ATTR_ASSUME_ALIGNED(x) 420 #endif 421 422 /* On 64 bit gcc (Ubuntu/Linaro 4.6.3-1ubuntu5) with -O3, the inline 423 asm in ularith_div_2ul_ul_ul_r() is wrongly optimized (Alex Kruppa 424 finds that gcc optimizes away the whole asm block and simply 425 leaves a constant). */ 426 /* On gcc 4.8.0, ..., 4.8.2, asm() blocks can be optimized away erroneously 427 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=58805 */ 428 /* In both cases we force gcc not to omit any asm() blocks by declaring them 429 volatile. */ 430 #if defined(VOLATILE_IF_GCC_UBUNTU_BUG) || defined(VOLATILE_IF_GCC_58805_BUG) 431 #define __VOLATILE __volatile__ 432 #else 433 #define __VOLATILE 434 #endif 435 436 #ifndef LIKELY 437 #define LIKELY(x) EXPECT(x,1) 438 #endif 439 #ifndef UNLIKELY 440 #define UNLIKELY(x) EXPECT(x,0) 441 #endif 442 443 #ifndef CADO_CONCATENATE 444 #define CADO_CONCATENATE_SUB(a,b) a##b // actually concatenate 445 #define CADO_CONCATENATE(a,b) CADO_CONCATENATE_SUB(a,b) // force expand 446 #define CADO_CONCATENATE3_SUB(a,b,c) a##b##c 447 #define CADO_CONCATENATE3(a,b,c) CADO_CONCATENATE3_SUB(a,b,c) 448 #define CADO_CONCATENATE4_SUB(a,b,c,d) a##b##c##d 449 #define CADO_CONCATENATE4(a,b,c,d) CADO_CONCATENATE4_SUB(a,b,c,d) 450 #endif 451 452 #ifndef CADO_STRINGIZE 453 #define CADO_STRINGIZE_(x) #x 454 #define CADO_STRINGIZE(x) CADO_STRINGIZE_(x) 455 #endif 456 457 #endif /* CADO_MACROS_H_ */ 458