20 #ifndef EIGEN_MEMORY_H
21 #define EIGEN_MEMORY_H
23 #ifndef EIGEN_MALLOC_ALREADY_ALIGNED
34 #if defined(__GLIBC__) && ((__GLIBC__>=2 && __GLIBC_MINOR__ >= 8) || __GLIBC__>2) \
35 && defined(__LP64__) && ! defined( __SANITIZE_ADDRESS__ ) && (EIGEN_DEFAULT_ALIGN_BYTES == 16)
36 #define EIGEN_GLIBC_MALLOC_ALREADY_ALIGNED 1
38 #define EIGEN_GLIBC_MALLOC_ALREADY_ALIGNED 0
45 #if defined(__FreeBSD__) && !(EIGEN_ARCH_ARM || EIGEN_ARCH_MIPS) && (EIGEN_DEFAULT_ALIGN_BYTES == 16)
46 #define EIGEN_FREEBSD_MALLOC_ALREADY_ALIGNED 1
48 #define EIGEN_FREEBSD_MALLOC_ALREADY_ALIGNED 0
51 #if (EIGEN_OS_MAC && (EIGEN_DEFAULT_ALIGN_BYTES == 16)) \
52 || (EIGEN_OS_WIN64 && (EIGEN_DEFAULT_ALIGN_BYTES == 16)) \
53 || EIGEN_GLIBC_MALLOC_ALREADY_ALIGNED \
54 || EIGEN_FREEBSD_MALLOC_ALREADY_ALIGNED
55 #define EIGEN_MALLOC_ALREADY_ALIGNED 1
57 #define EIGEN_MALLOC_ALREADY_ALIGNED 0
67 inline void throw_std_bad_alloc()
69 #ifdef EIGEN_EXCEPTIONS
70 throw std::bad_alloc();
72 std::size_t huge =
static_cast<std::size_t
>(-1);
73 #if defined(EIGEN_HIPCC)
85 void* unused = ::operator
new(huge);
86 EIGEN_UNUSED_VARIABLE(unused);
100 EIGEN_DEVICE_FUNC
inline void* handmade_aligned_malloc(std::size_t size, std::size_t alignment = EIGEN_DEFAULT_ALIGN_BYTES)
102 eigen_assert(alignment >=
sizeof(
void*) && (alignment & (alignment-1)) == 0 &&
"Alignment must be at least sizeof(void*) and a power of 2");
104 EIGEN_USING_STD(malloc)
105 void *original = malloc(size+alignment);
107 if (original == 0)
return 0;
108 void *aligned =
reinterpret_cast<void*
>((
reinterpret_cast<std::size_t
>(original) & ~(std::size_t(alignment-1))) + alignment);
109 *(
reinterpret_cast<void**
>(aligned) - 1) = original;
114 EIGEN_DEVICE_FUNC
inline void handmade_aligned_free(
void *ptr)
117 EIGEN_USING_STD(free)
118 free(*(
reinterpret_cast<void**
>(ptr) - 1));
127 inline void* handmade_aligned_realloc(
void* ptr, std::size_t size, std::size_t = 0)
129 if (ptr == 0)
return handmade_aligned_malloc(size);
130 void *original = *(
reinterpret_cast<void**
>(ptr) - 1);
131 std::ptrdiff_t previous_offset =
static_cast<char *
>(ptr)-
static_cast<char *
>(original);
132 original = std::realloc(original,size+EIGEN_DEFAULT_ALIGN_BYTES);
133 if (original == 0)
return 0;
134 void *aligned =
reinterpret_cast<void*
>((
reinterpret_cast<std::size_t
>(original) & ~(std::size_t(EIGEN_DEFAULT_ALIGN_BYTES-1))) + EIGEN_DEFAULT_ALIGN_BYTES);
135 void *previous_aligned =
static_cast<char *
>(original)+previous_offset;
136 if(aligned!=previous_aligned)
137 std::memmove(aligned, previous_aligned, size);
139 *(
reinterpret_cast<void**
>(aligned) - 1) = original;
147 #ifdef EIGEN_NO_MALLOC
148 EIGEN_DEVICE_FUNC
inline void check_that_malloc_is_allowed()
150 eigen_assert(
false &&
"heap allocation is forbidden (EIGEN_NO_MALLOC is defined)");
152 #elif defined EIGEN_RUNTIME_NO_MALLOC
153 EIGEN_DEVICE_FUNC
inline bool is_malloc_allowed_impl(
bool update,
bool new_value =
false)
155 static bool value =
true;
160 EIGEN_DEVICE_FUNC
inline bool is_malloc_allowed() {
return is_malloc_allowed_impl(
false); }
161 EIGEN_DEVICE_FUNC
inline bool set_is_malloc_allowed(
bool new_value) {
return is_malloc_allowed_impl(
true, new_value); }
162 EIGEN_DEVICE_FUNC
inline void check_that_malloc_is_allowed()
164 eigen_assert(is_malloc_allowed() &&
"heap allocation is forbidden (EIGEN_RUNTIME_NO_MALLOC is defined and g_is_malloc_allowed is false)");
167 EIGEN_DEVICE_FUNC
inline void check_that_malloc_is_allowed()
174 EIGEN_DEVICE_FUNC
inline void* aligned_malloc(std::size_t size)
176 check_that_malloc_is_allowed();
179 #if (EIGEN_DEFAULT_ALIGN_BYTES==0) || EIGEN_MALLOC_ALREADY_ALIGNED
181 EIGEN_USING_STD(malloc)
182 result = malloc(size);
184 #if EIGEN_DEFAULT_ALIGN_BYTES==16
185 eigen_assert((size<16 || (std::size_t(result)%16)==0) &&
"System's malloc returned an unaligned pointer. Compile with EIGEN_MALLOC_ALREADY_ALIGNED=0 to fallback to handmade aligned memory allocator.");
188 result = handmade_aligned_malloc(size);
192 throw_std_bad_alloc();
198 EIGEN_DEVICE_FUNC
inline void aligned_free(
void *ptr)
200 #if (EIGEN_DEFAULT_ALIGN_BYTES==0) || EIGEN_MALLOC_ALREADY_ALIGNED
202 EIGEN_USING_STD(free)
206 handmade_aligned_free(ptr);
215 inline void* aligned_realloc(
void *ptr, std::size_t new_size, std::size_t old_size)
217 EIGEN_UNUSED_VARIABLE(old_size)
220 #if (EIGEN_DEFAULT_ALIGN_BYTES==0) || EIGEN_MALLOC_ALREADY_ALIGNED
221 result = std::realloc(ptr,new_size);
223 result = handmade_aligned_realloc(ptr,new_size,old_size);
226 if (!result && new_size)
227 throw_std_bad_alloc();
239 template<
bool Align> EIGEN_DEVICE_FUNC
inline void* conditional_aligned_malloc(std::size_t size)
241 return aligned_malloc(size);
244 template<> EIGEN_DEVICE_FUNC
inline void* conditional_aligned_malloc<false>(std::size_t size)
246 check_that_malloc_is_allowed();
248 EIGEN_USING_STD(malloc)
249 void *result = malloc(size);
252 throw_std_bad_alloc();
257 template<
bool Align> EIGEN_DEVICE_FUNC
inline void conditional_aligned_free(
void *ptr)
262 template<> EIGEN_DEVICE_FUNC
inline void conditional_aligned_free<false>(
void *ptr)
264 EIGEN_USING_STD(free)
268 template<
bool Align>
inline void* conditional_aligned_realloc(
void* ptr, std::size_t new_size, std::size_t old_size)
270 return aligned_realloc(ptr, new_size, old_size);
273 template<>
inline void* conditional_aligned_realloc<false>(
void* ptr, std::size_t new_size, std::size_t)
275 return std::realloc(ptr, new_size);
285 template<
typename T> EIGEN_DEVICE_FUNC
inline void destruct_elements_of_array(T *ptr, std::size_t size)
289 while(size) ptr[--size].~T();
295 template<
typename T> EIGEN_DEVICE_FUNC
inline T* construct_elements_of_array(T *ptr, std::size_t size)
300 for (i = 0; i < size; ++i) ::
new (ptr + i) T;
305 destruct_elements_of_array(ptr, i);
316 EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
void check_size_for_overflow(std::size_t size)
318 if(size > std::size_t(-1) /
sizeof(T))
319 throw_std_bad_alloc();
326 template<
typename T> EIGEN_DEVICE_FUNC
inline T* aligned_new(std::size_t size)
328 check_size_for_overflow<T>(size);
329 T *result =
reinterpret_cast<T*
>(aligned_malloc(
sizeof(T)*size));
332 return construct_elements_of_array(result, size);
336 aligned_free(result);
342 template<
typename T,
bool Align> EIGEN_DEVICE_FUNC
inline T* conditional_aligned_new(std::size_t size)
344 check_size_for_overflow<T>(size);
345 T *result =
reinterpret_cast<T*
>(conditional_aligned_malloc<Align>(
sizeof(T)*size));
348 return construct_elements_of_array(result, size);
352 conditional_aligned_free<Align>(result);
361 template<
typename T> EIGEN_DEVICE_FUNC
inline void aligned_delete(T *ptr, std::size_t size)
363 destruct_elements_of_array<T>(ptr, size);
364 Eigen::internal::aligned_free(ptr);
370 template<
typename T,
bool Align> EIGEN_DEVICE_FUNC
inline void conditional_aligned_delete(T *ptr, std::size_t size)
372 destruct_elements_of_array<T>(ptr, size);
373 conditional_aligned_free<Align>(ptr);
376 template<
typename T,
bool Align> EIGEN_DEVICE_FUNC
inline T* conditional_aligned_realloc_new(T* pts, std::size_t new_size, std::size_t old_size)
378 check_size_for_overflow<T>(new_size);
379 check_size_for_overflow<T>(old_size);
380 if(new_size < old_size)
381 destruct_elements_of_array(pts+new_size, old_size-new_size);
382 T *result =
reinterpret_cast<T*
>(conditional_aligned_realloc<Align>(
reinterpret_cast<void*
>(pts),
sizeof(T)*new_size,
sizeof(T)*old_size));
383 if(new_size > old_size)
387 construct_elements_of_array(result+old_size, new_size-old_size);
391 conditional_aligned_free<Align>(result);
399 template<
typename T,
bool Align> EIGEN_DEVICE_FUNC
inline T* conditional_aligned_new_auto(std::size_t size)
403 check_size_for_overflow<T>(size);
404 T *result =
reinterpret_cast<T*
>(conditional_aligned_malloc<Align>(
sizeof(T)*size));
405 if(NumTraits<T>::RequireInitialization)
409 construct_elements_of_array(result, size);
413 conditional_aligned_free<Align>(result);
420 template<
typename T,
bool Align>
inline T* conditional_aligned_realloc_new_auto(T* pts, std::size_t new_size, std::size_t old_size)
422 check_size_for_overflow<T>(new_size);
423 check_size_for_overflow<T>(old_size);
424 if(NumTraits<T>::RequireInitialization && (new_size < old_size))
425 destruct_elements_of_array(pts+new_size, old_size-new_size);
426 T *result =
reinterpret_cast<T*
>(conditional_aligned_realloc<Align>(
reinterpret_cast<void*
>(pts),
sizeof(T)*new_size,
sizeof(T)*old_size));
427 if(NumTraits<T>::RequireInitialization && (new_size > old_size))
431 construct_elements_of_array(result+old_size, new_size-old_size);
435 conditional_aligned_free<Align>(result);
442 template<
typename T,
bool Align> EIGEN_DEVICE_FUNC
inline void conditional_aligned_delete_auto(T *ptr, std::size_t size)
444 if(NumTraits<T>::RequireInitialization)
445 destruct_elements_of_array<T>(ptr, size);
446 conditional_aligned_free<Align>(ptr);
468 template<
int Alignment,
typename Scalar,
typename Index>
469 EIGEN_DEVICE_FUNC
inline Index first_aligned(
const Scalar* array,
Index size)
471 const Index ScalarSize =
sizeof(Scalar);
472 const Index AlignmentSize = Alignment / ScalarSize;
473 const Index AlignmentMask = AlignmentSize-1;
481 else if( (UIntPtr(array) & (
sizeof(Scalar)-1)) || (Alignment%ScalarSize)!=0)
489 Index first = (AlignmentSize - (
Index((UIntPtr(array)/
sizeof(Scalar))) & AlignmentMask)) & AlignmentMask;
490 return (first < size) ? first : size;
496 template<
typename Scalar,
typename Index>
497 EIGEN_DEVICE_FUNC
inline Index first_default_aligned(
const Scalar* array,
Index size)
499 typedef typename packet_traits<Scalar>::type DefaultPacketType;
500 return first_aligned<unpacket_traits<DefaultPacketType>::alignment>(array, size);
505 template<
typename Index>
508 return ((size+base-1)/base)*base;
515 template<
typename T> EIGEN_DEVICE_FUNC
void smart_copy(
const T* start,
const T* end, T* target)
521 EIGEN_DEVICE_FUNC
static inline void run(
const T* start,
const T* end, T* target)
523 IntPtr size = IntPtr(end)-IntPtr(start);
525 eigen_internal_assert(start!=0 && end!=0 && target!=0);
526 EIGEN_USING_STD(memcpy)
527 memcpy(target, start, size);
532 EIGEN_DEVICE_FUNC
static inline void run(
const T* start,
const T* end, T* target)
533 { std::copy(start, end, target); }
539 template<
typename T>
void smart_memmove(
const T* start,
const T* end, T* target)
545 static inline void run(
const T* start,
const T* end, T* target)
547 IntPtr size = IntPtr(end)-IntPtr(start);
549 eigen_internal_assert(start!=0 && end!=0 && target!=0);
550 std::memmove(target, start, size);
555 static inline void run(
const T* start,
const T* end, T* target)
557 if (UIntPtr(target) < UIntPtr(start))
559 std::copy(start, end, target);
563 std::ptrdiff_t count = (std::ptrdiff_t(end)-std::ptrdiff_t(start)) /
sizeof(T);
564 std::copy_backward(start, end, target + count);
576 #if ! defined EIGEN_ALLOCA && ! defined EIGEN_GPU_COMPILE_PHASE
577 #if EIGEN_OS_LINUX || EIGEN_OS_MAC || (defined alloca)
578 #define EIGEN_ALLOCA alloca
579 #elif EIGEN_COMP_MSVC
580 #define EIGEN_ALLOCA _alloca
589 #if defined(__clang__) && defined(__thumb__)
606 : m_ptr(ptr), m_size(size), m_deallocate(dealloc)
609 Eigen::internal::construct_elements_of_array(m_ptr, size);
615 Eigen::internal::destruct_elements_of_array<T>(m_ptr, m_size);
617 Eigen::internal::aligned_free(m_ptr);
627 template<
typename Xpr,
int NbEvaluations,
630 struct local_nested_eval_wrapper
632 static const bool NeedExternalBuffer =
false;
633 typedef typename Xpr::Scalar Scalar;
638 local_nested_eval_wrapper(
const Xpr& xpr, Scalar* ptr) : object(xpr)
640 EIGEN_UNUSED_VARIABLE(ptr);
641 eigen_internal_assert(ptr==0);
645 template<
typename Xpr,
int NbEvaluations>
646 struct local_nested_eval_wrapper<Xpr,NbEvaluations,true>
648 static const bool NeedExternalBuffer =
true;
649 typedef typename Xpr::Scalar Scalar;
650 typedef typename plain_object_eval<Xpr>::type PlainObject;
651 typedef Map<PlainObject,EIGEN_DEFAULT_ALIGN_BYTES> ObjectType;
655 local_nested_eval_wrapper(
const Xpr& xpr, Scalar* ptr)
656 : object(ptr==0 ? reinterpret_cast<Scalar*>(
Eigen::internal::aligned_malloc(sizeof(Scalar)*xpr.size())) : ptr, xpr.rows(), xpr.cols()),
659 if(NumTraits<Scalar>::RequireInitialization &&
object.data())
660 Eigen::internal::construct_elements_of_array(
object.data(),
object.size());
665 ~local_nested_eval_wrapper()
667 if(NumTraits<Scalar>::RequireInitialization &&
object.data())
668 Eigen::internal::destruct_elements_of_array(
object.data(),
object.size());
670 Eigen::internal::aligned_free(
object.data());
677 #endif // EIGEN_ALLOCA
691 T& operator[](std::ptrdiff_t i) {
return m_ptr[i]; }
692 const T& operator[](std::ptrdiff_t i)
const {
return m_ptr[i]; }
693 T* &ptr() {
return m_ptr; }
694 const T* ptr()
const {
return m_ptr; }
695 operator const T*()
const {
return m_ptr; }
700 std::swap(a.ptr(),b.ptr());
732 #if EIGEN_DEFAULT_ALIGN_BYTES>0
735 #define EIGEN_ALIGNED_ALLOCA(SIZE) reinterpret_cast<void*>((internal::UIntPtr(EIGEN_ALLOCA(SIZE+EIGEN_DEFAULT_ALIGN_BYTES-1)) + EIGEN_DEFAULT_ALIGN_BYTES-1) & ~(std::size_t(EIGEN_DEFAULT_ALIGN_BYTES-1)))
737 #define EIGEN_ALIGNED_ALLOCA(SIZE) EIGEN_ALLOCA(SIZE)
740 #define ei_declare_aligned_stack_constructed_variable(TYPE,NAME,SIZE,BUFFER) \
741 Eigen::internal::check_size_for_overflow<TYPE>(SIZE); \
742 TYPE* NAME = (BUFFER)!=0 ? (BUFFER) \
743 : reinterpret_cast<TYPE*>( \
744 (sizeof(TYPE)*SIZE<=EIGEN_STACK_ALLOCATION_LIMIT) ? EIGEN_ALIGNED_ALLOCA(sizeof(TYPE)*SIZE) \
745 : Eigen::internal::aligned_malloc(sizeof(TYPE)*SIZE) ); \
746 Eigen::internal::aligned_stack_memory_handler<TYPE> EIGEN_CAT(NAME,_stack_memory_destructor)((BUFFER)==0 ? NAME : 0,SIZE,sizeof(TYPE)*SIZE>EIGEN_STACK_ALLOCATION_LIMIT)
749 #define ei_declare_local_nested_eval(XPR_T,XPR,N,NAME) \
750 Eigen::internal::local_nested_eval_wrapper<XPR_T,N> EIGEN_CAT(NAME,_wrapper)(XPR, reinterpret_cast<typename XPR_T::Scalar*>( \
751 ( (Eigen::internal::local_nested_eval_wrapper<XPR_T,N>::NeedExternalBuffer) && ((sizeof(typename XPR_T::Scalar)*XPR.size())<=EIGEN_STACK_ALLOCATION_LIMIT) ) \
752 ? EIGEN_ALIGNED_ALLOCA( sizeof(typename XPR_T::Scalar)*XPR.size() ) : 0 ) ) ; \
753 typename Eigen::internal::local_nested_eval_wrapper<XPR_T,N>::ObjectType NAME(EIGEN_CAT(NAME,_wrapper).object)
757 #define ei_declare_aligned_stack_constructed_variable(TYPE,NAME,SIZE,BUFFER) \
758 Eigen::internal::check_size_for_overflow<TYPE>(SIZE); \
759 TYPE* NAME = (BUFFER)!=0 ? BUFFER : reinterpret_cast<TYPE*>(Eigen::internal::aligned_malloc(sizeof(TYPE)*SIZE)); \
760 Eigen::internal::aligned_stack_memory_handler<TYPE> EIGEN_CAT(NAME,_stack_memory_destructor)((BUFFER)==0 ? NAME : 0,SIZE,true)
763 #define ei_declare_local_nested_eval(XPR_T,XPR,N,NAME) typename Eigen::internal::nested_eval<XPR_T,N>::type NAME(XPR)
772 #if EIGEN_HAS_CXX17_OVERALIGN
776 #define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_NOTHROW(NeedsToAlign)
777 #define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(NeedsToAlign)
778 #define EIGEN_MAKE_ALIGNED_OPERATOR_NEW
779 #define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF_VECTORIZABLE_FIXED_SIZE(Scalar,Size)
783 #if EIGEN_MAX_ALIGN_BYTES!=0
784 #define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_NOTHROW(NeedsToAlign) \
785 void* operator new(std::size_t size, const std::nothrow_t&) EIGEN_NO_THROW { \
786 EIGEN_TRY { return Eigen::internal::conditional_aligned_malloc<NeedsToAlign>(size); } \
787 EIGEN_CATCH (...) { return 0; } \
789 #define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(NeedsToAlign) \
790 void *operator new(std::size_t size) { \
791 return Eigen::internal::conditional_aligned_malloc<NeedsToAlign>(size); \
793 void *operator new[](std::size_t size) { \
794 return Eigen::internal::conditional_aligned_malloc<NeedsToAlign>(size); \
796 void operator delete(void * ptr) EIGEN_NO_THROW { Eigen::internal::conditional_aligned_free<NeedsToAlign>(ptr); } \
797 void operator delete[](void * ptr) EIGEN_NO_THROW { Eigen::internal::conditional_aligned_free<NeedsToAlign>(ptr); } \
798 void operator delete(void * ptr, std::size_t ) EIGEN_NO_THROW { Eigen::internal::conditional_aligned_free<NeedsToAlign>(ptr); } \
799 void operator delete[](void * ptr, std::size_t ) EIGEN_NO_THROW { Eigen::internal::conditional_aligned_free<NeedsToAlign>(ptr); } \
803 static void *operator new(std::size_t size, void *ptr) { return ::operator new(size,ptr); } \
804 static void *operator new[](std::size_t size, void* ptr) { return ::operator new[](size,ptr); } \
805 void operator delete(void * memory, void *ptr) EIGEN_NO_THROW { return ::operator delete(memory,ptr); } \
806 void operator delete[](void * memory, void *ptr) EIGEN_NO_THROW { return ::operator delete[](memory,ptr); } \
808 EIGEN_MAKE_ALIGNED_OPERATOR_NEW_NOTHROW(NeedsToAlign) \
809 void operator delete(void *ptr, const std::nothrow_t&) EIGEN_NO_THROW { \
810 Eigen::internal::conditional_aligned_free<NeedsToAlign>(ptr); \
812 typedef void eigen_aligned_operator_new_marker_type;
814 #define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(NeedsToAlign)
817 #define EIGEN_MAKE_ALIGNED_OPERATOR_NEW EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(true)
818 #define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF_VECTORIZABLE_FIXED_SIZE(Scalar,Size) \
819 EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(bool( \
820 ((Size)!=Eigen::Dynamic) && \
821 (((EIGEN_MAX_ALIGN_BYTES>=16) && ((sizeof(Scalar)*(Size))%(EIGEN_MAX_ALIGN_BYTES )==0)) || \
822 ((EIGEN_MAX_ALIGN_BYTES>=32) && ((sizeof(Scalar)*(Size))%(EIGEN_MAX_ALIGN_BYTES/2)==0)) || \
823 ((EIGEN_MAX_ALIGN_BYTES>=64) && ((sizeof(Scalar)*(Size))%(EIGEN_MAX_ALIGN_BYTES/4)==0)) )))
857 typedef std::size_t size_type;
858 typedef std::ptrdiff_t difference_type;
860 typedef const T* const_pointer;
861 typedef T& reference;
862 typedef const T& const_reference;
863 typedef T value_type;
876 aligned_allocator(
const aligned_allocator<U>& other) : std::allocator<T>(other) {}
878 ~aligned_allocator() {}
880 #if EIGEN_COMP_GNUC_STRICT && EIGEN_GNUC_AT_LEAST(7,0)
884 size_type max_size()
const {
885 return (std::numeric_limits<std::ptrdiff_t>::max)()/
sizeof(T);
889 pointer allocate(size_type num,
const void* = 0)
891 internal::check_size_for_overflow<T>(num);
892 return static_cast<pointer
>( internal::aligned_malloc(num *
sizeof(T)) );
895 void deallocate(pointer p, size_type )
897 internal::aligned_free(p);
903 #if !defined(EIGEN_NO_CPUID)
904 # if EIGEN_COMP_GNUC && EIGEN_ARCH_i386_OR_x86_64
905 # if defined(__PIC__) && EIGEN_ARCH_i386
907 # define EIGEN_CPUID(abcd,func,id) \
908 __asm__ __volatile__ ("xchgl %%ebx, %k1;cpuid; xchgl %%ebx,%k1": "=a" (abcd[0]), "=&r" (abcd[1]), "=c" (abcd[2]), "=d" (abcd[3]) : "a" (func), "c" (id));
909 # elif defined(__PIC__) && EIGEN_ARCH_x86_64
912 # define EIGEN_CPUID(abcd,func,id) \
913 __asm__ __volatile__ ("xchg{q}\t{%%}rbx, %q1; cpuid; xchg{q}\t{%%}rbx, %q1": "=a" (abcd[0]), "=&r" (abcd[1]), "=c" (abcd[2]), "=d" (abcd[3]) : "0" (func), "2" (id));
916 # define EIGEN_CPUID(abcd,func,id) \
917 __asm__ __volatile__ ("cpuid": "=a" (abcd[0]), "=b" (abcd[1]), "=c" (abcd[2]), "=d" (abcd[3]) : "0" (func), "2" (id) );
919 # elif EIGEN_COMP_MSVC
920 # if (EIGEN_COMP_MSVC > 1500) && EIGEN_ARCH_i386_OR_x86_64
921 # define EIGEN_CPUID(abcd,func,id) __cpuidex((int*)abcd,func,id)
930 inline bool cpuid_is_vendor(
int abcd[4],
const int vendor[3])
932 return abcd[1]==vendor[0] && abcd[3]==vendor[1] && abcd[2]==vendor[2];
935 inline void queryCacheSizes_intel_direct(
int& l1,
int& l2,
int& l3)
942 abcd[0] = abcd[1] = abcd[2] = abcd[3] = 0;
943 EIGEN_CPUID(abcd,0x4,cache_id);
944 cache_type = (abcd[0] & 0x0F) >> 0;
945 if(cache_type==1||cache_type==3)
947 int cache_level = (abcd[0] & 0xE0) >> 5;
948 int ways = (abcd[1] & 0xFFC00000) >> 22;
949 int partitions = (abcd[1] & 0x003FF000) >> 12;
950 int line_size = (abcd[1] & 0x00000FFF) >> 0;
951 int sets = (abcd[2]);
953 int cache_size = (ways+1) * (partitions+1) * (line_size+1) * (sets+1);
957 case 1: l1 = cache_size;
break;
958 case 2: l2 = cache_size;
break;
959 case 3: l3 = cache_size;
break;
964 }
while(cache_type>0 && cache_id<16);
967 inline void queryCacheSizes_intel_codes(
int& l1,
int& l2,
int& l3)
970 abcd[0] = abcd[1] = abcd[2] = abcd[3] = 0;
972 EIGEN_CPUID(abcd,0x00000002,0);
973 unsigned char * bytes =
reinterpret_cast<unsigned char *
>(abcd)+2;
974 bool check_for_p2_core2 =
false;
975 for(
int i=0; i<14; ++i)
979 case 0x0A: l1 = 8;
break;
980 case 0x0C: l1 = 16;
break;
981 case 0x0E: l1 = 24;
break;
982 case 0x10: l1 = 16;
break;
983 case 0x15: l1 = 16;
break;
984 case 0x2C: l1 = 32;
break;
985 case 0x30: l1 = 32;
break;
986 case 0x60: l1 = 16;
break;
987 case 0x66: l1 = 8;
break;
988 case 0x67: l1 = 16;
break;
989 case 0x68: l1 = 32;
break;
990 case 0x1A: l2 = 96;
break;
991 case 0x22: l3 = 512;
break;
992 case 0x23: l3 = 1024;
break;
993 case 0x25: l3 = 2048;
break;
994 case 0x29: l3 = 4096;
break;
995 case 0x39: l2 = 128;
break;
996 case 0x3A: l2 = 192;
break;
997 case 0x3B: l2 = 128;
break;
998 case 0x3C: l2 = 256;
break;
999 case 0x3D: l2 = 384;
break;
1000 case 0x3E: l2 = 512;
break;
1001 case 0x40: l2 = 0;
break;
1002 case 0x41: l2 = 128;
break;
1003 case 0x42: l2 = 256;
break;
1004 case 0x43: l2 = 512;
break;
1005 case 0x44: l2 = 1024;
break;
1006 case 0x45: l2 = 2048;
break;
1007 case 0x46: l3 = 4096;
break;
1008 case 0x47: l3 = 8192;
break;
1009 case 0x48: l2 = 3072;
break;
1010 case 0x49:
if(l2!=0) l3 = 4096;
else {check_for_p2_core2=
true; l3 = l2 = 4096;}
break;
1011 case 0x4A: l3 = 6144;
break;
1012 case 0x4B: l3 = 8192;
break;
1013 case 0x4C: l3 = 12288;
break;
1014 case 0x4D: l3 = 16384;
break;
1015 case 0x4E: l2 = 6144;
break;
1016 case 0x78: l2 = 1024;
break;
1017 case 0x79: l2 = 128;
break;
1018 case 0x7A: l2 = 256;
break;
1019 case 0x7B: l2 = 512;
break;
1020 case 0x7C: l2 = 1024;
break;
1021 case 0x7D: l2 = 2048;
break;
1022 case 0x7E: l2 = 256;
break;
1023 case 0x7F: l2 = 512;
break;
1024 case 0x80: l2 = 512;
break;
1025 case 0x81: l2 = 128;
break;
1026 case 0x82: l2 = 256;
break;
1027 case 0x83: l2 = 512;
break;
1028 case 0x84: l2 = 1024;
break;
1029 case 0x85: l2 = 2048;
break;
1030 case 0x86: l2 = 512;
break;
1031 case 0x87: l2 = 1024;
break;
1032 case 0x88: l3 = 2048;
break;
1033 case 0x89: l3 = 4096;
break;
1034 case 0x8A: l3 = 8192;
break;
1035 case 0x8D: l3 = 3072;
break;
1040 if(check_for_p2_core2 && l2 == l3)
1047 inline void queryCacheSizes_intel(
int& l1,
int& l2,
int& l3,
int max_std_funcs)
1049 if(max_std_funcs>=4)
1050 queryCacheSizes_intel_direct(l1,l2,l3);
1052 queryCacheSizes_intel_codes(l1,l2,l3);
1055 inline void queryCacheSizes_amd(
int& l1,
int& l2,
int& l3)
1058 abcd[0] = abcd[1] = abcd[2] = abcd[3] = 0;
1059 EIGEN_CPUID(abcd,0x80000005,0);
1060 l1 = (abcd[2] >> 24) * 1024;
1061 abcd[0] = abcd[1] = abcd[2] = abcd[3] = 0;
1062 EIGEN_CPUID(abcd,0x80000006,0);
1063 l2 = (abcd[2] >> 16) * 1024;
1064 l3 = ((abcd[3] & 0xFFFC000) >> 18) * 512 * 1024;
1070 inline void queryCacheSizes(
int& l1,
int& l2,
int& l3)
1074 const int GenuineIntel[] = {0x756e6547, 0x49656e69, 0x6c65746e};
1075 const int AuthenticAMD[] = {0x68747541, 0x69746e65, 0x444d4163};
1076 const int AMDisbetter_[] = {0x69444d41, 0x74656273, 0x21726574};
1079 EIGEN_CPUID(abcd,0x0,0);
1080 int max_std_funcs = abcd[1];
1081 if(cpuid_is_vendor(abcd,GenuineIntel))
1082 queryCacheSizes_intel(l1,l2,l3,max_std_funcs);
1083 else if(cpuid_is_vendor(abcd,AuthenticAMD) || cpuid_is_vendor(abcd,AMDisbetter_))
1084 queryCacheSizes_amd(l1,l2,l3);
1087 queryCacheSizes_intel(l1,l2,l3,max_std_funcs);
1107 inline int queryL1CacheSize()
1110 queryCacheSizes(l1,l2,l3);
1116 inline int queryTopLevelCacheSize()
1118 int l1, l2(-1), l3(-1);
1119 queryCacheSizes(l1,l2,l3);
1120 return (std::max)(l2,l3);
1127 #endif // EIGEN_MEMORY_H