blob: 8f9492ba9dbdbd4bb7722324ff7acdaf5b142500 [file] [log] [blame]
Googler45874d82019-08-21 12:06:47 -07001// 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
C. Antonio Sanchezdcfd7702021-10-26 11:41:24 -070013#include "../InternalHeaderCheck.h"
14
Googler45874d82019-08-21 12:06:47 -070015namespace Eigen {
16
17namespace internal {
18
19//---------- associative ternary functors ----------
20
Rasmus Munk Larsenad9fe062023-03-13 18:16:49 -070021template <typename ThenScalar, typename ElseScalar, typename ConditionScalar>
22struct 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};
Googler45874d82019-08-21 12:06:47 -070035
Rasmus Munk Larsenad9fe062023-03-13 18:16:49 -070036template <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};
Googler45874d82019-08-21 12:06:47 -070044
45} // end namespace internal
46
47} // end namespace Eigen
48
49#endif // EIGEN_TERNARY_FUNCTORS_H