1 #ifndef PETSC_PREPROCESSOR_MACROS_H 2 #define PETSC_PREPROCESSOR_MACROS_H 3 4 #include <petscconf.h> 5 #include <petscconf_poison.h> /* for PetscDefined() error checking */ 6 7 /* SUBMANSEC = Sys */ 8 9 #if defined(__cplusplus) 10 #if __cplusplus <= 201103L 11 #define PETSC_CPP_VERSION 11 12 #elif __cplusplus <= 201402L 13 #define PETSC_CPP_VERSION 14 14 #elif __cplusplus <= 201703L 15 #define PETSC_CPP_VERSION 17 16 #elif __cplusplus <= 202002L 17 #define PETSC_CPP_VERSION 20 18 #else 19 #define PETSC_CPP_VERSION 22 // current year, or date of c++2b ratification 20 #endif 21 #endif // __cplusplus 22 23 #ifndef PETSC_CPP_VERSION 24 #define PETSC_CPP_VERSION 0 25 #endif 26 27 #if defined(__STDC_VERSION__) 28 #if __STDC_VERSION__ <= 199901L 29 // C99 except that 99 is >= 11 or 17 so we shorten it to 9 instead 30 #define PETSC_C_VERSION 9 31 #elif __STDC_VERSION__ <= 201112L 32 #define PETSC_C_VERSION 11 33 #elif __STDC_VERSION__ <= 201710L 34 #define PETSC_C_VERSION 17 35 #else 36 #define PETSC_C_VERSION 22 // current year, or date of c2b ratification 37 #endif 38 #endif // __STDC_VERSION__ 39 40 #ifndef PETSC_C_VERSION 41 #define PETSC_C_VERSION 0 42 #endif 43 44 /* ========================================================================== */ 45 /* This facilitates using the C version of PETSc from C++ and the C++ version from C. */ 46 #if defined(__cplusplus) 47 #define PETSC_FUNCTION_NAME PETSC_FUNCTION_NAME_CXX 48 #else 49 #define PETSC_FUNCTION_NAME PETSC_FUNCTION_NAME_C 50 #endif 51 52 /* ========================================================================== */ 53 /* Since PETSc manages its own extern "C" handling users should never include PETSc include 54 * files within extern "C". This will generate a compiler error if a user does put the include 55 * file within an extern "C". 56 */ 57 #if defined(__cplusplus) 58 void assert_never_put_petsc_headers_inside_an_extern_c(int); 59 void assert_never_put_petsc_headers_inside_an_extern_c(double); 60 #endif 61 62 #if defined(__cplusplus) 63 #define PETSC_RESTRICT PETSC_CXX_RESTRICT 64 #else 65 #define PETSC_RESTRICT restrict 66 #endif 67 68 #define PETSC_INLINE PETSC_DEPRECATED_MACRO("GCC warning \"PETSC_INLINE is deprecated (since version 3.17)\"") inline 69 #define PETSC_STATIC_INLINE PETSC_DEPRECATED_MACRO("GCC warning \"PETSC_STATIC_INLINE is deprecated (since version 3.17)\"") static inline 70 71 #if defined(_WIN32) && defined(PETSC_USE_SHARED_LIBRARIES) /* For Win32 shared libraries */ 72 #define PETSC_DLLEXPORT __declspec(dllexport) 73 #define PETSC_DLLIMPORT __declspec(dllimport) 74 #define PETSC_VISIBILITY_INTERNAL 75 #elif defined(__cplusplus) && defined(PETSC_USE_VISIBILITY_CXX) 76 #define PETSC_DLLEXPORT __attribute__((visibility("default"))) 77 #define PETSC_DLLIMPORT __attribute__((visibility("default"))) 78 #define PETSC_VISIBILITY_INTERNAL __attribute__((visibility("hidden"))) 79 #elif !defined(__cplusplus) && defined(PETSC_USE_VISIBILITY_C) 80 #define PETSC_DLLEXPORT __attribute__((visibility("default"))) 81 #define PETSC_DLLIMPORT __attribute__((visibility("default"))) 82 #define PETSC_VISIBILITY_INTERNAL __attribute__((visibility("hidden"))) 83 #else 84 #define PETSC_DLLEXPORT 85 #define PETSC_DLLIMPORT 86 #define PETSC_VISIBILITY_INTERNAL 87 #endif 88 89 #if defined(petsc_EXPORTS) /* CMake defines this when building the shared library */ 90 #define PETSC_VISIBILITY_PUBLIC PETSC_DLLEXPORT 91 #else /* Win32 users need this to import symbols from petsc.dll */ 92 #define PETSC_VISIBILITY_PUBLIC PETSC_DLLIMPORT 93 #endif 94 95 /* Functions tagged with PETSC_EXTERN in the header files are always defined as extern "C" when 96 * compiled with C++ so they may be used from C and are always visible in the shared libraries 97 */ 98 #if defined(__cplusplus) 99 #define PETSC_EXTERN extern "C" PETSC_VISIBILITY_PUBLIC 100 #define PETSC_EXTERN_TYPEDEF extern "C" 101 #define PETSC_INTERN extern "C" PETSC_VISIBILITY_INTERNAL 102 #else 103 #define PETSC_EXTERN extern PETSC_VISIBILITY_PUBLIC 104 #define PETSC_EXTERN_TYPEDEF 105 #define PETSC_INTERN extern PETSC_VISIBILITY_INTERNAL 106 #endif 107 108 #if defined(PETSC_USE_SINGLE_LIBRARY) 109 #define PETSC_SINGLE_LIBRARY_INTERN PETSC_INTERN 110 #else 111 #define PETSC_SINGLE_LIBRARY_INTERN PETSC_EXTERN 112 #endif 113 114 #if !defined(__has_feature) 115 #define __has_feature(x) 0 116 #endif 117 118 /*MC 119 PetscHasAttribute - Determine whether a particular __attribute__ is supported by the compiler 120 121 Synopsis: 122 #include <petscmacros.h> 123 int PetscHasAttribute(name) 124 125 Input Parameter: 126 . name - The name of the attribute to test 127 128 Notes: 129 name should be identical to what you might pass to the __attribute__ declaration itself -- 130 plain, unbroken text. 131 132 As `PetscHasAttribute()` is wrapper over the function-like macro `__has_attribute()`, the 133 exact type and value returned is implementation defined. In practice however, it usually 134 returns `1` if the attribute is supported and `0` if the attribute is not supported. 135 136 Example Usage: 137 Typical usage is using the preprocessor 138 139 .vb 140 #if PetscHasAttribute(always_inline) 141 # define MY_ALWAYS_INLINE __attribute__((always_inline)) 142 #else 143 # define MY_ALWAYS_INLINE 144 #endif 145 146 void foo(void) MY_ALWAYS_INLINE; 147 .ve 148 149 but it can also be used in regular code 150 151 .vb 152 if (PetscHasAttribute(some_attribute)) { 153 foo(); 154 } else { 155 bar(); 156 } 157 .ve 158 159 Level: intermediate 160 161 .seealso: `PetscHasBuiltin()`, `PetscDefined()`, `PetscLikely()`, `PetscUnlikely()`, 162 `PETSC_ATTRIBUTE_FORMAT`, `PETSC_ATTRIBUTE_MAY_ALIAS` 163 M*/ 164 #if !defined(__has_attribute) 165 #define __has_attribute(x) 0 166 #endif 167 #define PetscHasAttribute(name) __has_attribute(name) 168 169 /*MC 170 PetscHasBuiltin - Determine whether a particular builtin method is supported by the compiler 171 172 Synopsis: 173 #include <petscmacros.h> 174 int PetscHasBuiltin(name) 175 176 Input Parameter: 177 . name - the name of the builtin routine 178 179 Notes: 180 Evaluates to `1` if the builtin is supported and `0` otherwise. Note the term "evaluates" 181 (vs "expands") is deliberate; even though `PetscHasBuiltin()` is a macro the underlying 182 detector is itself is a compiler extension with implementation-defined return type and 183 semantics. Some compilers implement it as a macro, others as a compiler function. In practice 184 however, all supporting compilers return an integer boolean as described. 185 186 Example Usage: 187 Typical usage is in preprocessor directives 188 189 .vb 190 #if PetscHasBuiltin(__builtin_trap) 191 __builtin_trap(); 192 #else 193 abort(); 194 #endif 195 .ve 196 197 But it may also be used in regular code 198 199 .vb 200 if (PetscHasBuiltin(__builtin_alloca)) { 201 foo(); 202 } else { 203 bar(); 204 } 205 .ve 206 207 Level: intermediate 208 209 .seealso: `PetscHasAttribute()`, `PetscAssume()` 210 M*/ 211 #if !defined(__has_builtin) 212 #define __has_builtin(x) 0 213 #endif 214 // clangs __has_builtin prior to clang 10 did not properly handle non-function builtins such as 215 // __builtin_types_compatible_p which take types or other non-functiony things as 216 // arguments. The correct way to detect these then is to use __is_identifier (also a clang 217 // extension). GCC has always worked as expected. see https://stackoverflow.com/a/45043153 218 #if defined(__clang__) && defined(__clang_major__) && (__clang_major__ < 10) && defined(__is_identifier) 219 #define PetscHasBuiltin(name) __is_identifier(name) 220 #else 221 #define PetscHasBuiltin(name) __has_builtin(name) 222 #endif 223 224 #if !defined(PETSC_SKIP_ATTRIBUTE_MPI_TYPE_TAG) 225 /* 226 Support for Clang (>=3.2) matching type tag arguments with void* buffer types. 227 This allows the compiler to detect cases where the MPI datatype argument passed to a MPI routine 228 does not match the actual type of the argument being passed in 229 */ 230 #if PetscHasAttribute(pointer_with_type_tag) 231 #define PETSC_ATTRIBUTE_MPI_POINTER_WITH_TYPE(bufno, typeno) __attribute__((pointer_with_type_tag(MPI, bufno, typeno))) 232 #endif 233 234 #if PetscHasAttribute(type_tag_for_datatype) 235 #define PETSC_ATTRIBUTE_MPI_TYPE_TAG(type) __attribute__((type_tag_for_datatype(MPI, type))) 236 #define PETSC_ATTRIBUTE_MPI_TYPE_TAG_LAYOUT_COMPATIBLE(type) __attribute__((type_tag_for_datatype(MPI, type, layout_compatible))) 237 #endif 238 #endif // PETSC_SKIP_ATTRIBUTE_MPI_TYPE_TAG 239 240 #ifndef PETSC_ATTRIBUTE_MPI_POINTER_WITH_TYPE 241 #define PETSC_ATTRIBUTE_MPI_POINTER_WITH_TYPE(bufno, typeno) 242 #endif 243 244 #ifndef PETSC_ATTRIBUTE_MPI_TYPE_TAG 245 #define PETSC_ATTRIBUTE_MPI_TYPE_TAG(type) 246 #endif 247 248 #ifndef PETSC_ATTRIBUTE_MPI_TYPE_TAG_LAYOUT_COMPATIBLE 249 #define PETSC_ATTRIBUTE_MPI_TYPE_TAG_LAYOUT_COMPATIBLE(type) 250 #endif 251 252 /*MC 253 PETSC_ATTRIBUTE_FORMAT - Indicate to the compiler that specified arguments should be treated 254 as format specifiers and checked for validity 255 256 Synopsis: 257 #include <petscmacros.h> 258 <attribute declaration> PETSC_ATTRIBUTE_FORMAT(int strIdx, int vaArgIdx) 259 260 Input Parameters: 261 + strIdx - The (1-indexed) location of the format string in the argument list 262 - vaArgIdx - The (1-indexed) location of the first formattable argument in the argument list 263 264 Notes: 265 This function attribute causes the compiler to issue warnings when the format specifier does 266 not match the type of the variable that will be formatted, or when there exists a mismatch 267 between the number of format specifiers and variables to be formatted. It is safe to use this 268 macro if your compiler does not support format specifier checking (though this is 269 exceeedingly rare). 270 271 Both strIdx and vaArgIdx must be compile-time constant integer literals and cannot have the 272 same value. 273 274 The arguments to be formatted (and therefore checked by the compiler) must be "contiguous" in 275 the argument list, that is, there is no way to indicate gaps which should not be checked. 276 277 Definition is suppressed by defining `PETSC_SKIP_ATTRIBUTE_FORMAT` prior to including PETSc 278 header files. In this case the macro will expand empty. 279 280 Example Usage: 281 .vb 282 // format string is 2nd argument, variable argument list containing args is 3rd argument 283 void my_printf(void *obj, const char *fmt_string, ...) PETSC_ATTRIBUTE_FORMAT(2,3) 284 285 int x = 1; 286 double y = 50.0; 287 288 my_printf(NULL,"%g",x); // WARNING, format specifier does not match for 'int'! 289 my_printf(NULL,"%d",x,y); // WARNING, more arguments than format specifiers! 290 my_printf(NULL,"%d %g",x,y); // OK 291 .ve 292 293 Level: developer 294 295 .seealso: `PETSC_ATTRIBUTE_COLD`, `PetscHasAttribute()` 296 M*/ 297 #if PetscHasAttribute(format) && !defined(PETSC_SKIP_ATTRIBUTE_FORMAT) 298 #define PETSC_ATTRIBUTE_FORMAT(strIdx, vaArgIdx) __attribute__((format(printf, strIdx, vaArgIdx))) 299 #else 300 #define PETSC_ATTRIBUTE_FORMAT(strIdx, vaArgIdx) 301 #endif 302 303 /*MC 304 PETSC_ATTRIBUTE_COLD - Indicate to the compiler that a function is very unlikely to be 305 executed 306 307 Notes: 308 The marked function is often optimized for size rather than speed and may be grouped alongside 309 other equally frigid routines improving code locality of lukewarm or hotter parts of program. 310 311 The paths leading to cold functions are usually automatically marked as unlikely by the 312 compiler. It may thus be useful to mark functions used to handle unlikely conditions -- such 313 as error handlers -- as cold to improve optimization of the surrounding temperate functions. 314 315 Example Usage: 316 .vb 317 void my_error_handler(...) PETSC_ATTRIBUTE_COLD; 318 319 if (temperature < 0) { 320 return my_error_handler(...); // chilly! 321 } 322 .ve 323 324 Level: intermediate 325 326 .seealso: `PetscUnlikely()`, `PetscUnlikelyDebug()`, `PetscLikely()`, `PetscLikelyDebug()`, 327 `PetscUnreachable()`, `PETSC_ATTRIBUTE_FORMAT` 328 M*/ 329 #if PetscHasAttribute(__cold__) 330 #define PETSC_ATTRIBUTE_COLD __attribute__((__cold__)) 331 #elif PetscHasAttribute(cold) /* some implementations (old gcc) use no underscores */ 332 #define PETSC_ATTRIBUTE_COLD __attribute__((cold)) 333 #else 334 #define PETSC_ATTRIBUTE_COLD 335 #endif 336 337 /*MC 338 PETSC_ATTRIBUTE_MAY_ALIAS - Indicate to the compiler that a type is not 339 subjected to type-based alias analysis, but is instead assumed to be able to 340 alias any other type of objects 341 342 Example Usage: 343 .vb 344 typedef PetscScalar PetscScalarAlias PETSC_ATTRIBUTE_MAY_ALIAS; 345 346 PetscReal *pointer; 347 PetscScalarAlias *other_pointer = reinterpret_cast<PetscScalarAlias *>(pointer); 348 .ve 349 350 Level: advanced 351 352 .seealso: `PetscHasAttribute()` 353 M*/ 354 #if PetscHasAttribute(may_alias) && !defined(PETSC_SKIP_ATTRIBUTE_MAY_ALIAS) 355 #define PETSC_ATTRIBUTE_MAY_ALIAS __attribute__((may_alias)) 356 #else 357 #define PETSC_ATTRIBUTE_MAY_ALIAS 358 #endif 359 360 /*MC 361 PETSC_NULLPTR - Standard way of indicating a null value or pointer 362 363 No Fortran Support 364 365 Level: beginner 366 367 Notes: 368 Equivalent to `NULL` in C source, and nullptr in C++ source. Note that for the purposes of 369 interoperability between C and C++, setting a pointer to `PETSC_NULLPTR` in C++ is functionally 370 equivalent to setting the same pointer to `NULL` in C. That is to say that the following 371 expressions are equivalent\: 372 373 .vb 374 ptr == PETSC_NULLPTR 375 ptr == NULL 376 ptr == 0 377 !ptr 378 379 ptr = PETSC_NULLPTR 380 ptr = NULL 381 ptr = 0 382 .ve 383 384 and for completeness' sake\: 385 386 .vb 387 PETSC_NULLPTR == NULL 388 .ve 389 390 Example Usage: 391 .vb 392 // may be used in place of '\0' or other such teminators in the definition of char arrays 393 const char *const MyEnumTypes[] = { 394 "foo", 395 "bar", 396 PETSC_NULLPTR 397 }; 398 399 // may be used to nullify objects 400 PetscObject obj = PETSC_NULLPTR; 401 402 // may be used in any function expecting NULL 403 PetscInfo(PETSC_NULLPTR,"Lorem Ipsum Dolor"); 404 .ve 405 406 Developer Notes: 407 `PETSC_NULLPTR` must be used in place of NULL in all C++ source files. Using NULL in source 408 files compiled with a C++ compiler may lead to unexpected side-effects in function overload 409 resolution and/or compiler warnings. 410 411 .seealso: `PETSC_CONSTEXPR_14`, `PETSC_NODISCARD` 412 M*/ 413 414 /*MC 415 PETSC_CONSTEXPR_14 - C++14 constexpr 416 417 No Fortran Support 418 419 Level: beginner 420 421 Notes: 422 Equivalent to constexpr when using a C++ compiler that supports C++14. Expands to nothing 423 if the C++ compiler does not support C++14 or when not compiling with a C++ compiler. Note 424 that this cannot be used in cases where an empty expansion would result in invalid code. It 425 is safe to use this in C source files. 426 427 Example Usage: 428 .vb 429 PETSC_CONSTEXPR_14 int factorial(int n) 430 { 431 int r = 1; 432 433 do { 434 r *= n; 435 } while (--n); 436 return r; 437 } 438 .ve 439 440 .seealso: `PETSC_NULLPTR`, `PETSC_NODISCARD` 441 M*/ 442 443 /*MC 444 PETSC_NODISCARD - Mark the return value of a function as non-discardable 445 446 Not available in Fortran 447 448 Level: beginner 449 450 Notes: 451 Hints to the compiler that the return value of a function must be captured. A diagnostic may 452 (but is not required to) be emitted if the value is discarded. It is safe to use this in both 453 C and C++ source files. 454 455 Example Usage: 456 .vb 457 class Foo 458 { 459 int x; 460 461 public: 462 PETSC_NODISCARD Foo(int y) : x(y) { } 463 }; 464 465 PETSC_NODISCARD int factorial(int n) 466 { 467 return n <= 1 ? 1 : (n * factorial(n - 1)); 468 } 469 470 auto x = factorial(10); // OK, capturing return value 471 factorial(10); // Warning: ignoring return value of function declared 'nodiscard' 472 473 auto f = Foo(x); // OK, capturing constructed object 474 Foo(x); // Warning: Ignoring temporary created by a constructor declared 'nodiscard' 475 .ve 476 477 .seealso: `PETSC_NULLPTR`, `PETSC_CONSTEXPR_14` 478 M*/ 479 480 /* C++11 features */ 481 #if defined(__cplusplus) || (PETSC_C_VERSION >= 23) 482 #define PETSC_NULLPTR nullptr 483 #else 484 #define PETSC_NULLPTR NULL 485 #endif 486 487 /* C++14 features */ 488 #if PETSC_CPP_VERSION >= 14 489 #define PETSC_CONSTEXPR_14 constexpr 490 #else 491 #define PETSC_CONSTEXPR_14 492 #endif 493 494 /* C++17 features */ 495 #if PETSC_CPP_VERSION >= 17 496 #define PETSC_CONSTEXPR_17 constexpr 497 #else 498 #define PETSC_CONSTEXPR_17 499 #endif 500 501 #if (PETSC_CPP_VERSION >= 17) || (PETSC_C_VERSION >= 23) 502 #define PETSC_NODISCARD [[nodiscard]] 503 #elif PetscHasAttribute(warn_unused_result) 504 #define PETSC_NODISCARD __attribute__((warn_unused_result)) 505 #else 506 #define PETSC_NODISCARD 507 #endif 508 509 #include <petscversion.h> 510 #define PETSC_AUTHOR_INFO " The PETSc Team\n petsc-maint@mcs.anl.gov\n https://petsc.org/\n" 511 512 /* designated initializers since C99 and C++20, MSVC never supports them though */ 513 #if defined(_MSC_VER) || (defined(__cplusplus) && (PETSC_CPP_VERSION < 20)) 514 #define PetscDesignatedInitializer(name, ...) __VA_ARGS__ 515 #else 516 #define PetscDesignatedInitializer(name, ...) .name = __VA_ARGS__ 517 #endif 518 519 /*MC 520 PetscUnlikely - Hints the compiler that the given condition is usually false 521 522 Synopsis: 523 #include <petscmacros.h> 524 bool PetscUnlikely(bool cond) 525 526 Not Collective 527 528 Input Parameter: 529 . cond - Boolean expression 530 531 Notes: 532 Not available from fortran. 533 534 This returns the same truth value, it is only a hint to compilers that the result of cond is 535 unlikely to be true. 536 537 Example usage: 538 .vb 539 if (PetscUnlikely(cond)) { 540 foo(); // cold path 541 } else { 542 bar(); // hot path 543 } 544 .ve 545 546 Level: advanced 547 548 .seealso: `PetscLikely()`, `PetscUnlikelyDebug()`, `PetscCall()`, `PetscDefined()`, `PetscHasAttribute()`, 549 `PETSC_ATTRIBUTE_COLD` 550 M*/ 551 552 /*MC 553 PetscLikely - Hints the compiler that the given condition is usually true 554 555 Synopsis: 556 #include <petscmacros.h> 557 bool PetscLikely(bool cond) 558 559 Not Collective 560 561 Input Parameter: 562 . cond - Boolean expression 563 564 Notes: 565 Not available from fortran. 566 567 This returns the same truth value, it is only a hint to compilers that the result of cond is 568 likely to be true. 569 570 Example usage: 571 .vb 572 if (PetscLikely(cond)) { 573 foo(); // hot path 574 } else { 575 bar(); // cold path 576 } 577 .ve 578 579 Level: advanced 580 581 .seealso: `PetscUnlikely()`, `PetscDefined()`, `PetscHasAttribute()` 582 `PETSC_ATTRIBUTE_COLD` 583 M*/ 584 #if defined(PETSC_HAVE_BUILTIN_EXPECT) 585 #define PetscUnlikely(cond) __builtin_expect(!!(cond), 0) 586 #define PetscLikely(cond) __builtin_expect(!!(cond), 1) 587 #else 588 #define PetscUnlikely(cond) (cond) 589 #define PetscLikely(cond) (cond) 590 #endif 591 592 /*MC 593 PetscUnreachable - Indicate to the compiler that a code-path is logically unreachable 594 595 Synopsis: 596 #include <petscmacros.h> 597 void PetscUnreachable(void) 598 599 Notes: 600 Indicates to the compiler (usually via some built-in) that a particular code path is always 601 unreachable. Behavior is undefined if this function is ever executed, the user can expect an 602 unceremonious crash. 603 604 Example usage: 605 Useful in situations such as switches over enums where not all enumeration values are 606 explicitly covered by the switch 607 608 .vb 609 typedef enum {RED, GREEN, BLUE} Color; 610 611 int foo(Color c) 612 { 613 // it is known to programmer (or checked previously) that c is either RED or GREEN 614 // but compiler may not be able to deduce this and/or emit spurious warnings 615 switch (c) { 616 case RED: 617 return bar(); 618 case GREEN: 619 return baz(); 620 default: 621 PetscUnreachable(); // program is ill-formed if executed 622 } 623 } 624 .ve 625 626 Level: advanced 627 628 .seealso: `SETERRABORT()`, `PETSCABORT()`, `PETSC_ATTRIBUTE_COLD`, `PetscAssume()` 629 M*/ 630 #if PETSC_CPP_VERSION >= 23 631 #include <utility> 632 #define PetscUnreachable() std::unreachable() 633 #elif defined(__GNUC__) 634 /* GCC 4.8+, Clang, Intel and other compilers compatible with GCC (-std=c++0x or above) */ 635 #define PetscUnreachable() __builtin_unreachable() 636 #elif defined(_MSC_VER) /* MSVC */ 637 #define PetscUnreachable() __assume(0) 638 #else /* ??? */ 639 #define PetscUnreachable() SETERRABORT(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Code path explicitly marked as unreachable executed") 640 #endif 641 642 /*MC 643 PetscAssume - Indicate to the compiler a condition that is defined to be true 644 645 Synopsis: 646 #include <petscmacros.h> 647 void PetscAssume(bool cond) 648 649 Input Parameter: 650 . cond - Boolean expression 651 652 Notes: 653 If supported by the compiler, `cond` is used to inform the optimizer of an invariant 654 truth. The argument itself is never evaluated, so any side effects of the expression will be 655 discarded. This macro is used in `PetscAssert()` to retain information gained from debug 656 checks that would be lost in optimized builds. For example\: 657 658 .vb 659 PetscErrorCode foo(PetscInt x) { 660 661 PetscAssert(x >= 0, ...); 662 } 663 .ve 664 665 The assertion checks that `x` is positive when debugging is enabled (and returns from `foo()` 666 if it is not). This implicitly informs the optimizer that `x` cannot be negative. However, 667 when debugging is disabled any `PetscAssert()` checks are tautologically false, and hence the 668 optimizer cannot deduce any information from them. 669 670 Due to compiler limitations `PetscAssume()` works best when `cond` involves 671 constants. Certain compilers do not yet propagate symbolic inequalities i.e.\: 672 673 .vb 674 int a, b, var_five; 675 676 // BEST, all supporting compilers will understand a cannot be >= 5 677 PetscAssume(a < 5); 678 679 // OK, some compilers may understand that a cannot be >= 5 680 PetscAssume(a <= b && b < 5); 681 682 // WORST, most compilers will not get the memo 683 PetscAssume(a <= b && b < var_five); 684 .ve 685 686 If the condition is violated at runtime then behavior is wholly undefined. If the 687 condition is violated at compile-time, the condition "supersedes" the compile-time violation 688 and the program is ill-formed, no diagnostic required. For example consider the following\: 689 690 .vb 691 PetscInt x = 0; 692 693 PetscAssume(x != 0); 694 if (x == 0) { 695 x += 10; 696 } else { 697 popen("rm -rf /", "w"); 698 } 699 .ve 700 701 Even though `x` is demonstrably `0` the compiler may opt to\: 702 703 - emit an unconditional `popen("rm -rf /", "w")` 704 - ignore `PetscAssume()` altogether and emit the correct path of `x += 10` 705 - reformat the primary disk partition 706 707 Level: advanced 708 709 .seealso: `PetscAssert()` 710 M*/ 711 #if PETSC_CPP_VERSION >= 23 712 #define PetscAssume(...) [[assume(__VA_ARGS__)]] 713 #elif defined(_MSC_VER) // msvc 714 #define PetscAssume(...) __assume(__VA_ARGS__) 715 #elif defined(__clang__) && PetscHasBuiltin(__builtin_assume) // clang 716 #define PetscAssume(...) \ 717 do { \ 718 _Pragma("clang diagnostic push"); \ 719 _Pragma("clang diagnostic ignored \"-Wassume\""); \ 720 __builtin_assume(__VA_ARGS__); \ 721 _Pragma("clang diagnostic pop"); \ 722 } while (0) 723 #else // gcc (and really old clang) 724 // gcc does not have its own __builtin_assume() intrinsic. One could fake it via 725 // 726 // if (PetscUnlikely(!cond)) PetscUnreachable(); 727 // 728 // but this it unsavory because the side effects of cond are not guaranteed to be 729 // discarded. Though in most circumstances gcc will optimize out the if (because any evaluation 730 // for which cond is false would be undefined results in undefined behavior anyway) it cannot 731 // always do so. This is especially the case for opaque or non-inline function calls: 732 // 733 // extern int bar(int); 734 // 735 // int foo(int x) { 736 // PetscAssume(bar(x) == 2); 737 // if (bar(x) == 2) { 738 // return 1; 739 // } else { 740 // return 0; 741 // } 742 // } 743 // 744 // Here gcc would (if just using builtin_expect()) emit 2 calls to bar(). Note we still have 745 // cond "tested" in the condition, but this is done to silence unused-but-set variable warnings 746 #define PetscAssume(...) \ 747 do { \ 748 if (0 && (__VA_ARGS__)) PetscUnreachable(); \ 749 } while (0) 750 #endif 751 752 /*MC 753 PetscExpand - Expand macro argument 754 755 Synopsis: 756 #include <petscmacros.h> 757 <macro-expansion> PetscExpand(x) 758 759 Input Parameter: 760 . x - The preprocessor token to expand 761 762 Level: beginner 763 764 .seealso: `PetscStringize()`, `PetscConcat()` 765 M*/ 766 #define PetscExpand_(...) __VA_ARGS__ 767 #define PetscExpand(...) PetscExpand_(__VA_ARGS__) 768 769 /*MC 770 PetscStringize - Stringize a token 771 772 Synopsis: 773 #include <petscmacros.h> 774 const char* PetscStringize(x) 775 776 Input Parameter: 777 . x - The token you would like to stringize 778 779 Output Parameter: 780 . <return-value> - The string representation of x 781 782 Notes: 783 Not available from Fortran. 784 785 PetscStringize() expands x before stringizing it, if you do not wish to do so, use 786 PetscStringize_() instead. 787 788 Example Usage: 789 .vb 790 #define MY_OTHER_VAR hello there 791 #define MY_VAR MY_OTHER_VAR 792 793 PetscStringize(MY_VAR) -> "hello there" 794 PetscStringize_(MY_VAR) -> "MY_VAR" 795 796 int foo; 797 PetscStringize(foo) -> "foo" 798 PetscStringize_(foo) -> "foo" 799 .ve 800 801 Level: beginner 802 803 .seealso: `PetscConcat()`, `PetscExpandToNothing()`, `PetscExpand()` 804 M*/ 805 #define PetscStringize_(...) #__VA_ARGS__ 806 #define PetscStringize(...) PetscStringize_(__VA_ARGS__) 807 808 /*MC 809 PetscConcat - Concatenate two tokens 810 811 Synopsis: 812 #include <petscmacros.h> 813 <macro-expansion> PetscConcat(x, y) 814 815 Input Parameters: 816 + x - First token 817 - y - Second token 818 819 Notes: 820 Not available from Fortran. 821 822 PetscConcat() will expand both arguments before pasting them together, use PetscConcat_() 823 if you don't want to expand them. 824 825 Example usage: 826 .vb 827 PetscConcat(hello,there) -> hellothere 828 829 #define HELLO hello 830 PetscConcat(HELLO,there) -> hellothere 831 PetscConcat_(HELLO,there) -> HELLOthere 832 .ve 833 834 Level: beginner 835 836 .seealso: `PetscStringize()`, `PetscExpand()` 837 M*/ 838 #define PetscConcat_(x, y) x##y 839 #define PetscConcat(x, y) PetscConcat_(x, y) 840 841 #define PETSC_INTERNAL_COMPL_0 1 842 #define PETSC_INTERNAL_COMPL_1 0 843 844 /*MC 845 PetscCompl - Expands to the integer complement of its argument 846 847 Synopsis: 848 #include <petscmacros.h> 849 int PetscCompl(b) 850 851 Input Parameter: 852 . b - Preprocessor variable, must expand to either integer literal 0 or 1 853 854 Output Parameter: 855 . <return-value> - Either integer literal 0 or 1 856 857 Notes: 858 Not available from Fortran. 859 860 Expands to integer literal 0 if b expands to 1, or integer literal 1 if b expands to 861 0. Behaviour is undefined if b expands to anything else. PetscCompl() will expand its 862 argument before returning the complement. 863 864 This macro can be useful for negating PetscDefined() inside macros e.g. 865 866 $ #define PETSC_DONT_HAVE_FOO PetscCompl(PetscDefined(HAVE_FOO)) 867 868 Example usage: 869 .vb 870 #define MY_VAR 1 871 PetscCompl(MY_VAR) -> 0 872 873 #undef MY_VAR 874 #define MY_VAR 0 875 PetscCompl(MY_VAR) -> 1 876 .ve 877 878 Level: beginner 879 880 .seealso: `PetscConcat()`, `PetscDefined()` 881 M*/ 882 #define PetscCompl(b) PetscConcat_(PETSC_INTERNAL_COMPL_, PetscExpand(b)) 883 884 /*MC 885 PetscDefined - Determine whether a boolean macro is defined 886 887 No Fortran Support 888 889 Synopsis: 890 #include <petscmacros.h> 891 int PetscDefined(def) 892 893 Input Parameter: 894 . def - PETSc-style preprocessor variable (without PETSC_ prepended!) 895 896 Output Parameter: 897 . <return-value> - Either integer literal 0 or 1 898 899 Notes: 900 `PetscDefined()` returns 1 if and only if "PETSC_ ## def" is defined (but empty) or defined to 901 integer literal 1. In all other cases, `PetscDefined()` returns integer literal 0. Therefore 902 this macro should not be used if its argument may be defined to a non-empty value other than 903 1. 904 905 The prefix "PETSC_" is automatically prepended to def. To avoid prepending "PETSC_", say to 906 add custom checks in user code, one should use `PetscDefined_()`. 907 908 $ #define FooDefined(d) PetscDefined_(PetscConcat(FOO_,d)) 909 910 Developer Notes: 911 Getting something that works in C and CPP for an arg that may or may not be defined is 912 tricky. Here, if we have "#define PETSC_HAVE_BOOGER 1" we match on the placeholder define, 913 insert the "0," for arg1 and generate the triplet (0, 1, 0). Then the last step cherry picks 914 the 2nd arg (a one). When PETSC_HAVE_BOOGER is not defined, we generate a (... 1, 0) pair, 915 and when the last step cherry picks the 2nd arg, we get a zero. 916 917 Our extra expansion via PetscDefined__take_second_expand() is needed with MSVC, which has a 918 nonconforming implementation of variadic macros. 919 920 Example Usage: 921 Suppose you would like to call either "foo()" or "bar()" depending on whether PETSC_USE_DEBUG 922 is defined then 923 924 .vb 925 #if PetscDefined(USE_DEBUG) 926 foo(); 927 #else 928 bar(); 929 #endif 930 931 // or alternatively within normal code 932 if (PetscDefined(USE_DEBUG)) { 933 foo(); 934 } else { 935 bar(); 936 } 937 .ve 938 939 is equivalent to 940 941 .vb 942 #if defined(PETSC_USE_DEBUG) 943 # if MY_DETECT_EMPTY_MACRO(PETSC_USE_DEBUG) // assuming you have such a macro 944 foo(); 945 # elif PETSC_USE_DEBUG == 1 946 foo(); 947 # else 948 bar(); 949 # endif 950 #else 951 bar(); 952 #endif 953 .ve 954 955 Level: intermediate 956 957 .seealso: `PetscHasAttribute()`, `PetscUnlikely()`, `PetscLikely()`, `PetscConcat()`, 958 `PetscExpandToNothing()`, `PetscCompl()` 959 M*/ 960 #define PetscDefined_arg_1 shift, 961 #define PetscDefined_arg_ shift, 962 #define PetscDefined__take_second_expanded(ignored, val, ...) val 963 #define PetscDefined__take_second_expand(args) PetscDefined__take_second_expanded args 964 #define PetscDefined__take_second(...) PetscDefined__take_second_expand((__VA_ARGS__)) 965 #define PetscDefined__(arg1_or_junk) PetscDefined__take_second(arg1_or_junk 1, 0, at_) 966 #define PetscDefined_(value) PetscDefined__(PetscConcat_(PetscDefined_arg_, value)) 967 #define PetscDefined(def) PetscDefined_(PetscConcat(PETSC_, def)) 968 969 /*MC 970 PetscUnlikelyDebug - Hints the compiler that the given condition is usually false, eliding 971 the check in optimized mode 972 973 No Fortran Support 974 975 Synopsis: 976 #include <petscmacros.h> 977 bool PetscUnlikelyDebug(bool cond) 978 979 Not Collective 980 981 Input Parameter: 982 . cond - Boolean expression 983 984 This returns the same truth value, it is only a hint to compilers that the result of cond is 985 likely to be false. When PETSc is compiled in optimized mode this will always return 986 false. Additionally, cond is guaranteed to not be evaluated when PETSc is compiled in 987 optimized mode. 988 989 Example usage: 990 This routine is shorthand for checking both the condition and whether PetscDefined(USE_DEBUG) 991 is true. So 992 993 .vb 994 if (PetscUnlikelyDebug(cond)) { 995 foo(); 996 } else { 997 bar(); 998 } 999 .ve 1000 1001 is equivalent to 1002 1003 .vb 1004 if (PetscDefined(USE_DEBUG)) { 1005 if (PetscUnlikely(cond)) { 1006 foo(); 1007 } else { 1008 bar(); 1009 } 1010 } else { 1011 bar(); 1012 } 1013 .ve 1014 1015 Level: advanced 1016 1017 .seealso: `PetscUnlikely()`, `PetscLikely()`, `PetscCall()`, `SETERRQ` 1018 M*/ 1019 #define PetscUnlikelyDebug(cond) (PetscDefined(USE_DEBUG) && PetscUnlikely(cond)) 1020 1021 #if defined(PETSC_CLANG_STATIC_ANALYZER) 1022 // silence compiler warnings when using -pedantic, this is only used by the linter and it cares 1023 // not what ISO C allows 1024 #define PetscMacroReturns_(retexpr, ...) \ 1025 __extension__({ \ 1026 __VA_ARGS__; \ 1027 retexpr; \ 1028 }) 1029 #else 1030 #define PetscMacroReturns_(retexpr, ...) \ 1031 retexpr; \ 1032 do { \ 1033 __VA_ARGS__; \ 1034 } while (0) 1035 #endif 1036 1037 /*MC 1038 PetscExpandToNothing - Expands to absolutely nothing at all 1039 1040 No Fortran Support 1041 1042 Synopsis: 1043 #include <petscmacros.h> 1044 void PetscExpandToNothing(...) 1045 1046 Input Parameter: 1047 . __VA_ARGS__ - Anything at all 1048 1049 Notes: 1050 Must have at least 1 parameter. 1051 1052 Example usage: 1053 .vb 1054 PetscExpandToNothing(a,b,c) -> *nothing* 1055 .ve 1056 1057 Level: beginner 1058 1059 .seealso: `PetscConcat()`, `PetscDefined()`, `PetscStringize()`, `PetscExpand()` 1060 M*/ 1061 #define PetscExpandToNothing(...) 1062 1063 /*MC 1064 PetscMacroReturns - Define a macro body that returns a value 1065 1066 Synopsis: 1067 #include <petscmacros.h> 1068 return_type PetscMacroReturns(return_type retexpr, ...) 1069 1070 Input Parameters: 1071 + retexpr - The value or expression that the macro should return 1072 - __VA_ARGS__ - The body of the macro 1073 1074 Notes: 1075 Due to limitations of the C-preprocessor retexpr cannot depend on symbols declared in the 1076 body of the macro and should not depend on values produced as a result of the expression. The 1077 user should not assume that the result of this macro is equivalent to a single logical source 1078 line. It is not portable to use macros defined using this one in conditional or loop bodies 1079 without enclosing them in curly braces\: 1080 1081 .vb 1082 #define FOO(arg1) PetscMacroReturns(0,arg1+=10) // returns 0 1083 1084 int err,x = 10; 1085 1086 if (...) err = FOO(x); // ERROR, body of FOO() executed outside the if statement 1087 if (...) { err = FOO(x); } // OK 1088 1089 for (...) err = FOO(x); // ERROR, body of FOO() executed outside the loop 1090 for (...) { err = FOO(x); } // OK 1091 .ve 1092 1093 It is also not portable to use this macro directly inside function call, conditional, loop, 1094 or switch statements\: 1095 1096 .vb 1097 extern void bar(int); 1098 1099 int ret = FOO(x); 1100 1101 bar(FOO(x)); // ERROR, may not compile 1102 bar(ret); // OK 1103 1104 if (FOO(x)) // ERROR, may not compile 1105 if (ret) // OK 1106 .ve 1107 1108 Example usage: 1109 .vb 1110 #define MY_SIMPLE_RETURNING_MACRO(arg1) PetscMacroReturns(0,arg1+=10) 1111 1112 int x = 10; 1113 int err = MY_SIMPLE_RETURNING_MACRO(x); // err = 0, x = 20 1114 1115 // multiline macros allowed, but must declare with line continuation as usual 1116 #define MY_COMPLEX_RETURNING_MACRO(arg1) PetscMacroReturns(0, \ 1117 if (arg1 > 10) { \ 1118 puts("big int!"); \ 1119 } else { \ 1120 return 7355608; \ 1121 } \ 1122 ) 1123 1124 // if retexpr contains commas, must enclose it with braces 1125 #define MY_COMPLEX_RETEXPR_MACRO_1() PetscMacroReturns(x+=10,0,body...) 1126 #define MY_COMPLEX_RETEXPR_MACRO_2() PetscMacroReturns((x+=10,0),body...) 1127 1128 int x = 10; 1129 int y = MY_COMPLEX_RETEXPR_MACRO_1(); // ERROR, y = x = 20 not 0 1130 int z = MY_COMPLEX_RETEXPR_MACRO_2(); // OK, y = 0, x = 20 1131 .ve 1132 1133 Level: intermediate 1134 1135 .seealso: `PetscExpand()`, `PetscConcat()`, `PetscStringize()` 1136 M*/ 1137 #define PetscMacroReturns(retexpr, ...) PetscMacroReturns_(retexpr, __VA_ARGS__) 1138 1139 #define PetscMacroReturnStandard(...) PetscMacroReturns(PETSC_SUCCESS, __VA_ARGS__) 1140 1141 /*MC 1142 PETSC_STATIC_ARRAY_LENGTH - Return the length of a static array 1143 1144 Level: intermediate 1145 M*/ 1146 #define PETSC_STATIC_ARRAY_LENGTH(a) (sizeof(a) / sizeof((a)[0])) 1147 1148 /* 1149 These macros allow extracting out the first argument or all but the first argument from a macro __VAR_ARGS__ INSIDE another macro. 1150 1151 Example usage: 1152 1153 #define mymacro(obj,...) { 1154 PETSC_FIRST_ARG((__VA_ARGS__,unused)); 1155 f(22 PETSC_REST_ARG(__VA_ARGS__)); 1156 } 1157 1158 Note you add a dummy extra argument to __VA_ARGS__ and enclose them in an extra set of () for PETSC_FIRST_ARG() and PETSC_REST_ARG(__VA_ARGS__) automatically adds a leading comma only if there are additional arguments 1159 1160 Reference: 1161 https://stackoverflow.com/questions/5588855/standard-alternative-to-gccs-va-args-trick 1162 */ 1163 #define PETSC_FIRST_ARG_(N, ...) N 1164 #define PETSC_FIRST_ARG(args) PETSC_FIRST_ARG_ args 1165 #define PETSC_SELECT_16TH(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, ...) a16 1166 #define PETSC_NUM(...) PETSC_SELECT_16TH(__VA_ARGS__, TWOORMORE, TWOORMORE, TWOORMORE, TWOORMORE, TWOORMORE, TWOORMORE, TWOORMORE, TWOORMORE, TWOORMORE, TWOORMORE, TWOORMORE, TWOORMORE, TWOORMORE, TWOORMORE, ONE, throwaway) 1167 #define PETSC_REST_HELPER_TWOORMORE(first, ...) , __VA_ARGS__ 1168 #define PETSC_REST_HELPER_ONE(first) 1169 #define PETSC_REST_HELPER2(qty, ...) PETSC_REST_HELPER_##qty(__VA_ARGS__) 1170 #define PETSC_REST_HELPER(qty, ...) PETSC_REST_HELPER2(qty, __VA_ARGS__) 1171 #define PETSC_REST_ARG(...) PETSC_REST_HELPER(PETSC_NUM(__VA_ARGS__), __VA_ARGS__) 1172 1173 #define PETSC_PRAGMA_DIAGNOSTIC_IGNORED_BEGIN_(name, ...) \ 1174 _Pragma(PetscStringize(name diagnostic push)) \ 1175 _Pragma(PetscStringize(name diagnostic ignored __VA_ARGS__)) 1176 1177 #define PETSC_PRAGMA_DIAGNOSTIC_IGNORED_END_(name) _Pragma(PetscStringize(name diagnostic pop)) 1178 1179 #if defined(__clang__) 1180 #define PETSC_PRAGMA_DIAGNOSTIC_IGNORED_BEGIN(...) PETSC_PRAGMA_DIAGNOSTIC_IGNORED_BEGIN_(clang, __VA_ARGS__) 1181 #define PETSC_PRAGMA_DIAGNOSTIC_IGNORED_END() PETSC_PRAGMA_DIAGNOSTIC_IGNORED_END_(clang) 1182 #elif defined(__GNUC__) || defined(__GNUG__) 1183 #define PETSC_PRAGMA_DIAGNOSTIC_IGNORED_BEGIN(...) PETSC_PRAGMA_DIAGNOSTIC_IGNORED_BEGIN_(GCC, __VA_ARGS__) 1184 #define PETSC_PRAGMA_DIAGNOSTIC_IGNORED_END() PETSC_PRAGMA_DIAGNOSTIC_IGNORED_END_(GCC) 1185 #endif 1186 1187 #ifndef PETSC_PRAGMA_DIAGNOSTIC_IGNORED_BEGIN 1188 #define PETSC_PRAGMA_DIAGNOSTIC_IGNORED_BEGIN(...) 1189 #define PETSC_PRAGMA_DIAGNOSTIC_IGNORED_END(...) 1190 // only undefine these if they are not used 1191 #undef PETSC_PRAGMA_DIAGNOSTIC_IGNORED_BEGIN_ 1192 #undef PETSC_PRAGMA_DIAGNOSTIC_IGNORED_END_ 1193 #endif 1194 1195 /* OpenMP support */ 1196 #if defined(_OPENMP) 1197 #if defined(_MSC_VER) 1198 #define PetscPragmaOMP(...) __pragma(__VA_ARGS__) 1199 #else 1200 #define PetscPragmaOMP(...) _Pragma(PetscStringize(omp __VA_ARGS__)) 1201 #endif 1202 #endif 1203 1204 #ifndef PetscPragmaOMP 1205 #define PetscPragmaOMP(...) 1206 #endif 1207 1208 /* PetscPragmaSIMD - from CeedPragmaSIMD */ 1209 #if defined(__NEC__) 1210 #define PetscPragmaSIMD _Pragma("_NEC ivdep") 1211 #elif defined(__INTEL_COMPILER) && !defined(_WIN32) 1212 #define PetscPragmaSIMD _Pragma("vector") 1213 #elif defined(__GNUC__) 1214 #if __GNUC__ >= 5 && !defined(__PGI) 1215 #define PetscPragmaSIMD _Pragma("GCC ivdep") 1216 #endif 1217 #elif defined(_OPENMP) && _OPENMP >= 201307 1218 #define PetscPragmaSIMD PetscPragmaOMP(simd) 1219 #elif defined(PETSC_HAVE_CRAY_VECTOR) 1220 #define PetscPragmaSIMD _Pragma("_CRI ivdep") 1221 #endif 1222 1223 #ifndef PetscPragmaSIMD 1224 #define PetscPragmaSIMD 1225 #endif 1226 1227 #endif /* PETSC_PREPROCESSOR_MACROS_H */ 1228