1 // This file is part of Eigen, a lightweight C++ template library
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>
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/.
16 /*****************************************************************************
17 *** Platform checks for aligned malloc functions ***
18 *****************************************************************************/
20 #ifndef EIGEN_MEMORY_H
21 #define EIGEN_MEMORY_H
23 #ifndef EIGEN_MALLOC_ALREADY_ALIGNED
25 // Try to determine automatically if malloc is already aligned.
27 // On 64-bit systems, glibc's malloc returns 16-byte-aligned pointers, see:
28 // http://www.gnu.org/s/libc/manual/html_node/Aligned-Memory-Blocks.html
29 // This is true at least since glibc 2.8.
30 // This leaves the question how to detect 64-bit. According to this document,
31 // http://gcc.fyxm.net/summit/2003/Porting%20to%2064%20bit.pdf
32 // page 114, "[The] LP64 model [...] is used by all 64-bit UNIX ports" so it's indeed
33 // quite safe, at least within the context of glibc, to equate 64-bit with LP64.
34 #if defined(__GLIBC__) && ((__GLIBC__>=2 && __GLIBC_MINOR__ >= 8) || __GLIBC__>2) \
35 && defined(__LP64__) && ! defined( __SANITIZE_ADDRESS__ ) && (EIGEN_DEFAULT_ALIGN_BYTES == 16)
36 #define EIGEN_GLIBC_MALLOC_ALREADY_ALIGNED 1
38 #define EIGEN_GLIBC_MALLOC_ALREADY_ALIGNED 0
41 // FreeBSD 6 seems to have 16-byte aligned malloc
42 // See http://svn.freebsd.org/viewvc/base/stable/6/lib/libc/stdlib/malloc.c?view=markup
43 // FreeBSD 7 seems to have 16-byte aligned malloc except on ARM and MIPS architectures
44 // See http://svn.freebsd.org/viewvc/base/stable/7/lib/libc/stdlib/malloc.c?view=markup
45 #if defined(__FreeBSD__) && !(EIGEN_ARCH_ARM || EIGEN_ARCH_MIPS) && (EIGEN_DEFAULT_ALIGN_BYTES == 16)
46 #define EIGEN_FREEBSD_MALLOC_ALREADY_ALIGNED 1
48 #define EIGEN_FREEBSD_MALLOC_ALREADY_ALIGNED 0
51 #if (EIGEN_OS_MAC && (EIGEN_DEFAULT_ALIGN_BYTES == 16)) \
52 || (EIGEN_OS_WIN64 && (EIGEN_DEFAULT_ALIGN_BYTES == 16)) \
53 || EIGEN_GLIBC_MALLOC_ALREADY_ALIGNED \
54 || EIGEN_FREEBSD_MALLOC_ALREADY_ALIGNED
55 #define EIGEN_MALLOC_ALREADY_ALIGNED 1
57 #define EIGEN_MALLOC_ALREADY_ALIGNED 0
62 #ifndef EIGEN_MALLOC_CHECK_THREAD_LOCAL
64 // Check whether we can use the thread_local keyword to allow or disallow
65 // allocating memory with per-thread granularity, by means of the
66 // set_is_malloc_allowed() function.
67 #ifndef EIGEN_AVOID_THREAD_LOCAL
69 #if ((EIGEN_COMP_GNUC) || __has_feature(cxx_thread_local) || EIGEN_COMP_MSVC >= 1900) && !defined(EIGEN_GPU_COMPILE_PHASE)
70 #define EIGEN_MALLOC_CHECK_THREAD_LOCAL thread_local
72 #define EIGEN_MALLOC_CHECK_THREAD_LOCAL
75 #else // EIGEN_AVOID_THREAD_LOCAL
76 #define EIGEN_MALLOC_CHECK_THREAD_LOCAL
77 #endif // EIGEN_AVOID_THREAD_LOCAL
81 // IWYU pragma: private
82 #include "../InternalHeaderCheck.h"
88 /*****************************************************************************
89 *** Implementation of portable aligned versions of malloc/free/realloc ***
90 *****************************************************************************/
92 #ifdef EIGEN_NO_MALLOC
93 EIGEN_DEVICE_FUNC inline void check_that_malloc_is_allowed()
95 eigen_assert(false && "heap allocation is forbidden (EIGEN_NO_MALLOC is defined)");
97 #elif defined EIGEN_RUNTIME_NO_MALLOC
98 EIGEN_DEVICE_FUNC inline bool is_malloc_allowed_impl(bool update, bool new_value = false)
100 EIGEN_MALLOC_CHECK_THREAD_LOCAL static bool value = true;
105 EIGEN_DEVICE_FUNC inline bool is_malloc_allowed() { return is_malloc_allowed_impl(false); }
106 EIGEN_DEVICE_FUNC inline bool set_is_malloc_allowed(bool new_value) { return is_malloc_allowed_impl(true, new_value); }
107 EIGEN_DEVICE_FUNC inline void check_that_malloc_is_allowed()
109 eigen_assert(is_malloc_allowed() && "heap allocation is forbidden (EIGEN_RUNTIME_NO_MALLOC is defined and g_is_malloc_allowed is false)");
112 EIGEN_DEVICE_FUNC inline void check_that_malloc_is_allowed()
118 inline void throw_std_bad_alloc()
120 #ifdef EIGEN_EXCEPTIONS
121 throw std::bad_alloc();
123 std::size_t huge = static_cast<std::size_t>(-1);
124 #if defined(EIGEN_HIPCC)
126 // calls to "::operator new" are to be treated as opaque function calls (i.e no inlining),
127 // and as a consequence the code in the #else block triggers the hipcc warning :
128 // "no overloaded function has restriction specifiers that are compatible with the ambient context"
130 // "throw_std_bad_alloc" has the EIGEN_DEVICE_FUNC attribute, so it seems that hipcc expects
131 // the same on "operator new"
132 // Reverting code back to the old version in this #if block for the hipcc compiler
136 void* unused = ::operator new(huge);
137 EIGEN_UNUSED_VARIABLE(unused);
142 /*****************************************************************************
143 *** Implementation of handmade aligned functions ***
144 *****************************************************************************/
146 /* ----- Hand made implementations of aligned malloc/free and realloc ----- */
148 /** \internal Like malloc, but the returned pointer is guaranteed to be aligned to `alignment`.
149 * Fast, but wastes `alignment` additional bytes of memory. Does not throw any exception.
151 EIGEN_DEVICE_FUNC inline void* handmade_aligned_malloc(std::size_t size, std::size_t alignment = EIGEN_DEFAULT_ALIGN_BYTES)
153 eigen_assert(alignment >= sizeof(void*) && alignment <= 128 && (alignment & (alignment-1)) == 0 && "Alignment must be at least sizeof(void*), less than or equal to 128, and a power of 2");
155 check_that_malloc_is_allowed();
156 EIGEN_USING_STD(malloc)
157 void* original = malloc(size + alignment);
158 if (original == 0) return 0;
159 uint8_t offset = static_cast<uint8_t>(alignment - (reinterpret_cast<std::size_t>(original) & (alignment - 1)));
160 void* aligned = static_cast<void*>(static_cast<uint8_t*>(original) + offset);
161 *(static_cast<uint8_t*>(aligned) - 1) = offset;
165 /** \internal Frees memory allocated with handmade_aligned_malloc */
166 EIGEN_DEVICE_FUNC inline void handmade_aligned_free(void *ptr)
169 uint8_t offset = static_cast<uint8_t>(*(static_cast<uint8_t*>(ptr) - 1));
170 void* original = static_cast<void*>(static_cast<uint8_t*>(ptr) - offset);
172 check_that_malloc_is_allowed();
173 EIGEN_USING_STD(free)
179 * \brief Reallocates aligned memory.
180 * Since we know that our handmade version is based on std::malloc
181 * we can use std::realloc to implement efficient reallocation.
183 EIGEN_DEVICE_FUNC inline void* handmade_aligned_realloc(void* ptr, std::size_t new_size, std::size_t old_size, std::size_t alignment = EIGEN_DEFAULT_ALIGN_BYTES)
185 if (ptr == nullptr) return handmade_aligned_malloc(new_size, alignment);
186 uint8_t old_offset = *(static_cast<uint8_t*>(ptr) - 1);
187 void* old_original = static_cast<uint8_t*>(ptr) - old_offset;
189 check_that_malloc_is_allowed();
190 EIGEN_USING_STD(realloc)
191 void* original = realloc(old_original, new_size + alignment);
192 if (original == nullptr) return nullptr;
193 if (original == old_original) return ptr;
194 uint8_t offset = static_cast<uint8_t>(alignment - (reinterpret_cast<std::size_t>(original) & (alignment - 1)));
195 void* aligned = static_cast<void*>(static_cast<uint8_t*>(original) + offset);
196 if (offset != old_offset) {
197 const void* src = static_cast<const void*>(static_cast<uint8_t*>(original) + old_offset);
198 std::size_t count = (std::min)(new_size, old_size);
199 std::memmove(aligned, src, count);
201 *(static_cast<uint8_t*>(aligned) - 1) = offset;
205 /** \internal Allocates \a size bytes. The returned pointer is guaranteed to have 16 or 32 bytes alignment depending on the requirements.
206 * On allocation error, the returned pointer is null, and std::bad_alloc is thrown.
208 EIGEN_DEVICE_FUNC inline void* aligned_malloc(std::size_t size)
210 if (size == 0) return nullptr;
213 #if (EIGEN_DEFAULT_ALIGN_BYTES==0) || EIGEN_MALLOC_ALREADY_ALIGNED
215 check_that_malloc_is_allowed();
216 EIGEN_USING_STD(malloc)
217 result = malloc(size);
219 #if EIGEN_DEFAULT_ALIGN_BYTES==16
220 eigen_assert((size<16 || (std::size_t(result)%16)==0) && "System's malloc returned an unaligned pointer. Compile with EIGEN_MALLOC_ALREADY_ALIGNED=0 to fallback to handmade aligned memory allocator.");
223 result = handmade_aligned_malloc(size);
227 throw_std_bad_alloc();
232 /** \internal Frees memory allocated with aligned_malloc. */
233 EIGEN_DEVICE_FUNC inline void aligned_free(void *ptr)
235 #if (EIGEN_DEFAULT_ALIGN_BYTES==0) || EIGEN_MALLOC_ALREADY_ALIGNED
238 check_that_malloc_is_allowed();
239 EIGEN_USING_STD(free)
243 handmade_aligned_free(ptr);
249 * \brief Reallocates an aligned block of memory.
250 * \throws std::bad_alloc on allocation failure
252 EIGEN_DEVICE_FUNC inline void* aligned_realloc(void *ptr, std::size_t new_size, std::size_t old_size)
254 if (ptr == nullptr) return aligned_malloc(new_size);
255 if (old_size == new_size) return ptr;
256 if (new_size == 0) { aligned_free(ptr); return nullptr; }
259 #if (EIGEN_DEFAULT_ALIGN_BYTES==0) || EIGEN_MALLOC_ALREADY_ALIGNED
260 EIGEN_UNUSED_VARIABLE(old_size)
262 check_that_malloc_is_allowed();
263 EIGEN_USING_STD(realloc)
264 result = realloc(ptr,new_size);
266 result = handmade_aligned_realloc(ptr,new_size,old_size);
269 if (!result && new_size)
270 throw_std_bad_alloc();
275 /*****************************************************************************
276 *** Implementation of conditionally aligned functions ***
277 *****************************************************************************/
279 /** \internal Allocates \a size bytes. If Align is true, then the returned ptr is 16-byte-aligned.
280 * On allocation error, the returned pointer is null, and a std::bad_alloc is thrown.
282 template<bool Align> EIGEN_DEVICE_FUNC inline void* conditional_aligned_malloc(std::size_t size)
284 return aligned_malloc(size);
287 template<> EIGEN_DEVICE_FUNC inline void* conditional_aligned_malloc<false>(std::size_t size)
289 if (size == 0) return nullptr;
291 check_that_malloc_is_allowed();
292 EIGEN_USING_STD(malloc)
293 void *result = malloc(size);
296 throw_std_bad_alloc();
300 /** \internal Frees memory allocated with conditional_aligned_malloc */
301 template<bool Align> EIGEN_DEVICE_FUNC inline void conditional_aligned_free(void *ptr)
306 template<> EIGEN_DEVICE_FUNC inline void conditional_aligned_free<false>(void *ptr)
309 check_that_malloc_is_allowed();
310 EIGEN_USING_STD(free)
314 template<bool Align> EIGEN_DEVICE_FUNC inline void* conditional_aligned_realloc(void* ptr, std::size_t new_size, std::size_t old_size)
316 return aligned_realloc(ptr, new_size, old_size);
319 template<> EIGEN_DEVICE_FUNC inline void* conditional_aligned_realloc<false>(void* ptr, std::size_t new_size, std::size_t old_size)
321 if (ptr == nullptr) return conditional_aligned_malloc<false>(new_size);
322 if (old_size == new_size) return ptr;
323 if (new_size == 0) { conditional_aligned_free<false>(ptr); return nullptr; }
325 check_that_malloc_is_allowed();
326 EIGEN_USING_STD(realloc)
327 return realloc(ptr, new_size);
330 /*****************************************************************************
331 *** Construction/destruction of array elements ***
332 *****************************************************************************/
334 /** \internal Destructs the elements of an array.
335 * The \a size parameters tells on how many objects to call the destructor of T.
337 template<typename T> EIGEN_DEVICE_FUNC inline void destruct_elements_of_array(T *ptr, std::size_t size)
339 // always destruct an array starting from the end.
341 while(size) ptr[--size].~T();
344 /** \internal Constructs the elements of an array.
345 * The \a size parameter tells on how many objects to call the constructor of T.
347 template<typename T> EIGEN_DEVICE_FUNC inline T* default_construct_elements_of_array(T *ptr, std::size_t size)
352 for (i = 0; i < size; ++i) ::new (ptr + i) T;
356 destruct_elements_of_array(ptr, i);
362 /** \internal Copy-constructs the elements of an array.
363 * The \a size parameter tells on how many objects to copy.
365 template<typename T> EIGEN_DEVICE_FUNC inline T* copy_construct_elements_of_array(T *ptr, const T* src, std::size_t size)
370 for (i = 0; i < size; ++i) ::new (ptr + i) T(*(src + i));
374 destruct_elements_of_array(ptr, i);
380 /** \internal Move-constructs the elements of an array.
381 * The \a size parameter tells on how many objects to move.
383 template<typename T> EIGEN_DEVICE_FUNC inline T* move_construct_elements_of_array(T *ptr, T* src, std::size_t size)
388 for (i = 0; i < size; ++i) ::new (ptr + i) T(std::move(*(src + i)));
392 destruct_elements_of_array(ptr, i);
398 /*****************************************************************************
399 *** Implementation of aligned new/delete-like functions ***
400 *****************************************************************************/
403 EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE void check_size_for_overflow(std::size_t size)
405 if(size > std::size_t(-1) / sizeof(T))
406 throw_std_bad_alloc();
409 /** \internal Allocates \a size objects of type T. The returned pointer is guaranteed to have 16 bytes alignment.
410 * On allocation error, the returned pointer is undefined, but a std::bad_alloc is thrown.
411 * The default constructor of T is called.
413 template<typename T> EIGEN_DEVICE_FUNC inline T* aligned_new(std::size_t size)
415 check_size_for_overflow<T>(size);
416 T *result = static_cast<T*>(aligned_malloc(sizeof(T)*size));
419 return default_construct_elements_of_array(result, size);
423 aligned_free(result);
429 template<typename T, bool Align> EIGEN_DEVICE_FUNC inline T* conditional_aligned_new(std::size_t size)
431 check_size_for_overflow<T>(size);
432 T *result = static_cast<T*>(conditional_aligned_malloc<Align>(sizeof(T)*size));
435 return default_construct_elements_of_array(result, size);
439 conditional_aligned_free<Align>(result);
445 /** \internal Deletes objects constructed with aligned_new
446 * The \a size parameters tells on how many objects to call the destructor of T.
448 template<typename T> EIGEN_DEVICE_FUNC inline void aligned_delete(T *ptr, std::size_t size)
450 destruct_elements_of_array<T>(ptr, size);
454 /** \internal Deletes objects constructed with conditional_aligned_new
455 * The \a size parameters tells on how many objects to call the destructor of T.
457 template<typename T, bool Align> EIGEN_DEVICE_FUNC inline void conditional_aligned_delete(T *ptr, std::size_t size)
459 destruct_elements_of_array<T>(ptr, size);
460 conditional_aligned_free<Align>(ptr);
463 template<typename T, bool Align> EIGEN_DEVICE_FUNC inline T* conditional_aligned_realloc_new(T* pts, std::size_t new_size, std::size_t old_size)
465 check_size_for_overflow<T>(new_size);
466 check_size_for_overflow<T>(old_size);
468 // If elements need to be explicitly initialized, we cannot simply realloc
469 // (or memcpy) the memory block - each element needs to be reconstructed.
470 // Otherwise, objects that contain internal pointers like mpfr or
471 // AnnoyingScalar can be pointing to the wrong thing.
472 T* result = static_cast<T*>(conditional_aligned_malloc<Align>(sizeof(T)*new_size));
475 // Move-construct initial elements.
476 std::size_t copy_size = (std::min)(old_size, new_size);
477 move_construct_elements_of_array(result, pts, copy_size);
479 // Default-construct remaining elements.
480 if (new_size > old_size) {
481 default_construct_elements_of_array(result + copy_size, new_size - old_size);
484 // Delete old elements.
485 conditional_aligned_delete<T, Align>(pts, old_size);
489 conditional_aligned_free<Align>(result);
497 template<typename T, bool Align> EIGEN_DEVICE_FUNC inline T* conditional_aligned_new_auto(std::size_t size)
500 return 0; // short-cut. Also fixes Bug 884
501 check_size_for_overflow<T>(size);
502 T *result = static_cast<T*>(conditional_aligned_malloc<Align>(sizeof(T)*size));
503 if(NumTraits<T>::RequireInitialization)
507 default_construct_elements_of_array(result, size);
511 conditional_aligned_free<Align>(result);
518 template<typename T, bool Align> EIGEN_DEVICE_FUNC inline T* conditional_aligned_realloc_new_auto(T* pts, std::size_t new_size, std::size_t old_size)
520 if (NumTraits<T>::RequireInitialization) {
521 return conditional_aligned_realloc_new<T, Align>(pts, new_size, old_size);
524 check_size_for_overflow<T>(new_size);
525 check_size_for_overflow<T>(old_size);
526 return static_cast<T*>(conditional_aligned_realloc<Align>(static_cast<void*>(pts), sizeof(T)*new_size, sizeof(T)*old_size));
529 template<typename T, bool Align> EIGEN_DEVICE_FUNC inline void conditional_aligned_delete_auto(T *ptr, std::size_t size)
531 if(NumTraits<T>::RequireInitialization)
532 destruct_elements_of_array<T>(ptr, size);
533 conditional_aligned_free<Align>(ptr);
536 /****************************************************************************/
538 /** \internal Returns the index of the first element of the array that is well aligned with respect to the requested \a Alignment.
540 * \tparam Alignment requested alignment in Bytes.
541 * \param array the address of the start of the array
542 * \param size the size of the array
544 * \note If no element of the array is well aligned or the requested alignment is not a multiple of a scalar,
545 * the size of the array is returned. For example with SSE, the requested alignment is typically 16-bytes. If
546 * packet size for the given scalar type is 1, then everything is considered well-aligned.
548 * \note Otherwise, if the Alignment is larger that the scalar size, we rely on the assumptions that sizeof(Scalar) is a
549 * power of 2. On the other hand, we do not assume that the array address is a multiple of sizeof(Scalar), as that fails for
550 * example with Scalar=double on certain 32-bit platforms, see bug #79.
552 * There is also the variant first_aligned(const MatrixBase&) defined in DenseCoeffsBase.h.
553 * \sa first_default_aligned()
555 template<int Alignment, typename Scalar, typename Index>
556 EIGEN_DEVICE_FUNC inline Index first_aligned(const Scalar* array, Index size)
558 const Index ScalarSize = sizeof(Scalar);
559 const Index AlignmentSize = Alignment / ScalarSize;
560 const Index AlignmentMask = AlignmentSize-1;
564 // Either the requested alignment if smaller than a scalar, or it exactly match a 1 scalar
565 // so that all elements of the array have the same alignment.
568 else if( (std::uintptr_t(array) & (sizeof(Scalar)-1)) || (Alignment%ScalarSize)!=0)
570 // The array is not aligned to the size of a single scalar, or the requested alignment is not a multiple of the scalar size.
571 // Consequently, no element of the array is well aligned.
576 Index first = (AlignmentSize - (Index((std::uintptr_t(array)/sizeof(Scalar))) & AlignmentMask)) & AlignmentMask;
577 return (first < size) ? first : size;
581 /** \internal Returns the index of the first element of the array that is well aligned with respect the largest packet requirement.
582 * \sa first_aligned(Scalar*,Index) and first_default_aligned(DenseBase<Derived>) */
583 template<typename Scalar, typename Index>
584 EIGEN_DEVICE_FUNC inline Index first_default_aligned(const Scalar* array, Index size)
586 typedef typename packet_traits<Scalar>::type DefaultPacketType;
587 return first_aligned<unpacket_traits<DefaultPacketType>::alignment>(array, size);
590 /** \internal Returns the smallest integer multiple of \a base and greater or equal to \a size
592 template<typename Index>
593 inline Index first_multiple(Index size, Index base)
595 return ((size+base-1)/base)*base;
598 // std::copy is much slower than memcpy, so let's introduce a smart_copy which
599 // use memcpy on trivial types, i.e., on types that does not require an initialization ctor.
600 template<typename T, bool UseMemcpy> struct smart_copy_helper;
602 template<typename T> EIGEN_DEVICE_FUNC void smart_copy(const T* start, const T* end, T* target)
604 smart_copy_helper<T,!NumTraits<T>::RequireInitialization>::run(start, end, target);
607 template<typename T> struct smart_copy_helper<T,true> {
608 EIGEN_DEVICE_FUNC static inline void run(const T* start, const T* end, T* target)
610 std::intptr_t size = std::intptr_t(end)-std::intptr_t(start);
612 eigen_internal_assert(start!=0 && end!=0 && target!=0);
613 EIGEN_USING_STD(memcpy)
614 memcpy(target, start, size);
618 template<typename T> struct smart_copy_helper<T,false> {
619 EIGEN_DEVICE_FUNC static inline void run(const T* start, const T* end, T* target)
620 { std::copy(start, end, target); }
623 // intelligent memmove. falls back to std::memmove for POD types, uses std::copy otherwise.
624 template<typename T, bool UseMemmove> struct smart_memmove_helper;
626 template<typename T> void smart_memmove(const T* start, const T* end, T* target)
628 smart_memmove_helper<T,!NumTraits<T>::RequireInitialization>::run(start, end, target);
631 template<typename T> struct smart_memmove_helper<T,true> {
632 static inline void run(const T* start, const T* end, T* target)
634 std::intptr_t size = std::intptr_t(end)-std::intptr_t(start);
636 eigen_internal_assert(start!=0 && end!=0 && target!=0);
637 std::memmove(target, start, size);
641 template<typename T> struct smart_memmove_helper<T,false> {
642 static inline void run(const T* start, const T* end, T* target)
644 if (std::uintptr_t(target) < std::uintptr_t(start))
646 std::copy(start, end, target);
650 std::ptrdiff_t count = (std::ptrdiff_t(end)-std::ptrdiff_t(start)) / sizeof(T);
651 std::copy_backward(start, end, target + count);
656 template<typename T> EIGEN_DEVICE_FUNC T* smart_move(T* start, T* end, T* target)
658 return std::move(start, end, target);
661 /*****************************************************************************
662 *** Implementation of runtime stack allocation (falling back to malloc) ***
663 *****************************************************************************/
665 // you can overwrite Eigen's default behavior regarding alloca by defining EIGEN_ALLOCA
666 // to the appropriate stack allocation function
667 #if ! defined EIGEN_ALLOCA && ! defined EIGEN_GPU_COMPILE_PHASE
668 #if EIGEN_OS_LINUX || EIGEN_OS_MAC || (defined alloca)
669 #define EIGEN_ALLOCA alloca
670 #elif EIGEN_COMP_MSVC
671 #define EIGEN_ALLOCA _alloca
675 // With clang -Oz -mthumb, alloca changes the stack pointer in a way that is
676 // not allowed in Thumb2. -DEIGEN_STACK_ALLOCATION_LIMIT=0 doesn't work because
677 // the compiler still emits bad code because stack allocation checks use "<=".
678 // TODO: Eliminate after https://bugs.llvm.org/show_bug.cgi?id=23772
680 #if defined(__clang__) && defined(__thumb__)
684 // This helper class construct the allocated memory, and takes care of destructing and freeing the handled data
685 // at destruction time. In practice this helper class is mainly useful to avoid memory leak in case of exceptions.
686 template<typename T> class aligned_stack_memory_handler : noncopyable
689 /* Creates a stack_memory_handler responsible for the buffer \a ptr of size \a size.
690 * Note that \a ptr can be 0 regardless of the other parameters.
691 * This constructor takes care of constructing/initializing the elements of the buffer if required by the scalar type T (see NumTraits<T>::RequireInitialization).
692 * In this case, the buffer elements will also be destructed when this handler will be destructed.
693 * Finally, if \a dealloc is true, then the pointer \a ptr is freed.
696 aligned_stack_memory_handler(T* ptr, std::size_t size, bool dealloc)
697 : m_ptr(ptr), m_size(size), m_deallocate(dealloc)
699 if(NumTraits<T>::RequireInitialization && m_ptr)
700 Eigen::internal::default_construct_elements_of_array(m_ptr, size);
703 ~aligned_stack_memory_handler()
705 if(NumTraits<T>::RequireInitialization && m_ptr)
706 Eigen::internal::destruct_elements_of_array<T>(m_ptr, m_size);
708 Eigen::internal::aligned_free(m_ptr);
718 template<typename Xpr, int NbEvaluations,
719 bool MapExternalBuffer = nested_eval<Xpr,NbEvaluations>::Evaluate && Xpr::MaxSizeAtCompileTime==Dynamic
721 struct local_nested_eval_wrapper
723 static constexpr bool NeedExternalBuffer = false;
724 typedef typename Xpr::Scalar Scalar;
725 typedef typename nested_eval<Xpr,NbEvaluations>::type ObjectType;
729 local_nested_eval_wrapper(const Xpr& xpr, Scalar* ptr) : object(xpr)
731 EIGEN_UNUSED_VARIABLE(ptr);
732 eigen_internal_assert(ptr==0);
736 template<typename Xpr, int NbEvaluations>
737 struct local_nested_eval_wrapper<Xpr,NbEvaluations,true>
739 static constexpr bool NeedExternalBuffer = true;
740 typedef typename Xpr::Scalar Scalar;
741 typedef typename plain_object_eval<Xpr>::type PlainObject;
742 typedef Map<PlainObject,EIGEN_DEFAULT_ALIGN_BYTES> ObjectType;
746 local_nested_eval_wrapper(const Xpr& xpr, Scalar* ptr)
747 : object(ptr==0 ? reinterpret_cast<Scalar*>(Eigen::internal::aligned_malloc(sizeof(Scalar)*xpr.size())) : ptr, xpr.rows(), xpr.cols()),
750 if(NumTraits<Scalar>::RequireInitialization && object.data())
751 Eigen::internal::default_construct_elements_of_array(object.data(), object.size());
756 ~local_nested_eval_wrapper()
758 if(NumTraits<Scalar>::RequireInitialization && object.data())
759 Eigen::internal::destruct_elements_of_array(object.data(), object.size());
761 Eigen::internal::aligned_free(object.data());
768 #endif // EIGEN_ALLOCA
770 template<typename T> class scoped_array : noncopyable
774 explicit scoped_array(std::ptrdiff_t size)
782 T& operator[](std::ptrdiff_t i) { return m_ptr[i]; }
783 const T& operator[](std::ptrdiff_t i) const { return m_ptr[i]; }
784 T* &ptr() { return m_ptr; }
785 const T* ptr() const { return m_ptr; }
786 operator const T*() const { return m_ptr; }
789 template<typename T> void swap(scoped_array<T> &a,scoped_array<T> &b)
791 std::swap(a.ptr(),b.ptr());
794 } // end namespace internal
798 * The macro ei_declare_aligned_stack_constructed_variable(TYPE,NAME,SIZE,BUFFER) declares, allocates,
799 * and construct an aligned buffer named NAME of SIZE elements of type TYPE on the stack
800 * if the size in bytes is smaller than EIGEN_STACK_ALLOCATION_LIMIT, and if stack allocation is supported by the platform
801 * (currently, this is Linux, OSX and Visual Studio only). Otherwise the memory is allocated on the heap.
802 * The allocated buffer is automatically deleted when exiting the scope of this declaration.
803 * If BUFFER is non null, then the declared variable is simply an alias for BUFFER, and no allocation/deletion occurs.
804 * Here is an example:
807 * ei_declare_aligned_stack_constructed_variable(float,data,size,0);
808 * // use data[0] to data[size-1]
811 * The underlying stack allocation function can controlled with the EIGEN_ALLOCA preprocessor token.
813 * The macro ei_declare_local_nested_eval(XPR_T,XPR,N,NAME) is analogue to
815 * typename internal::nested_eval<XPRT_T,N>::type NAME(XPR);
817 * with the advantage of using aligned stack allocation even if the maximal size of XPR at compile time is unknown.
818 * This is accomplished through alloca if this later is supported and if the required number of bytes
819 * is below EIGEN_STACK_ALLOCATION_LIMIT.
823 #if EIGEN_DEFAULT_ALIGN_BYTES>0
824 // We always manually re-align the result of EIGEN_ALLOCA.
825 // If alloca is already aligned, the compiler should be smart enough to optimize away the re-alignment.
826 #define EIGEN_ALIGNED_ALLOCA(SIZE) reinterpret_cast<void*>((std::uintptr_t(EIGEN_ALLOCA(SIZE+EIGEN_DEFAULT_ALIGN_BYTES-1)) + EIGEN_DEFAULT_ALIGN_BYTES-1) & ~(std::size_t(EIGEN_DEFAULT_ALIGN_BYTES-1)))
828 #define EIGEN_ALIGNED_ALLOCA(SIZE) EIGEN_ALLOCA(SIZE)
831 #define ei_declare_aligned_stack_constructed_variable(TYPE,NAME,SIZE,BUFFER) \
832 Eigen::internal::check_size_for_overflow<TYPE>(SIZE); \
833 TYPE* NAME = (BUFFER)!=0 ? (BUFFER) \
834 : reinterpret_cast<TYPE*>( \
835 (sizeof(TYPE)*SIZE<=EIGEN_STACK_ALLOCATION_LIMIT) ? EIGEN_ALIGNED_ALLOCA(sizeof(TYPE)*SIZE) \
836 : Eigen::internal::aligned_malloc(sizeof(TYPE)*SIZE) ); \
837 Eigen::internal::aligned_stack_memory_handler<TYPE> EIGEN_CAT(NAME,_stack_memory_destructor)((BUFFER)==0 ? NAME : 0,SIZE,sizeof(TYPE)*SIZE>EIGEN_STACK_ALLOCATION_LIMIT)
840 #define ei_declare_local_nested_eval(XPR_T,XPR,N,NAME) \
841 Eigen::internal::local_nested_eval_wrapper<XPR_T,N> EIGEN_CAT(NAME,_wrapper)(XPR, reinterpret_cast<typename XPR_T::Scalar*>( \
842 ( (Eigen::internal::local_nested_eval_wrapper<XPR_T,N>::NeedExternalBuffer) && ((sizeof(typename XPR_T::Scalar)*XPR.size())<=EIGEN_STACK_ALLOCATION_LIMIT) ) \
843 ? EIGEN_ALIGNED_ALLOCA( sizeof(typename XPR_T::Scalar)*XPR.size() ) : 0 ) ) ; \
844 typename Eigen::internal::local_nested_eval_wrapper<XPR_T,N>::ObjectType NAME(EIGEN_CAT(NAME,_wrapper).object)
848 #define ei_declare_aligned_stack_constructed_variable(TYPE,NAME,SIZE,BUFFER) \
849 Eigen::internal::check_size_for_overflow<TYPE>(SIZE); \
850 TYPE* NAME = (BUFFER)!=0 ? BUFFER : reinterpret_cast<TYPE*>(Eigen::internal::aligned_malloc(sizeof(TYPE)*SIZE)); \
851 Eigen::internal::aligned_stack_memory_handler<TYPE> EIGEN_CAT(NAME,_stack_memory_destructor)((BUFFER)==0 ? NAME : 0,SIZE,true)
854 #define ei_declare_local_nested_eval(XPR_T,XPR,N,NAME) typename Eigen::internal::nested_eval<XPR_T,N>::type NAME(XPR)
859 /*****************************************************************************
860 *** Implementation of EIGEN_MAKE_ALIGNED_OPERATOR_NEW [_IF] ***
861 *****************************************************************************/
863 #if EIGEN_HAS_CXX17_OVERALIGN
865 // C++17 -> no need to bother about alignment anymore :)
867 #define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_NOTHROW(NeedsToAlign)
868 #define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(NeedsToAlign)
869 #define EIGEN_MAKE_ALIGNED_OPERATOR_NEW
870 #define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF_VECTORIZABLE_FIXED_SIZE(Scalar,Size)
874 // HIP does not support new/delete on device.
875 #if EIGEN_MAX_ALIGN_BYTES!=0 && !defined(EIGEN_HIP_DEVICE_COMPILE)
876 #define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_NOTHROW(NeedsToAlign) \
878 void* operator new(std::size_t size, const std::nothrow_t&) EIGEN_NO_THROW { \
879 EIGEN_TRY { return Eigen::internal::conditional_aligned_malloc<NeedsToAlign>(size); } \
880 EIGEN_CATCH (...) { return 0; } \
882 #define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(NeedsToAlign) \
884 void *operator new(std::size_t size) { \
885 return Eigen::internal::conditional_aligned_malloc<NeedsToAlign>(size); \
888 void *operator new[](std::size_t size) { \
889 return Eigen::internal::conditional_aligned_malloc<NeedsToAlign>(size); \
892 void operator delete(void * ptr) EIGEN_NO_THROW { Eigen::internal::conditional_aligned_free<NeedsToAlign>(ptr); } \
894 void operator delete[](void * ptr) EIGEN_NO_THROW { Eigen::internal::conditional_aligned_free<NeedsToAlign>(ptr); } \
896 void operator delete(void * ptr, std::size_t /* sz */) EIGEN_NO_THROW { Eigen::internal::conditional_aligned_free<NeedsToAlign>(ptr); } \
898 void operator delete[](void * ptr, std::size_t /* sz */) EIGEN_NO_THROW { Eigen::internal::conditional_aligned_free<NeedsToAlign>(ptr); } \
899 /* in-place new and delete. since (at least afaik) there is no actual */ \
900 /* memory allocated we can safely let the default implementation handle */ \
901 /* this particular case. */ \
903 static void *operator new(std::size_t size, void *ptr) { return ::operator new(size,ptr); } \
905 static void *operator new[](std::size_t size, void* ptr) { return ::operator new[](size,ptr); } \
907 void operator delete(void * memory, void *ptr) EIGEN_NO_THROW { return ::operator delete(memory,ptr); } \
909 void operator delete[](void * memory, void *ptr) EIGEN_NO_THROW { return ::operator delete[](memory,ptr); } \
910 /* nothrow-new (returns zero instead of std::bad_alloc) */ \
911 EIGEN_MAKE_ALIGNED_OPERATOR_NEW_NOTHROW(NeedsToAlign) \
913 void operator delete(void *ptr, const std::nothrow_t&) EIGEN_NO_THROW { \
914 Eigen::internal::conditional_aligned_free<NeedsToAlign>(ptr); \
916 typedef void eigen_aligned_operator_new_marker_type;
918 #define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(NeedsToAlign)
921 #define EIGEN_MAKE_ALIGNED_OPERATOR_NEW EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(true)
922 #define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF_VECTORIZABLE_FIXED_SIZE(Scalar,Size) \
923 EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(bool( \
924 ((Size)!=Eigen::Dynamic) && \
925 (((EIGEN_MAX_ALIGN_BYTES>=16) && ((sizeof(Scalar)*(Size))%(EIGEN_MAX_ALIGN_BYTES )==0)) || \
926 ((EIGEN_MAX_ALIGN_BYTES>=32) && ((sizeof(Scalar)*(Size))%(EIGEN_MAX_ALIGN_BYTES/2)==0)) || \
927 ((EIGEN_MAX_ALIGN_BYTES>=64) && ((sizeof(Scalar)*(Size))%(EIGEN_MAX_ALIGN_BYTES/4)==0)) )))
931 /****************************************************************************/
933 /** \class aligned_allocator
934 * \ingroup Core_Module
936 * \brief STL compatible allocator to use with types requiring a non-standard alignment.
938 * The memory is aligned as for dynamically aligned matrix/array types such as MatrixXd.
939 * By default, it will thus provide at least 16 bytes alignment and more in following cases:
940 * - 32 bytes alignment if AVX is enabled.
941 * - 64 bytes alignment if AVX512 is enabled.
943 * This can be controlled using the \c EIGEN_MAX_ALIGN_BYTES macro as documented
944 * \link TopicPreprocessorDirectivesPerformance there \endlink.
948 * // Matrix4f requires 16 bytes alignment:
949 * std::map< int, Matrix4f, std::less<int>,
950 * aligned_allocator<std::pair<const int, Matrix4f> > > my_map_mat4;
951 * // Vector3f does not require 16 bytes alignment, no need to use Eigen's allocator:
952 * std::map< int, Vector3f > my_map_vec3;
955 * \sa \blank \ref TopicStlContainers.
958 class aligned_allocator : public std::allocator<T>
961 typedef std::size_t size_type;
962 typedef std::ptrdiff_t difference_type;
964 typedef const T* const_pointer;
965 typedef T& reference;
966 typedef const T& const_reference;
967 typedef T value_type;
972 typedef aligned_allocator<U> other;
975 aligned_allocator() : std::allocator<T>() {}
977 aligned_allocator(const aligned_allocator& other) : std::allocator<T>(other) {}
980 aligned_allocator(const aligned_allocator<U>& other) : std::allocator<T>(other) {}
982 ~aligned_allocator() {}
984 #if EIGEN_COMP_GNUC_STRICT && EIGEN_GNUC_STRICT_AT_LEAST(7,0,0)
985 // In gcc std::allocator::max_size() is bugged making gcc triggers a warning:
986 // eigen/Eigen/src/Core/util/Memory.h:189:12: warning: argument 1 value '18446744073709551612' exceeds maximum object size 9223372036854775807
987 // See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87544
988 size_type max_size() const {
989 return (std::numeric_limits<std::ptrdiff_t>::max)()/sizeof(T);
993 pointer allocate(size_type num, const void* /*hint*/ = 0)
995 internal::check_size_for_overflow<T>(num);
996 return static_cast<pointer>( internal::aligned_malloc(num * sizeof(T)) );
999 void deallocate(pointer p, size_type /*num*/)
1001 internal::aligned_free(p);
1005 //---------- Cache sizes ----------
1007 #if !defined(EIGEN_NO_CPUID)
1008 # if EIGEN_COMP_GNUC && EIGEN_ARCH_i386_OR_x86_64
1009 # if defined(__PIC__) && EIGEN_ARCH_i386
1010 // Case for x86 with PIC
1011 # define EIGEN_CPUID(abcd,func,id) \
1012 __asm__ __volatile__ ("xchgl %%ebx, %k1;cpuid; xchgl %%ebx,%k1": "=a" (abcd[0]), "=&r" (abcd[1]), "=c" (abcd[2]), "=d" (abcd[3]) : "a" (func), "c" (id));
1013 # elif defined(__PIC__) && EIGEN_ARCH_x86_64
1014 // Case for x64 with PIC. In theory this is only a problem with recent gcc and with medium or large code model, not with the default small code model.
1015 // However, we cannot detect which code model is used, and the xchg overhead is negligible anyway.
1016 # define EIGEN_CPUID(abcd,func,id) \
1017 __asm__ __volatile__ ("xchg{q}\t{%%}rbx, %q1; cpuid; xchg{q}\t{%%}rbx, %q1": "=a" (abcd[0]), "=&r" (abcd[1]), "=c" (abcd[2]), "=d" (abcd[3]) : "0" (func), "2" (id));
1019 // Case for x86_64 or x86 w/o PIC
1020 # define EIGEN_CPUID(abcd,func,id) \
1021 __asm__ __volatile__ ("cpuid": "=a" (abcd[0]), "=b" (abcd[1]), "=c" (abcd[2]), "=d" (abcd[3]) : "0" (func), "2" (id) );
1023 # elif EIGEN_COMP_MSVC
1024 # if EIGEN_ARCH_i386_OR_x86_64
1025 # define EIGEN_CPUID(abcd,func,id) __cpuidex((int*)abcd,func,id)
1030 namespace internal {
1034 inline bool cpuid_is_vendor(int abcd[4], const int vendor[3])
1036 return abcd[1]==vendor[0] && abcd[3]==vendor[1] && abcd[2]==vendor[2];
1039 inline void queryCacheSizes_intel_direct(int& l1, int& l2, int& l3)
1046 abcd[0] = abcd[1] = abcd[2] = abcd[3] = 0;
1047 EIGEN_CPUID(abcd,0x4,cache_id);
1048 cache_type = (abcd[0] & 0x0F) >> 0;
1049 if(cache_type==1||cache_type==3) // data or unified cache
1051 int cache_level = (abcd[0] & 0xE0) >> 5; // A[7:5]
1052 int ways = (abcd[1] & 0xFFC00000) >> 22; // B[31:22]
1053 int partitions = (abcd[1] & 0x003FF000) >> 12; // B[21:12]
1054 int line_size = (abcd[1] & 0x00000FFF) >> 0; // B[11:0]
1055 int sets = (abcd[2]); // C[31:0]
1057 int cache_size = (ways+1) * (partitions+1) * (line_size+1) * (sets+1);
1061 case 1: l1 = cache_size; break;
1062 case 2: l2 = cache_size; break;
1063 case 3: l3 = cache_size; break;
1068 } while(cache_type>0 && cache_id<16);
1071 inline void queryCacheSizes_intel_codes(int& l1, int& l2, int& l3)
1074 abcd[0] = abcd[1] = abcd[2] = abcd[3] = 0;
1076 EIGEN_CPUID(abcd,0x00000002,0);
1077 unsigned char * bytes = reinterpret_cast<unsigned char *>(abcd)+2;
1078 bool check_for_p2_core2 = false;
1079 for(int i=0; i<14; ++i)
1083 case 0x0A: l1 = 8; break; // 0Ah data L1 cache, 8 KB, 2 ways, 32 byte lines
1084 case 0x0C: l1 = 16; break; // 0Ch data L1 cache, 16 KB, 4 ways, 32 byte lines
1085 case 0x0E: l1 = 24; break; // 0Eh data L1 cache, 24 KB, 6 ways, 64 byte lines
1086 case 0x10: l1 = 16; break; // 10h data L1 cache, 16 KB, 4 ways, 32 byte lines (IA-64)
1087 case 0x15: l1 = 16; break; // 15h code L1 cache, 16 KB, 4 ways, 32 byte lines (IA-64)
1088 case 0x2C: l1 = 32; break; // 2Ch data L1 cache, 32 KB, 8 ways, 64 byte lines
1089 case 0x30: l1 = 32; break; // 30h code L1 cache, 32 KB, 8 ways, 64 byte lines
1090 case 0x60: l1 = 16; break; // 60h data L1 cache, 16 KB, 8 ways, 64 byte lines, sectored
1091 case 0x66: l1 = 8; break; // 66h data L1 cache, 8 KB, 4 ways, 64 byte lines, sectored
1092 case 0x67: l1 = 16; break; // 67h data L1 cache, 16 KB, 4 ways, 64 byte lines, sectored
1093 case 0x68: l1 = 32; break; // 68h data L1 cache, 32 KB, 4 ways, 64 byte lines, sectored
1094 case 0x1A: l2 = 96; break; // code and data L2 cache, 96 KB, 6 ways, 64 byte lines (IA-64)
1095 case 0x22: l3 = 512; break; // code and data L3 cache, 512 KB, 4 ways (!), 64 byte lines, dual-sectored
1096 case 0x23: l3 = 1024; break; // code and data L3 cache, 1024 KB, 8 ways, 64 byte lines, dual-sectored
1097 case 0x25: l3 = 2048; break; // code and data L3 cache, 2048 KB, 8 ways, 64 byte lines, dual-sectored
1098 case 0x29: l3 = 4096; break; // code and data L3 cache, 4096 KB, 8 ways, 64 byte lines, dual-sectored
1099 case 0x39: l2 = 128; break; // code and data L2 cache, 128 KB, 4 ways, 64 byte lines, sectored
1100 case 0x3A: l2 = 192; break; // code and data L2 cache, 192 KB, 6 ways, 64 byte lines, sectored
1101 case 0x3B: l2 = 128; break; // code and data L2 cache, 128 KB, 2 ways, 64 byte lines, sectored
1102 case 0x3C: l2 = 256; break; // code and data L2 cache, 256 KB, 4 ways, 64 byte lines, sectored
1103 case 0x3D: l2 = 384; break; // code and data L2 cache, 384 KB, 6 ways, 64 byte lines, sectored
1104 case 0x3E: l2 = 512; break; // code and data L2 cache, 512 KB, 4 ways, 64 byte lines, sectored
1105 case 0x40: l2 = 0; break; // no integrated L2 cache (P6 core) or L3 cache (P4 core)
1106 case 0x41: l2 = 128; break; // code and data L2 cache, 128 KB, 4 ways, 32 byte lines
1107 case 0x42: l2 = 256; break; // code and data L2 cache, 256 KB, 4 ways, 32 byte lines
1108 case 0x43: l2 = 512; break; // code and data L2 cache, 512 KB, 4 ways, 32 byte lines
1109 case 0x44: l2 = 1024; break; // code and data L2 cache, 1024 KB, 4 ways, 32 byte lines
1110 case 0x45: l2 = 2048; break; // code and data L2 cache, 2048 KB, 4 ways, 32 byte lines
1111 case 0x46: l3 = 4096; break; // code and data L3 cache, 4096 KB, 4 ways, 64 byte lines
1112 case 0x47: l3 = 8192; break; // code and data L3 cache, 8192 KB, 8 ways, 64 byte lines
1113 case 0x48: l2 = 3072; break; // code and data L2 cache, 3072 KB, 12 ways, 64 byte lines
1114 case 0x49: if(l2!=0) l3 = 4096; else {check_for_p2_core2=true; l3 = l2 = 4096;} break;// code and data L3 cache, 4096 KB, 16 ways, 64 byte lines (P4) or L2 for core2
1115 case 0x4A: l3 = 6144; break; // code and data L3 cache, 6144 KB, 12 ways, 64 byte lines
1116 case 0x4B: l3 = 8192; break; // code and data L3 cache, 8192 KB, 16 ways, 64 byte lines
1117 case 0x4C: l3 = 12288; break; // code and data L3 cache, 12288 KB, 12 ways, 64 byte lines
1118 case 0x4D: l3 = 16384; break; // code and data L3 cache, 16384 KB, 16 ways, 64 byte lines
1119 case 0x4E: l2 = 6144; break; // code and data L2 cache, 6144 KB, 24 ways, 64 byte lines
1120 case 0x78: l2 = 1024; break; // code and data L2 cache, 1024 KB, 4 ways, 64 byte lines
1121 case 0x79: l2 = 128; break; // code and data L2 cache, 128 KB, 8 ways, 64 byte lines, dual-sectored
1122 case 0x7A: l2 = 256; break; // code and data L2 cache, 256 KB, 8 ways, 64 byte lines, dual-sectored
1123 case 0x7B: l2 = 512; break; // code and data L2 cache, 512 KB, 8 ways, 64 byte lines, dual-sectored
1124 case 0x7C: l2 = 1024; break; // code and data L2 cache, 1024 KB, 8 ways, 64 byte lines, dual-sectored
1125 case 0x7D: l2 = 2048; break; // code and data L2 cache, 2048 KB, 8 ways, 64 byte lines
1126 case 0x7E: l2 = 256; break; // code and data L2 cache, 256 KB, 8 ways, 128 byte lines, sect. (IA-64)
1127 case 0x7F: l2 = 512; break; // code and data L2 cache, 512 KB, 2 ways, 64 byte lines
1128 case 0x80: l2 = 512; break; // code and data L2 cache, 512 KB, 8 ways, 64 byte lines
1129 case 0x81: l2 = 128; break; // code and data L2 cache, 128 KB, 8 ways, 32 byte lines
1130 case 0x82: l2 = 256; break; // code and data L2 cache, 256 KB, 8 ways, 32 byte lines
1131 case 0x83: l2 = 512; break; // code and data L2 cache, 512 KB, 8 ways, 32 byte lines
1132 case 0x84: l2 = 1024; break; // code and data L2 cache, 1024 KB, 8 ways, 32 byte lines
1133 case 0x85: l2 = 2048; break; // code and data L2 cache, 2048 KB, 8 ways, 32 byte lines
1134 case 0x86: l2 = 512; break; // code and data L2 cache, 512 KB, 4 ways, 64 byte lines
1135 case 0x87: l2 = 1024; break; // code and data L2 cache, 1024 KB, 8 ways, 64 byte lines
1136 case 0x88: l3 = 2048; break; // code and data L3 cache, 2048 KB, 4 ways, 64 byte lines (IA-64)
1137 case 0x89: l3 = 4096; break; // code and data L3 cache, 4096 KB, 4 ways, 64 byte lines (IA-64)
1138 case 0x8A: l3 = 8192; break; // code and data L3 cache, 8192 KB, 4 ways, 64 byte lines (IA-64)
1139 case 0x8D: l3 = 3072; break; // code and data L3 cache, 3072 KB, 12 ways, 128 byte lines (IA-64)
1144 if(check_for_p2_core2 && l2 == l3)
1151 inline void queryCacheSizes_intel(int& l1, int& l2, int& l3, int max_std_funcs)
1153 if(max_std_funcs>=4)
1154 queryCacheSizes_intel_direct(l1,l2,l3);
1155 else if(max_std_funcs>=2)
1156 queryCacheSizes_intel_codes(l1,l2,l3);
1161 inline void queryCacheSizes_amd(int& l1, int& l2, int& l3)
1164 abcd[0] = abcd[1] = abcd[2] = abcd[3] = 0;
1166 // First query the max supported function.
1167 EIGEN_CPUID(abcd,0x80000000,0);
1168 if(static_cast<numext::uint32_t>(abcd[0]) >= static_cast<numext::uint32_t>(0x80000006))
1170 EIGEN_CPUID(abcd,0x80000005,0);
1171 l1 = (abcd[2] >> 24) * 1024; // C[31:24] = L1 size in KB
1172 abcd[0] = abcd[1] = abcd[2] = abcd[3] = 0;
1173 EIGEN_CPUID(abcd,0x80000006,0);
1174 l2 = (abcd[2] >> 16) * 1024; // C[31;16] = l2 cache size in KB
1175 l3 = ((abcd[3] & 0xFFFC000) >> 18) * 512 * 1024; // D[31;18] = l3 cache size in 512KB
1185 * Queries and returns the cache sizes in Bytes of the L1, L2, and L3 data caches respectively */
1186 inline void queryCacheSizes(int& l1, int& l2, int& l3)
1190 const int GenuineIntel[] = {0x756e6547, 0x49656e69, 0x6c65746e};
1191 const int AuthenticAMD[] = {0x68747541, 0x69746e65, 0x444d4163};
1192 const int AMDisbetter_[] = {0x69444d41, 0x74656273, 0x21726574}; // "AMDisbetter!"
1194 // identify the CPU vendor
1195 EIGEN_CPUID(abcd,0x0,0);
1196 int max_std_funcs = abcd[0];
1197 if(cpuid_is_vendor(abcd,GenuineIntel))
1198 queryCacheSizes_intel(l1,l2,l3,max_std_funcs);
1199 else if(cpuid_is_vendor(abcd,AuthenticAMD) || cpuid_is_vendor(abcd,AMDisbetter_))
1200 queryCacheSizes_amd(l1,l2,l3);
1202 // by default let's use Intel's API
1203 queryCacheSizes_intel(l1,l2,l3,max_std_funcs);
1205 // here is the list of other vendors:
1206 // ||cpuid_is_vendor(abcd,"VIA VIA VIA ")
1207 // ||cpuid_is_vendor(abcd,"CyrixInstead")
1208 // ||cpuid_is_vendor(abcd,"CentaurHauls")
1209 // ||cpuid_is_vendor(abcd,"GenuineTMx86")
1210 // ||cpuid_is_vendor(abcd,"TransmetaCPU")
1211 // ||cpuid_is_vendor(abcd,"RiseRiseRise")
1212 // ||cpuid_is_vendor(abcd,"Geode by NSC")
1213 // ||cpuid_is_vendor(abcd,"SiS SiS SiS ")
1214 // ||cpuid_is_vendor(abcd,"UMC UMC UMC ")
1215 // ||cpuid_is_vendor(abcd,"NexGenDriven")
1222 * \returns the size in Bytes of the L1 data cache */
1223 inline int queryL1CacheSize()
1226 queryCacheSizes(l1,l2,l3);
1231 * \returns the size in Bytes of the L2 or L3 cache if this later is present */
1232 inline int queryTopLevelCacheSize()
1234 int l1, l2(-1), l3(-1);
1235 queryCacheSizes(l1,l2,l3);
1236 return (std::max)(l2,l3);
1242 * This wraps C++20's std::construct_at, using placement new instead if it is not available.
1245 #if EIGEN_COMP_CXXVER >= 20 && !BUILDFLAG(IS_TIZEN)
1246 using std::construct_at;
1248 template<class T, class... Args>
1249 EIGEN_DEVICE_FUNC T* construct_at( T* p, Args&&... args )
1251 return ::new (const_cast<void*>(static_cast<const volatile void*>(p)))
1252 T(std::forward<Args>(args)...);
1257 * This wraps C++17's std::destroy_at. If it's not available it calls the destructor.
1258 * The wrapper is not a full replacement for C++20's std::destroy_at as it cannot
1259 * be applied to std::array.
1261 #if EIGEN_COMP_CXXVER >= 17
1262 using std::destroy_at;
1265 EIGEN_DEVICE_FUNC void destroy_at(T* p)
1271 } // end namespace internal
1273 } // end namespace Eigen
1275 #endif // EIGEN_MEMORY_H