Viskores  1.0
BinaryOperators.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_BinaryOperators_h
19 #define viskores_BinaryOperators_h
20 
21 #include <viskores/Math.h>
23 
24 namespace viskores
25 {
26 
27 // Disable conversion warnings for Sum and Product on GCC only.
28 // GCC creates false positive warnings for signed/unsigned char* operations.
29 // This occurs because the values are implicitly casted up to int's for the
30 // operation, and than casted back down to char's when return.
31 // This causes a false positive warning, even when the values is within
32 // the value types range
33 #if (defined(VISKORES_GCC) || defined(VISKORES_CLANG))
34 #pragma GCC diagnostic push
35 #pragma GCC diagnostic ignored "-Wconversion"
36 #endif // gcc || clang
37 
41 struct Sum
42 {
43  template <typename T, typename U>
44  VISKORES_EXEC_CONT auto operator()(const T& x, const U& y) const -> decltype(x + y)
45  {
46  return x + y;
47  }
48 
49  // If both types are the same integral type, explicitly cast the result to
50  // type T to avoid narrowing conversion warnings from operations that promote
51  // to int (e.g. `int operator+(char, char)`)
52  template <typename T>
54  typename std::enable_if<std::is_integral<T>::value && sizeof(T) < sizeof(int), T>::type
55  operator()(const T& x, const T& y) const
56  {
57  return static_cast<T>(x + y);
58  }
59 };
60 
64 struct Product
65 {
66  template <typename T, typename U>
67  VISKORES_EXEC_CONT auto operator()(const T& x, const U& y) const -> decltype(x * y)
68  {
69  return x * y;
70  }
71 
72  // If both types are the same integral type, explicitly cast the result to
73  // type T to avoid narrowing conversion warnings from operations that promote
74  // to int (e.g. `int operator+(char, char)`)
75  template <typename T>
77  typename std::enable_if<std::is_integral<T>::value && sizeof(T) < sizeof(int), T>::type
78  operator()(const T& x, const T& y) const
79  {
80  return static_cast<T>(x * y);
81  }
82 };
83 
84 #if (defined(VISKORES_GCC) || defined(VISKORES_CLANG))
85 #pragma GCC diagnostic pop
86 #endif // gcc || clang
87 
92 //needs to be full length to not clash with viskores::math function Max.
93 struct Maximum
94 {
95  template <typename T, typename U>
96  VISKORES_EXEC_CONT typename std::common_type<T, U>::type operator()(const T& x, const U& y) const
97  {
98  return x < y ? y : x;
99  }
100 };
101 
106 //needs to be full length to not clash with viskores::math function Min.
107 struct Minimum
108 {
109  template <typename T, typename U>
110  VISKORES_EXEC_CONT typename std::common_type<T, U>::type operator()(const T& x, const U& y) const
111  {
112  return x < y ? x : y;
113  }
114 };
115 
119 template <typename T>
120 struct MinAndMax
121 {
123  viskores::Vec<T, 2> operator()(const T& a) const { return viskores::make_Vec(a, a); }
124 
126  viskores::Vec<T, 2> operator()(const T& a, const T& b) const
127  {
128  return viskores::make_Vec(viskores::Min(a, b), viskores::Max(a, b));
129  }
130 
133  {
134  return viskores::make_Vec(viskores::Min(a[0], b[0]), viskores::Max(a[1], b[1]));
135  }
136 
139  {
140  return viskores::make_Vec(viskores::Min(a, b[0]), viskores::Max(a, b[1]));
141  }
142 
145  {
146  return viskores::make_Vec(viskores::Min(a[0], b), viskores::Max(a[1], b));
147  }
148 };
149 
154 {
155  template <typename T, typename U>
156  VISKORES_EXEC_CONT auto operator()(const T& x, const U& y) const -> decltype(x & y)
157  {
158  return x & y;
159  }
160 
161  // If both types are the same integral type, explicitly cast the result to
162  // type T to avoid narrowing conversion warnings from operations that promote
163  // to int (e.g. `int operator+(char, char)`)
164  template <typename T>
166  typename std::enable_if<std::is_integral<T>::value && sizeof(T) < sizeof(int), T>::type
167  operator()(const T& x, const T& y) const
168  {
169  return static_cast<T>(x & y);
170  }
171 };
172 
176 struct BitwiseOr
177 {
178  template <typename T, typename U>
179  VISKORES_EXEC_CONT auto operator()(const T& x, const U& y) const -> decltype(x | y)
180  {
181  return x | y;
182  }
183 
184  // If both types are the same integral type, explicitly cast the result to
185  // type T to avoid narrowing conversion warnings from operations that promote
186  // to int (e.g. `int operator+(char, char)`)
187  template <typename T>
189  typename std::enable_if<std::is_integral<T>::value && sizeof(T) < sizeof(int), T>::type
190  operator()(const T& x, const T& y) const
191  {
192  return static_cast<T>(x | y);
193  }
194 };
195 
200 {
201  template <typename T, typename U>
202  VISKORES_EXEC_CONT auto operator()(const T& x, const U& y) const -> decltype(x ^ y)
203  {
204  return x ^ y;
205  }
206 
207  // If both types are the same integral type, explicitly cast the result to
208  // type T to avoid narrowing conversion warnings from operations that promote
209  // to int (e.g. `int operator+(char, char)`)
210  template <typename T>
212  typename std::enable_if<std::is_integral<T>::value && sizeof(T) < sizeof(int), T>::type
213  operator()(const T& x, const T& y) const
214  {
215  return static_cast<T>(x ^ y);
216  }
217 };
218 
219 } // namespace viskores
220 
221 #endif //viskores_BinaryOperators_h
viskores::Product
Binary Predicate that takes two arguments argument x, and y and returns product (multiplication) of t...
Definition: BinaryOperators.h:64
viskores::MinAndMax::operator()
viskores::Vec< T, 2 > operator()(const T &a, const viskores::Vec< T, 2 > &b) const
Definition: BinaryOperators.h:138
viskores::MinAndMax::operator()
viskores::Vec< T, 2 > operator()(const viskores::Vec< T, 2 > &a, const T &b) const
Definition: BinaryOperators.h:144
viskores::BitwiseAnd::operator()
auto operator()(const T &x, const U &y) const -> decltype(x &y)
Definition: BinaryOperators.h:156
viskores::MinAndMax::operator()
viskores::Vec< T, 2 > operator()(const viskores::Vec< T, 2 > &a, const viskores::Vec< T, 2 > &b) const
Definition: BinaryOperators.h:132
viskores::MinAndMax::operator()
viskores::Vec< T, 2 > operator()(const T &a, const T &b) const
Definition: BinaryOperators.h:126
viskores::BitwiseXor::operator()
auto operator()(const T &x, const U &y) const -> decltype(x ^ y)
Definition: BinaryOperators.h:202
viskores::BitwiseAnd
Binary Predicate that takes two arguments argument x, and y and returns the bitwise operation x&y
Definition: BinaryOperators.h:153
viskores::Maximum::operator()
std::common_type< T, U >::type operator()(const T &x, const U &y) const
Definition: BinaryOperators.h:96
viskores::BitwiseOr::operator()
auto operator()(const T &x, const U &y) const -> decltype(x|y)
Definition: BinaryOperators.h:179
VISKORES_EXEC_CONT
#define VISKORES_EXEC_CONT
Definition: ExportMacros.h:60
viskores::BitwiseXor
Binary Predicate that takes two arguments argument x, and y and returns the bitwise operation x^y
Definition: BinaryOperators.h:199
viskores::Vec< T, 2 >
Definition: Types.h:909
viskores::Maximum
Binary Predicate that takes two arguments argument x, and y and returns the x if x > y otherwise retu...
Definition: BinaryOperators.h:93
ExportMacros.h
viskores::BitwiseOr
Binary Predicate that takes two arguments argument x, and y and returns the bitwise operation x|y
Definition: BinaryOperators.h:176
viskores
Groups connected points that have the same field value.
Definition: Atomic.h:27
Math.h
viskores::make_Vec
constexpr viskores::Vec< T, viskores::IdComponent(sizeof...(Ts)+1)> make_Vec(T value0, Ts &&... args)
Initializes and returns a Vec containing all the arguments.
Definition: Types.h:1262
viskores::Sum
Binary Predicate that takes two arguments argument x, and y and returns sum (addition) of the two val...
Definition: BinaryOperators.h:41
viskores::Product::operator()
auto operator()(const T &x, const U &y) const -> decltype(x *y)
Definition: BinaryOperators.h:67
viskores::MinAndMax::operator()
viskores::Vec< T, 2 > operator()(const T &a) const
Definition: BinaryOperators.h:123
viskores::Minimum
Binary Predicate that takes two arguments argument x, and y and returns the x if x < y otherwise retu...
Definition: BinaryOperators.h:107
viskores::Minimum::operator()
std::common_type< T, U >::type operator()(const T &x, const U &y) const
Definition: BinaryOperators.h:110
viskores::Sum::operator()
auto operator()(const T &x, const U &y) const -> decltype(x+y)
Definition: BinaryOperators.h:44
viskores::MinAndMax
Binary Predicate that takes two arguments argument x, and y and returns a viskores::Vec<T,...
Definition: BinaryOperators.h:120