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);
86 inline void* handmade_aligned_malloc(std::size_t size)
88 void *original = std::malloc(size+EIGEN_DEFAULT_ALIGN_BYTES);
89 if (original == 0)
return 0;
90 void *aligned =
reinterpret_cast<void*
>((
reinterpret_cast<std::size_t
>(original) & ~(std::size_t(EIGEN_DEFAULT_ALIGN_BYTES-1))) + EIGEN_DEFAULT_ALIGN_BYTES);
91 *(
reinterpret_cast<void**
>(aligned) - 1) = original;
96 inline void handmade_aligned_free(
void *ptr)
98 if (ptr) std::free(*(reinterpret_cast<void**>(ptr) - 1));
106 inline void* handmade_aligned_realloc(
void* ptr, std::size_t size, std::size_t = 0)
108 if (ptr == 0)
return handmade_aligned_malloc(size);
109 void *original = *(
reinterpret_cast<void**
>(ptr) - 1);
110 std::ptrdiff_t previous_offset =
static_cast<char *
>(ptr)-static_cast<char *>(original);
111 original = std::realloc(original,size+EIGEN_DEFAULT_ALIGN_BYTES);
112 if (original == 0)
return 0;
113 void *aligned =
reinterpret_cast<void*
>((
reinterpret_cast<std::size_t
>(original) & ~(std::size_t(EIGEN_DEFAULT_ALIGN_BYTES-1))) + EIGEN_DEFAULT_ALIGN_BYTES);
114 void *previous_aligned =
static_cast<char *
>(original)+previous_offset;
115 if(aligned!=previous_aligned)
116 std::memmove(aligned, previous_aligned, size);
118 *(
reinterpret_cast<void**
>(aligned) - 1) = original;
126 #ifdef EIGEN_NO_MALLOC 127 EIGEN_DEVICE_FUNC
inline void check_that_malloc_is_allowed()
129 eigen_assert(
false &&
"heap allocation is forbidden (EIGEN_NO_MALLOC is defined)");
131 #elif defined EIGEN_RUNTIME_NO_MALLOC 132 EIGEN_DEVICE_FUNC
inline bool is_malloc_allowed_impl(
bool update,
bool new_value =
false)
134 static bool value =
true;
139 EIGEN_DEVICE_FUNC
inline bool is_malloc_allowed() {
return is_malloc_allowed_impl(
false); }
140 EIGEN_DEVICE_FUNC
inline bool set_is_malloc_allowed(
bool new_value) {
return is_malloc_allowed_impl(
true, new_value); }
141 EIGEN_DEVICE_FUNC
inline void check_that_malloc_is_allowed()
143 eigen_assert(is_malloc_allowed() &&
"heap allocation is forbidden (EIGEN_RUNTIME_NO_MALLOC is defined and g_is_malloc_allowed is false)");
146 EIGEN_DEVICE_FUNC
inline void check_that_malloc_is_allowed()
153 EIGEN_DEVICE_FUNC
inline void* aligned_malloc(
size_t size)
155 check_that_malloc_is_allowed();
158 #if (EIGEN_DEFAULT_ALIGN_BYTES==0) || EIGEN_MALLOC_ALREADY_ALIGNED 159 result = std::malloc(size);
160 #if EIGEN_DEFAULT_ALIGN_BYTES==16 161 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 alignd memory allocator.");
164 result = handmade_aligned_malloc(size);
168 throw_std_bad_alloc();
174 EIGEN_DEVICE_FUNC
inline void aligned_free(
void *ptr)
176 #if (EIGEN_DEFAULT_ALIGN_BYTES==0) || EIGEN_MALLOC_ALREADY_ALIGNED 179 handmade_aligned_free(ptr);
188 inline void* aligned_realloc(
void *ptr,
size_t new_size,
size_t old_size)
190 EIGEN_UNUSED_VARIABLE(old_size);
193 #if (EIGEN_DEFAULT_ALIGN_BYTES==0) || EIGEN_MALLOC_ALREADY_ALIGNED 194 result = std::realloc(ptr,new_size);
196 result = handmade_aligned_realloc(ptr,new_size,old_size);
199 if (!result && new_size)
200 throw_std_bad_alloc();
212 template<
bool Align> EIGEN_DEVICE_FUNC
inline void* conditional_aligned_malloc(
size_t size)
214 return aligned_malloc(size);
217 template<> EIGEN_DEVICE_FUNC
inline void* conditional_aligned_malloc<false>(
size_t size)
219 check_that_malloc_is_allowed();
221 void *result = std::malloc(size);
223 throw_std_bad_alloc();
228 template<
bool Align> EIGEN_DEVICE_FUNC
inline void conditional_aligned_free(
void *ptr)
233 template<> EIGEN_DEVICE_FUNC
inline void conditional_aligned_free<false>(
void *ptr)
238 template<
bool Align>
inline void* conditional_aligned_realloc(
void* ptr,
size_t new_size,
size_t old_size)
240 return aligned_realloc(ptr, new_size, old_size);
243 template<>
inline void* conditional_aligned_realloc<false>(
void* ptr,
size_t new_size, size_t)
245 return std::realloc(ptr, new_size);
255 template<
typename T> EIGEN_DEVICE_FUNC
inline void destruct_elements_of_array(T *ptr,
size_t size)
259 while(size) ptr[--size].~T();
265 template<
typename T> EIGEN_DEVICE_FUNC
inline T* construct_elements_of_array(T *ptr,
size_t size)
270 for (i = 0; i < size; ++i) ::
new (ptr + i) T;
275 destruct_elements_of_array(ptr, i);
285 EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
void check_size_for_overflow(
size_t size)
287 if(size >
size_t(-1) /
sizeof(T))
288 throw_std_bad_alloc();
295 template<
typename T> EIGEN_DEVICE_FUNC
inline T* aligned_new(
size_t size)
297 check_size_for_overflow<T>(size);
298 T *result =
reinterpret_cast<T*
>(aligned_malloc(
sizeof(T)*size));
301 return construct_elements_of_array(result, size);
305 aligned_free(result);
310 template<
typename T,
bool Align> EIGEN_DEVICE_FUNC
inline T* conditional_aligned_new(
size_t size)
312 check_size_for_overflow<T>(size);
313 T *result =
reinterpret_cast<T*
>(conditional_aligned_malloc<Align>(
sizeof(T)*size));
316 return construct_elements_of_array(result, size);
320 conditional_aligned_free<Align>(result);
328 template<
typename T> EIGEN_DEVICE_FUNC
inline void aligned_delete(T *ptr,
size_t size)
330 destruct_elements_of_array<T>(ptr, size);
337 template<
typename T,
bool Align> EIGEN_DEVICE_FUNC
inline void conditional_aligned_delete(T *ptr,
size_t size)
339 destruct_elements_of_array<T>(ptr, size);
340 conditional_aligned_free<Align>(ptr);
343 template<
typename T,
bool Align> EIGEN_DEVICE_FUNC
inline T* conditional_aligned_realloc_new(T* pts,
size_t new_size,
size_t old_size)
345 check_size_for_overflow<T>(new_size);
346 check_size_for_overflow<T>(old_size);
347 if(new_size < old_size)
348 destruct_elements_of_array(pts+new_size, old_size-new_size);
349 T *result =
reinterpret_cast<T*
>(conditional_aligned_realloc<Align>(
reinterpret_cast<void*
>(pts),
sizeof(T)*new_size,
sizeof(T)*old_size));
350 if(new_size > old_size)
354 construct_elements_of_array(result+old_size, new_size-old_size);
358 conditional_aligned_free<Align>(result);
366 template<
typename T,
bool Align> EIGEN_DEVICE_FUNC
inline T* conditional_aligned_new_auto(
size_t size)
370 check_size_for_overflow<T>(size);
371 T *result =
reinterpret_cast<T*
>(conditional_aligned_malloc<Align>(
sizeof(T)*size));
372 if(NumTraits<T>::RequireInitialization)
376 construct_elements_of_array(result, size);
380 conditional_aligned_free<Align>(result);
387 template<
typename T,
bool Align>
inline T* conditional_aligned_realloc_new_auto(T* pts,
size_t new_size,
size_t old_size)
389 check_size_for_overflow<T>(new_size);
390 check_size_for_overflow<T>(old_size);
391 if(NumTraits<T>::RequireInitialization && (new_size < old_size))
392 destruct_elements_of_array(pts+new_size, old_size-new_size);
393 T *result =
reinterpret_cast<T*
>(conditional_aligned_realloc<Align>(
reinterpret_cast<void*
>(pts),
sizeof(T)*new_size,
sizeof(T)*old_size));
394 if(NumTraits<T>::RequireInitialization && (new_size > old_size))
398 construct_elements_of_array(result+old_size, new_size-old_size);
402 conditional_aligned_free<Align>(result);
409 template<
typename T,
bool Align> EIGEN_DEVICE_FUNC
inline void conditional_aligned_delete_auto(T *ptr,
size_t size)
411 if(NumTraits<T>::RequireInitialization)
412 destruct_elements_of_array<T>(ptr, size);
413 conditional_aligned_free<Align>(ptr);
435 template<
int Alignment,
typename Scalar,
typename Index>
436 EIGEN_DEVICE_FUNC
inline Index first_aligned(
const Scalar* array,
Index size)
438 const Index ScalarSize =
sizeof(Scalar);
439 const Index AlignmentSize = Alignment / ScalarSize;
440 const Index AlignmentMask = AlignmentSize-1;
448 else if( (UIntPtr(array) & (
sizeof(Scalar)-1)) || (Alignment%ScalarSize)!=0)
456 Index first = (AlignmentSize - (
Index((UIntPtr(array)/
sizeof(Scalar))) & AlignmentMask)) & AlignmentMask;
457 return (first < size) ? first : size;
463 template<
typename Scalar,
typename Index>
464 EIGEN_DEVICE_FUNC
inline Index first_default_aligned(
const Scalar* array,
Index size)
466 typedef typename packet_traits<Scalar>::type DefaultPacketType;
467 return first_aligned<unpacket_traits<DefaultPacketType>::alignment>(array, size);
472 template<
typename Index>
475 return ((size+base-1)/base)*base;
480 template<
typename T,
bool UseMemcpy>
struct smart_copy_helper;
482 template<
typename T> EIGEN_DEVICE_FUNC
void smart_copy(
const T* start,
const T* end, T* target)
484 smart_copy_helper<T,!NumTraits<T>::RequireInitialization>::run(start, end, target);
487 template<
typename T>
struct smart_copy_helper<T,true> {
488 EIGEN_DEVICE_FUNC
static inline void run(
const T* start,
const T* end, T* target)
490 IntPtr size = IntPtr(end)-IntPtr(start);
492 eigen_internal_assert(start!=0 && end!=0 && target!=0);
493 memcpy(target, start, size);
497 template<
typename T>
struct smart_copy_helper<T,false> {
498 EIGEN_DEVICE_FUNC
static inline void run(
const T* start,
const T* end, T* target)
499 { std::copy(start, end, target); }
503 template<
typename T,
bool UseMemmove>
struct smart_memmove_helper;
505 template<
typename T>
void smart_memmove(
const T* start,
const T* end, T* target)
507 smart_memmove_helper<T,!NumTraits<T>::RequireInitialization>::run(start, end, target);
510 template<
typename T>
struct smart_memmove_helper<T,true> {
511 static inline void run(
const T* start,
const T* end, T* target)
513 IntPtr size = IntPtr(end)-IntPtr(start);
515 eigen_internal_assert(start!=0 && end!=0 && target!=0);
516 std::memmove(target, start, size);
520 template<
typename T>
struct smart_memmove_helper<T,false> {
521 static inline void run(
const T* start,
const T* end, T* target)
523 if (uintptr_t(target) < uintptr_t(start))
525 std::copy(start, end, target);
529 std::ptrdiff_t count = (std::ptrdiff_t(end)-std::ptrdiff_t(start)) /
sizeof(T);
530 std::copy_backward(start, end, target + count);
543 #if EIGEN_OS_LINUX || EIGEN_OS_MAC || (defined alloca) 544 #define EIGEN_ALLOCA alloca 545 #elif EIGEN_COMP_MSVC 546 #define EIGEN_ALLOCA _alloca 552 template<
typename T>
class aligned_stack_memory_handler : noncopyable
561 aligned_stack_memory_handler(T* ptr,
size_t size,
bool dealloc)
562 : m_ptr(ptr), m_size(size), m_deallocate(dealloc)
564 if(NumTraits<T>::RequireInitialization && m_ptr)
565 Eigen::internal::construct_elements_of_array(m_ptr, size);
567 ~aligned_stack_memory_handler()
569 if(NumTraits<T>::RequireInitialization && m_ptr)
570 Eigen::internal::destruct_elements_of_array<T>(m_ptr, m_size);
572 Eigen::internal::aligned_free(m_ptr);
580 template<
typename T>
class scoped_array : noncopyable
584 explicit scoped_array(std::ptrdiff_t size)
592 T& operator[](std::ptrdiff_t i) {
return m_ptr[i]; }
593 const T& operator[](std::ptrdiff_t i)
const {
return m_ptr[i]; }
594 T* &ptr() {
return m_ptr; }
595 const T* ptr()
const {
return m_ptr; }
596 operator const T*()
const {
return m_ptr; }
599 template<
typename T>
void swap(scoped_array<T> &a,scoped_array<T> &b)
601 std::swap(a.ptr(),b.ptr());
623 #if EIGEN_DEFAULT_ALIGN_BYTES>0 626 #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))) 628 #define EIGEN_ALIGNED_ALLOCA(SIZE) EIGEN_ALLOCA(SIZE) 631 #define ei_declare_aligned_stack_constructed_variable(TYPE,NAME,SIZE,BUFFER) \ 632 Eigen::internal::check_size_for_overflow<TYPE>(SIZE); \ 633 TYPE* NAME = (BUFFER)!=0 ? (BUFFER) \ 634 : reinterpret_cast<TYPE*>( \ 635 (sizeof(TYPE)*SIZE<=EIGEN_STACK_ALLOCATION_LIMIT) ? EIGEN_ALIGNED_ALLOCA(sizeof(TYPE)*SIZE) \ 636 : Eigen::internal::aligned_malloc(sizeof(TYPE)*SIZE) ); \ 637 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) 641 #define ei_declare_aligned_stack_constructed_variable(TYPE,NAME,SIZE,BUFFER) \ 642 Eigen::internal::check_size_for_overflow<TYPE>(SIZE); \ 643 TYPE* NAME = (BUFFER)!=0 ? BUFFER : reinterpret_cast<TYPE*>(Eigen::internal::aligned_malloc(sizeof(TYPE)*SIZE)); \ 644 Eigen::internal::aligned_stack_memory_handler<TYPE> EIGEN_CAT(NAME,_stack_memory_destructor)((BUFFER)==0 ? NAME : 0,SIZE,true) 653 #if EIGEN_MAX_ALIGN_BYTES!=0 654 #define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_NOTHROW(NeedsToAlign) \ 655 void* operator new(size_t size, const std::nothrow_t&) EIGEN_NO_THROW { \ 656 EIGEN_TRY { return Eigen::internal::conditional_aligned_malloc<NeedsToAlign>(size); } \ 657 EIGEN_CATCH (...) { return 0; } \ 659 #define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(NeedsToAlign) \ 660 void *operator new(size_t size) { \ 661 return Eigen::internal::conditional_aligned_malloc<NeedsToAlign>(size); \ 663 void *operator new[](size_t size) { \ 664 return Eigen::internal::conditional_aligned_malloc<NeedsToAlign>(size); \ 666 void operator delete(void * ptr) EIGEN_NO_THROW { Eigen::internal::conditional_aligned_free<NeedsToAlign>(ptr); } \ 667 void operator delete[](void * ptr) EIGEN_NO_THROW { Eigen::internal::conditional_aligned_free<NeedsToAlign>(ptr); } \ 668 void operator delete(void * ptr, std::size_t ) EIGEN_NO_THROW { Eigen::internal::conditional_aligned_free<NeedsToAlign>(ptr); } \ 669 void operator delete[](void * ptr, std::size_t ) EIGEN_NO_THROW { Eigen::internal::conditional_aligned_free<NeedsToAlign>(ptr); } \ 673 static void *operator new(size_t size, void *ptr) { return ::operator new(size,ptr); } \ 674 static void *operator new[](size_t size, void* ptr) { return ::operator new[](size,ptr); } \ 675 void operator delete(void * memory, void *ptr) EIGEN_NO_THROW { return ::operator delete(memory,ptr); } \ 676 void operator delete[](void * memory, void *ptr) EIGEN_NO_THROW { return ::operator delete[](memory,ptr); } \ 678 EIGEN_MAKE_ALIGNED_OPERATOR_NEW_NOTHROW(NeedsToAlign) \ 679 void operator delete(void *ptr, const std::nothrow_t&) EIGEN_NO_THROW { \ 680 Eigen::internal::conditional_aligned_free<NeedsToAlign>(ptr); \ 682 typedef void eigen_aligned_operator_new_marker_type; 684 #define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(NeedsToAlign) 687 #define EIGEN_MAKE_ALIGNED_OPERATOR_NEW EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(true) 688 #define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF_VECTORIZABLE_FIXED_SIZE(Scalar,Size) \ 689 EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(bool(((Size)!=Eigen::Dynamic) && ((sizeof(Scalar)*(Size))%EIGEN_MAX_ALIGN_BYTES==0))) 713 typedef size_t size_type;
714 typedef std::ptrdiff_t difference_type;
716 typedef const T* const_pointer;
717 typedef T& reference;
718 typedef const T& const_reference;
719 typedef T value_type;
736 pointer allocate(size_type num,
const void* = 0)
738 internal::check_size_for_overflow<T>(num);
739 return static_cast<pointer
>( internal::aligned_malloc(num *
sizeof(T)) );
742 void deallocate(pointer p, size_type )
744 internal::aligned_free(p);
750 #if !defined(EIGEN_NO_CPUID) 751 # if EIGEN_COMP_GNUC && EIGEN_ARCH_i386_OR_x86_64 752 # if defined(__PIC__) && EIGEN_ARCH_i386 754 # define EIGEN_CPUID(abcd,func,id) \ 755 __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)); 756 # elif defined(__PIC__) && EIGEN_ARCH_x86_64 759 # define EIGEN_CPUID(abcd,func,id) \ 760 __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)); 763 # define EIGEN_CPUID(abcd,func,id) \ 764 __asm__ __volatile__ ("cpuid": "=a" (abcd[0]), "=b" (abcd[1]), "=c" (abcd[2]), "=d" (abcd[3]) : "0" (func), "2" (id) ); 766 # elif EIGEN_COMP_MSVC 767 # if (EIGEN_COMP_MSVC > 1500) && EIGEN_ARCH_i386_OR_x86_64 768 # define EIGEN_CPUID(abcd,func,id) __cpuidex((int*)abcd,func,id) 777 inline bool cpuid_is_vendor(
int abcd[4],
const int vendor[3])
779 return abcd[1]==vendor[0] && abcd[3]==vendor[1] && abcd[2]==vendor[2];
782 inline void queryCacheSizes_intel_direct(
int& l1,
int& l2,
int& l3)
789 abcd[0] = abcd[1] = abcd[2] = abcd[3] = 0;
790 EIGEN_CPUID(abcd,0x4,cache_id);
791 cache_type = (abcd[0] & 0x0F) >> 0;
792 if(cache_type==1||cache_type==3)
794 int cache_level = (abcd[0] & 0xE0) >> 5;
795 int ways = (abcd[1] & 0xFFC00000) >> 22;
796 int partitions = (abcd[1] & 0x003FF000) >> 12;
797 int line_size = (abcd[1] & 0x00000FFF) >> 0;
798 int sets = (abcd[2]);
800 int cache_size = (ways+1) * (partitions+1) * (line_size+1) * (sets+1);
804 case 1: l1 = cache_size;
break;
805 case 2: l2 = cache_size;
break;
806 case 3: l3 = cache_size;
break;
811 }
while(cache_type>0 && cache_id<16);
814 inline void queryCacheSizes_intel_codes(
int& l1,
int& l2,
int& l3)
817 abcd[0] = abcd[1] = abcd[2] = abcd[3] = 0;
819 EIGEN_CPUID(abcd,0x00000002,0);
820 unsigned char * bytes =
reinterpret_cast<unsigned char *
>(abcd)+2;
821 bool check_for_p2_core2 =
false;
822 for(
int i=0; i<14; ++i)
826 case 0x0A: l1 = 8;
break;
827 case 0x0C: l1 = 16;
break;
828 case 0x0E: l1 = 24;
break;
829 case 0x10: l1 = 16;
break;
830 case 0x15: l1 = 16;
break;
831 case 0x2C: l1 = 32;
break;
832 case 0x30: l1 = 32;
break;
833 case 0x60: l1 = 16;
break;
834 case 0x66: l1 = 8;
break;
835 case 0x67: l1 = 16;
break;
836 case 0x68: l1 = 32;
break;
837 case 0x1A: l2 = 96;
break;
838 case 0x22: l3 = 512;
break;
839 case 0x23: l3 = 1024;
break;
840 case 0x25: l3 = 2048;
break;
841 case 0x29: l3 = 4096;
break;
842 case 0x39: l2 = 128;
break;
843 case 0x3A: l2 = 192;
break;
844 case 0x3B: l2 = 128;
break;
845 case 0x3C: l2 = 256;
break;
846 case 0x3D: l2 = 384;
break;
847 case 0x3E: l2 = 512;
break;
848 case 0x40: l2 = 0;
break;
849 case 0x41: l2 = 128;
break;
850 case 0x42: l2 = 256;
break;
851 case 0x43: l2 = 512;
break;
852 case 0x44: l2 = 1024;
break;
853 case 0x45: l2 = 2048;
break;
854 case 0x46: l3 = 4096;
break;
855 case 0x47: l3 = 8192;
break;
856 case 0x48: l2 = 3072;
break;
857 case 0x49:
if(l2!=0) l3 = 4096;
else {check_for_p2_core2=
true; l3 = l2 = 4096;}
break;
858 case 0x4A: l3 = 6144;
break;
859 case 0x4B: l3 = 8192;
break;
860 case 0x4C: l3 = 12288;
break;
861 case 0x4D: l3 = 16384;
break;
862 case 0x4E: l2 = 6144;
break;
863 case 0x78: l2 = 1024;
break;
864 case 0x79: l2 = 128;
break;
865 case 0x7A: l2 = 256;
break;
866 case 0x7B: l2 = 512;
break;
867 case 0x7C: l2 = 1024;
break;
868 case 0x7D: l2 = 2048;
break;
869 case 0x7E: l2 = 256;
break;
870 case 0x7F: l2 = 512;
break;
871 case 0x80: l2 = 512;
break;
872 case 0x81: l2 = 128;
break;
873 case 0x82: l2 = 256;
break;
874 case 0x83: l2 = 512;
break;
875 case 0x84: l2 = 1024;
break;
876 case 0x85: l2 = 2048;
break;
877 case 0x86: l2 = 512;
break;
878 case 0x87: l2 = 1024;
break;
879 case 0x88: l3 = 2048;
break;
880 case 0x89: l3 = 4096;
break;
881 case 0x8A: l3 = 8192;
break;
882 case 0x8D: l3 = 3072;
break;
887 if(check_for_p2_core2 && l2 == l3)
894 inline void queryCacheSizes_intel(
int& l1,
int& l2,
int& l3,
int max_std_funcs)
897 queryCacheSizes_intel_direct(l1,l2,l3);
899 queryCacheSizes_intel_codes(l1,l2,l3);
902 inline void queryCacheSizes_amd(
int& l1,
int& l2,
int& l3)
905 abcd[0] = abcd[1] = abcd[2] = abcd[3] = 0;
906 EIGEN_CPUID(abcd,0x80000005,0);
907 l1 = (abcd[2] >> 24) * 1024;
908 abcd[0] = abcd[1] = abcd[2] = abcd[3] = 0;
909 EIGEN_CPUID(abcd,0x80000006,0);
910 l2 = (abcd[2] >> 16) * 1024;
911 l3 = ((abcd[3] & 0xFFFC000) >> 18) * 512 * 1024;
917 inline void queryCacheSizes(
int& l1,
int& l2,
int& l3)
921 const int GenuineIntel[] = {0x756e6547, 0x49656e69, 0x6c65746e};
922 const int AuthenticAMD[] = {0x68747541, 0x69746e65, 0x444d4163};
923 const int AMDisbetter_[] = {0x69444d41, 0x74656273, 0x21726574};
926 EIGEN_CPUID(abcd,0x0,0);
927 int max_std_funcs = abcd[1];
928 if(cpuid_is_vendor(abcd,GenuineIntel))
929 queryCacheSizes_intel(l1,l2,l3,max_std_funcs);
930 else if(cpuid_is_vendor(abcd,AuthenticAMD) || cpuid_is_vendor(abcd,AMDisbetter_))
931 queryCacheSizes_amd(l1,l2,l3);
934 queryCacheSizes_intel(l1,l2,l3,max_std_funcs);
954 inline int queryL1CacheSize()
957 queryCacheSizes(l1,l2,l3);
963 inline int queryTopLevelCacheSize()
965 int l1, l2(-1), l3(-1);
966 queryCacheSizes(l1,l2,l3);
967 return (std::max)(l2,l3);
974 #endif // EIGEN_MEMORY_H Namespace containing all symbols from the Eigen library.
Definition: Core:271
STL compatible allocator to use with with 16 byte aligned types.
Definition: Memory.h:710
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: XprHelper.h:35
Definition: Eigen_Colamd.h:50