Eigen  5.0.1
 
Loading...
Searching...
No Matches
TernaryFunctors.h
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// SPDX-License-Identifier: MPL-2.0
10
11#ifndef EIGEN_TERNARY_FUNCTORS_H
12#define EIGEN_TERNARY_FUNCTORS_H
13
14// IWYU pragma: private
15#include "../InternalHeaderCheck.h"
16
17namespace Eigen {
18
19namespace internal {
20
21//---------- associative ternary functors ----------
22
23template <typename ThenScalar, typename ElseScalar, typename ConditionScalar>
24struct scalar_boolean_select_op {
25 static constexpr bool ThenElseAreSame =
26 std::is_same<std::remove_const_t<ThenScalar>, std::remove_const_t<ElseScalar>>::value;
27 EIGEN_STATIC_ASSERT(ThenElseAreSame, THEN AND ELSE MUST BE SAME TYPE)
28 using Scalar = ThenScalar;
29 using result_type = Scalar;
30 EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE Scalar operator()(const ThenScalar& a, const ElseScalar& b,
31 const ConditionScalar& cond) const {
32 return cond == ConditionScalar(0) ? b : a;
33 }
34 template <typename Packet>
35 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Packet packetOp(const Packet& a, const Packet& b, const Packet& cond) const {
36 return pselect(pcmp_eq(cond, pzero(cond)), b, a);
37 }
38};
39
40template <typename ThenScalar, typename ElseScalar, typename ConditionScalar>
41struct functor_traits<scalar_boolean_select_op<ThenScalar, ElseScalar, ConditionScalar>> {
42 using Scalar = ThenScalar;
43 enum {
44 Cost = 1,
45 PacketAccess = std::is_same<ThenScalar, ElseScalar>::value && std::is_same<ConditionScalar, Scalar>::value &&
46 packet_traits<Scalar>::HasCmp
47 };
48};
49
50} // end namespace internal
51
52} // end namespace Eigen
53
54#endif // EIGEN_TERNARY_FUNCTORS_H