Eigen  5.0.1
 
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 <jacob.benoit.1@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_NUMTRAITS_H
12#define EIGEN_NUMTRAITS_H
13
14// IWYU pragma: private
15#include "./InternalHeaderCheck.h"
16
17namespace Eigen {
18
19namespace internal {
20
21// default implementation of digits(), based on numeric_limits if specialized,
22// 0 for integer types, and log2(epsilon()) otherwise.
23template <typename T, bool use_numeric_limits = std::numeric_limits<T>::is_specialized,
24 bool is_integer = NumTraits<T>::IsInteger>
25struct default_digits_impl {
26 EIGEN_DEVICE_FUNC constexpr static int run() { return std::numeric_limits<T>::digits; }
27};
28
29template <typename T>
30struct default_digits_impl<T, false, false> // Floating point
31{
32 EIGEN_DEVICE_FUNC constexpr static int run() {
33 using std::ceil;
34 using std::log2;
35 using Real = typename NumTraits<T>::Real;
36 return int(ceil(-log2(NumTraits<Real>::epsilon())));
37 }
38};
39
40template <typename T>
41struct default_digits_impl<T, false, true> // Integer
42{
43 EIGEN_DEVICE_FUNC constexpr static int run() { return 0; }
44};
45
46// default implementation of digits10(), based on numeric_limits if specialized,
47// 0 for integer types, and floor((digits()-1)*log10(2)) otherwise.
48template <typename T, bool use_numeric_limits = std::numeric_limits<T>::is_specialized,
49 bool is_integer = NumTraits<T>::IsInteger>
50struct default_digits10_impl {
51 EIGEN_DEVICE_FUNC constexpr static int run() { return std::numeric_limits<T>::digits10; }
52};
53
54template <typename T>
55struct default_digits10_impl<T, false, false> // Floating point
56{
57 EIGEN_DEVICE_FUNC constexpr static int run() {
58 using std::floor;
59 using std::log10;
60 using Real = typename NumTraits<T>::Real;
61 return int(floor((internal::default_digits_impl<Real>::run() - 1) * log10(2)));
62 }
63};
64
65template <typename T>
66struct default_digits10_impl<T, false, true> // Integer
67{
68 EIGEN_DEVICE_FUNC constexpr static int run() { return 0; }
69};
70
71// default implementation of max_digits10(), based on numeric_limits if specialized,
72// 0 for integer types, and log10(2) * digits() + 1 otherwise.
73template <typename T, bool use_numeric_limits = std::numeric_limits<T>::is_specialized,
74 bool is_integer = NumTraits<T>::IsInteger>
75struct default_max_digits10_impl {
76 EIGEN_DEVICE_FUNC constexpr static int run() { return std::numeric_limits<T>::max_digits10; }
77};
78
79template <typename T>
80struct default_max_digits10_impl<T, false, false> // Floating point
81{
82 EIGEN_DEVICE_FUNC constexpr static int run() {
83 using std::ceil;
84 using std::log10;
85 using Real = typename NumTraits<T>::Real;
86 return int(ceil(internal::default_digits_impl<Real>::run() * log10(2) + 1));
87 }
88};
89
90template <typename T>
91struct default_max_digits10_impl<T, false, true> // Integer
92{
93 EIGEN_DEVICE_FUNC constexpr static int run() { return 0; }
94};
95
96} // end namespace internal
97
98namespace numext {
99
101#if defined(__cpp_lib_bit_cast) && __cpp_lib_bit_cast >= 201806L
102template <typename Tgt, typename Src>
103EIGEN_DEVICE_FUNC constexpr Tgt bit_cast(const Src& src) {
104 return std::bit_cast<Tgt>(src);
105}
106#elif EIGEN_HAS_BUILTIN(__builtin_bit_cast)
107template <typename Tgt, typename Src>
108EIGEN_DEVICE_FUNC constexpr Tgt bit_cast(const Src& src) {
109 EIGEN_STATIC_ASSERT(std::is_trivially_copyable<Src>::value, THIS_TYPE_IS_NOT_SUPPORTED)
110 EIGEN_STATIC_ASSERT(std::is_trivially_copyable<Tgt>::value, THIS_TYPE_IS_NOT_SUPPORTED)
111 EIGEN_STATIC_ASSERT(sizeof(Src) == sizeof(Tgt), THIS_TYPE_IS_NOT_SUPPORTED)
112 return __builtin_bit_cast(Tgt, src);
113}
114#else
115template <typename Tgt, typename Src>
116EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC Tgt bit_cast(const Src& src) {
117 // The behaviour of memcpy is not specified for non-trivially copyable types
118 EIGEN_STATIC_ASSERT(std::is_trivially_copyable<Src>::value, THIS_TYPE_IS_NOT_SUPPORTED)
119 EIGEN_STATIC_ASSERT(std::is_trivially_copyable<Tgt>::value && std::is_default_constructible<Tgt>::value,
120 THIS_TYPE_IS_NOT_SUPPORTED)
121 EIGEN_STATIC_ASSERT(sizeof(Src) == sizeof(Tgt), THIS_TYPE_IS_NOT_SUPPORTED)
122
123 Tgt tgt;
124 // Load src into registers first. This allows the memcpy to be elided by CUDA.
125 const Src staged = src;
126 EIGEN_USING_STD(memcpy)
127 memcpy(static_cast<void*>(&tgt), static_cast<const void*>(&staged), sizeof(Tgt));
128 return tgt;
129}
130#endif
131} // namespace numext
132
133// clang-format off
188// clang-format on
189template <typename T>
190struct GenericNumTraits {
191 enum {
192 IsInteger = std::numeric_limits<T>::is_integer,
193 IsSigned = std::numeric_limits<T>::is_signed,
194 IsComplex = 0,
195 RequireInitialization = internal::is_arithmetic<T>::value ? 0 : 1,
196 ReadCost = 1,
197 AddCost = 1,
198 MulCost = 1
199 };
200
201 using Real = T;
202 using NonInteger = std::conditional_t<IsInteger, std::conditional_t<sizeof(T) <= 2, float, double>, T>;
203 using Nested = T;
204 using Literal = T;
205
206 EIGEN_DEVICE_FUNC constexpr static Real epsilon() { return numext::numeric_limits<T>::epsilon(); }
207
208 EIGEN_DEVICE_FUNC constexpr static int digits10() { return internal::default_digits10_impl<T>::run(); }
209
210 EIGEN_DEVICE_FUNC constexpr static int max_digits10() { return internal::default_max_digits10_impl<T>::run(); }
211
212 EIGEN_DEVICE_FUNC constexpr static int digits() { return internal::default_digits_impl<T>::run(); }
213
214 EIGEN_DEVICE_FUNC constexpr static int min_exponent() { return numext::numeric_limits<T>::min_exponent; }
215
216 EIGEN_DEVICE_FUNC constexpr static int max_exponent() { return numext::numeric_limits<T>::max_exponent; }
217
218 EIGEN_DEVICE_FUNC constexpr static Real dummy_precision() {
219 // make sure to override this for floating-point types
220 return Real(0);
221 }
222
223 EIGEN_DEVICE_FUNC constexpr static T highest() { return (numext::numeric_limits<T>::max)(); }
224
225 EIGEN_DEVICE_FUNC constexpr static T lowest() { return (numext::numeric_limits<T>::lowest)(); }
226
227 EIGEN_DEVICE_FUNC constexpr static T infinity() { return numext::numeric_limits<T>::infinity(); }
228
229 EIGEN_DEVICE_FUNC constexpr static T quiet_NaN() { return numext::numeric_limits<T>::quiet_NaN(); }
230};
231
232template <typename T>
233struct NumTraits : GenericNumTraits<T> {};
234
235template <>
236struct NumTraits<float> : GenericNumTraits<float> {
237 EIGEN_DEVICE_FUNC constexpr static float dummy_precision() { return 1e-5f; }
238};
239
240template <>
241struct NumTraits<double> : GenericNumTraits<double> {
242 EIGEN_DEVICE_FUNC constexpr static double dummy_precision() { return 1e-12; }
243};
244
245// GPU devices treat `long double` as `double`.
246#ifndef EIGEN_GPU_COMPILE_PHASE
247template <>
248struct NumTraits<long double> : GenericNumTraits<long double> {
249 EIGEN_DEVICE_FUNC constexpr static long double dummy_precision() { return static_cast<long double>(1e-15l); }
250
251#if defined(EIGEN_ARCH_PPC) && (__LDBL_MANT_DIG__ == 106)
252 // PowerPC double double causes issues with some values
253 EIGEN_DEVICE_FUNC constexpr static long double epsilon() {
254 // 2^(-(__LDBL_MANT_DIG__)+1)
255 return static_cast<long double>(2.4651903288156618919116517665087e-32l);
256 }
257#endif
258};
259#endif
260
261template <typename Real_>
262struct NumTraits<std::complex<Real_> > : GenericNumTraits<std::complex<Real_> > {
263 using Real = Real_;
264 using Literal = typename NumTraits<Real_>::Literal;
265 enum {
266 IsComplex = 1,
267 IsSigned = NumTraits<Real_>::IsSigned,
268 RequireInitialization = NumTraits<Real_>::RequireInitialization,
269 ReadCost = 2 * NumTraits<Real_>::ReadCost,
270 AddCost = 2 * NumTraits<Real>::AddCost,
271 MulCost = 4 * NumTraits<Real>::MulCost + 2 * NumTraits<Real>::AddCost
272 };
273
274 EIGEN_DEVICE_FUNC constexpr static Real epsilon() { return NumTraits<Real>::epsilon(); }
275 EIGEN_DEVICE_FUNC constexpr static Real dummy_precision() { return NumTraits<Real>::dummy_precision(); }
276 EIGEN_DEVICE_FUNC constexpr static int digits10() { return NumTraits<Real>::digits10(); }
277 EIGEN_DEVICE_FUNC constexpr static int max_digits10() { return NumTraits<Real>::max_digits10(); }
278};
279
280template <typename Scalar, int Rows, int Cols, int Options, int MaxRows, int MaxCols>
281struct NumTraits<Array<Scalar, Rows, Cols, Options, MaxRows, MaxCols> > {
282 using ArrayType = Array<Scalar, Rows, Cols, Options, MaxRows, MaxCols>;
283 using RealScalar = typename NumTraits<Scalar>::Real;
284 using Real = Array<RealScalar, Rows, Cols, Options, MaxRows, MaxCols>;
285 using NonIntegerScalar = typename NumTraits<Scalar>::NonInteger;
286 using NonInteger = Array<NonIntegerScalar, Rows, Cols, Options, MaxRows, MaxCols>;
287 using Nested = ArrayType&;
288 using Literal = typename NumTraits<Scalar>::Literal;
289
290 enum {
291 IsComplex = NumTraits<Scalar>::IsComplex,
292 IsInteger = NumTraits<Scalar>::IsInteger,
293 IsSigned = NumTraits<Scalar>::IsSigned,
294 RequireInitialization = 1,
295 ReadCost = ArrayType::SizeAtCompileTime == Dynamic
296 ? HugeCost
297 : ArrayType::SizeAtCompileTime * int(NumTraits<Scalar>::ReadCost),
298 AddCost = ArrayType::SizeAtCompileTime == Dynamic ? HugeCost
299 : ArrayType::SizeAtCompileTime * int(NumTraits<Scalar>::AddCost),
300 MulCost = ArrayType::SizeAtCompileTime == Dynamic ? HugeCost
301 : ArrayType::SizeAtCompileTime * int(NumTraits<Scalar>::MulCost)
302 };
303
304 EIGEN_DEVICE_FUNC constexpr static RealScalar epsilon() { return NumTraits<RealScalar>::epsilon(); }
305 EIGEN_DEVICE_FUNC constexpr static RealScalar dummy_precision() { return NumTraits<RealScalar>::dummy_precision(); }
306
307 constexpr static int digits10() { return NumTraits<Scalar>::digits10(); }
308 constexpr static int max_digits10() { return NumTraits<Scalar>::max_digits10(); }
309};
310
311template <>
312struct NumTraits<std::string> : GenericNumTraits<std::string> {
313 enum { RequireInitialization = 1, ReadCost = HugeCost, AddCost = HugeCost, MulCost = HugeCost };
314
315 constexpr static int digits10() { return 0; }
316 constexpr static int max_digits10() { return 0; }
317
318 private:
319 static inline std::string epsilon();
320 static inline std::string dummy_precision();
321 static inline std::string lowest();
322 static inline std::string highest();
323 static inline std::string infinity();
324 static inline std::string quiet_NaN();
325};
326
327// Empty specialization for void to allow template specialization based on NumTraits<T>::Real with T==void and SFINAE.
328template <>
329struct NumTraits<void> {};
330
331template <>
332struct NumTraits<bool> : GenericNumTraits<bool> {};
333
334} // end namespace Eigen
335
336#endif // EIGEN_NUMTRAITS_H
General-purpose arrays with easy API for coefficient-wise operations.
Definition Array.h:55
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition NumTraits.h:233