Eigen  5.0.1-dev+master
 
Loading...
Searching...
No Matches
BinaryFunctors.h
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2008-2010 Gael Guennebaud <gael.guennebaud@inria.fr>
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_BINARY_FUNCTORS_H
12#define EIGEN_BINARY_FUNCTORS_H
13
14// IWYU pragma: private
15#include "../InternalHeaderCheck.h"
16
17namespace Eigen {
18
19namespace internal {
20
21//---------- associative binary functors ----------
22
23template <typename Arg1, typename Arg2>
24struct binary_op_base {
25 using first_argument_type = Arg1;
26 using second_argument_type = Arg2;
27};
28
34template <typename LhsScalar, typename RhsScalar>
35struct scalar_sum_op : binary_op_base<LhsScalar, RhsScalar> {
36 using result_type = typename ScalarBinaryOpTraits<LhsScalar, RhsScalar, scalar_sum_op>::ReturnType;
37#ifdef EIGEN_SCALAR_BINARY_OP_PLUGIN
38 scalar_sum_op(){EIGEN_SCALAR_BINARY_OP_PLUGIN}
39#endif
40 EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE result_type
41 operator()(const LhsScalar& a, const RhsScalar& b) const {
42 return a + b;
43 }
44 template <typename Packet>
45 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Packet packetOp(const Packet& a, const Packet& b) const {
46 return internal::padd(a, b);
47 }
48 template <typename Packet>
49 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE result_type predux(const Packet& a) const {
50 return internal::predux(a);
51 }
52};
53template <typename LhsScalar, typename RhsScalar>
54struct functor_traits<scalar_sum_op<LhsScalar, RhsScalar>> {
55 enum {
56 Cost = (int(NumTraits<LhsScalar>::AddCost) + int(NumTraits<RhsScalar>::AddCost)) / 2, // rough estimate!
57 PacketAccess = std::is_same<LhsScalar, RhsScalar>::value && packet_traits<LhsScalar>::HasAdd &&
58 packet_traits<RhsScalar>::HasAdd
59 // TODO: vectorize mixed sum
60 };
61};
62
63// Addition commutes for arithmetic and complex scalars. A user-defined Scalar may define a
64// non-commutative operator+ (e.g. concatenation), so it keeps the order-preserving default.
65template <typename Scalar>
66struct functor_is_commutative<scalar_sum_op<Scalar, Scalar>>
67 : bool_constant<is_arithmetic<Scalar>::value || NumTraits<Scalar>::IsComplex> {};
68
69template <>
70EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE bool scalar_sum_op<bool, bool>::operator()(const bool& a,
71 const bool& b) const {
72 return a | b;
73}
74
80template <typename LhsScalar, typename RhsScalar>
81struct scalar_product_op : binary_op_base<LhsScalar, RhsScalar> {
82 using result_type = typename ScalarBinaryOpTraits<LhsScalar, RhsScalar, scalar_product_op>::ReturnType;
83#ifdef EIGEN_SCALAR_BINARY_OP_PLUGIN
84 scalar_product_op(){EIGEN_SCALAR_BINARY_OP_PLUGIN}
85#endif
86 EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE result_type
87 operator()(const LhsScalar& a, const RhsScalar& b) const {
88 return internal::mul(a, b);
89 }
90 template <typename Packet>
91 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Packet packetOp(const Packet& a, const Packet& b) const {
92 return internal::pmul(a, b);
93 }
94 template <typename Packet>
95 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE result_type predux(const Packet& a) const {
96 return internal::predux_mul(a);
97 }
98};
99template <typename LhsScalar, typename RhsScalar>
100struct functor_traits<scalar_product_op<LhsScalar, RhsScalar>> {
101 enum {
102 Cost = (int(NumTraits<LhsScalar>::MulCost) + int(NumTraits<RhsScalar>::MulCost)) / 2, // rough estimate!
103 PacketAccess = std::is_same<LhsScalar, RhsScalar>::value && packet_traits<LhsScalar>::HasMul &&
104 packet_traits<RhsScalar>::HasMul
105 // TODO: vectorize mixed product
106 };
107};
108
109template <typename LhsScalar, typename RhsScalar, typename = void>
110struct scalar_product_commutativity
111 : bool_constant<is_arithmetic<LhsScalar>::value && is_arithmetic<RhsScalar>::value> {};
112
113template <typename LhsScalar, typename RhsScalar>
114struct scalar_product_commutativity<
115 LhsScalar, RhsScalar,
116 void_t<
117 decltype(ScalarBinaryOpTraits<LhsScalar, RhsScalar, scalar_product_op<LhsScalar, RhsScalar>>::IsCommutative)>>
118 : bool_constant<
119 ScalarBinaryOpTraits<LhsScalar, RhsScalar, scalar_product_op<LhsScalar, RhsScalar>>::IsCommutative> {};
120
121// IsComplex does not imply commutativity; custom scalars opt in through ScalarBinaryOpTraits.
122template <typename LhsScalar, typename RhsScalar>
123struct functor_is_commutative<scalar_product_op<LhsScalar, RhsScalar>>
124 : scalar_product_commutativity<LhsScalar, RhsScalar> {};
125
126template <typename RealScalar>
127struct functor_is_commutative<scalar_product_op<std::complex<RealScalar>, std::complex<RealScalar>>>
128 : bool_constant<is_arithmetic<RealScalar>::value ||
129 scalar_product_commutativity<std::complex<RealScalar>, std::complex<RealScalar>>::value> {};
130
131template <typename RealScalar>
132struct functor_is_commutative<scalar_product_op<std::complex<RealScalar>, RealScalar>>
133 : bool_constant<is_arithmetic<RealScalar>::value ||
134 scalar_product_commutativity<std::complex<RealScalar>, RealScalar>::value> {};
135
136template <typename RealScalar>
137struct functor_is_commutative<scalar_product_op<RealScalar, std::complex<RealScalar>>>
138 : bool_constant<is_arithmetic<RealScalar>::value ||
139 scalar_product_commutativity<RealScalar, std::complex<RealScalar>>::value> {};
140
141// Same as scalar_product_op, but its scalar path uses pmul instead of operator*. For complex scalars
142// pmul is Eigen's explicit (non-Annex-G) complex multiply, so this avoids std::complex::operator*,
143// which GCC lowers to the slow libgcc __mul?c3 call. The packet path (packetOp/predux) is inherited
144// unchanged -- it already uses pmul -- so the vectorized reduction and its precision are identical to
145// scalar_product_op (this only matters for the non-vectorized scalar tail). Used by the coeff-based
146// product's coeff(); pmul requires matching operand types, so mixed types use internal::mul,
147// which also keeps real*complex multiplication component-wise with MSVC's standard library.
148template <typename LhsScalar, typename RhsScalar>
149struct fast_mult_op : scalar_product_op<LhsScalar, RhsScalar> {
150 using result_type = typename scalar_product_op<LhsScalar, RhsScalar>::result_type;
151 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE result_type operator()(const LhsScalar& a, const RhsScalar& b) const {
152 return mul(a, b, bool_constant<std::is_same<LhsScalar, RhsScalar>::value>());
153 }
154
155 private:
156 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE result_type mul(const LhsScalar& a, const RhsScalar& b, std::true_type) const {
157 return pmul(a, b);
158 }
159 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE result_type mul(const LhsScalar& a, const RhsScalar& b, std::false_type) const {
160 return internal::mul(a, b);
161 }
162};
163template <typename LhsScalar, typename RhsScalar>
164struct functor_traits<fast_mult_op<LhsScalar, RhsScalar>> : functor_traits<scalar_product_op<LhsScalar, RhsScalar>> {};
165
166template <>
167EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE bool scalar_product_op<bool, bool>::operator()(const bool& a,
168 const bool& b) const {
169 return a & b;
170}
171
178template <typename LhsScalar, typename RhsScalar>
179struct scalar_conj_product_op : binary_op_base<LhsScalar, RhsScalar> {
180 enum { Conj = NumTraits<LhsScalar>::IsComplex };
181
182 using result_type = typename ScalarBinaryOpTraits<LhsScalar, RhsScalar, scalar_conj_product_op>::ReturnType;
183
184 EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE result_type operator()(const LhsScalar& a, const RhsScalar& b) const {
185 return conj_helper<LhsScalar, RhsScalar, Conj, false>().pmul(a, b);
186 }
187
188 template <typename Packet>
189 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Packet packetOp(const Packet& a, const Packet& b) const {
190 return conj_helper<Packet, Packet, Conj, false>().pmul(a, b);
191 }
192};
193template <typename LhsScalar, typename RhsScalar>
194struct functor_traits<scalar_conj_product_op<LhsScalar, RhsScalar>> {
195 enum {
196 Cost = NumTraits<LhsScalar>::MulCost,
197 PacketAccess = std::is_same<LhsScalar, RhsScalar>::value && packet_traits<LhsScalar>::HasMul
198 };
199};
200
206template <typename LhsScalar, typename RhsScalar, int NaNPropagation>
207struct scalar_min_op : binary_op_base<LhsScalar, RhsScalar> {
208 using result_type = typename ScalarBinaryOpTraits<LhsScalar, RhsScalar, scalar_min_op>::ReturnType;
209 EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE result_type operator()(const LhsScalar& a, const RhsScalar& b) const {
210 return internal::pmin<NaNPropagation>(a, b);
211 }
212 template <typename Packet>
213 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Packet packetOp(const Packet& a, const Packet& b) const {
214 return internal::pmin<NaNPropagation>(a, b);
215 }
216 template <typename Packet>
217 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE result_type predux(const Packet& a) const {
218 return internal::predux_min<NaNPropagation>(a);
219 }
220};
221
222template <typename LhsScalar, typename RhsScalar, int NaNPropagation>
223struct functor_traits<scalar_min_op<LhsScalar, RhsScalar, NaNPropagation>> {
224 enum {
225 Cost = (NumTraits<LhsScalar>::AddCost + NumTraits<RhsScalar>::AddCost) / 2,
226 PacketAccess = std::is_same<LhsScalar, RhsScalar>::value && packet_traits<LhsScalar>::HasMin
227 };
228};
229
230// min/max commute for arithmetic scalars only: PropagateNumbers and PropagateNaN treat NaN
231// operands symmetrically, PropagateFast leaves NaN results unspecified, and -0.0/+0.0 ties may
232// already resolve either way in the packet reduction paths. A custom scalar is excluded even
233// though it compares with operator<, because the generic std::min/std::max keep the first
234// operand when values compare equivalent, and equivalent custom values may be observably
235// distinct (e.g. carry a payload the comparison ignores).
236template <typename Scalar, int NaNPropagation>
237struct functor_is_commutative<scalar_min_op<Scalar, Scalar, NaNPropagation>> : is_arithmetic<Scalar> {};
238
244template <typename LhsScalar, typename RhsScalar, int NaNPropagation>
245struct scalar_max_op : binary_op_base<LhsScalar, RhsScalar> {
246 using result_type = typename ScalarBinaryOpTraits<LhsScalar, RhsScalar, scalar_max_op>::ReturnType;
247 EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE result_type operator()(const LhsScalar& a, const RhsScalar& b) const {
248 return internal::pmax<NaNPropagation>(a, b);
249 }
250 template <typename Packet>
251 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Packet packetOp(const Packet& a, const Packet& b) const {
252 return internal::pmax<NaNPropagation>(a, b);
253 }
254 template <typename Packet>
255 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE result_type predux(const Packet& a) const {
256 return internal::predux_max<NaNPropagation>(a);
257 }
258};
259
260template <typename LhsScalar, typename RhsScalar, int NaNPropagation>
261struct functor_traits<scalar_max_op<LhsScalar, RhsScalar, NaNPropagation>> {
262 enum {
263 Cost = (NumTraits<LhsScalar>::AddCost + NumTraits<RhsScalar>::AddCost) / 2,
264 PacketAccess = std::is_same<LhsScalar, RhsScalar>::value && packet_traits<LhsScalar>::HasMax
265 };
266};
267
268template <typename Scalar, int NaNPropagation>
269struct functor_is_commutative<scalar_max_op<Scalar, Scalar, NaNPropagation>> : is_arithmetic<Scalar> {};
270
275template <typename LhsScalar, typename RhsScalar, ComparisonName cmp, bool UseTypedComparators = false>
276struct scalar_cmp_op;
277
278template <typename LhsScalar, typename RhsScalar, ComparisonName cmp, bool UseTypedComparators>
279struct functor_traits<scalar_cmp_op<LhsScalar, RhsScalar, cmp, UseTypedComparators>> {
280 enum {
281 Cost = (NumTraits<LhsScalar>::AddCost + NumTraits<RhsScalar>::AddCost) / 2,
282 PacketAccess = (UseTypedComparators || std::is_same<LhsScalar, bool>::value) &&
283 std::is_same<LhsScalar, RhsScalar>::value && packet_traits<LhsScalar>::HasCmp
284 };
285};
286
287template <typename LhsScalar, typename RhsScalar, bool UseTypedComparators>
288struct scalar_cmp_op<LhsScalar, RhsScalar, cmp_EQ, UseTypedComparators> : binary_op_base<LhsScalar, RhsScalar> {
289 using result_type = std::conditional_t<UseTypedComparators, LhsScalar, bool>;
290 EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE result_type operator()(const LhsScalar& a, const RhsScalar& b) const {
291 return result_type(a == b);
292 }
293 template <typename Packet>
294 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Packet packetOp(const Packet& a, const Packet& b) const {
295 const Packet cst_one = pset1<Packet>(result_type(true));
296 return pand(pcmp_eq(a, b), cst_one);
297 }
298};
299
300template <typename LhsScalar, typename RhsScalar, bool UseTypedComparators>
301struct scalar_cmp_op<LhsScalar, RhsScalar, cmp_LT, UseTypedComparators> : binary_op_base<LhsScalar, RhsScalar> {
302 using result_type = std::conditional_t<UseTypedComparators, LhsScalar, bool>;
303 EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE result_type operator()(const LhsScalar& a, const RhsScalar& b) const {
304 return result_type(a < b);
305 }
306 template <typename Packet>
307 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Packet packetOp(const Packet& a, const Packet& b) const {
308 const Packet cst_one = pset1<Packet>(result_type(true));
309 return pand(pcmp_lt(a, b), cst_one);
310 }
311};
312
313template <typename LhsScalar, typename RhsScalar, bool UseTypedComparators>
314struct scalar_cmp_op<LhsScalar, RhsScalar, cmp_LE, UseTypedComparators> : binary_op_base<LhsScalar, RhsScalar> {
315 using result_type = std::conditional_t<UseTypedComparators, LhsScalar, bool>;
316 EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE result_type operator()(const LhsScalar& a, const RhsScalar& b) const {
317 return result_type(a <= b);
318 }
319 template <typename Packet>
320 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Packet packetOp(const Packet& a, const Packet& b) const {
321 const Packet cst_one = pset1<Packet>(result_type(true));
322 return pand(cst_one, pcmp_le(a, b));
323 }
324};
325
326template <typename LhsScalar, typename RhsScalar, bool UseTypedComparators>
327struct scalar_cmp_op<LhsScalar, RhsScalar, cmp_GT, UseTypedComparators> : binary_op_base<LhsScalar, RhsScalar> {
328 using result_type = std::conditional_t<UseTypedComparators, LhsScalar, bool>;
329 EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE result_type operator()(const LhsScalar& a, const RhsScalar& b) const {
330 return result_type(a > b);
331 }
332 template <typename Packet>
333 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Packet packetOp(const Packet& a, const Packet& b) const {
334 const Packet cst_one = pset1<Packet>(result_type(true));
335 return pand(cst_one, pcmp_lt(b, a));
336 }
337};
338
339template <typename LhsScalar, typename RhsScalar, bool UseTypedComparators>
340struct scalar_cmp_op<LhsScalar, RhsScalar, cmp_GE, UseTypedComparators> : binary_op_base<LhsScalar, RhsScalar> {
341 using result_type = std::conditional_t<UseTypedComparators, LhsScalar, bool>;
342 EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE result_type operator()(const LhsScalar& a, const RhsScalar& b) const {
343 return result_type(a >= b);
344 }
345 template <typename Packet>
346 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Packet packetOp(const Packet& a, const Packet& b) const {
347 const Packet cst_one = pset1<Packet>(result_type(true));
348 return pand(cst_one, pcmp_le(b, a));
349 }
350};
351
352template <typename LhsScalar, typename RhsScalar, bool UseTypedComparators>
353struct scalar_cmp_op<LhsScalar, RhsScalar, cmp_UNORD, UseTypedComparators> : binary_op_base<LhsScalar, RhsScalar> {
354 using result_type = std::conditional_t<UseTypedComparators, LhsScalar, bool>;
355 EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE result_type operator()(const LhsScalar& a, const RhsScalar& b) const {
356 return result_type(!(a <= b || b <= a));
357 }
358 template <typename Packet>
359 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Packet packetOp(const Packet& a, const Packet& b) const {
360 const Packet cst_one = pset1<Packet>(result_type(true));
361 return pandnot(cst_one, por(pcmp_le(a, b), pcmp_le(b, a)));
362 }
363};
364
365template <typename LhsScalar, typename RhsScalar, bool UseTypedComparators>
366struct scalar_cmp_op<LhsScalar, RhsScalar, cmp_NEQ, UseTypedComparators> : binary_op_base<LhsScalar, RhsScalar> {
367 using result_type = std::conditional_t<UseTypedComparators, LhsScalar, bool>;
368 EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE result_type operator()(const LhsScalar& a, const RhsScalar& b) const {
369 return result_type(a != b);
370 }
371 template <typename Packet>
372 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Packet packetOp(const Packet& a, const Packet& b) const {
373 const Packet cst_one = pset1<Packet>(result_type(true));
374 return pandnot(cst_one, pcmp_eq(a, b));
375 }
376};
377
383template <typename Scalar>
384struct scalar_hypot_op<Scalar, Scalar> : binary_op_base<Scalar, Scalar> {
385 EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE Scalar operator()(const Scalar& x, const Scalar& y) const {
386 // This functor is used by hypotNorm only for which it is faster to first apply abs
387 // on all coefficients prior to reduction through hypot.
388 // This way we avoid calling abs on positive and real entries, and this also permits
389 // to seamlessly handle complexes. Otherwise we would have to handle both real and complexes
390 // through the same functor...
391 return internal::positive_real_hypot(x, y);
392 }
393};
394template <typename Scalar>
395struct functor_traits<scalar_hypot_op<Scalar, Scalar>> {
396 enum {
397 Cost = 3 * NumTraits<Scalar>::AddCost + 2 * NumTraits<Scalar>::MulCost + 2 * scalar_div_cost<Scalar, false>::value,
398 PacketAccess = false
399 };
400};
401
406template <typename Scalar, typename Exponent>
407struct scalar_pow_op : binary_op_base<Scalar, Exponent> {
408 using result_type = typename ScalarBinaryOpTraits<Scalar, Exponent, scalar_pow_op>::ReturnType;
409#ifdef EIGEN_SCALAR_BINARY_OP_PLUGIN
410 scalar_pow_op() {
411 using LhsScalar = Scalar;
412 using RhsScalar = Exponent;
413 EIGEN_SCALAR_BINARY_OP_PLUGIN
414 }
415#endif
416
417 EIGEN_DEVICE_FUNC constexpr inline result_type operator()(const Scalar& a, const Exponent& b) const {
418 return numext::pow(a, b);
419 }
420
421 template <typename Packet>
422 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Packet packetOp(const Packet& a, const Packet& b) const {
423 return generic_pow(a, b);
424 }
425};
426
427template <typename Scalar, typename Exponent>
428struct functor_traits<scalar_pow_op<Scalar, Exponent>> {
429 enum {
430 Cost = 5 * NumTraits<Scalar>::MulCost,
431 PacketAccess = (!NumTraits<Scalar>::IsComplex && !NumTraits<Scalar>::IsInteger && packet_traits<Scalar>::HasPow)
432 };
433};
434
435//---------- non associative binary functors ----------
436
442template <typename LhsScalar, typename RhsScalar>
443struct scalar_difference_op : binary_op_base<LhsScalar, RhsScalar> {
444 EIGEN_STATIC_ASSERT((!std::is_same<LhsScalar, bool>::value || !std::is_same<RhsScalar, bool>::value),
445 BOOLEAN_SUBTRACTION_IS_NOT_SUPPORTED__CAST_TO_A_SIGNED_INTEGER_TYPE)
446 using result_type = typename ScalarBinaryOpTraits<LhsScalar, RhsScalar, scalar_difference_op>::ReturnType;
447#ifdef EIGEN_SCALAR_BINARY_OP_PLUGIN
448 scalar_difference_op(){EIGEN_SCALAR_BINARY_OP_PLUGIN}
449#endif
450 EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE result_type
451 operator()(const LhsScalar& a, const RhsScalar& b) const {
452 return a - b;
453 }
454 template <typename Packet>
455 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Packet packetOp(const Packet& a, const Packet& b) const {
456 return internal::psub(a, b);
457 }
458};
459template <typename LhsScalar, typename RhsScalar>
460struct functor_traits<scalar_difference_op<LhsScalar, RhsScalar>> {
461 enum {
462 Cost = (int(NumTraits<LhsScalar>::AddCost) + int(NumTraits<RhsScalar>::AddCost)) / 2,
463 PacketAccess = std::is_same<LhsScalar, RhsScalar>::value && packet_traits<LhsScalar>::HasSub &&
464 packet_traits<RhsScalar>::HasSub
465 };
466};
467
468template <typename Packet, bool IsInteger = NumTraits<typename unpacket_traits<Packet>::type>::IsInteger>
469struct maybe_raise_div_by_zero {
470 static EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE void run(Packet x) { EIGEN_UNUSED_VARIABLE(x); }
471};
472
473#ifndef EIGEN_GPU_COMPILE_PHASE
474template <typename Packet>
475struct maybe_raise_div_by_zero<Packet, true> {
476 static EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE void run(Packet x) {
477 if (EIGEN_PREDICT_FALSE(predux_any(pcmp_eq(x, pzero(x))))) {
478 // Use volatile variables to force a division by zero, which will
479 // result in the default platform behaviour (usually SIGFPE).
480 volatile typename unpacket_traits<Packet>::type zero = 0;
481 volatile typename unpacket_traits<Packet>::type val = 1;
482 val = val / zero;
483 }
484 }
485};
486#endif
487
493template <typename LhsScalar, typename RhsScalar>
494struct scalar_quotient_op : binary_op_base<LhsScalar, RhsScalar> {
495 using result_type = typename ScalarBinaryOpTraits<LhsScalar, RhsScalar, scalar_quotient_op>::ReturnType;
496#ifdef EIGEN_SCALAR_BINARY_OP_PLUGIN
497 scalar_quotient_op(){EIGEN_SCALAR_BINARY_OP_PLUGIN}
498#endif
499 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE result_type
500 operator()(const LhsScalar& a, const RhsScalar& b) const {
501 return numext::divide(a, b);
502 }
503 template <typename Packet>
504 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Packet packetOp(const Packet& a, const Packet& b) const {
505 return internal::pdiv(a, b);
506 }
507};
508template <typename LhsScalar, typename RhsScalar>
509struct functor_traits<scalar_quotient_op<LhsScalar, RhsScalar>> {
510 using result_type = typename scalar_quotient_op<LhsScalar, RhsScalar>::result_type;
511 enum {
512 PacketAccess = std::is_same<LhsScalar, RhsScalar>::value && packet_traits<LhsScalar>::HasDiv &&
513 packet_traits<RhsScalar>::HasDiv,
514 Cost = scalar_div_cost<result_type, PacketAccess>::value
515 };
516};
517
518// Packet16b is currently Eigen's only Boolean packet. Its loads contain valid bool objects, and its casts,
519// comparisons, and Boolean packet operations canonicalize every lane to 0 or 1. The direct bitwise fast paths below
520// rely on this invariant.
521
527template <typename LhsScalar, typename RhsScalar>
528struct scalar_modulus_op : binary_op_base<LhsScalar, RhsScalar> {
529 using result_type = typename ScalarBinaryOpTraits<LhsScalar, RhsScalar, scalar_modulus_op>::ReturnType;
530 static constexpr bool Enable = std::is_same<LhsScalar, RhsScalar>::value && NumTraits<LhsScalar>::IsInteger;
531 EIGEN_STATIC_ASSERT(Enable, "LhsScalar and RhsScalar must be the same integer type")
532#ifdef EIGEN_SCALAR_BINARY_OP_PLUGIN
533 scalar_modulus_op(){EIGEN_SCALAR_BINARY_OP_PLUGIN}
534#endif
535 EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE result_type
536 operator()(const LhsScalar& a, const RhsScalar& b) const {
537 return a % b;
538 }
539};
540template <typename LhsScalar, typename RhsScalar>
541struct functor_traits<scalar_modulus_op<LhsScalar, RhsScalar>> {
542 using result_type = typename scalar_modulus_op<LhsScalar, RhsScalar>::result_type;
543 static constexpr bool PacketAccess = false;
544 static constexpr int Cost = scalar_div_cost<result_type, false>::value;
545};
546
552template <typename Scalar>
553struct scalar_boolean_and_op {
554 using result_type = Scalar;
555 // `false` any value `a` that satisfies `a == Scalar(0)`
556 // `true` is the complement of `false`
557 EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE Scalar operator()(const Scalar& a, const Scalar& b) const {
558 return Scalar((a != Scalar(0)) && (b != Scalar(0)));
559 }
560 template <typename Packet>
561 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Packet packetOp(const Packet& a, const Packet& b) const {
562 EIGEN_IF_CONSTEXPR ((std::is_same<Scalar, bool>::value)) {
563 return pand(a, b);
564 } else {
565 const Packet cst_one = pset1<Packet>(Scalar(true));
566 // and(a,b) == !or(!a,!b)
567 Packet not_a = pcmp_eq(a, pzero(a));
568 Packet not_b = pcmp_eq(b, pzero(b));
569 Packet a_nand_b = por(not_a, not_b);
570 return pandnot(cst_one, a_nand_b);
571 }
572 }
573};
574// Keep bool logical functors eager so scalar evaluator loops remain branch-free.
575template <>
576EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE bool scalar_boolean_and_op<bool>::operator()(const bool& a,
577 const bool& b) const {
578 return a & b;
579}
580template <typename Scalar>
581struct functor_traits<scalar_boolean_and_op<Scalar>> {
582 enum { Cost = NumTraits<Scalar>::AddCost, PacketAccess = packet_traits<Scalar>::HasCmp };
583};
584
590template <typename Scalar>
591struct scalar_boolean_or_op {
592 using result_type = Scalar;
593 // `false` any value `a` that satisfies `a == Scalar(0)`
594 // `true` is the complement of `false`
595 EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE Scalar operator()(const Scalar& a, const Scalar& b) const {
596 return Scalar((a != Scalar(0)) || (b != Scalar(0)));
597 }
598 template <typename Packet>
599 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Packet packetOp(const Packet& a, const Packet& b) const {
600 EIGEN_IF_CONSTEXPR ((std::is_same<Scalar, bool>::value)) {
601 return por(a, b);
602 } else {
603 const Packet cst_one = pset1<Packet>(Scalar(true));
604 // if or(a,b) == 0, then a == 0 and b == 0
605 // or(a,b) == !nor(a,b)
606 Packet a_nor_b = pcmp_eq(por(a, b), pzero(a));
607 return pandnot(cst_one, a_nor_b);
608 }
609 }
610};
611template <>
612EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE bool scalar_boolean_or_op<bool>::operator()(const bool& a,
613 const bool& b) const {
614 return a | b;
615}
616template <typename Scalar>
617struct functor_traits<scalar_boolean_or_op<Scalar>> {
618 enum { Cost = NumTraits<Scalar>::AddCost, PacketAccess = packet_traits<Scalar>::HasCmp };
619};
620
626template <typename Scalar>
627struct scalar_boolean_xor_op {
628 using result_type = Scalar;
629 // `false` any value `a` that satisfies `a == Scalar(0)`
630 // `true` is the complement of `false`
631 EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE Scalar operator()(const Scalar& a, const Scalar& b) const {
632 return Scalar((a != Scalar(0)) != (b != Scalar(0)));
633 }
634 template <typename Packet>
635 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Packet packetOp(const Packet& a, const Packet& b) const {
636 EIGEN_IF_CONSTEXPR ((std::is_same<Scalar, bool>::value)) {
637 return pxor(a, b);
638 } else {
639 const Packet cst_one = pset1<Packet>(Scalar(true));
640 // xor(a,b) == xor(!a,!b)
641 Packet not_a = pcmp_eq(a, pzero(a));
642 Packet not_b = pcmp_eq(b, pzero(b));
643 Packet a_xor_b = pxor(not_a, not_b);
644 return pand(cst_one, a_xor_b);
645 }
646 }
647};
648template <typename Scalar>
649struct functor_traits<scalar_boolean_xor_op<Scalar>> {
650 enum { Cost = NumTraits<Scalar>::AddCost, PacketAccess = packet_traits<Scalar>::HasCmp };
651};
652
653template <typename Scalar, bool IsComplex = NumTraits<Scalar>::IsComplex>
654struct bitwise_binary_impl {
655 static constexpr size_t Size = sizeof(Scalar);
656 using uint_t = typename numext::get_integer_by_size<Size>::unsigned_type;
657 static EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE Scalar run_and(const Scalar& a, const Scalar& b) {
658 uint_t a_as_uint = numext::bit_cast<uint_t, Scalar>(a);
659 uint_t b_as_uint = numext::bit_cast<uint_t, Scalar>(b);
660 uint_t result = a_as_uint & b_as_uint;
661 return numext::bit_cast<Scalar, uint_t>(result);
662 }
663 static EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE Scalar run_or(const Scalar& a, const Scalar& b) {
664 uint_t a_as_uint = numext::bit_cast<uint_t, Scalar>(a);
665 uint_t b_as_uint = numext::bit_cast<uint_t, Scalar>(b);
666 uint_t result = a_as_uint | b_as_uint;
667 return numext::bit_cast<Scalar, uint_t>(result);
668 }
669 static EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE Scalar run_xor(const Scalar& a, const Scalar& b) {
670 uint_t a_as_uint = numext::bit_cast<uint_t, Scalar>(a);
671 uint_t b_as_uint = numext::bit_cast<uint_t, Scalar>(b);
672 uint_t result = a_as_uint ^ b_as_uint;
673 return numext::bit_cast<Scalar, uint_t>(result);
674 }
675};
676
677template <typename Scalar>
678struct bitwise_binary_impl<Scalar, true> {
679 using Real = typename NumTraits<Scalar>::Real;
680 static EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE Scalar run_and(const Scalar& a, const Scalar& b) {
681 Real real_result = bitwise_binary_impl<Real>::run_and(numext::real(a), numext::real(b));
682 Real imag_result = bitwise_binary_impl<Real>::run_and(numext::imag(a), numext::imag(b));
683 return Scalar(real_result, imag_result);
684 }
685 static EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE Scalar run_or(const Scalar& a, const Scalar& b) {
686 Real real_result = bitwise_binary_impl<Real>::run_or(numext::real(a), numext::real(b));
687 Real imag_result = bitwise_binary_impl<Real>::run_or(numext::imag(a), numext::imag(b));
688 return Scalar(real_result, imag_result);
689 }
690 static EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE Scalar run_xor(const Scalar& a, const Scalar& b) {
691 Real real_result = bitwise_binary_impl<Real>::run_xor(numext::real(a), numext::real(b));
692 Real imag_result = bitwise_binary_impl<Real>::run_xor(numext::imag(a), numext::imag(b));
693 return Scalar(real_result, imag_result);
694 }
695};
696
702template <typename Scalar>
703struct scalar_bitwise_and_op {
704 EIGEN_STATIC_ASSERT(!NumTraits<Scalar>::RequireInitialization,
705 BITWISE OPERATIONS MAY ONLY BE PERFORMED ON PLAIN DATA TYPES)
706 EIGEN_STATIC_ASSERT((!std::is_same<Scalar, bool>::value), DONT USE BITWISE OPS ON BOOLEAN TYPES)
707 using result_type = Scalar;
708 EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE Scalar operator()(const Scalar& a, const Scalar& b) const {
709 return bitwise_binary_impl<Scalar>::run_and(a, b);
710 }
711 template <typename Packet>
712 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Packet packetOp(const Packet& a, const Packet& b) const {
713 return pand(a, b);
714 }
715};
716template <typename Scalar>
717struct functor_traits<scalar_bitwise_and_op<Scalar>> {
718 enum { Cost = NumTraits<Scalar>::AddCost, PacketAccess = true };
719};
720
726template <typename Scalar>
727struct scalar_bitwise_or_op {
728 EIGEN_STATIC_ASSERT(!NumTraits<Scalar>::RequireInitialization,
729 BITWISE OPERATIONS MAY ONLY BE PERFORMED ON PLAIN DATA TYPES)
730 EIGEN_STATIC_ASSERT((!std::is_same<Scalar, bool>::value), DONT USE BITWISE OPS ON BOOLEAN TYPES)
731 using result_type = Scalar;
732 EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE Scalar operator()(const Scalar& a, const Scalar& b) const {
733 return bitwise_binary_impl<Scalar>::run_or(a, b);
734 }
735 template <typename Packet>
736 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Packet packetOp(const Packet& a, const Packet& b) const {
737 return por(a, b);
738 }
739};
740template <typename Scalar>
741struct functor_traits<scalar_bitwise_or_op<Scalar>> {
742 enum { Cost = NumTraits<Scalar>::AddCost, PacketAccess = true };
743};
744
750template <typename Scalar>
751struct scalar_bitwise_xor_op {
752 EIGEN_STATIC_ASSERT(!NumTraits<Scalar>::RequireInitialization,
753 BITWISE OPERATIONS MAY ONLY BE PERFORMED ON PLAIN DATA TYPES)
754 EIGEN_STATIC_ASSERT((!std::is_same<Scalar, bool>::value), DONT USE BITWISE OPS ON BOOLEAN TYPES)
755 using result_type = Scalar;
756 EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE Scalar operator()(const Scalar& a, const Scalar& b) const {
757 return bitwise_binary_impl<Scalar>::run_xor(a, b);
758 }
759 template <typename Packet>
760 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Packet packetOp(const Packet& a, const Packet& b) const {
761 return pxor(a, b);
762 }
763};
764template <typename Scalar>
765struct functor_traits<scalar_bitwise_xor_op<Scalar>> {
766 enum { Cost = NumTraits<Scalar>::AddCost, PacketAccess = true };
767};
768
774template <typename LhsScalar, typename RhsScalar>
775struct scalar_absolute_difference_op : binary_op_base<LhsScalar, RhsScalar> {
776 using result_type = typename ScalarBinaryOpTraits<LhsScalar, RhsScalar, scalar_absolute_difference_op>::ReturnType;
777#ifdef EIGEN_SCALAR_BINARY_OP_PLUGIN
778 scalar_absolute_difference_op(){EIGEN_SCALAR_BINARY_OP_PLUGIN}
779#endif
780 EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE result_type
781 operator()(const LhsScalar& a, const RhsScalar& b) const {
782 return numext::absdiff(a, b);
783 }
784 template <typename Packet>
785 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Packet packetOp(const Packet& a, const Packet& b) const {
786 return internal::pabsdiff(a, b);
787 }
788};
789template <typename LhsScalar, typename RhsScalar>
790struct functor_traits<scalar_absolute_difference_op<LhsScalar, RhsScalar>> {
791 enum {
792 Cost = (NumTraits<LhsScalar>::AddCost + NumTraits<RhsScalar>::AddCost) / 2,
793 PacketAccess = std::is_same<LhsScalar, RhsScalar>::value && packet_traits<LhsScalar>::HasAbsDiff
794 };
795};
796
797template <typename LhsScalar, typename RhsScalar>
798struct scalar_atan2_op {
799 using Scalar = LhsScalar;
800
801 static constexpr bool Enable =
802 std::is_same<LhsScalar, RhsScalar>::value && !NumTraits<Scalar>::IsInteger && !NumTraits<Scalar>::IsComplex;
803 EIGEN_STATIC_ASSERT(Enable, "LhsScalar and RhsScalar must be the same non-integer, non-complex type")
804
805 EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE Scalar operator()(const Scalar& y, const Scalar& x) const {
806 return numext::atan2(y, x);
807 }
808 template <typename Packet>
809 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Packet packetOp(const Packet& y, const Packet& x) const {
810 return internal::patan2(y, x);
811 }
812};
813
814template <typename LhsScalar, typename RhsScalar>
815struct functor_traits<scalar_atan2_op<LhsScalar, RhsScalar>> {
816 using Scalar = LhsScalar;
817 enum {
818 PacketAccess = std::is_same<LhsScalar, RhsScalar>::value && packet_traits<Scalar>::HasATan &&
819 packet_traits<Scalar>::HasDiv && !NumTraits<Scalar>::IsInteger && !NumTraits<Scalar>::IsComplex,
820 Cost = int(scalar_div_cost<Scalar, PacketAccess>::value) + int(functor_traits<scalar_atan_op<Scalar>>::Cost)
821 };
822};
823
824//---------- binary functors bound to a constant, thus appearing as a unary functor ----------
825
826// The following two classes permits to turn any binary functor into a unary one with one argument bound to a constant
827// value. They are analogues to the removed std::binder1st/binder2nd and are also compatible with packetOp.
828template <typename BinaryOp>
829struct bind1st_op : BinaryOp {
830 using first_argument_type = typename BinaryOp::first_argument_type;
831 using second_argument_type = typename BinaryOp::second_argument_type;
832 using result_type = typename BinaryOp::result_type;
833
834 EIGEN_DEVICE_FUNC constexpr explicit bind1st_op(const first_argument_type& val) : m_value(val) {}
835
836 EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE result_type operator()(const second_argument_type& b) const {
837 return BinaryOp::operator()(m_value, b);
838 }
839
840 template <typename Packet>
841 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Packet packetOp(const Packet& b) const {
842 return BinaryOp::packetOp(internal::pset1<Packet>(m_value), b);
843 }
844
845 first_argument_type m_value;
846};
847template <typename BinaryOp>
848struct functor_traits<bind1st_op<BinaryOp>> : functor_traits<BinaryOp> {};
849
850template <typename BinaryOp>
851struct bind2nd_op : BinaryOp {
852 using first_argument_type = typename BinaryOp::first_argument_type;
853 using second_argument_type = typename BinaryOp::second_argument_type;
854 using result_type = typename BinaryOp::result_type;
855
856 EIGEN_DEVICE_FUNC constexpr explicit bind2nd_op(const second_argument_type& val) : m_value(val) {}
857
858 EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE result_type operator()(const first_argument_type& a) const {
859 return BinaryOp::operator()(a, m_value);
860 }
861
862 template <typename Packet>
863 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Packet packetOp(const Packet& a) const {
864 return BinaryOp::packetOp(a, internal::pset1<Packet>(m_value));
865 }
866
867 second_argument_type m_value;
868};
869template <typename BinaryOp>
870struct functor_traits<bind2nd_op<BinaryOp>> : functor_traits<BinaryOp> {};
871
872} // end namespace internal
873
874} // end namespace Eigen
875
876#endif // EIGEN_BINARY_FUNCTORS_H