blob: 745779a137d6c72b6d35eb4cd41b30ec51bc8744 [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
Rasmus Munk Larsen66c640f2023-08-22 14:16:58 -070013// IWYU pragma: private
C. Antonio Sanchezdcfd7702021-10-26 11:41:24 -070014#include "../InternalHeaderCheck.h"
15
Googler45874d82019-08-21 12:06:47 -070016namespace Eigen {
17
18namespace internal {
19
20//---------- associative ternary functors ----------
21
Rasmus Munk Larsenad9fe062023-03-13 18:16:49 -070022template <typename ThenScalar, typename ElseScalar, typename ConditionScalar>
23struct scalar_boolean_select_op {
24 static constexpr bool ThenElseAreSame = is_same<ThenScalar, ElseScalar>::value;
25 EIGEN_STATIC_ASSERT(ThenElseAreSame, THEN AND ELSE MUST BE SAME TYPE)
26 using Scalar = ThenScalar;
27 using result_type = Scalar;
Rasmus Munk Larsen2434cfd2023-12-06 12:02:41 -080028 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar operator()(const ThenScalar& a, const ElseScalar& b,
29 const ConditionScalar& cond) const {
Rasmus Munk Larsenad9fe062023-03-13 18:16:49 -070030 return cond == ConditionScalar(0) ? b : a;
31 }
32 template <typename Packet>
Rasmus Munk Larsen2434cfd2023-12-06 12:02:41 -080033 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Packet packetOp(const Packet& a, const Packet& b, const Packet& cond) const {
Rasmus Munk Larsenad9fe062023-03-13 18:16:49 -070034 return pselect(pcmp_eq(cond, pzero(cond)), b, a);
35 }
36};
Googler45874d82019-08-21 12:06:47 -070037
Rasmus Munk Larsenad9fe062023-03-13 18:16:49 -070038template <typename ThenScalar, typename ElseScalar, typename ConditionScalar>
Rasmus Munk Larsen2434cfd2023-12-06 12:02:41 -080039struct functor_traits<scalar_boolean_select_op<ThenScalar, ElseScalar, ConditionScalar>> {
Rasmus Munk Larsenad9fe062023-03-13 18:16:49 -070040 using Scalar = ThenScalar;
41 enum {
42 Cost = 1,
Rasmus Munk Larsen2434cfd2023-12-06 12:02:41 -080043 PacketAccess = is_same<ThenScalar, ElseScalar>::value && is_same<ConditionScalar, Scalar>::value &&
44 packet_traits<Scalar>::HasCmp
Rasmus Munk Larsenad9fe062023-03-13 18:16:49 -070045 };
46};
Googler45874d82019-08-21 12:06:47 -070047
Rasmus Munk Larsen2434cfd2023-12-06 12:02:41 -080048} // end namespace internal
Googler45874d82019-08-21 12:06:47 -070049
Rasmus Munk Larsen2434cfd2023-12-06 12:02:41 -080050} // end namespace Eigen
Googler45874d82019-08-21 12:06:47 -070051
Rasmus Munk Larsen2434cfd2023-12-06 12:02:41 -080052#endif // EIGEN_TERNARY_FUNCTORS_H