BinaryOperators.h 4.89 KB
Newer Older
1 2 3 4 5 6 7 8
//============================================================================
//  Copyright (c) Kitware, Inc.
//  All rights reserved.
//  See LICENSE.txt 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.
//
9
//  Copyright 2014 National Technology & Engineering Solutions of Sandia, LLC (NTESS).
10 11 12
//  Copyright 2014 UT-Battelle, LLC.
//  Copyright 2014 Los Alamos National Security.
//
13
//  Under the terms of Contract DE-NA0003525 with NTESS,
14 15 16 17 18 19 20 21 22
//  the U.S. Government retains certain rights in this software.
//
//  Under the terms of Contract DE-AC52-06NA25396 with Los Alamos National
//  Laboratory (LANL), the U.S. Government retains certain rights in
//  this software.
//============================================================================
#ifndef vtk_m_BinaryOperators_h
#define vtk_m_BinaryOperators_h

23
#include <vtkm/Math.h>
24
#include <vtkm/internal/ExportMacros.h>
25

26 27
namespace vtkm
{
28

29 30 31 32 33 34
// Disable conversion warnings for Sum and Product on GCC only.
// GCC creates false positive warnings for signed/unsigned char* operations.
// This occurs because the values are implicitly casted up to int's for the
// operation, and than  casted back down to char's when return.
// This causes a false positive warning, even when the values is within
// the value types range
35
#if (defined(VTKM_GCC) || defined(VTKM_CLANG))
36 37 38 39
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wconversion"
#endif // gcc || clang

40 41 42 43 44
/// Binary Predicate that takes two arguments argument \c x, and \c y and
/// returns sum (addition) of the two values.
/// Note: Requires Type \p T implement the + operator.
struct Sum
{
45
  template <typename T>
46
  VTKM_EXEC_CONT T operator()(const T& x, const T& y) const
47 48 49 50 51 52 53 54 55 56
  {
    return x + y;
  }
};

/// Binary Predicate that takes two arguments argument \c x, and \c y and
/// returns product (multiplication) of the two values.
/// Note: Requires Type \p T implement the * operator.
struct Product
{
57
  template <typename T>
58
  VTKM_EXEC_CONT T operator()(const T& x, const T& y) const
59 60 61 62 63
  {
    return x * y;
  }
};

64
#if (defined(VTKM_GCC) || defined(VTKM_CLANG))
65 66 67
#pragma GCC diagnostic pop
#endif // gcc || clang

68 69 70 71 72 73
/// Binary Predicate that takes two arguments argument \c x, and \c y and
/// returns the \c x if x > y otherwise returns \c y.
/// Note: Requires Type \p T implement the < operator.
//needs to be full length to not clash with vtkm::math function Max.
struct Maximum
{
74
  template <typename T>
75
  VTKM_EXEC_CONT T operator()(const T& x, const T& y) const
76
  {
77
    return x < y ? y : x;
78 79 80 81 82 83 84 85 86
  }
};

/// Binary Predicate that takes two arguments argument \c x, and \c y and
/// returns the \c x if x < y otherwise returns \c y.
/// Note: Requires Type \p T implement the < operator.
//needs to be full length to not clash with vtkm::math function Min.
struct Minimum
{
87
  template <typename T>
88
  VTKM_EXEC_CONT T operator()(const T& x, const T& y) const
89
  {
90
    return x < y ? x : y;
91 92 93
  }
};

94
/// Binary Predicate that takes two arguments argument \c x, and \c y and
95
/// returns a vtkm::Vec<T,2> that represents the minimum and maximum values
96 97 98 99 100
/// Note: Requires Type \p T implement the vtkm::Min and vtkm::Max functions.
template <typename T>
struct MinAndMax
{
  VTKM_EXEC_CONT
101
  vtkm::Vec<T, 2> operator()(const T& a, const T& b) const
102
  {
103
    return vtkm::make_Vec(vtkm::Min(a, b), vtkm::Max(a, b));
104 105 106
  }

  VTKM_EXEC_CONT
107
  vtkm::Vec<T, 2> operator()(const vtkm::Vec<T, 2>& a, const vtkm::Vec<T, 2>& b) const
108
  {
109
    return vtkm::make_Vec(vtkm::Min(a[0], b[0]), vtkm::Max(a[1], b[1]));
110 111 112
  }

  VTKM_EXEC_CONT
113
  vtkm::Vec<T, 2> operator()(const T& a, const vtkm::Vec<T, 2>& b) const
114
  {
115
    return vtkm::make_Vec(vtkm::Min(a, b[0]), vtkm::Max(a, b[1]));
116 117 118
  }

  VTKM_EXEC_CONT
119
  vtkm::Vec<T, 2> operator()(const vtkm::Vec<T, 2>& a, const T& b) const
120
  {
121
    return vtkm::make_Vec(vtkm::Min(a[0], b), vtkm::Max(a[1], b));
122 123 124
  }
};

125 126 127 128 129
/// Binary Predicate that takes two arguments argument \c x, and \c y and
/// returns the bitwise operation <tt>x&y</tt>
/// Note: Requires Type \p T implement the & operator.
struct BitwiseAnd
{
130
  template <typename T>
131
  VTKM_EXEC_CONT T operator()(const T& x, const T& y) const
132 133 134 135 136 137 138 139 140 141
  {
    return x & y;
  }
};

/// Binary Predicate that takes two arguments argument \c x, and \c y and
/// returns the bitwise operation <tt>x|y</tt>
/// Note: Requires Type \p T implement the | operator.
struct BitwiseOr
{
142
  template <typename T>
143
  VTKM_EXEC_CONT T operator()(const T& x, const T& y) const
144 145 146 147 148 149 150 151 152 153
  {
    return x | y;
  }
};

/// Binary Predicate that takes two arguments argument \c x, and \c y and
/// returns the bitwise operation <tt>x^y</tt>
/// Note: Requires Type \p T implement the ^ operator.
struct BitwiseXor
{
154
  template <typename T>
155
  VTKM_EXEC_CONT T operator()(const T& x, const T& y) const
156 157 158 159 160 161 162
  {
    return x ^ y;
  }
};

} // namespace vtkm

163
#endif //vtk_m_BinaryOperators_h