xref: /aosp_15_r20/external/abseil-cpp/absl/base/optimization.h (revision 9356374a3709195abf420251b3e825997ff56c0f)
1*9356374aSAndroid Build Coastguard Worker //
2*9356374aSAndroid Build Coastguard Worker // Copyright 2017 The Abseil Authors.
3*9356374aSAndroid Build Coastguard Worker //
4*9356374aSAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License");
5*9356374aSAndroid Build Coastguard Worker // you may not use this file except in compliance with the License.
6*9356374aSAndroid Build Coastguard Worker // You may obtain a copy of the License at
7*9356374aSAndroid Build Coastguard Worker //
8*9356374aSAndroid Build Coastguard Worker //      https://www.apache.org/licenses/LICENSE-2.0
9*9356374aSAndroid Build Coastguard Worker //
10*9356374aSAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
11*9356374aSAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS,
12*9356374aSAndroid Build Coastguard Worker // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*9356374aSAndroid Build Coastguard Worker // See the License for the specific language governing permissions and
14*9356374aSAndroid Build Coastguard Worker // limitations under the License.
15*9356374aSAndroid Build Coastguard Worker //
16*9356374aSAndroid Build Coastguard Worker // -----------------------------------------------------------------------------
17*9356374aSAndroid Build Coastguard Worker // File: optimization.h
18*9356374aSAndroid Build Coastguard Worker // -----------------------------------------------------------------------------
19*9356374aSAndroid Build Coastguard Worker //
20*9356374aSAndroid Build Coastguard Worker // This header file defines portable macros for performance optimization.
21*9356374aSAndroid Build Coastguard Worker //
22*9356374aSAndroid Build Coastguard Worker // This header is included in both C++ code and legacy C code and thus must
23*9356374aSAndroid Build Coastguard Worker // remain compatible with both C and C++. C compatibility will be removed if
24*9356374aSAndroid Build Coastguard Worker // the legacy code is removed or converted to C++. Do not include this header in
25*9356374aSAndroid Build Coastguard Worker // new code that requires C compatibility or assume C compatibility will remain
26*9356374aSAndroid Build Coastguard Worker // indefinitely.
27*9356374aSAndroid Build Coastguard Worker 
28*9356374aSAndroid Build Coastguard Worker #ifndef ABSL_BASE_OPTIMIZATION_H_
29*9356374aSAndroid Build Coastguard Worker #define ABSL_BASE_OPTIMIZATION_H_
30*9356374aSAndroid Build Coastguard Worker 
31*9356374aSAndroid Build Coastguard Worker #include <assert.h>
32*9356374aSAndroid Build Coastguard Worker 
33*9356374aSAndroid Build Coastguard Worker #ifdef __cplusplus
34*9356374aSAndroid Build Coastguard Worker // Included for std::unreachable()
35*9356374aSAndroid Build Coastguard Worker #include <utility>
36*9356374aSAndroid Build Coastguard Worker #endif  // __cplusplus
37*9356374aSAndroid Build Coastguard Worker 
38*9356374aSAndroid Build Coastguard Worker #include "absl/base/config.h"
39*9356374aSAndroid Build Coastguard Worker #include "absl/base/options.h"
40*9356374aSAndroid Build Coastguard Worker 
41*9356374aSAndroid Build Coastguard Worker // ABSL_BLOCK_TAIL_CALL_OPTIMIZATION
42*9356374aSAndroid Build Coastguard Worker //
43*9356374aSAndroid Build Coastguard Worker // Instructs the compiler to avoid optimizing tail-call recursion. This macro is
44*9356374aSAndroid Build Coastguard Worker // useful when you wish to preserve the existing function order within a stack
45*9356374aSAndroid Build Coastguard Worker // trace for logging, debugging, or profiling purposes.
46*9356374aSAndroid Build Coastguard Worker //
47*9356374aSAndroid Build Coastguard Worker // Example:
48*9356374aSAndroid Build Coastguard Worker //
49*9356374aSAndroid Build Coastguard Worker //   int f() {
50*9356374aSAndroid Build Coastguard Worker //     int result = g();
51*9356374aSAndroid Build Coastguard Worker //     ABSL_BLOCK_TAIL_CALL_OPTIMIZATION();
52*9356374aSAndroid Build Coastguard Worker //     return result;
53*9356374aSAndroid Build Coastguard Worker //   }
54*9356374aSAndroid Build Coastguard Worker #if defined(__pnacl__)
55*9356374aSAndroid Build Coastguard Worker #define ABSL_BLOCK_TAIL_CALL_OPTIMIZATION() if (volatile int x = 0) { (void)x; }
56*9356374aSAndroid Build Coastguard Worker #elif defined(__clang__)
57*9356374aSAndroid Build Coastguard Worker // Clang will not tail call given inline volatile assembly.
58*9356374aSAndroid Build Coastguard Worker #define ABSL_BLOCK_TAIL_CALL_OPTIMIZATION() __asm__ __volatile__("")
59*9356374aSAndroid Build Coastguard Worker #elif defined(__GNUC__)
60*9356374aSAndroid Build Coastguard Worker // GCC will not tail call given inline volatile assembly.
61*9356374aSAndroid Build Coastguard Worker #define ABSL_BLOCK_TAIL_CALL_OPTIMIZATION() __asm__ __volatile__("")
62*9356374aSAndroid Build Coastguard Worker #elif defined(_MSC_VER)
63*9356374aSAndroid Build Coastguard Worker #include <intrin.h>
64*9356374aSAndroid Build Coastguard Worker // The __nop() intrinsic blocks the optimisation.
65*9356374aSAndroid Build Coastguard Worker #define ABSL_BLOCK_TAIL_CALL_OPTIMIZATION() __nop()
66*9356374aSAndroid Build Coastguard Worker #else
67*9356374aSAndroid Build Coastguard Worker #define ABSL_BLOCK_TAIL_CALL_OPTIMIZATION() if (volatile int x = 0) { (void)x; }
68*9356374aSAndroid Build Coastguard Worker #endif
69*9356374aSAndroid Build Coastguard Worker 
70*9356374aSAndroid Build Coastguard Worker // ABSL_CACHELINE_SIZE
71*9356374aSAndroid Build Coastguard Worker //
72*9356374aSAndroid Build Coastguard Worker // Explicitly defines the size of the L1 cache for purposes of alignment.
73*9356374aSAndroid Build Coastguard Worker // Setting the cacheline size allows you to specify that certain objects be
74*9356374aSAndroid Build Coastguard Worker // aligned on a cacheline boundary with `ABSL_CACHELINE_ALIGNED` declarations.
75*9356374aSAndroid Build Coastguard Worker // (See below.)
76*9356374aSAndroid Build Coastguard Worker //
77*9356374aSAndroid Build Coastguard Worker // NOTE: this macro should be replaced with the following C++17 features, when
78*9356374aSAndroid Build Coastguard Worker // those are generally available:
79*9356374aSAndroid Build Coastguard Worker //
80*9356374aSAndroid Build Coastguard Worker //   * `std::hardware_constructive_interference_size`
81*9356374aSAndroid Build Coastguard Worker //   * `std::hardware_destructive_interference_size`
82*9356374aSAndroid Build Coastguard Worker //
83*9356374aSAndroid Build Coastguard Worker // See http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0154r1.html
84*9356374aSAndroid Build Coastguard Worker // for more information.
85*9356374aSAndroid Build Coastguard Worker #if defined(__GNUC__)
86*9356374aSAndroid Build Coastguard Worker // Cache line alignment
87*9356374aSAndroid Build Coastguard Worker #if defined(__i386__) || defined(__x86_64__)
88*9356374aSAndroid Build Coastguard Worker #define ABSL_CACHELINE_SIZE 64
89*9356374aSAndroid Build Coastguard Worker #elif defined(__powerpc64__)
90*9356374aSAndroid Build Coastguard Worker #define ABSL_CACHELINE_SIZE 128
91*9356374aSAndroid Build Coastguard Worker #elif defined(__aarch64__)
92*9356374aSAndroid Build Coastguard Worker // We would need to read special register ctr_el0 to find out L1 dcache size.
93*9356374aSAndroid Build Coastguard Worker // This value is a good estimate based on a real aarch64 machine.
94*9356374aSAndroid Build Coastguard Worker #define ABSL_CACHELINE_SIZE 64
95*9356374aSAndroid Build Coastguard Worker #elif defined(__arm__)
96*9356374aSAndroid Build Coastguard Worker // Cache line sizes for ARM: These values are not strictly correct since
97*9356374aSAndroid Build Coastguard Worker // cache line sizes depend on implementations, not architectures.  There
98*9356374aSAndroid Build Coastguard Worker // are even implementations with cache line sizes configurable at boot
99*9356374aSAndroid Build Coastguard Worker // time.
100*9356374aSAndroid Build Coastguard Worker #if defined(__ARM_ARCH_5T__)
101*9356374aSAndroid Build Coastguard Worker #define ABSL_CACHELINE_SIZE 32
102*9356374aSAndroid Build Coastguard Worker #elif defined(__ARM_ARCH_7A__)
103*9356374aSAndroid Build Coastguard Worker #define ABSL_CACHELINE_SIZE 64
104*9356374aSAndroid Build Coastguard Worker #endif
105*9356374aSAndroid Build Coastguard Worker #endif
106*9356374aSAndroid Build Coastguard Worker #endif
107*9356374aSAndroid Build Coastguard Worker 
108*9356374aSAndroid Build Coastguard Worker #ifndef ABSL_CACHELINE_SIZE
109*9356374aSAndroid Build Coastguard Worker // A reasonable default guess.  Note that overestimates tend to waste more
110*9356374aSAndroid Build Coastguard Worker // space, while underestimates tend to waste more time.
111*9356374aSAndroid Build Coastguard Worker #define ABSL_CACHELINE_SIZE 64
112*9356374aSAndroid Build Coastguard Worker #endif
113*9356374aSAndroid Build Coastguard Worker 
114*9356374aSAndroid Build Coastguard Worker // ABSL_CACHELINE_ALIGNED
115*9356374aSAndroid Build Coastguard Worker //
116*9356374aSAndroid Build Coastguard Worker // Indicates that the declared object be cache aligned using
117*9356374aSAndroid Build Coastguard Worker // `ABSL_CACHELINE_SIZE` (see above). Cacheline aligning objects allows you to
118*9356374aSAndroid Build Coastguard Worker // load a set of related objects in the L1 cache for performance improvements.
119*9356374aSAndroid Build Coastguard Worker // Cacheline aligning objects properly allows constructive memory sharing and
120*9356374aSAndroid Build Coastguard Worker // prevents destructive (or "false") memory sharing.
121*9356374aSAndroid Build Coastguard Worker //
122*9356374aSAndroid Build Coastguard Worker // NOTE: callers should replace uses of this macro with `alignas()` using
123*9356374aSAndroid Build Coastguard Worker // `std::hardware_constructive_interference_size` and/or
124*9356374aSAndroid Build Coastguard Worker // `std::hardware_destructive_interference_size` when C++17 becomes available to
125*9356374aSAndroid Build Coastguard Worker // them.
126*9356374aSAndroid Build Coastguard Worker //
127*9356374aSAndroid Build Coastguard Worker // See http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0154r1.html
128*9356374aSAndroid Build Coastguard Worker // for more information.
129*9356374aSAndroid Build Coastguard Worker //
130*9356374aSAndroid Build Coastguard Worker // On some compilers, `ABSL_CACHELINE_ALIGNED` expands to an `__attribute__`
131*9356374aSAndroid Build Coastguard Worker // or `__declspec` attribute. For compilers where this is not known to work,
132*9356374aSAndroid Build Coastguard Worker // the macro expands to nothing.
133*9356374aSAndroid Build Coastguard Worker //
134*9356374aSAndroid Build Coastguard Worker // No further guarantees are made here. The result of applying the macro
135*9356374aSAndroid Build Coastguard Worker // to variables and types is always implementation-defined.
136*9356374aSAndroid Build Coastguard Worker //
137*9356374aSAndroid Build Coastguard Worker // WARNING: It is easy to use this attribute incorrectly, even to the point
138*9356374aSAndroid Build Coastguard Worker // of causing bugs that are difficult to diagnose, crash, etc. It does not
139*9356374aSAndroid Build Coastguard Worker // of itself guarantee that objects are aligned to a cache line.
140*9356374aSAndroid Build Coastguard Worker //
141*9356374aSAndroid Build Coastguard Worker // NOTE: Some compilers are picky about the locations of annotations such as
142*9356374aSAndroid Build Coastguard Worker // this attribute, so prefer to put it at the beginning of your declaration.
143*9356374aSAndroid Build Coastguard Worker // For example,
144*9356374aSAndroid Build Coastguard Worker //
145*9356374aSAndroid Build Coastguard Worker //   ABSL_CACHELINE_ALIGNED static Foo* foo = ...
146*9356374aSAndroid Build Coastguard Worker //
147*9356374aSAndroid Build Coastguard Worker //   class ABSL_CACHELINE_ALIGNED Bar { ...
148*9356374aSAndroid Build Coastguard Worker //
149*9356374aSAndroid Build Coastguard Worker // Recommendations:
150*9356374aSAndroid Build Coastguard Worker //
151*9356374aSAndroid Build Coastguard Worker // 1) Consult compiler documentation; this comment is not kept in sync as
152*9356374aSAndroid Build Coastguard Worker //    toolchains evolve.
153*9356374aSAndroid Build Coastguard Worker // 2) Verify your use has the intended effect. This often requires inspecting
154*9356374aSAndroid Build Coastguard Worker //    the generated machine code.
155*9356374aSAndroid Build Coastguard Worker // 3) Prefer applying this attribute to individual variables. Avoid
156*9356374aSAndroid Build Coastguard Worker //    applying it to types. This tends to localize the effect.
157*9356374aSAndroid Build Coastguard Worker #if defined(__clang__) || defined(__GNUC__)
158*9356374aSAndroid Build Coastguard Worker #define ABSL_CACHELINE_ALIGNED __attribute__((aligned(ABSL_CACHELINE_SIZE)))
159*9356374aSAndroid Build Coastguard Worker #elif defined(_MSC_VER)
160*9356374aSAndroid Build Coastguard Worker #define ABSL_CACHELINE_ALIGNED __declspec(align(ABSL_CACHELINE_SIZE))
161*9356374aSAndroid Build Coastguard Worker #else
162*9356374aSAndroid Build Coastguard Worker #define ABSL_CACHELINE_ALIGNED
163*9356374aSAndroid Build Coastguard Worker #endif
164*9356374aSAndroid Build Coastguard Worker 
165*9356374aSAndroid Build Coastguard Worker // ABSL_PREDICT_TRUE, ABSL_PREDICT_FALSE
166*9356374aSAndroid Build Coastguard Worker //
167*9356374aSAndroid Build Coastguard Worker // Enables the compiler to prioritize compilation using static analysis for
168*9356374aSAndroid Build Coastguard Worker // likely paths within a boolean branch.
169*9356374aSAndroid Build Coastguard Worker //
170*9356374aSAndroid Build Coastguard Worker // Example:
171*9356374aSAndroid Build Coastguard Worker //
172*9356374aSAndroid Build Coastguard Worker //   if (ABSL_PREDICT_TRUE(expression)) {
173*9356374aSAndroid Build Coastguard Worker //     return result;                        // Faster if more likely
174*9356374aSAndroid Build Coastguard Worker //   } else {
175*9356374aSAndroid Build Coastguard Worker //     return 0;
176*9356374aSAndroid Build Coastguard Worker //   }
177*9356374aSAndroid Build Coastguard Worker //
178*9356374aSAndroid Build Coastguard Worker // Compilers can use the information that a certain branch is not likely to be
179*9356374aSAndroid Build Coastguard Worker // taken (for instance, a CHECK failure) to optimize for the common case in
180*9356374aSAndroid Build Coastguard Worker // the absence of better information (ie. compiling gcc with `-fprofile-arcs`).
181*9356374aSAndroid Build Coastguard Worker //
182*9356374aSAndroid Build Coastguard Worker // Recommendation: Modern CPUs dynamically predict branch execution paths,
183*9356374aSAndroid Build Coastguard Worker // typically with accuracy greater than 97%. As a result, annotating every
184*9356374aSAndroid Build Coastguard Worker // branch in a codebase is likely counterproductive; however, annotating
185*9356374aSAndroid Build Coastguard Worker // specific branches that are both hot and consistently mispredicted is likely
186*9356374aSAndroid Build Coastguard Worker // to yield performance improvements.
187*9356374aSAndroid Build Coastguard Worker #if ABSL_HAVE_BUILTIN(__builtin_expect) || \
188*9356374aSAndroid Build Coastguard Worker     (defined(__GNUC__) && !defined(__clang__))
189*9356374aSAndroid Build Coastguard Worker #define ABSL_PREDICT_FALSE(x) (__builtin_expect(false || (x), false))
190*9356374aSAndroid Build Coastguard Worker #define ABSL_PREDICT_TRUE(x) (__builtin_expect(false || (x), true))
191*9356374aSAndroid Build Coastguard Worker #else
192*9356374aSAndroid Build Coastguard Worker #define ABSL_PREDICT_FALSE(x) (x)
193*9356374aSAndroid Build Coastguard Worker #define ABSL_PREDICT_TRUE(x) (x)
194*9356374aSAndroid Build Coastguard Worker #endif
195*9356374aSAndroid Build Coastguard Worker 
196*9356374aSAndroid Build Coastguard Worker // `ABSL_INTERNAL_IMMEDIATE_ABORT_IMPL()` aborts the program in the fastest
197*9356374aSAndroid Build Coastguard Worker // possible way, with no attempt at logging. One use is to implement hardening
198*9356374aSAndroid Build Coastguard Worker // aborts with ABSL_OPTION_HARDENED.  Since this is an internal symbol, it
199*9356374aSAndroid Build Coastguard Worker // should not be used directly outside of Abseil.
200*9356374aSAndroid Build Coastguard Worker #if ABSL_HAVE_BUILTIN(__builtin_trap) || \
201*9356374aSAndroid Build Coastguard Worker     (defined(__GNUC__) && !defined(__clang__))
202*9356374aSAndroid Build Coastguard Worker #define ABSL_INTERNAL_IMMEDIATE_ABORT_IMPL() __builtin_trap()
203*9356374aSAndroid Build Coastguard Worker #else
204*9356374aSAndroid Build Coastguard Worker #define ABSL_INTERNAL_IMMEDIATE_ABORT_IMPL() abort()
205*9356374aSAndroid Build Coastguard Worker #endif
206*9356374aSAndroid Build Coastguard Worker 
207*9356374aSAndroid Build Coastguard Worker // `ABSL_INTERNAL_UNREACHABLE_IMPL()` is the platform specific directive to
208*9356374aSAndroid Build Coastguard Worker // indicate that a statement is unreachable, and to allow the compiler to
209*9356374aSAndroid Build Coastguard Worker // optimize accordingly. Clients should use `ABSL_UNREACHABLE()`, which is
210*9356374aSAndroid Build Coastguard Worker // defined below.
211*9356374aSAndroid Build Coastguard Worker #if defined(__cpp_lib_unreachable) && __cpp_lib_unreachable >= 202202L
212*9356374aSAndroid Build Coastguard Worker #define ABSL_INTERNAL_UNREACHABLE_IMPL() std::unreachable()
213*9356374aSAndroid Build Coastguard Worker #elif defined(__GNUC__) || ABSL_HAVE_BUILTIN(__builtin_unreachable)
214*9356374aSAndroid Build Coastguard Worker #define ABSL_INTERNAL_UNREACHABLE_IMPL() __builtin_unreachable()
215*9356374aSAndroid Build Coastguard Worker #elif ABSL_HAVE_BUILTIN(__builtin_assume)
216*9356374aSAndroid Build Coastguard Worker #define ABSL_INTERNAL_UNREACHABLE_IMPL() __builtin_assume(false)
217*9356374aSAndroid Build Coastguard Worker #elif defined(_MSC_VER)
218*9356374aSAndroid Build Coastguard Worker #define ABSL_INTERNAL_UNREACHABLE_IMPL() __assume(false)
219*9356374aSAndroid Build Coastguard Worker #else
220*9356374aSAndroid Build Coastguard Worker #define ABSL_INTERNAL_UNREACHABLE_IMPL()
221*9356374aSAndroid Build Coastguard Worker #endif
222*9356374aSAndroid Build Coastguard Worker 
223*9356374aSAndroid Build Coastguard Worker // `ABSL_UNREACHABLE()` is an unreachable statement.  A program which reaches
224*9356374aSAndroid Build Coastguard Worker // one has undefined behavior, and the compiler may optimize accordingly.
225*9356374aSAndroid Build Coastguard Worker #if ABSL_OPTION_HARDENED == 1 && defined(NDEBUG)
226*9356374aSAndroid Build Coastguard Worker // Abort in hardened mode to avoid dangerous undefined behavior.
227*9356374aSAndroid Build Coastguard Worker #define ABSL_UNREACHABLE()                \
228*9356374aSAndroid Build Coastguard Worker   do {                                    \
229*9356374aSAndroid Build Coastguard Worker     ABSL_INTERNAL_IMMEDIATE_ABORT_IMPL(); \
230*9356374aSAndroid Build Coastguard Worker     ABSL_INTERNAL_UNREACHABLE_IMPL();     \
231*9356374aSAndroid Build Coastguard Worker   } while (false)
232*9356374aSAndroid Build Coastguard Worker #else
233*9356374aSAndroid Build Coastguard Worker // The assert only fires in debug mode to aid in debugging.
234*9356374aSAndroid Build Coastguard Worker // When NDEBUG is defined, reaching ABSL_UNREACHABLE() is undefined behavior.
235*9356374aSAndroid Build Coastguard Worker #define ABSL_UNREACHABLE()                       \
236*9356374aSAndroid Build Coastguard Worker   do {                                           \
237*9356374aSAndroid Build Coastguard Worker     /* NOLINTNEXTLINE: misc-static-assert */     \
238*9356374aSAndroid Build Coastguard Worker     assert(false && "ABSL_UNREACHABLE reached"); \
239*9356374aSAndroid Build Coastguard Worker     ABSL_INTERNAL_UNREACHABLE_IMPL();            \
240*9356374aSAndroid Build Coastguard Worker   } while (false)
241*9356374aSAndroid Build Coastguard Worker #endif
242*9356374aSAndroid Build Coastguard Worker 
243*9356374aSAndroid Build Coastguard Worker // ABSL_ASSUME(cond)
244*9356374aSAndroid Build Coastguard Worker //
245*9356374aSAndroid Build Coastguard Worker // Informs the compiler that a condition is always true and that it can assume
246*9356374aSAndroid Build Coastguard Worker // it to be true for optimization purposes.
247*9356374aSAndroid Build Coastguard Worker //
248*9356374aSAndroid Build Coastguard Worker // WARNING: If the condition is false, the program can produce undefined and
249*9356374aSAndroid Build Coastguard Worker // potentially dangerous behavior.
250*9356374aSAndroid Build Coastguard Worker //
251*9356374aSAndroid Build Coastguard Worker // In !NDEBUG mode, the condition is checked with an assert().
252*9356374aSAndroid Build Coastguard Worker //
253*9356374aSAndroid Build Coastguard Worker // NOTE: The expression must not have side effects, as it may only be evaluated
254*9356374aSAndroid Build Coastguard Worker // in some compilation modes and not others. Some compilers may issue a warning
255*9356374aSAndroid Build Coastguard Worker // if the compiler cannot prove the expression has no side effects. For example,
256*9356374aSAndroid Build Coastguard Worker // the expression should not use a function call since the compiler cannot prove
257*9356374aSAndroid Build Coastguard Worker // that a function call does not have side effects.
258*9356374aSAndroid Build Coastguard Worker //
259*9356374aSAndroid Build Coastguard Worker // Example:
260*9356374aSAndroid Build Coastguard Worker //
261*9356374aSAndroid Build Coastguard Worker //   int x = ...;
262*9356374aSAndroid Build Coastguard Worker //   ABSL_ASSUME(x >= 0);
263*9356374aSAndroid Build Coastguard Worker //   // The compiler can optimize the division to a simple right shift using the
264*9356374aSAndroid Build Coastguard Worker //   // assumption specified above.
265*9356374aSAndroid Build Coastguard Worker //   int y = x / 16;
266*9356374aSAndroid Build Coastguard Worker //
267*9356374aSAndroid Build Coastguard Worker #if !defined(NDEBUG)
268*9356374aSAndroid Build Coastguard Worker #define ABSL_ASSUME(cond) assert(cond)
269*9356374aSAndroid Build Coastguard Worker #elif ABSL_HAVE_BUILTIN(__builtin_assume)
270*9356374aSAndroid Build Coastguard Worker #define ABSL_ASSUME(cond) __builtin_assume(cond)
271*9356374aSAndroid Build Coastguard Worker #elif defined(_MSC_VER)
272*9356374aSAndroid Build Coastguard Worker #define ABSL_ASSUME(cond) __assume(cond)
273*9356374aSAndroid Build Coastguard Worker #elif defined(__cpp_lib_unreachable) && __cpp_lib_unreachable >= 202202L
274*9356374aSAndroid Build Coastguard Worker #define ABSL_ASSUME(cond)            \
275*9356374aSAndroid Build Coastguard Worker   do {                               \
276*9356374aSAndroid Build Coastguard Worker     if (!(cond)) std::unreachable(); \
277*9356374aSAndroid Build Coastguard Worker   } while (false)
278*9356374aSAndroid Build Coastguard Worker #elif defined(__GNUC__) || ABSL_HAVE_BUILTIN(__builtin_unreachable)
279*9356374aSAndroid Build Coastguard Worker #define ABSL_ASSUME(cond)                 \
280*9356374aSAndroid Build Coastguard Worker   do {                                    \
281*9356374aSAndroid Build Coastguard Worker     if (!(cond)) __builtin_unreachable(); \
282*9356374aSAndroid Build Coastguard Worker   } while (false)
283*9356374aSAndroid Build Coastguard Worker #else
284*9356374aSAndroid Build Coastguard Worker #define ABSL_ASSUME(cond)               \
285*9356374aSAndroid Build Coastguard Worker   do {                                  \
286*9356374aSAndroid Build Coastguard Worker     static_cast<void>(false && (cond)); \
287*9356374aSAndroid Build Coastguard Worker   } while (false)
288*9356374aSAndroid Build Coastguard Worker #endif
289*9356374aSAndroid Build Coastguard Worker 
290*9356374aSAndroid Build Coastguard Worker // ABSL_INTERNAL_UNIQUE_SMALL_NAME(cond)
291*9356374aSAndroid Build Coastguard Worker // This macro forces small unique name on a static file level symbols like
292*9356374aSAndroid Build Coastguard Worker // static local variables or static functions. This is intended to be used in
293*9356374aSAndroid Build Coastguard Worker // macro definitions to optimize the cost of generated code. Do NOT use it on
294*9356374aSAndroid Build Coastguard Worker // symbols exported from translation unit since it may cause a link time
295*9356374aSAndroid Build Coastguard Worker // conflict.
296*9356374aSAndroid Build Coastguard Worker //
297*9356374aSAndroid Build Coastguard Worker // Example:
298*9356374aSAndroid Build Coastguard Worker //
299*9356374aSAndroid Build Coastguard Worker // #define MY_MACRO(txt)
300*9356374aSAndroid Build Coastguard Worker // namespace {
301*9356374aSAndroid Build Coastguard Worker //  char VeryVeryLongVarName[] ABSL_INTERNAL_UNIQUE_SMALL_NAME() = txt;
302*9356374aSAndroid Build Coastguard Worker //  const char* VeryVeryLongFuncName() ABSL_INTERNAL_UNIQUE_SMALL_NAME();
303*9356374aSAndroid Build Coastguard Worker //  const char* VeryVeryLongFuncName() { return txt; }
304*9356374aSAndroid Build Coastguard Worker // }
305*9356374aSAndroid Build Coastguard Worker //
306*9356374aSAndroid Build Coastguard Worker 
307*9356374aSAndroid Build Coastguard Worker #if defined(__GNUC__)
308*9356374aSAndroid Build Coastguard Worker #define ABSL_INTERNAL_UNIQUE_SMALL_NAME2(x) #x
309*9356374aSAndroid Build Coastguard Worker #define ABSL_INTERNAL_UNIQUE_SMALL_NAME1(x) ABSL_INTERNAL_UNIQUE_SMALL_NAME2(x)
310*9356374aSAndroid Build Coastguard Worker #define ABSL_INTERNAL_UNIQUE_SMALL_NAME() \
311*9356374aSAndroid Build Coastguard Worker   asm(ABSL_INTERNAL_UNIQUE_SMALL_NAME1(.absl.__COUNTER__))
312*9356374aSAndroid Build Coastguard Worker #else
313*9356374aSAndroid Build Coastguard Worker #define ABSL_INTERNAL_UNIQUE_SMALL_NAME()
314*9356374aSAndroid Build Coastguard Worker #endif
315*9356374aSAndroid Build Coastguard Worker 
316*9356374aSAndroid Build Coastguard Worker #endif  // ABSL_BASE_OPTIMIZATION_H_
317