VTK  9.5.20251008
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
8#ifndef vtkAssume_h
9#define vtkAssume_h
10
11#include "vtkCompiler.h"
12
13#include <cassert>
14
30#define VTK_ASSUME(cond) \
31 do \
32 { \
33 const bool c = cond; \
34 assert("Bad assumption in VTK_ASSUME: " #cond&& c); \
35 VTK_ASSUME_IMPL(c); \
36 (void)c; /* Prevents unused var warnings */ \
37 } while (false) /* do-while prevents extra semicolon warnings */
38
39#define VTK_ASSUME_NO_ASSERT(cond) \
40 do \
41 { \
42 const bool c = cond; \
43 VTK_ASSUME_IMPL(c); \
44 (void)c; /* Prevents unused var warnings */ \
45 } while (false) /* do-while prevents extra semicolon warnings */
46
47#ifdef __has_builtin
48#define VTK_HAS_BUILTIN(x) __has_builtin(x)
49#else
50#define VTK_HAS_BUILTIN(x) 0
51#endif
52
53// VTK_ASSUME_IMPL is compiler-specific:
54#if defined(VTK_COMPILER_MSVC) || defined(VTK_COMPILER_ICC)
55#define VTK_ASSUME_IMPL(cond) __assume(cond)
56#elif VTK_HAS_BUILTIN(__builtin_assume) || defined(VTK_COMPILER_CLANG)
57#define VTK_ASSUME_IMPL(cond) __builtin_assume(cond)
58#elif defined(VTK_COMPILER_GCC) && VTK_COMPILER_GCC_VERSION >= 130000
59#define VTK_ASSUME_IMPL(cond) __attribute__((__assume__(cond)))
60#elif VTK_HAS_BUILTIN(__builtin_unreachable) || defined(VTK_COMPILER_GCC)
61#define VTK_ASSUME_IMPL(cond) \
62 if (!(cond)) \
63 __builtin_unreachable()
64#else
65#define VTK_ASSUME_IMPL(cond) \
66 do \
67 { \
68 } while (false) /* no-op */
69#endif
70
71// VTK_EXPECT & VTK_LIKELY & VTK_UNLIKELY
72#if VTK_HAS_BUILTIN(__builtin_expect) || defined(VTK_COMPILER_GCC) || defined(VTK_COMPILER_CLANG)
73#define VTK_EXPECT(cond, expected) __builtin_expect(cond, expected)
74#define VTK_LIKELY(cond) VTK_EXPECT(!!(cond), 1)
75#define VTK_UNLIKELY(cond) VTK_EXPECT(!!(cond), 0)
76#else
77#define VTK_EXPECT(cond, expected) (cond)
78#define VTK_LIKELY(cond) (cond)
79#define VTK_UNLIKELY(cond) (cond)
80#endif
81
82#endif // vtkAssume_h
83// VTK-HeaderTest-Exclude: vtkAssume.h