Eigen  3.4.90 (git rev 67eeba6e720c5745abc77ae6c92ce0a44aa7b7ae)
Memory.h
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2008-2015 Gael Guennebaud <gael.guennebaud@inria.fr>
5 // Copyright (C) 2008-2009 Benoit Jacob <jacob.benoit.1@gmail.com>
6 // Copyright (C) 2009 Kenneth Riddile <kfriddile@yahoo.com>
7 // Copyright (C) 2010 Hauke Heibel <hauke.heibel@gmail.com>
8 // Copyright (C) 2010 Thomas Capricelli <orzel@freehackers.org>
9 // Copyright (C) 2013 Pavel Holoborodko <pavel@holoborodko.com>
10 //
11 // This Source Code Form is subject to the terms of the Mozilla
12 // Public License v. 2.0. If a copy of the MPL was not distributed
13 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
14 
15 
16 /*****************************************************************************
17 *** Platform checks for aligned malloc functions ***
18 *****************************************************************************/
19 
20 #ifndef EIGEN_MEMORY_H
21 #define EIGEN_MEMORY_H
22 
23 #ifndef EIGEN_MALLOC_ALREADY_ALIGNED
24 
25 // Try to determine automatically if malloc is already aligned.
26 
27 // On 64-bit systems, glibc's malloc returns 16-byte-aligned pointers, see:
28 // http://www.gnu.org/s/libc/manual/html_node/Aligned-Memory-Blocks.html
29 // This is true at least since glibc 2.8.
30 // This leaves the question how to detect 64-bit. According to this document,
31 // http://gcc.fyxm.net/summit/2003/Porting%20to%2064%20bit.pdf
32 // page 114, "[The] LP64 model [...] is used by all 64-bit UNIX ports" so it's indeed
33 // quite safe, at least within the context of glibc, to equate 64-bit with LP64.
34 #if defined(__GLIBC__) && ((__GLIBC__>=2 && __GLIBC_MINOR__ >= 8) || __GLIBC__>2) \
35  && defined(__LP64__) && ! defined( __SANITIZE_ADDRESS__ ) && (EIGEN_DEFAULT_ALIGN_BYTES == 16)
36  #define EIGEN_GLIBC_MALLOC_ALREADY_ALIGNED 1
37 #else
38  #define EIGEN_GLIBC_MALLOC_ALREADY_ALIGNED 0
39 #endif
40 
41 // FreeBSD 6 seems to have 16-byte aligned malloc
42 // See http://svn.freebsd.org/viewvc/base/stable/6/lib/libc/stdlib/malloc.c?view=markup
43 // FreeBSD 7 seems to have 16-byte aligned malloc except on ARM and MIPS architectures
44 // See http://svn.freebsd.org/viewvc/base/stable/7/lib/libc/stdlib/malloc.c?view=markup
45 #if defined(__FreeBSD__) && !(EIGEN_ARCH_ARM || EIGEN_ARCH_MIPS) && (EIGEN_DEFAULT_ALIGN_BYTES == 16)
46  #define EIGEN_FREEBSD_MALLOC_ALREADY_ALIGNED 1
47 #else
48  #define EIGEN_FREEBSD_MALLOC_ALREADY_ALIGNED 0
49 #endif
50 
51 #if (EIGEN_OS_MAC && (EIGEN_DEFAULT_ALIGN_BYTES == 16)) \
52  || (EIGEN_OS_WIN64 && (EIGEN_DEFAULT_ALIGN_BYTES == 16)) \
53  || EIGEN_GLIBC_MALLOC_ALREADY_ALIGNED \
54  || EIGEN_FREEBSD_MALLOC_ALREADY_ALIGNED
55  #define EIGEN_MALLOC_ALREADY_ALIGNED 1
56 #else
57  #define EIGEN_MALLOC_ALREADY_ALIGNED 0
58 #endif
59 
60 #endif
61 
62 #include "../InternalHeaderCheck.h"
63 
64 namespace Eigen {
65 
66 namespace internal {
67 
68 EIGEN_DEVICE_FUNC
69 inline void throw_std_bad_alloc()
70 {
71  #ifdef EIGEN_EXCEPTIONS
72  throw std::bad_alloc();
73  #else
74  std::size_t huge = static_cast<std::size_t>(-1);
75  #if defined(EIGEN_HIPCC)
76  //
77  // calls to "::operator new" are to be treated as opaque function calls (i.e no inlining),
78  // and as a consequence the code in the #else block triggers the hipcc warning :
79  // "no overloaded function has restriction specifiers that are compatible with the ambient context"
80  //
81  // "throw_std_bad_alloc" has the EIGEN_DEVICE_FUNC attribute, so it seems that hipcc expects
82  // the same on "operator new"
83  // Reverting code back to the old version in this #if block for the hipcc compiler
84  //
85  new int[huge];
86  #else
87  void* unused = ::operator new(huge);
88  EIGEN_UNUSED_VARIABLE(unused);
89  #endif
90  #endif
91 }
92 
93 /*****************************************************************************
94 *** Implementation of handmade aligned functions ***
95 *****************************************************************************/
96 
97 /* ----- Hand made implementations of aligned malloc/free and realloc ----- */
98 
102 EIGEN_DEVICE_FUNC inline void* handmade_aligned_malloc(std::size_t size, std::size_t alignment = EIGEN_DEFAULT_ALIGN_BYTES)
103 {
104  eigen_assert(alignment >= sizeof(void*) && (alignment & (alignment-1)) == 0 && "Alignment must be at least sizeof(void*) and a power of 2");
105 
106  EIGEN_USING_STD(malloc)
107  void *original = malloc(size+alignment);
108 
109  if (original == 0) return 0;
110  void *aligned = reinterpret_cast<void*>((reinterpret_cast<std::size_t>(original) & ~(std::size_t(alignment-1))) + alignment);
111  *(reinterpret_cast<void**>(aligned) - 1) = original;
112  return aligned;
113 }
114 
116 EIGEN_DEVICE_FUNC inline void handmade_aligned_free(void *ptr)
117 {
118  if (ptr) {
119  EIGEN_USING_STD(free)
120  free(*(reinterpret_cast<void**>(ptr) - 1));
121  }
122 }
123 
129 inline void* handmade_aligned_realloc(void* ptr, std::size_t size, std::size_t = 0)
130 {
131  if (ptr == 0) return handmade_aligned_malloc(size);
132  void *original = *(reinterpret_cast<void**>(ptr) - 1);
133  std::ptrdiff_t previous_offset = static_cast<char *>(ptr)-static_cast<char *>(original);
134  original = std::realloc(original,size+EIGEN_DEFAULT_ALIGN_BYTES);
135  if (original == 0) return 0;
136  void *aligned = reinterpret_cast<void*>((reinterpret_cast<std::size_t>(original) & ~(std::size_t(EIGEN_DEFAULT_ALIGN_BYTES-1))) + EIGEN_DEFAULT_ALIGN_BYTES);
137  void *previous_aligned = static_cast<char *>(original)+previous_offset;
138  if(aligned!=previous_aligned)
139  std::memmove(aligned, previous_aligned, size);
140 
141  *(reinterpret_cast<void**>(aligned) - 1) = original;
142  return aligned;
143 }
144 
145 /*****************************************************************************
146 *** Implementation of portable aligned versions of malloc/free/realloc ***
147 *****************************************************************************/
148 
149 #ifdef EIGEN_NO_MALLOC
150 EIGEN_DEVICE_FUNC inline void check_that_malloc_is_allowed()
151 {
152  eigen_assert(false && "heap allocation is forbidden (EIGEN_NO_MALLOC is defined)");
153 }
154 #elif defined EIGEN_RUNTIME_NO_MALLOC
155 EIGEN_DEVICE_FUNC inline bool is_malloc_allowed_impl(bool update, bool new_value = false)
156 {
157  static bool value = true;
158  if (update == 1)
159  value = new_value;
160  return value;
161 }
162 EIGEN_DEVICE_FUNC inline bool is_malloc_allowed() { return is_malloc_allowed_impl(false); }
163 EIGEN_DEVICE_FUNC inline bool set_is_malloc_allowed(bool new_value) { return is_malloc_allowed_impl(true, new_value); }
164 EIGEN_DEVICE_FUNC inline void check_that_malloc_is_allowed()
165 {
166  eigen_assert(is_malloc_allowed() && "heap allocation is forbidden (EIGEN_RUNTIME_NO_MALLOC is defined and g_is_malloc_allowed is false)");
167 }
168 #else
169 EIGEN_DEVICE_FUNC inline void check_that_malloc_is_allowed()
170 {}
171 #endif
172 
176 EIGEN_DEVICE_FUNC inline void* aligned_malloc(std::size_t size)
177 {
178  check_that_malloc_is_allowed();
179 
180  void *result;
181  #if (EIGEN_DEFAULT_ALIGN_BYTES==0) || EIGEN_MALLOC_ALREADY_ALIGNED
182 
183  EIGEN_USING_STD(malloc)
184  result = malloc(size);
185 
186  #if EIGEN_DEFAULT_ALIGN_BYTES==16
187  eigen_assert((size<16 || (std::size_t(result)%16)==0) && "System's malloc returned an unaligned pointer. Compile with EIGEN_MALLOC_ALREADY_ALIGNED=0 to fallback to handmade aligned memory allocator.");
188  #endif
189  #else
190  result = handmade_aligned_malloc(size);
191  #endif
192 
193  if(!result && size)
194  throw_std_bad_alloc();
195 
196  return result;
197 }
198 
200 EIGEN_DEVICE_FUNC inline void aligned_free(void *ptr)
201 {
202  #if (EIGEN_DEFAULT_ALIGN_BYTES==0) || EIGEN_MALLOC_ALREADY_ALIGNED
203 
204  EIGEN_USING_STD(free)
205  free(ptr);
206 
207  #else
208  handmade_aligned_free(ptr);
209  #endif
210 }
211 
217 inline void* aligned_realloc(void *ptr, std::size_t new_size, std::size_t old_size)
218 {
219  EIGEN_UNUSED_VARIABLE(old_size)
220 
221  void *result;
222 #if (EIGEN_DEFAULT_ALIGN_BYTES==0) || EIGEN_MALLOC_ALREADY_ALIGNED
223  result = std::realloc(ptr,new_size);
224 #else
225  result = handmade_aligned_realloc(ptr,new_size,old_size);
226 #endif
227 
228  if (!result && new_size)
229  throw_std_bad_alloc();
230 
231  return result;
232 }
233 
234 /*****************************************************************************
235 *** Implementation of conditionally aligned functions ***
236 *****************************************************************************/
237 
241 template<bool Align> EIGEN_DEVICE_FUNC inline void* conditional_aligned_malloc(std::size_t size)
242 {
243  return aligned_malloc(size);
244 }
245 
246 template<> EIGEN_DEVICE_FUNC inline void* conditional_aligned_malloc<false>(std::size_t size)
247 {
248  check_that_malloc_is_allowed();
249 
250  EIGEN_USING_STD(malloc)
251  void *result = malloc(size);
252 
253  if(!result && size)
254  throw_std_bad_alloc();
255  return result;
256 }
257 
259 template<bool Align> EIGEN_DEVICE_FUNC inline void conditional_aligned_free(void *ptr)
260 {
261  aligned_free(ptr);
262 }
263 
264 template<> EIGEN_DEVICE_FUNC inline void conditional_aligned_free<false>(void *ptr)
265 {
266  EIGEN_USING_STD(free)
267  free(ptr);
268 }
269 
270 template<bool Align> inline void* conditional_aligned_realloc(void* ptr, std::size_t new_size, std::size_t old_size)
271 {
272  return aligned_realloc(ptr, new_size, old_size);
273 }
274 
275 template<> inline void* conditional_aligned_realloc<false>(void* ptr, std::size_t new_size, std::size_t)
276 {
277  return std::realloc(ptr, new_size);
278 }
279 
280 /*****************************************************************************
281 *** Construction/destruction of array elements ***
282 *****************************************************************************/
283 
287 template<typename T> EIGEN_DEVICE_FUNC inline void destruct_elements_of_array(T *ptr, std::size_t size)
288 {
289  // always destruct an array starting from the end.
290  if(ptr)
291  while(size) ptr[--size].~T();
292 }
293 
297 template<typename T> EIGEN_DEVICE_FUNC inline T* construct_elements_of_array(T *ptr, std::size_t size)
298 {
299  std::size_t i=0;
300  EIGEN_TRY
301  {
302  for (i = 0; i < size; ++i) ::new (ptr + i) T;
303  }
304  EIGEN_CATCH(...)
305  {
306  destruct_elements_of_array(ptr, i);
307  EIGEN_THROW;
308  }
309  return ptr;
310 }
311 
312 /*****************************************************************************
313 *** Implementation of aligned new/delete-like functions ***
314 *****************************************************************************/
315 
316 template<typename T>
317 EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE void check_size_for_overflow(std::size_t size)
318 {
319  if(size > std::size_t(-1) / sizeof(T))
320  throw_std_bad_alloc();
321 }
322 
327 template<typename T> EIGEN_DEVICE_FUNC inline T* aligned_new(std::size_t size)
328 {
329  check_size_for_overflow<T>(size);
330  T *result = reinterpret_cast<T*>(aligned_malloc(sizeof(T)*size));
331  EIGEN_TRY
332  {
333  return construct_elements_of_array(result, size);
334  }
335  EIGEN_CATCH(...)
336  {
337  aligned_free(result);
338  EIGEN_THROW;
339  }
340  return result;
341 }
342 
343 template<typename T, bool Align> EIGEN_DEVICE_FUNC inline T* conditional_aligned_new(std::size_t size)
344 {
345  check_size_for_overflow<T>(size);
346  T *result = reinterpret_cast<T*>(conditional_aligned_malloc<Align>(sizeof(T)*size));
347  EIGEN_TRY
348  {
349  return construct_elements_of_array(result, size);
350  }
351  EIGEN_CATCH(...)
352  {
353  conditional_aligned_free<Align>(result);
354  EIGEN_THROW;
355  }
356  return result;
357 }
358 
362 template<typename T> EIGEN_DEVICE_FUNC inline void aligned_delete(T *ptr, std::size_t size)
363 {
364  destruct_elements_of_array<T>(ptr, size);
365  Eigen::internal::aligned_free(ptr);
366 }
367 
371 template<typename T, bool Align> EIGEN_DEVICE_FUNC inline void conditional_aligned_delete(T *ptr, std::size_t size)
372 {
373  destruct_elements_of_array<T>(ptr, size);
374  conditional_aligned_free<Align>(ptr);
375 }
376 
377 template<typename T, bool Align> EIGEN_DEVICE_FUNC inline T* conditional_aligned_realloc_new(T* pts, std::size_t new_size, std::size_t old_size)
378 {
379  check_size_for_overflow<T>(new_size);
380  check_size_for_overflow<T>(old_size);
381  if(new_size < old_size)
382  destruct_elements_of_array(pts+new_size, old_size-new_size);
383  T *result = reinterpret_cast<T*>(conditional_aligned_realloc<Align>(reinterpret_cast<void*>(pts), sizeof(T)*new_size, sizeof(T)*old_size));
384  if(new_size > old_size)
385  {
386  EIGEN_TRY
387  {
388  construct_elements_of_array(result+old_size, new_size-old_size);
389  }
390  EIGEN_CATCH(...)
391  {
392  conditional_aligned_free<Align>(result);
393  EIGEN_THROW;
394  }
395  }
396  return result;
397 }
398 
399 
400 template<typename T, bool Align> EIGEN_DEVICE_FUNC inline T* conditional_aligned_new_auto(std::size_t size)
401 {
402  if(size==0)
403  return 0; // short-cut. Also fixes Bug 884
404  check_size_for_overflow<T>(size);
405  T *result = reinterpret_cast<T*>(conditional_aligned_malloc<Align>(sizeof(T)*size));
406  if(NumTraits<T>::RequireInitialization)
407  {
408  EIGEN_TRY
409  {
410  construct_elements_of_array(result, size);
411  }
412  EIGEN_CATCH(...)
413  {
414  conditional_aligned_free<Align>(result);
415  EIGEN_THROW;
416  }
417  }
418  return result;
419 }
420 
421 template<typename T, bool Align> inline T* conditional_aligned_realloc_new_auto(T* pts, std::size_t new_size, std::size_t old_size)
422 {
423  check_size_for_overflow<T>(new_size);
424  check_size_for_overflow<T>(old_size);
425  if(NumTraits<T>::RequireInitialization && (new_size < old_size))
426  destruct_elements_of_array(pts+new_size, old_size-new_size);
427  T *result = reinterpret_cast<T*>(conditional_aligned_realloc<Align>(reinterpret_cast<void*>(pts), sizeof(T)*new_size, sizeof(T)*old_size));
428  if(NumTraits<T>::RequireInitialization && (new_size > old_size))
429  {
430  EIGEN_TRY
431  {
432  construct_elements_of_array(result+old_size, new_size-old_size);
433  }
434  EIGEN_CATCH(...)
435  {
436  conditional_aligned_free<Align>(result);
437  EIGEN_THROW;
438  }
439  }
440  return result;
441 }
442 
443 template<typename T, bool Align> EIGEN_DEVICE_FUNC inline void conditional_aligned_delete_auto(T *ptr, std::size_t size)
444 {
445  if(NumTraits<T>::RequireInitialization)
446  destruct_elements_of_array<T>(ptr, size);
447  conditional_aligned_free<Align>(ptr);
448 }
449 
450 /****************************************************************************/
451 
469 template<int Alignment, typename Scalar, typename Index>
470 EIGEN_DEVICE_FUNC inline Index first_aligned(const Scalar* array, Index size)
471 {
472  const Index ScalarSize = sizeof(Scalar);
473  const Index AlignmentSize = Alignment / ScalarSize;
474  const Index AlignmentMask = AlignmentSize-1;
475 
476  if(AlignmentSize<=1)
477  {
478  // Either the requested alignment if smaller than a scalar, or it exactly match a 1 scalar
479  // so that all elements of the array have the same alignment.
480  return 0;
481  }
482  else if( (UIntPtr(array) & (sizeof(Scalar)-1)) || (Alignment%ScalarSize)!=0)
483  {
484  // The array is not aligned to the size of a single scalar, or the requested alignment is not a multiple of the scalar size.
485  // Consequently, no element of the array is well aligned.
486  return size;
487  }
488  else
489  {
490  Index first = (AlignmentSize - (Index((UIntPtr(array)/sizeof(Scalar))) & AlignmentMask)) & AlignmentMask;
491  return (first < size) ? first : size;
492  }
493 }
494 
497 template<typename Scalar, typename Index>
498 EIGEN_DEVICE_FUNC inline Index first_default_aligned(const Scalar* array, Index size)
499 {
500  typedef typename packet_traits<Scalar>::type DefaultPacketType;
501  return first_aligned<unpacket_traits<DefaultPacketType>::alignment>(array, size);
502 }
503 
506 template<typename Index>
507 inline Index first_multiple(Index size, Index base)
508 {
509  return ((size+base-1)/base)*base;
510 }
511 
512 // std::copy is much slower than memcpy, so let's introduce a smart_copy which
513 // use memcpy on trivial types, i.e., on types that does not require an initialization ctor.
514 template<typename T, bool UseMemcpy> struct smart_copy_helper;
515 
516 template<typename T> EIGEN_DEVICE_FUNC void smart_copy(const T* start, const T* end, T* target)
517 {
518  smart_copy_helper<T,!NumTraits<T>::RequireInitialization>::run(start, end, target);
519 }
520 
521 template<typename T> struct smart_copy_helper<T,true> {
522  EIGEN_DEVICE_FUNC static inline void run(const T* start, const T* end, T* target)
523  {
524  IntPtr size = IntPtr(end)-IntPtr(start);
525  if(size==0) return;
526  eigen_internal_assert(start!=0 && end!=0 && target!=0);
527  EIGEN_USING_STD(memcpy)
528  memcpy(target, start, size);
529  }
530 };
531 
532 template<typename T> struct smart_copy_helper<T,false> {
533  EIGEN_DEVICE_FUNC static inline void run(const T* start, const T* end, T* target)
534  { std::copy(start, end, target); }
535 };
536 
537 // intelligent memmove. falls back to std::memmove for POD types, uses std::copy otherwise.
538 template<typename T, bool UseMemmove> struct smart_memmove_helper;
539 
540 template<typename T> void smart_memmove(const T* start, const T* end, T* target)
541 {
542  smart_memmove_helper<T,!NumTraits<T>::RequireInitialization>::run(start, end, target);
543 }
544 
545 template<typename T> struct smart_memmove_helper<T,true> {
546  static inline void run(const T* start, const T* end, T* target)
547  {
548  IntPtr size = IntPtr(end)-IntPtr(start);
549  if(size==0) return;
550  eigen_internal_assert(start!=0 && end!=0 && target!=0);
551  std::memmove(target, start, size);
552  }
553 };
554 
555 template<typename T> struct smart_memmove_helper<T,false> {
556  static inline void run(const T* start, const T* end, T* target)
557  {
558  if (UIntPtr(target) < UIntPtr(start))
559  {
560  std::copy(start, end, target);
561  }
562  else
563  {
564  std::ptrdiff_t count = (std::ptrdiff_t(end)-std::ptrdiff_t(start)) / sizeof(T);
565  std::copy_backward(start, end, target + count);
566  }
567  }
568 };
569 
570 template<typename T> EIGEN_DEVICE_FUNC T* smart_move(T* start, T* end, T* target)
571 {
572  return std::move(start, end, target);
573 }
574 
575 /*****************************************************************************
576 *** Implementation of runtime stack allocation (falling back to malloc) ***
577 *****************************************************************************/
578 
579 // you can overwrite Eigen's default behavior regarding alloca by defining EIGEN_ALLOCA
580 // to the appropriate stack allocation function
581 #if ! defined EIGEN_ALLOCA && ! defined EIGEN_GPU_COMPILE_PHASE
582  #if EIGEN_OS_LINUX || EIGEN_OS_MAC || (defined alloca)
583  #define EIGEN_ALLOCA alloca
584  #elif EIGEN_COMP_MSVC
585  #define EIGEN_ALLOCA _alloca
586  #endif
587 #endif
588 
589 // With clang -Oz -mthumb, alloca changes the stack pointer in a way that is
590 // not allowed in Thumb2. -DEIGEN_STACK_ALLOCATION_LIMIT=0 doesn't work because
591 // the compiler still emits bad code because stack allocation checks use "<=".
592 // TODO: Eliminate after https://bugs.llvm.org/show_bug.cgi?id=23772
593 // is fixed.
594 #if defined(__clang__) && defined(__thumb__)
595  #undef EIGEN_ALLOCA
596 #endif
597 
598 // This helper class construct the allocated memory, and takes care of destructing and freeing the handled data
599 // at destruction time. In practice this helper class is mainly useful to avoid memory leak in case of exceptions.
600 template<typename T> class aligned_stack_memory_handler : noncopyable
601 {
602  public:
603  /* Creates a stack_memory_handler responsible for the buffer \a ptr of size \a size.
604  * Note that \a ptr can be 0 regardless of the other parameters.
605  * This constructor takes care of constructing/initializing the elements of the buffer if required by the scalar type T (see NumTraits<T>::RequireInitialization).
606  * In this case, the buffer elements will also be destructed when this handler will be destructed.
607  * Finally, if \a dealloc is true, then the pointer \a ptr is freed.
608  **/
609  EIGEN_DEVICE_FUNC
610  aligned_stack_memory_handler(T* ptr, std::size_t size, bool dealloc)
611  : m_ptr(ptr), m_size(size), m_deallocate(dealloc)
612  {
613  if(NumTraits<T>::RequireInitialization && m_ptr)
614  Eigen::internal::construct_elements_of_array(m_ptr, size);
615  }
616  EIGEN_DEVICE_FUNC
617  ~aligned_stack_memory_handler()
618  {
619  if(NumTraits<T>::RequireInitialization && m_ptr)
620  Eigen::internal::destruct_elements_of_array<T>(m_ptr, m_size);
621  if(m_deallocate)
622  Eigen::internal::aligned_free(m_ptr);
623  }
624  protected:
625  T* m_ptr;
626  std::size_t m_size;
627  bool m_deallocate;
628 };
629 
630 #ifdef EIGEN_ALLOCA
631 
632 template<typename Xpr, int NbEvaluations,
633  bool MapExternalBuffer = nested_eval<Xpr,NbEvaluations>::Evaluate && Xpr::MaxSizeAtCompileTime==Dynamic
634  >
635 struct local_nested_eval_wrapper
636 {
637  static constexpr bool NeedExternalBuffer = false;
638  typedef typename Xpr::Scalar Scalar;
639  typedef typename nested_eval<Xpr,NbEvaluations>::type ObjectType;
640  ObjectType object;
641 
642  EIGEN_DEVICE_FUNC
643  local_nested_eval_wrapper(const Xpr& xpr, Scalar* ptr) : object(xpr)
644  {
645  EIGEN_UNUSED_VARIABLE(ptr);
646  eigen_internal_assert(ptr==0);
647  }
648 };
649 
650 template<typename Xpr, int NbEvaluations>
651 struct local_nested_eval_wrapper<Xpr,NbEvaluations,true>
652 {
653  static constexpr bool NeedExternalBuffer = true;
654  typedef typename Xpr::Scalar Scalar;
655  typedef typename plain_object_eval<Xpr>::type PlainObject;
656  typedef Map<PlainObject,EIGEN_DEFAULT_ALIGN_BYTES> ObjectType;
657  ObjectType object;
658 
659  EIGEN_DEVICE_FUNC
660  local_nested_eval_wrapper(const Xpr& xpr, Scalar* ptr)
661  : object(ptr==0 ? reinterpret_cast<Scalar*>(Eigen::internal::aligned_malloc(sizeof(Scalar)*xpr.size())) : ptr, xpr.rows(), xpr.cols()),
662  m_deallocate(ptr==0)
663  {
664  if(NumTraits<Scalar>::RequireInitialization && object.data())
665  Eigen::internal::construct_elements_of_array(object.data(), object.size());
666  object = xpr;
667  }
668 
669  EIGEN_DEVICE_FUNC
670  ~local_nested_eval_wrapper()
671  {
672  if(NumTraits<Scalar>::RequireInitialization && object.data())
673  Eigen::internal::destruct_elements_of_array(object.data(), object.size());
674  if(m_deallocate)
675  Eigen::internal::aligned_free(object.data());
676  }
677 
678 private:
679  bool m_deallocate;
680 };
681 
682 #endif // EIGEN_ALLOCA
683 
684 template<typename T> class scoped_array : noncopyable
685 {
686  T* m_ptr;
687 public:
688  explicit scoped_array(std::ptrdiff_t size)
689  {
690  m_ptr = new T[size];
691  }
692  ~scoped_array()
693  {
694  delete[] m_ptr;
695  }
696  T& operator[](std::ptrdiff_t i) { return m_ptr[i]; }
697  const T& operator[](std::ptrdiff_t i) const { return m_ptr[i]; }
698  T* &ptr() { return m_ptr; }
699  const T* ptr() const { return m_ptr; }
700  operator const T*() const { return m_ptr; }
701 };
702 
703 template<typename T> void swap(scoped_array<T> &a,scoped_array<T> &b)
704 {
705  std::swap(a.ptr(),b.ptr());
706 }
707 
708 } // end namespace internal
709 
735 #ifdef EIGEN_ALLOCA
736 
737  #if EIGEN_DEFAULT_ALIGN_BYTES>0
738  // We always manually re-align the result of EIGEN_ALLOCA.
739  // If alloca is already aligned, the compiler should be smart enough to optimize away the re-alignment.
740  #define EIGEN_ALIGNED_ALLOCA(SIZE) reinterpret_cast<void*>((internal::UIntPtr(EIGEN_ALLOCA(SIZE+EIGEN_DEFAULT_ALIGN_BYTES-1)) + EIGEN_DEFAULT_ALIGN_BYTES-1) & ~(std::size_t(EIGEN_DEFAULT_ALIGN_BYTES-1)))
741  #else
742  #define EIGEN_ALIGNED_ALLOCA(SIZE) EIGEN_ALLOCA(SIZE)
743  #endif
744 
745  #define ei_declare_aligned_stack_constructed_variable(TYPE,NAME,SIZE,BUFFER) \
746  Eigen::internal::check_size_for_overflow<TYPE>(SIZE); \
747  TYPE* NAME = (BUFFER)!=0 ? (BUFFER) \
748  : reinterpret_cast<TYPE*>( \
749  (sizeof(TYPE)*SIZE<=EIGEN_STACK_ALLOCATION_LIMIT) ? EIGEN_ALIGNED_ALLOCA(sizeof(TYPE)*SIZE) \
750  : Eigen::internal::aligned_malloc(sizeof(TYPE)*SIZE) ); \
751  Eigen::internal::aligned_stack_memory_handler<TYPE> EIGEN_CAT(NAME,_stack_memory_destructor)((BUFFER)==0 ? NAME : 0,SIZE,sizeof(TYPE)*SIZE>EIGEN_STACK_ALLOCATION_LIMIT)
752 
753 
754  #define ei_declare_local_nested_eval(XPR_T,XPR,N,NAME) \
755  Eigen::internal::local_nested_eval_wrapper<XPR_T,N> EIGEN_CAT(NAME,_wrapper)(XPR, reinterpret_cast<typename XPR_T::Scalar*>( \
756  ( (Eigen::internal::local_nested_eval_wrapper<XPR_T,N>::NeedExternalBuffer) && ((sizeof(typename XPR_T::Scalar)*XPR.size())<=EIGEN_STACK_ALLOCATION_LIMIT) ) \
757  ? EIGEN_ALIGNED_ALLOCA( sizeof(typename XPR_T::Scalar)*XPR.size() ) : 0 ) ) ; \
758  typename Eigen::internal::local_nested_eval_wrapper<XPR_T,N>::ObjectType NAME(EIGEN_CAT(NAME,_wrapper).object)
759 
760 #else
761 
762  #define ei_declare_aligned_stack_constructed_variable(TYPE,NAME,SIZE,BUFFER) \
763  Eigen::internal::check_size_for_overflow<TYPE>(SIZE); \
764  TYPE* NAME = (BUFFER)!=0 ? BUFFER : reinterpret_cast<TYPE*>(Eigen::internal::aligned_malloc(sizeof(TYPE)*SIZE)); \
765  Eigen::internal::aligned_stack_memory_handler<TYPE> EIGEN_CAT(NAME,_stack_memory_destructor)((BUFFER)==0 ? NAME : 0,SIZE,true)
766 
767 
768 #define ei_declare_local_nested_eval(XPR_T,XPR,N,NAME) typename Eigen::internal::nested_eval<XPR_T,N>::type NAME(XPR)
769 
770 #endif
771 
772 
773 /*****************************************************************************
774 *** Implementation of EIGEN_MAKE_ALIGNED_OPERATOR_NEW [_IF] ***
775 *****************************************************************************/
776 
777 #if EIGEN_HAS_CXX17_OVERALIGN
778 
779 // C++17 -> no need to bother about alignment anymore :)
780 
781 #define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_NOTHROW(NeedsToAlign)
782 #define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(NeedsToAlign)
783 #define EIGEN_MAKE_ALIGNED_OPERATOR_NEW
784 #define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF_VECTORIZABLE_FIXED_SIZE(Scalar,Size)
785 
786 #else
787 
788 // HIP does not support new/delete on device.
789 #if EIGEN_MAX_ALIGN_BYTES!=0 && !defined(EIGEN_HIP_DEVICE_COMPILE)
790  #define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_NOTHROW(NeedsToAlign) \
791  EIGEN_DEVICE_FUNC \
792  void* operator new(std::size_t size, const std::nothrow_t&) EIGEN_NO_THROW { \
793  EIGEN_TRY { return Eigen::internal::conditional_aligned_malloc<NeedsToAlign>(size); } \
794  EIGEN_CATCH (...) { return 0; } \
795  }
796  #define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(NeedsToAlign) \
797  EIGEN_DEVICE_FUNC \
798  void *operator new(std::size_t size) { \
799  return Eigen::internal::conditional_aligned_malloc<NeedsToAlign>(size); \
800  } \
801  EIGEN_DEVICE_FUNC \
802  void *operator new[](std::size_t size) { \
803  return Eigen::internal::conditional_aligned_malloc<NeedsToAlign>(size); \
804  } \
805  EIGEN_DEVICE_FUNC \
806  void operator delete(void * ptr) EIGEN_NO_THROW { Eigen::internal::conditional_aligned_free<NeedsToAlign>(ptr); } \
807  EIGEN_DEVICE_FUNC \
808  void operator delete[](void * ptr) EIGEN_NO_THROW { Eigen::internal::conditional_aligned_free<NeedsToAlign>(ptr); } \
809  EIGEN_DEVICE_FUNC \
810  void operator delete(void * ptr, std::size_t /* sz */) EIGEN_NO_THROW { Eigen::internal::conditional_aligned_free<NeedsToAlign>(ptr); } \
811  EIGEN_DEVICE_FUNC \
812  void operator delete[](void * ptr, std::size_t /* sz */) EIGEN_NO_THROW { Eigen::internal::conditional_aligned_free<NeedsToAlign>(ptr); } \
813  /* in-place new and delete. since (at least afaik) there is no actual */ \
814  /* memory allocated we can safely let the default implementation handle */ \
815  /* this particular case. */ \
816  EIGEN_DEVICE_FUNC \
817  static void *operator new(std::size_t size, void *ptr) { return ::operator new(size,ptr); } \
818  EIGEN_DEVICE_FUNC \
819  static void *operator new[](std::size_t size, void* ptr) { return ::operator new[](size,ptr); } \
820  EIGEN_DEVICE_FUNC \
821  void operator delete(void * memory, void *ptr) EIGEN_NO_THROW { return ::operator delete(memory,ptr); } \
822  EIGEN_DEVICE_FUNC \
823  void operator delete[](void * memory, void *ptr) EIGEN_NO_THROW { return ::operator delete[](memory,ptr); } \
824  /* nothrow-new (returns zero instead of std::bad_alloc) */ \
825  EIGEN_MAKE_ALIGNED_OPERATOR_NEW_NOTHROW(NeedsToAlign) \
826  EIGEN_DEVICE_FUNC \
827  void operator delete(void *ptr, const std::nothrow_t&) EIGEN_NO_THROW { \
828  Eigen::internal::conditional_aligned_free<NeedsToAlign>(ptr); \
829  } \
830  typedef void eigen_aligned_operator_new_marker_type;
831 #else
832  #define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(NeedsToAlign)
833 #endif
834 
835 #define EIGEN_MAKE_ALIGNED_OPERATOR_NEW EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(true)
836 #define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF_VECTORIZABLE_FIXED_SIZE(Scalar,Size) \
837  EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(bool( \
838  ((Size)!=Eigen::Dynamic) && \
839  (((EIGEN_MAX_ALIGN_BYTES>=16) && ((sizeof(Scalar)*(Size))%(EIGEN_MAX_ALIGN_BYTES )==0)) || \
840  ((EIGEN_MAX_ALIGN_BYTES>=32) && ((sizeof(Scalar)*(Size))%(EIGEN_MAX_ALIGN_BYTES/2)==0)) || \
841  ((EIGEN_MAX_ALIGN_BYTES>=64) && ((sizeof(Scalar)*(Size))%(EIGEN_MAX_ALIGN_BYTES/4)==0)) )))
842 
843 #endif
844 
845 /****************************************************************************/
846 
871 template<class T>
872 class aligned_allocator : public std::allocator<T>
873 {
874 public:
875  typedef std::size_t size_type;
876  typedef std::ptrdiff_t difference_type;
877  typedef T* pointer;
878  typedef const T* const_pointer;
879  typedef T& reference;
880  typedef const T& const_reference;
881  typedef T value_type;
882 
883  template<class U>
884  struct rebind
885  {
886  typedef aligned_allocator<U> other;
887  };
888 
889  aligned_allocator() : std::allocator<T>() {}
890 
891  aligned_allocator(const aligned_allocator& other) : std::allocator<T>(other) {}
892 
893  template<class U>
894  aligned_allocator(const aligned_allocator<U>& other) : std::allocator<T>(other) {}
895 
896  ~aligned_allocator() {}
897 
898  #if EIGEN_COMP_GNUC_STRICT && EIGEN_GNUC_AT_LEAST(7,0)
899  // In gcc std::allocator::max_size() is bugged making gcc triggers a warning:
900  // eigen/Eigen/src/Core/util/Memory.h:189:12: warning: argument 1 value '18446744073709551612' exceeds maximum object size 9223372036854775807
901  // See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87544
902  size_type max_size() const {
903  return (std::numeric_limits<std::ptrdiff_t>::max)()/sizeof(T);
904  }
905  #endif
906 
907  pointer allocate(size_type num, const void* /*hint*/ = 0)
908  {
909  internal::check_size_for_overflow<T>(num);
910  return static_cast<pointer>( internal::aligned_malloc(num * sizeof(T)) );
911  }
912 
913  void deallocate(pointer p, size_type /*num*/)
914  {
915  internal::aligned_free(p);
916  }
917 };
918 
919 //---------- Cache sizes ----------
920 
921 #if !defined(EIGEN_NO_CPUID)
922 # if EIGEN_COMP_GNUC && EIGEN_ARCH_i386_OR_x86_64
923 # if defined(__PIC__) && EIGEN_ARCH_i386
924  // Case for x86 with PIC
925 # define EIGEN_CPUID(abcd,func,id) \
926  __asm__ __volatile__ ("xchgl %%ebx, %k1;cpuid; xchgl %%ebx,%k1": "=a" (abcd[0]), "=&r" (abcd[1]), "=c" (abcd[2]), "=d" (abcd[3]) : "a" (func), "c" (id));
927 # elif defined(__PIC__) && EIGEN_ARCH_x86_64
928  // Case for x64 with PIC. In theory this is only a problem with recent gcc and with medium or large code model, not with the default small code model.
929  // However, we cannot detect which code model is used, and the xchg overhead is negligible anyway.
930 # define EIGEN_CPUID(abcd,func,id) \
931  __asm__ __volatile__ ("xchg{q}\t{%%}rbx, %q1; cpuid; xchg{q}\t{%%}rbx, %q1": "=a" (abcd[0]), "=&r" (abcd[1]), "=c" (abcd[2]), "=d" (abcd[3]) : "0" (func), "2" (id));
932 # else
933  // Case for x86_64 or x86 w/o PIC
934 # define EIGEN_CPUID(abcd,func,id) \
935  __asm__ __volatile__ ("cpuid": "=a" (abcd[0]), "=b" (abcd[1]), "=c" (abcd[2]), "=d" (abcd[3]) : "0" (func), "2" (id) );
936 # endif
937 # elif EIGEN_COMP_MSVC
938 # if EIGEN_ARCH_i386_OR_x86_64
939 # define EIGEN_CPUID(abcd,func,id) __cpuidex((int*)abcd,func,id)
940 # endif
941 # endif
942 #endif
943 
944 namespace internal {
945 
946 #ifdef EIGEN_CPUID
947 
948 inline bool cpuid_is_vendor(int abcd[4], const int vendor[3])
949 {
950  return abcd[1]==vendor[0] && abcd[3]==vendor[1] && abcd[2]==vendor[2];
951 }
952 
953 inline void queryCacheSizes_intel_direct(int& l1, int& l2, int& l3)
954 {
955  int abcd[4];
956  l1 = l2 = l3 = 0;
957  int cache_id = 0;
958  int cache_type = 0;
959  do {
960  abcd[0] = abcd[1] = abcd[2] = abcd[3] = 0;
961  EIGEN_CPUID(abcd,0x4,cache_id);
962  cache_type = (abcd[0] & 0x0F) >> 0;
963  if(cache_type==1||cache_type==3) // data or unified cache
964  {
965  int cache_level = (abcd[0] & 0xE0) >> 5; // A[7:5]
966  int ways = (abcd[1] & 0xFFC00000) >> 22; // B[31:22]
967  int partitions = (abcd[1] & 0x003FF000) >> 12; // B[21:12]
968  int line_size = (abcd[1] & 0x00000FFF) >> 0; // B[11:0]
969  int sets = (abcd[2]); // C[31:0]
970 
971  int cache_size = (ways+1) * (partitions+1) * (line_size+1) * (sets+1);
972 
973  switch(cache_level)
974  {
975  case 1: l1 = cache_size; break;
976  case 2: l2 = cache_size; break;
977  case 3: l3 = cache_size; break;
978  default: break;
979  }
980  }
981  cache_id++;
982  } while(cache_type>0 && cache_id<16);
983 }
984 
985 inline void queryCacheSizes_intel_codes(int& l1, int& l2, int& l3)
986 {
987  int abcd[4];
988  abcd[0] = abcd[1] = abcd[2] = abcd[3] = 0;
989  l1 = l2 = l3 = 0;
990  EIGEN_CPUID(abcd,0x00000002,0);
991  unsigned char * bytes = reinterpret_cast<unsigned char *>(abcd)+2;
992  bool check_for_p2_core2 = false;
993  for(int i=0; i<14; ++i)
994  {
995  switch(bytes[i])
996  {
997  case 0x0A: l1 = 8; break; // 0Ah data L1 cache, 8 KB, 2 ways, 32 byte lines
998  case 0x0C: l1 = 16; break; // 0Ch data L1 cache, 16 KB, 4 ways, 32 byte lines
999  case 0x0E: l1 = 24; break; // 0Eh data L1 cache, 24 KB, 6 ways, 64 byte lines
1000  case 0x10: l1 = 16; break; // 10h data L1 cache, 16 KB, 4 ways, 32 byte lines (IA-64)
1001  case 0x15: l1 = 16; break; // 15h code L1 cache, 16 KB, 4 ways, 32 byte lines (IA-64)
1002  case 0x2C: l1 = 32; break; // 2Ch data L1 cache, 32 KB, 8 ways, 64 byte lines
1003  case 0x30: l1 = 32; break; // 30h code L1 cache, 32 KB, 8 ways, 64 byte lines
1004  case 0x60: l1 = 16; break; // 60h data L1 cache, 16 KB, 8 ways, 64 byte lines, sectored
1005  case 0x66: l1 = 8; break; // 66h data L1 cache, 8 KB, 4 ways, 64 byte lines, sectored
1006  case 0x67: l1 = 16; break; // 67h data L1 cache, 16 KB, 4 ways, 64 byte lines, sectored
1007  case 0x68: l1 = 32; break; // 68h data L1 cache, 32 KB, 4 ways, 64 byte lines, sectored
1008  case 0x1A: l2 = 96; break; // code and data L2 cache, 96 KB, 6 ways, 64 byte lines (IA-64)
1009  case 0x22: l3 = 512; break; // code and data L3 cache, 512 KB, 4 ways (!), 64 byte lines, dual-sectored
1010  case 0x23: l3 = 1024; break; // code and data L3 cache, 1024 KB, 8 ways, 64 byte lines, dual-sectored
1011  case 0x25: l3 = 2048; break; // code and data L3 cache, 2048 KB, 8 ways, 64 byte lines, dual-sectored
1012  case 0x29: l3 = 4096; break; // code and data L3 cache, 4096 KB, 8 ways, 64 byte lines, dual-sectored
1013  case 0x39: l2 = 128; break; // code and data L2 cache, 128 KB, 4 ways, 64 byte lines, sectored
1014  case 0x3A: l2 = 192; break; // code and data L2 cache, 192 KB, 6 ways, 64 byte lines, sectored
1015  case 0x3B: l2 = 128; break; // code and data L2 cache, 128 KB, 2 ways, 64 byte lines, sectored
1016  case 0x3C: l2 = 256; break; // code and data L2 cache, 256 KB, 4 ways, 64 byte lines, sectored
1017  case 0x3D: l2 = 384; break; // code and data L2 cache, 384 KB, 6 ways, 64 byte lines, sectored
1018  case 0x3E: l2 = 512; break; // code and data L2 cache, 512 KB, 4 ways, 64 byte lines, sectored
1019  case 0x40: l2 = 0; break; // no integrated L2 cache (P6 core) or L3 cache (P4 core)
1020  case 0x41: l2 = 128; break; // code and data L2 cache, 128 KB, 4 ways, 32 byte lines
1021  case 0x42: l2 = 256; break; // code and data L2 cache, 256 KB, 4 ways, 32 byte lines
1022  case 0x43: l2 = 512; break; // code and data L2 cache, 512 KB, 4 ways, 32 byte lines
1023  case 0x44: l2 = 1024; break; // code and data L2 cache, 1024 KB, 4 ways, 32 byte lines
1024  case 0x45: l2 = 2048; break; // code and data L2 cache, 2048 KB, 4 ways, 32 byte lines
1025  case 0x46: l3 = 4096; break; // code and data L3 cache, 4096 KB, 4 ways, 64 byte lines
1026  case 0x47: l3 = 8192; break; // code and data L3 cache, 8192 KB, 8 ways, 64 byte lines
1027  case 0x48: l2 = 3072; break; // code and data L2 cache, 3072 KB, 12 ways, 64 byte lines
1028  case 0x49: if(l2!=0) l3 = 4096; else {check_for_p2_core2=true; l3 = l2 = 4096;} break;// code and data L3 cache, 4096 KB, 16 ways, 64 byte lines (P4) or L2 for core2
1029  case 0x4A: l3 = 6144; break; // code and data L3 cache, 6144 KB, 12 ways, 64 byte lines
1030  case 0x4B: l3 = 8192; break; // code and data L3 cache, 8192 KB, 16 ways, 64 byte lines
1031  case 0x4C: l3 = 12288; break; // code and data L3 cache, 12288 KB, 12 ways, 64 byte lines
1032  case 0x4D: l3 = 16384; break; // code and data L3 cache, 16384 KB, 16 ways, 64 byte lines
1033  case 0x4E: l2 = 6144; break; // code and data L2 cache, 6144 KB, 24 ways, 64 byte lines
1034  case 0x78: l2 = 1024; break; // code and data L2 cache, 1024 KB, 4 ways, 64 byte lines
1035  case 0x79: l2 = 128; break; // code and data L2 cache, 128 KB, 8 ways, 64 byte lines, dual-sectored
1036  case 0x7A: l2 = 256; break; // code and data L2 cache, 256 KB, 8 ways, 64 byte lines, dual-sectored
1037  case 0x7B: l2 = 512; break; // code and data L2 cache, 512 KB, 8 ways, 64 byte lines, dual-sectored
1038  case 0x7C: l2 = 1024; break; // code and data L2 cache, 1024 KB, 8 ways, 64 byte lines, dual-sectored
1039  case 0x7D: l2 = 2048; break; // code and data L2 cache, 2048 KB, 8 ways, 64 byte lines
1040  case 0x7E: l2 = 256; break; // code and data L2 cache, 256 KB, 8 ways, 128 byte lines, sect. (IA-64)
1041  case 0x7F: l2 = 512; break; // code and data L2 cache, 512 KB, 2 ways, 64 byte lines
1042  case 0x80: l2 = 512; break; // code and data L2 cache, 512 KB, 8 ways, 64 byte lines
1043  case 0x81: l2 = 128; break; // code and data L2 cache, 128 KB, 8 ways, 32 byte lines
1044  case 0x82: l2 = 256; break; // code and data L2 cache, 256 KB, 8 ways, 32 byte lines
1045  case 0x83: l2 = 512; break; // code and data L2 cache, 512 KB, 8 ways, 32 byte lines
1046  case 0x84: l2 = 1024; break; // code and data L2 cache, 1024 KB, 8 ways, 32 byte lines
1047  case 0x85: l2 = 2048; break; // code and data L2 cache, 2048 KB, 8 ways, 32 byte lines
1048  case 0x86: l2 = 512; break; // code and data L2 cache, 512 KB, 4 ways, 64 byte lines
1049  case 0x87: l2 = 1024; break; // code and data L2 cache, 1024 KB, 8 ways, 64 byte lines
1050  case 0x88: l3 = 2048; break; // code and data L3 cache, 2048 KB, 4 ways, 64 byte lines (IA-64)
1051  case 0x89: l3 = 4096; break; // code and data L3 cache, 4096 KB, 4 ways, 64 byte lines (IA-64)
1052  case 0x8A: l3 = 8192; break; // code and data L3 cache, 8192 KB, 4 ways, 64 byte lines (IA-64)
1053  case 0x8D: l3 = 3072; break; // code and data L3 cache, 3072 KB, 12 ways, 128 byte lines (IA-64)
1054 
1055  default: break;
1056  }
1057  }
1058  if(check_for_p2_core2 && l2 == l3)
1059  l3 = 0;
1060  l1 *= 1024;
1061  l2 *= 1024;
1062  l3 *= 1024;
1063 }
1064 
1065 inline void queryCacheSizes_intel(int& l1, int& l2, int& l3, int max_std_funcs)
1066 {
1067  if(max_std_funcs>=4)
1068  queryCacheSizes_intel_direct(l1,l2,l3);
1069  else if(max_std_funcs>=2)
1070  queryCacheSizes_intel_codes(l1,l2,l3);
1071  else
1072  l1 = l2 = l3 = 0;
1073 }
1074 
1075 inline void queryCacheSizes_amd(int& l1, int& l2, int& l3)
1076 {
1077  int abcd[4];
1078  abcd[0] = abcd[1] = abcd[2] = abcd[3] = 0;
1079 
1080  // First query the max supported function.
1081  EIGEN_CPUID(abcd,0x80000000,0);
1082  if(static_cast<numext::uint32_t>(abcd[0]) >= static_cast<numext::uint32_t>(0x80000006))
1083  {
1084  EIGEN_CPUID(abcd,0x80000005,0);
1085  l1 = (abcd[2] >> 24) * 1024; // C[31:24] = L1 size in KB
1086  abcd[0] = abcd[1] = abcd[2] = abcd[3] = 0;
1087  EIGEN_CPUID(abcd,0x80000006,0);
1088  l2 = (abcd[2] >> 16) * 1024; // C[31;16] = l2 cache size in KB
1089  l3 = ((abcd[3] & 0xFFFC000) >> 18) * 512 * 1024; // D[31;18] = l3 cache size in 512KB
1090  }
1091  else
1092  {
1093  l1 = l2 = l3 = 0;
1094  }
1095 }
1096 #endif
1097 
1100 inline void queryCacheSizes(int& l1, int& l2, int& l3)
1101 {
1102  #ifdef EIGEN_CPUID
1103  int abcd[4];
1104  const int GenuineIntel[] = {0x756e6547, 0x49656e69, 0x6c65746e};
1105  const int AuthenticAMD[] = {0x68747541, 0x69746e65, 0x444d4163};
1106  const int AMDisbetter_[] = {0x69444d41, 0x74656273, 0x21726574}; // "AMDisbetter!"
1107 
1108  // identify the CPU vendor
1109  EIGEN_CPUID(abcd,0x0,0);
1110  int max_std_funcs = abcd[0];
1111  if(cpuid_is_vendor(abcd,GenuineIntel))
1112  queryCacheSizes_intel(l1,l2,l3,max_std_funcs);
1113  else if(cpuid_is_vendor(abcd,AuthenticAMD) || cpuid_is_vendor(abcd,AMDisbetter_))
1114  queryCacheSizes_amd(l1,l2,l3);
1115  else
1116  // by default let's use Intel's API
1117  queryCacheSizes_intel(l1,l2,l3,max_std_funcs);
1118 
1119  // here is the list of other vendors:
1120 // ||cpuid_is_vendor(abcd,"VIA VIA VIA ")
1121 // ||cpuid_is_vendor(abcd,"CyrixInstead")
1122 // ||cpuid_is_vendor(abcd,"CentaurHauls")
1123 // ||cpuid_is_vendor(abcd,"GenuineTMx86")
1124 // ||cpuid_is_vendor(abcd,"TransmetaCPU")
1125 // ||cpuid_is_vendor(abcd,"RiseRiseRise")
1126 // ||cpuid_is_vendor(abcd,"Geode by NSC")
1127 // ||cpuid_is_vendor(abcd,"SiS SiS SiS ")
1128 // ||cpuid_is_vendor(abcd,"UMC UMC UMC ")
1129 // ||cpuid_is_vendor(abcd,"NexGenDriven")
1130  #else
1131  l1 = l2 = l3 = -1;
1132  #endif
1133 }
1134 
1137 inline int queryL1CacheSize()
1138 {
1139  int l1(-1), l2, l3;
1140  queryCacheSizes(l1,l2,l3);
1141  return l1;
1142 }
1143 
1146 inline int queryTopLevelCacheSize()
1147 {
1148  int l1, l2(-1), l3(-1);
1149  queryCacheSizes(l1,l2,l3);
1150  return (std::max)(l2,l3);
1151 }
1152 
1153 
1154 
1159 #if EIGEN_COMP_CXXVER >= 20
1160 using std::construct_at;
1161 #else
1162 template<class T, class... Args>
1163 EIGEN_DEVICE_FUNC T* construct_at( T* p, Args&&... args )
1164 {
1165  return ::new (const_cast<void*>(static_cast<const volatile void*>(p)))
1166  T(std::forward<Args>(args)...);
1167 }
1168 #endif
1169 
1175 #if EIGEN_COMP_CXXVER >= 17
1176 using std::destroy_at;
1177 #else
1178 template<class T>
1179 EIGEN_DEVICE_FUNC void destroy_at(T* p)
1180 {
1181  p->~T();
1182 }
1183 #endif
1184 
1185 } // end namespace internal
1186 
1187 } // end namespace Eigen
1188 
1189 #endif // EIGEN_MEMORY_H
STL compatible allocator to use with types requiring a non-standard alignment.
Definition: Memory.h:873
static const lastp1_t end
Definition: IndexedViewHelper.h:183
Namespace containing all symbols from the Eigen library.
Definition: Core:139
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:59
const int Dynamic
Definition: Constants.h:24