forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtkAssume.h
74 lines (61 loc) · 3.77 KB
/
vtkAssume.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*=========================================================================
Program: Visualization Toolkit
Module: vtkAssume.h
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
/**
* @class VTK_ASSUME
* @brief Provide compiler hints for non-obvious conditions.
*/
#ifndef vtkAssume_h
#define vtkAssume_h
#include "vtkCompiler.h"
#include <cassert>
/**
* VTK_ASSUME instructs the compiler that a certain non-obvious condition will
* *always* be true. Beware that if cond is false at runtime, the results are
* unpredictable (and likely catastrophic). A runtime assertion is added so
* that debugging builds may easily catch violations of the condition.
* A useful application of this macro is when a vtkGenericDataArray subclass has
* a known number of components at compile time. Adding, for example,
* VTK_ASSUME(array->GetNumberOfComponents() == 3); allows the compiler to
* provide faster access through the GetTypedComponent method, as the fixed data
* stride in AOS arrays allows advanced optimization of the accesses.
* A more detailed description of this class and related tools can be found
* \ref VTK-7-1-ArrayDispatch "here".
*/
#define VTK_ASSUME(cond) \
do \
{ \
const bool c = cond; \
assert("Bad assumption in VTK_ASSUME: " #cond&& c); \
VTK_ASSUME_IMPL(c); \
(void)c; /* Prevents unused var warnings */ \
} while (false) /* do-while prevents extra semicolon warnings */
#define VTK_ASSUME_NO_ASSERT(cond) \
do \
{ \
const bool c = cond; \
VTK_ASSUME_IMPL(c); \
(void)c; /* Prevents unused var warnings */ \
} while (false) /* do-while prevents extra semicolon warnings */
// VTK_ASSUME_IMPL is compiler-specific:
#if defined(VTK_COMPILER_MSVC) || defined(VTK_COMPILER_ICC)
#define VTK_ASSUME_IMPL(cond) __assume(cond)
#elif defined(VTK_COMPILER_GCC) || defined(VTK_COMPILER_CLANG)
#define VTK_ASSUME_IMPL(cond) \
if (!(cond)) \
__builtin_unreachable()
#else
#define VTK_ASSUME_IMPL(cond) \
do \
{ \
} while (false) /* no-op */
#endif
#endif // vtkAssume_h
// VTK-HeaderTest-Exclude: vtkAssume.h