Eigen  3.4.90 (git rev 5a9f66fb35d03a4da9ef8976e67a61b30aa16dcf)
 
Loading...
Searching...
No Matches
NumTraits.h
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2006-2010 Benoit Jacob <[email protected]>
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_NUMTRAITS_H
11#define EIGEN_NUMTRAITS_H
12
13// IWYU pragma: private
14#include "./InternalHeaderCheck.h"
15
16namespace Eigen {
17
18namespace internal {
19
20// default implementation of digits(), based on numeric_limits if specialized,
21// 0 for integer types, and log2(epsilon()) otherwise.
22template <typename T, bool use_numeric_limits = std::numeric_limits<T>::is_specialized,
23 bool is_integer = NumTraits<T>::IsInteger>
24struct default_digits_impl {
25 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR static int run() { return std::numeric_limits<T>::digits; }
26};
27
28template <typename T>
29struct default_digits_impl<T, false, false> // Floating point
30{
31 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR static int run() {
32 using std::ceil;
33 using std::log2;
34 typedef typename NumTraits<T>::Real Real;
35 return int(ceil(-log2(NumTraits<Real>::epsilon())));
36 }
37};
38
39template <typename T>
40struct default_digits_impl<T, false, true> // Integer
41{
42 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR static int run() { return 0; }
43};
44
45// default implementation of digits10(), based on numeric_limits if specialized,
46// 0 for integer types, and floor((digits()-1)*log10(2)) otherwise.
47template <typename T, bool use_numeric_limits = std::numeric_limits<T>::is_specialized,
48 bool is_integer = NumTraits<T>::IsInteger>
49struct default_digits10_impl {
50 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR static int run() { return std::numeric_limits<T>::digits10; }
51};
52
53template <typename T>
54struct default_digits10_impl<T, false, false> // Floating point
55{
56 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR static int run() {
57 using std::floor;
58 using std::log10;
59 typedef typename NumTraits<T>::Real Real;
60 return int(floor((internal::default_digits_impl<Real>::run() - 1) * log10(2)));
61 }
62};
63
64template <typename T>
65struct default_digits10_impl<T, false, true> // Integer
66{
67 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR static int run() { return 0; }
68};
69
70// default implementation of max_digits10(), based on numeric_limits if specialized,
71// 0 for integer types, and log10(2) * digits() + 1 otherwise.
72template <typename T, bool use_numeric_limits = std::numeric_limits<T>::is_specialized,
73 bool is_integer = NumTraits<T>::IsInteger>
74struct default_max_digits10_impl {
75 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR static int run() { return std::numeric_limits<T>::max_digits10; }
76};
77
78template <typename T>
79struct default_max_digits10_impl<T, false, false> // Floating point
80{
81 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR static int run() {
82 using std::ceil;
83 using std::log10;
84 typedef typename NumTraits<T>::Real Real;
85 return int(ceil(internal::default_digits_impl<Real>::run() * log10(2) + 1));
86 }
87};
88
89template <typename T>
90struct default_max_digits10_impl<T, false, true> // Integer
91{
92 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR static int run() { return 0; }
93};
94
95} // end namespace internal
96
97namespace numext {
100// TODO: Replace by std::bit_cast (available in C++20)
101template <typename Tgt, typename Src>
102EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC Tgt bit_cast(const Src& src) {
103 // The behaviour of memcpy is not specified for non-trivially copyable types
104 EIGEN_STATIC_ASSERT(std::is_trivially_copyable<Src>::value, THIS_TYPE_IS_NOT_SUPPORTED)
105 EIGEN_STATIC_ASSERT(std::is_trivially_copyable<Tgt>::value && std::is_default_constructible<Tgt>::value,
106 THIS_TYPE_IS_NOT_SUPPORTED)
107 EIGEN_STATIC_ASSERT(sizeof(Src) == sizeof(Tgt), THIS_TYPE_IS_NOT_SUPPORTED)
108
109 Tgt tgt;
110 // Load src into registers first. This allows the memcpy to be elided by CUDA.
111 const Src staged = src;
112 EIGEN_USING_STD(memcpy)
113 memcpy(static_cast<void*>(&tgt), static_cast<const void*>(&staged), sizeof(Tgt));
114 return tgt;
115}
116} // namespace numext
117
171template <typename T>
172struct GenericNumTraits {
173 enum {
174 IsInteger = std::numeric_limits<T>::is_integer,
175 IsSigned = std::numeric_limits<T>::is_signed,
176 IsComplex = 0,
177 RequireInitialization = internal::is_arithmetic<T>::value ? 0 : 1,
178 ReadCost = 1,
179 AddCost = 1,
180 MulCost = 1
181 };
182
183 typedef T Real;
184 typedef std::conditional_t<IsInteger, std::conditional_t<sizeof(T) <= 2, float, double>, T> NonInteger;
185 typedef T Nested;
186 typedef T Literal;
187
188 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR static inline Real epsilon() { return numext::numeric_limits<T>::epsilon(); }
189
190 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR static inline int digits10() { return internal::default_digits10_impl<T>::run(); }
191
192 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR static inline int max_digits10() {
193 return internal::default_max_digits10_impl<T>::run();
194 }
195
196 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR static inline int digits() { return internal::default_digits_impl<T>::run(); }
197
198 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR static inline int min_exponent() { return numext::numeric_limits<T>::min_exponent; }
199
200 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR static inline int max_exponent() { return numext::numeric_limits<T>::max_exponent; }
201
202 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR static inline Real dummy_precision() {
203 // make sure to override this for floating-point types
204 return Real(0);
205 }
206
207 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR static inline T highest() { return (numext::numeric_limits<T>::max)(); }
208
209 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR static inline T lowest() { return (numext::numeric_limits<T>::lowest)(); }
210
211 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR static inline T infinity() { return numext::numeric_limits<T>::infinity(); }
212
213 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR static inline T quiet_NaN() { return numext::numeric_limits<T>::quiet_NaN(); }
214};
215
216template <typename T>
217struct NumTraits : GenericNumTraits<T> {};
218
219template <>
220struct NumTraits<float> : GenericNumTraits<float> {
221 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR static inline float dummy_precision() { return 1e-5f; }
222};
223
224template <>
225struct NumTraits<double> : GenericNumTraits<double> {
226 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR static inline double dummy_precision() { return 1e-12; }
227};
228
229// GPU devices treat `long double` as `double`.
230#ifndef EIGEN_GPU_COMPILE_PHASE
231template <>
232struct NumTraits<long double> : GenericNumTraits<long double> {
233 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR static inline long double dummy_precision() {
234 return static_cast<long double>(1e-15l);
235 }
236
237#if defined(EIGEN_ARCH_PPC) && (__LDBL_MANT_DIG__ == 106)
238 // PowerPC double double causes issues with some values
239 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR static inline long double epsilon() {
240 // 2^(-(__LDBL_MANT_DIG__)+1)
241 return static_cast<long double>(2.4651903288156618919116517665087e-32l);
242 }
243#endif
244};
245#endif
246
247template <typename Real_>
248struct NumTraits<std::complex<Real_> > : GenericNumTraits<std::complex<Real_> > {
249 typedef Real_ Real;
250 typedef typename NumTraits<Real_>::Literal Literal;
251 enum {
252 IsComplex = 1,
253 RequireInitialization = NumTraits<Real_>::RequireInitialization,
254 ReadCost = 2 * NumTraits<Real_>::ReadCost,
255 AddCost = 2 * NumTraits<Real>::AddCost,
256 MulCost = 4 * NumTraits<Real>::MulCost + 2 * NumTraits<Real>::AddCost
257 };
258
259 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR static inline Real epsilon() { return NumTraits<Real>::epsilon(); }
260 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR static inline Real dummy_precision() { return NumTraits<Real>::dummy_precision(); }
261 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR static inline int digits10() { return NumTraits<Real>::digits10(); }
262 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR static inline int max_digits10() { return NumTraits<Real>::max_digits10(); }
263};
264
265template <typename Scalar, int Rows, int Cols, int Options, int MaxRows, int MaxCols>
266struct NumTraits<Array<Scalar, Rows, Cols, Options, MaxRows, MaxCols> > {
267 typedef Array<Scalar, Rows, Cols, Options, MaxRows, MaxCols> ArrayType;
268 typedef typename NumTraits<Scalar>::Real RealScalar;
269 typedef Array<RealScalar, Rows, Cols, Options, MaxRows, MaxCols> Real;
270 typedef typename NumTraits<Scalar>::NonInteger NonIntegerScalar;
271 typedef Array<NonIntegerScalar, Rows, Cols, Options, MaxRows, MaxCols> NonInteger;
272 typedef ArrayType& Nested;
273 typedef typename NumTraits<Scalar>::Literal Literal;
274
275 enum {
276 IsComplex = NumTraits<Scalar>::IsComplex,
277 IsInteger = NumTraits<Scalar>::IsInteger,
278 IsSigned = NumTraits<Scalar>::IsSigned,
279 RequireInitialization = 1,
280 ReadCost = ArrayType::SizeAtCompileTime == Dynamic
281 ? HugeCost
282 : ArrayType::SizeAtCompileTime * int(NumTraits<Scalar>::ReadCost),
283 AddCost = ArrayType::SizeAtCompileTime == Dynamic ? HugeCost
284 : ArrayType::SizeAtCompileTime * int(NumTraits<Scalar>::AddCost),
285 MulCost = ArrayType::SizeAtCompileTime == Dynamic ? HugeCost
286 : ArrayType::SizeAtCompileTime * int(NumTraits<Scalar>::MulCost)
287 };
288
289 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR static inline RealScalar epsilon() { return NumTraits<RealScalar>::epsilon(); }
290 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR static inline RealScalar dummy_precision() {
291 return NumTraits<RealScalar>::dummy_precision();
292 }
293
294 EIGEN_CONSTEXPR
295 static inline int digits10() { return NumTraits<Scalar>::digits10(); }
296 EIGEN_CONSTEXPR
297 static inline int max_digits10() { return NumTraits<Scalar>::max_digits10(); }
298};
299
300template <>
301struct NumTraits<std::string> : GenericNumTraits<std::string> {
302 enum { RequireInitialization = 1, ReadCost = HugeCost, AddCost = HugeCost, MulCost = HugeCost };
303
304 EIGEN_CONSTEXPR
305 static inline int digits10() { return 0; }
306 EIGEN_CONSTEXPR
307 static inline int max_digits10() { return 0; }
308
309 private:
310 static inline std::string epsilon();
311 static inline std::string dummy_precision();
312 static inline std::string lowest();
313 static inline std::string highest();
314 static inline std::string infinity();
315 static inline std::string quiet_NaN();
316};
317
318// Empty specialization for void to allow template specialization based on NumTraits<T>::Real with T==void and SFINAE.
319template <>
320struct NumTraits<void> {};
321
322template <>
323struct NumTraits<bool> : GenericNumTraits<bool> {};
324
325} // end namespace Eigen
326
327#endif // EIGEN_NUMTRAITS_H
Namespace containing all symbols from the Eigen library.
Definition Core:137
const int HugeCost
Definition Constants.h:48
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_log2_op< typename Derived::Scalar >, const Derived > log2(const Eigen::ArrayBase< Derived > &x)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_log10_op< typename Derived::Scalar >, const Derived > log10(const Eigen::ArrayBase< Derived > &x)
const int Dynamic
Definition Constants.h:25
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_floor_op< typename Derived::Scalar >, const Derived > floor(const Eigen::ArrayBase< Derived > &x)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_ceil_op< typename Derived::Scalar >, const Derived > ceil(const Eigen::ArrayBase< Derived > &x)