27#undef EIGEN_MALLOC_ALREADY_ALIGNED
28#define EIGEN_MALLOC_ALREADY_ALIGNED 0
31#if defined(__x86_64__) || defined(__i386__)
32#define EIGEN_HEAP_ALIGN_BYTES 64
33#elif defined(__HVX__) && (__HVX_LENGTH__ == 128)
34#define EIGEN_HEAP_ALIGN_BYTES 128
36#define EIGEN_HEAP_ALIGN_BYTES 16
39#ifndef EIGEN_MALLOC_CHECK_THREAD_LOCAL
44#ifndef EIGEN_AVOID_THREAD_LOCAL
46#if ((EIGEN_COMP_GNUC) || __has_feature(cxx_thread_local) || EIGEN_COMP_MSVC >= 1900) && \
47 !defined(EIGEN_GPU_COMPILE_PHASE)
48#define EIGEN_MALLOC_CHECK_THREAD_LOCAL thread_local
50#define EIGEN_MALLOC_CHECK_THREAD_LOCAL
54#define EIGEN_MALLOC_CHECK_THREAD_LOCAL
60#include "../InternalHeaderCheck.h"
71EIGEN_DEVICE_FUNC
inline void check_that_malloc_is_allowed() {
72 eigen_assert(
false &&
"heap allocation is forbidden (EIGEN_NO_MALLOC is defined)");
74EIGEN_DEVICE_FUNC
inline void check_that_free_is_allowed() {
75 eigen_assert(
false &&
"heap deallocation is forbidden (EIGEN_NO_MALLOC is defined)");
77#elif defined EIGEN_RUNTIME_NO_MALLOC
78EIGEN_DEVICE_FUNC
inline bool is_malloc_allowed_impl(
bool update,
bool new_value =
false) {
79 EIGEN_MALLOC_CHECK_THREAD_LOCAL
static bool value =
true;
80 if (update == 1) value = new_value;
83EIGEN_DEVICE_FUNC
inline bool is_malloc_allowed() {
return is_malloc_allowed_impl(
false); }
84EIGEN_DEVICE_FUNC
inline bool set_is_malloc_allowed(
bool new_value) {
return is_malloc_allowed_impl(
true, new_value); }
85EIGEN_DEVICE_FUNC
inline void check_that_malloc_is_allowed() {
86 eigen_assert(is_malloc_allowed() &&
87 "heap allocation is forbidden (EIGEN_RUNTIME_NO_MALLOC is defined and set_is_malloc_allowed is false)");
89EIGEN_DEVICE_FUNC
inline bool is_free_allowed_impl(
bool update,
bool new_value =
false) {
90 EIGEN_MALLOC_CHECK_THREAD_LOCAL
static bool value =
true;
91 if (update == 1) value = new_value;
94EIGEN_DEVICE_FUNC
inline bool is_free_allowed() {
return is_free_allowed_impl(
false); }
95EIGEN_DEVICE_FUNC
inline bool set_is_free_allowed(
bool new_value) {
return is_free_allowed_impl(
true, new_value); }
96EIGEN_DEVICE_FUNC
inline void check_that_free_is_allowed() {
97 eigen_assert(is_malloc_allowed() &&
98 "heap deallocation is forbidden (EIGEN_RUNTIME_NO_MALLOC is defined and set_is_free_allowed is false)");
101EIGEN_DEVICE_FUNC
inline void check_that_malloc_is_allowed() {}
102EIGEN_DEVICE_FUNC
inline void check_that_free_is_allowed() {}
105EIGEN_DEVICE_FUNC
inline void throw_std_bad_alloc() {
106#ifdef EIGEN_EXCEPTIONS
107 throw std::bad_alloc();
109 std::size_t huge =
static_cast<std::size_t
>(-1);
110#if defined(EIGEN_HIPCC)
122 void* unused = ::operator
new(huge);
123 EIGEN_UNUSED_VARIABLE(unused);
137EIGEN_DEVICE_FUNC
inline void* handmade_aligned_malloc(std::size_t size,
138 std::size_t alignment = EIGEN_DEFAULT_ALIGN_BYTES) {
139 eigen_assert(alignment >=
sizeof(
void*) && alignment <= 256 && (alignment & (alignment - 1)) == 0 &&
140 "Alignment must be at least sizeof(void*), less than or equal to 256, and a power of 2");
142 check_that_malloc_is_allowed();
143 EIGEN_USING_STD(malloc)
144 void* original = malloc(size + alignment);
145 if (original ==
nullptr)
return nullptr;
146 std::size_t offset = alignment - (
reinterpret_cast<std::size_t
>(original) & (alignment - 1));
147 void* aligned =
static_cast<void*
>(
static_cast<uint8_t*
>(original) + offset);
149 *(
static_cast<uint8_t*
>(aligned) - 1) =
static_cast<uint8_t
>(offset - 1);
154EIGEN_DEVICE_FUNC
inline void handmade_aligned_free(
void* ptr) {
155 if (ptr !=
nullptr) {
156 std::size_t offset =
static_cast<std::size_t
>(*(
static_cast<uint8_t*
>(ptr) - 1)) + 1;
157 void* original =
static_cast<void*
>(
static_cast<uint8_t*
>(ptr) - offset);
159 check_that_free_is_allowed();
160 EIGEN_USING_STD(free)
170EIGEN_DEVICE_FUNC
inline void* handmade_aligned_realloc(
void* ptr, std::size_t new_size, std::size_t old_size,
171 std::size_t alignment = EIGEN_DEFAULT_ALIGN_BYTES) {
172 if (ptr ==
nullptr)
return handmade_aligned_malloc(new_size, alignment);
173 std::size_t old_offset =
static_cast<std::size_t
>(*(
static_cast<uint8_t*
>(ptr) - 1)) + 1;
174 void* old_original =
static_cast<uint8_t*
>(ptr) - old_offset;
176 check_that_malloc_is_allowed();
177 EIGEN_USING_STD(realloc)
178 void* original = realloc(old_original, new_size + alignment);
179 if (original ==
nullptr)
return nullptr;
180 if (original == old_original)
return ptr;
181 std::size_t offset = alignment - (
reinterpret_cast<std::size_t
>(original) & (alignment - 1));
182 void* aligned =
static_cast<void*
>(
static_cast<uint8_t*
>(original) + offset);
183 if (offset != old_offset) {
184 const void* src =
static_cast<const void*
>(
static_cast<uint8_t*
>(original) + old_offset);
185 std::size_t count = (std::min)(new_size, old_size);
186 std::memmove(aligned, src, count);
189 *(
static_cast<uint8_t*
>(aligned) - 1) =
static_cast<uint8_t
>(offset - 1);
196EIGEN_DEVICE_FUNC
inline void* aligned_malloc(std::size_t size) {
197 if (size == 0)
return nullptr;
199 void* result = handmade_aligned_malloc(size, EIGEN_HEAP_ALIGN_BYTES);
201 if (!result && size) throw_std_bad_alloc();
207EIGEN_DEVICE_FUNC
inline void aligned_free(
void* ptr) { handmade_aligned_free(ptr); }
214EIGEN_DEVICE_FUNC
inline void* aligned_realloc(
void* ptr, std::size_t new_size, std::size_t old_size) {
215 if (ptr ==
nullptr)
return aligned_malloc(new_size);
216 if (old_size == new_size)
return ptr;
222 void* result = handmade_aligned_realloc(ptr, new_size, old_size, EIGEN_HEAP_ALIGN_BYTES);
224 if (!result && new_size) throw_std_bad_alloc();
237EIGEN_DEVICE_FUNC
inline void* conditional_aligned_malloc(std::size_t size) {
238 return aligned_malloc(size);
242EIGEN_DEVICE_FUNC
inline void* conditional_aligned_malloc<false>(std::size_t size) {
243 if (size == 0)
return nullptr;
245 check_that_malloc_is_allowed();
246 EIGEN_USING_STD(malloc)
247 void* result = malloc(size);
249 if (!result && size) throw_std_bad_alloc();
255EIGEN_DEVICE_FUNC
inline void conditional_aligned_free(
void* ptr) {
260EIGEN_DEVICE_FUNC
inline void conditional_aligned_free<false>(
void* ptr) {
261 if (ptr !=
nullptr) {
262 check_that_free_is_allowed();
263 EIGEN_USING_STD(free)
269EIGEN_DEVICE_FUNC
inline void* conditional_aligned_realloc(
void* ptr, std::size_t new_size, std::size_t old_size) {
270 return aligned_realloc(ptr, new_size, old_size);
274EIGEN_DEVICE_FUNC
inline void* conditional_aligned_realloc<false>(
void* ptr, std::size_t new_size,
275 std::size_t old_size) {
276 if (ptr ==
nullptr)
return conditional_aligned_malloc<false>(new_size);
277 if (old_size == new_size)
return ptr;
279 conditional_aligned_free<false>(ptr);
283 check_that_malloc_is_allowed();
284 EIGEN_USING_STD(realloc)
285 return realloc(ptr, new_size);
296EIGEN_DEVICE_FUNC
inline void destruct_elements_of_array(T* ptr, std::size_t size) {
299 while (size) ptr[--size].~T();
306EIGEN_DEVICE_FUNC
inline T* default_construct_elements_of_array(T* ptr, std::size_t size) {
309 for (i = 0; i < size; ++i) ::new (ptr + i) T;
312 destruct_elements_of_array(ptr, i);
322EIGEN_DEVICE_FUNC
inline T* copy_construct_elements_of_array(T* ptr,
const T* src, std::size_t size) {
325 for (i = 0; i < size; ++i) ::new (ptr + i) T(*(src + i));
328 destruct_elements_of_array(ptr, i);
338EIGEN_DEVICE_FUNC
inline T* move_construct_elements_of_array(T* ptr, T* src, std::size_t size) {
341 for (i = 0; i < size; ++i) ::new (ptr + i) T(std::move(*(src + i)));
344 destruct_elements_of_array(ptr, i);
355EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
void check_size_for_overflow(std::size_t size) {
356 constexpr std::size_t max_elements = (std::numeric_limits<std::ptrdiff_t>::max)() /
sizeof(T);
357 if (size > max_elements) throw_std_bad_alloc();
365EIGEN_DEVICE_FUNC
inline T* aligned_new(std::size_t size) {
366 check_size_for_overflow<T>(size);
367 T* result =
static_cast<T*
>(aligned_malloc(
sizeof(T) * size));
368 EIGEN_TRY {
return default_construct_elements_of_array(result, size); }
370 aligned_free(result);
376template <
typename T,
bool Align>
377EIGEN_DEVICE_FUNC
inline T* conditional_aligned_new(std::size_t size) {
378 check_size_for_overflow<T>(size);
379 T* result =
static_cast<T*
>(conditional_aligned_malloc<Align>(
sizeof(T) * size));
380 EIGEN_TRY {
return default_construct_elements_of_array(result, size); }
382 conditional_aligned_free<Align>(result);
392EIGEN_DEVICE_FUNC
inline void aligned_delete(T* ptr, std::size_t size) {
393 destruct_elements_of_array<T>(ptr, size);
400template <
typename T,
bool Align>
401EIGEN_DEVICE_FUNC
inline void conditional_aligned_delete(T* ptr, std::size_t size) {
402 destruct_elements_of_array<T>(ptr, size);
403 conditional_aligned_free<Align>(ptr);
406template <
typename T,
bool Align>
407EIGEN_DEVICE_FUNC
inline T* conditional_aligned_realloc_new(T* pts, std::size_t new_size, std::size_t old_size) {
408 check_size_for_overflow<T>(new_size);
409 check_size_for_overflow<T>(old_size);
415 T* result =
static_cast<T*
>(conditional_aligned_malloc<Align>(
sizeof(T) * new_size));
418 std::size_t copy_size = (std::min)(old_size, new_size);
419 move_construct_elements_of_array(result, pts, copy_size);
422 if (new_size > old_size) {
423 default_construct_elements_of_array(result + copy_size, new_size - old_size);
427 conditional_aligned_delete<T, Align>(pts, old_size);
430 conditional_aligned_free<Align>(result);
437template <
typename T,
bool Align>
438EIGEN_DEVICE_FUNC
inline T* conditional_aligned_new_auto(std::size_t size) {
439 if (size == 0)
return nullptr;
440 check_size_for_overflow<T>(size);
441 T* result =
static_cast<T*
>(conditional_aligned_malloc<Align>(
sizeof(T) * size));
442 if (NumTraits<T>::RequireInitialization) {
443 EIGEN_TRY { default_construct_elements_of_array(result, size); }
445 conditional_aligned_free<Align>(result);
452template <
typename T,
bool Align>
453EIGEN_DEVICE_FUNC
inline T* conditional_aligned_realloc_new_auto(T* pts, std::size_t new_size, std::size_t old_size) {
454 if (NumTraits<T>::RequireInitialization) {
455 return conditional_aligned_realloc_new<T, Align>(pts, new_size, old_size);
458 check_size_for_overflow<T>(new_size);
459 check_size_for_overflow<T>(old_size);
460 return static_cast<T*
>(
461 conditional_aligned_realloc<Align>(
static_cast<void*
>(pts),
sizeof(T) * new_size,
sizeof(T) * old_size));
464template <
typename T,
bool Align>
465EIGEN_DEVICE_FUNC
inline void conditional_aligned_delete_auto(T* ptr, std::size_t size) {
466 if (NumTraits<T>::RequireInitialization) destruct_elements_of_array<T>(ptr, size);
467 conditional_aligned_free<Align>(ptr);
490template <
int Alignment,
typename Scalar,
typename Index>
491EIGEN_DEVICE_FUNC
inline Index first_aligned(
const Scalar* array,
Index size) {
492 const Index ScalarSize =
sizeof(Scalar);
493 const Index AlignmentSize = Alignment / ScalarSize;
494 const Index AlignmentMask = AlignmentSize - 1;
496 if (AlignmentSize <= 1) {
500 }
else if ((std::uintptr_t(array) & (
sizeof(Scalar) - 1)) || (Alignment % ScalarSize) != 0) {
505 Index first = (AlignmentSize - (
Index((std::uintptr_t(array) /
sizeof(Scalar))) & AlignmentMask)) & AlignmentMask;
506 return (first < size) ? first : size;
512template <
typename Scalar,
typename Index>
513EIGEN_DEVICE_FUNC
inline Index first_default_aligned(
const Scalar* array,
Index size) {
514 typedef typename packet_traits<Scalar>::type DefaultPacketType;
515 return first_aligned<unpacket_traits<DefaultPacketType>::alignment>(array, size);
520template <
typename Index>
522 return ((size + base - 1) / base) * base;
527template <
typename T,
bool UseMemcpy>
528struct smart_copy_helper;
531EIGEN_DEVICE_FUNC
void smart_copy(
const T* start,
const T* end, T* target) {
532 smart_copy_helper<T, !NumTraits<T>::RequireInitialization>::run(start, end, target);
536struct smart_copy_helper<T, true> {
537 EIGEN_DEVICE_FUNC
static inline void run(
const T* start,
const T* end, T* target) {
538 std::intptr_t size = std::intptr_t(end) - std::intptr_t(start);
539 if (size == 0)
return;
540 eigen_internal_assert(start != 0 && end != 0 && target != 0);
541 EIGEN_USING_STD(memcpy)
542 memcpy(target, start, size);
547struct smart_copy_helper<T, false> {
548 EIGEN_DEVICE_FUNC
static inline void run(
const T* start,
const T* end, T* target) { std::copy(start, end, target); }
552template <
typename T,
bool UseMemmove>
553struct smart_memmove_helper;
556void smart_memmove(
const T* start,
const T* end, T* target) {
557 smart_memmove_helper<T, !NumTraits<T>::RequireInitialization>::run(start, end, target);
561struct smart_memmove_helper<T, true> {
562 static inline void run(
const T* start,
const T* end, T* target) {
563 std::intptr_t size = std::intptr_t(end) - std::intptr_t(start);
564 if (size == 0)
return;
565 eigen_internal_assert(start != 0 && end != 0 && target != 0);
566 std::memmove(target, start, size);
571struct smart_memmove_helper<T, false> {
572 static inline void run(
const T* start,
const T* end, T* target) {
573 if (std::uintptr_t(target) < std::uintptr_t(start)) {
574 std::copy(start, end, target);
576 std::ptrdiff_t count = (std::ptrdiff_t(end) - std::ptrdiff_t(start)) /
sizeof(T);
577 std::copy_backward(start, end, target + count);
583EIGEN_DEVICE_FUNC T* smart_move(T* start, T* end, T* target) {
584 return std::move(start, end, target);
593#if !defined EIGEN_ALLOCA && !defined EIGEN_GPU_COMPILE_PHASE
594#if EIGEN_OS_LINUX || EIGEN_OS_MAC || (defined alloca)
595#define EIGEN_ALLOCA alloca
597#define EIGEN_ALLOCA _alloca
606#if defined(__clang__) && defined(__thumb__)
613class aligned_stack_memory_handler : noncopyable {
621 EIGEN_DEVICE_FUNC aligned_stack_memory_handler(T* ptr, std::size_t size,
bool dealloc)
622 : m_ptr(ptr), m_size(size), m_deallocate(dealloc) {
623 if (NumTraits<T>::RequireInitialization && m_ptr) Eigen::internal::default_construct_elements_of_array(m_ptr, size);
625 EIGEN_DEVICE_FUNC ~aligned_stack_memory_handler() {
626 if (NumTraits<T>::RequireInitialization && m_ptr) Eigen::internal::destruct_elements_of_array<T>(m_ptr, m_size);
627 if (m_deallocate) Eigen::internal::aligned_free(m_ptr);
638template <
typename Xpr,
int NbEvaluations,
639 bool MapExternalBuffer = nested_eval<Xpr, NbEvaluations>::Evaluate && Xpr::MaxSizeAtCompileTime ==
Dynamic>
640struct local_nested_eval_wrapper {
641 static constexpr bool NeedExternalBuffer =
false;
642 typedef typename Xpr::Scalar Scalar;
643 typedef typename nested_eval<Xpr, NbEvaluations>::type ObjectType;
646 EIGEN_DEVICE_FUNC local_nested_eval_wrapper(
const Xpr& xpr, Scalar* ptr) : object(xpr) {
647 EIGEN_UNUSED_VARIABLE(ptr);
648 eigen_internal_assert(ptr == 0);
652template <
typename Xpr,
int NbEvaluations>
653struct local_nested_eval_wrapper<Xpr, NbEvaluations, true> {
654 static constexpr bool NeedExternalBuffer =
true;
655 typedef typename Xpr::Scalar Scalar;
656 typedef typename plain_object_eval<Xpr>::type PlainObject;
657 typedef Map<PlainObject, EIGEN_DEFAULT_ALIGN_BYTES> ObjectType;
660 EIGEN_DEVICE_FUNC local_nested_eval_wrapper(
const Xpr& xpr, Scalar* ptr)
661 : object(ptr == 0 ? reinterpret_cast<Scalar*>(Eigen::internal::aligned_malloc(sizeof(Scalar) * xpr.size())) : ptr,
662 xpr.rows(), xpr.cols()),
663 m_deallocate(ptr == 0) {
664 if (NumTraits<Scalar>::RequireInitialization &&
object.data())
665 Eigen::internal::default_construct_elements_of_array(
object.data(),
object.size());
669 EIGEN_DEVICE_FUNC ~local_nested_eval_wrapper() {
670 if (NumTraits<Scalar>::RequireInitialization &&
object.data())
671 Eigen::internal::destruct_elements_of_array(
object.data(),
object.size());
672 if (m_deallocate) Eigen::internal::aligned_free(
object.data());
682class scoped_array : noncopyable {
686 explicit scoped_array(std::ptrdiff_t size) { m_ptr =
new T[size]; }
687 ~scoped_array() {
delete[] m_ptr; }
688 T& operator[](std::ptrdiff_t i) {
return m_ptr[i]; }
689 const T& operator[](std::ptrdiff_t i)
const {
return m_ptr[i]; }
690 T*& ptr() {
return m_ptr; }
691 const T* ptr()
const {
return m_ptr; }
692 operator const T*()
const {
return m_ptr; }
696void swap(scoped_array<T>& a, scoped_array<T>& b) {
697 std::swap(a.ptr(), b.ptr());
725#if defined(EIGEN_ALLOCA) && !defined(EIGEN_NO_ALLOCA)
727#if EIGEN_DEFAULT_ALIGN_BYTES > 0
731#if ((EIGEN_COMP_GNUC || EIGEN_COMP_CLANG) && !EIGEN_COMP_NVHPC)
732#define EIGEN_ALIGNED_ALLOCA(SIZE) __builtin_alloca_with_align(SIZE, CHAR_BIT* EIGEN_DEFAULT_ALIGN_BYTES)
734EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void* eigen_aligned_alloca_helper(
void* ptr) {
735 constexpr std::uintptr_t mask = EIGEN_DEFAULT_ALIGN_BYTES - 1;
736 std::uintptr_t ptr_int = std::uintptr_t(ptr);
737 std::uintptr_t aligned_ptr_int = (ptr_int + mask) & ~mask;
738 std::uintptr_t offset = aligned_ptr_int - ptr_int;
739 return static_cast<void*
>(
static_cast<uint8_t*
>(ptr) + offset);
741#define EIGEN_ALIGNED_ALLOCA(SIZE) eigen_aligned_alloca_helper(EIGEN_ALLOCA(SIZE + EIGEN_DEFAULT_ALIGN_BYTES - 1))
745#define EIGEN_ALIGNED_ALLOCA(SIZE) EIGEN_ALLOCA(SIZE)
748#define ei_declare_aligned_stack_constructed_variable(TYPE, NAME, SIZE, BUFFER) \
749 Eigen::internal::check_size_for_overflow<TYPE>(SIZE); \
750 TYPE* NAME = (BUFFER) != 0 ? (BUFFER) \
751 : reinterpret_cast<TYPE*>((sizeof(TYPE) * (SIZE) <= EIGEN_STACK_ALLOCATION_LIMIT) \
752 ? EIGEN_ALIGNED_ALLOCA(sizeof(TYPE) * (SIZE)) \
753 : Eigen::internal::aligned_malloc(sizeof(TYPE) * (SIZE))); \
754 Eigen::internal::aligned_stack_memory_handler<TYPE> EIGEN_CAT(NAME, _stack_memory_destructor)( \
755 (BUFFER) == 0 ? NAME : 0, SIZE, sizeof(TYPE) * (SIZE) > EIGEN_STACK_ALLOCATION_LIMIT)
757#define ei_declare_local_nested_eval(XPR_T, XPR, N, NAME) \
758 Eigen::internal::local_nested_eval_wrapper<XPR_T, N> EIGEN_CAT(NAME, _wrapper)( \
759 XPR, reinterpret_cast<typename XPR_T::Scalar*>( \
760 ((Eigen::internal::local_nested_eval_wrapper<XPR_T, N>::NeedExternalBuffer) && \
761 ((sizeof(typename XPR_T::Scalar) * XPR.size()) <= EIGEN_STACK_ALLOCATION_LIMIT)) \
762 ? EIGEN_ALIGNED_ALLOCA(sizeof(typename XPR_T::Scalar) * XPR.size()) \
764 typename Eigen::internal::local_nested_eval_wrapper<XPR_T, N>::ObjectType NAME(EIGEN_CAT(NAME, _wrapper).object)
768#define ei_declare_aligned_stack_constructed_variable(TYPE, NAME, SIZE, BUFFER) \
769 Eigen::internal::check_size_for_overflow<TYPE>(SIZE); \
771 (BUFFER) != 0 ? BUFFER : reinterpret_cast<TYPE*>(Eigen::internal::aligned_malloc(sizeof(TYPE) * (SIZE))); \
772 Eigen::internal::aligned_stack_memory_handler<TYPE> EIGEN_CAT(NAME, _stack_memory_destructor)( \
773 (BUFFER) == 0 ? NAME : 0, SIZE, true)
775#define ei_declare_local_nested_eval(XPR_T, XPR, N, NAME) \
776 typename Eigen::internal::nested_eval<XPR_T, N>::type NAME(XPR)
784#if EIGEN_HAS_CXX17_OVERALIGN
788#define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_NOTHROW(NeedsToAlign)
789#define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(NeedsToAlign)
790#define EIGEN_MAKE_ALIGNED_OPERATOR_NEW
791#define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF_VECTORIZABLE_FIXED_SIZE(Scalar, Size)
796#if EIGEN_MAX_ALIGN_BYTES != 0 && !defined(EIGEN_HIP_DEVICE_COMPILE)
797#define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_NOTHROW(NeedsToAlign) \
798 EIGEN_DEVICE_FUNC void* operator new(std::size_t size, const std::nothrow_t&) noexcept { \
799 EIGEN_TRY { return Eigen::internal::conditional_aligned_malloc<NeedsToAlign>(size); } \
800 EIGEN_CATCH(...) { return 0; } \
802#define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(NeedsToAlign) \
803 EIGEN_DEVICE_FUNC void* operator new(std::size_t size) { \
804 return Eigen::internal::conditional_aligned_malloc<NeedsToAlign>(size); \
806 EIGEN_DEVICE_FUNC void* operator new[](std::size_t size) { \
807 return Eigen::internal::conditional_aligned_malloc<NeedsToAlign>(size); \
809 EIGEN_DEVICE_FUNC void operator delete(void* ptr) noexcept { \
810 Eigen::internal::conditional_aligned_free<NeedsToAlign>(ptr); \
812 EIGEN_DEVICE_FUNC void operator delete[](void* ptr) noexcept { \
813 Eigen::internal::conditional_aligned_free<NeedsToAlign>(ptr); \
815 EIGEN_DEVICE_FUNC void operator delete(void* ptr, std::size_t ) noexcept { \
816 Eigen::internal::conditional_aligned_free<NeedsToAlign>(ptr); \
818 EIGEN_DEVICE_FUNC void operator delete[](void* ptr, std::size_t ) noexcept { \
819 Eigen::internal::conditional_aligned_free<NeedsToAlign>(ptr); \
824 EIGEN_DEVICE_FUNC static void* operator new(std::size_t size, void* ptr) { return ::operator new(size, ptr); } \
825 EIGEN_DEVICE_FUNC static void* operator new[](std::size_t size, void* ptr) { return ::operator new[](size, ptr); } \
826 EIGEN_DEVICE_FUNC void operator delete(void* memory, void* ptr) noexcept { return ::operator delete(memory, ptr); } \
827 EIGEN_DEVICE_FUNC void operator delete[](void* memory, void* ptr) noexcept { \
828 return ::operator delete[](memory, ptr); \
831 EIGEN_MAKE_ALIGNED_OPERATOR_NEW_NOTHROW(NeedsToAlign) \
832 EIGEN_DEVICE_FUNC void operator delete(void* ptr, const std::nothrow_t&) noexcept { \
833 Eigen::internal::conditional_aligned_free<NeedsToAlign>(ptr); \
835 typedef void eigen_aligned_operator_new_marker_type;
837#define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(NeedsToAlign)
840#define EIGEN_MAKE_ALIGNED_OPERATOR_NEW EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(true)
841#define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF_VECTORIZABLE_FIXED_SIZE(Scalar, Size) \
842 EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF( \
843 bool(((Size) != Eigen::Dynamic) && \
844 (((EIGEN_MAX_ALIGN_BYTES >= 16) && ((sizeof(Scalar) * (Size)) % (EIGEN_MAX_ALIGN_BYTES) == 0)) || \
845 ((EIGEN_MAX_ALIGN_BYTES >= 32) && ((sizeof(Scalar) * (Size)) % (EIGEN_MAX_ALIGN_BYTES / 2) == 0)) || \
846 ((EIGEN_MAX_ALIGN_BYTES >= 64) && ((sizeof(Scalar) * (Size)) % (EIGEN_MAX_ALIGN_BYTES / 4) == 0)))))
877class aligned_allocator {
879 typedef std::size_t size_type;
880 typedef std::ptrdiff_t difference_type;
882 typedef const T* const_pointer;
883 typedef T& reference;
884 typedef const T& const_reference;
885 typedef T value_type;
889 typedef aligned_allocator<U> other;
892 aligned_allocator() =
default;
894 aligned_allocator(
const aligned_allocator&) =
default;
897 aligned_allocator(
const aligned_allocator<U>&) {}
900 constexpr bool operator==(
const aligned_allocator<U>&)
const noexcept {
904 constexpr bool operator!=(
const aligned_allocator<U>&)
const noexcept {
908#if EIGEN_COMP_GNUC_STRICT && EIGEN_GNUC_STRICT_AT_LEAST(7, 0, 0)
912 size_type max_size()
const {
return (std::numeric_limits<std::ptrdiff_t>::max)() /
sizeof(T); }
915 pointer allocate(size_type num,
const void* = 0) {
916 internal::check_size_for_overflow<T>(num);
917 return static_cast<pointer
>(internal::aligned_malloc(num *
sizeof(T)));
920 void deallocate(pointer p, size_type ) { internal::aligned_free(p); }
925#if !defined(EIGEN_NO_CPUID)
926#if EIGEN_COMP_GNUC && EIGEN_ARCH_i386_OR_x86_64
927#if defined(__PIC__) && EIGEN_ARCH_i386
929#define EIGEN_CPUID(abcd, func, id) \
930 __asm__ __volatile__("xchgl %%ebx, %k1;cpuid; xchgl %%ebx,%k1" \
931 : "=a"(abcd[0]), "=&r"(abcd[1]), "=c"(abcd[2]), "=d"(abcd[3]) \
932 : "a"(func), "c"(id));
933#elif defined(__PIC__) && EIGEN_ARCH_x86_64
937#define EIGEN_CPUID(abcd, func, id) \
938 __asm__ __volatile__("xchg{q}\t{%%}rbx, %q1; cpuid; xchg{q}\t{%%}rbx, %q1" \
939 : "=a"(abcd[0]), "=&r"(abcd[1]), "=c"(abcd[2]), "=d"(abcd[3]) \
940 : "0"(func), "2"(id));
943#define EIGEN_CPUID(abcd, func, id) \
944 __asm__ __volatile__("cpuid" : "=a"(abcd[0]), "=b"(abcd[1]), "=c"(abcd[2]), "=d"(abcd[3]) : "0"(func), "2"(id));
947#if EIGEN_ARCH_i386_OR_x86_64
948#define EIGEN_CPUID(abcd, func, id) __cpuidex((int*)abcd, func, id)
957inline bool cpuid_is_vendor(
int abcd[4],
const int vendor[3]) {
958 return abcd[1] == vendor[0] && abcd[3] == vendor[1] && abcd[2] == vendor[2];
961inline void queryCacheSizes_intel_direct(
int& l1,
int& l2,
int& l3) {
967 abcd[0] = abcd[1] = abcd[2] = abcd[3] = 0;
968 EIGEN_CPUID(abcd, 0x4, cache_id);
969 cache_type = (abcd[0] & 0x0F) >> 0;
970 if (cache_type == 1 || cache_type == 3)
972 int cache_level = (abcd[0] & 0xE0) >> 5;
973 int ways = (abcd[1] & 0xFFC00000) >> 22;
974 int partitions = (abcd[1] & 0x003FF000) >> 12;
975 int line_size = (abcd[1] & 0x00000FFF) >> 0;
976 int sets = (abcd[2]);
978 int cache_size = (ways + 1) * (partitions + 1) * (line_size + 1) * (sets + 1);
980 switch (cache_level) {
995 }
while (cache_type > 0 && cache_id < 16);
998inline void queryCacheSizes_intel_codes(
int& l1,
int& l2,
int& l3) {
1000 abcd[0] = abcd[1] = abcd[2] = abcd[3] = 0;
1002 EIGEN_CPUID(abcd, 0x00000002, 0);
1003 unsigned char* bytes =
reinterpret_cast<unsigned char*
>(abcd) + 2;
1004 bool check_for_p2_core2 =
false;
1005 for (
int i = 0; i < 14; ++i) {
1104 check_for_p2_core2 =
true;
1188 if (check_for_p2_core2 && l2 == l3) l3 = 0;
1194inline void queryCacheSizes_intel(
int& l1,
int& l2,
int& l3,
int max_std_funcs) {
1195 if (max_std_funcs >= 4)
1196 queryCacheSizes_intel_direct(l1, l2, l3);
1197 else if (max_std_funcs >= 2)
1198 queryCacheSizes_intel_codes(l1, l2, l3);
1203inline void queryCacheSizes_amd(
int& l1,
int& l2,
int& l3) {
1205 abcd[0] = abcd[1] = abcd[2] = abcd[3] = 0;
1208 EIGEN_CPUID(abcd, 0x80000000, 0);
1209 if (
static_cast<numext::uint32_t
>(abcd[0]) >=
static_cast<numext::uint32_t
>(0x80000006)) {
1210 EIGEN_CPUID(abcd, 0x80000005, 0);
1211 l1 = (abcd[2] >> 24) * 1024;
1212 abcd[0] = abcd[1] = abcd[2] = abcd[3] = 0;
1213 EIGEN_CPUID(abcd, 0x80000006, 0);
1214 l2 = (abcd[2] >> 16) * 1024;
1215 l3 = ((abcd[3] & 0xFFFC000) >> 18) * 512 * 1024;
1224inline void queryCacheSizes(
int& l1,
int& l2,
int& l3) {
1227 const int GenuineIntel[] = {0x756e6547, 0x49656e69, 0x6c65746e};
1228 const int AuthenticAMD[] = {0x68747541, 0x69746e65, 0x444d4163};
1229 const int AMDisbetter_[] = {0x69444d41, 0x74656273, 0x21726574};
1232 EIGEN_CPUID(abcd, 0x0, 0);
1233 int max_std_funcs = abcd[0];
1234 if (cpuid_is_vendor(abcd, GenuineIntel))
1235 queryCacheSizes_intel(l1, l2, l3, max_std_funcs);
1236 else if (cpuid_is_vendor(abcd, AuthenticAMD) || cpuid_is_vendor(abcd, AMDisbetter_))
1237 queryCacheSizes_amd(l1, l2, l3);
1240 queryCacheSizes_intel(l1, l2, l3, max_std_funcs);
1260inline int queryL1CacheSize() {
1262 queryCacheSizes(l1, l2, l3);
1268inline int queryTopLevelCacheSize() {
1269 int l1, l2(-1), l3(-1);
1270 queryCacheSizes(l1, l2, l3);
1271 return (std::max)(l2, l3);
1278#if EIGEN_COMP_CXXVER >= 20 && defined(__cpp_lib_constexpr_dynamic_alloc) && \
1279 __cpp_lib_constexpr_dynamic_alloc >= 201907L
1280using std::construct_at;
1282template <
class T,
class... Args>
1283EIGEN_DEVICE_FUNC T* construct_at(T* p, Args&&... args) {
1284 return ::new (
const_cast<void*
>(
static_cast<const volatile void*
>(p))) T(std::forward<Args>(args)...);
1293#if EIGEN_COMP_CXXVER >= 17
1294using std::destroy_at;
1297EIGEN_DEVICE_FUNC
void destroy_at(T* p) {
1303#if !defined(EIGEN_DONT_ASSUME_ALIGNED) && __has_feature(memory_sanitizer) && \
1304 (EIGEN_ARCH_ARM || EIGEN_ARCH_ARM64)
1305#define EIGEN_DONT_ASSUME_ALIGNED
1309#if !defined(EIGEN_DONT_ASSUME_ALIGNED) && defined(__cpp_lib_assume_aligned) && (__cpp_lib_assume_aligned >= 201811L)
1310template <std::
size_t N,
typename T>
1311EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC
constexpr T* assume_aligned(T* ptr) {
1312 return std::assume_aligned<N, T>(ptr);
1314#elif !defined(EIGEN_DONT_ASSUME_ALIGNED) && EIGEN_HAS_BUILTIN(__builtin_assume_aligned)
1315template <std::
size_t N,
typename T>
1316EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC T* assume_aligned(T* ptr) {
1317 return static_cast<T*
>(__builtin_assume_aligned(ptr, N));
1320template <std::
size_t N,
typename T>
1321EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC
constexpr T* assume_aligned(T* ptr) {
Namespace containing all symbols from the Eigen library.
Definition B01_Experimental.dox:1
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition Meta.h:82
const int Dynamic
Definition Constants.h:25