|
3 | 3 |
|
4 | 4 | #pragma once
|
5 | 5 |
|
6 |
| -#if !defined(OPENTELEMETRY_LIKELY_IF) && defined(__cplusplus) |
| 6 | +/* |
| 7 | + Expected usage pattern: |
| 8 | +
|
| 9 | + if OPENTELEMETRY_LIKELY_CONDITION (ptr != nullptr) |
| 10 | + { |
| 11 | + do_something_likely(); |
| 12 | + } else { |
| 13 | + do_something_unlikely(); |
| 14 | + } |
| 15 | +
|
| 16 | + This pattern works with gcc/clang and __builtin_expect(), |
| 17 | + as well as with C++20. |
| 18 | + It is unclear if __builtin_expect() will be deprecated |
| 19 | + in favor of C++20 [[likely]] or not. |
| 20 | +
|
| 21 | + OPENTELEMETRY_LIKELY_CONDITION is preferred over OPENTELEMETRY_LIKELY, |
| 22 | + to be revisited when C++20 is required. |
| 23 | +*/ |
| 24 | + |
| 25 | +#if !defined(OPENTELEMETRY_LIKELY_CONDITION) && defined(__cplusplus) |
| 26 | +// Only use likely with C++20 |
| 27 | +# if __cplusplus >= 202002L |
7 | 28 | // GCC 9 has likely attribute but do not support declare it at the beginning of statement
|
8 |
| -# if defined(__has_cpp_attribute) && (defined(__clang__) || !defined(__GNUC__) || __GNUC__ > 9) |
9 |
| -# if __has_cpp_attribute(likely) |
10 |
| -# define OPENTELEMETRY_LIKELY_IF(...) \ |
11 |
| - if (__VA_ARGS__) \ |
12 |
| - [[likely]] |
| 29 | +# if defined(__has_cpp_attribute) && (defined(__clang__) || !defined(__GNUC__) || __GNUC__ > 9) |
| 30 | +# if __has_cpp_attribute(likely) |
| 31 | +# define OPENTELEMETRY_LIKELY_CONDITION(C) (C) [[likely]] |
| 32 | +# endif |
| 33 | +# endif |
| 34 | +# endif |
| 35 | +#endif |
| 36 | +#if !defined(OPENTELEMETRY_LIKELY_CONDITION) && (defined(__clang__) || defined(__GNUC__)) |
| 37 | +// Only use if supported by the compiler |
| 38 | +# define OPENTELEMETRY_LIKELY_CONDITION(C) (__builtin_expect(!!(C), true)) |
| 39 | +#endif |
| 40 | +#ifndef OPENTELEMETRY_LIKELY_CONDITION |
| 41 | +// Do not use likely annotations |
| 42 | +# define OPENTELEMETRY_LIKELY_CONDITION(C) (C) |
| 43 | +#endif |
13 | 44 |
|
| 45 | +#if !defined(OPENTELEMETRY_UNLIKELY_CONDITION) && defined(__cplusplus) |
| 46 | +// Only use unlikely with C++20 |
| 47 | +# if __cplusplus >= 202002L |
| 48 | +// GCC 9 has unlikely attribute but do not support declare it at the beginning of statement |
| 49 | +# if defined(__has_cpp_attribute) && (defined(__clang__) || !defined(__GNUC__) || __GNUC__ > 9) |
| 50 | +# if __has_cpp_attribute(unlikely) |
| 51 | +# define OPENTELEMETRY_UNLIKELY_CONDITION(C) (C) [[unlikely]] |
| 52 | +# endif |
14 | 53 | # endif
|
15 | 54 | # endif
|
16 | 55 | #endif
|
17 |
| -#if !defined(OPENTELEMETRY_LIKELY_IF) && (defined(__clang__) || defined(__GNUC__)) |
18 |
| -# define OPENTELEMETRY_LIKELY_IF(...) if (__builtin_expect(!!(__VA_ARGS__), true)) |
| 56 | +#if !defined(OPENTELEMETRY_UNLIKELY_CONDITION) && (defined(__clang__) || defined(__GNUC__)) |
| 57 | +// Only use if supported by the compiler |
| 58 | +# define OPENTELEMETRY_UNLIKELY_CONDITION(C) (__builtin_expect(!!(C), false)) |
19 | 59 | #endif
|
20 |
| -#ifndef OPENTELEMETRY_LIKELY_IF |
21 |
| -# define OPENTELEMETRY_LIKELY_IF(...) if (__VA_ARGS__) |
| 60 | +#ifndef OPENTELEMETRY_UNLIKELY_CONDITION |
| 61 | +// Do not use unlikely annotations |
| 62 | +# define OPENTELEMETRY_UNLIKELY_CONDITION(C) (C) |
| 63 | +#endif |
| 64 | + |
| 65 | +/* |
| 66 | + Expected usage pattern: |
| 67 | +
|
| 68 | + if (ptr != nullptr) |
| 69 | + OPENTELEMETRY_LIKELY |
| 70 | + { |
| 71 | + do_something_likely(); |
| 72 | + } else { |
| 73 | + do_something_unlikely(); |
| 74 | + } |
| 75 | +
|
| 76 | + This pattern works starting with C++20. |
| 77 | + See https://en.cppreference.com/w/cpp/language/attributes/likely |
| 78 | +
|
| 79 | + Please use OPENTELEMETRY_LIKELY_CONDITION instead for now. |
| 80 | +*/ |
| 81 | + |
| 82 | +#if !defined(OPENTELEMETRY_LIKELY) && defined(__cplusplus) |
| 83 | +// Only use likely with C++20 |
| 84 | +# if __cplusplus >= 202002L |
| 85 | +// GCC 9 has likely attribute but do not support declare it at the beginning of statement |
| 86 | +# if defined(__has_cpp_attribute) && (defined(__clang__) || !defined(__GNUC__) || __GNUC__ > 9) |
| 87 | +# if __has_cpp_attribute(likely) |
| 88 | +# define OPENTELEMETRY_LIKELY [[likely]] |
| 89 | +# endif |
| 90 | +# endif |
| 91 | +# endif |
| 92 | +#endif |
| 93 | + |
| 94 | +#ifndef OPENTELEMETRY_LIKELY |
| 95 | +# define OPENTELEMETRY_LIKELY |
| 96 | +#endif |
| 97 | + |
| 98 | +#if !defined(OPENTELEMETRY_UNLIKELY) && defined(__cplusplus) |
| 99 | +// Only use unlikely with C++20 |
| 100 | +# if __cplusplus >= 202002L |
| 101 | +// GCC 9 has unlikely attribute but do not support declare it at the beginning of statement |
| 102 | +# if defined(__has_cpp_attribute) && (defined(__clang__) || !defined(__GNUC__) || __GNUC__ > 9) |
| 103 | +# if __has_cpp_attribute(unlikely) |
| 104 | +# define OPENTELEMETRY_UNLIKELY [[unlikely]] |
| 105 | +# endif |
| 106 | +# endif |
| 107 | +# endif |
| 108 | +#endif |
| 109 | + |
| 110 | +#ifndef OPENTELEMETRY_UNLIKELY |
| 111 | +# define OPENTELEMETRY_UNLIKELY |
22 | 112 | #endif
|
23 | 113 |
|
24 | 114 | /// \brief Declare variable as maybe unused
|
|
0 commit comments