VTK  9.6.20260606
vtkAssume.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
2// SPDX-License-Identifier: BSD-3-Clause
18
19#ifndef vtkAssume_h
20#define vtkAssume_h
21
22#include "vtkCompiler.h"
23
24#include <cassert>
25
70#define VTK_ASSUME(cond) \
71 do \
72 { \
73 const bool c = cond; \
74 assert("Bad assumption in VTK_ASSUME: " #cond&& c); \
75 VTK_ASSUME_IMPL(c); \
76 (void)c; /* Prevents unused var warnings */ \
77 } while (false) /* do-while prevents extra semicolon warnings */
78
101#define VTK_ASSUME_NO_ASSERT(cond) \
102 do \
103 { \
104 const bool c = cond; \
105 VTK_ASSUME_IMPL(c); \
106 (void)c; /* Prevents unused var warnings */ \
107 } while (false) /* do-while prevents extra semicolon warnings */
108
109#ifdef __has_builtin
110#define VTK_HAS_BUILTIN(x) __has_builtin(x)
111#else
112#define VTK_HAS_BUILTIN(x) 0
113#endif
114
115// VTK_ASSUME_IMPL is compiler-specific (not part of the public API):
116#if defined(VTK_COMPILER_MSVC) || defined(VTK_COMPILER_ICC)
117#define VTK_ASSUME_IMPL(cond) __assume(cond)
118#elif VTK_HAS_BUILTIN(__builtin_assume) || defined(VTK_COMPILER_CLANG)
119#define VTK_ASSUME_IMPL(cond) __builtin_assume(cond)
120#elif defined(VTK_COMPILER_GCC) && VTK_COMPILER_GCC_VERSION >= 130000
121#define VTK_ASSUME_IMPL(cond) __attribute__((__assume__(cond)))
122#elif VTK_HAS_BUILTIN(__builtin_unreachable) || defined(VTK_COMPILER_GCC)
123#define VTK_ASSUME_IMPL(cond) \
124 if (!(cond)) \
125 __builtin_unreachable()
126#else
127#define VTK_ASSUME_IMPL(cond) \
128 do \
129 { \
130 } while (false) /* no-op */
131#endif
132
152
175
194#if VTK_HAS_BUILTIN(__builtin_expect) || defined(VTK_COMPILER_GCC) || defined(VTK_COMPILER_CLANG)
195#define VTK_EXPECT(cond, expected) __builtin_expect(cond, expected)
196#define VTK_LIKELY(cond) VTK_EXPECT(!!(cond), 1)
197#define VTK_UNLIKELY(cond) VTK_EXPECT(!!(cond), 0)
198#else
199#define VTK_EXPECT(cond, expected) (cond)
200#define VTK_LIKELY(cond) (cond)
201#define VTK_UNLIKELY(cond) (cond)
202#endif
203
204#endif // vtkAssume_h
205// VTK-HeaderTest-Exclude: vtkAssume.h