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