00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifndef EIGEN_MEMORY_H
00020 #define EIGEN_MEMORY_H
00021
00022
00023
00024
00025
00026
00027
00028
00029 #if defined(__GLIBC__) && ((__GLIBC__>=2 && __GLIBC_MINOR__ >= 8) || __GLIBC__>2) \
00030 && defined(__LP64__)
00031 #define EIGEN_GLIBC_MALLOC_ALREADY_ALIGNED 1
00032 #else
00033 #define EIGEN_GLIBC_MALLOC_ALREADY_ALIGNED 0
00034 #endif
00035
00036
00037
00038
00039
00040 #if defined(__FreeBSD__) && !defined(__arm__) && !defined(__mips__)
00041 #define EIGEN_FREEBSD_MALLOC_ALREADY_ALIGNED 1
00042 #else
00043 #define EIGEN_FREEBSD_MALLOC_ALREADY_ALIGNED 0
00044 #endif
00045
00046 #if defined(__APPLE__) \
00047 || defined(_WIN64) \
00048 || EIGEN_GLIBC_MALLOC_ALREADY_ALIGNED \
00049 || EIGEN_FREEBSD_MALLOC_ALREADY_ALIGNED
00050 #define EIGEN_MALLOC_ALREADY_ALIGNED 1
00051 #else
00052 #define EIGEN_MALLOC_ALREADY_ALIGNED 0
00053 #endif
00054
00055 #if ((defined __QNXNTO__) || (defined _GNU_SOURCE) || ((defined _XOPEN_SOURCE) && (_XOPEN_SOURCE >= 600))) \
00056 && (defined _POSIX_ADVISORY_INFO) && (_POSIX_ADVISORY_INFO > 0)
00057 #define EIGEN_HAS_POSIX_MEMALIGN 1
00058 #else
00059 #define EIGEN_HAS_POSIX_MEMALIGN 0
00060 #endif
00061
00062 #ifdef EIGEN_VECTORIZE_SSE
00063 #define EIGEN_HAS_MM_MALLOC 1
00064 #else
00065 #define EIGEN_HAS_MM_MALLOC 0
00066 #endif
00067
00068 namespace Eigen {
00069
00070 namespace internal {
00071
00072 inline void throw_std_bad_alloc()
00073 {
00074 #ifdef EIGEN_EXCEPTIONS
00075 throw std::bad_alloc();
00076 #else
00077 std::size_t huge = -1;
00078 new int[huge];
00079 #endif
00080 }
00081
00082
00083
00084
00085
00086
00087
00091 inline void* handmade_aligned_malloc(size_t size)
00092 {
00093 void *original = std::malloc(size+16);
00094 if (original == 0) return 0;
00095 void *aligned = reinterpret_cast<void*>((reinterpret_cast<size_t>(original) & ~(size_t(15))) + 16);
00096 *(reinterpret_cast<void**>(aligned) - 1) = original;
00097 return aligned;
00098 }
00099
00101 inline void handmade_aligned_free(void *ptr)
00102 {
00103 if (ptr) std::free(*(reinterpret_cast<void**>(ptr) - 1));
00104 }
00105
00111 inline void* handmade_aligned_realloc(void* ptr, size_t size, size_t = 0)
00112 {
00113 if (ptr == 0) return handmade_aligned_malloc(size);
00114 void *original = *(reinterpret_cast<void**>(ptr) - 1);
00115 original = std::realloc(original,size+16);
00116 if (original == 0) return 0;
00117 void *aligned = reinterpret_cast<void*>((reinterpret_cast<size_t>(original) & ~(size_t(15))) + 16);
00118 *(reinterpret_cast<void**>(aligned) - 1) = original;
00119 return aligned;
00120 }
00121
00122
00123
00124
00125
00126 void* aligned_malloc(size_t size);
00127 void aligned_free(void *ptr);
00128
00134 inline void* generic_aligned_realloc(void* ptr, size_t size, size_t old_size)
00135 {
00136 if (ptr==0)
00137 return aligned_malloc(size);
00138
00139 if (size==0)
00140 {
00141 aligned_free(ptr);
00142 return 0;
00143 }
00144
00145 void* newptr = aligned_malloc(size);
00146 if (newptr == 0)
00147 {
00148 #ifdef EIGEN_HAS_ERRNO
00149 errno = ENOMEM;
00150 #endif
00151 return 0;
00152 }
00153
00154 if (ptr != 0)
00155 {
00156 std::memcpy(newptr, ptr, (std::min)(size,old_size));
00157 aligned_free(ptr);
00158 }
00159
00160 return newptr;
00161 }
00162
00163
00164
00165
00166
00167 #ifdef EIGEN_NO_MALLOC
00168 inline void check_that_malloc_is_allowed()
00169 {
00170 eigen_assert(false && "heap allocation is forbidden (EIGEN_NO_MALLOC is defined)");
00171 }
00172 #elif defined EIGEN_RUNTIME_NO_MALLOC
00173 inline bool is_malloc_allowed_impl(bool update, bool new_value = false)
00174 {
00175 static bool value = true;
00176 if (update == 1)
00177 value = new_value;
00178 return value;
00179 }
00180 inline bool is_malloc_allowed() { return is_malloc_allowed_impl(false); }
00181 inline bool set_is_malloc_allowed(bool new_value) { return is_malloc_allowed_impl(true, new_value); }
00182 inline void check_that_malloc_is_allowed()
00183 {
00184 eigen_assert(is_malloc_allowed() && "heap allocation is forbidden (EIGEN_RUNTIME_NO_MALLOC is defined and g_is_malloc_allowed is false)");
00185 }
00186 #else
00187 inline void check_that_malloc_is_allowed()
00188 {}
00189 #endif
00190
00194 inline void* aligned_malloc(size_t size)
00195 {
00196 check_that_malloc_is_allowed();
00197
00198 void *result;
00199 #if !EIGEN_ALIGN
00200 result = std::malloc(size);
00201 #elif EIGEN_MALLOC_ALREADY_ALIGNED
00202 result = std::malloc(size);
00203 #elif EIGEN_HAS_POSIX_MEMALIGN
00204 if(posix_memalign(&result, 16, size)) result = 0;
00205 #elif EIGEN_HAS_MM_MALLOC
00206 result = _mm_malloc(size, 16);
00207 #elif defined(_MSC_VER) && (!defined(_WIN32_WCE))
00208 result = _aligned_malloc(size, 16);
00209 #else
00210 result = handmade_aligned_malloc(size);
00211 #endif
00212
00213 if(!result && size)
00214 throw_std_bad_alloc();
00215
00216 return result;
00217 }
00218
00220 inline void aligned_free(void *ptr)
00221 {
00222 #if !EIGEN_ALIGN
00223 std::free(ptr);
00224 #elif EIGEN_MALLOC_ALREADY_ALIGNED
00225 std::free(ptr);
00226 #elif EIGEN_HAS_POSIX_MEMALIGN
00227 std::free(ptr);
00228 #elif EIGEN_HAS_MM_MALLOC
00229 _mm_free(ptr);
00230 #elif defined(_MSC_VER)
00231 _aligned_free(ptr);
00232 #else
00233 handmade_aligned_free(ptr);
00234 #endif
00235 }
00236
00242 inline void* aligned_realloc(void *ptr, size_t new_size, size_t old_size)
00243 {
00244 EIGEN_UNUSED_VARIABLE(old_size);
00245
00246 void *result;
00247 #if !EIGEN_ALIGN
00248 result = std::realloc(ptr,new_size);
00249 #elif EIGEN_MALLOC_ALREADY_ALIGNED
00250 result = std::realloc(ptr,new_size);
00251 #elif EIGEN_HAS_POSIX_MEMALIGN
00252 result = generic_aligned_realloc(ptr,new_size,old_size);
00253 #elif EIGEN_HAS_MM_MALLOC
00254
00255
00256
00257 #if defined(_MSC_VER) && defined(_mm_free)
00258 result = _aligned_realloc(ptr,new_size,16);
00259 #else
00260 result = generic_aligned_realloc(ptr,new_size,old_size);
00261 #endif
00262 #elif defined(_MSC_VER)
00263 result = _aligned_realloc(ptr,new_size,16);
00264 #else
00265 result = handmade_aligned_realloc(ptr,new_size,old_size);
00266 #endif
00267
00268 if (!result && new_size)
00269 throw_std_bad_alloc();
00270
00271 return result;
00272 }
00273
00274
00275
00276
00277
00281 template<bool Align> inline void* conditional_aligned_malloc(size_t size)
00282 {
00283 return aligned_malloc(size);
00284 }
00285
00286 template<> inline void* conditional_aligned_malloc<false>(size_t size)
00287 {
00288 check_that_malloc_is_allowed();
00289
00290 void *result = std::malloc(size);
00291 if(!result && size)
00292 throw_std_bad_alloc();
00293 return result;
00294 }
00295
00297 template<bool Align> inline void conditional_aligned_free(void *ptr)
00298 {
00299 aligned_free(ptr);
00300 }
00301
00302 template<> inline void conditional_aligned_free<false>(void *ptr)
00303 {
00304 std::free(ptr);
00305 }
00306
00307 template<bool Align> inline void* conditional_aligned_realloc(void* ptr, size_t new_size, size_t old_size)
00308 {
00309 return aligned_realloc(ptr, new_size, old_size);
00310 }
00311
00312 template<> inline void* conditional_aligned_realloc<false>(void* ptr, size_t new_size, size_t)
00313 {
00314 return std::realloc(ptr, new_size);
00315 }
00316
00317
00318
00319
00320
00324 template<typename T> inline T* construct_elements_of_array(T *ptr, size_t size)
00325 {
00326 for (size_t i=0; i < size; ++i) ::new (ptr + i) T;
00327 return ptr;
00328 }
00329
00333 template<typename T> inline void destruct_elements_of_array(T *ptr, size_t size)
00334 {
00335
00336 if(ptr)
00337 while(size) ptr[--size].~T();
00338 }
00339
00340
00341
00342
00343
00344 template<typename T>
00345 EIGEN_ALWAYS_INLINE void check_size_for_overflow(size_t size)
00346 {
00347 if(size > size_t(-1) / sizeof(T))
00348 throw_std_bad_alloc();
00349 }
00350
00355 template<typename T> inline T* aligned_new(size_t size)
00356 {
00357 check_size_for_overflow<T>(size);
00358 T *result = reinterpret_cast<T*>(aligned_malloc(sizeof(T)*size));
00359 return construct_elements_of_array(result, size);
00360 }
00361
00362 template<typename T, bool Align> inline T* conditional_aligned_new(size_t size)
00363 {
00364 check_size_for_overflow<T>(size);
00365 T *result = reinterpret_cast<T*>(conditional_aligned_malloc<Align>(sizeof(T)*size));
00366 return construct_elements_of_array(result, size);
00367 }
00368
00372 template<typename T> inline void aligned_delete(T *ptr, size_t size)
00373 {
00374 destruct_elements_of_array<T>(ptr, size);
00375 aligned_free(ptr);
00376 }
00377
00381 template<typename T, bool Align> inline void conditional_aligned_delete(T *ptr, size_t size)
00382 {
00383 destruct_elements_of_array<T>(ptr, size);
00384 conditional_aligned_free<Align>(ptr);
00385 }
00386
00387 template<typename T, bool Align> inline T* conditional_aligned_realloc_new(T* pts, size_t new_size, size_t old_size)
00388 {
00389 check_size_for_overflow<T>(new_size);
00390 check_size_for_overflow<T>(old_size);
00391 if(new_size < old_size)
00392 destruct_elements_of_array(pts+new_size, old_size-new_size);
00393 T *result = reinterpret_cast<T*>(conditional_aligned_realloc<Align>(reinterpret_cast<void*>(pts), sizeof(T)*new_size, sizeof(T)*old_size));
00394 if(new_size > old_size)
00395 construct_elements_of_array(result+old_size, new_size-old_size);
00396 return result;
00397 }
00398
00399
00400 template<typename T, bool Align> inline T* conditional_aligned_new_auto(size_t size)
00401 {
00402 check_size_for_overflow<T>(size);
00403 T *result = reinterpret_cast<T*>(conditional_aligned_malloc<Align>(sizeof(T)*size));
00404 if(NumTraits<T>::RequireInitialization)
00405 construct_elements_of_array(result, size);
00406 return result;
00407 }
00408
00409 template<typename T, bool Align> inline T* conditional_aligned_realloc_new_auto(T* pts, size_t new_size, size_t old_size)
00410 {
00411 check_size_for_overflow<T>(new_size);
00412 check_size_for_overflow<T>(old_size);
00413 if(NumTraits<T>::RequireInitialization && (new_size < old_size))
00414 destruct_elements_of_array(pts+new_size, old_size-new_size);
00415 T *result = reinterpret_cast<T*>(conditional_aligned_realloc<Align>(reinterpret_cast<void*>(pts), sizeof(T)*new_size, sizeof(T)*old_size));
00416 if(NumTraits<T>::RequireInitialization && (new_size > old_size))
00417 construct_elements_of_array(result+old_size, new_size-old_size);
00418 return result;
00419 }
00420
00421 template<typename T, bool Align> inline void conditional_aligned_delete_auto(T *ptr, size_t size)
00422 {
00423 if(NumTraits<T>::RequireInitialization)
00424 destruct_elements_of_array<T>(ptr, size);
00425 conditional_aligned_free<Align>(ptr);
00426 }
00427
00428
00429
00446 template<typename Scalar, typename Index>
00447 static inline Index first_aligned(const Scalar* array, Index size)
00448 {
00449 typedef typename packet_traits<Scalar>::type Packet;
00450 enum { PacketSize = packet_traits<Scalar>::size,
00451 PacketAlignedMask = PacketSize-1
00452 };
00453
00454 if(PacketSize==1)
00455 {
00456
00457
00458 return 0;
00459 }
00460 else if(size_t(array) & (sizeof(Scalar)-1))
00461 {
00462
00463
00464 return size;
00465 }
00466 else
00467 {
00468 return std::min<Index>( (PacketSize - (Index((size_t(array)/sizeof(Scalar))) & PacketAlignedMask))
00469 & PacketAlignedMask, size);
00470 }
00471 }
00472
00473
00474
00475
00476 template<typename T, bool UseMemcpy> struct smart_copy_helper;
00477
00478 template<typename T> void smart_copy(const T* start, const T* end, T* target)
00479 {
00480 smart_copy_helper<T,!NumTraits<T>::RequireInitialization>::run(start, end, target);
00481 }
00482
00483 template<typename T> struct smart_copy_helper<T,true> {
00484 static inline void run(const T* start, const T* end, T* target)
00485 { memcpy(target, start, std::ptrdiff_t(end)-std::ptrdiff_t(start)); }
00486 };
00487
00488 template<typename T> struct smart_copy_helper<T,false> {
00489 static inline void run(const T* start, const T* end, T* target)
00490 { std::copy(start, end, target); }
00491 };
00492
00493
00494
00495
00496
00497
00498
00499
00500 #ifndef EIGEN_ALLOCA
00501 #if (defined __linux__)
00502 #define EIGEN_ALLOCA alloca
00503 #elif defined(_MSC_VER)
00504 #define EIGEN_ALLOCA _alloca
00505 #endif
00506 #endif
00507
00508
00509
00510 template<typename T> class aligned_stack_memory_handler
00511 {
00512 public:
00513
00514
00515
00516
00517
00518
00519 aligned_stack_memory_handler(T* ptr, size_t size, bool dealloc)
00520 : m_ptr(ptr), m_size(size), m_deallocate(dealloc)
00521 {
00522 if(NumTraits<T>::RequireInitialization && m_ptr)
00523 Eigen::internal::construct_elements_of_array(m_ptr, size);
00524 }
00525 ~aligned_stack_memory_handler()
00526 {
00527 if(NumTraits<T>::RequireInitialization && m_ptr)
00528 Eigen::internal::destruct_elements_of_array<T>(m_ptr, m_size);
00529 if(m_deallocate)
00530 Eigen::internal::aligned_free(m_ptr);
00531 }
00532 protected:
00533 T* m_ptr;
00534 size_t m_size;
00535 bool m_deallocate;
00536 };
00537
00538 }
00539
00555 #ifdef EIGEN_ALLOCA
00556
00557 #ifdef __arm__
00558 #define EIGEN_ALIGNED_ALLOCA(SIZE) reinterpret_cast<void*>((reinterpret_cast<size_t>(EIGEN_ALLOCA(SIZE+16)) & ~(size_t(15))) + 16)
00559 #else
00560 #define EIGEN_ALIGNED_ALLOCA EIGEN_ALLOCA
00561 #endif
00562
00563 #define ei_declare_aligned_stack_constructed_variable(TYPE,NAME,SIZE,BUFFER) \
00564 Eigen::internal::check_size_for_overflow<TYPE>(SIZE); \
00565 TYPE* NAME = (BUFFER)!=0 ? (BUFFER) \
00566 : reinterpret_cast<TYPE*>( \
00567 (sizeof(TYPE)*SIZE<=EIGEN_STACK_ALLOCATION_LIMIT) ? EIGEN_ALIGNED_ALLOCA(sizeof(TYPE)*SIZE) \
00568 : Eigen::internal::aligned_malloc(sizeof(TYPE)*SIZE) ); \
00569 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)
00570
00571 #else
00572
00573 #define ei_declare_aligned_stack_constructed_variable(TYPE,NAME,SIZE,BUFFER) \
00574 Eigen::internal::check_size_for_overflow<TYPE>(SIZE); \
00575 TYPE* NAME = (BUFFER)!=0 ? BUFFER : reinterpret_cast<TYPE*>(Eigen::internal::aligned_malloc(sizeof(TYPE)*SIZE)); \
00576 Eigen::internal::aligned_stack_memory_handler<TYPE> EIGEN_CAT(NAME,_stack_memory_destructor)((BUFFER)==0 ? NAME : 0,SIZE,true)
00577
00578 #endif
00579
00580
00581
00582
00583
00584
00585 #if EIGEN_ALIGN
00586 #ifdef EIGEN_EXCEPTIONS
00587 #define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_NOTHROW(NeedsToAlign) \
00588 void* operator new(size_t size, const std::nothrow_t&) throw() { \
00589 try { return Eigen::internal::conditional_aligned_malloc<NeedsToAlign>(size); } \
00590 catch (...) { return 0; } \
00591 return 0; \
00592 }
00593 #else
00594 #define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_NOTHROW(NeedsToAlign) \
00595 void* operator new(size_t size, const std::nothrow_t&) throw() { \
00596 return Eigen::internal::conditional_aligned_malloc<NeedsToAlign>(size); \
00597 }
00598 #endif
00599
00600 #define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(NeedsToAlign) \
00601 void *operator new(size_t size) { \
00602 return Eigen::internal::conditional_aligned_malloc<NeedsToAlign>(size); \
00603 } \
00604 void *operator new[](size_t size) { \
00605 return Eigen::internal::conditional_aligned_malloc<NeedsToAlign>(size); \
00606 } \
00607 void operator delete(void * ptr) throw() { Eigen::internal::conditional_aligned_free<NeedsToAlign>(ptr); } \
00608 void operator delete[](void * ptr) throw() { Eigen::internal::conditional_aligned_free<NeedsToAlign>(ptr); } \
00609 \
00610 \
00611 \
00612 static void *operator new(size_t size, void *ptr) { return ::operator new(size,ptr); } \
00613 void operator delete(void * memory, void *ptr) throw() { return ::operator delete(memory,ptr); } \
00614 \
00615 EIGEN_MAKE_ALIGNED_OPERATOR_NEW_NOTHROW(NeedsToAlign) \
00616 void operator delete(void *ptr, const std::nothrow_t&) throw() { \
00617 Eigen::internal::conditional_aligned_free<NeedsToAlign>(ptr); \
00618 } \
00619 typedef void eigen_aligned_operator_new_marker_type;
00620 #else
00621 #define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(NeedsToAlign)
00622 #endif
00623
00624 #define EIGEN_MAKE_ALIGNED_OPERATOR_NEW EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(true)
00625 #define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF_VECTORIZABLE_FIXED_SIZE(Scalar,Size) \
00626 EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(bool(((Size)!=Eigen::Dynamic) && ((sizeof(Scalar)*(Size))%16==0)))
00627
00628
00629
00646 template<class T>
00647 class aligned_allocator
00648 {
00649 public:
00650 typedef size_t size_type;
00651 typedef std::ptrdiff_t difference_type;
00652 typedef T* pointer;
00653 typedef const T* const_pointer;
00654 typedef T& reference;
00655 typedef const T& const_reference;
00656 typedef T value_type;
00657
00658 template<class U>
00659 struct rebind
00660 {
00661 typedef aligned_allocator<U> other;
00662 };
00663
00664 pointer address( reference value ) const
00665 {
00666 return &value;
00667 }
00668
00669 const_pointer address( const_reference value ) const
00670 {
00671 return &value;
00672 }
00673
00674 aligned_allocator()
00675 {
00676 }
00677
00678 aligned_allocator( const aligned_allocator& )
00679 {
00680 }
00681
00682 template<class U>
00683 aligned_allocator( const aligned_allocator<U>& )
00684 {
00685 }
00686
00687 ~aligned_allocator()
00688 {
00689 }
00690
00691 size_type max_size() const
00692 {
00693 return (std::numeric_limits<size_type>::max)();
00694 }
00695
00696 pointer allocate( size_type num, const void* hint = 0 )
00697 {
00698 EIGEN_UNUSED_VARIABLE(hint);
00699 internal::check_size_for_overflow<T>(num);
00700 return static_cast<pointer>( internal::aligned_malloc( num * sizeof(T) ) );
00701 }
00702
00703 void construct( pointer p, const T& value )
00704 {
00705 ::new( p ) T( value );
00706 }
00707
00708
00709 #if (__cplusplus >= 201103L)
00710 template<typename... Args>
00711 void construct(pointer p, Args&&... args)
00712 {
00713 ::new(p) T(std::forward<Args>(args)...);
00714 }
00715 #endif
00716
00717 void destroy( pointer p )
00718 {
00719 p->~T();
00720 }
00721
00722 void deallocate( pointer p, size_type )
00723 {
00724 internal::aligned_free( p );
00725 }
00726
00727 bool operator!=(const aligned_allocator<T>& ) const
00728 { return false; }
00729
00730 bool operator==(const aligned_allocator<T>& ) const
00731 { return true; }
00732 };
00733
00734
00735
00736 #if !defined(EIGEN_NO_CPUID)
00737 # if defined(__GNUC__) && ( defined(__i386__) || defined(__x86_64__) )
00738 # if defined(__PIC__) && defined(__i386__)
00739
00740 # define EIGEN_CPUID(abcd,func,id) \
00741 __asm__ __volatile__ ("xchgl %%ebx, %%esi;cpuid; xchgl %%ebx,%%esi": "=a" (abcd[0]), "=S" (abcd[1]), "=c" (abcd[2]), "=d" (abcd[3]) : "a" (func), "c" (id));
00742 # else
00743
00744 # define EIGEN_CPUID(abcd,func,id) \
00745 __asm__ __volatile__ ("cpuid": "=a" (abcd[0]), "=b" (abcd[1]), "=c" (abcd[2]), "=d" (abcd[3]) : "a" (func), "c" (id) );
00746 # endif
00747 # elif defined(_MSC_VER)
00748 # if (_MSC_VER > 1500) && ( defined(_M_IX86) || defined(_M_X64) )
00749 # define EIGEN_CPUID(abcd,func,id) __cpuidex((int*)abcd,func,id)
00750 # endif
00751 # endif
00752 #endif
00753
00754 namespace internal {
00755
00756 #ifdef EIGEN_CPUID
00757
00758 inline bool cpuid_is_vendor(int abcd[4], const char* vendor)
00759 {
00760 return abcd[1]==(reinterpret_cast<const int*>(vendor))[0] && abcd[3]==(reinterpret_cast<const int*>(vendor))[1] && abcd[2]==(reinterpret_cast<const int*>(vendor))[2];
00761 }
00762
00763 inline void queryCacheSizes_intel_direct(int& l1, int& l2, int& l3)
00764 {
00765 int abcd[4];
00766 l1 = l2 = l3 = 0;
00767 int cache_id = 0;
00768 int cache_type = 0;
00769 do {
00770 abcd[0] = abcd[1] = abcd[2] = abcd[3] = 0;
00771 EIGEN_CPUID(abcd,0x4,cache_id);
00772 cache_type = (abcd[0] & 0x0F) >> 0;
00773 if(cache_type==1||cache_type==3)
00774 {
00775 int cache_level = (abcd[0] & 0xE0) >> 5;
00776 int ways = (abcd[1] & 0xFFC00000) >> 22;
00777 int partitions = (abcd[1] & 0x003FF000) >> 12;
00778 int line_size = (abcd[1] & 0x00000FFF) >> 0;
00779 int sets = (abcd[2]);
00780
00781 int cache_size = (ways+1) * (partitions+1) * (line_size+1) * (sets+1);
00782
00783 switch(cache_level)
00784 {
00785 case 1: l1 = cache_size; break;
00786 case 2: l2 = cache_size; break;
00787 case 3: l3 = cache_size; break;
00788 default: break;
00789 }
00790 }
00791 cache_id++;
00792 } while(cache_type>0 && cache_id<16);
00793 }
00794
00795 inline void queryCacheSizes_intel_codes(int& l1, int& l2, int& l3)
00796 {
00797 int abcd[4];
00798 abcd[0] = abcd[1] = abcd[2] = abcd[3] = 0;
00799 l1 = l2 = l3 = 0;
00800 EIGEN_CPUID(abcd,0x00000002,0);
00801 unsigned char * bytes = reinterpret_cast<unsigned char *>(abcd)+2;
00802 bool check_for_p2_core2 = false;
00803 for(int i=0; i<14; ++i)
00804 {
00805 switch(bytes[i])
00806 {
00807 case 0x0A: l1 = 8; break;
00808 case 0x0C: l1 = 16; break;
00809 case 0x0E: l1 = 24; break;
00810 case 0x10: l1 = 16; break;
00811 case 0x15: l1 = 16; break;
00812 case 0x2C: l1 = 32; break;
00813 case 0x30: l1 = 32; break;
00814 case 0x60: l1 = 16; break;
00815 case 0x66: l1 = 8; break;
00816 case 0x67: l1 = 16; break;
00817 case 0x68: l1 = 32; break;
00818 case 0x1A: l2 = 96; break;
00819 case 0x22: l3 = 512; break;
00820 case 0x23: l3 = 1024; break;
00821 case 0x25: l3 = 2048; break;
00822 case 0x29: l3 = 4096; break;
00823 case 0x39: l2 = 128; break;
00824 case 0x3A: l2 = 192; break;
00825 case 0x3B: l2 = 128; break;
00826 case 0x3C: l2 = 256; break;
00827 case 0x3D: l2 = 384; break;
00828 case 0x3E: l2 = 512; break;
00829 case 0x40: l2 = 0; break;
00830 case 0x41: l2 = 128; break;
00831 case 0x42: l2 = 256; break;
00832 case 0x43: l2 = 512; break;
00833 case 0x44: l2 = 1024; break;
00834 case 0x45: l2 = 2048; break;
00835 case 0x46: l3 = 4096; break;
00836 case 0x47: l3 = 8192; break;
00837 case 0x48: l2 = 3072; break;
00838 case 0x49: if(l2!=0) l3 = 4096; else {check_for_p2_core2=true; l3 = l2 = 4096;} break;
00839 case 0x4A: l3 = 6144; break;
00840 case 0x4B: l3 = 8192; break;
00841 case 0x4C: l3 = 12288; break;
00842 case 0x4D: l3 = 16384; break;
00843 case 0x4E: l2 = 6144; break;
00844 case 0x78: l2 = 1024; break;
00845 case 0x79: l2 = 128; break;
00846 case 0x7A: l2 = 256; break;
00847 case 0x7B: l2 = 512; break;
00848 case 0x7C: l2 = 1024; break;
00849 case 0x7D: l2 = 2048; break;
00850 case 0x7E: l2 = 256; break;
00851 case 0x7F: l2 = 512; break;
00852 case 0x80: l2 = 512; break;
00853 case 0x81: l2 = 128; break;
00854 case 0x82: l2 = 256; break;
00855 case 0x83: l2 = 512; break;
00856 case 0x84: l2 = 1024; break;
00857 case 0x85: l2 = 2048; break;
00858 case 0x86: l2 = 512; break;
00859 case 0x87: l2 = 1024; break;
00860 case 0x88: l3 = 2048; break;
00861 case 0x89: l3 = 4096; break;
00862 case 0x8A: l3 = 8192; break;
00863 case 0x8D: l3 = 3072; break;
00864
00865 default: break;
00866 }
00867 }
00868 if(check_for_p2_core2 && l2 == l3)
00869 l3 = 0;
00870 l1 *= 1024;
00871 l2 *= 1024;
00872 l3 *= 1024;
00873 }
00874
00875 inline void queryCacheSizes_intel(int& l1, int& l2, int& l3, int max_std_funcs)
00876 {
00877 if(max_std_funcs>=4)
00878 queryCacheSizes_intel_direct(l1,l2,l3);
00879 else
00880 queryCacheSizes_intel_codes(l1,l2,l3);
00881 }
00882
00883 inline void queryCacheSizes_amd(int& l1, int& l2, int& l3)
00884 {
00885 int abcd[4];
00886 abcd[0] = abcd[1] = abcd[2] = abcd[3] = 0;
00887 EIGEN_CPUID(abcd,0x80000005,0);
00888 l1 = (abcd[2] >> 24) * 1024;
00889 abcd[0] = abcd[1] = abcd[2] = abcd[3] = 0;
00890 EIGEN_CPUID(abcd,0x80000006,0);
00891 l2 = (abcd[2] >> 16) * 1024;
00892 l3 = ((abcd[3] & 0xFFFC000) >> 18) * 512 * 1024;
00893 }
00894 #endif
00895
00898 inline void queryCacheSizes(int& l1, int& l2, int& l3)
00899 {
00900 #ifdef EIGEN_CPUID
00901 int abcd[4];
00902
00903
00904 EIGEN_CPUID(abcd,0x0,0);
00905 int max_std_funcs = abcd[1];
00906 if(cpuid_is_vendor(abcd,"GenuineIntel"))
00907 queryCacheSizes_intel(l1,l2,l3,max_std_funcs);
00908 else if(cpuid_is_vendor(abcd,"AuthenticAMD") || cpuid_is_vendor(abcd,"AMDisbetter!"))
00909 queryCacheSizes_amd(l1,l2,l3);
00910 else
00911
00912 queryCacheSizes_intel(l1,l2,l3,max_std_funcs);
00913
00914
00915
00916
00917
00918
00919
00920
00921
00922
00923
00924
00925 #else
00926 l1 = l2 = l3 = -1;
00927 #endif
00928 }
00929
00932 inline int queryL1CacheSize()
00933 {
00934 int l1(-1), l2, l3;
00935 queryCacheSizes(l1,l2,l3);
00936 return l1;
00937 }
00938
00941 inline int queryTopLevelCacheSize()
00942 {
00943 int l1, l2(-1), l3(-1);
00944 queryCacheSizes(l1,l2,l3);
00945 return (std::max)(l2,l3);
00946 }
00947
00948 }
00949
00950 }
00951
00952 #endif // EIGEN_MEMORY_H