$darkmode
Eigen  5.0.1-dev
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 *** Platform checks for aligned malloc functions ***
17 *****************************************************************************/
18 
19 #ifndef EIGEN_MEMORY_H
20 #define EIGEN_MEMORY_H
21 
22 #ifndef EIGEN_MALLOC_ALREADY_ALIGNED
23 
24 // Try to determine automatically if malloc is already aligned.
25 
26 // On 64-bit systems, glibc's malloc returns 16-byte-aligned pointers, see:
27 // http://www.gnu.org/s/libc/manual/html_node/Aligned-Memory-Blocks.html
28 // This is true at least since glibc 2.8.
29 // This leaves the question how to detect 64-bit. According to this document,
30 // http://gcc.fyxm.net/summit/2003/Porting%20to%2064%20bit.pdf
31 // page 114, "[The] LP64 model [...] is used by all 64-bit UNIX ports" so it's indeed
32 // quite safe, at least within the context of glibc, to equate 64-bit with LP64.
33 #if defined(__GLIBC__) && ((__GLIBC__ >= 2 && __GLIBC_MINOR__ >= 8) || __GLIBC__ > 2) && defined(__LP64__) && \
34  !defined(__SANITIZE_ADDRESS__) && (EIGEN_DEFAULT_ALIGN_BYTES == 16)
35 #define EIGEN_GLIBC_MALLOC_ALREADY_ALIGNED 1
36 #else
37 #define EIGEN_GLIBC_MALLOC_ALREADY_ALIGNED 0
38 #endif
39 
40 // FreeBSD 6 seems to have 16-byte aligned malloc
41 // See http://svn.freebsd.org/viewvc/base/stable/6/lib/libc/stdlib/malloc.c?view=markup
42 // FreeBSD 7 seems to have 16-byte aligned malloc except on ARM and MIPS architectures
43 // See http://svn.freebsd.org/viewvc/base/stable/7/lib/libc/stdlib/malloc.c?view=markup
44 #if defined(__FreeBSD__) && !(EIGEN_ARCH_ARM || EIGEN_ARCH_MIPS) && (EIGEN_DEFAULT_ALIGN_BYTES == 16)
45 #define EIGEN_FREEBSD_MALLOC_ALREADY_ALIGNED 1
46 #else
47 #define EIGEN_FREEBSD_MALLOC_ALREADY_ALIGNED 0
48 #endif
49 
50 #if (EIGEN_OS_MAC && (EIGEN_DEFAULT_ALIGN_BYTES == 16)) || (EIGEN_OS_WIN64 && (EIGEN_DEFAULT_ALIGN_BYTES == 16)) || \
51  EIGEN_GLIBC_MALLOC_ALREADY_ALIGNED || EIGEN_FREEBSD_MALLOC_ALREADY_ALIGNED
52 #define EIGEN_MALLOC_ALREADY_ALIGNED 1
53 #else
54 #define EIGEN_MALLOC_ALREADY_ALIGNED 0
55 #endif
56 
57 #endif
58 
59 #ifndef EIGEN_MALLOC_CHECK_THREAD_LOCAL
60 
61 // Check whether we can use the thread_local keyword to allow or disallow
62 // allocating memory with per-thread granularity, by means of the
63 // set_is_malloc_allowed() function.
64 #ifndef EIGEN_AVOID_THREAD_LOCAL
65 
66 #if ((EIGEN_COMP_GNUC) || __has_feature(cxx_thread_local) || EIGEN_COMP_MSVC >= 1900) && \
67  !defined(EIGEN_GPU_COMPILE_PHASE)
68 #define EIGEN_MALLOC_CHECK_THREAD_LOCAL thread_local
69 #else
70 #define EIGEN_MALLOC_CHECK_THREAD_LOCAL
71 #endif
72 
73 #else // EIGEN_AVOID_THREAD_LOCAL
74 #define EIGEN_MALLOC_CHECK_THREAD_LOCAL
75 #endif // EIGEN_AVOID_THREAD_LOCAL
76 
77 #endif
78 
79 // IWYU pragma: private
80 #include "../InternalHeaderCheck.h"
81 
82 namespace Eigen {
83 
84 namespace internal {
85 
86 /*****************************************************************************
87 *** Implementation of portable aligned versions of malloc/free/realloc ***
88 *****************************************************************************/
89 
90 #ifdef EIGEN_NO_MALLOC
91 EIGEN_DEVICE_FUNC inline void check_that_malloc_is_allowed() {
92  eigen_assert(false && "heap allocation is forbidden (EIGEN_NO_MALLOC is defined)");
93 }
94 EIGEN_DEVICE_FUNC inline void check_that_free_is_allowed() {
95  eigen_assert(false && "heap deallocation is forbidden (EIGEN_NO_MALLOC is defined)");
96 }
97 #elif defined EIGEN_RUNTIME_NO_MALLOC
98 EIGEN_DEVICE_FUNC inline bool is_malloc_allowed_impl(bool update, bool new_value = false) {
99  EIGEN_MALLOC_CHECK_THREAD_LOCAL static bool value = true;
100  if (update == 1) value = new_value;
101  return value;
102 }
103 EIGEN_DEVICE_FUNC inline bool is_malloc_allowed() { return is_malloc_allowed_impl(false); }
104 EIGEN_DEVICE_FUNC inline bool set_is_malloc_allowed(bool new_value) { return is_malloc_allowed_impl(true, new_value); }
105 EIGEN_DEVICE_FUNC inline void check_that_malloc_is_allowed() {
106  eigen_assert(is_malloc_allowed() &&
107  "heap allocation is forbidden (EIGEN_RUNTIME_NO_MALLOC is defined and set_is_malloc_allowed is false)");
108 }
109 EIGEN_DEVICE_FUNC inline bool is_free_allowed_impl(bool update, bool new_value = false) {
110  EIGEN_MALLOC_CHECK_THREAD_LOCAL static bool value = true;
111  if (update == 1) value = new_value;
112  return value;
113 }
114 EIGEN_DEVICE_FUNC inline bool is_free_allowed() { return is_free_allowed_impl(false); }
115 EIGEN_DEVICE_FUNC inline bool set_is_free_allowed(bool new_value) { return is_free_allowed_impl(true, new_value); }
116 EIGEN_DEVICE_FUNC inline void check_that_free_is_allowed() {
117  eigen_assert(is_malloc_allowed() &&
118  "heap deallocation is forbidden (EIGEN_RUNTIME_NO_MALLOC is defined and set_is_free_allowed is false)");
119 }
120 #else
121 EIGEN_DEVICE_FUNC inline void check_that_malloc_is_allowed() {}
122 EIGEN_DEVICE_FUNC inline void check_that_free_is_allowed() {}
123 #endif
124 
125 EIGEN_DEVICE_FUNC inline void throw_std_bad_alloc() {
126 #ifdef EIGEN_EXCEPTIONS
127  throw std::bad_alloc();
128 #else
129  std::size_t huge = static_cast<std::size_t>(-1);
130 #if defined(EIGEN_HIPCC)
131  //
132  // calls to "::operator new" are to be treated as opaque function calls (i.e no inlining),
133  // and as a consequence the code in the #else block triggers the hipcc warning :
134  // "no overloaded function has restriction specifiers that are compatible with the ambient context"
135  //
136  // "throw_std_bad_alloc" has the EIGEN_DEVICE_FUNC attribute, so it seems that hipcc expects
137  // the same on "operator new"
138  // Reverting code back to the old version in this #if block for the hipcc compiler
139  //
140  new int[huge];
141 #else
142  void* unused = ::operator new(huge);
143  EIGEN_UNUSED_VARIABLE(unused);
144 #endif
145 #endif
146 }
147 
148 /*****************************************************************************
149 *** Implementation of handmade aligned functions ***
150 *****************************************************************************/
151 
152 /* ----- Hand made implementations of aligned malloc/free and realloc ----- */
153 
157 EIGEN_DEVICE_FUNC inline void* handmade_aligned_malloc(std::size_t size,
158  std::size_t alignment = EIGEN_DEFAULT_ALIGN_BYTES) {
159  eigen_assert(alignment >= sizeof(void*) && alignment <= 256 && (alignment & (alignment - 1)) == 0 &&
160  "Alignment must be at least sizeof(void*), less than or equal to 256, and a power of 2");
161 
162  check_that_malloc_is_allowed();
163  EIGEN_USING_STD(malloc)
164  void* original = malloc(size + alignment);
165  if (original == nullptr) return nullptr;
166  std::size_t offset = alignment - (reinterpret_cast<std::size_t>(original) & (alignment - 1));
167  void* aligned = static_cast<void*>(static_cast<uint8_t*>(original) + offset);
168  // Store offset - 1, since it is guaranteed to be at least 1.
169  *(static_cast<uint8_t*>(aligned) - 1) = static_cast<uint8_t>(offset - 1);
170  return aligned;
171 }
172 
174 EIGEN_DEVICE_FUNC inline void handmade_aligned_free(void* ptr) {
175  if (ptr != nullptr) {
176  std::size_t offset = static_cast<std::size_t>(*(static_cast<uint8_t*>(ptr) - 1)) + 1;
177  void* original = static_cast<void*>(static_cast<uint8_t*>(ptr) - offset);
178 
179  check_that_free_is_allowed();
180  EIGEN_USING_STD(free)
181  free(original);
182  }
183 }
184 
190 EIGEN_DEVICE_FUNC inline void* handmade_aligned_realloc(void* ptr, std::size_t new_size, std::size_t old_size,
191  std::size_t alignment = EIGEN_DEFAULT_ALIGN_BYTES) {
192  if (ptr == nullptr) return handmade_aligned_malloc(new_size, alignment);
193  std::size_t old_offset = static_cast<std::size_t>(*(static_cast<uint8_t*>(ptr) - 1)) + 1;
194  void* old_original = static_cast<uint8_t*>(ptr) - old_offset;
195 
196  check_that_malloc_is_allowed();
197  EIGEN_USING_STD(realloc)
198  void* original = realloc(old_original, new_size + alignment);
199  if (original == nullptr) return nullptr;
200  if (original == old_original) return ptr;
201  std::size_t offset = alignment - (reinterpret_cast<std::size_t>(original) & (alignment - 1));
202  void* aligned = static_cast<void*>(static_cast<uint8_t*>(original) + offset);
203  if (offset != old_offset) {
204  const void* src = static_cast<const void*>(static_cast<uint8_t*>(original) + old_offset);
205  std::size_t count = (std::min)(new_size, old_size);
206  std::memmove(aligned, src, count);
207  }
208  // Store offset - 1, since it is guaranteed to be at least 1.
209  *(static_cast<uint8_t*>(aligned) - 1) = static_cast<uint8_t>(offset - 1);
210  return aligned;
211 }
212 
216 EIGEN_DEVICE_FUNC inline void* aligned_malloc(std::size_t size) {
217  if (size == 0) return nullptr;
218 
219  void* result;
220 #if (EIGEN_DEFAULT_ALIGN_BYTES == 0) || EIGEN_MALLOC_ALREADY_ALIGNED
221 
222  check_that_malloc_is_allowed();
223  EIGEN_USING_STD(malloc)
224  result = malloc(size);
225 
226 #if EIGEN_DEFAULT_ALIGN_BYTES == 16
227  eigen_assert((size < 16 || (std::size_t(result) % 16) == 0) &&
228  "System's malloc returned an unaligned pointer. Compile with EIGEN_MALLOC_ALREADY_ALIGNED=0 to fallback "
229  "to handmade aligned memory allocator.");
230 #endif
231 #else
232  result = handmade_aligned_malloc(size);
233 #endif
234 
235  if (!result && size) throw_std_bad_alloc();
236 
237  return result;
238 }
239 
241 EIGEN_DEVICE_FUNC inline void aligned_free(void* ptr) {
242 #if (EIGEN_DEFAULT_ALIGN_BYTES == 0) || EIGEN_MALLOC_ALREADY_ALIGNED
243 
244  if (ptr != nullptr) {
245  check_that_free_is_allowed();
246  EIGEN_USING_STD(free)
247  free(ptr);
248  }
249 
250 #else
251  handmade_aligned_free(ptr);
252 #endif
253 }
254 
260 EIGEN_DEVICE_FUNC inline void* aligned_realloc(void* ptr, std::size_t new_size, std::size_t old_size) {
261  if (ptr == nullptr) return aligned_malloc(new_size);
262  if (old_size == new_size) return ptr;
263  if (new_size == 0) {
264  aligned_free(ptr);
265  return nullptr;
266  }
267 
268  void* result;
269 #if (EIGEN_DEFAULT_ALIGN_BYTES == 0) || EIGEN_MALLOC_ALREADY_ALIGNED
270  EIGEN_UNUSED_VARIABLE(old_size)
271 
272  check_that_malloc_is_allowed();
273  EIGEN_USING_STD(realloc)
274  result = realloc(ptr, new_size);
275 #else
276  result = handmade_aligned_realloc(ptr, new_size, old_size);
277 #endif
278 
279  if (!result && new_size) throw_std_bad_alloc();
280 
281  return result;
282 }
283 
284 /*****************************************************************************
285 *** Implementation of conditionally aligned functions ***
286 *****************************************************************************/
287 
291 template <bool Align>
292 EIGEN_DEVICE_FUNC inline void* conditional_aligned_malloc(std::size_t size) {
293  return aligned_malloc(size);
294 }
295 
296 template <>
297 EIGEN_DEVICE_FUNC inline void* conditional_aligned_malloc<false>(std::size_t size) {
298  if (size == 0) return nullptr;
299 
300  check_that_malloc_is_allowed();
301  EIGEN_USING_STD(malloc)
302  void* result = malloc(size);
303 
304  if (!result && size) throw_std_bad_alloc();
305  return result;
306 }
307 
309 template <bool Align>
310 EIGEN_DEVICE_FUNC inline void conditional_aligned_free(void* ptr) {
311  aligned_free(ptr);
312 }
313 
314 template <>
315 EIGEN_DEVICE_FUNC inline void conditional_aligned_free<false>(void* ptr) {
316  if (ptr != nullptr) {
317  check_that_free_is_allowed();
318  EIGEN_USING_STD(free)
319  free(ptr);
320  }
321 }
322 
323 template <bool Align>
324 EIGEN_DEVICE_FUNC inline void* conditional_aligned_realloc(void* ptr, std::size_t new_size, std::size_t old_size) {
325  return aligned_realloc(ptr, new_size, old_size);
326 }
327 
328 template <>
329 EIGEN_DEVICE_FUNC inline void* conditional_aligned_realloc<false>(void* ptr, std::size_t new_size,
330  std::size_t old_size) {
331  if (ptr == nullptr) return conditional_aligned_malloc<false>(new_size);
332  if (old_size == new_size) return ptr;
333  if (new_size == 0) {
334  conditional_aligned_free<false>(ptr);
335  return nullptr;
336  }
337 
338  check_that_malloc_is_allowed();
339  EIGEN_USING_STD(realloc)
340  return realloc(ptr, new_size);
341 }
342 
343 /*****************************************************************************
344 *** Construction/destruction of array elements ***
345 *****************************************************************************/
346 
350 template <typename T>
351 EIGEN_DEVICE_FUNC inline void destruct_elements_of_array(T* ptr, std::size_t size) {
352  // always destruct an array starting from the end.
353  if (ptr)
354  while (size) ptr[--size].~T();
355 }
356 
360 template <typename T>
361 EIGEN_DEVICE_FUNC inline T* default_construct_elements_of_array(T* ptr, std::size_t size) {
362  std::size_t i = 0;
363  EIGEN_TRY {
364  for (i = 0; i < size; ++i) ::new (ptr + i) T;
365  }
366  EIGEN_CATCH(...) {
367  destruct_elements_of_array(ptr, i);
368  EIGEN_THROW;
369  }
370  return ptr;
371 }
372 
376 template <typename T>
377 EIGEN_DEVICE_FUNC inline T* copy_construct_elements_of_array(T* ptr, const T* src, std::size_t size) {
378  std::size_t i = 0;
379  EIGEN_TRY {
380  for (i = 0; i < size; ++i) ::new (ptr + i) T(*(src + i));
381  }
382  EIGEN_CATCH(...) {
383  destruct_elements_of_array(ptr, i);
384  EIGEN_THROW;
385  }
386  return ptr;
387 }
388 
392 template <typename T>
393 EIGEN_DEVICE_FUNC inline T* move_construct_elements_of_array(T* ptr, T* src, std::size_t size) {
394  std::size_t i = 0;
395  EIGEN_TRY {
396  for (i = 0; i < size; ++i) ::new (ptr + i) T(std::move(*(src + i)));
397  }
398  EIGEN_CATCH(...) {
399  destruct_elements_of_array(ptr, i);
400  EIGEN_THROW;
401  }
402  return ptr;
403 }
404 
405 /*****************************************************************************
406 *** Implementation of aligned new/delete-like functions ***
407 *****************************************************************************/
408 
409 template <typename T>
410 EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE void check_size_for_overflow(std::size_t size) {
411  constexpr std::size_t max_elements = (std::numeric_limits<std::ptrdiff_t>::max)() / sizeof(T);
412  if (size > max_elements) throw_std_bad_alloc();
413 }
414 
419 template <typename T>
420 EIGEN_DEVICE_FUNC inline T* aligned_new(std::size_t size) {
421  check_size_for_overflow<T>(size);
422  T* result = static_cast<T*>(aligned_malloc(sizeof(T) * size));
423  EIGEN_TRY { return default_construct_elements_of_array(result, size); }
424  EIGEN_CATCH(...) {
425  aligned_free(result);
426  EIGEN_THROW;
427  }
428  return result;
429 }
430 
431 template <typename T, bool Align>
432 EIGEN_DEVICE_FUNC inline T* conditional_aligned_new(std::size_t size) {
433  check_size_for_overflow<T>(size);
434  T* result = static_cast<T*>(conditional_aligned_malloc<Align>(sizeof(T) * size));
435  EIGEN_TRY { return default_construct_elements_of_array(result, size); }
436  EIGEN_CATCH(...) {
437  conditional_aligned_free<Align>(result);
438  EIGEN_THROW;
439  }
440  return result;
441 }
442 
446 template <typename T>
447 EIGEN_DEVICE_FUNC inline void aligned_delete(T* ptr, std::size_t size) {
448  destruct_elements_of_array<T>(ptr, size);
449  aligned_free(ptr);
450 }
451 
455 template <typename T, bool Align>
456 EIGEN_DEVICE_FUNC inline void conditional_aligned_delete(T* ptr, std::size_t size) {
457  destruct_elements_of_array<T>(ptr, size);
458  conditional_aligned_free<Align>(ptr);
459 }
460 
461 template <typename T, bool Align>
462 EIGEN_DEVICE_FUNC inline T* conditional_aligned_realloc_new(T* pts, std::size_t new_size, std::size_t old_size) {
463  check_size_for_overflow<T>(new_size);
464  check_size_for_overflow<T>(old_size);
465 
466  // If elements need to be explicitly initialized, we cannot simply realloc
467  // (or memcpy) the memory block - each element needs to be reconstructed.
468  // Otherwise, objects that contain internal pointers like mpfr or
469  // AnnoyingScalar can be pointing to the wrong thing.
470  T* result = static_cast<T*>(conditional_aligned_malloc<Align>(sizeof(T) * new_size));
471  EIGEN_TRY {
472  // Move-construct initial elements.
473  std::size_t copy_size = (std::min)(old_size, new_size);
474  move_construct_elements_of_array(result, pts, copy_size);
475 
476  // Default-construct remaining elements.
477  if (new_size > old_size) {
478  default_construct_elements_of_array(result + copy_size, new_size - old_size);
479  }
480 
481  // Delete old elements.
482  conditional_aligned_delete<T, Align>(pts, old_size);
483  }
484  EIGEN_CATCH(...) {
485  conditional_aligned_free<Align>(result);
486  EIGEN_THROW;
487  }
488 
489  return result;
490 }
491 
492 template <typename T, bool Align>
493 EIGEN_DEVICE_FUNC inline T* conditional_aligned_new_auto(std::size_t size) {
494  if (size == 0) return nullptr; // short-cut. Also fixes Bug 884
495  check_size_for_overflow<T>(size);
496  T* result = static_cast<T*>(conditional_aligned_malloc<Align>(sizeof(T) * size));
497  if (NumTraits<T>::RequireInitialization) {
498  EIGEN_TRY { default_construct_elements_of_array(result, size); }
499  EIGEN_CATCH(...) {
500  conditional_aligned_free<Align>(result);
501  EIGEN_THROW;
502  }
503  }
504  return result;
505 }
506 
507 template <typename T, bool Align>
508 EIGEN_DEVICE_FUNC inline T* conditional_aligned_realloc_new_auto(T* pts, std::size_t new_size, std::size_t old_size) {
509  if (NumTraits<T>::RequireInitialization) {
510  return conditional_aligned_realloc_new<T, Align>(pts, new_size, old_size);
511  }
512 
513  check_size_for_overflow<T>(new_size);
514  check_size_for_overflow<T>(old_size);
515  return static_cast<T*>(
516  conditional_aligned_realloc<Align>(static_cast<void*>(pts), sizeof(T) * new_size, sizeof(T) * old_size));
517 }
518 
519 template <typename T, bool Align>
520 EIGEN_DEVICE_FUNC inline void conditional_aligned_delete_auto(T* ptr, std::size_t size) {
521  if (NumTraits<T>::RequireInitialization) destruct_elements_of_array<T>(ptr, size);
522  conditional_aligned_free<Align>(ptr);
523 }
524 
525 /****************************************************************************/
526 
545 template <int Alignment, typename Scalar, typename Index>
546 EIGEN_DEVICE_FUNC inline Index first_aligned(const Scalar* array, Index size) {
547  const Index ScalarSize = sizeof(Scalar);
548  const Index AlignmentSize = Alignment / ScalarSize;
549  const Index AlignmentMask = AlignmentSize - 1;
550 
551  if (AlignmentSize <= 1) {
552  // Either the requested alignment if smaller than a scalar, or it exactly match a 1 scalar
553  // so that all elements of the array have the same alignment.
554  return 0;
555  } else if ((std::uintptr_t(array) & (sizeof(Scalar) - 1)) || (Alignment % ScalarSize) != 0) {
556  // The array is not aligned to the size of a single scalar, or the requested alignment is not a multiple of the
557  // scalar size. Consequently, no element of the array is well aligned.
558  return size;
559  } else {
560  Index first = (AlignmentSize - (Index((std::uintptr_t(array) / sizeof(Scalar))) & AlignmentMask)) & AlignmentMask;
561  return (first < size) ? first : size;
562  }
563 }
564 
567 template <typename Scalar, typename Index>
568 EIGEN_DEVICE_FUNC inline Index first_default_aligned(const Scalar* array, Index size) {
569  typedef typename packet_traits<Scalar>::type DefaultPacketType;
570  return first_aligned<unpacket_traits<DefaultPacketType>::alignment>(array, size);
571 }
572 
575 template <typename Index>
576 inline Index first_multiple(Index size, Index base) {
577  return ((size + base - 1) / base) * base;
578 }
579 
580 // std::copy is much slower than memcpy, so let's introduce a smart_copy which
581 // use memcpy on trivial types, i.e., on types that does not require an initialization ctor.
582 template <typename T, bool UseMemcpy>
583 struct smart_copy_helper;
584 
585 template <typename T>
586 EIGEN_DEVICE_FUNC void smart_copy(const T* start, const T* end, T* target) {
587  smart_copy_helper<T, !NumTraits<T>::RequireInitialization>::run(start, end, target);
588 }
589 
590 template <typename T>
591 struct smart_copy_helper<T, true> {
592  EIGEN_DEVICE_FUNC static inline void run(const T* start, const T* end, T* target) {
593  std::intptr_t size = std::intptr_t(end) - std::intptr_t(start);
594  if (size == 0) return;
595  eigen_internal_assert(start != 0 && end != 0 && target != 0);
596  EIGEN_USING_STD(memcpy)
597  memcpy(target, start, size);
598  }
599 };
600 
601 template <typename T>
602 struct smart_copy_helper<T, false> {
603  EIGEN_DEVICE_FUNC static inline void run(const T* start, const T* end, T* target) { std::copy(start, end, target); }
604 };
605 
606 // intelligent memmove. falls back to std::memmove for POD types, uses std::copy otherwise.
607 template <typename T, bool UseMemmove>
608 struct smart_memmove_helper;
609 
610 template <typename T>
611 void smart_memmove(const T* start, const T* end, T* target) {
612  smart_memmove_helper<T, !NumTraits<T>::RequireInitialization>::run(start, end, target);
613 }
614 
615 template <typename T>
616 struct smart_memmove_helper<T, true> {
617  static inline void run(const T* start, const T* end, T* target) {
618  std::intptr_t size = std::intptr_t(end) - std::intptr_t(start);
619  if (size == 0) return;
620  eigen_internal_assert(start != 0 && end != 0 && target != 0);
621  std::memmove(target, start, size);
622  }
623 };
624 
625 template <typename T>
626 struct smart_memmove_helper<T, false> {
627  static inline void run(const T* start, const T* end, T* target) {
628  if (std::uintptr_t(target) < std::uintptr_t(start)) {
629  std::copy(start, end, target);
630  } else {
631  std::ptrdiff_t count = (std::ptrdiff_t(end) - std::ptrdiff_t(start)) / sizeof(T);
632  std::copy_backward(start, end, target + count);
633  }
634  }
635 };
636 
637 template <typename T>
638 EIGEN_DEVICE_FUNC T* smart_move(T* start, T* end, T* target) {
639  return std::move(start, end, target);
640 }
641 
642 /*****************************************************************************
643 *** Implementation of runtime stack allocation (falling back to malloc) ***
644 *****************************************************************************/
645 
646 // you can overwrite Eigen's default behavior regarding alloca by defining EIGEN_ALLOCA
647 // to the appropriate stack allocation function
648 #if !defined EIGEN_ALLOCA && !defined EIGEN_GPU_COMPILE_PHASE
649 #if EIGEN_OS_LINUX || EIGEN_OS_MAC || (defined alloca)
650 #define EIGEN_ALLOCA alloca
651 #elif EIGEN_COMP_MSVC
652 #define EIGEN_ALLOCA _alloca
653 #endif
654 #endif
655 
656 // With clang -Oz -mthumb, alloca changes the stack pointer in a way that is
657 // not allowed in Thumb2. -DEIGEN_STACK_ALLOCATION_LIMIT=0 doesn't work because
658 // the compiler still emits bad code because stack allocation checks use "<=".
659 // TODO: Eliminate after https://bugs.llvm.org/show_bug.cgi?id=23772
660 // is fixed.
661 #if defined(__clang__) && defined(__thumb__)
662 #undef EIGEN_ALLOCA
663 #endif
664 
665 // This helper class construct the allocated memory, and takes care of destructing and freeing the handled data
666 // at destruction time. In practice this helper class is mainly useful to avoid memory leak in case of exceptions.
667 template <typename T>
668 class aligned_stack_memory_handler : noncopyable {
669  public:
670  /* Creates a stack_memory_handler responsible for the buffer \a ptr of size \a size.
671  * Note that \a ptr can be 0 regardless of the other parameters.
672  * This constructor takes care of constructing/initializing the elements of the buffer if required by the scalar type
673  *T (see NumTraits<T>::RequireInitialization). In this case, the buffer elements will also be destructed when this
674  *handler will be destructed. Finally, if \a dealloc is true, then the pointer \a ptr is freed.
675  **/
676  EIGEN_DEVICE_FUNC aligned_stack_memory_handler(T* ptr, std::size_t size, bool dealloc)
677  : m_ptr(ptr), m_size(size), m_deallocate(dealloc) {
678  if (NumTraits<T>::RequireInitialization && m_ptr) Eigen::internal::default_construct_elements_of_array(m_ptr, size);
679  }
680  EIGEN_DEVICE_FUNC ~aligned_stack_memory_handler() {
681  if (NumTraits<T>::RequireInitialization && m_ptr) Eigen::internal::destruct_elements_of_array<T>(m_ptr, m_size);
682  if (m_deallocate) Eigen::internal::aligned_free(m_ptr);
683  }
684 
685  protected:
686  T* m_ptr;
687  std::size_t m_size;
688  bool m_deallocate;
689 };
690 
691 #ifdef EIGEN_ALLOCA
692 
693 template <typename Xpr, int NbEvaluations,
694  bool MapExternalBuffer = nested_eval<Xpr, NbEvaluations>::Evaluate && Xpr::MaxSizeAtCompileTime == Dynamic>
695 struct local_nested_eval_wrapper {
696  static constexpr bool NeedExternalBuffer = false;
697  typedef typename Xpr::Scalar Scalar;
698  typedef typename nested_eval<Xpr, NbEvaluations>::type ObjectType;
699  ObjectType object;
700 
701  EIGEN_DEVICE_FUNC local_nested_eval_wrapper(const Xpr& xpr, Scalar* ptr) : object(xpr) {
702  EIGEN_UNUSED_VARIABLE(ptr);
703  eigen_internal_assert(ptr == 0);
704  }
705 };
706 
707 template <typename Xpr, int NbEvaluations>
708 struct local_nested_eval_wrapper<Xpr, NbEvaluations, true> {
709  static constexpr bool NeedExternalBuffer = true;
710  typedef typename Xpr::Scalar Scalar;
711  typedef typename plain_object_eval<Xpr>::type PlainObject;
712  typedef Map<PlainObject, EIGEN_DEFAULT_ALIGN_BYTES> ObjectType;
713  ObjectType object;
714 
715  EIGEN_DEVICE_FUNC local_nested_eval_wrapper(const Xpr& xpr, Scalar* ptr)
716  : object(ptr == 0 ? reinterpret_cast<Scalar*>(Eigen::internal::aligned_malloc(sizeof(Scalar) * xpr.size())) : ptr,
717  xpr.rows(), xpr.cols()),
718  m_deallocate(ptr == 0) {
719  if (NumTraits<Scalar>::RequireInitialization && object.data())
720  Eigen::internal::default_construct_elements_of_array(object.data(), object.size());
721  object = xpr;
722  }
723 
724  EIGEN_DEVICE_FUNC ~local_nested_eval_wrapper() {
725  if (NumTraits<Scalar>::RequireInitialization && object.data())
726  Eigen::internal::destruct_elements_of_array(object.data(), object.size());
727  if (m_deallocate) Eigen::internal::aligned_free(object.data());
728  }
729 
730  private:
731  bool m_deallocate;
732 };
733 
734 #endif // EIGEN_ALLOCA
735 
736 template <typename T>
737 class scoped_array : noncopyable {
738  T* m_ptr;
739 
740  public:
741  explicit scoped_array(std::ptrdiff_t size) { m_ptr = new T[size]; }
742  ~scoped_array() { delete[] m_ptr; }
743  T& operator[](std::ptrdiff_t i) { return m_ptr[i]; }
744  const T& operator[](std::ptrdiff_t i) const { return m_ptr[i]; }
745  T*& ptr() { return m_ptr; }
746  const T* ptr() const { return m_ptr; }
747  operator const T*() const { return m_ptr; }
748 };
749 
750 template <typename T>
751 void swap(scoped_array<T>& a, scoped_array<T>& b) {
752  std::swap(a.ptr(), b.ptr());
753 }
754 
755 } // end namespace internal
756 
780 #if defined(EIGEN_ALLOCA) && !defined(EIGEN_NO_ALLOCA)
781 
782 #if EIGEN_DEFAULT_ALIGN_BYTES > 0
783 // We always manually re-align the result of EIGEN_ALLOCA.
784 // If alloca is already aligned, the compiler should be smart enough to optimize away the re-alignment.
785 
786 #if ((EIGEN_COMP_GNUC || EIGEN_COMP_CLANG) && !EIGEN_COMP_NVHPC)
787 #define EIGEN_ALIGNED_ALLOCA(SIZE) __builtin_alloca_with_align(SIZE, CHAR_BIT* EIGEN_DEFAULT_ALIGN_BYTES)
788 #else
789 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void* eigen_aligned_alloca_helper(void* ptr) {
790  constexpr std::uintptr_t mask = EIGEN_DEFAULT_ALIGN_BYTES - 1;
791  std::uintptr_t ptr_int = std::uintptr_t(ptr);
792  std::uintptr_t aligned_ptr_int = (ptr_int + mask) & ~mask;
793  std::uintptr_t offset = aligned_ptr_int - ptr_int;
794  return static_cast<void*>(static_cast<uint8_t*>(ptr) + offset);
795 }
796 #define EIGEN_ALIGNED_ALLOCA(SIZE) eigen_aligned_alloca_helper(EIGEN_ALLOCA(SIZE + EIGEN_DEFAULT_ALIGN_BYTES - 1))
797 #endif
798 
799 #else
800 #define EIGEN_ALIGNED_ALLOCA(SIZE) EIGEN_ALLOCA(SIZE)
801 #endif
802 
803 #define ei_declare_aligned_stack_constructed_variable(TYPE, NAME, SIZE, BUFFER) \
804  Eigen::internal::check_size_for_overflow<TYPE>(SIZE); \
805  TYPE* NAME = (BUFFER) != 0 ? (BUFFER) \
806  : reinterpret_cast<TYPE*>((sizeof(TYPE) * (SIZE) <= EIGEN_STACK_ALLOCATION_LIMIT) \
807  ? EIGEN_ALIGNED_ALLOCA(sizeof(TYPE) * (SIZE)) \
808  : Eigen::internal::aligned_malloc(sizeof(TYPE) * (SIZE))); \
809  Eigen::internal::aligned_stack_memory_handler<TYPE> EIGEN_CAT(NAME, _stack_memory_destructor)( \
810  (BUFFER) == 0 ? NAME : 0, SIZE, sizeof(TYPE) * (SIZE) > EIGEN_STACK_ALLOCATION_LIMIT)
811 
812 #define ei_declare_local_nested_eval(XPR_T, XPR, N, NAME) \
813  Eigen::internal::local_nested_eval_wrapper<XPR_T, N> EIGEN_CAT(NAME, _wrapper)( \
814  XPR, reinterpret_cast<typename XPR_T::Scalar*>( \
815  ((Eigen::internal::local_nested_eval_wrapper<XPR_T, N>::NeedExternalBuffer) && \
816  ((sizeof(typename XPR_T::Scalar) * XPR.size()) <= EIGEN_STACK_ALLOCATION_LIMIT)) \
817  ? EIGEN_ALIGNED_ALLOCA(sizeof(typename XPR_T::Scalar) * XPR.size()) \
818  : 0)); \
819  typename Eigen::internal::local_nested_eval_wrapper<XPR_T, N>::ObjectType NAME(EIGEN_CAT(NAME, _wrapper).object)
820 
821 #else
822 
823 #define ei_declare_aligned_stack_constructed_variable(TYPE, NAME, SIZE, BUFFER) \
824  Eigen::internal::check_size_for_overflow<TYPE>(SIZE); \
825  TYPE* NAME = \
826  (BUFFER) != 0 ? BUFFER : reinterpret_cast<TYPE*>(Eigen::internal::aligned_malloc(sizeof(TYPE) * (SIZE))); \
827  Eigen::internal::aligned_stack_memory_handler<TYPE> EIGEN_CAT(NAME, _stack_memory_destructor)( \
828  (BUFFER) == 0 ? NAME : 0, SIZE, true)
829 
830 #define ei_declare_local_nested_eval(XPR_T, XPR, N, NAME) \
831  typename Eigen::internal::nested_eval<XPR_T, N>::type NAME(XPR)
832 
833 #endif
834 
835 /*****************************************************************************
836 *** Implementation of EIGEN_MAKE_ALIGNED_OPERATOR_NEW [_IF] ***
837 *****************************************************************************/
838 
839 #if EIGEN_HAS_CXX17_OVERALIGN
840 
841 // C++17 -> no need to bother about alignment anymore :)
842 
843 #define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_NOTHROW(NeedsToAlign)
844 #define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(NeedsToAlign)
845 #define EIGEN_MAKE_ALIGNED_OPERATOR_NEW
846 #define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF_VECTORIZABLE_FIXED_SIZE(Scalar, Size)
847 
848 #else
849 
850 // HIP does not support new/delete on device.
851 #if EIGEN_MAX_ALIGN_BYTES != 0 && !defined(EIGEN_HIP_DEVICE_COMPILE)
852 #define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_NOTHROW(NeedsToAlign) \
853  EIGEN_DEVICE_FUNC void* operator new(std::size_t size, const std::nothrow_t&) noexcept { \
854  EIGEN_TRY { return Eigen::internal::conditional_aligned_malloc<NeedsToAlign>(size); } \
855  EIGEN_CATCH(...) { return 0; } \
856  }
857 #define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(NeedsToAlign) \
858  EIGEN_DEVICE_FUNC void* operator new(std::size_t size) { \
859  return Eigen::internal::conditional_aligned_malloc<NeedsToAlign>(size); \
860  } \
861  EIGEN_DEVICE_FUNC void* operator new[](std::size_t size) { \
862  return Eigen::internal::conditional_aligned_malloc<NeedsToAlign>(size); \
863  } \
864  EIGEN_DEVICE_FUNC void operator delete(void* ptr) noexcept { \
865  Eigen::internal::conditional_aligned_free<NeedsToAlign>(ptr); \
866  } \
867  EIGEN_DEVICE_FUNC void operator delete[](void* ptr) noexcept { \
868  Eigen::internal::conditional_aligned_free<NeedsToAlign>(ptr); \
869  } \
870  EIGEN_DEVICE_FUNC void operator delete(void* ptr, std::size_t /* sz */) noexcept { \
871  Eigen::internal::conditional_aligned_free<NeedsToAlign>(ptr); \
872  } \
873  EIGEN_DEVICE_FUNC void operator delete[](void* ptr, std::size_t /* sz */) noexcept { \
874  Eigen::internal::conditional_aligned_free<NeedsToAlign>(ptr); \
875  } \
876  /* in-place new and delete. since (at least afaik) there is no actual */ \
877  /* memory allocated we can safely let the default implementation handle */ \
878  /* this particular case. */ \
879  EIGEN_DEVICE_FUNC static void* operator new(std::size_t size, void* ptr) { return ::operator new(size, ptr); } \
880  EIGEN_DEVICE_FUNC static void* operator new[](std::size_t size, void* ptr) { return ::operator new[](size, ptr); } \
881  EIGEN_DEVICE_FUNC void operator delete(void* memory, void* ptr) noexcept { return ::operator delete(memory, ptr); } \
882  EIGEN_DEVICE_FUNC void operator delete[](void* memory, void* ptr) noexcept { \
883  return ::operator delete[](memory, ptr); \
884  } \
885  /* nothrow-new (returns zero instead of std::bad_alloc) */ \
886  EIGEN_MAKE_ALIGNED_OPERATOR_NEW_NOTHROW(NeedsToAlign) \
887  EIGEN_DEVICE_FUNC void operator delete(void* ptr, const std::nothrow_t&) noexcept { \
888  Eigen::internal::conditional_aligned_free<NeedsToAlign>(ptr); \
889  } \
890  typedef void eigen_aligned_operator_new_marker_type;
891 #else
892 #define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(NeedsToAlign)
893 #endif
894 
895 #define EIGEN_MAKE_ALIGNED_OPERATOR_NEW EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(true)
896 #define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF_VECTORIZABLE_FIXED_SIZE(Scalar, Size) \
897  EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF( \
898  bool(((Size) != Eigen::Dynamic) && \
899  (((EIGEN_MAX_ALIGN_BYTES >= 16) && ((sizeof(Scalar) * (Size)) % (EIGEN_MAX_ALIGN_BYTES) == 0)) || \
900  ((EIGEN_MAX_ALIGN_BYTES >= 32) && ((sizeof(Scalar) * (Size)) % (EIGEN_MAX_ALIGN_BYTES / 2) == 0)) || \
901  ((EIGEN_MAX_ALIGN_BYTES >= 64) && ((sizeof(Scalar) * (Size)) % (EIGEN_MAX_ALIGN_BYTES / 4) == 0)))))
902 
903 #endif
904 
905 /****************************************************************************/
906 
931 template <class T>
933  public:
934  typedef std::size_t size_type;
935  typedef std::ptrdiff_t difference_type;
936  typedef T* pointer;
937  typedef const T* const_pointer;
938  typedef T& reference;
939  typedef const T& const_reference;
940  typedef T value_type;
941 
942  template <class U>
943  struct rebind {
944  typedef aligned_allocator<U> other;
945  };
946 
947  aligned_allocator() = default;
948 
949  aligned_allocator(const aligned_allocator&) = default;
950 
951  template <class U>
953 
954  template <class U>
955  constexpr bool operator==(const aligned_allocator<U>&) const noexcept {
956  return true;
957  }
958  template <class U>
959  constexpr bool operator!=(const aligned_allocator<U>&) const noexcept {
960  return false;
961  }
962 
963 #if EIGEN_COMP_GNUC_STRICT && EIGEN_GNUC_STRICT_AT_LEAST(7, 0, 0)
964  // In gcc std::allocator::max_size() is bugged making gcc triggers a warning:
965  // eigen/Eigen/src/Core/util/Memory.h:189:12: warning: argument 1 value '18446744073709551612' exceeds maximum object
966  // size 9223372036854775807 See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87544
967  size_type max_size() const { return (std::numeric_limits<std::ptrdiff_t>::max)() / sizeof(T); }
968 #endif
969 
970  pointer allocate(size_type num, const void* /*hint*/ = 0) {
971  internal::check_size_for_overflow<T>(num);
972  return static_cast<pointer>(internal::aligned_malloc(num * sizeof(T)));
973  }
974 
975  void deallocate(pointer p, size_type /*num*/) { internal::aligned_free(p); }
976 };
977 
978 //---------- Cache sizes ----------
979 
980 #if !defined(EIGEN_NO_CPUID)
981 #if EIGEN_COMP_GNUC && EIGEN_ARCH_i386_OR_x86_64
982 #if defined(__PIC__) && EIGEN_ARCH_i386
983 // Case for x86 with PIC
984 #define EIGEN_CPUID(abcd, func, id) \
985  __asm__ __volatile__("xchgl %%ebx, %k1;cpuid; xchgl %%ebx,%k1" \
986  : "=a"(abcd[0]), "=&r"(abcd[1]), "=c"(abcd[2]), "=d"(abcd[3]) \
987  : "a"(func), "c"(id));
988 #elif defined(__PIC__) && EIGEN_ARCH_x86_64
989 // Case for x64 with PIC. In theory this is only a problem with recent gcc and with medium or large code model, not with
990 // the default small code model. However, we cannot detect which code model is used, and the xchg overhead is negligible
991 // anyway.
992 #define EIGEN_CPUID(abcd, func, id) \
993  __asm__ __volatile__("xchg{q}\t{%%}rbx, %q1; cpuid; xchg{q}\t{%%}rbx, %q1" \
994  : "=a"(abcd[0]), "=&r"(abcd[1]), "=c"(abcd[2]), "=d"(abcd[3]) \
995  : "0"(func), "2"(id));
996 #else
997 // Case for x86_64 or x86 w/o PIC
998 #define EIGEN_CPUID(abcd, func, id) \
999  __asm__ __volatile__("cpuid" : "=a"(abcd[0]), "=b"(abcd[1]), "=c"(abcd[2]), "=d"(abcd[3]) : "0"(func), "2"(id));
1000 #endif
1001 #elif EIGEN_COMP_MSVC
1002 #if EIGEN_ARCH_i386_OR_x86_64
1003 #define EIGEN_CPUID(abcd, func, id) __cpuidex((int*)abcd, func, id)
1004 #endif
1005 #endif
1006 #endif
1007 
1008 namespace internal {
1009 
1010 #ifdef EIGEN_CPUID
1011 
1012 inline bool cpuid_is_vendor(int abcd[4], const int vendor[3]) {
1013  return abcd[1] == vendor[0] && abcd[3] == vendor[1] && abcd[2] == vendor[2];
1014 }
1015 
1016 inline void queryCacheSizes_intel_direct(int& l1, int& l2, int& l3) {
1017  int abcd[4];
1018  l1 = l2 = l3 = 0;
1019  int cache_id = 0;
1020  int cache_type = 0;
1021  do {
1022  abcd[0] = abcd[1] = abcd[2] = abcd[3] = 0;
1023  EIGEN_CPUID(abcd, 0x4, cache_id);
1024  cache_type = (abcd[0] & 0x0F) >> 0;
1025  if (cache_type == 1 || cache_type == 3) // data or unified cache
1026  {
1027  int cache_level = (abcd[0] & 0xE0) >> 5; // A[7:5]
1028  int ways = (abcd[1] & 0xFFC00000) >> 22; // B[31:22]
1029  int partitions = (abcd[1] & 0x003FF000) >> 12; // B[21:12]
1030  int line_size = (abcd[1] & 0x00000FFF) >> 0; // B[11:0]
1031  int sets = (abcd[2]); // C[31:0]
1032 
1033  int cache_size = (ways + 1) * (partitions + 1) * (line_size + 1) * (sets + 1);
1034 
1035  switch (cache_level) {
1036  case 1:
1037  l1 = cache_size;
1038  break;
1039  case 2:
1040  l2 = cache_size;
1041  break;
1042  case 3:
1043  l3 = cache_size;
1044  break;
1045  default:
1046  break;
1047  }
1048  }
1049  cache_id++;
1050  } while (cache_type > 0 && cache_id < 16);
1051 }
1052 
1053 inline void queryCacheSizes_intel_codes(int& l1, int& l2, int& l3) {
1054  int abcd[4];
1055  abcd[0] = abcd[1] = abcd[2] = abcd[3] = 0;
1056  l1 = l2 = l3 = 0;
1057  EIGEN_CPUID(abcd, 0x00000002, 0);
1058  unsigned char* bytes = reinterpret_cast<unsigned char*>(abcd) + 2;
1059  bool check_for_p2_core2 = false;
1060  for (int i = 0; i < 14; ++i) {
1061  switch (bytes[i]) {
1062  case 0x0A:
1063  l1 = 8;
1064  break; // 0Ah data L1 cache, 8 KB, 2 ways, 32 byte lines
1065  case 0x0C:
1066  l1 = 16;
1067  break; // 0Ch data L1 cache, 16 KB, 4 ways, 32 byte lines
1068  case 0x0E:
1069  l1 = 24;
1070  break; // 0Eh data L1 cache, 24 KB, 6 ways, 64 byte lines
1071  case 0x10:
1072  l1 = 16;
1073  break; // 10h data L1 cache, 16 KB, 4 ways, 32 byte lines (IA-64)
1074  case 0x15:
1075  l1 = 16;
1076  break; // 15h code L1 cache, 16 KB, 4 ways, 32 byte lines (IA-64)
1077  case 0x2C:
1078  l1 = 32;
1079  break; // 2Ch data L1 cache, 32 KB, 8 ways, 64 byte lines
1080  case 0x30:
1081  l1 = 32;
1082  break; // 30h code L1 cache, 32 KB, 8 ways, 64 byte lines
1083  case 0x60:
1084  l1 = 16;
1085  break; // 60h data L1 cache, 16 KB, 8 ways, 64 byte lines, sectored
1086  case 0x66:
1087  l1 = 8;
1088  break; // 66h data L1 cache, 8 KB, 4 ways, 64 byte lines, sectored
1089  case 0x67:
1090  l1 = 16;
1091  break; // 67h data L1 cache, 16 KB, 4 ways, 64 byte lines, sectored
1092  case 0x68:
1093  l1 = 32;
1094  break; // 68h data L1 cache, 32 KB, 4 ways, 64 byte lines, sectored
1095  case 0x1A:
1096  l2 = 96;
1097  break; // code and data L2 cache, 96 KB, 6 ways, 64 byte lines (IA-64)
1098  case 0x22:
1099  l3 = 512;
1100  break; // code and data L3 cache, 512 KB, 4 ways (!), 64 byte lines, dual-sectored
1101  case 0x23:
1102  l3 = 1024;
1103  break; // code and data L3 cache, 1024 KB, 8 ways, 64 byte lines, dual-sectored
1104  case 0x25:
1105  l3 = 2048;
1106  break; // code and data L3 cache, 2048 KB, 8 ways, 64 byte lines, dual-sectored
1107  case 0x29:
1108  l3 = 4096;
1109  break; // code and data L3 cache, 4096 KB, 8 ways, 64 byte lines, dual-sectored
1110  case 0x39:
1111  l2 = 128;
1112  break; // code and data L2 cache, 128 KB, 4 ways, 64 byte lines, sectored
1113  case 0x3A:
1114  l2 = 192;
1115  break; // code and data L2 cache, 192 KB, 6 ways, 64 byte lines, sectored
1116  case 0x3B:
1117  l2 = 128;
1118  break; // code and data L2 cache, 128 KB, 2 ways, 64 byte lines, sectored
1119  case 0x3C:
1120  l2 = 256;
1121  break; // code and data L2 cache, 256 KB, 4 ways, 64 byte lines, sectored
1122  case 0x3D:
1123  l2 = 384;
1124  break; // code and data L2 cache, 384 KB, 6 ways, 64 byte lines, sectored
1125  case 0x3E:
1126  l2 = 512;
1127  break; // code and data L2 cache, 512 KB, 4 ways, 64 byte lines, sectored
1128  case 0x40:
1129  l2 = 0;
1130  break; // no integrated L2 cache (P6 core) or L3 cache (P4 core)
1131  case 0x41:
1132  l2 = 128;
1133  break; // code and data L2 cache, 128 KB, 4 ways, 32 byte lines
1134  case 0x42:
1135  l2 = 256;
1136  break; // code and data L2 cache, 256 KB, 4 ways, 32 byte lines
1137  case 0x43:
1138  l2 = 512;
1139  break; // code and data L2 cache, 512 KB, 4 ways, 32 byte lines
1140  case 0x44:
1141  l2 = 1024;
1142  break; // code and data L2 cache, 1024 KB, 4 ways, 32 byte lines
1143  case 0x45:
1144  l2 = 2048;
1145  break; // code and data L2 cache, 2048 KB, 4 ways, 32 byte lines
1146  case 0x46:
1147  l3 = 4096;
1148  break; // code and data L3 cache, 4096 KB, 4 ways, 64 byte lines
1149  case 0x47:
1150  l3 = 8192;
1151  break; // code and data L3 cache, 8192 KB, 8 ways, 64 byte lines
1152  case 0x48:
1153  l2 = 3072;
1154  break; // code and data L2 cache, 3072 KB, 12 ways, 64 byte lines
1155  case 0x49:
1156  if (l2 != 0)
1157  l3 = 4096;
1158  else {
1159  check_for_p2_core2 = true;
1160  l3 = l2 = 4096;
1161  }
1162  break; // code and data L3 cache, 4096 KB, 16 ways, 64 byte lines (P4) or L2 for core2
1163  case 0x4A:
1164  l3 = 6144;
1165  break; // code and data L3 cache, 6144 KB, 12 ways, 64 byte lines
1166  case 0x4B:
1167  l3 = 8192;
1168  break; // code and data L3 cache, 8192 KB, 16 ways, 64 byte lines
1169  case 0x4C:
1170  l3 = 12288;
1171  break; // code and data L3 cache, 12288 KB, 12 ways, 64 byte lines
1172  case 0x4D:
1173  l3 = 16384;
1174  break; // code and data L3 cache, 16384 KB, 16 ways, 64 byte lines
1175  case 0x4E:
1176  l2 = 6144;
1177  break; // code and data L2 cache, 6144 KB, 24 ways, 64 byte lines
1178  case 0x78:
1179  l2 = 1024;
1180  break; // code and data L2 cache, 1024 KB, 4 ways, 64 byte lines
1181  case 0x79:
1182  l2 = 128;
1183  break; // code and data L2 cache, 128 KB, 8 ways, 64 byte lines, dual-sectored
1184  case 0x7A:
1185  l2 = 256;
1186  break; // code and data L2 cache, 256 KB, 8 ways, 64 byte lines, dual-sectored
1187  case 0x7B:
1188  l2 = 512;
1189  break; // code and data L2 cache, 512 KB, 8 ways, 64 byte lines, dual-sectored
1190  case 0x7C:
1191  l2 = 1024;
1192  break; // code and data L2 cache, 1024 KB, 8 ways, 64 byte lines, dual-sectored
1193  case 0x7D:
1194  l2 = 2048;
1195  break; // code and data L2 cache, 2048 KB, 8 ways, 64 byte lines
1196  case 0x7E:
1197  l2 = 256;
1198  break; // code and data L2 cache, 256 KB, 8 ways, 128 byte lines, sect. (IA-64)
1199  case 0x7F:
1200  l2 = 512;
1201  break; // code and data L2 cache, 512 KB, 2 ways, 64 byte lines
1202  case 0x80:
1203  l2 = 512;
1204  break; // code and data L2 cache, 512 KB, 8 ways, 64 byte lines
1205  case 0x81:
1206  l2 = 128;
1207  break; // code and data L2 cache, 128 KB, 8 ways, 32 byte lines
1208  case 0x82:
1209  l2 = 256;
1210  break; // code and data L2 cache, 256 KB, 8 ways, 32 byte lines
1211  case 0x83:
1212  l2 = 512;
1213  break; // code and data L2 cache, 512 KB, 8 ways, 32 byte lines
1214  case 0x84:
1215  l2 = 1024;
1216  break; // code and data L2 cache, 1024 KB, 8 ways, 32 byte lines
1217  case 0x85:
1218  l2 = 2048;
1219  break; // code and data L2 cache, 2048 KB, 8 ways, 32 byte lines
1220  case 0x86:
1221  l2 = 512;
1222  break; // code and data L2 cache, 512 KB, 4 ways, 64 byte lines
1223  case 0x87:
1224  l2 = 1024;
1225  break; // code and data L2 cache, 1024 KB, 8 ways, 64 byte lines
1226  case 0x88:
1227  l3 = 2048;
1228  break; // code and data L3 cache, 2048 KB, 4 ways, 64 byte lines (IA-64)
1229  case 0x89:
1230  l3 = 4096;
1231  break; // code and data L3 cache, 4096 KB, 4 ways, 64 byte lines (IA-64)
1232  case 0x8A:
1233  l3 = 8192;
1234  break; // code and data L3 cache, 8192 KB, 4 ways, 64 byte lines (IA-64)
1235  case 0x8D:
1236  l3 = 3072;
1237  break; // code and data L3 cache, 3072 KB, 12 ways, 128 byte lines (IA-64)
1238 
1239  default:
1240  break;
1241  }
1242  }
1243  if (check_for_p2_core2 && l2 == l3) l3 = 0;
1244  l1 *= 1024;
1245  l2 *= 1024;
1246  l3 *= 1024;
1247 }
1248 
1249 inline void queryCacheSizes_intel(int& l1, int& l2, int& l3, int max_std_funcs) {
1250  if (max_std_funcs >= 4)
1251  queryCacheSizes_intel_direct(l1, l2, l3);
1252  else if (max_std_funcs >= 2)
1253  queryCacheSizes_intel_codes(l1, l2, l3);
1254  else
1255  l1 = l2 = l3 = 0;
1256 }
1257 
1258 inline void queryCacheSizes_amd(int& l1, int& l2, int& l3) {
1259  int abcd[4];
1260  abcd[0] = abcd[1] = abcd[2] = abcd[3] = 0;
1261 
1262  // First query the max supported function.
1263  EIGEN_CPUID(abcd, 0x80000000, 0);
1264  if (static_cast<numext::uint32_t>(abcd[0]) >= static_cast<numext::uint32_t>(0x80000006)) {
1265  EIGEN_CPUID(abcd, 0x80000005, 0);
1266  l1 = (abcd[2] >> 24) * 1024; // C[31:24] = L1 size in KB
1267  abcd[0] = abcd[1] = abcd[2] = abcd[3] = 0;
1268  EIGEN_CPUID(abcd, 0x80000006, 0);
1269  l2 = (abcd[2] >> 16) * 1024; // C[31;16] = l2 cache size in KB
1270  l3 = ((abcd[3] & 0xFFFC000) >> 18) * 512 * 1024; // D[31;18] = l3 cache size in 512KB
1271  } else {
1272  l1 = l2 = l3 = 0;
1273  }
1274 }
1275 #endif
1276 
1279 inline void queryCacheSizes(int& l1, int& l2, int& l3) {
1280 #ifdef EIGEN_CPUID
1281  int abcd[4];
1282  const int GenuineIntel[] = {0x756e6547, 0x49656e69, 0x6c65746e};
1283  const int AuthenticAMD[] = {0x68747541, 0x69746e65, 0x444d4163};
1284  const int AMDisbetter_[] = {0x69444d41, 0x74656273, 0x21726574}; // "AMDisbetter!"
1285 
1286  // identify the CPU vendor
1287  EIGEN_CPUID(abcd, 0x0, 0);
1288  int max_std_funcs = abcd[0];
1289  if (cpuid_is_vendor(abcd, GenuineIntel))
1290  queryCacheSizes_intel(l1, l2, l3, max_std_funcs);
1291  else if (cpuid_is_vendor(abcd, AuthenticAMD) || cpuid_is_vendor(abcd, AMDisbetter_))
1292  queryCacheSizes_amd(l1, l2, l3);
1293  else
1294  // by default let's use Intel's API
1295  queryCacheSizes_intel(l1, l2, l3, max_std_funcs);
1296 
1297  // here is the list of other vendors:
1298  // ||cpuid_is_vendor(abcd,"VIA VIA VIA ")
1299  // ||cpuid_is_vendor(abcd,"CyrixInstead")
1300  // ||cpuid_is_vendor(abcd,"CentaurHauls")
1301  // ||cpuid_is_vendor(abcd,"GenuineTMx86")
1302  // ||cpuid_is_vendor(abcd,"TransmetaCPU")
1303  // ||cpuid_is_vendor(abcd,"RiseRiseRise")
1304  // ||cpuid_is_vendor(abcd,"Geode by NSC")
1305  // ||cpuid_is_vendor(abcd,"SiS SiS SiS ")
1306  // ||cpuid_is_vendor(abcd,"UMC UMC UMC ")
1307  // ||cpuid_is_vendor(abcd,"NexGenDriven")
1308 #else
1309  l1 = l2 = l3 = -1;
1310 #endif
1311 }
1312 
1315 inline int queryL1CacheSize() {
1316  int l1(-1), l2, l3;
1317  queryCacheSizes(l1, l2, l3);
1318  return l1;
1319 }
1320 
1323 inline int queryTopLevelCacheSize() {
1324  int l1, l2(-1), l3(-1);
1325  queryCacheSizes(l1, l2, l3);
1326  return (std::max)(l2, l3);
1327 }
1328 
1333 #if EIGEN_COMP_CXXVER >= 20 && defined(__cpp_lib_constexpr_dynamic_alloc) && \
1334  __cpp_lib_constexpr_dynamic_alloc >= 201907L
1335 using std::construct_at;
1336 #else
1337 template <class T, class... Args>
1338 EIGEN_DEVICE_FUNC T* construct_at(T* p, Args&&... args) {
1339  return ::new (const_cast<void*>(static_cast<const volatile void*>(p))) T(std::forward<Args>(args)...);
1340 }
1341 #endif
1342 
1348 #if EIGEN_COMP_CXXVER >= 17
1349 using std::destroy_at;
1350 #else
1351 template <class T>
1352 EIGEN_DEVICE_FUNC void destroy_at(T* p) {
1353  p->~T();
1354 }
1355 #endif
1356 
1357 // FIXME(rmlarsen): Work around missing linker symbol with msan on ARM.
1358 #if !defined(EIGEN_DONT_ASSUME_ALIGNED) && __has_feature(memory_sanitizer) && \
1359  (EIGEN_ARCH_ARM || EIGEN_ARCH_ARM64)
1360 #define EIGEN_DONT_ASSUME_ALIGNED
1361 #endif
1362 
1363 
1364 #if !defined(EIGEN_DONT_ASSUME_ALIGNED) && defined(__cpp_lib_assume_aligned) && (__cpp_lib_assume_aligned >= 201811L)
1365 template <std::size_t N, typename T>
1366 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC constexpr T* assume_aligned(T* ptr) {
1367  return std::assume_aligned<N, T>(ptr);
1368 }
1369 #elif !defined(EIGEN_DONT_ASSUME_ALIGNED) && EIGEN_HAS_BUILTIN(__builtin_assume_aligned)
1370 template <std::size_t N, typename T>
1371 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC T* assume_aligned(T* ptr) {
1372  return static_cast<T*>(__builtin_assume_aligned(ptr, N));
1373 }
1374 #else
1375 template <std::size_t N, typename T>
1376 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC constexpr T* assume_aligned(T* ptr) {
1377  return ptr;
1378 }
1379 #endif
1380 
1381 } // end namespace internal
1382 
1383 } // end namespace Eigen
1384 
1385 #endif // EIGEN_MEMORY_H
static constexpr lastp1_t end
Definition: IndexedViewHelper.h:79
Namespace containing all symbols from the Eigen library.
Definition: B01_Experimental.dox:1
STL compatible allocator to use with types requiring a non-standard alignment.
Definition: Memory.h:932
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:82
std::enable_if_t< std::is_base_of< DenseBase< std::decay_t< DerivedA > >, std::decay_t< DerivedA > >::value &&std::is_base_of< DenseBase< std::decay_t< DerivedB > >, std::decay_t< DerivedB > >::value, void > swap(DerivedA &&a, DerivedB &&b)
Definition: DenseBase.h:667
const int Dynamic
Definition: Constants.h:25