VTK-m  2.0
Assume.h
Go to the documentation of this file.
1 //============================================================================
2 // Copyright (c) Kitware, Inc.
3 // All rights reserved.
4 // See LICENSE.txt for details.
5 //
6 // This software is distributed WITHOUT ANY WARRANTY; without even
7 // the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
8 // PURPOSE. See the above copyright notice for more information.
9 //============================================================================
10 #ifndef vtk_m_internal_Assume_h
11 #define vtk_m_internal_Assume_h
12 
13 #include <vtkm/Assert.h>
14 
15 // Description:
16 // VTKM_ASSUME instructs the compiler that a certain non-obvious condition will
17 // *always* be true. Beware that if cond is false at runtime, the results are
18 // unpredictable (and likely catastrophic). A runtime assertion is added so
19 // that debugging builds may easily catch violations of the condition.
20 //
21 // A useful application of this macro is when a method is passed in a
22 // vtkm::Vec that is uninitialized and conditional fills the vtkm::Vec
23 // based on other runtime information such as cell type. This allows you to
24 // assert that only valid cell types will be used, producing more efficient
25 // code.
26 //
27 #define VTKM_ASSUME(cond) \
28  VTKM_SWALLOW_SEMICOLON_PRE_BLOCK \
29  { \
30  const bool c = cond; \
31  VTKM_ASSERT("Bad assumption in VTKM_ASSUME: " #cond&& c); \
32  VTKM_ASSUME_IMPL(c); \
33  (void)c; /* Prevents unused var warnings */ \
34  } \
35  VTKM_SWALLOW_SEMICOLON_POST_BLOCK
36 
37 // VTKM_ASSUME_IMPL is compiler-specific:
38 #if defined(VTKM_CUDA_DEVICE_PASS)
39 //For all versions of CUDA this is a no-op while we look
40 //for a CUDA asm snippet that replicates this kind of behavior
41 #define VTKM_ASSUME_IMPL(cond) (void)0 /* no-op */
42 #else
43 
44 #if defined(VTKM_MSVC)
45 #define VTKM_ASSUME_IMPL(cond) __assume(cond)
46 #elif defined(VTKM_ICC) && !defined(__GNUC__)
47 #define VTKM_ASSUME_IMPL(cond) __assume(cond)
48 #elif (defined(VTKM_GCC) || defined(VTKM_ICC)) && \
49  (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5))
50 // Added in 4.5.0:
51 #define VTKM_ASSUME_IMPL(cond) \
52  if (!(cond)) \
53  __builtin_unreachable()
54 #elif defined(VTKM_CLANG)
55 #define VTKM_ASSUME_IMPL(cond) \
56  if (!(cond)) \
57  __builtin_unreachable()
58 #else
59 #define VTKM_ASSUME_IMPL(cond) (void)0 /* no-op */
60 #endif
61 
62 #endif
63 
64 #endif // vtk_m_internal_Assume_h
Assert.h