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