Eigen  5.0.1-dev+master
 
Loading...
Searching...
No Matches
NullaryFunctors.h
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2008-2016 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_NULLARY_FUNCTORS_H
12#define EIGEN_NULLARY_FUNCTORS_H
13
14// IWYU pragma: private
15#include "../InternalHeaderCheck.h"
16
17namespace Eigen {
18
19namespace internal {
20
21template <typename Scalar>
22struct scalar_constant_op {
23 EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE scalar_constant_op(const Scalar& other) : m_other(other) {}
24 EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE Scalar operator()() const { return m_other; }
25 template <typename PacketType>
26 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketType packetOp() const {
27 return internal::pset1<PacketType>(m_other);
28 }
29 const Scalar m_other;
30};
31template <typename Scalar>
32struct functor_traits<scalar_constant_op<Scalar>> {
33 enum {
34 Cost = 0 /* as the constant value should be loaded in register only once for the whole expression */,
35 PacketAccess = packet_traits<Scalar>::Vectorizable,
36 IsRepeatable = true
37 };
38};
39
40template <typename Scalar>
41struct scalar_zero_op {
42 EIGEN_DEVICE_FUNC scalar_zero_op() = default;
43 EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE Scalar operator()() const { return Scalar(0); }
44 template <typename PacketType>
45 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketType packetOp() const {
46 return internal::pzero<PacketType>(PacketType());
47 }
48};
49template <typename Scalar>
50struct functor_traits<scalar_zero_op<Scalar>> : functor_traits<scalar_constant_op<Scalar>> {};
51
52template <typename Scalar>
53struct scalar_identity_op {
54 template <typename IndexType>
55 EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE Scalar operator()(IndexType row, IndexType col) const {
56 return row == col ? Scalar(1) : Scalar(0);
57 }
58};
59template <typename Scalar>
60struct functor_traits<scalar_identity_op<Scalar>> {
61 enum { Cost = NumTraits<Scalar>::AddCost, PacketAccess = false, IsRepeatable = true };
62};
63
64template <typename Scalar, bool IsInteger>
65struct linspaced_op_impl;
66
67template <typename Scalar>
68struct linspaced_op_impl<Scalar, /*IsInteger*/ false> {
69 using RealScalar = typename NumTraits<Scalar>::Real;
70
71 EIGEN_DEVICE_FUNC constexpr linspaced_op_impl(const Scalar& low, const Scalar& high, Index num_steps)
72 : m_low(low),
73 m_high(high),
74 m_size1(num_steps == 1 ? 1 : num_steps - 1),
75 m_step(num_steps == 1 ? Scalar() : Scalar((high - low) / RealScalar(num_steps - 1))),
76 m_flip(numext::abs(high) < numext::abs(low)) {}
77
78 template <typename IndexType>
79 EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE Scalar operator()(IndexType i) const {
80 if (m_flip)
81 return (i == 0) ? m_low : Scalar(m_high - RealScalar(m_size1 - i) * m_step);
82 else
83 return (i == m_size1) ? m_high : Scalar(m_low + RealScalar(i) * m_step);
84 }
85
86 template <typename Packet, typename IndexType>
87 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Packet packetOp(IndexType i) const {
88 // Principle:
89 // [low, ..., low] + ( [step, ..., step] * ( [i, ..., i] + [0, ..., size] ) )
90 Packet low = pset1<Packet>(m_low);
91 Packet high = pset1<Packet>(m_high);
92 Packet step = pset1<Packet>(m_step);
93 if (m_flip) {
94 Packet pi = plset<Packet>(Scalar(i - m_size1));
95 Packet res = pmadd(step, pi, high);
96 Packet mask = pcmp_lt(pzero(res), plset<Packet>(Scalar(i)));
97 return pselect<Packet>(mask, res, low);
98 } else {
99 Packet pi = plset<Packet>(Scalar(i));
100 Packet res = pmadd(step, pi, low);
101 Packet mask = pcmp_lt(pi, pset1<Packet>(Scalar(m_size1)));
102 return pselect<Packet>(mask, res, high);
103 }
104 }
105
106 const Scalar m_low;
107 const Scalar m_high;
108 const Index m_size1;
109 const Scalar m_step;
110 const bool m_flip;
111};
112
113// Built-in integers compute in the unsigned type of Scalar's width, widened to Index's, which holds num_steps, the
114// index i, |high - low| and the offset from low where high - low overflows a signed Scalar or wraps an unsigned one;
115// low +- offset then wraps to the coefficient. Other scalars, custom integers included, compute in Scalar.
116template <typename Scalar, bool IsBuiltin = std::is_integral<Scalar>::value>
117struct linspaced_integer_traits {
118 using Wide = std::make_unsigned_t<std::conditional_t<(sizeof(Scalar) < sizeof(Index)), Index, Scalar>>;
119 EIGEN_DEVICE_FUNC static constexpr Scalar narrow(const Wide& x) {
120 return static_cast<Scalar>(numext::bit_cast<std::make_signed_t<Wide>>(x));
121 }
122};
123
124template <typename Scalar>
125struct linspaced_integer_traits<Scalar, false> {
126 using Wide = Scalar;
127 EIGEN_DEVICE_FUNC static constexpr Scalar narrow(const Wide& x) { return x; }
128};
129
130template <typename Scalar>
131struct linspaced_op_impl<Scalar, /*IsInteger*/ true> {
132 using Traits = linspaced_integer_traits<Scalar>;
133 using Wide = typename Traits::Wide;
134
135 // Coefficient i is low +- i * m_step with m_step = |high - low| / (num_steps - 1), or, where values repeat because
136 // |high - low| + 1 < num_steps, low +- i / m_step with m_step = (num_steps + |high - low|) / (|high - low| + 1).
137 EIGEN_DEVICE_FUNC constexpr linspaced_op_impl(const Scalar& low, const Scalar& high, Index num_steps)
138 : m_low(low),
139 m_decreasing(high < low),
140 m_range(high < low ? Wide(Wide(low) - Wide(high)) : Wide(Wide(high) - Wide(low))),
141 m_use_divisor(num_steps > 1 && m_range < convert_index<Wide>(num_steps - 1)),
142 m_step(m_use_divisor ? (convert_index<Wide>(num_steps) + m_range) / (m_range + Wide(1))
143 : m_range / convert_index<Wide>(num_steps <= 1 ? 1 : num_steps - 1)) {}
144
145 template <typename IndexType>
146 EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE Scalar operator()(IndexType i) const {
147 const Wide offset = m_use_divisor ? Wide(convert_index<Wide>(i) / m_step) : Wide(convert_index<Wide>(i) * m_step);
148 return Traits::narrow(m_decreasing ? Wide(Wide(m_low) - offset) : Wide(Wide(m_low) + offset));
149 }
150
151 const Scalar m_low;
152 const bool m_decreasing;
153 const Wide m_range;
154 const bool m_use_divisor;
155 const Wide m_step;
156};
157
158// ----- Linspace functor ----------------------------------------------------------------
159
160// Forward declaration (we default to random access which does not really give
161// us a speed gain when using packet access but it allows to use the functor in
162// nested expressions).
163template <typename Scalar>
164struct linspaced_op;
165template <typename Scalar>
166struct functor_traits<linspaced_op<Scalar>> {
167 enum {
168 Cost = 1,
169 PacketAccess = (!NumTraits<Scalar>::IsInteger) && packet_traits<Scalar>::HasSetLinear,
170 /*&& ((!NumTraits<Scalar>::IsInteger) || packet_traits<Scalar>::HasDiv),*/ // <- vectorization for integer is
171 // currently disabled
172 IsRepeatable = true
173 };
174};
175template <typename Scalar>
176struct linspaced_op {
177 EIGEN_DEVICE_FUNC constexpr linspaced_op(const Scalar& low, const Scalar& high, Index num_steps)
178 : impl((num_steps == 1 ? high : low), high, num_steps) {}
179
180 template <typename IndexType>
181 EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE Scalar operator()(IndexType i) const {
182 return impl(i);
183 }
184
185 template <typename Packet, typename IndexType>
186 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Packet packetOp(IndexType i) const {
187 return impl.template packetOp<Packet>(i);
188 }
189
190 // This proxy object handles the actual required temporaries and the different
191 // implementations (integer vs. floating point).
192 const linspaced_op_impl<Scalar, NumTraits<Scalar>::IsInteger> impl;
193};
194
195template <typename Scalar>
196struct equalspaced_op {
197 EIGEN_DEVICE_FUNC constexpr equalspaced_op(const Scalar& start, const Scalar& step) : m_start(start), m_step(step) {}
198 template <typename IndexType>
199 EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE Scalar operator()(IndexType i) const {
200 return m_start + m_step * static_cast<Scalar>(i);
201 }
202 template <typename Packet, typename IndexType>
203 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Packet packetOp(IndexType i) const {
204 const Packet cst_start = pset1<Packet>(m_start);
205 const Packet cst_step = pset1<Packet>(m_step);
206 const Packet cst_lin0 = plset<Packet>(Scalar(0));
207 const Packet cst_offset = pmadd(cst_lin0, cst_step, cst_start);
208
209 Packet i_packet = pset1<Packet>(static_cast<Scalar>(i));
210 return pmadd(i_packet, cst_step, cst_offset);
211 }
212 const Scalar m_start;
213 const Scalar m_step;
214};
215
216template <typename Scalar>
217struct functor_traits<equalspaced_op<Scalar>> {
218 enum {
219 Cost = NumTraits<Scalar>::AddCost + NumTraits<Scalar>::MulCost,
220 PacketAccess =
221 packet_traits<Scalar>::HasSetLinear && packet_traits<Scalar>::HasMul && packet_traits<Scalar>::HasAdd,
222 IsRepeatable = true
223 };
224};
225
226// Linear access is automatically determined from the operator() prototypes available for the given functor.
227// If it exposes an operator()(i,j), then we assume the i and j coefficients are required independently
228// and linear access is not possible. In all other cases, linear access is enabled.
229// Users should not have to deal with this structure.
230template <typename Functor>
231using functor_has_linear_access = bool_constant<!has_binary_operator<Functor>::value>;
232
233} // end namespace internal
234
235} // end namespace Eigen
236
237#endif // EIGEN_NULLARY_FUNCTORS_H