23#ifndef EIGEN_MALLOC_ALREADY_ALIGNED
34#if defined(__GLIBC__) && ((__GLIBC__ >= 2 && __GLIBC_MINOR__ >= 8) || __GLIBC__ > 2) && defined(__LP64__) && \
35 !defined(__SANITIZE_ADDRESS__) && (EIGEN_DEFAULT_ALIGN_BYTES == 16)
36#define EIGEN_GLIBC_MALLOC_ALREADY_ALIGNED 1
38#define EIGEN_GLIBC_MALLOC_ALREADY_ALIGNED 0
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)) || (EIGEN_OS_WIN64 && (EIGEN_DEFAULT_ALIGN_BYTES == 16)) || \
52 EIGEN_GLIBC_MALLOC_ALREADY_ALIGNED || EIGEN_FREEBSD_MALLOC_ALREADY_ALIGNED
53#define EIGEN_MALLOC_ALREADY_ALIGNED 1
55#define EIGEN_MALLOC_ALREADY_ALIGNED 0
60#ifndef EIGEN_MALLOC_CHECK_THREAD_LOCAL
65#ifndef EIGEN_AVOID_THREAD_LOCAL
67#if !defined(EIGEN_GPU_COMPILE_PHASE)
68#define EIGEN_MALLOC_CHECK_THREAD_LOCAL thread_local
70#define EIGEN_MALLOC_CHECK_THREAD_LOCAL
74#define EIGEN_MALLOC_CHECK_THREAD_LOCAL
80#include "../InternalHeaderCheck.h"
91EIGEN_DEVICE_FUNC
inline void check_that_malloc_is_allowed() {
92 eigen_assert(
false &&
"heap allocation is forbidden (EIGEN_NO_MALLOC is defined)");
94EIGEN_DEVICE_FUNC
inline void check_that_free_is_allowed() {
95 eigen_assert(
false &&
"heap deallocation is forbidden (EIGEN_NO_MALLOC is defined)");
97#elif defined EIGEN_RUNTIME_NO_MALLOC
98EIGEN_DEVICE_FUNC
inline bool is_malloc_allowed_impl(
bool update,
bool new_value =
false) {
99 EIGEN_MALLOC_CHECK_THREAD_LOCAL
static bool value =
true;
100 if (update == 1) value = new_value;
103EIGEN_DEVICE_FUNC
inline bool is_malloc_allowed() {
return is_malloc_allowed_impl(
false); }
104EIGEN_DEVICE_FUNC
inline bool set_is_malloc_allowed(
bool new_value) {
return is_malloc_allowed_impl(
true, new_value); }
105EIGEN_DEVICE_FUNC
inline void check_that_malloc_is_allowed() {
106 eigen_assert(is_malloc_allowed() &&
107 "heap allocation is forbidden (EIGEN_RUNTIME_NO_MALLOC is defined and set_is_malloc_allowed is false)");
109EIGEN_DEVICE_FUNC
inline bool is_free_allowed_impl(
bool update,
bool new_value =
false) {
110 EIGEN_MALLOC_CHECK_THREAD_LOCAL
static bool value =
true;
111 if (update == 1) value = new_value;
114EIGEN_DEVICE_FUNC
inline bool is_free_allowed() {
return is_free_allowed_impl(
false); }
115EIGEN_DEVICE_FUNC
inline bool set_is_free_allowed(
bool new_value) {
return is_free_allowed_impl(
true, new_value); }
116EIGEN_DEVICE_FUNC
inline void check_that_free_is_allowed() {
117 eigen_assert(is_free_allowed() &&
118 "heap deallocation is forbidden (EIGEN_RUNTIME_NO_MALLOC is defined and set_is_free_allowed is false)");
121EIGEN_DEVICE_FUNC
inline void check_that_malloc_is_allowed() {}
122EIGEN_DEVICE_FUNC
inline void check_that_free_is_allowed() {}
125EIGEN_DEVICE_FUNC
inline void throw_std_bad_alloc() {
126#ifdef EIGEN_EXCEPTIONS
127 throw std::bad_alloc();
129 std::size_t huge =
static_cast<std::size_t
>(-1);
130#if defined(EIGEN_HIPCC)
142 void* unused = ::operator
new(huge);
143 EIGEN_UNUSED_VARIABLE(unused);
157EIGEN_DEVICE_FUNC
inline void* handmade_aligned_malloc(std::size_t size,
158 std::size_t alignment = EIGEN_DEFAULT_ALIGN_BYTES) {
159 eigen_assert(alignment >=
sizeof(
void*) && alignment <= 256 && (alignment & (alignment - 1)) == 0 &&
160 "Alignment must be at least sizeof(void*), less than or equal to 256, and a power of 2");
162 check_that_malloc_is_allowed();
163 EIGEN_USING_STD(malloc)
164 void* original = malloc(size + alignment);
165 if (original ==
nullptr)
return nullptr;
166 std::size_t offset = alignment - (
reinterpret_cast<std::size_t
>(original) & (alignment - 1));
167 void* aligned =
static_cast<void*
>(
static_cast<uint8_t*
>(original) + offset);
169 *(
static_cast<uint8_t*
>(aligned) - 1) =
static_cast<uint8_t
>(offset - 1);
174EIGEN_DEVICE_FUNC
inline void handmade_aligned_free(
void* ptr) {
175 if (ptr !=
nullptr) {
176 std::size_t offset =
static_cast<std::size_t
>(*(
static_cast<uint8_t*
>(ptr) - 1)) + 1;
177 void* original =
static_cast<void*
>(
static_cast<uint8_t*
>(ptr) - offset);
179 check_that_free_is_allowed();
180 EIGEN_USING_STD(free)
190EIGEN_DEVICE_FUNC
inline void* handmade_aligned_realloc(
void* ptr, std::size_t new_size, std::size_t old_size,
191 std::size_t alignment = EIGEN_DEFAULT_ALIGN_BYTES) {
192 if (ptr ==
nullptr)
return handmade_aligned_malloc(new_size, alignment);
193 std::size_t old_offset =
static_cast<std::size_t
>(*(
static_cast<uint8_t*
>(ptr) - 1)) + 1;
194 void* old_original =
static_cast<uint8_t*
>(ptr) - old_offset;
196 check_that_malloc_is_allowed();
197 EIGEN_USING_STD(realloc)
198 void* original = realloc(old_original, new_size + alignment);
199 if (original ==
nullptr)
return nullptr;
200 if (original == old_original)
return ptr;
201 std::size_t offset = alignment - (
reinterpret_cast<std::size_t
>(original) & (alignment - 1));
202 void* aligned =
static_cast<void*
>(
static_cast<uint8_t*
>(original) + offset);
203 if (offset != old_offset) {
204 const void* src =
static_cast<const void*
>(
static_cast<uint8_t*
>(original) + old_offset);
205 std::size_t count = (std::min)(new_size, old_size);
206 std::memmove(aligned, src, count);
209 *(
static_cast<uint8_t*
>(aligned) - 1) =
static_cast<uint8_t
>(offset - 1);
216EIGEN_DEVICE_FUNC
inline void* aligned_malloc(std::size_t size) {
217 if (size == 0)
return nullptr;
220#if (EIGEN_DEFAULT_ALIGN_BYTES == 0) || EIGEN_MALLOC_ALREADY_ALIGNED
222 check_that_malloc_is_allowed();
223 EIGEN_USING_STD(malloc)
224 result = malloc(size);
226#if EIGEN_DEFAULT_ALIGN_BYTES == 16
227 eigen_assert((size < 16 || (std::size_t(result) % 16) == 0) &&
228 "System's malloc returned an unaligned pointer. Compile with EIGEN_MALLOC_ALREADY_ALIGNED=0 to fallback "
229 "to handmade aligned memory allocator.");
232 result = handmade_aligned_malloc(size);
235 if (!result && size) throw_std_bad_alloc();
241EIGEN_DEVICE_FUNC
inline void aligned_free(
void* ptr) {
242#if (EIGEN_DEFAULT_ALIGN_BYTES == 0) || EIGEN_MALLOC_ALREADY_ALIGNED
244 if (ptr !=
nullptr) {
245 check_that_free_is_allowed();
246 EIGEN_USING_STD(free)
251 handmade_aligned_free(ptr);
260EIGEN_DEVICE_FUNC
inline void* aligned_realloc(
void* ptr, std::size_t new_size, std::size_t old_size) {
261 if (ptr ==
nullptr)
return aligned_malloc(new_size);
262 if (old_size == new_size)
return ptr;
269#if (EIGEN_DEFAULT_ALIGN_BYTES == 0) || EIGEN_MALLOC_ALREADY_ALIGNED
270 EIGEN_UNUSED_VARIABLE(old_size);
272 check_that_malloc_is_allowed();
273 EIGEN_USING_STD(realloc)
274 result = realloc(ptr, new_size);
276 result = handmade_aligned_realloc(ptr, new_size, old_size);
279 if (!result && new_size) throw_std_bad_alloc();
292EIGEN_DEVICE_FUNC
inline void* conditional_aligned_malloc(std::size_t size) {
293 return aligned_malloc(size);
297EIGEN_DEVICE_FUNC
inline void* conditional_aligned_malloc<false>(std::size_t size) {
298 if (size == 0)
return nullptr;
300 check_that_malloc_is_allowed();
301 EIGEN_USING_STD(malloc)
302 void* result = malloc(size);
304 if (!result && size) throw_std_bad_alloc();
310EIGEN_DEVICE_FUNC
inline void conditional_aligned_free(
void* ptr) {
315EIGEN_DEVICE_FUNC
inline void conditional_aligned_free<false>(
void* ptr) {
316 if (ptr !=
nullptr) {
317 check_that_free_is_allowed();
318 EIGEN_USING_STD(free)
324EIGEN_DEVICE_FUNC
inline void* conditional_aligned_realloc(
void* ptr, std::size_t new_size, std::size_t old_size) {
325 return aligned_realloc(ptr, new_size, old_size);
329EIGEN_DEVICE_FUNC
inline void* conditional_aligned_realloc<false>(
void* ptr, std::size_t new_size,
330 std::size_t old_size) {
331 if (ptr ==
nullptr)
return conditional_aligned_malloc<false>(new_size);
332 if (old_size == new_size)
return ptr;
334 conditional_aligned_free<false>(ptr);
338 check_that_malloc_is_allowed();
339 EIGEN_USING_STD(realloc)
340 return realloc(ptr, new_size);
351EIGEN_DEVICE_FUNC
inline void destruct_elements_of_array(T* ptr, std::size_t size) {
354 while (size) ptr[--size].~T();
361EIGEN_DEVICE_FUNC
inline T* default_construct_elements_of_array(T* ptr, std::size_t size) {
364 for (i = 0; i < size; ++i) ::new (ptr + i) T;
367 destruct_elements_of_array(ptr, i);
377EIGEN_DEVICE_FUNC
inline T* copy_construct_elements_of_array(T* ptr,
const T* src, std::size_t size) {
380 for (i = 0; i < size; ++i) ::new (ptr + i) T(*(src + i));
383 destruct_elements_of_array(ptr, i);
393EIGEN_DEVICE_FUNC
inline T* move_construct_elements_of_array(T* ptr, T* src, std::size_t size) {
396 for (i = 0; i < size; ++i) ::new (ptr + i) T(std::move(*(src + i)));
399 destruct_elements_of_array(ptr, i);
410EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
void check_size_for_overflow(std::size_t size) {
411 constexpr std::size_t max_elements = (std::numeric_limits<std::ptrdiff_t>::max)() /
sizeof(T);
412 if (size > max_elements) throw_std_bad_alloc();
420EIGEN_DEVICE_FUNC
inline T* aligned_new(std::size_t size) {
421 check_size_for_overflow<T>(size);
422 T* result =
static_cast<T*
>(aligned_malloc(
sizeof(T) * size));
423 EIGEN_TRY { default_construct_elements_of_array(result, size); }
425 aligned_free(result);
431template <
typename T,
bool Align>
432EIGEN_DEVICE_FUNC
inline T* conditional_aligned_new(std::size_t size) {
433 check_size_for_overflow<T>(size);
434 T* result =
static_cast<T*
>(conditional_aligned_malloc<Align>(
sizeof(T) * size));
435 EIGEN_TRY { default_construct_elements_of_array(result, size); }
437 conditional_aligned_free<Align>(result);
447EIGEN_DEVICE_FUNC
inline void aligned_delete(T* ptr, std::size_t size) {
448 destruct_elements_of_array<T>(ptr, size);
455template <
typename T,
bool Align>
456EIGEN_DEVICE_FUNC
inline void conditional_aligned_delete(T* ptr, std::size_t size) {
457 destruct_elements_of_array<T>(ptr, size);
458 conditional_aligned_free<Align>(ptr);
461template <
typename T,
bool Align>
462EIGEN_DEVICE_FUNC
inline T* conditional_aligned_realloc_new(T* pts, std::size_t new_size, std::size_t old_size) {
463 check_size_for_overflow<T>(new_size);
464 check_size_for_overflow<T>(old_size);
470 T* result =
static_cast<T*
>(conditional_aligned_malloc<Align>(
sizeof(T) * new_size));
473 std::size_t copy_size = (std::min)(old_size, new_size);
474 move_construct_elements_of_array(result, pts, copy_size);
477 if (new_size > old_size) {
478 default_construct_elements_of_array(result + copy_size, new_size - old_size);
482 conditional_aligned_delete<T, Align>(pts, old_size);
485 conditional_aligned_free<Align>(result);
492template <
typename T,
bool Align>
493EIGEN_DEVICE_FUNC
inline T* conditional_aligned_new_auto(std::size_t size) {
494 if (size == 0)
return nullptr;
495 check_size_for_overflow<T>(size);
496 T* result =
static_cast<T*
>(conditional_aligned_malloc<Align>(
sizeof(T) * size));
497 EIGEN_IF_CONSTEXPR (NumTraits<T>::RequireInitialization) {
498 EIGEN_TRY { default_construct_elements_of_array(result, size); }
500 conditional_aligned_free<Align>(result);
507template <
typename T,
bool Align>
508EIGEN_DEVICE_FUNC
inline T* conditional_aligned_realloc_new_auto(T* pts, std::size_t new_size, std::size_t old_size) {
509 EIGEN_IF_CONSTEXPR (NumTraits<T>::RequireInitialization) {
510 return conditional_aligned_realloc_new<T, Align>(pts, new_size, old_size);
513 check_size_for_overflow<T>(new_size);
514 check_size_for_overflow<T>(old_size);
515 return static_cast<T*
>(
516 conditional_aligned_realloc<Align>(
static_cast<void*
>(pts),
sizeof(T) * new_size,
sizeof(T) * old_size));
519template <
typename T,
bool Align>
520EIGEN_DEVICE_FUNC
inline void conditional_aligned_delete_auto(T* ptr, std::size_t size) {
521 EIGEN_IF_CONSTEXPR (NumTraits<T>::RequireInitialization) destruct_elements_of_array<T>(ptr, size);
522 conditional_aligned_free<Align>(ptr);
545template <
int Alignment,
typename Scalar,
typename Index>
546EIGEN_DEVICE_FUNC
inline Index first_aligned(
const Scalar* array, Index size) {
547 constexpr Index ScalarSize =
sizeof(Scalar);
548 constexpr Index AlignmentSize = Alignment / ScalarSize;
549 constexpr Index AlignmentMask = AlignmentSize - 1;
551 EIGEN_IF_CONSTEXPR (AlignmentSize <= 1) {
555 }
else EIGEN_IF_CONSTEXPR ((Alignment % ScalarSize) != 0) {
559 }
else if (std::uintptr_t(array) & (
sizeof(Scalar) - 1)) {
563 Index first = (AlignmentSize - (Index((std::uintptr_t(array) /
sizeof(Scalar))) & AlignmentMask)) & AlignmentMask;
564 return (first < size) ? first : size;
570template <
typename Scalar,
typename Index>
571EIGEN_DEVICE_FUNC
inline Index first_default_aligned(
const Scalar* array, Index size) {
572 using DefaultPacketType =
typename packet_traits<Scalar>::type;
573 return first_aligned<unpacket_traits<DefaultPacketType>::alignment>(array, size);
578template <
typename Index>
579inline Index first_multiple(Index size, Index base) {
580 return ((size + base - 1) / base) * base;
585template <
typename T,
bool UseMemcpy>
586struct smart_copy_helper;
589EIGEN_DEVICE_FUNC
void smart_copy(
const T* start,
const T* end, T* target) {
590 smart_copy_helper<T, !NumTraits<T>::RequireInitialization>::run(start, end, target);
594struct smart_copy_helper<T, true> {
595 EIGEN_DEVICE_FUNC
static inline void run(
const T* start,
const T* end, T* target) {
596 std::ptrdiff_t count = end - start;
597 if (count <= 0)
return;
598 eigen_internal_assert(start != 0 && end != 0 && target != 0);
599 EIGEN_USING_STD(memcpy)
600 memcpy(target, start,
static_cast<std::size_t
>(count) *
sizeof(T));
605struct smart_copy_helper<T, false> {
606 EIGEN_DEVICE_FUNC
static inline void run(
const T* start,
const T* end, T* target) { std::copy(start, end, target); }
610template <
typename T,
bool UseMemmove>
611struct smart_memmove_helper;
614void smart_memmove(
const T* start,
const T* end, T* target) {
615 smart_memmove_helper<T, !NumTraits<T>::RequireInitialization>::run(start, end, target);
619struct smart_memmove_helper<T, true> {
620 static inline void run(
const T* start,
const T* end, T* target) {
621 std::ptrdiff_t count = end - start;
622 if (count <= 0)
return;
623 eigen_internal_assert(start != 0 && end != 0 && target != 0);
624 std::memmove(target, start,
static_cast<std::size_t
>(count) *
sizeof(T));
629struct smart_memmove_helper<T, false> {
630 static inline void run(
const T* start,
const T* end, T* target) {
631 if (std::uintptr_t(target) < std::uintptr_t(start)) {
632 std::copy(start, end, target);
634 std::ptrdiff_t count = (std::ptrdiff_t(end) - std::ptrdiff_t(start)) /
sizeof(T);
635 std::copy_backward(start, end, target + count);
646#if !defined EIGEN_ALLOCA && !defined EIGEN_GPU_COMPILE_PHASE
647#if EIGEN_OS_LINUX || EIGEN_OS_MAC || (defined alloca)
648#define EIGEN_ALLOCA alloca
650#define EIGEN_ALLOCA _alloca
659#if defined(__clang__) && defined(__thumb__)
665#ifndef EIGEN_STACK_ALIGN_BYTES
666#if defined(EIGEN_VECTORIZE_SME) && EIGEN_DEFAULT_ALIGN_BYTES < 64
667#define EIGEN_STACK_ALIGN_BYTES 64
669#define EIGEN_STACK_ALIGN_BYTES EIGEN_DEFAULT_ALIGN_BYTES
674EIGEN_DEVICE_FUNC
inline void* scratch_malloc(std::size_t size) {
675#if EIGEN_STACK_ALIGN_BYTES > EIGEN_DEFAULT_ALIGN_BYTES
676 void* result = handmade_aligned_malloc(size, EIGEN_STACK_ALIGN_BYTES);
677 if (!result && size) throw_std_bad_alloc();
680 return aligned_malloc(size);
683EIGEN_DEVICE_FUNC
inline void scratch_free(
void* ptr) {
684#if EIGEN_STACK_ALIGN_BYTES > EIGEN_DEFAULT_ALIGN_BYTES
685 handmade_aligned_free(ptr);
693EIGEN_DEVICE_FUNC
inline T* scratch_new(std::size_t size) {
694 check_size_for_overflow<T>(size);
695 T* result =
static_cast<T*
>(scratch_malloc(
sizeof(T) * size));
696 EIGEN_TRY { default_construct_elements_of_array(result, size); }
698 scratch_free(result);
706EIGEN_DEVICE_FUNC
inline void scratch_delete(T* ptr, std::size_t size) {
707 destruct_elements_of_array<T>(ptr, size);
714class aligned_stack_memory_handler {
716 aligned_stack_memory_handler(
const aligned_stack_memory_handler&) =
delete;
717 aligned_stack_memory_handler& operator=(
const aligned_stack_memory_handler&) =
delete;
725 EIGEN_DEVICE_FUNC aligned_stack_memory_handler(T* ptr, std::size_t size,
bool dealloc)
726 : m_ptr(ptr), m_size(size), m_deallocate(dealloc) {
727 EIGEN_IF_CONSTEXPR (NumTraits<T>::RequireInitialization) {
728 if (m_ptr) Eigen::internal::default_construct_elements_of_array(m_ptr, size);
731 EIGEN_DEVICE_FUNC ~aligned_stack_memory_handler() {
732 EIGEN_IF_CONSTEXPR (NumTraits<T>::RequireInitialization) {
733 if (m_ptr) Eigen::internal::destruct_elements_of_array<T>(m_ptr, m_size);
735 if (m_deallocate) Eigen::internal::scratch_free(m_ptr);
746template <
typename Xpr,
int NbEvaluations,
747 bool MapExternalBuffer = nested_eval<Xpr, NbEvaluations>::Evaluate && Xpr::MaxSizeAtCompileTime == Dynamic>
748struct local_nested_eval_wrapper {
749 static constexpr bool NeedExternalBuffer =
false;
750 using Scalar =
typename Xpr::Scalar;
751 using ObjectType =
typename nested_eval<Xpr, NbEvaluations>::type;
754 EIGEN_DEVICE_FUNC local_nested_eval_wrapper(
const Xpr& xpr, Scalar* ptr) : object(xpr) {
755 EIGEN_UNUSED_VARIABLE(ptr);
756 eigen_internal_assert(ptr == 0);
760template <
typename Xpr,
int NbEvaluations>
761struct local_nested_eval_wrapper<Xpr, NbEvaluations, true> {
762 static constexpr bool NeedExternalBuffer =
true;
763 using Scalar =
typename Xpr::Scalar;
764 using PlainObject =
typename plain_object_eval<Xpr>::type;
765 using ObjectType = Map<PlainObject, EIGEN_DEFAULT_ALIGN_BYTES>;
768 EIGEN_DEVICE_FUNC local_nested_eval_wrapper(
const Xpr& xpr, Scalar* ptr)
769 : object(ptr == 0 ? reinterpret_cast<Scalar*>(Eigen::internal::aligned_malloc(sizeof(Scalar) * xpr.size())) : ptr,
770 xpr.rows(), xpr.cols()),
771 m_deallocate(ptr == 0) {
772 EIGEN_IF_CONSTEXPR (NumTraits<Scalar>::RequireInitialization) {
773 if (
object.data()) Eigen::internal::default_construct_elements_of_array(
object.data(),
object.size());
778 EIGEN_DEVICE_FUNC ~local_nested_eval_wrapper() {
779 EIGEN_IF_CONSTEXPR (NumTraits<Scalar>::RequireInitialization) {
780 if (
object.data()) Eigen::internal::destruct_elements_of_array(
object.data(),
object.size());
782 if (m_deallocate) Eigen::internal::aligned_free(
object.data());
817#if defined(EIGEN_ALLOCA) && !defined(EIGEN_NO_ALLOCA)
819#if EIGEN_STACK_ALIGN_BYTES > 0
823#if ((EIGEN_COMP_GNUC || EIGEN_COMP_CLANG) && !EIGEN_COMP_NVHPC && !EIGEN_COMP_ICC)
824#define EIGEN_ALIGNED_ALLOCA(SIZE) __builtin_alloca_with_align(SIZE, CHAR_BIT* EIGEN_STACK_ALIGN_BYTES)
826EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void* eigen_aligned_alloca_helper(
void* ptr) {
827 constexpr std::uintptr_t mask = EIGEN_STACK_ALIGN_BYTES - 1;
828 std::uintptr_t ptr_int = std::uintptr_t(ptr);
829 std::uintptr_t aligned_ptr_int = (ptr_int + mask) & ~mask;
830 std::uintptr_t offset = aligned_ptr_int - ptr_int;
831 return static_cast<void*
>(
static_cast<uint8_t*
>(ptr) + offset);
833#define EIGEN_ALIGNED_ALLOCA(SIZE) eigen_aligned_alloca_helper(EIGEN_ALLOCA(SIZE + EIGEN_STACK_ALIGN_BYTES - 1))
837#define EIGEN_ALIGNED_ALLOCA(SIZE) EIGEN_ALLOCA(SIZE)
840#define ei_declare_aligned_stack_constructed_variable(TYPE, NAME, SIZE, BUFFER) \
841 Eigen::internal::check_size_for_overflow<TYPE>(SIZE); \
842 TYPE* NAME = (BUFFER) != 0 ? (BUFFER) \
843 : reinterpret_cast<TYPE*>((sizeof(TYPE) * (SIZE) <= EIGEN_STACK_ALLOCATION_LIMIT) \
844 ? EIGEN_ALIGNED_ALLOCA(sizeof(TYPE) * (SIZE)) \
845 : Eigen::internal::scratch_malloc(sizeof(TYPE) * (SIZE))); \
846 Eigen::internal::aligned_stack_memory_handler<TYPE> EIGEN_CAT(NAME, _stack_memory_destructor)( \
847 (BUFFER) == 0 ? NAME : 0, SIZE, sizeof(TYPE) * (SIZE) > EIGEN_STACK_ALLOCATION_LIMIT)
849#define ei_declare_local_nested_eval(XPR_T, XPR, N, NAME) \
850 Eigen::internal::local_nested_eval_wrapper<XPR_T, N> EIGEN_CAT(NAME, _wrapper)( \
851 XPR, reinterpret_cast<typename XPR_T::Scalar*>( \
852 ((Eigen::internal::local_nested_eval_wrapper<XPR_T, N>::NeedExternalBuffer) && \
853 ((sizeof(typename XPR_T::Scalar) * XPR.size()) <= EIGEN_STACK_ALLOCATION_LIMIT)) \
854 ? EIGEN_ALIGNED_ALLOCA(sizeof(typename XPR_T::Scalar) * XPR.size()) \
856 typename Eigen::internal::local_nested_eval_wrapper<XPR_T, N>::ObjectType NAME(EIGEN_CAT(NAME, _wrapper).object)
860#define ei_declare_aligned_stack_constructed_variable(TYPE, NAME, SIZE, BUFFER) \
861 Eigen::internal::check_size_for_overflow<TYPE>(SIZE); \
863 (BUFFER) != 0 ? BUFFER : reinterpret_cast<TYPE*>(Eigen::internal::scratch_malloc(sizeof(TYPE) * (SIZE))); \
864 Eigen::internal::aligned_stack_memory_handler<TYPE> EIGEN_CAT(NAME, _stack_memory_destructor)( \
865 (BUFFER) == 0 ? NAME : 0, SIZE, true)
867#define ei_declare_local_nested_eval(XPR_T, XPR, N, NAME) \
868 typename Eigen::internal::nested_eval<XPR_T, N>::type NAME(XPR)
876#if EIGEN_HAS_CXX17_OVERALIGN
880#define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_NOTHROW(NeedsToAlign)
881#define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(NeedsToAlign)
882#define EIGEN_MAKE_ALIGNED_OPERATOR_NEW
883#define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF_VECTORIZABLE_FIXED_SIZE(Scalar, Size)
888#if EIGEN_MAX_ALIGN_BYTES != 0 && !defined(EIGEN_HIP_DEVICE_COMPILE)
889#define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_NOTHROW(NeedsToAlign) \
890 EIGEN_DEVICE_FUNC void* operator new(std::size_t size, const std::nothrow_t&) noexcept { \
891 EIGEN_TRY { return Eigen::internal::conditional_aligned_malloc<NeedsToAlign>(size); } \
892 EIGEN_CATCH(...) { return 0; } \
894#define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(NeedsToAlign) \
895 EIGEN_DEVICE_FUNC void* operator new(std::size_t size) { \
896 return Eigen::internal::conditional_aligned_malloc<NeedsToAlign>(size); \
898 EIGEN_DEVICE_FUNC void* operator new[](std::size_t size) { \
899 return Eigen::internal::conditional_aligned_malloc<NeedsToAlign>(size); \
901 EIGEN_DEVICE_FUNC void operator delete(void* ptr) noexcept { \
902 Eigen::internal::conditional_aligned_free<NeedsToAlign>(ptr); \
904 EIGEN_DEVICE_FUNC void operator delete[](void* ptr) noexcept { \
905 Eigen::internal::conditional_aligned_free<NeedsToAlign>(ptr); \
907 EIGEN_DEVICE_FUNC void operator delete(void* ptr, std::size_t ) noexcept { \
908 Eigen::internal::conditional_aligned_free<NeedsToAlign>(ptr); \
910 EIGEN_DEVICE_FUNC void operator delete[](void* ptr, std::size_t ) noexcept { \
911 Eigen::internal::conditional_aligned_free<NeedsToAlign>(ptr); \
916 EIGEN_DEVICE_FUNC static void* operator new(std::size_t size, void* ptr) { return ::operator new(size, ptr); } \
917 EIGEN_DEVICE_FUNC static void* operator new[](std::size_t size, void* ptr) { return ::operator new[](size, ptr); } \
918 EIGEN_DEVICE_FUNC void operator delete(void* memory, void* ptr) noexcept { return ::operator delete(memory, ptr); } \
919 EIGEN_DEVICE_FUNC void operator delete[](void* memory, void* ptr) noexcept { \
920 return ::operator delete[](memory, ptr); \
923 EIGEN_MAKE_ALIGNED_OPERATOR_NEW_NOTHROW(NeedsToAlign) \
924 EIGEN_DEVICE_FUNC void operator delete(void* ptr, const std::nothrow_t&) noexcept { \
925 Eigen::internal::conditional_aligned_free<NeedsToAlign>(ptr); \
927 typedef void eigen_aligned_operator_new_marker_type;
929#define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(NeedsToAlign)
932#define EIGEN_MAKE_ALIGNED_OPERATOR_NEW EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(true)
933#define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF_VECTORIZABLE_FIXED_SIZE(Scalar, Size) \
934 EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF( \
935 bool(((Size) != Eigen::Dynamic) && \
936 (((EIGEN_MAX_ALIGN_BYTES >= 16) && ((sizeof(Scalar) * size_t(Size)) % (EIGEN_MAX_ALIGN_BYTES) == 0)) || \
937 ((EIGEN_MAX_ALIGN_BYTES >= 32) && ((sizeof(Scalar) * size_t(Size)) % (EIGEN_MAX_ALIGN_BYTES / 2) == 0)) || \
938 ((EIGEN_MAX_ALIGN_BYTES >= 64) && ((sizeof(Scalar) * size_t(Size)) % (EIGEN_MAX_ALIGN_BYTES / 4) == 0)))))
969class aligned_allocator {
971 using size_type = std::size_t;
972 using difference_type = std::ptrdiff_t;
974 using const_pointer =
const T*;
975 using reference = T&;
976 using const_reference =
const T&;
977 using value_type = T;
981 using other = aligned_allocator<U>;
984 aligned_allocator() =
default;
986 aligned_allocator(
const aligned_allocator&) =
default;
989 aligned_allocator(
const aligned_allocator<U>&) {}
992 constexpr bool operator==(
const aligned_allocator<U>&)
const noexcept {
996 constexpr bool operator!=(
const aligned_allocator<U>&)
const noexcept {
1000#if EIGEN_COMP_GNUC_STRICT && EIGEN_GNUC_STRICT_AT_LEAST(7, 0, 0)
1004 size_type max_size()
const {
return (std::numeric_limits<std::ptrdiff_t>::max)() /
sizeof(T); }
1007 pointer allocate(size_type num,
const void* = 0) {
1008 internal::check_size_for_overflow<T>(num);
1009 return static_cast<pointer
>(internal::aligned_malloc(num *
sizeof(T)));
1012 void deallocate(pointer p, size_type ) { internal::aligned_free(p); }
1017#if EIGEN_OS_LINUX && !defined(EIGEN_NO_CPU_CACHE_SYSFS)
1019#define EIGEN_CPU_CACHE_SYSFS 1
1022#if !defined(EIGEN_NO_CPUID)
1023#if EIGEN_COMP_GNUC && EIGEN_ARCH_i386_OR_x86_64
1024#if defined(__PIC__) && EIGEN_ARCH_i386
1026#define EIGEN_CPUID(abcd, func, id) \
1027 __asm__ __volatile__("xchgl %%ebx, %k1;cpuid; xchgl %%ebx,%k1" \
1028 : "=a"(abcd[0]), "=&r"(abcd[1]), "=c"(abcd[2]), "=d"(abcd[3]) \
1029 : "a"(func), "c"(id));
1030#elif defined(__PIC__) && EIGEN_ARCH_x86_64
1034#define EIGEN_CPUID(abcd, func, id) \
1035 __asm__ __volatile__("xchg{q}\t{%%}rbx, %q1; cpuid; xchg{q}\t{%%}rbx, %q1" \
1036 : "=a"(abcd[0]), "=&r"(abcd[1]), "=c"(abcd[2]), "=d"(abcd[3]) \
1037 : "0"(func), "2"(id));
1040#define EIGEN_CPUID(abcd, func, id) \
1041 __asm__ __volatile__("cpuid" : "=a"(abcd[0]), "=b"(abcd[1]), "=c"(abcd[2]), "=d"(abcd[3]) : "0"(func), "2"(id));
1043#elif EIGEN_COMP_MSVC
1044#if EIGEN_ARCH_i386_OR_x86_64
1045#define EIGEN_CPUID(abcd, func, id) __cpuidex((int*)abcd, func, id)
1054inline bool cpuid_is_vendor(
int abcd[4],
const int vendor[3]) {
1055 return abcd[1] == vendor[0] && abcd[3] == vendor[1] && abcd[2] == vendor[2];
1058inline void queryCacheSizes_intel_direct(std::ptrdiff_t& l1, std::ptrdiff_t& l2, std::ptrdiff_t& l3) {
1064 abcd[0] = abcd[1] = abcd[2] = abcd[3] = 0;
1065 EIGEN_CPUID(abcd, 0x4, cache_id);
1066 cache_type = (abcd[0] & 0x0F) >> 0;
1067 if (cache_type == 1 || cache_type == 3)
1069 int cache_level = (abcd[0] & 0xE0) >> 5;
1070 int ways = (abcd[1] & 0xFFC00000) >> 22;
1071 int partitions = (abcd[1] & 0x003FF000) >> 12;
1072 int line_size = (abcd[1] & 0x00000FFF) >> 0;
1073 int sets = (abcd[2]);
1075 std::ptrdiff_t cache_size =
1076 static_cast<std::ptrdiff_t
>(ways + 1) * (partitions + 1) * (line_size + 1) * (sets + 1);
1078 switch (cache_level) {
1093 }
while (cache_type > 0 && cache_id < 16);
1096inline void queryCacheSizes_intel_codes(std::ptrdiff_t& l1, std::ptrdiff_t& l2, std::ptrdiff_t& l3) {
1098 abcd[0] = abcd[1] = abcd[2] = abcd[3] = 0;
1100 EIGEN_CPUID(abcd, 0x00000002, 0);
1101 unsigned char* bytes =
reinterpret_cast<unsigned char*
>(abcd) + 2;
1102 bool check_for_p2_core2 =
false;
1103 for (
int i = 0; i < 14; ++i) {
1202 check_for_p2_core2 =
true;
1286 if (check_for_p2_core2 && l2 == l3) l3 = 0;
1292inline void queryCacheSizes_intel(std::ptrdiff_t& l1, std::ptrdiff_t& l2, std::ptrdiff_t& l3,
int max_std_funcs) {
1293 if (max_std_funcs >= 4)
1294 queryCacheSizes_intel_direct(l1, l2, l3);
1295 else if (max_std_funcs >= 2)
1296 queryCacheSizes_intel_codes(l1, l2, l3);
1301inline void queryCacheSizes_amd(std::ptrdiff_t& l1, std::ptrdiff_t& l2, std::ptrdiff_t& l3) {
1303 abcd[0] = abcd[1] = abcd[2] = abcd[3] = 0;
1306 EIGEN_CPUID(abcd, 0x80000000, 0);
1307 if (
static_cast<numext::uint32_t
>(abcd[0]) >=
static_cast<numext::uint32_t
>(0x80000006)) {
1308 EIGEN_CPUID(abcd, 0x80000005, 0);
1309 l1 = (abcd[2] >> 24) * 1024;
1310 abcd[0] = abcd[1] = abcd[2] = abcd[3] = 0;
1311 EIGEN_CPUID(abcd, 0x80000006, 0);
1312 l2 = (abcd[2] >> 16) * 1024;
1313 l3 =
static_cast<std::ptrdiff_t
>((abcd[3] & 0xFFFC000) >> 18) * 512 * 1024;
1320#ifdef EIGEN_CPU_CACHE_SYSFS
1323struct CpuCacheTopology {
1324 std::ptrdiff_t l1 = 0;
1325 std::ptrdiff_t l2 = 0;
1326 std::ptrdiff_t l3 = 0;
1327 std::ptrdiff_t l3_per_cpu = 0;
1333inline bool readSysfsLine(
const char* root,
const char* relative,
char (&value)[Size]) {
1335 const int length = std::snprintf(path,
sizeof(path),
"%s/%s", root, relative);
1336 if (length <= 0 || length >=
static_cast<int>(
sizeof(path)))
return false;
1337 const int fd = ::open(path, O_RDONLY | O_CLOEXEC);
1338 if (fd < 0)
return false;
1339 const ssize_t bytes_read = ::read(fd, value, Size - 1);
1341 if (bytes_read <= 0)
return false;
1342 value[bytes_read] =
'\0';
1343 char* newline = std::strchr(value,
'\n');
1344 if (newline !=
nullptr) {
1352inline bool readCpuCacheAttribute(
const char* root,
int cpu,
int index,
const char* name,
char (&value)[Size]) {
1354 const int length = std::snprintf(relative,
sizeof(relative),
"cpu%d/cache/index%d/%s", cpu, index, name);
1355 return length > 0 && length < static_cast<int>(
sizeof(relative)) && readSysfsLine(root, relative, value);
1359inline std::ptrdiff_t parseCpuCacheSize(
const char* text) {
1360 char* suffix =
nullptr;
1361 const std::ptrdiff_t value = std::strtol(text, &suffix, 10);
1362 if (value <= 0)
return 0;
1365 const std::ptrdiff_t multiplier = (*suffix ==
'K' || *suffix ==
'k') ? 1024
1366 : (*suffix ==
'M' || *suffix ==
'm') ? 1024 * 1024
1368 return value * multiplier;
1372inline bool isCpuListTerminator(
const char* text) {
return *text ==
'\0' || *text ==
'\n' || *text ==
'\r'; }
1378template <
typename Visitor>
1379inline bool parseCpuList(
const char* text, Visitor&& visit) {
1380 if (isCpuListTerminator(text))
return false;
1383 const long max_cpu_id = 1 << 16;
1384 const char* cursor = text;
1386 char* end =
nullptr;
1387 const long first = std::strtol(cursor, &end, 10);
1388 if (end == cursor || first < 0)
return false;
1392 last = std::strtol(cursor, &end, 10);
1393 if (end == cursor || last < first)
return false;
1395 if (last >= max_cpu_id)
return false;
1396 visit(
static_cast<int>(first),
static_cast<int>(last));
1405 return isCpuListTerminator(end);
1412inline int parseCpuListCount(
const char* text) {
1414 return parseCpuList(text, [&count](
int first,
int last) { count += last - first + 1; }) ? count : 0;
1424inline CpuCacheTopology readCpuCacheTopologySysfs(
const char* root,
int cpu) {
1425 CpuCacheTopology topology;
1428 for (
int index = 0; index < 16; ++index) {
1433 if (!readCpuCacheAttribute(root, cpu, index,
"level", value))
break;
1434 const long level = std::strtol(value,
nullptr, 10);
1435 std::ptrdiff_t* target =
1436 level == 1 ? &topology.l1 : (level == 2 ? &topology.l2 : (level == 3 ? &topology.l3 :
nullptr));
1437 if (target ==
nullptr || *target > 0)
continue;
1441 if (!readCpuCacheAttribute(root, cpu, index,
"type", value) || value[0] ==
'I')
continue;
1443 if (!readCpuCacheAttribute(root, cpu, index,
"size", value))
continue;
1444 const std::ptrdiff_t size = parseCpuCacheSize(value);
1445 if (size <= 0)
continue;
1448 if (level == 3 && readCpuCacheAttribute(root, cpu, index,
"shared_cpu_list", value) &&
1449 std::strchr(value,
'\n') !=
nullptr) {
1450 const int sharing = parseCpuListCount(value);
1451 if (sharing > 0) topology.l3_per_cpu = size / sharing;
1458inline std::ptrdiff_t smallerReportedCacheSize(std::ptrdiff_t a, std::ptrdiff_t b) {
1459 return a == 0 ? b : (b == 0 ? a : (std::min)(a, b));
1467template <
typename IsAllowed>
1468inline CpuCacheTopology queryCpuCacheTopologySysfs(
const char* root, IsAllowed&& is_allowed) {
1469 CpuCacheTopology topology;
1471 if (!readSysfsLine(root,
"online", online) || std::strchr(online,
'\n') ==
nullptr)
return topology;
1473 if (!parseCpuList(online, [](
int,
int) {}))
return topology;
1474 parseCpuList(online, [&](
int first,
int last) {
1475 for (
int cpu = first; cpu <= last; ++cpu) {
1476 if (!is_allowed(cpu))
continue;
1477 const CpuCacheTopology candidate = readCpuCacheTopologySysfs(root, cpu);
1478 topology.l1 = smallerReportedCacheSize(topology.l1, candidate.l1);
1479 topology.l2 = smallerReportedCacheSize(topology.l2, candidate.l2);
1480 topology.l3 = smallerReportedCacheSize(topology.l3, candidate.l3);
1481 topology.l3_per_cpu = smallerReportedCacheSize(topology.l3_per_cpu, candidate.l3_per_cpu);
1493inline CpuCacheTopology queryCpuCacheTopologySysfs(
const char* root =
"/sys/devices/system/cpu") {
1499 if (sched_getaffinity(0,
sizeof(allowed), &allowed) == 0) {
1500 return queryCpuCacheTopologySysfs(root,
1501 [&allowed](
int cpu) {
return cpu < CPU_SETSIZE && CPU_ISSET(cpu, &allowed); });
1504 return queryCpuCacheTopologySysfs(root, [](
int) {
return true; });
1512inline void queryCacheSizes(std::ptrdiff_t& l1, std::ptrdiff_t& l2, std::ptrdiff_t& l3, std::ptrdiff_t& l3_per_cpu) {
1516 const int GenuineIntel[] = {0x756e6547, 0x49656e69, 0x6c65746e};
1517 const int AuthenticAMD[] = {0x68747541, 0x69746e65, 0x444d4163};
1518 const int AMDisbetter_[] = {0x69444d41, 0x74656273, 0x21726574};
1521 const int HygonGenuine[] = {0x6f677948, 0x6e65476e, 0x656e6975};
1524 EIGEN_CPUID(abcd, 0x0, 0);
1525 int max_std_funcs = abcd[0];
1526 if (cpuid_is_vendor(abcd, GenuineIntel))
1527 queryCacheSizes_intel(l1, l2, l3, max_std_funcs);
1528 else if (cpuid_is_vendor(abcd, AuthenticAMD) || cpuid_is_vendor(abcd, AMDisbetter_) ||
1529 cpuid_is_vendor(abcd, HygonGenuine))
1530 queryCacheSizes_amd(l1, l2, l3);
1533 queryCacheSizes_intel(l1, l2, l3, max_std_funcs);
1556 std::size_t val_size =
sizeof(val);
1558 val_size =
sizeof(val);
1559 if (sysctlbyname(
"hw.perflevel0.l1dcachesize", &val, &val_size,
nullptr, 0) == 0 && val > 0)
1562 val_size =
sizeof(val);
1563 if (sysctlbyname(
"hw.l1dcachesize", &val, &val_size,
nullptr, 0) == 0) l1 = val;
1566 val_size =
sizeof(val);
1567 if (sysctlbyname(
"hw.l2cachesize", &val, &val_size,
nullptr, 0) == 0) l2 = val;
1569 val_size =
sizeof(val);
1570 if (sysctlbyname(
"hw.l3cachesize", &val, &val_size,
nullptr, 0) == 0 && val > 0) l3 = val;
1572#elif EIGEN_OS_UNIX && defined(_SC_LEVEL1_DCACHE_SIZE)
1574 l1 = sysconf(_SC_LEVEL1_DCACHE_SIZE);
1575 l2 = sysconf(_SC_LEVEL2_CACHE_SIZE);
1576 l3 = sysconf(_SC_LEVEL3_CACHE_SIZE);
1580#ifdef EIGEN_CPU_CACHE_SYSFS
1585 const CpuCacheTopology topology = queryCpuCacheTopologySysfs();
1586 if (l1 <= 0) l1 = topology.l1;
1587 if (l2 <= 0) l2 = topology.l2;
1588 if (l3 <= 0) l3 = topology.l3;
1589 l3_per_cpu = topology.l3_per_cpu;
1595inline void queryCacheSizes(std::ptrdiff_t& l1, std::ptrdiff_t& l2, std::ptrdiff_t& l3) {
1596 std::ptrdiff_t l3_per_cpu;
1597 queryCacheSizes(l1, l2, l3, l3_per_cpu);
1602inline std::ptrdiff_t queryL1CacheSize() {
1603 std::ptrdiff_t l1(-1), l2, l3;
1604 queryCacheSizes(l1, l2, l3);
1610inline std::ptrdiff_t queryTopLevelCacheSize() {
1611 std::ptrdiff_t l1, l2(-1), l3(-1);
1612 queryCacheSizes(l1, l2, l3);
1613 return (std::max)(l2, l3);
1620#if !defined(EIGEN_GPU_COMPILE_PHASE) && EIGEN_COMP_CXXVER >= 20 && defined(__cpp_lib_constexpr_dynamic_alloc) && \
1621 __cpp_lib_constexpr_dynamic_alloc >= 201907L
1622using std::construct_at;
1624template <
class T,
class... Args>
1625EIGEN_DEVICE_FUNC T* construct_at(T* p, Args&&... args) {
1626 return ::new (
const_cast<void*
>(
static_cast<const volatile void*
>(p))) T(std::forward<Args>(args)...);
1635#if EIGEN_COMP_CXXVER >= 17
1636using std::destroy_at;
1639EIGEN_DEVICE_FUNC
void destroy_at(T* p) {
1645#if !defined(EIGEN_DONT_ASSUME_ALIGNED) && __has_feature(memory_sanitizer) && (EIGEN_ARCH_ARM || EIGEN_ARCH_ARM64)
1646#define EIGEN_DONT_ASSUME_ALIGNED
1649#if !defined(EIGEN_DONT_ASSUME_ALIGNED) && defined(__cpp_lib_assume_aligned) && (__cpp_lib_assume_aligned >= 201811L)
1650template <std::
size_t N,
typename T>
1651EIGEN_DEVICE_FUNC
constexpr T* assume_aligned(T* ptr) {
1652 return std::assume_aligned<N, T>(ptr);
1654#elif !defined(EIGEN_DONT_ASSUME_ALIGNED) && EIGEN_HAS_BUILTIN(__builtin_assume_aligned)
1655template <std::
size_t N,
typename T>
1656EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC T* assume_aligned(T* ptr) {
1657 return static_cast<T*
>(__builtin_assume_aligned(ptr, N));
1660template <std::
size_t N,
typename T>
1661EIGEN_DEVICE_FUNC
constexpr T* assume_aligned(T* ptr) {