TernaryFunctors.h
Go to the documentation of this file.
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2016 Eugene Brevdo <ebrevdo@gmail.com>
5 //
6 // This Source Code Form is subject to the terms of the Mozilla
7 // Public License v. 2.0. If a copy of the MPL was not distributed
8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 
10 #ifndef EIGEN_TERNARY_FUNCTORS_H
11 #define EIGEN_TERNARY_FUNCTORS_H
12 
13 #include "../InternalHeaderCheck.h"
14 
15 namespace Eigen {
16 
17 namespace internal {
18 
19 //---------- associative ternary functors ----------
20 
21 template <typename ThenScalar, typename ElseScalar, typename ConditionScalar>
22 struct scalar_boolean_select_op {
23  static constexpr bool ThenElseAreSame = is_same<ThenScalar, ElseScalar>::value;
24  EIGEN_STATIC_ASSERT(ThenElseAreSame, THEN AND ELSE MUST BE SAME TYPE)
25  using Scalar = ThenScalar;
26  using result_type = Scalar;
27  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar operator()(const ThenScalar& a, const ElseScalar& b, const ConditionScalar& cond) const {
28  return cond == ConditionScalar(0) ? b : a;
29  }
30  template <typename Packet>
31  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Packet packetOp(const Packet& a, const Packet& b, const Packet& cond) const {
32  return pselect(pcmp_eq(cond, pzero(cond)), b, a);
33  }
34 };
35 
36 template <typename ThenScalar, typename ElseScalar, typename ConditionScalar>
37  struct functor_traits<scalar_boolean_select_op<ThenScalar, ElseScalar, ConditionScalar>> {
38  using Scalar = ThenScalar;
39  enum {
40  Cost = 1,
41  PacketAccess = is_same<ThenScalar, ElseScalar>::value && is_same<ConditionScalar, Scalar>::value && packet_traits<Scalar>::HasCmp
42  };
43 };
44 
45 } // end namespace internal
46 
47 } // end namespace Eigen
48 
49 #endif // EIGEN_TERNARY_FUNCTORS_H
Array< int, 3, 1 > b
IndexedView_or_Block operator()(const RowIndices &rowIndices, const ColIndices &colIndices)
#define EIGEN_DEVICE_FUNC
Definition: Macros.h:883
#define EIGEN_STATIC_ASSERT(X, MSG)
Definition: StaticAssert.h:26
Packet8f pzero(const Packet8f &)
Packet2cf pcmp_eq(const Packet2cf &a, const Packet2cf &b)
Packet4f pselect(const Packet4f &mask, const Packet4f &a, const Packet4f &b)
: InteropHeaders
Definition: Core:139