19 #ifndef EIGEN_MEMORY_H 20 #define EIGEN_MEMORY_H 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 73 #else // EIGEN_AVOID_THREAD_LOCAL 74 #define EIGEN_MALLOC_CHECK_THREAD_LOCAL 75 #endif // EIGEN_AVOID_THREAD_LOCAL 80 #include "../InternalHeaderCheck.h" 90 #ifdef EIGEN_NO_MALLOC 91 EIGEN_DEVICE_FUNC
inline void check_that_malloc_is_allowed() {
92 eigen_assert(
false &&
"heap allocation is forbidden (EIGEN_NO_MALLOC is defined)");
94 EIGEN_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 98 EIGEN_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;
103 EIGEN_DEVICE_FUNC
inline bool is_malloc_allowed() {
return is_malloc_allowed_impl(
false); }
104 EIGEN_DEVICE_FUNC
inline bool set_is_malloc_allowed(
bool new_value) {
return is_malloc_allowed_impl(
true, new_value); }
105 EIGEN_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)");
109 EIGEN_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;
114 EIGEN_DEVICE_FUNC
inline bool is_free_allowed() {
return is_free_allowed_impl(
false); }
115 EIGEN_DEVICE_FUNC
inline bool set_is_free_allowed(
bool new_value) {
return is_free_allowed_impl(
true, new_value); }
116 EIGEN_DEVICE_FUNC
inline void check_that_free_is_allowed() {
117 eigen_assert(is_malloc_allowed() &&
118 "heap deallocation is forbidden (EIGEN_RUNTIME_NO_MALLOC is defined and set_is_free_allowed is false)");
121 EIGEN_DEVICE_FUNC
inline void check_that_malloc_is_allowed() {}
122 EIGEN_DEVICE_FUNC
inline void check_that_free_is_allowed() {}
125 EIGEN_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);
157 EIGEN_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);
174 EIGEN_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)
190 EIGEN_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);
216 EIGEN_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();
241 EIGEN_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);
260 EIGEN_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();
291 template <
bool Align>
292 EIGEN_DEVICE_FUNC
inline void* conditional_aligned_malloc(std::size_t size) {
293 return aligned_malloc(size);
297 EIGEN_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();
309 template <
bool Align>
310 EIGEN_DEVICE_FUNC
inline void conditional_aligned_free(
void* ptr) {
315 EIGEN_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)
323 template <
bool Align>
324 EIGEN_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);
329 EIGEN_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);
350 template <
typename T>
351 EIGEN_DEVICE_FUNC
inline void destruct_elements_of_array(T* ptr, std::size_t size) {
354 while (size) ptr[--size].~T();
360 template <
typename T>
361 EIGEN_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);
376 template <
typename T>
377 EIGEN_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);
392 template <
typename T>
393 EIGEN_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);
409 template <
typename T>
410 EIGEN_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();
419 template <
typename T>
420 EIGEN_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 {
return default_construct_elements_of_array(result, size); }
425 aligned_free(result);
431 template <
typename T,
bool Align>
432 EIGEN_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 {
return default_construct_elements_of_array(result, size); }
437 conditional_aligned_free<Align>(result);
446 template <
typename T>
447 EIGEN_DEVICE_FUNC
inline void aligned_delete(T* ptr, std::size_t size) {
448 destruct_elements_of_array<T>(ptr, size);
455 template <
typename T,
bool Align>
456 EIGEN_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);
461 template <
typename T,
bool Align>
462 EIGEN_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);
492 template <
typename T,
bool Align>
493 EIGEN_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 if (NumTraits<T>::RequireInitialization) {
498 EIGEN_TRY { default_construct_elements_of_array(result, size); }
500 conditional_aligned_free<Align>(result);
507 template <
typename T,
bool Align>
508 EIGEN_DEVICE_FUNC
inline T* conditional_aligned_realloc_new_auto(T* pts, std::size_t new_size, std::size_t old_size) {
509 if (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));
519 template <
typename T,
bool Align>
520 EIGEN_DEVICE_FUNC
inline void conditional_aligned_delete_auto(T* ptr, std::size_t size) {
521 if (NumTraits<T>::RequireInitialization) destruct_elements_of_array<T>(ptr, size);
522 conditional_aligned_free<Align>(ptr);
545 template <
int Alignment,
typename Scalar,
typename Index>
546 EIGEN_DEVICE_FUNC
inline Index first_aligned(
const Scalar* array,
Index size) {
547 const Index ScalarSize =
sizeof(Scalar);
548 const Index AlignmentSize = Alignment / ScalarSize;
549 const Index AlignmentMask = AlignmentSize - 1;
551 if (AlignmentSize <= 1) {
555 }
else if ((std::uintptr_t(array) & (
sizeof(Scalar) - 1)) || (Alignment % ScalarSize) != 0) {
560 Index first = (AlignmentSize - (
Index((std::uintptr_t(array) /
sizeof(Scalar))) & AlignmentMask)) & AlignmentMask;
561 return (first < size) ? first : size;
567 template <
typename Scalar,
typename Index>
568 EIGEN_DEVICE_FUNC
inline Index first_default_aligned(
const Scalar* array,
Index size) {
569 typedef typename packet_traits<Scalar>::type DefaultPacketType;
570 return first_aligned<unpacket_traits<DefaultPacketType>::alignment>(array, size);
575 template <
typename Index>
577 return ((size + base - 1) / base) * base;
582 template <
typename T,
bool UseMemcpy>
583 struct smart_copy_helper;
585 template <
typename T>
586 EIGEN_DEVICE_FUNC
void smart_copy(
const T* start,
const T*
end, T* target) {
587 smart_copy_helper<T, !NumTraits<T>::RequireInitialization>::run(start,
end, target);
590 template <
typename T>
591 struct smart_copy_helper<T, true> {
592 EIGEN_DEVICE_FUNC
static inline void run(
const T* start,
const T*
end, T* target) {
593 std::intptr_t size = std::intptr_t(
end) - std::intptr_t(start);
594 if (size == 0)
return;
595 eigen_internal_assert(start != 0 &&
end != 0 && target != 0);
596 EIGEN_USING_STD(memcpy)
597 memcpy(target, start, size);
601 template <
typename T>
602 struct smart_copy_helper<T, false> {
603 EIGEN_DEVICE_FUNC
static inline void run(
const T* start,
const T*
end, T* target) { std::copy(start,
end, target); }
607 template <
typename T,
bool UseMemmove>
608 struct smart_memmove_helper;
610 template <
typename T>
611 void smart_memmove(
const T* start,
const T*
end, T* target) {
612 smart_memmove_helper<T, !NumTraits<T>::RequireInitialization>::run(start,
end, target);
615 template <
typename T>
616 struct smart_memmove_helper<T, true> {
617 static inline void run(
const T* start,
const T*
end, T* target) {
618 std::intptr_t size = std::intptr_t(
end) - std::intptr_t(start);
619 if (size == 0)
return;
620 eigen_internal_assert(start != 0 &&
end != 0 && target != 0);
621 std::memmove(target, start, size);
625 template <
typename T>
626 struct smart_memmove_helper<T, false> {
627 static inline void run(
const T* start,
const T*
end, T* target) {
628 if (std::uintptr_t(target) < std::uintptr_t(start)) {
629 std::copy(start,
end, target);
631 std::ptrdiff_t count = (std::ptrdiff_t(
end) - std::ptrdiff_t(start)) /
sizeof(T);
632 std::copy_backward(start,
end, target + count);
637 template <
typename T>
638 EIGEN_DEVICE_FUNC T* smart_move(T* start, T*
end, T* target) {
639 return std::move(start,
end, target);
648 #if !defined EIGEN_ALLOCA && !defined EIGEN_GPU_COMPILE_PHASE 649 #if EIGEN_OS_LINUX || EIGEN_OS_MAC || (defined alloca) 650 #define EIGEN_ALLOCA alloca 651 #elif EIGEN_COMP_MSVC 652 #define EIGEN_ALLOCA _alloca 661 #if defined(__clang__) && defined(__thumb__) 667 template <
typename T>
668 class aligned_stack_memory_handler : noncopyable {
676 EIGEN_DEVICE_FUNC aligned_stack_memory_handler(T* ptr, std::size_t size,
bool dealloc)
677 : m_ptr(ptr), m_size(size), m_deallocate(dealloc) {
678 if (NumTraits<T>::RequireInitialization && m_ptr) Eigen::internal::default_construct_elements_of_array(m_ptr, size);
680 EIGEN_DEVICE_FUNC ~aligned_stack_memory_handler() {
681 if (NumTraits<T>::RequireInitialization && m_ptr) Eigen::internal::destruct_elements_of_array<T>(m_ptr, m_size);
682 if (m_deallocate) Eigen::internal::aligned_free(m_ptr);
693 template <
typename Xpr,
int NbEvaluations,
694 bool MapExternalBuffer = nested_eval<Xpr, NbEvaluations>::Evaluate && Xpr::MaxSizeAtCompileTime ==
Dynamic>
695 struct local_nested_eval_wrapper {
696 static constexpr
bool NeedExternalBuffer =
false;
697 typedef typename Xpr::Scalar Scalar;
698 typedef typename nested_eval<Xpr, NbEvaluations>::type ObjectType;
701 EIGEN_DEVICE_FUNC local_nested_eval_wrapper(
const Xpr& xpr, Scalar* ptr) : object(xpr) {
702 EIGEN_UNUSED_VARIABLE(ptr);
703 eigen_internal_assert(ptr == 0);
707 template <
typename Xpr,
int NbEvaluations>
708 struct local_nested_eval_wrapper<Xpr, NbEvaluations, true> {
709 static constexpr
bool NeedExternalBuffer =
true;
710 typedef typename Xpr::Scalar Scalar;
711 typedef typename plain_object_eval<Xpr>::type PlainObject;
712 typedef Map<PlainObject, EIGEN_DEFAULT_ALIGN_BYTES> ObjectType;
715 EIGEN_DEVICE_FUNC local_nested_eval_wrapper(
const Xpr& xpr, Scalar* ptr)
716 : object(ptr == 0 ? reinterpret_cast<Scalar*>(
Eigen::internal::aligned_malloc(sizeof(Scalar) * xpr.size())) : ptr,
717 xpr.rows(), xpr.cols()),
718 m_deallocate(ptr == 0) {
719 if (NumTraits<Scalar>::RequireInitialization &&
object.data())
720 Eigen::internal::default_construct_elements_of_array(
object.data(),
object.size());
724 EIGEN_DEVICE_FUNC ~local_nested_eval_wrapper() {
725 if (NumTraits<Scalar>::RequireInitialization &&
object.data())
726 Eigen::internal::destruct_elements_of_array(
object.data(),
object.size());
727 if (m_deallocate) Eigen::internal::aligned_free(
object.data());
734 #endif // EIGEN_ALLOCA 736 template <
typename T>
737 class scoped_array : noncopyable {
741 explicit scoped_array(std::ptrdiff_t size) { m_ptr =
new T[size]; }
742 ~scoped_array() {
delete[] m_ptr; }
743 T& operator[](std::ptrdiff_t i) {
return m_ptr[i]; }
744 const T& operator[](std::ptrdiff_t i)
const {
return m_ptr[i]; }
745 T*& ptr() {
return m_ptr; }
746 const T* ptr()
const {
return m_ptr; }
747 operator const T*()
const {
return m_ptr; }
750 template <
typename T>
751 void swap(scoped_array<T>& a, scoped_array<T>& b) {
752 std::swap(a.ptr(), b.ptr());
780 #if defined(EIGEN_ALLOCA) && !defined(EIGEN_NO_ALLOCA) 782 #if EIGEN_DEFAULT_ALIGN_BYTES > 0 786 #if ((EIGEN_COMP_GNUC || EIGEN_COMP_CLANG) && !EIGEN_COMP_NVHPC) 787 #define EIGEN_ALIGNED_ALLOCA(SIZE) __builtin_alloca_with_align(SIZE, CHAR_BIT* EIGEN_DEFAULT_ALIGN_BYTES) 789 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void* eigen_aligned_alloca_helper(
void* ptr) {
790 constexpr std::uintptr_t mask = EIGEN_DEFAULT_ALIGN_BYTES - 1;
791 std::uintptr_t ptr_int = std::uintptr_t(ptr);
792 std::uintptr_t aligned_ptr_int = (ptr_int + mask) & ~mask;
793 std::uintptr_t offset = aligned_ptr_int - ptr_int;
794 return static_cast<void*
>(
static_cast<uint8_t*
>(ptr) + offset);
796 #define EIGEN_ALIGNED_ALLOCA(SIZE) eigen_aligned_alloca_helper(EIGEN_ALLOCA(SIZE + EIGEN_DEFAULT_ALIGN_BYTES - 1)) 800 #define EIGEN_ALIGNED_ALLOCA(SIZE) EIGEN_ALLOCA(SIZE) 803 #define ei_declare_aligned_stack_constructed_variable(TYPE, NAME, SIZE, BUFFER) \ 804 Eigen::internal::check_size_for_overflow<TYPE>(SIZE); \ 805 TYPE* NAME = (BUFFER) != 0 ? (BUFFER) \ 806 : reinterpret_cast<TYPE*>((sizeof(TYPE) * (SIZE) <= EIGEN_STACK_ALLOCATION_LIMIT) \ 807 ? EIGEN_ALIGNED_ALLOCA(sizeof(TYPE) * (SIZE)) \ 808 : Eigen::internal::aligned_malloc(sizeof(TYPE) * (SIZE))); \ 809 Eigen::internal::aligned_stack_memory_handler<TYPE> EIGEN_CAT(NAME, _stack_memory_destructor)( \ 810 (BUFFER) == 0 ? NAME : 0, SIZE, sizeof(TYPE) * (SIZE) > EIGEN_STACK_ALLOCATION_LIMIT) 812 #define ei_declare_local_nested_eval(XPR_T, XPR, N, NAME) \ 813 Eigen::internal::local_nested_eval_wrapper<XPR_T, N> EIGEN_CAT(NAME, _wrapper)( \ 814 XPR, reinterpret_cast<typename XPR_T::Scalar*>( \ 815 ((Eigen::internal::local_nested_eval_wrapper<XPR_T, N>::NeedExternalBuffer) && \ 816 ((sizeof(typename XPR_T::Scalar) * XPR.size()) <= EIGEN_STACK_ALLOCATION_LIMIT)) \ 817 ? EIGEN_ALIGNED_ALLOCA(sizeof(typename XPR_T::Scalar) * XPR.size()) \ 819 typename Eigen::internal::local_nested_eval_wrapper<XPR_T, N>::ObjectType NAME(EIGEN_CAT(NAME, _wrapper).object) 823 #define ei_declare_aligned_stack_constructed_variable(TYPE, NAME, SIZE, BUFFER) \ 824 Eigen::internal::check_size_for_overflow<TYPE>(SIZE); \ 826 (BUFFER) != 0 ? BUFFER : reinterpret_cast<TYPE*>(Eigen::internal::aligned_malloc(sizeof(TYPE) * (SIZE))); \ 827 Eigen::internal::aligned_stack_memory_handler<TYPE> EIGEN_CAT(NAME, _stack_memory_destructor)( \ 828 (BUFFER) == 0 ? NAME : 0, SIZE, true) 830 #define ei_declare_local_nested_eval(XPR_T, XPR, N, NAME) \ 831 typename Eigen::internal::nested_eval<XPR_T, N>::type NAME(XPR) 839 #if EIGEN_HAS_CXX17_OVERALIGN 843 #define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_NOTHROW(NeedsToAlign) 844 #define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(NeedsToAlign) 845 #define EIGEN_MAKE_ALIGNED_OPERATOR_NEW 846 #define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF_VECTORIZABLE_FIXED_SIZE(Scalar, Size) 851 #if EIGEN_MAX_ALIGN_BYTES != 0 && !defined(EIGEN_HIP_DEVICE_COMPILE) 852 #define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_NOTHROW(NeedsToAlign) \ 853 EIGEN_DEVICE_FUNC void* operator new(std::size_t size, const std::nothrow_t&) noexcept { \ 854 EIGEN_TRY { return Eigen::internal::conditional_aligned_malloc<NeedsToAlign>(size); } \ 855 EIGEN_CATCH(...) { return 0; } \ 857 #define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(NeedsToAlign) \ 858 EIGEN_DEVICE_FUNC void* operator new(std::size_t size) { \ 859 return Eigen::internal::conditional_aligned_malloc<NeedsToAlign>(size); \ 861 EIGEN_DEVICE_FUNC void* operator new[](std::size_t size) { \ 862 return Eigen::internal::conditional_aligned_malloc<NeedsToAlign>(size); \ 864 EIGEN_DEVICE_FUNC void operator delete(void* ptr) noexcept { \ 865 Eigen::internal::conditional_aligned_free<NeedsToAlign>(ptr); \ 867 EIGEN_DEVICE_FUNC void operator delete[](void* ptr) noexcept { \ 868 Eigen::internal::conditional_aligned_free<NeedsToAlign>(ptr); \ 870 EIGEN_DEVICE_FUNC void operator delete(void* ptr, std::size_t ) noexcept { \ 871 Eigen::internal::conditional_aligned_free<NeedsToAlign>(ptr); \ 873 EIGEN_DEVICE_FUNC void operator delete[](void* ptr, std::size_t ) noexcept { \ 874 Eigen::internal::conditional_aligned_free<NeedsToAlign>(ptr); \ 879 EIGEN_DEVICE_FUNC static void* operator new(std::size_t size, void* ptr) { return ::operator new(size, ptr); } \ 880 EIGEN_DEVICE_FUNC static void* operator new[](std::size_t size, void* ptr) { return ::operator new[](size, ptr); } \ 881 EIGEN_DEVICE_FUNC void operator delete(void* memory, void* ptr) noexcept { return ::operator delete(memory, ptr); } \ 882 EIGEN_DEVICE_FUNC void operator delete[](void* memory, void* ptr) noexcept { \ 883 return ::operator delete[](memory, ptr); \ 886 EIGEN_MAKE_ALIGNED_OPERATOR_NEW_NOTHROW(NeedsToAlign) \ 887 EIGEN_DEVICE_FUNC void operator delete(void* ptr, const std::nothrow_t&) noexcept { \ 888 Eigen::internal::conditional_aligned_free<NeedsToAlign>(ptr); \ 890 typedef void eigen_aligned_operator_new_marker_type; 892 #define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(NeedsToAlign) 895 #define EIGEN_MAKE_ALIGNED_OPERATOR_NEW EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(true) 896 #define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF_VECTORIZABLE_FIXED_SIZE(Scalar, Size) \ 897 EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF( \ 898 bool(((Size) != Eigen::Dynamic) && \ 899 (((EIGEN_MAX_ALIGN_BYTES >= 16) && ((sizeof(Scalar) * (Size)) % (EIGEN_MAX_ALIGN_BYTES) == 0)) || \ 900 ((EIGEN_MAX_ALIGN_BYTES >= 32) && ((sizeof(Scalar) * (Size)) % (EIGEN_MAX_ALIGN_BYTES / 2) == 0)) || \ 901 ((EIGEN_MAX_ALIGN_BYTES >= 64) && ((sizeof(Scalar) * (Size)) % (EIGEN_MAX_ALIGN_BYTES / 4) == 0))))) 934 typedef std::size_t size_type;
935 typedef std::ptrdiff_t difference_type;
937 typedef const T* const_pointer;
938 typedef T& reference;
939 typedef const T& const_reference;
940 typedef T value_type;
963 #if EIGEN_COMP_GNUC_STRICT && EIGEN_GNUC_STRICT_AT_LEAST(7, 0, 0) 967 size_type max_size()
const {
return (std::numeric_limits<std::ptrdiff_t>::max)() /
sizeof(T); }
970 pointer allocate(size_type num,
const void* = 0) {
971 internal::check_size_for_overflow<T>(num);
972 return static_cast<pointer
>(internal::aligned_malloc(num *
sizeof(T)));
975 void deallocate(pointer p, size_type ) { internal::aligned_free(p); }
980 #if !defined(EIGEN_NO_CPUID) 981 #if EIGEN_COMP_GNUC && EIGEN_ARCH_i386_OR_x86_64 982 #if defined(__PIC__) && EIGEN_ARCH_i386 984 #define EIGEN_CPUID(abcd, func, id) \ 985 __asm__ __volatile__("xchgl %%ebx, %k1;cpuid; xchgl %%ebx,%k1" \ 986 : "=a"(abcd[0]), "=&r"(abcd[1]), "=c"(abcd[2]), "=d"(abcd[3]) \ 987 : "a"(func), "c"(id)); 988 #elif defined(__PIC__) && EIGEN_ARCH_x86_64 992 #define EIGEN_CPUID(abcd, func, id) \ 993 __asm__ __volatile__("xchg{q}\t{%%}rbx, %q1; cpuid; xchg{q}\t{%%}rbx, %q1" \ 994 : "=a"(abcd[0]), "=&r"(abcd[1]), "=c"(abcd[2]), "=d"(abcd[3]) \ 995 : "0"(func), "2"(id)); 998 #define EIGEN_CPUID(abcd, func, id) \ 999 __asm__ __volatile__("cpuid" : "=a"(abcd[0]), "=b"(abcd[1]), "=c"(abcd[2]), "=d"(abcd[3]) : "0"(func), "2"(id)); 1001 #elif EIGEN_COMP_MSVC 1002 #if EIGEN_ARCH_i386_OR_x86_64 1003 #define EIGEN_CPUID(abcd, func, id) __cpuidex((int*)abcd, func, id) 1008 namespace internal {
1012 inline bool cpuid_is_vendor(
int abcd[4],
const int vendor[3]) {
1013 return abcd[1] == vendor[0] && abcd[3] == vendor[1] && abcd[2] == vendor[2];
1016 inline void queryCacheSizes_intel_direct(
int& l1,
int& l2,
int& l3) {
1022 abcd[0] = abcd[1] = abcd[2] = abcd[3] = 0;
1023 EIGEN_CPUID(abcd, 0x4, cache_id);
1024 cache_type = (abcd[0] & 0x0F) >> 0;
1025 if (cache_type == 1 || cache_type == 3)
1027 int cache_level = (abcd[0] & 0xE0) >> 5;
1028 int ways = (abcd[1] & 0xFFC00000) >> 22;
1029 int partitions = (abcd[1] & 0x003FF000) >> 12;
1030 int line_size = (abcd[1] & 0x00000FFF) >> 0;
1031 int sets = (abcd[2]);
1033 int cache_size = (ways + 1) * (partitions + 1) * (line_size + 1) * (sets + 1);
1035 switch (cache_level) {
1050 }
while (cache_type > 0 && cache_id < 16);
1053 inline void queryCacheSizes_intel_codes(
int& l1,
int& l2,
int& l3) {
1055 abcd[0] = abcd[1] = abcd[2] = abcd[3] = 0;
1057 EIGEN_CPUID(abcd, 0x00000002, 0);
1058 unsigned char* bytes =
reinterpret_cast<unsigned char*
>(abcd) + 2;
1059 bool check_for_p2_core2 =
false;
1060 for (
int i = 0; i < 14; ++i) {
1159 check_for_p2_core2 =
true;
1243 if (check_for_p2_core2 && l2 == l3) l3 = 0;
1249 inline void queryCacheSizes_intel(
int& l1,
int& l2,
int& l3,
int max_std_funcs) {
1250 if (max_std_funcs >= 4)
1251 queryCacheSizes_intel_direct(l1, l2, l3);
1252 else if (max_std_funcs >= 2)
1253 queryCacheSizes_intel_codes(l1, l2, l3);
1258 inline void queryCacheSizes_amd(
int& l1,
int& l2,
int& l3) {
1260 abcd[0] = abcd[1] = abcd[2] = abcd[3] = 0;
1263 EIGEN_CPUID(abcd, 0x80000000, 0);
1264 if (static_cast<numext::uint32_t>(abcd[0]) >= static_cast<numext::uint32_t>(0x80000006)) {
1265 EIGEN_CPUID(abcd, 0x80000005, 0);
1266 l1 = (abcd[2] >> 24) * 1024;
1267 abcd[0] = abcd[1] = abcd[2] = abcd[3] = 0;
1268 EIGEN_CPUID(abcd, 0x80000006, 0);
1269 l2 = (abcd[2] >> 16) * 1024;
1270 l3 = ((abcd[3] & 0xFFFC000) >> 18) * 512 * 1024;
1279 inline void queryCacheSizes(
int& l1,
int& l2,
int& l3) {
1282 const int GenuineIntel[] = {0x756e6547, 0x49656e69, 0x6c65746e};
1283 const int AuthenticAMD[] = {0x68747541, 0x69746e65, 0x444d4163};
1284 const int AMDisbetter_[] = {0x69444d41, 0x74656273, 0x21726574};
1287 EIGEN_CPUID(abcd, 0x0, 0);
1288 int max_std_funcs = abcd[0];
1289 if (cpuid_is_vendor(abcd, GenuineIntel))
1290 queryCacheSizes_intel(l1, l2, l3, max_std_funcs);
1291 else if (cpuid_is_vendor(abcd, AuthenticAMD) || cpuid_is_vendor(abcd, AMDisbetter_))
1292 queryCacheSizes_amd(l1, l2, l3);
1295 queryCacheSizes_intel(l1, l2, l3, max_std_funcs);
1315 inline int queryL1CacheSize() {
1317 queryCacheSizes(l1, l2, l3);
1323 inline int queryTopLevelCacheSize() {
1324 int l1, l2(-1), l3(-1);
1325 queryCacheSizes(l1, l2, l3);
1326 return (std::max)(l2, l3);
1333 #if EIGEN_COMP_CXXVER >= 20 && defined(__cpp_lib_constexpr_dynamic_alloc) && \ 1334 __cpp_lib_constexpr_dynamic_alloc >= 201907L 1335 using std::construct_at;
1337 template <
class T,
class... Args>
1338 EIGEN_DEVICE_FUNC T* construct_at(T* p, Args&&... args) {
1339 return ::new (const_cast<void*>(static_cast<const volatile void*>(p))) T(std::forward<Args>(args)...);
1348 #if EIGEN_COMP_CXXVER >= 17 1349 using std::destroy_at;
1352 EIGEN_DEVICE_FUNC
void destroy_at(T* p) {
1358 #if !defined(EIGEN_DONT_ASSUME_ALIGNED) && __has_feature(memory_sanitizer) && \ 1359 (EIGEN_ARCH_ARM || EIGEN_ARCH_ARM64) 1360 #define EIGEN_DONT_ASSUME_ALIGNED 1364 #if !defined(EIGEN_DONT_ASSUME_ALIGNED) && defined(__cpp_lib_assume_aligned) && (__cpp_lib_assume_aligned >= 201811L) 1365 template <std::
size_t N,
typename T>
1366 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC constexpr T* assume_aligned(T* ptr) {
1367 return std::assume_aligned<N, T>(ptr);
1369 #elif !defined(EIGEN_DONT_ASSUME_ALIGNED) && EIGEN_HAS_BUILTIN(__builtin_assume_aligned) 1370 template <std::
size_t N,
typename T>
1371 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC T* assume_aligned(T* ptr) {
1372 return static_cast<T*
>(__builtin_assume_aligned(ptr, N));
1375 template <std::
size_t N,
typename T>
1376 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC constexpr T* assume_aligned(T* ptr) {
1385 #endif // EIGEN_MEMORY_H static constexpr lastp1_t end
Definition: IndexedViewHelper.h:79
Namespace containing all symbols from the Eigen library.
Definition: B01_Experimental.dox:1
STL compatible allocator to use with types requiring a non-standard alignment.
Definition: Memory.h:932
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:82
std::enable_if_t< std::is_base_of< DenseBase< std::decay_t< DerivedA > >, std::decay_t< DerivedA > >::value &&std::is_base_of< DenseBase< std::decay_t< DerivedB > >, std::decay_t< DerivedB > >::value, void > swap(DerivedA &&a, DerivedB &&b)
Definition: DenseBase.h:667
const int Dynamic
Definition: Constants.h:25