VTK-m  2.2
BinaryOperators.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_BinaryOperators_h
11 #define vtk_m_BinaryOperators_h
12 
13 #include <vtkm/Math.h>
15 
16 namespace vtkm
17 {
18 
19 // Disable conversion warnings for Sum and Product on GCC only.
20 // GCC creates false positive warnings for signed/unsigned char* operations.
21 // This occurs because the values are implicitly casted up to int's for the
22 // operation, and than casted back down to char's when return.
23 // This causes a false positive warning, even when the values is within
24 // the value types range
25 #if (defined(VTKM_GCC) || defined(VTKM_CLANG))
26 #pragma GCC diagnostic push
27 #pragma GCC diagnostic ignored "-Wconversion"
28 #endif // gcc || clang
29 
33 struct Sum
34 {
35  template <typename T, typename U>
36  VTKM_EXEC_CONT auto operator()(const T& x, const U& y) const -> decltype(x + y)
37  {
38  return x + y;
39  }
40 
41  // If both types are the same integral type, explicitly cast the result to
42  // type T to avoid narrowing conversion warnings from operations that promote
43  // to int (e.g. `int operator+(char, char)`)
44  template <typename T>
46  typename std::enable_if<std::is_integral<T>::value && sizeof(T) < sizeof(int), T>::type
47  operator()(const T& x, const T& y) const
48  {
49  return static_cast<T>(x + y);
50  }
51 };
52 
56 struct Product
57 {
58  template <typename T, typename U>
59  VTKM_EXEC_CONT auto operator()(const T& x, const U& y) const -> decltype(x * y)
60  {
61  return x * y;
62  }
63 
64  // If both types are the same integral type, explicitly cast the result to
65  // type T to avoid narrowing conversion warnings from operations that promote
66  // to int (e.g. `int operator+(char, char)`)
67  template <typename T>
69  typename std::enable_if<std::is_integral<T>::value && sizeof(T) < sizeof(int), T>::type
70  operator()(const T& x, const T& y) const
71  {
72  return static_cast<T>(x * y);
73  }
74 };
75 
76 #if (defined(VTKM_GCC) || defined(VTKM_CLANG))
77 #pragma GCC diagnostic pop
78 #endif // gcc || clang
79 
84 //needs to be full length to not clash with vtkm::math function Max.
85 struct Maximum
86 {
87  template <typename T, typename U>
88  VTKM_EXEC_CONT typename std::common_type<T, U>::type operator()(const T& x, const U& y) const
89  {
90  return x < y ? y : x;
91  }
92 };
93 
98 //needs to be full length to not clash with vtkm::math function Min.
99 struct Minimum
100 {
101  template <typename T, typename U>
102  VTKM_EXEC_CONT typename std::common_type<T, U>::type operator()(const T& x, const U& y) const
103  {
104  return x < y ? x : y;
105  }
106 };
107 
111 template <typename T>
112 struct MinAndMax
113 {
115  vtkm::Vec<T, 2> operator()(const T& a) const { return vtkm::make_Vec(a, a); }
116 
118  vtkm::Vec<T, 2> operator()(const T& a, const T& b) const
119  {
120  return vtkm::make_Vec(vtkm::Min(a, b), vtkm::Max(a, b));
121  }
122 
125  {
126  return vtkm::make_Vec(vtkm::Min(a[0], b[0]), vtkm::Max(a[1], b[1]));
127  }
128 
130  vtkm::Vec<T, 2> operator()(const T& a, const vtkm::Vec<T, 2>& b) const
131  {
132  return vtkm::make_Vec(vtkm::Min(a, b[0]), vtkm::Max(a, b[1]));
133  }
134 
136  vtkm::Vec<T, 2> operator()(const vtkm::Vec<T, 2>& a, const T& b) const
137  {
138  return vtkm::make_Vec(vtkm::Min(a[0], b), vtkm::Max(a[1], b));
139  }
140 };
141 
146 {
147  template <typename T, typename U>
148  VTKM_EXEC_CONT auto operator()(const T& x, const U& y) const -> decltype(x & y)
149  {
150  return x & y;
151  }
152 
153  // If both types are the same integral type, explicitly cast the result to
154  // type T to avoid narrowing conversion warnings from operations that promote
155  // to int (e.g. `int operator+(char, char)`)
156  template <typename T>
158  typename std::enable_if<std::is_integral<T>::value && sizeof(T) < sizeof(int), T>::type
159  operator()(const T& x, const T& y) const
160  {
161  return static_cast<T>(x & y);
162  }
163 };
164 
168 struct BitwiseOr
169 {
170  template <typename T, typename U>
171  VTKM_EXEC_CONT auto operator()(const T& x, const U& y) const -> decltype(x | y)
172  {
173  return x | y;
174  }
175 
176  // If both types are the same integral type, explicitly cast the result to
177  // type T to avoid narrowing conversion warnings from operations that promote
178  // to int (e.g. `int operator+(char, char)`)
179  template <typename T>
181  typename std::enable_if<std::is_integral<T>::value && sizeof(T) < sizeof(int), T>::type
182  operator()(const T& x, const T& y) const
183  {
184  return static_cast<T>(x | y);
185  }
186 };
187 
192 {
193  template <typename T, typename U>
194  VTKM_EXEC_CONT auto operator()(const T& x, const U& y) const -> decltype(x ^ y)
195  {
196  return x ^ y;
197  }
198 
199  // If both types are the same integral type, explicitly cast the result to
200  // type T to avoid narrowing conversion warnings from operations that promote
201  // to int (e.g. `int operator+(char, char)`)
202  template <typename T>
204  typename std::enable_if<std::is_integral<T>::value && sizeof(T) < sizeof(int), T>::type
205  operator()(const T& x, const T& y) const
206  {
207  return static_cast<T>(x ^ y);
208  }
209 };
210 
211 } // namespace vtkm
212 
213 #endif //vtk_m_BinaryOperators_h
vtkm::MinAndMax::operator()
vtkm::Vec< T, 2 > operator()(const T &a, const vtkm::Vec< T, 2 > &b) const
Definition: BinaryOperators.h:130
vtkm::MinAndMax::operator()
vtkm::Vec< T, 2 > operator()(const vtkm::Vec< T, 2 > &a, const T &b) const
Definition: BinaryOperators.h:136
vtkm
Groups connected points that have the same field value.
Definition: Atomic.h:19
vtkm::Product
Binary Predicate that takes two arguments argument x, and y and returns product (multiplication) of t...
Definition: BinaryOperators.h:56
vtkm::BitwiseOr
Binary Predicate that takes two arguments argument x, and y and returns the bitwise operation x|y
Definition: BinaryOperators.h:168
vtkm::Product::operator()
auto operator()(const T &x, const U &y) const -> decltype(x *y)
Definition: BinaryOperators.h:59
vtkm::MinAndMax
Binary Predicate that takes two arguments argument x, and y and returns a vtkm::Vec<T,...
Definition: BinaryOperators.h:112
VTKM_EXEC_CONT
#define VTKM_EXEC_CONT
Definition: ExportMacros.h:52
vtkm::make_Vec
constexpr vtkm::Vec< T, vtkm::IdComponent(sizeof...(Ts)+1)> make_Vec(T value0, Ts &&... args)
Initializes and returns a Vec containing all the arguments.
Definition: Types.h:1253
vtkm::Sum::operator()
auto operator()(const T &x, const U &y) const -> decltype(x+y)
Definition: BinaryOperators.h:36
vtkm::Maximum
Binary Predicate that takes two arguments argument x, and y and returns the x if x > y otherwise retu...
Definition: BinaryOperators.h:85
vtkm::Maximum::operator()
std::common_type< T, U >::type operator()(const T &x, const U &y) const
Definition: BinaryOperators.h:88
ExportMacros.h
vtkm::Sum
Binary Predicate that takes two arguments argument x, and y and returns sum (addition) of the two val...
Definition: BinaryOperators.h:33
Math.h
vtkm::Vec< T, 2 >
Definition: Types.h:900
vtkm::BitwiseXor
Binary Predicate that takes two arguments argument x, and y and returns the bitwise operation x^y
Definition: BinaryOperators.h:191
vtkm::BitwiseXor::operator()
auto operator()(const T &x, const U &y) const -> decltype(x ^ y)
Definition: BinaryOperators.h:194
vtkm::BitwiseOr::operator()
auto operator()(const T &x, const U &y) const -> decltype(x|y)
Definition: BinaryOperators.h:171
vtkm::MinAndMax::operator()
vtkm::Vec< T, 2 > operator()(const vtkm::Vec< T, 2 > &a, const vtkm::Vec< T, 2 > &b) const
Definition: BinaryOperators.h:124
vtkm::MinAndMax::operator()
vtkm::Vec< T, 2 > operator()(const T &a, const T &b) const
Definition: BinaryOperators.h:118
vtkm::BitwiseAnd
Binary Predicate that takes two arguments argument x, and y and returns the bitwise operation x&y
Definition: BinaryOperators.h:145
vtkm::MinAndMax::operator()
vtkm::Vec< T, 2 > operator()(const T &a) const
Definition: BinaryOperators.h:115
vtkm::Minimum::operator()
std::common_type< T, U >::type operator()(const T &x, const U &y) const
Definition: BinaryOperators.h:102
vtkm::BitwiseAnd::operator()
auto operator()(const T &x, const U &y) const -> decltype(x &y)
Definition: BinaryOperators.h:148
vtkm::Minimum
Binary Predicate that takes two arguments argument x, and y and returns the x if x < y otherwise retu...
Definition: BinaryOperators.h:99