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 62 #ifndef EIGEN_HAS_POSIX_MEMALIGN 66 #if EIGEN_OS_UNIX && !(EIGEN_OS_SUN || EIGEN_OS_SOLARIS) 68 #if (EIGEN_OS_QNX || (defined _GNU_SOURCE) || EIGEN_COMP_PGI || ((defined _XOPEN_SOURCE) && (_XOPEN_SOURCE >= 600))) && (defined _POSIX_ADVISORY_INFO) && (_POSIX_ADVISORY_INFO > 0) 69 #define EIGEN_HAS_POSIX_MEMALIGN 1 73 #ifndef EIGEN_HAS_POSIX_MEMALIGN 74 #define EIGEN_HAS_POSIX_MEMALIGN 0 78 #if defined EIGEN_VECTORIZE_SSE || defined EIGEN_VECTORIZE_AVX 79 #define EIGEN_HAS_MM_MALLOC 1 81 #define EIGEN_HAS_MM_MALLOC 0 89 inline void throw_std_bad_alloc()
91 #ifdef EIGEN_EXCEPTIONS 92 throw std::bad_alloc();
94 std::size_t huge =
static_cast<std::size_t
>(-1);
108 inline void* handmade_aligned_malloc(std::size_t size)
110 void *original = std::malloc(size+EIGEN_DEFAULT_ALIGN_BYTES);
111 if (original == 0)
return 0;
112 void *aligned =
reinterpret_cast<void*
>((
reinterpret_cast<std::size_t
>(original) & ~(std::size_t(EIGEN_DEFAULT_ALIGN_BYTES-1))) + EIGEN_DEFAULT_ALIGN_BYTES);
113 *(
reinterpret_cast<void**
>(aligned) - 1) = original;
118 inline void handmade_aligned_free(
void *ptr)
120 if (ptr) std::free(*(reinterpret_cast<void**>(ptr) - 1));
128 inline void* handmade_aligned_realloc(
void* ptr, std::size_t size, std::size_t = 0)
130 if (ptr == 0)
return handmade_aligned_malloc(size);
131 void *original = *(
reinterpret_cast<void**
>(ptr) - 1);
132 std::ptrdiff_t previous_offset =
static_cast<char *
>(ptr)-static_cast<char *>(original);
133 original = std::realloc(original,size+EIGEN_DEFAULT_ALIGN_BYTES);
134 if (original == 0)
return 0;
135 void *aligned =
reinterpret_cast<void*
>((
reinterpret_cast<std::size_t
>(original) & ~(std::size_t(EIGEN_DEFAULT_ALIGN_BYTES-1))) + EIGEN_DEFAULT_ALIGN_BYTES);
136 void *previous_aligned =
static_cast<char *
>(original)+previous_offset;
137 if(aligned!=previous_aligned)
138 std::memmove(aligned, previous_aligned, size);
140 *(
reinterpret_cast<void**
>(aligned) - 1) = original;
148 EIGEN_DEVICE_FUNC
void* aligned_malloc(std::size_t size);
149 EIGEN_DEVICE_FUNC
void aligned_free(
void *ptr);
156 inline void* generic_aligned_realloc(
void* ptr,
size_t size,
size_t old_size)
159 return aligned_malloc(size);
167 void* newptr = aligned_malloc(size);
170 #ifdef EIGEN_HAS_ERRNO 178 std::memcpy(newptr, ptr, (std::min)(size,old_size));
189 #ifdef EIGEN_NO_MALLOC 190 EIGEN_DEVICE_FUNC
inline void check_that_malloc_is_allowed()
192 eigen_assert(
false &&
"heap allocation is forbidden (EIGEN_NO_MALLOC is defined)");
194 #elif defined EIGEN_RUNTIME_NO_MALLOC 195 EIGEN_DEVICE_FUNC
inline bool is_malloc_allowed_impl(
bool update,
bool new_value =
false)
197 static bool value =
true;
202 EIGEN_DEVICE_FUNC
inline bool is_malloc_allowed() {
return is_malloc_allowed_impl(
false); }
203 EIGEN_DEVICE_FUNC
inline bool set_is_malloc_allowed(
bool new_value) {
return is_malloc_allowed_impl(
true, new_value); }
204 EIGEN_DEVICE_FUNC
inline void check_that_malloc_is_allowed()
206 eigen_assert(is_malloc_allowed() &&
"heap allocation is forbidden (EIGEN_RUNTIME_NO_MALLOC is defined and g_is_malloc_allowed is false)");
209 EIGEN_DEVICE_FUNC
inline void check_that_malloc_is_allowed()
216 EIGEN_DEVICE_FUNC
inline void* aligned_malloc(
size_t size)
218 check_that_malloc_is_allowed();
221 #if EIGEN_DEFAULT_ALIGN_BYTES==0 222 result = std::malloc(size);
223 #elif EIGEN_MALLOC_ALREADY_ALIGNED 224 result = std::malloc(size);
225 #elif EIGEN_HAS_POSIX_MEMALIGN 226 if(posix_memalign(&result, EIGEN_DEFAULT_ALIGN_BYTES, size)) result = 0;
227 #elif EIGEN_HAS_MM_MALLOC 228 result = _mm_malloc(size, EIGEN_DEFAULT_ALIGN_BYTES);
229 #elif EIGEN_OS_WIN_STRICT 230 result = _aligned_malloc(size, EIGEN_DEFAULT_ALIGN_BYTES);
232 result = handmade_aligned_malloc(size);
236 throw_std_bad_alloc();
242 EIGEN_DEVICE_FUNC
inline void aligned_free(
void *ptr)
244 #if EIGEN_DEFAULT_ALIGN_BYTES==0 246 #elif EIGEN_MALLOC_ALREADY_ALIGNED 248 #elif EIGEN_HAS_POSIX_MEMALIGN 250 #elif EIGEN_HAS_MM_MALLOC 252 #elif EIGEN_OS_WIN_STRICT 255 handmade_aligned_free(ptr);
264 inline void* aligned_realloc(
void *ptr,
size_t new_size,
size_t old_size)
266 EIGEN_UNUSED_VARIABLE(old_size);
269 #if EIGEN_DEFAULT_ALIGN_BYTES==0 270 result = std::realloc(ptr,new_size);
271 #elif EIGEN_MALLOC_ALREADY_ALIGNED 272 result = std::realloc(ptr,new_size);
273 #elif EIGEN_HAS_POSIX_MEMALIGN 274 result = generic_aligned_realloc(ptr,new_size,old_size);
275 #elif EIGEN_HAS_MM_MALLOC 279 #if EIGEN_OS_WIN_STRICT && defined(_mm_free) 280 result = _aligned_realloc(ptr,new_size,EIGEN_DEFAULT_ALIGN_BYTES);
282 result = generic_aligned_realloc(ptr,new_size,old_size);
284 #elif EIGEN_OS_WIN_STRICT 285 result = _aligned_realloc(ptr,new_size,EIGEN_DEFAULT_ALIGN_BYTES);
287 result = handmade_aligned_realloc(ptr,new_size,old_size);
290 if (!result && new_size)
291 throw_std_bad_alloc();
303 template<
bool Align> EIGEN_DEVICE_FUNC
inline void* conditional_aligned_malloc(
size_t size)
305 return aligned_malloc(size);
308 template<> EIGEN_DEVICE_FUNC
inline void* conditional_aligned_malloc<false>(
size_t size)
310 check_that_malloc_is_allowed();
312 void *result = std::malloc(size);
314 throw_std_bad_alloc();
319 template<
bool Align> EIGEN_DEVICE_FUNC
inline void conditional_aligned_free(
void *ptr)
324 template<> EIGEN_DEVICE_FUNC
inline void conditional_aligned_free<false>(
void *ptr)
329 template<
bool Align>
inline void* conditional_aligned_realloc(
void* ptr,
size_t new_size,
size_t old_size)
331 return aligned_realloc(ptr, new_size, old_size);
334 template<>
inline void* conditional_aligned_realloc<false>(
void* ptr,
size_t new_size, size_t)
336 return std::realloc(ptr, new_size);
346 template<
typename T> EIGEN_DEVICE_FUNC
inline void destruct_elements_of_array(T *ptr,
size_t size)
350 while(size) ptr[--size].~T();
356 template<
typename T> EIGEN_DEVICE_FUNC
inline T* construct_elements_of_array(T *ptr,
size_t size)
361 for (i = 0; i < size; ++i) ::
new (ptr + i) T;
366 destruct_elements_of_array(ptr, i);
376 EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
void check_size_for_overflow(
size_t size)
378 if(size >
size_t(-1) /
sizeof(T))
379 throw_std_bad_alloc();
386 template<
typename T> EIGEN_DEVICE_FUNC
inline T* aligned_new(
size_t size)
388 check_size_for_overflow<T>(size);
389 T *result =
reinterpret_cast<T*
>(aligned_malloc(
sizeof(T)*size));
392 return construct_elements_of_array(result, size);
396 aligned_free(result);
401 template<
typename T,
bool Align> EIGEN_DEVICE_FUNC
inline T* conditional_aligned_new(
size_t size)
403 check_size_for_overflow<T>(size);
404 T *result =
reinterpret_cast<T*
>(conditional_aligned_malloc<Align>(
sizeof(T)*size));
407 return construct_elements_of_array(result, size);
411 conditional_aligned_free<Align>(result);
419 template<
typename T> EIGEN_DEVICE_FUNC
inline void aligned_delete(T *ptr,
size_t size)
421 destruct_elements_of_array<T>(ptr, size);
428 template<
typename T,
bool Align> EIGEN_DEVICE_FUNC
inline void conditional_aligned_delete(T *ptr,
size_t size)
430 destruct_elements_of_array<T>(ptr, size);
431 conditional_aligned_free<Align>(ptr);
434 template<
typename T,
bool Align> EIGEN_DEVICE_FUNC
inline T* conditional_aligned_realloc_new(T* pts,
size_t new_size,
size_t old_size)
436 check_size_for_overflow<T>(new_size);
437 check_size_for_overflow<T>(old_size);
438 if(new_size < old_size)
439 destruct_elements_of_array(pts+new_size, old_size-new_size);
440 T *result =
reinterpret_cast<T*
>(conditional_aligned_realloc<Align>(
reinterpret_cast<void*
>(pts),
sizeof(T)*new_size,
sizeof(T)*old_size));
441 if(new_size > old_size)
445 construct_elements_of_array(result+old_size, new_size-old_size);
449 conditional_aligned_free<Align>(result);
457 template<
typename T,
bool Align> EIGEN_DEVICE_FUNC
inline T* conditional_aligned_new_auto(
size_t size)
461 check_size_for_overflow<T>(size);
462 T *result =
reinterpret_cast<T*
>(conditional_aligned_malloc<Align>(
sizeof(T)*size));
463 if(NumTraits<T>::RequireInitialization)
467 construct_elements_of_array(result, size);
471 conditional_aligned_free<Align>(result);
478 template<
typename T,
bool Align>
inline T* conditional_aligned_realloc_new_auto(T* pts,
size_t new_size,
size_t old_size)
480 check_size_for_overflow<T>(new_size);
481 check_size_for_overflow<T>(old_size);
482 if(NumTraits<T>::RequireInitialization && (new_size < old_size))
483 destruct_elements_of_array(pts+new_size, old_size-new_size);
484 T *result =
reinterpret_cast<T*
>(conditional_aligned_realloc<Align>(
reinterpret_cast<void*
>(pts),
sizeof(T)*new_size,
sizeof(T)*old_size));
485 if(NumTraits<T>::RequireInitialization && (new_size > old_size))
489 construct_elements_of_array(result+old_size, new_size-old_size);
493 conditional_aligned_free<Align>(result);
500 template<
typename T,
bool Align> EIGEN_DEVICE_FUNC
inline void conditional_aligned_delete_auto(T *ptr,
size_t size)
502 if(NumTraits<T>::RequireInitialization)
503 destruct_elements_of_array<T>(ptr, size);
504 conditional_aligned_free<Align>(ptr);
526 template<
int Alignment,
typename Scalar,
typename Index>
527 inline Index first_aligned(
const Scalar* array, Index size)
529 static const Index ScalarSize =
sizeof(Scalar);
530 static const Index AlignmentSize = Alignment / ScalarSize;
531 static const Index AlignmentMask = AlignmentSize-1;
539 else if( (std::size_t(array) & (
sizeof(Scalar)-1)) || (Alignment%ScalarSize)!=0)
547 return std::min<Index>( (AlignmentSize - (Index((std::size_t(array)/
sizeof(Scalar))) & AlignmentMask)) & AlignmentMask, size);
553 template<
typename Scalar,
typename Index>
554 inline Index first_default_aligned(
const Scalar* array, Index size)
556 typedef typename packet_traits<Scalar>::type DefaultPacketType;
557 return first_aligned<unpacket_traits<DefaultPacketType>::alignment>(array, size);
562 template<
typename Index>
563 inline Index first_multiple(Index size, Index base)
565 return ((size+base-1)/base)*base;
570 template<
typename T,
bool UseMemcpy>
struct smart_copy_helper;
572 template<
typename T> EIGEN_DEVICE_FUNC
void smart_copy(
const T* start,
const T* end, T* target)
574 smart_copy_helper<T,!NumTraits<T>::RequireInitialization>::run(start, end, target);
577 template<
typename T>
struct smart_copy_helper<T,true> {
578 EIGEN_DEVICE_FUNC
static inline void run(
const T* start,
const T* end, T* target)
579 { memcpy(target, start, std::ptrdiff_t(end)-std::ptrdiff_t(start)); }
582 template<
typename T>
struct smart_copy_helper<T,false> {
583 EIGEN_DEVICE_FUNC
static inline void run(
const T* start,
const T* end, T* target)
584 { std::copy(start, end, target); }
588 template<
typename T,
bool UseMemmove>
struct smart_memmove_helper;
590 template<
typename T>
void smart_memmove(
const T* start,
const T* end, T* target)
592 smart_memmove_helper<T,!NumTraits<T>::RequireInitialization>::run(start, end, target);
595 template<
typename T>
struct smart_memmove_helper<T,true> {
596 static inline void run(
const T* start,
const T* end, T* target)
597 { std::memmove(target, start, std::ptrdiff_t(end)-std::ptrdiff_t(start)); }
600 template<
typename T>
struct smart_memmove_helper<T,false> {
601 static inline void run(
const T* start,
const T* end, T* target)
603 if (uintptr_t(target) < uintptr_t(start))
605 std::copy(start, end, target);
609 std::ptrdiff_t count = (std::ptrdiff_t(end)-std::ptrdiff_t(start)) /
sizeof(T);
610 std::copy_backward(start, end, target + count);
623 #if EIGEN_OS_LINUX || EIGEN_OS_MAC || (defined alloca) 624 #define EIGEN_ALLOCA alloca 625 #elif EIGEN_COMP_MSVC 626 #define EIGEN_ALLOCA _alloca 632 template<
typename T>
class aligned_stack_memory_handler : noncopyable
641 aligned_stack_memory_handler(T* ptr,
size_t size,
bool dealloc)
642 : m_ptr(ptr), m_size(size), m_deallocate(dealloc)
644 if(NumTraits<T>::RequireInitialization && m_ptr)
645 Eigen::internal::construct_elements_of_array(m_ptr, size);
647 ~aligned_stack_memory_handler()
649 if(NumTraits<T>::RequireInitialization && m_ptr)
650 Eigen::internal::destruct_elements_of_array<T>(m_ptr, m_size);
652 Eigen::internal::aligned_free(m_ptr);
660 template<
typename T>
class scoped_array : noncopyable
664 explicit scoped_array(std::ptrdiff_t size)
672 T& operator[](std::ptrdiff_t i) {
return m_ptr[i]; }
673 const T& operator[](std::ptrdiff_t i)
const {
return m_ptr[i]; }
674 T* &ptr() {
return m_ptr; }
675 const T* ptr()
const {
return m_ptr; }
676 operator const T*()
const {
return m_ptr; }
679 template<
typename T>
void swap(scoped_array<T> &a,scoped_array<T> &b)
681 std::swap(a.ptr(),b.ptr());
703 #if EIGEN_DEFAULT_ALIGN_BYTES>0 706 #define EIGEN_ALIGNED_ALLOCA(SIZE) reinterpret_cast<void*>((reinterpret_cast<std::size_t>(EIGEN_ALLOCA(SIZE+EIGEN_DEFAULT_ALIGN_BYTES-1)) + EIGEN_DEFAULT_ALIGN_BYTES-1) & ~(std::size_t(EIGEN_DEFAULT_ALIGN_BYTES-1))) 708 #define EIGEN_ALIGNED_ALLOCA(SIZE) EIGEN_ALLOCA(SIZE) 711 #define ei_declare_aligned_stack_constructed_variable(TYPE,NAME,SIZE,BUFFER) \ 712 Eigen::internal::check_size_for_overflow<TYPE>(SIZE); \ 713 TYPE* NAME = (BUFFER)!=0 ? (BUFFER) \ 714 : reinterpret_cast<TYPE*>( \ 715 (sizeof(TYPE)*SIZE<=EIGEN_STACK_ALLOCATION_LIMIT) ? EIGEN_ALIGNED_ALLOCA(sizeof(TYPE)*SIZE) \ 716 : Eigen::internal::aligned_malloc(sizeof(TYPE)*SIZE) ); \ 717 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) 721 #define ei_declare_aligned_stack_constructed_variable(TYPE,NAME,SIZE,BUFFER) \ 722 Eigen::internal::check_size_for_overflow<TYPE>(SIZE); \ 723 TYPE* NAME = (BUFFER)!=0 ? BUFFER : reinterpret_cast<TYPE*>(Eigen::internal::aligned_malloc(sizeof(TYPE)*SIZE)); \ 724 Eigen::internal::aligned_stack_memory_handler<TYPE> EIGEN_CAT(NAME,_stack_memory_destructor)((BUFFER)==0 ? NAME : 0,SIZE,true) 733 #if EIGEN_MAX_ALIGN_BYTES!=0 734 #define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_NOTHROW(NeedsToAlign) \ 735 void* operator new(size_t size, const std::nothrow_t&) EIGEN_NO_THROW { \ 736 EIGEN_TRY { return Eigen::internal::conditional_aligned_malloc<NeedsToAlign>(size); } \ 737 EIGEN_CATCH (...) { return 0; } \ 739 #define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(NeedsToAlign) \ 740 void *operator new(size_t size) { \ 741 return Eigen::internal::conditional_aligned_malloc<NeedsToAlign>(size); \ 743 void *operator new[](size_t size) { \ 744 return Eigen::internal::conditional_aligned_malloc<NeedsToAlign>(size); \ 746 void operator delete(void * ptr) EIGEN_NO_THROW { Eigen::internal::conditional_aligned_free<NeedsToAlign>(ptr); } \ 747 void operator delete[](void * ptr) EIGEN_NO_THROW { Eigen::internal::conditional_aligned_free<NeedsToAlign>(ptr); } \ 748 void operator delete(void * ptr, std::size_t ) EIGEN_NO_THROW { Eigen::internal::conditional_aligned_free<NeedsToAlign>(ptr); } \ 749 void operator delete[](void * ptr, std::size_t ) EIGEN_NO_THROW { Eigen::internal::conditional_aligned_free<NeedsToAlign>(ptr); } \ 753 static void *operator new(size_t size, void *ptr) { return ::operator new(size,ptr); } \ 754 static void *operator new[](size_t size, void* ptr) { return ::operator new[](size,ptr); } \ 755 void operator delete(void * memory, void *ptr) EIGEN_NO_THROW { return ::operator delete(memory,ptr); } \ 756 void operator delete[](void * memory, void *ptr) EIGEN_NO_THROW { return ::operator delete[](memory,ptr); } \ 758 EIGEN_MAKE_ALIGNED_OPERATOR_NEW_NOTHROW(NeedsToAlign) \ 759 void operator delete(void *ptr, const std::nothrow_t&) EIGEN_NO_THROW { \ 760 Eigen::internal::conditional_aligned_free<NeedsToAlign>(ptr); \ 762 typedef void eigen_aligned_operator_new_marker_type; 764 #define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(NeedsToAlign) 767 #define EIGEN_MAKE_ALIGNED_OPERATOR_NEW EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(true) 768 #define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF_VECTORIZABLE_FIXED_SIZE(Scalar,Size) \ 769 EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(bool(((Size)!=Eigen::Dynamic) && ((sizeof(Scalar)*(Size))%EIGEN_MAX_ALIGN_BYTES==0))) 793 typedef size_t size_type;
794 typedef std::ptrdiff_t difference_type;
796 typedef const T* const_pointer;
797 typedef T& reference;
798 typedef const T& const_reference;
799 typedef T value_type;
816 pointer allocate(size_type num,
const void* = 0)
818 internal::check_size_for_overflow<T>(num);
819 return static_cast<pointer
>( internal::aligned_malloc(num *
sizeof(T)) );
822 void deallocate(pointer p, size_type )
824 internal::aligned_free(p);
830 #if !defined(EIGEN_NO_CPUID) 831 # if EIGEN_COMP_GNUC && EIGEN_ARCH_i386_OR_x86_64 832 # if defined(__PIC__) && EIGEN_ARCH_i386 834 # define EIGEN_CPUID(abcd,func,id) \ 835 __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)); 836 # elif defined(__PIC__) && EIGEN_ARCH_x86_64 839 # define EIGEN_CPUID(abcd,func,id) \ 840 __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)); 843 # define EIGEN_CPUID(abcd,func,id) \ 844 __asm__ __volatile__ ("cpuid": "=a" (abcd[0]), "=b" (abcd[1]), "=c" (abcd[2]), "=d" (abcd[3]) : "0" (func), "2" (id) ); 846 # elif EIGEN_COMP_MSVC 847 # if (EIGEN_COMP_MSVC > 1500) && EIGEN_ARCH_i386_OR_x86_64 848 # define EIGEN_CPUID(abcd,func,id) __cpuidex((int*)abcd,func,id) 857 inline bool cpuid_is_vendor(
int abcd[4],
const int vendor[3])
859 return abcd[1]==vendor[0] && abcd[3]==vendor[1] && abcd[2]==vendor[2];
862 inline void queryCacheSizes_intel_direct(
int& l1,
int& l2,
int& l3)
869 abcd[0] = abcd[1] = abcd[2] = abcd[3] = 0;
870 EIGEN_CPUID(abcd,0x4,cache_id);
871 cache_type = (abcd[0] & 0x0F) >> 0;
872 if(cache_type==1||cache_type==3)
874 int cache_level = (abcd[0] & 0xE0) >> 5;
875 int ways = (abcd[1] & 0xFFC00000) >> 22;
876 int partitions = (abcd[1] & 0x003FF000) >> 12;
877 int line_size = (abcd[1] & 0x00000FFF) >> 0;
878 int sets = (abcd[2]);
880 int cache_size = (ways+1) * (partitions+1) * (line_size+1) * (sets+1);
884 case 1: l1 = cache_size;
break;
885 case 2: l2 = cache_size;
break;
886 case 3: l3 = cache_size;
break;
891 }
while(cache_type>0 && cache_id<16);
894 inline void queryCacheSizes_intel_codes(
int& l1,
int& l2,
int& l3)
897 abcd[0] = abcd[1] = abcd[2] = abcd[3] = 0;
899 EIGEN_CPUID(abcd,0x00000002,0);
900 unsigned char * bytes =
reinterpret_cast<unsigned char *
>(abcd)+2;
901 bool check_for_p2_core2 =
false;
902 for(
int i=0; i<14; ++i)
906 case 0x0A: l1 = 8;
break;
907 case 0x0C: l1 = 16;
break;
908 case 0x0E: l1 = 24;
break;
909 case 0x10: l1 = 16;
break;
910 case 0x15: l1 = 16;
break;
911 case 0x2C: l1 = 32;
break;
912 case 0x30: l1 = 32;
break;
913 case 0x60: l1 = 16;
break;
914 case 0x66: l1 = 8;
break;
915 case 0x67: l1 = 16;
break;
916 case 0x68: l1 = 32;
break;
917 case 0x1A: l2 = 96;
break;
918 case 0x22: l3 = 512;
break;
919 case 0x23: l3 = 1024;
break;
920 case 0x25: l3 = 2048;
break;
921 case 0x29: l3 = 4096;
break;
922 case 0x39: l2 = 128;
break;
923 case 0x3A: l2 = 192;
break;
924 case 0x3B: l2 = 128;
break;
925 case 0x3C: l2 = 256;
break;
926 case 0x3D: l2 = 384;
break;
927 case 0x3E: l2 = 512;
break;
928 case 0x40: l2 = 0;
break;
929 case 0x41: l2 = 128;
break;
930 case 0x42: l2 = 256;
break;
931 case 0x43: l2 = 512;
break;
932 case 0x44: l2 = 1024;
break;
933 case 0x45: l2 = 2048;
break;
934 case 0x46: l3 = 4096;
break;
935 case 0x47: l3 = 8192;
break;
936 case 0x48: l2 = 3072;
break;
937 case 0x49:
if(l2!=0) l3 = 4096;
else {check_for_p2_core2=
true; l3 = l2 = 4096;}
break;
938 case 0x4A: l3 = 6144;
break;
939 case 0x4B: l3 = 8192;
break;
940 case 0x4C: l3 = 12288;
break;
941 case 0x4D: l3 = 16384;
break;
942 case 0x4E: l2 = 6144;
break;
943 case 0x78: l2 = 1024;
break;
944 case 0x79: l2 = 128;
break;
945 case 0x7A: l2 = 256;
break;
946 case 0x7B: l2 = 512;
break;
947 case 0x7C: l2 = 1024;
break;
948 case 0x7D: l2 = 2048;
break;
949 case 0x7E: l2 = 256;
break;
950 case 0x7F: l2 = 512;
break;
951 case 0x80: l2 = 512;
break;
952 case 0x81: l2 = 128;
break;
953 case 0x82: l2 = 256;
break;
954 case 0x83: l2 = 512;
break;
955 case 0x84: l2 = 1024;
break;
956 case 0x85: l2 = 2048;
break;
957 case 0x86: l2 = 512;
break;
958 case 0x87: l2 = 1024;
break;
959 case 0x88: l3 = 2048;
break;
960 case 0x89: l3 = 4096;
break;
961 case 0x8A: l3 = 8192;
break;
962 case 0x8D: l3 = 3072;
break;
967 if(check_for_p2_core2 && l2 == l3)
974 inline void queryCacheSizes_intel(
int& l1,
int& l2,
int& l3,
int max_std_funcs)
977 queryCacheSizes_intel_direct(l1,l2,l3);
979 queryCacheSizes_intel_codes(l1,l2,l3);
982 inline void queryCacheSizes_amd(
int& l1,
int& l2,
int& l3)
985 abcd[0] = abcd[1] = abcd[2] = abcd[3] = 0;
986 EIGEN_CPUID(abcd,0x80000005,0);
987 l1 = (abcd[2] >> 24) * 1024;
988 abcd[0] = abcd[1] = abcd[2] = abcd[3] = 0;
989 EIGEN_CPUID(abcd,0x80000006,0);
990 l2 = (abcd[2] >> 16) * 1024;
991 l3 = ((abcd[3] & 0xFFFC000) >> 18) * 512 * 1024;
997 inline void queryCacheSizes(
int& l1,
int& l2,
int& l3)
1001 const int GenuineIntel[] = {0x756e6547, 0x49656e69, 0x6c65746e};
1002 const int AuthenticAMD[] = {0x68747541, 0x69746e65, 0x444d4163};
1003 const int AMDisbetter_[] = {0x69444d41, 0x74656273, 0x21726574};
1006 EIGEN_CPUID(abcd,0x0,0);
1007 int max_std_funcs = abcd[1];
1008 if(cpuid_is_vendor(abcd,GenuineIntel))
1009 queryCacheSizes_intel(l1,l2,l3,max_std_funcs);
1010 else if(cpuid_is_vendor(abcd,AuthenticAMD) || cpuid_is_vendor(abcd,AMDisbetter_))
1011 queryCacheSizes_amd(l1,l2,l3);
1014 queryCacheSizes_intel(l1,l2,l3,max_std_funcs);
1034 inline int queryL1CacheSize()
1037 queryCacheSizes(l1,l2,l3);
1043 inline int queryTopLevelCacheSize()
1045 int l1, l2(-1), l3(-1);
1046 queryCacheSizes(l1,l2,l3);
1047 return (std::max)(l2,l3);
1054 #endif // EIGEN_MEMORY_H
STL compatible allocator to use with with 16 byte aligned types.
Definition: Memory.h:790
Definition: Eigen_Colamd.h:54