NVIDIA DeepStream SDK API Reference

9.2 Release
includes/opentelemetry/nostd/internal/absl/base/optimization.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3  * SPDX-License-Identifier: Apache-2.0
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #ifndef OTABSL_BASE_OPTIMIZATION_H_
19 #define OTABSL_BASE_OPTIMIZATION_H_
20 
21 #include "config.h"
22 
23 // OTABSL_BLOCK_TAIL_CALL_OPTIMIZATION
24 //
25 // Instructs the compiler to avoid optimizing tail-call recursion. Use of this
26 // macro is useful when you wish to preserve the existing function order within
27 // a stack trace for logging, debugging, or profiling purposes.
28 //
29 // Example:
30 //
31 // int f() {
32 // int result = g();
33 // OTABSL_BLOCK_TAIL_CALL_OPTIMIZATION();
34 // return result;
35 // }
36 #if defined(__pnacl__)
37 #define OTABSL_BLOCK_TAIL_CALL_OPTIMIZATION() if (volatile int x = 0) { (void)x; }
38 #elif defined(__clang__)
39 // Clang will not tail call given inline volatile assembly.
40 #define OTABSL_BLOCK_TAIL_CALL_OPTIMIZATION() __asm__ __volatile__("")
41 #elif defined(__GNUC__)
42 // GCC will not tail call given inline volatile assembly.
43 #define OTABSL_BLOCK_TAIL_CALL_OPTIMIZATION() __asm__ __volatile__("")
44 #elif defined(_MSC_VER)
45 #include <intrin.h>
46 // The __nop() intrinsic blocks the optimisation.
47 #define OTABSL_BLOCK_TAIL_CALL_OPTIMIZATION() __nop()
48 #else
49 #define OTABSL_BLOCK_TAIL_CALL_OPTIMIZATION() if (volatile int x = 0) { (void)x; }
50 #endif
51 
52 // OTABSL_CACHELINE_SIZE
53 //
54 // Explicitly defines the size of the L1 cache for purposes of alignment.
55 // Setting the cacheline size allows you to specify that certain objects be
56 // aligned on a cacheline boundary with `OTABSL_CACHELINE_ALIGNED` declarations.
57 // (See below.)
58 //
59 // NOTE: this macro should be replaced with the following C++17 features, when
60 // those are generally available:
61 //
62 // * `std::hardware_constructive_interference_size`
63 // * `std::hardware_destructive_interference_size`
64 //
65 // See http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0154r1.html
66 // for more information.
67 #if defined(__GNUC__)
68 // Cache line alignment
69 #if defined(__i386__) || defined(__x86_64__)
70 #define OTABSL_CACHELINE_SIZE 64
71 #elif defined(__powerpc64__)
72 #define OTABSL_CACHELINE_SIZE 128
73 #elif defined(__aarch64__)
74 // We would need to read special register ctr_el0 to find out L1 dcache size.
75 // This value is a good estimate based on a real aarch64 machine.
76 #define OTABSL_CACHELINE_SIZE 64
77 #elif defined(__arm__)
78 // Cache line sizes for ARM: These values are not strictly correct since
79 // cache line sizes depend on implementations, not architectures. There
80 // are even implementations with cache line sizes configurable at boot
81 // time.
82 #if defined(__ARM_ARCH_5T__)
83 #define OTABSL_CACHELINE_SIZE 32
84 #elif defined(__ARM_ARCH_7A__)
85 #define OTABSL_CACHELINE_SIZE 64
86 #endif
87 #endif
88 
89 #ifndef OTABSL_CACHELINE_SIZE
90 // A reasonable default guess. Note that overestimates tend to waste more
91 // space, while underestimates tend to waste more time.
92 #define OTABSL_CACHELINE_SIZE 64
93 #endif
94 
95 // OTABSL_CACHELINE_ALIGNED
96 //
97 // Indicates that the declared object be cache aligned using
98 // `OTABSL_CACHELINE_SIZE` (see above). Cacheline aligning objects allows you to
99 // load a set of related objects in the L1 cache for performance improvements.
100 // Cacheline aligning objects properly allows constructive memory sharing and
101 // prevents destructive (or "false") memory sharing.
102 //
103 // NOTE: this macro should be replaced with usage of `alignas()` using
104 // `std::hardware_constructive_interference_size` and/or
105 // `std::hardware_destructive_interference_size` when available within C++17.
106 //
107 // See http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0154r1.html
108 // for more information.
109 //
110 // On some compilers, `OTABSL_CACHELINE_ALIGNED` expands to an `__attribute__`
111 // or `__declspec` attribute. For compilers where this is not known to work,
112 // the macro expands to nothing.
113 //
114 // No further guarantees are made here. The result of applying the macro
115 // to variables and types is always implementation-defined.
116 //
117 // WARNING: It is easy to use this attribute incorrectly, even to the point
118 // of causing bugs that are difficult to diagnose, crash, etc. It does not
119 // of itself guarantee that objects are aligned to a cache line.
120 //
121 // NOTE: Some compilers are picky about the locations of annotations such as
122 // this attribute, so prefer to put it at the beginning of your declaration.
123 // For example,
124 //
125 // OTABSL_CACHELINE_ALIGNED static Foo* foo = ...
126 //
127 // class OTABSL_CACHELINE_ALIGNED Bar { ...
128 //
129 // Recommendations:
130 //
131 // 1) Consult compiler documentation; this comment is not kept in sync as
132 // toolchains evolve.
133 // 2) Verify your use has the intended effect. This often requires inspecting
134 // the generated machine code.
135 // 3) Prefer applying this attribute to individual variables. Avoid
136 // applying it to types. This tends to localize the effect.
137 #define OTABSL_CACHELINE_ALIGNED __attribute__((aligned(OTABSL_CACHELINE_SIZE)))
138 #elif defined(_MSC_VER)
139 #define OTABSL_CACHELINE_SIZE 64
140 #define OTABSL_CACHELINE_ALIGNED __declspec(align(OTABSL_CACHELINE_SIZE))
141 #else
142 #define OTABSL_CACHELINE_SIZE 64
143 #define OTABSL_CACHELINE_ALIGNED
144 #endif
145 
146 // OTABSL_PREDICT_TRUE, OTABSL_PREDICT_FALSE
147 //
148 // Enables the compiler to prioritize compilation using static analysis for
149 // likely paths within a boolean branch.
150 //
151 // Example:
152 //
153 // if (OTABSL_PREDICT_TRUE(expression)) {
154 // return result; // Faster if more likely
155 // } else {
156 // return 0;
157 // }
158 //
159 // Compilers can use the information that a certain branch is not likely to be
160 // taken (for instance, a CHECK failure) to optimize for the common case in
161 // the absence of better information (ie. compiling gcc with `-fprofile-arcs`).
162 //
163 // Recommendation: Modern CPUs dynamically predict branch execution paths,
164 // typically with accuracy greater than 97%. As a result, annotating every
165 // branch in a codebase is likely counterproductive; however, annotating
166 // specific branches that are both hot and consistently mispredicted is likely
167 // to yield performance improvements.
168 #if OTABSL_HAVE_BUILTIN(__builtin_expect) || \
169  (defined(__GNUC__) && !defined(__clang__))
170 #define OTABSL_PREDICT_FALSE(x) (__builtin_expect(x, 0))
171 #define OTABSL_PREDICT_TRUE(x) (__builtin_expect(false || (x), true))
172 #else
173 #define OTABSL_PREDICT_FALSE(x) (x)
174 #define OTABSL_PREDICT_TRUE(x) (x)
175 #endif
176 
177 #endif // OTABSL_BASE_OPTIMIZATION_H_
config.h