Googler | 45874d8 | 2019-08-21 12:06:47 -0700 | [diff] [blame] | 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 | |
Rasmus Munk Larsen | 66c640f | 2023-08-22 14:16:58 -0700 | [diff] [blame] | 13 | // IWYU pragma: private |
C. Antonio Sanchez | dcfd770 | 2021-10-26 11:41:24 -0700 | [diff] [blame] | 14 | #include "../InternalHeaderCheck.h" |
| 15 | |
Googler | 45874d8 | 2019-08-21 12:06:47 -0700 | [diff] [blame] | 16 | namespace Eigen { |
| 17 | |
| 18 | namespace internal { |
| 19 | |
| 20 | //---------- associative ternary functors ---------- |
| 21 | |
Rasmus Munk Larsen | ad9fe06 | 2023-03-13 18:16:49 -0700 | [diff] [blame] | 22 | template <typename ThenScalar, typename ElseScalar, typename ConditionScalar> |
| 23 | struct 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 Larsen | 2434cfd | 2023-12-06 12:02:41 -0800 | [diff] [blame] | 28 | EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar operator()(const ThenScalar& a, const ElseScalar& b, |
| 29 | const ConditionScalar& cond) const { |
Rasmus Munk Larsen | ad9fe06 | 2023-03-13 18:16:49 -0700 | [diff] [blame] | 30 | return cond == ConditionScalar(0) ? b : a; |
| 31 | } |
| 32 | template <typename Packet> |
Rasmus Munk Larsen | 2434cfd | 2023-12-06 12:02:41 -0800 | [diff] [blame] | 33 | EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Packet packetOp(const Packet& a, const Packet& b, const Packet& cond) const { |
Rasmus Munk Larsen | ad9fe06 | 2023-03-13 18:16:49 -0700 | [diff] [blame] | 34 | return pselect(pcmp_eq(cond, pzero(cond)), b, a); |
| 35 | } |
| 36 | }; |
Googler | 45874d8 | 2019-08-21 12:06:47 -0700 | [diff] [blame] | 37 | |
Rasmus Munk Larsen | ad9fe06 | 2023-03-13 18:16:49 -0700 | [diff] [blame] | 38 | template <typename ThenScalar, typename ElseScalar, typename ConditionScalar> |
Rasmus Munk Larsen | 2434cfd | 2023-12-06 12:02:41 -0800 | [diff] [blame] | 39 | struct functor_traits<scalar_boolean_select_op<ThenScalar, ElseScalar, ConditionScalar>> { |
Rasmus Munk Larsen | ad9fe06 | 2023-03-13 18:16:49 -0700 | [diff] [blame] | 40 | using Scalar = ThenScalar; |
| 41 | enum { |
| 42 | Cost = 1, |
Rasmus Munk Larsen | 2434cfd | 2023-12-06 12:02:41 -0800 | [diff] [blame] | 43 | PacketAccess = is_same<ThenScalar, ElseScalar>::value && is_same<ConditionScalar, Scalar>::value && |
| 44 | packet_traits<Scalar>::HasCmp |
Rasmus Munk Larsen | ad9fe06 | 2023-03-13 18:16:49 -0700 | [diff] [blame] | 45 | }; |
| 46 | }; |
Googler | 45874d8 | 2019-08-21 12:06:47 -0700 | [diff] [blame] | 47 | |
Rasmus Munk Larsen | 2434cfd | 2023-12-06 12:02:41 -0800 | [diff] [blame] | 48 | } // end namespace internal |
Googler | 45874d8 | 2019-08-21 12:06:47 -0700 | [diff] [blame] | 49 | |
Rasmus Munk Larsen | 2434cfd | 2023-12-06 12:02:41 -0800 | [diff] [blame] | 50 | } // end namespace Eigen |
Googler | 45874d8 | 2019-08-21 12:06:47 -0700 | [diff] [blame] | 51 | |
Rasmus Munk Larsen | 2434cfd | 2023-12-06 12:02:41 -0800 | [diff] [blame] | 52 | #endif // EIGEN_TERNARY_FUNCTORS_H |