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