Eigen  5.0.1-dev+master
 
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 1 - log2(epsilon()) otherwise, as epsilon() = 2^(1 - digits).
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()))) + 1;
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
46namespace precision_detail {
47using std::ceil;
48using std::log2;
49
50template <typename T, typename = void>
51struct has_epsilon_digits : std::false_type {};
52
53// Detect the complete calculation: ADL can return another custom expression type.
54template <typename T>
55struct has_epsilon_digits<T, void_t<decltype(int(ceil(-log2(NumTraits<typename NumTraits<T>::Real>::epsilon()))) + 1)>>
56 : std::true_type {};
57} // namespace precision_detail
58
59// Only formatting tolerates unspecified precision; numerical users of digits() still require it.
60template <typename T, bool Known = std::numeric_limits<T>::is_specialized || NumTraits<T>::IsInteger ||
61 precision_detail::has_epsilon_digits<T>::value>
62struct decimal_digits_source {
63 EIGEN_DEVICE_FUNC constexpr static int run() { return default_digits_impl<T>::run(); }
64};
65
66template <typename T>
67struct decimal_digits_source<T, false> {
68 EIGEN_DEVICE_FUNC constexpr static int run() { return 0; }
69};
70
71// default implementation of digits10(), based on numeric_limits if specialized,
72// 0 for integer types, and floor((digits()-1)*log10(2)) otherwise.
73template <typename T, bool use_numeric_limits = std::numeric_limits<T>::is_specialized,
74 bool is_integer = NumTraits<T>::IsInteger>
75struct default_digits10_impl {
76 EIGEN_DEVICE_FUNC constexpr static int run() { return std::numeric_limits<T>::digits10; }
77};
78
79template <typename T>
80struct default_digits10_impl<T, false, false> // Floating point
81{
82 EIGEN_DEVICE_FUNC constexpr static int run() {
83 using std::floor;
84 using std::log10;
85 using Real = typename NumTraits<T>::Real;
86 const int digits = decimal_digits_source<Real>::run();
87 return digits > 0 ? int(floor((digits - 1) * log10(2))) : 0;
88 }
89};
90
91template <typename T>
92struct default_digits10_impl<T, false, true> // Integer
93{
94 EIGEN_DEVICE_FUNC constexpr static int run() { return 0; }
95};
96
97// default implementation of max_digits10(), based on numeric_limits if specialized,
98// 0 for integer types, and log10(2) * digits() + 1 otherwise.
99template <typename T, bool use_numeric_limits = std::numeric_limits<T>::is_specialized,
100 bool is_integer = NumTraits<T>::IsInteger>
101struct default_max_digits10_impl {
102 EIGEN_DEVICE_FUNC constexpr static int run() { return std::numeric_limits<T>::max_digits10; }
103};
104
105template <typename T>
106struct default_max_digits10_impl<T, false, false> // Floating point
107{
108 EIGEN_DEVICE_FUNC constexpr static int run() {
109 using std::ceil;
110 using std::log10;
111 using Real = typename NumTraits<T>::Real;
112 const int digits = decimal_digits_source<Real>::run();
113 return digits > 0 ? int(ceil(digits * log10(2) + 1)) : 0;
114 }
115};
116
117template <typename T>
118struct default_max_digits10_impl<T, false, true> // Integer
119{
120 EIGEN_DEVICE_FUNC constexpr static int run() { return 0; }
121};
122
123} // end namespace internal
124
125namespace numext {
126
128// std::bit_cast has no __device__ qualifier; in GPU device passes it requires relaxed constexpr
129// and, for non-literal return types such as Eigen::half, stays host-only and miscompiles under NVCC.
130#if defined(__cpp_lib_bit_cast) && __cpp_lib_bit_cast >= 201806L && !defined(EIGEN_GPU_COMPILE_PHASE)
131template <typename Tgt, typename Src>
132EIGEN_DEVICE_FUNC constexpr Tgt bit_cast(const Src& src) {
133 return std::bit_cast<Tgt>(src);
134}
135#elif EIGEN_HAS_BUILTIN(__builtin_bit_cast)
136template <typename Tgt, typename Src>
137EIGEN_DEVICE_FUNC constexpr Tgt bit_cast(const Src& src) {
138 EIGEN_STATIC_ASSERT(std::is_trivially_copyable<Src>::value, THIS_TYPE_IS_NOT_SUPPORTED)
139 EIGEN_STATIC_ASSERT(std::is_trivially_copyable<Tgt>::value, THIS_TYPE_IS_NOT_SUPPORTED)
140 EIGEN_STATIC_ASSERT(sizeof(Src) == sizeof(Tgt), THIS_TYPE_IS_NOT_SUPPORTED)
141 return __builtin_bit_cast(Tgt, src);
142}
143#else
144template <typename Tgt, typename Src>
145EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC Tgt bit_cast(const Src& src) {
146 // The behaviour of memcpy is not specified for non-trivially copyable types
147 EIGEN_STATIC_ASSERT(std::is_trivially_copyable<Src>::value, THIS_TYPE_IS_NOT_SUPPORTED)
148 EIGEN_STATIC_ASSERT(std::is_trivially_copyable<Tgt>::value && std::is_default_constructible<Tgt>::value,
149 THIS_TYPE_IS_NOT_SUPPORTED)
150 EIGEN_STATIC_ASSERT(sizeof(Src) == sizeof(Tgt), THIS_TYPE_IS_NOT_SUPPORTED)
151
152 Tgt tgt;
153 // Load src into registers first. This allows the memcpy to be elided by CUDA.
154 const Src staged = src;
155 EIGEN_USING_STD(memcpy)
156 memcpy(static_cast<void*>(&tgt), static_cast<const void*>(&staged), sizeof(Tgt));
157 return tgt;
158}
159#endif
160} // namespace numext
161
162// clang-format off
222// clang-format on
223template <typename T>
224struct GenericNumTraits {
225 enum {
226 IsInteger = std::numeric_limits<T>::is_integer,
227 IsSigned = std::numeric_limits<T>::is_signed,
228 IsComplex = 0,
229 RequireInitialization = internal::is_arithmetic<T>::value ? 0 : 1,
230 ReadCost = 1,
231 AddCost = 1,
232 MulCost = 1
233 };
234
235 using Real = T;
236 using NonInteger = std::conditional_t<IsInteger, std::conditional_t<sizeof(T) <= 2, float, double>, T>;
237 using Nested = T;
238 using Literal = T;
239
240 EIGEN_DEVICE_FUNC constexpr static Real epsilon() { return numext::numeric_limits<T>::epsilon(); }
241
242 EIGEN_DEVICE_FUNC constexpr static int digits10() { return internal::default_digits10_impl<T>::run(); }
243
244 EIGEN_DEVICE_FUNC constexpr static int max_digits10() { return internal::default_max_digits10_impl<T>::run(); }
245
246 EIGEN_DEVICE_FUNC constexpr static int digits() { return internal::default_digits_impl<T>::run(); }
247
248 EIGEN_DEVICE_FUNC constexpr static int min_exponent() { return numext::numeric_limits<T>::min_exponent; }
249
250 EIGEN_DEVICE_FUNC constexpr static int max_exponent() { return numext::numeric_limits<T>::max_exponent; }
251
252 EIGEN_DEVICE_FUNC constexpr static Real dummy_precision() {
253 // make sure to override this for floating-point types
254 return Real(0);
255 }
256
257 EIGEN_DEVICE_FUNC constexpr static T highest() { return (numext::numeric_limits<T>::max)(); }
258
259 EIGEN_DEVICE_FUNC constexpr static T lowest() { return (numext::numeric_limits<T>::lowest)(); }
260
261 EIGEN_DEVICE_FUNC constexpr static T infinity() { return numext::numeric_limits<T>::infinity(); }
262
263 EIGEN_DEVICE_FUNC constexpr static T quiet_NaN() { return numext::numeric_limits<T>::quiet_NaN(); }
264};
265
266template <typename T>
267struct NumTraits : GenericNumTraits<T> {};
268
269template <>
270struct NumTraits<float> : GenericNumTraits<float> {
271 EIGEN_DEVICE_FUNC constexpr static float dummy_precision() { return 1e-5f; }
272};
273
274template <>
275struct NumTraits<double> : GenericNumTraits<double> {
276 EIGEN_DEVICE_FUNC constexpr static double dummy_precision() { return 1e-12; }
277};
278
279// GPU devices treat `long double` as `double`.
280#ifndef EIGEN_GPU_COMPILE_PHASE
281template <>
282struct NumTraits<long double> : GenericNumTraits<long double> {
283 EIGEN_DEVICE_FUNC constexpr static long double dummy_precision() { return static_cast<long double>(1e-15l); }
284
285#if EIGEN_ARCH_PPC && (__LDBL_MANT_DIG__ == 106)
286 // PowerPC double double causes issues with some values
287 EIGEN_DEVICE_FUNC constexpr static long double epsilon() {
288 // 2^(-(__LDBL_MANT_DIG__)+1)
289 return static_cast<long double>(2.4651903288156618919116517665087e-32l);
290 }
291#endif
292};
293#endif
294
295template <typename Real_>
296struct NumTraits<std::complex<Real_> > : GenericNumTraits<std::complex<Real_> > {
297 using Real = Real_;
298 using Literal = typename NumTraits<Real_>::Literal;
299 enum {
300 IsComplex = 1,
301 IsSigned = NumTraits<Real_>::IsSigned,
302 RequireInitialization = NumTraits<Real_>::RequireInitialization,
303 ReadCost = 2 * NumTraits<Real_>::ReadCost,
304 AddCost = 2 * NumTraits<Real>::AddCost,
305 MulCost = 4 * NumTraits<Real>::MulCost + 2 * NumTraits<Real>::AddCost
306 };
307
308 EIGEN_DEVICE_FUNC constexpr static Real epsilon() { return NumTraits<Real>::epsilon(); }
309 EIGEN_DEVICE_FUNC constexpr static Real dummy_precision() { return NumTraits<Real>::dummy_precision(); }
310 EIGEN_DEVICE_FUNC constexpr static int digits10() { return NumTraits<Real>::digits10(); }
311 EIGEN_DEVICE_FUNC constexpr static int max_digits10() { return NumTraits<Real>::max_digits10(); }
312};
313
314template <typename Scalar, int Rows, int Cols, int Options, int MaxRows, int MaxCols>
315struct NumTraits<Array<Scalar, Rows, Cols, Options, MaxRows, MaxCols> > {
316 using ArrayType = Array<Scalar, Rows, Cols, Options, MaxRows, MaxCols>;
317 using RealScalar = typename NumTraits<Scalar>::Real;
318 using Real = Array<RealScalar, Rows, Cols, Options, MaxRows, MaxCols>;
319 using NonIntegerScalar = typename NumTraits<Scalar>::NonInteger;
320 using NonInteger = Array<NonIntegerScalar, Rows, Cols, Options, MaxRows, MaxCols>;
321 using Nested = ArrayType&;
322 using Literal = typename NumTraits<Scalar>::Literal;
323
324 enum {
325 IsComplex = NumTraits<Scalar>::IsComplex,
326 IsInteger = NumTraits<Scalar>::IsInteger,
327 IsSigned = NumTraits<Scalar>::IsSigned,
328 RequireInitialization = 1,
329 ReadCost = ArrayType::SizeAtCompileTime == Dynamic
330 ? HugeCost
331 : ArrayType::SizeAtCompileTime * int(NumTraits<Scalar>::ReadCost),
332 AddCost = ArrayType::SizeAtCompileTime == Dynamic ? HugeCost
333 : ArrayType::SizeAtCompileTime * int(NumTraits<Scalar>::AddCost),
334 MulCost = ArrayType::SizeAtCompileTime == Dynamic ? HugeCost
335 : ArrayType::SizeAtCompileTime * int(NumTraits<Scalar>::MulCost)
336 };
337
338 EIGEN_DEVICE_FUNC constexpr static RealScalar epsilon() { return NumTraits<RealScalar>::epsilon(); }
339 EIGEN_DEVICE_FUNC constexpr static RealScalar dummy_precision() { return NumTraits<RealScalar>::dummy_precision(); }
340
341 constexpr static int digits10() { return NumTraits<Scalar>::digits10(); }
342 constexpr static int max_digits10() { return NumTraits<Scalar>::max_digits10(); }
343};
344
345template <>
346struct NumTraits<std::string> : GenericNumTraits<std::string> {
347 enum { RequireInitialization = 1, ReadCost = HugeCost, AddCost = HugeCost, MulCost = HugeCost };
348
349 constexpr static int digits10() { return 0; }
350 constexpr static int max_digits10() { return 0; }
351
352 private:
353 static inline std::string epsilon();
354 static inline std::string dummy_precision();
355 static inline std::string lowest();
356 static inline std::string highest();
357 static inline std::string infinity();
358 static inline std::string quiet_NaN();
359};
360
361// Empty specialization for void to allow template specialization based on NumTraits<T>::Real with T==void and SFINAE.
362template <>
363struct NumTraits<void> {};
364
365template <>
366struct NumTraits<bool> : GenericNumTraits<bool> {};
367
368} // end namespace Eigen
369
370#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:267