22#ifndef EIGEN_MALLOC_ALREADY_ALIGNED
33#if defined(__GLIBC__) && ((__GLIBC__ >= 2 && __GLIBC_MINOR__ >= 8) || __GLIBC__ > 2) && defined(__LP64__) && \
34 !defined(__SANITIZE_ADDRESS__) && (EIGEN_DEFAULT_ALIGN_BYTES == 16)
35#define EIGEN_GLIBC_MALLOC_ALREADY_ALIGNED 1
37#define EIGEN_GLIBC_MALLOC_ALREADY_ALIGNED 0
44#if defined(__FreeBSD__) && !(EIGEN_ARCH_ARM || EIGEN_ARCH_MIPS) && (EIGEN_DEFAULT_ALIGN_BYTES == 16)
45#define EIGEN_FREEBSD_MALLOC_ALREADY_ALIGNED 1
47#define EIGEN_FREEBSD_MALLOC_ALREADY_ALIGNED 0
50#if (EIGEN_OS_MAC && (EIGEN_DEFAULT_ALIGN_BYTES == 16)) || (EIGEN_OS_WIN64 && (EIGEN_DEFAULT_ALIGN_BYTES == 16)) || \
51 EIGEN_GLIBC_MALLOC_ALREADY_ALIGNED || EIGEN_FREEBSD_MALLOC_ALREADY_ALIGNED
52#define EIGEN_MALLOC_ALREADY_ALIGNED 1
54#define EIGEN_MALLOC_ALREADY_ALIGNED 0
59#ifndef EIGEN_MALLOC_CHECK_THREAD_LOCAL
64#ifndef EIGEN_AVOID_THREAD_LOCAL
66#if ((EIGEN_COMP_GNUC) || __has_feature(cxx_thread_local) || EIGEN_COMP_MSVC >= 1900) && \
67 !defined(EIGEN_GPU_COMPILE_PHASE)
68#define EIGEN_MALLOC_CHECK_THREAD_LOCAL thread_local
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)");
94#elif defined EIGEN_RUNTIME_NO_MALLOC
95EIGEN_DEVICE_FUNC
inline bool is_malloc_allowed_impl(
bool update,
bool new_value =
false) {
96 EIGEN_MALLOC_CHECK_THREAD_LOCAL
static bool value =
true;
97 if (update == 1) value = new_value;
100EIGEN_DEVICE_FUNC
inline bool is_malloc_allowed() {
return is_malloc_allowed_impl(
false); }
101EIGEN_DEVICE_FUNC
inline bool set_is_malloc_allowed(
bool new_value) {
return is_malloc_allowed_impl(
true, new_value); }
102EIGEN_DEVICE_FUNC
inline void check_that_malloc_is_allowed() {
103 eigen_assert(is_malloc_allowed() &&
104 "heap allocation is forbidden (EIGEN_RUNTIME_NO_MALLOC is defined and g_is_malloc_allowed is false)");
107EIGEN_DEVICE_FUNC
inline void check_that_malloc_is_allowed() {}
110EIGEN_DEVICE_FUNC
inline void throw_std_bad_alloc() {
111#ifdef EIGEN_EXCEPTIONS
112 throw std::bad_alloc();
114 std::size_t huge =
static_cast<std::size_t
>(-1);
115#if defined(EIGEN_HIPCC)
127 void* unused = ::operator
new(huge);
128 EIGEN_UNUSED_VARIABLE(unused);
142EIGEN_DEVICE_FUNC
inline void* handmade_aligned_malloc(std::size_t size,
143 std::size_t alignment = EIGEN_DEFAULT_ALIGN_BYTES) {
144 eigen_assert(alignment >=
sizeof(
void*) && alignment <= 256 && (alignment & (alignment - 1)) == 0 &&
145 "Alignment must be at least sizeof(void*), less than or equal to 256, and a power of 2");
147 check_that_malloc_is_allowed();
148 EIGEN_USING_STD(malloc)
149 void* original = malloc(size + alignment);
150 if (original ==
nullptr)
return nullptr;
151 std::size_t offset = alignment - (
reinterpret_cast<std::size_t
>(original) & (alignment - 1));
152 void* aligned =
static_cast<void*
>(
static_cast<uint8_t*
>(original) + offset);
154 *(
static_cast<uint8_t*
>(aligned) - 1) =
static_cast<uint8_t
>(offset - 1);
159EIGEN_DEVICE_FUNC
inline void handmade_aligned_free(
void* ptr) {
160 if (ptr !=
nullptr) {
161 std::size_t offset =
static_cast<std::size_t
>(*(
static_cast<uint8_t*
>(ptr) - 1)) + 1;
162 void* original =
static_cast<void*
>(
static_cast<uint8_t*
>(ptr) - offset);
164 check_that_malloc_is_allowed();
165 EIGEN_USING_STD(free)
175EIGEN_DEVICE_FUNC
inline void* handmade_aligned_realloc(
void* ptr, std::size_t new_size, std::size_t old_size,
176 std::size_t alignment = EIGEN_DEFAULT_ALIGN_BYTES) {
177 if (ptr ==
nullptr)
return handmade_aligned_malloc(new_size, alignment);
178 std::size_t old_offset =
static_cast<std::size_t
>(*(
static_cast<uint8_t*
>(ptr) - 1)) + 1;
179 void* old_original =
static_cast<uint8_t*
>(ptr) - old_offset;
181 check_that_malloc_is_allowed();
182 EIGEN_USING_STD(realloc)
183 void* original = realloc(old_original, new_size + alignment);
184 if (original ==
nullptr)
return nullptr;
185 if (original == old_original)
return ptr;
186 std::size_t offset = alignment - (
reinterpret_cast<std::size_t
>(original) & (alignment - 1));
187 void* aligned =
static_cast<void*
>(
static_cast<uint8_t*
>(original) + offset);
188 if (offset != old_offset) {
189 const void* src =
static_cast<const void*
>(
static_cast<uint8_t*
>(original) + old_offset);
190 std::size_t count = (std::min)(new_size, old_size);
191 std::memmove(aligned, src, count);
194 *(
static_cast<uint8_t*
>(aligned) - 1) =
static_cast<uint8_t
>(offset - 1);
201EIGEN_DEVICE_FUNC
inline void* aligned_malloc(std::size_t size) {
202 if (size == 0)
return nullptr;
205#if (EIGEN_DEFAULT_ALIGN_BYTES == 0) || EIGEN_MALLOC_ALREADY_ALIGNED
207 check_that_malloc_is_allowed();
208 EIGEN_USING_STD(malloc)
209 result = malloc(size);
211#if EIGEN_DEFAULT_ALIGN_BYTES == 16
212 eigen_assert((size < 16 || (std::size_t(result) % 16) == 0) &&
213 "System's malloc returned an unaligned pointer. Compile with EIGEN_MALLOC_ALREADY_ALIGNED=0 to fallback "
214 "to handmade aligned memory allocator.");
217 result = handmade_aligned_malloc(size);
220 if (!result && size) throw_std_bad_alloc();
226EIGEN_DEVICE_FUNC
inline void aligned_free(
void* ptr) {
227#if (EIGEN_DEFAULT_ALIGN_BYTES == 0) || EIGEN_MALLOC_ALREADY_ALIGNED
229 if (ptr !=
nullptr) {
230 check_that_malloc_is_allowed();
231 EIGEN_USING_STD(free)
236 handmade_aligned_free(ptr);
245EIGEN_DEVICE_FUNC
inline void* aligned_realloc(
void* ptr, std::size_t new_size, std::size_t old_size) {
246 if (ptr ==
nullptr)
return aligned_malloc(new_size);
247 if (old_size == new_size)
return ptr;
254#if (EIGEN_DEFAULT_ALIGN_BYTES == 0) || EIGEN_MALLOC_ALREADY_ALIGNED
255 EIGEN_UNUSED_VARIABLE(old_size)
257 check_that_malloc_is_allowed();
258 EIGEN_USING_STD(realloc)
259 result = realloc(ptr, new_size);
261 result = handmade_aligned_realloc(ptr, new_size, old_size);
264 if (!result && new_size) throw_std_bad_alloc();
277EIGEN_DEVICE_FUNC
inline void* conditional_aligned_malloc(std::size_t size) {
278 return aligned_malloc(size);
282EIGEN_DEVICE_FUNC
inline void* conditional_aligned_malloc<false>(std::size_t size) {
283 if (size == 0)
return nullptr;
285 check_that_malloc_is_allowed();
286 EIGEN_USING_STD(malloc)
287 void* result = malloc(size);
289 if (!result && size) throw_std_bad_alloc();
295EIGEN_DEVICE_FUNC
inline void conditional_aligned_free(
void* ptr) {
300EIGEN_DEVICE_FUNC
inline void conditional_aligned_free<false>(
void* ptr) {
301 if (ptr !=
nullptr) {
302 check_that_malloc_is_allowed();
303 EIGEN_USING_STD(free)
309EIGEN_DEVICE_FUNC
inline void* conditional_aligned_realloc(
void* ptr, std::size_t new_size, std::size_t old_size) {
310 return aligned_realloc(ptr, new_size, old_size);
314EIGEN_DEVICE_FUNC
inline void* conditional_aligned_realloc<false>(
void* ptr, std::size_t new_size,
315 std::size_t old_size) {
316 if (ptr ==
nullptr)
return conditional_aligned_malloc<false>(new_size);
317 if (old_size == new_size)
return ptr;
319 conditional_aligned_free<false>(ptr);
323 check_that_malloc_is_allowed();
324 EIGEN_USING_STD(realloc)
325 return realloc(ptr, new_size);
336EIGEN_DEVICE_FUNC
inline void destruct_elements_of_array(T* ptr, std::size_t size) {
339 while (size) ptr[--size].~T();
346EIGEN_DEVICE_FUNC
inline T* default_construct_elements_of_array(T* ptr, std::size_t size) {
349 for (i = 0; i < size; ++i) ::new (ptr + i) T;
352 destruct_elements_of_array(ptr, i);
362EIGEN_DEVICE_FUNC
inline T* copy_construct_elements_of_array(T* ptr,
const T* src, std::size_t size) {
365 for (i = 0; i < size; ++i) ::new (ptr + i) T(*(src + i));
368 destruct_elements_of_array(ptr, i);
378EIGEN_DEVICE_FUNC
inline T* move_construct_elements_of_array(T* ptr, T* src, std::size_t size) {
381 for (i = 0; i < size; ++i) ::new (ptr + i) T(std::move(*(src + i)));
384 destruct_elements_of_array(ptr, i);
395EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
void check_size_for_overflow(std::size_t size) {
396 constexpr std::size_t max_elements = (std::numeric_limits<std::ptrdiff_t>::max)() /
sizeof(T);
397 if (size > max_elements) throw_std_bad_alloc();
405EIGEN_DEVICE_FUNC
inline T* aligned_new(std::size_t size) {
406 check_size_for_overflow<T>(size);
407 T* result =
static_cast<T*
>(aligned_malloc(
sizeof(T) * size));
408 EIGEN_TRY {
return default_construct_elements_of_array(result, size); }
410 aligned_free(result);
416template <
typename T,
bool Align>
417EIGEN_DEVICE_FUNC
inline T* conditional_aligned_new(std::size_t size) {
418 check_size_for_overflow<T>(size);
419 T* result =
static_cast<T*
>(conditional_aligned_malloc<Align>(
sizeof(T) * size));
420 EIGEN_TRY {
return default_construct_elements_of_array(result, size); }
422 conditional_aligned_free<Align>(result);
432EIGEN_DEVICE_FUNC
inline void aligned_delete(T* ptr, std::size_t size) {
433 destruct_elements_of_array<T>(ptr, size);
440template <
typename T,
bool Align>
441EIGEN_DEVICE_FUNC
inline void conditional_aligned_delete(T* ptr, std::size_t size) {
442 destruct_elements_of_array<T>(ptr, size);
443 conditional_aligned_free<Align>(ptr);
446template <
typename T,
bool Align>
447EIGEN_DEVICE_FUNC
inline T* conditional_aligned_realloc_new(T* pts, std::size_t new_size, std::size_t old_size) {
448 check_size_for_overflow<T>(new_size);
449 check_size_for_overflow<T>(old_size);
455 T* result =
static_cast<T*
>(conditional_aligned_malloc<Align>(
sizeof(T) * new_size));
458 std::size_t copy_size = (std::min)(old_size, new_size);
459 move_construct_elements_of_array(result, pts, copy_size);
462 if (new_size > old_size) {
463 default_construct_elements_of_array(result + copy_size, new_size - old_size);
467 conditional_aligned_delete<T, Align>(pts, old_size);
470 conditional_aligned_free<Align>(result);
477template <
typename T,
bool Align>
478EIGEN_DEVICE_FUNC
inline T* conditional_aligned_new_auto(std::size_t size) {
479 if (size == 0)
return nullptr;
480 check_size_for_overflow<T>(size);
481 T* result =
static_cast<T*
>(conditional_aligned_malloc<Align>(
sizeof(T) * size));
482 if (NumTraits<T>::RequireInitialization) {
483 EIGEN_TRY { default_construct_elements_of_array(result, size); }
485 conditional_aligned_free<Align>(result);
492template <
typename T,
bool Align>
493EIGEN_DEVICE_FUNC
inline T* conditional_aligned_realloc_new_auto(T* pts, std::size_t new_size, std::size_t old_size) {
494 if (NumTraits<T>::RequireInitialization) {
495 return conditional_aligned_realloc_new<T, Align>(pts, new_size, old_size);
498 check_size_for_overflow<T>(new_size);
499 check_size_for_overflow<T>(old_size);
500 return static_cast<T*
>(
501 conditional_aligned_realloc<Align>(
static_cast<void*
>(pts),
sizeof(T) * new_size,
sizeof(T) * old_size));
504template <
typename T,
bool Align>
505EIGEN_DEVICE_FUNC
inline void conditional_aligned_delete_auto(T* ptr, std::size_t size) {
506 if (NumTraits<T>::RequireInitialization) destruct_elements_of_array<T>(ptr, size);
507 conditional_aligned_free<Align>(ptr);
530template <
int Alignment,
typename Scalar,
typename Index>
531EIGEN_DEVICE_FUNC
inline Index first_aligned(
const Scalar* array,
Index size) {
532 const Index ScalarSize =
sizeof(Scalar);
533 const Index AlignmentSize = Alignment / ScalarSize;
534 const Index AlignmentMask = AlignmentSize - 1;
536 if (AlignmentSize <= 1) {
540 }
else if ((std::uintptr_t(array) & (
sizeof(Scalar) - 1)) || (Alignment % ScalarSize) != 0) {
545 Index first = (AlignmentSize - (
Index((std::uintptr_t(array) /
sizeof(Scalar))) & AlignmentMask)) & AlignmentMask;
546 return (first < size) ? first : size;
552template <
typename Scalar,
typename Index>
553EIGEN_DEVICE_FUNC
inline Index first_default_aligned(
const Scalar* array,
Index size) {
554 typedef typename packet_traits<Scalar>::type DefaultPacketType;
555 return first_aligned<unpacket_traits<DefaultPacketType>::alignment>(array, size);
560template <
typename Index>
562 return ((size + base - 1) / base) * base;
567template <
typename T,
bool UseMemcpy>
568struct smart_copy_helper;
571EIGEN_DEVICE_FUNC
void smart_copy(
const T* start,
const T* end, T* target) {
572 smart_copy_helper<T, !NumTraits<T>::RequireInitialization>::run(start, end, target);
576struct smart_copy_helper<T, true> {
577 EIGEN_DEVICE_FUNC
static inline void run(
const T* start,
const T* end, T* target) {
578 std::intptr_t size = std::intptr_t(end) - std::intptr_t(start);
579 if (size == 0)
return;
580 eigen_internal_assert(start != 0 && end != 0 && target != 0);
581 EIGEN_USING_STD(memcpy)
582 memcpy(target, start, size);
587struct smart_copy_helper<T, false> {
588 EIGEN_DEVICE_FUNC
static inline void run(
const T* start,
const T* end, T* target) { std::copy(start, end, target); }
592template <
typename T,
bool UseMemmove>
593struct smart_memmove_helper;
596void smart_memmove(
const T* start,
const T* end, T* target) {
597 smart_memmove_helper<T, !NumTraits<T>::RequireInitialization>::run(start, end, target);
601struct smart_memmove_helper<T, true> {
602 static inline void run(
const T* start,
const T* end, T* target) {
603 std::intptr_t size = std::intptr_t(end) - std::intptr_t(start);
604 if (size == 0)
return;
605 eigen_internal_assert(start != 0 && end != 0 && target != 0);
606 std::memmove(target, start, size);
611struct smart_memmove_helper<T, false> {
612 static inline void run(
const T* start,
const T* end, T* target) {
613 if (std::uintptr_t(target) < std::uintptr_t(start)) {
614 std::copy(start, end, target);
616 std::ptrdiff_t count = (std::ptrdiff_t(end) - std::ptrdiff_t(start)) /
sizeof(T);
617 std::copy_backward(start, end, target + count);
623EIGEN_DEVICE_FUNC T* smart_move(T* start, T* end, T* target) {
624 return std::move(start, end, target);
633#if !defined EIGEN_ALLOCA && !defined EIGEN_GPU_COMPILE_PHASE
634#if EIGEN_OS_LINUX || EIGEN_OS_MAC || (defined alloca)
635#define EIGEN_ALLOCA alloca
637#define EIGEN_ALLOCA _alloca
646#if defined(__clang__) && defined(__thumb__)
653class aligned_stack_memory_handler : noncopyable {
661 EIGEN_DEVICE_FUNC aligned_stack_memory_handler(T* ptr, std::size_t size,
bool dealloc)
662 : m_ptr(ptr), m_size(size), m_deallocate(dealloc) {
663 if (NumTraits<T>::RequireInitialization && m_ptr) Eigen::internal::default_construct_elements_of_array(m_ptr, size);
665 EIGEN_DEVICE_FUNC ~aligned_stack_memory_handler() {
666 if (NumTraits<T>::RequireInitialization && m_ptr) Eigen::internal::destruct_elements_of_array<T>(m_ptr, m_size);
667 if (m_deallocate) Eigen::internal::aligned_free(m_ptr);
678template <
typename Xpr,
int NbEvaluations,
679 bool MapExternalBuffer = nested_eval<Xpr, NbEvaluations>::Evaluate && Xpr::MaxSizeAtCompileTime ==
Dynamic>
680struct local_nested_eval_wrapper {
681 static constexpr bool NeedExternalBuffer =
false;
682 typedef typename Xpr::Scalar Scalar;
683 typedef typename nested_eval<Xpr, NbEvaluations>::type ObjectType;
686 EIGEN_DEVICE_FUNC local_nested_eval_wrapper(
const Xpr& xpr, Scalar* ptr) : object(xpr) {
687 EIGEN_UNUSED_VARIABLE(ptr);
688 eigen_internal_assert(ptr == 0);
692template <
typename Xpr,
int NbEvaluations>
693struct local_nested_eval_wrapper<Xpr, NbEvaluations, true> {
694 static constexpr bool NeedExternalBuffer =
true;
695 typedef typename Xpr::Scalar Scalar;
696 typedef typename plain_object_eval<Xpr>::type PlainObject;
697 typedef Map<PlainObject, EIGEN_DEFAULT_ALIGN_BYTES> ObjectType;
700 EIGEN_DEVICE_FUNC local_nested_eval_wrapper(
const Xpr& xpr, Scalar* ptr)
701 : object(ptr == 0 ? reinterpret_cast<Scalar*>(Eigen::internal::aligned_malloc(sizeof(Scalar) * xpr.size())) : ptr,
702 xpr.rows(), xpr.cols()),
703 m_deallocate(ptr == 0) {
704 if (NumTraits<Scalar>::RequireInitialization &&
object.data())
705 Eigen::internal::default_construct_elements_of_array(
object.data(),
object.size());
709 EIGEN_DEVICE_FUNC ~local_nested_eval_wrapper() {
710 if (NumTraits<Scalar>::RequireInitialization &&
object.data())
711 Eigen::internal::destruct_elements_of_array(
object.data(),
object.size());
712 if (m_deallocate) Eigen::internal::aligned_free(
object.data());
722class scoped_array : noncopyable {
726 explicit scoped_array(std::ptrdiff_t size) { m_ptr =
new T[size]; }
727 ~scoped_array() {
delete[] m_ptr; }
728 T& operator[](std::ptrdiff_t i) {
return m_ptr[i]; }
729 const T& operator[](std::ptrdiff_t i)
const {
return m_ptr[i]; }
730 T*& ptr() {
return m_ptr; }
731 const T* ptr()
const {
return m_ptr; }
732 operator const T*()
const {
return m_ptr; }
736void swap(scoped_array<T>& a, scoped_array<T>& b) {
737 std::swap(a.ptr(), b.ptr());
765#if defined(EIGEN_ALLOCA) && !defined(EIGEN_NO_ALLOCA)
767#if EIGEN_DEFAULT_ALIGN_BYTES > 0
771#if ((EIGEN_COMP_GNUC || EIGEN_COMP_CLANG) && !EIGEN_COMP_NVHPC)
772#define EIGEN_ALIGNED_ALLOCA(SIZE) __builtin_alloca_with_align(SIZE, CHAR_BIT* EIGEN_DEFAULT_ALIGN_BYTES)
774EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void* eigen_aligned_alloca_helper(
void* ptr) {
775 constexpr std::uintptr_t mask = EIGEN_DEFAULT_ALIGN_BYTES - 1;
776 std::uintptr_t ptr_int = std::uintptr_t(ptr);
777 std::uintptr_t aligned_ptr_int = (ptr_int + mask) & ~mask;
778 std::uintptr_t offset = aligned_ptr_int - ptr_int;
779 return static_cast<void*
>(
static_cast<uint8_t*
>(ptr) + offset);
781#define EIGEN_ALIGNED_ALLOCA(SIZE) eigen_aligned_alloca_helper(EIGEN_ALLOCA(SIZE + EIGEN_DEFAULT_ALIGN_BYTES - 1))
785#define EIGEN_ALIGNED_ALLOCA(SIZE) EIGEN_ALLOCA(SIZE)
788#define ei_declare_aligned_stack_constructed_variable(TYPE, NAME, SIZE, BUFFER) \
789 Eigen::internal::check_size_for_overflow<TYPE>(SIZE); \
790 TYPE* NAME = (BUFFER) != 0 ? (BUFFER) \
791 : reinterpret_cast<TYPE*>((sizeof(TYPE) * (SIZE) <= EIGEN_STACK_ALLOCATION_LIMIT) \
792 ? EIGEN_ALIGNED_ALLOCA(sizeof(TYPE) * (SIZE)) \
793 : Eigen::internal::aligned_malloc(sizeof(TYPE) * (SIZE))); \
794 Eigen::internal::aligned_stack_memory_handler<TYPE> EIGEN_CAT(NAME, _stack_memory_destructor)( \
795 (BUFFER) == 0 ? NAME : 0, SIZE, sizeof(TYPE) * (SIZE) > EIGEN_STACK_ALLOCATION_LIMIT)
797#define ei_declare_local_nested_eval(XPR_T, XPR, N, NAME) \
798 Eigen::internal::local_nested_eval_wrapper<XPR_T, N> EIGEN_CAT(NAME, _wrapper)( \
799 XPR, reinterpret_cast<typename XPR_T::Scalar*>( \
800 ((Eigen::internal::local_nested_eval_wrapper<XPR_T, N>::NeedExternalBuffer) && \
801 ((sizeof(typename XPR_T::Scalar) * XPR.size()) <= EIGEN_STACK_ALLOCATION_LIMIT)) \
802 ? EIGEN_ALIGNED_ALLOCA(sizeof(typename XPR_T::Scalar) * XPR.size()) \
804 typename Eigen::internal::local_nested_eval_wrapper<XPR_T, N>::ObjectType NAME(EIGEN_CAT(NAME, _wrapper).object)
808#define ei_declare_aligned_stack_constructed_variable(TYPE, NAME, SIZE, BUFFER) \
809 Eigen::internal::check_size_for_overflow<TYPE>(SIZE); \
811 (BUFFER) != 0 ? BUFFER : reinterpret_cast<TYPE*>(Eigen::internal::aligned_malloc(sizeof(TYPE) * (SIZE))); \
812 Eigen::internal::aligned_stack_memory_handler<TYPE> EIGEN_CAT(NAME, _stack_memory_destructor)( \
813 (BUFFER) == 0 ? NAME : 0, SIZE, true)
815#define ei_declare_local_nested_eval(XPR_T, XPR, N, NAME) \
816 typename Eigen::internal::nested_eval<XPR_T, N>::type NAME(XPR)
824#if EIGEN_HAS_CXX17_OVERALIGN
828#define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_NOTHROW(NeedsToAlign)
829#define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(NeedsToAlign)
830#define EIGEN_MAKE_ALIGNED_OPERATOR_NEW
831#define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF_VECTORIZABLE_FIXED_SIZE(Scalar, Size)
836#if EIGEN_MAX_ALIGN_BYTES != 0 && !defined(EIGEN_HIP_DEVICE_COMPILE)
837#define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_NOTHROW(NeedsToAlign) \
838 EIGEN_DEVICE_FUNC void* operator new(std::size_t size, const std::nothrow_t&) noexcept { \
839 EIGEN_TRY { return Eigen::internal::conditional_aligned_malloc<NeedsToAlign>(size); } \
840 EIGEN_CATCH(...) { return 0; } \
842#define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(NeedsToAlign) \
843 EIGEN_DEVICE_FUNC void* operator new(std::size_t size) { \
844 return Eigen::internal::conditional_aligned_malloc<NeedsToAlign>(size); \
846 EIGEN_DEVICE_FUNC void* operator new[](std::size_t size) { \
847 return Eigen::internal::conditional_aligned_malloc<NeedsToAlign>(size); \
849 EIGEN_DEVICE_FUNC void operator delete(void* ptr) noexcept { \
850 Eigen::internal::conditional_aligned_free<NeedsToAlign>(ptr); \
852 EIGEN_DEVICE_FUNC void operator delete[](void* ptr) noexcept { \
853 Eigen::internal::conditional_aligned_free<NeedsToAlign>(ptr); \
855 EIGEN_DEVICE_FUNC void operator delete(void* ptr, std::size_t ) noexcept { \
856 Eigen::internal::conditional_aligned_free<NeedsToAlign>(ptr); \
858 EIGEN_DEVICE_FUNC void operator delete[](void* ptr, std::size_t ) noexcept { \
859 Eigen::internal::conditional_aligned_free<NeedsToAlign>(ptr); \
864 EIGEN_DEVICE_FUNC static void* operator new(std::size_t size, void* ptr) { return ::operator new(size, ptr); } \
865 EIGEN_DEVICE_FUNC static void* operator new[](std::size_t size, void* ptr) { return ::operator new[](size, ptr); } \
866 EIGEN_DEVICE_FUNC void operator delete(void* memory, void* ptr) noexcept { return ::operator delete(memory, ptr); } \
867 EIGEN_DEVICE_FUNC void operator delete[](void* memory, void* ptr) noexcept { \
868 return ::operator delete[](memory, ptr); \
871 EIGEN_MAKE_ALIGNED_OPERATOR_NEW_NOTHROW(NeedsToAlign) \
872 EIGEN_DEVICE_FUNC void operator delete(void* ptr, const std::nothrow_t&) noexcept { \
873 Eigen::internal::conditional_aligned_free<NeedsToAlign>(ptr); \
875 typedef void eigen_aligned_operator_new_marker_type;
877#define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(NeedsToAlign)
880#define EIGEN_MAKE_ALIGNED_OPERATOR_NEW EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(true)
881#define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF_VECTORIZABLE_FIXED_SIZE(Scalar, Size) \
882 EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF( \
883 bool(((Size) != Eigen::Dynamic) && \
884 (((EIGEN_MAX_ALIGN_BYTES >= 16) && ((sizeof(Scalar) * (Size)) % (EIGEN_MAX_ALIGN_BYTES) == 0)) || \
885 ((EIGEN_MAX_ALIGN_BYTES >= 32) && ((sizeof(Scalar) * (Size)) % (EIGEN_MAX_ALIGN_BYTES / 2) == 0)) || \
886 ((EIGEN_MAX_ALIGN_BYTES >= 64) && ((sizeof(Scalar) * (Size)) % (EIGEN_MAX_ALIGN_BYTES / 4) == 0)))))
917class aligned_allocator {
919 typedef std::size_t size_type;
920 typedef std::ptrdiff_t difference_type;
922 typedef const T* const_pointer;
923 typedef T& reference;
924 typedef const T& const_reference;
925 typedef T value_type;
929 typedef aligned_allocator<U> other;
932 aligned_allocator() =
default;
934 aligned_allocator(
const aligned_allocator&) =
default;
937 aligned_allocator(
const aligned_allocator<U>&) {}
940 constexpr bool operator==(
const aligned_allocator<U>&)
const noexcept {
944 constexpr bool operator!=(
const aligned_allocator<U>&)
const noexcept {
948#if EIGEN_COMP_GNUC_STRICT && EIGEN_GNUC_STRICT_AT_LEAST(7, 0, 0)
952 size_type max_size()
const {
return (std::numeric_limits<std::ptrdiff_t>::max)() /
sizeof(T); }
955 pointer allocate(size_type num,
const void* = 0) {
956 internal::check_size_for_overflow<T>(num);
957 return static_cast<pointer
>(internal::aligned_malloc(num *
sizeof(T)));
960 void deallocate(pointer p, size_type ) { internal::aligned_free(p); }
965#if !defined(EIGEN_NO_CPUID)
966#if EIGEN_COMP_GNUC && EIGEN_ARCH_i386_OR_x86_64
967#if defined(__PIC__) && EIGEN_ARCH_i386
969#define EIGEN_CPUID(abcd, func, id) \
970 __asm__ __volatile__("xchgl %%ebx, %k1;cpuid; xchgl %%ebx,%k1" \
971 : "=a"(abcd[0]), "=&r"(abcd[1]), "=c"(abcd[2]), "=d"(abcd[3]) \
972 : "a"(func), "c"(id));
973#elif defined(__PIC__) && EIGEN_ARCH_x86_64
977#define EIGEN_CPUID(abcd, func, id) \
978 __asm__ __volatile__("xchg{q}\t{%%}rbx, %q1; cpuid; xchg{q}\t{%%}rbx, %q1" \
979 : "=a"(abcd[0]), "=&r"(abcd[1]), "=c"(abcd[2]), "=d"(abcd[3]) \
980 : "0"(func), "2"(id));
983#define EIGEN_CPUID(abcd, func, id) \
984 __asm__ __volatile__("cpuid" : "=a"(abcd[0]), "=b"(abcd[1]), "=c"(abcd[2]), "=d"(abcd[3]) : "0"(func), "2"(id));
987#if EIGEN_ARCH_i386_OR_x86_64
988#define EIGEN_CPUID(abcd, func, id) __cpuidex((int*)abcd, func, id)
997inline bool cpuid_is_vendor(
int abcd[4],
const int vendor[3]) {
998 return abcd[1] == vendor[0] && abcd[3] == vendor[1] && abcd[2] == vendor[2];
1001inline void queryCacheSizes_intel_direct(
int& l1,
int& l2,
int& l3) {
1007 abcd[0] = abcd[1] = abcd[2] = abcd[3] = 0;
1008 EIGEN_CPUID(abcd, 0x4, cache_id);
1009 cache_type = (abcd[0] & 0x0F) >> 0;
1010 if (cache_type == 1 || cache_type == 3)
1012 int cache_level = (abcd[0] & 0xE0) >> 5;
1013 int ways = (abcd[1] & 0xFFC00000) >> 22;
1014 int partitions = (abcd[1] & 0x003FF000) >> 12;
1015 int line_size = (abcd[1] & 0x00000FFF) >> 0;
1016 int sets = (abcd[2]);
1018 int cache_size = (ways + 1) * (partitions + 1) * (line_size + 1) * (sets + 1);
1020 switch (cache_level) {
1035 }
while (cache_type > 0 && cache_id < 16);
1038inline void queryCacheSizes_intel_codes(
int& l1,
int& l2,
int& l3) {
1040 abcd[0] = abcd[1] = abcd[2] = abcd[3] = 0;
1042 EIGEN_CPUID(abcd, 0x00000002, 0);
1043 unsigned char* bytes =
reinterpret_cast<unsigned char*
>(abcd) + 2;
1044 bool check_for_p2_core2 =
false;
1045 for (
int i = 0; i < 14; ++i) {
1144 check_for_p2_core2 =
true;
1228 if (check_for_p2_core2 && l2 == l3) l3 = 0;
1234inline void queryCacheSizes_intel(
int& l1,
int& l2,
int& l3,
int max_std_funcs) {
1235 if (max_std_funcs >= 4)
1236 queryCacheSizes_intel_direct(l1, l2, l3);
1237 else if (max_std_funcs >= 2)
1238 queryCacheSizes_intel_codes(l1, l2, l3);
1243inline void queryCacheSizes_amd(
int& l1,
int& l2,
int& l3) {
1245 abcd[0] = abcd[1] = abcd[2] = abcd[3] = 0;
1248 EIGEN_CPUID(abcd, 0x80000000, 0);
1249 if (
static_cast<numext::uint32_t
>(abcd[0]) >=
static_cast<numext::uint32_t
>(0x80000006)) {
1250 EIGEN_CPUID(abcd, 0x80000005, 0);
1251 l1 = (abcd[2] >> 24) * 1024;
1252 abcd[0] = abcd[1] = abcd[2] = abcd[3] = 0;
1253 EIGEN_CPUID(abcd, 0x80000006, 0);
1254 l2 = (abcd[2] >> 16) * 1024;
1255 l3 = ((abcd[3] & 0xFFFC000) >> 18) * 512 * 1024;
1264inline void queryCacheSizes(
int& l1,
int& l2,
int& l3) {
1267 const int GenuineIntel[] = {0x756e6547, 0x49656e69, 0x6c65746e};
1268 const int AuthenticAMD[] = {0x68747541, 0x69746e65, 0x444d4163};
1269 const int AMDisbetter_[] = {0x69444d41, 0x74656273, 0x21726574};
1272 EIGEN_CPUID(abcd, 0x0, 0);
1273 int max_std_funcs = abcd[0];
1274 if (cpuid_is_vendor(abcd, GenuineIntel))
1275 queryCacheSizes_intel(l1, l2, l3, max_std_funcs);
1276 else if (cpuid_is_vendor(abcd, AuthenticAMD) || cpuid_is_vendor(abcd, AMDisbetter_))
1277 queryCacheSizes_amd(l1, l2, l3);
1280 queryCacheSizes_intel(l1, l2, l3, max_std_funcs);
1300inline int queryL1CacheSize() {
1302 queryCacheSizes(l1, l2, l3);
1308inline int queryTopLevelCacheSize() {
1309 int l1, l2(-1), l3(-1);
1310 queryCacheSizes(l1, l2, l3);
1311 return (std::max)(l2, l3);
1318#if EIGEN_COMP_CXXVER >= 20 && defined(__cpp_lib_constexpr_dynamic_alloc) && \
1319 __cpp_lib_constexpr_dynamic_alloc >= 201907L
1320using std::construct_at;
1322template <
class T,
class... Args>
1323EIGEN_DEVICE_FUNC T* construct_at(T* p, Args&&... args) {
1324 return ::new (
const_cast<void*
>(
static_cast<const volatile void*
>(p))) T(std::forward<Args>(args)...);
1333#if EIGEN_COMP_CXXVER >= 17
1334using std::destroy_at;
1337EIGEN_DEVICE_FUNC
void destroy_at(T* p) {
1345#ifndef EIGEN_ASSUME_ALIGNED
1346#if defined(__cpp_lib_assume_aligned) && (__cpp_lib_assume_aligned >= 201811L)
1347#define EIGEN_ASSUME_ALIGNED(PTR, ALIGN_BYTES) \
1348 { PTR = std::assume_aligned<8 * (ALIGN_BYTES)>(PTR); }
1349#elif EIGEN_HAS_BUILTIN(__builtin_assume_aligned)
1350#define EIGEN_ASSUME_ALIGNED(PTR, ALIGN_BYTES) \
1351 { PTR = static_cast<decltype(PTR)>(__builtin_assume_aligned(PTR, (ALIGN_BYTES))); }
1353#define EIGEN_ASSUME_ALIGNED(PTR, ALIGN_BYTES)
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