Eigen  3.3.90 (mercurial changeset 94875feeeeb9)
Memory.h
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2008-2015 Gael Guennebaud <gael.guennebaud@inria.fr>
5 // Copyright (C) 2008-2009 Benoit Jacob <jacob.benoit.1@gmail.com>
6 // Copyright (C) 2009 Kenneth Riddile <kfriddile@yahoo.com>
7 // Copyright (C) 2010 Hauke Heibel <hauke.heibel@gmail.com>
8 // Copyright (C) 2010 Thomas Capricelli <orzel@freehackers.org>
9 // Copyright (C) 2013 Pavel Holoborodko <pavel@holoborodko.com>
10 //
11 // This Source Code Form is subject to the terms of the Mozilla
12 // Public License v. 2.0. If a copy of the MPL was not distributed
13 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
14 
15 
16 /*****************************************************************************
17 *** Platform checks for aligned malloc functions ***
18 *****************************************************************************/
19 
20 #ifndef EIGEN_MEMORY_H
21 #define EIGEN_MEMORY_H
22 
23 #ifndef EIGEN_MALLOC_ALREADY_ALIGNED
24 
25 // Try to determine automatically if malloc is already aligned.
26 
27 // On 64-bit systems, glibc's malloc returns 16-byte-aligned pointers, see:
28 // http://www.gnu.org/s/libc/manual/html_node/Aligned-Memory-Blocks.html
29 // This is true at least since glibc 2.8.
30 // This leaves the question how to detect 64-bit. According to this document,
31 // http://gcc.fyxm.net/summit/2003/Porting%20to%2064%20bit.pdf
32 // page 114, "[The] LP64 model [...] is used by all 64-bit UNIX ports" so it's indeed
33 // quite safe, at least within the context of glibc, to equate 64-bit with LP64.
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
37 #else
38  #define EIGEN_GLIBC_MALLOC_ALREADY_ALIGNED 0
39 #endif
40 
41 // FreeBSD 6 seems to have 16-byte aligned malloc
42 // See http://svn.freebsd.org/viewvc/base/stable/6/lib/libc/stdlib/malloc.c?view=markup
43 // FreeBSD 7 seems to have 16-byte aligned malloc except on ARM and MIPS architectures
44 // See http://svn.freebsd.org/viewvc/base/stable/7/lib/libc/stdlib/malloc.c?view=markup
45 #if defined(__FreeBSD__) && !(EIGEN_ARCH_ARM || EIGEN_ARCH_MIPS) && (EIGEN_DEFAULT_ALIGN_BYTES == 16)
46  #define EIGEN_FREEBSD_MALLOC_ALREADY_ALIGNED 1
47 #else
48  #define EIGEN_FREEBSD_MALLOC_ALREADY_ALIGNED 0
49 #endif
50 
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
56 #else
57  #define EIGEN_MALLOC_ALREADY_ALIGNED 0
58 #endif
59 
60 #endif
61 
62 namespace Eigen {
63 
64 namespace internal {
65 
66 EIGEN_DEVICE_FUNC
67 inline void throw_std_bad_alloc()
68 {
69  #ifdef EIGEN_EXCEPTIONS
70  throw std::bad_alloc();
71  #else
72  std::size_t huge = static_cast<std::size_t>(-1);
73  #if defined(EIGEN_HIPCC)
74  //
75  // calls to "::operator new" are to be treated as opaque function calls (i.e no inlining),
76  // and as a consequence the code in the #else block triggers the hipcc warning :
77  // "no overloaded function has restriction specifiers that are compatible with the ambient context"
78  //
79  // "throw_std_bad_alloc" has the EIGEN_DEVICE_FUNC attribute, so it seems that hipcc expects
80  // the same on "operator new"
81  // Reverting code back to the old version in this #if block for the hipcc compiler
82  //
83  new int[huge];
84  #else
85  ::operator new(huge);
86  #endif
87  #endif
88 }
89 
90 /*****************************************************************************
91 *** Implementation of handmade aligned functions ***
92 *****************************************************************************/
93 
94 /* ----- Hand made implementations of aligned malloc/free and realloc ----- */
95 
99 EIGEN_DEVICE_FUNC inline void* handmade_aligned_malloc(std::size_t size, std::size_t alignment = EIGEN_DEFAULT_ALIGN_BYTES)
100 {
101  eigen_assert(alignment >= sizeof(void*) && (alignment & (alignment-1)) == 0 && "Alignment must be at least sizeof(void*) and a power of 2");
102 
103  EIGEN_USING_STD(malloc)
104  void *original = malloc(size+alignment);
105 
106  if (original == 0) return 0;
107  void *aligned = reinterpret_cast<void*>((reinterpret_cast<std::size_t>(original) & ~(std::size_t(alignment-1))) + alignment);
108  *(reinterpret_cast<void**>(aligned) - 1) = original;
109  return aligned;
110 }
111 
113 EIGEN_DEVICE_FUNC inline void handmade_aligned_free(void *ptr)
114 {
115  if (ptr) {
116  EIGEN_USING_STD(free)
117  free(*(reinterpret_cast<void**>(ptr) - 1));
118  }
119 }
120 
126 inline void* handmade_aligned_realloc(void* ptr, std::size_t size, std::size_t = 0)
127 {
128  if (ptr == 0) return handmade_aligned_malloc(size);
129  void *original = *(reinterpret_cast<void**>(ptr) - 1);
130  std::ptrdiff_t previous_offset = static_cast<char *>(ptr)-static_cast<char *>(original);
131  original = std::realloc(original,size+EIGEN_DEFAULT_ALIGN_BYTES);
132  if (original == 0) return 0;
133  void *aligned = reinterpret_cast<void*>((reinterpret_cast<std::size_t>(original) & ~(std::size_t(EIGEN_DEFAULT_ALIGN_BYTES-1))) + EIGEN_DEFAULT_ALIGN_BYTES);
134  void *previous_aligned = static_cast<char *>(original)+previous_offset;
135  if(aligned!=previous_aligned)
136  std::memmove(aligned, previous_aligned, size);
137 
138  *(reinterpret_cast<void**>(aligned) - 1) = original;
139  return aligned;
140 }
141 
142 /*****************************************************************************
143 *** Implementation of portable aligned versions of malloc/free/realloc ***
144 *****************************************************************************/
145 
146 #ifdef EIGEN_NO_MALLOC
147 EIGEN_DEVICE_FUNC inline void check_that_malloc_is_allowed()
148 {
149  eigen_assert(false && "heap allocation is forbidden (EIGEN_NO_MALLOC is defined)");
150 }
151 #elif defined EIGEN_RUNTIME_NO_MALLOC
152 EIGEN_DEVICE_FUNC inline bool is_malloc_allowed_impl(bool update, bool new_value = false)
153 {
154  static bool value = true;
155  if (update == 1)
156  value = new_value;
157  return value;
158 }
159 EIGEN_DEVICE_FUNC inline bool is_malloc_allowed() { return is_malloc_allowed_impl(false); }
160 EIGEN_DEVICE_FUNC inline bool set_is_malloc_allowed(bool new_value) { return is_malloc_allowed_impl(true, new_value); }
161 EIGEN_DEVICE_FUNC inline void check_that_malloc_is_allowed()
162 {
163  eigen_assert(is_malloc_allowed() && "heap allocation is forbidden (EIGEN_RUNTIME_NO_MALLOC is defined and g_is_malloc_allowed is false)");
164 }
165 #else
166 EIGEN_DEVICE_FUNC inline void check_that_malloc_is_allowed()
167 {}
168 #endif
169 
173 EIGEN_DEVICE_FUNC inline void* aligned_malloc(std::size_t size)
174 {
175  check_that_malloc_is_allowed();
176 
177  void *result;
178  #if (EIGEN_DEFAULT_ALIGN_BYTES==0) || EIGEN_MALLOC_ALREADY_ALIGNED
179 
180  EIGEN_USING_STD(malloc)
181  result = malloc(size);
182 
183  #if EIGEN_DEFAULT_ALIGN_BYTES==16
184  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.");
185  #endif
186  #else
187  result = handmade_aligned_malloc(size);
188  #endif
189 
190  if(!result && size)
191  throw_std_bad_alloc();
192 
193  return result;
194 }
195 
197 EIGEN_DEVICE_FUNC inline void aligned_free(void *ptr)
198 {
199  #if (EIGEN_DEFAULT_ALIGN_BYTES==0) || EIGEN_MALLOC_ALREADY_ALIGNED
200 
201  EIGEN_USING_STD(free)
202  free(ptr);
203 
204  #else
205  handmade_aligned_free(ptr);
206  #endif
207 }
208 
214 inline void* aligned_realloc(void *ptr, std::size_t new_size, std::size_t old_size)
215 {
216  EIGEN_UNUSED_VARIABLE(old_size);
217 
218  void *result;
219 #if (EIGEN_DEFAULT_ALIGN_BYTES==0) || EIGEN_MALLOC_ALREADY_ALIGNED
220  result = std::realloc(ptr,new_size);
221 #else
222  result = handmade_aligned_realloc(ptr,new_size,old_size);
223 #endif
224 
225  if (!result && new_size)
226  throw_std_bad_alloc();
227 
228  return result;
229 }
230 
231 /*****************************************************************************
232 *** Implementation of conditionally aligned functions ***
233 *****************************************************************************/
234 
238 template<bool Align> EIGEN_DEVICE_FUNC inline void* conditional_aligned_malloc(std::size_t size)
239 {
240  return aligned_malloc(size);
241 }
242 
243 template<> EIGEN_DEVICE_FUNC inline void* conditional_aligned_malloc<false>(std::size_t size)
244 {
245  check_that_malloc_is_allowed();
246 
247  EIGEN_USING_STD(malloc)
248  void *result = malloc(size);
249 
250  if(!result && size)
251  throw_std_bad_alloc();
252  return result;
253 }
254 
256 template<bool Align> EIGEN_DEVICE_FUNC inline void conditional_aligned_free(void *ptr)
257 {
258  aligned_free(ptr);
259 }
260 
261 template<> EIGEN_DEVICE_FUNC inline void conditional_aligned_free<false>(void *ptr)
262 {
263  EIGEN_USING_STD(free)
264  free(ptr);
265 }
266 
267 template<bool Align> inline void* conditional_aligned_realloc(void* ptr, std::size_t new_size, std::size_t old_size)
268 {
269  return aligned_realloc(ptr, new_size, old_size);
270 }
271 
272 template<> inline void* conditional_aligned_realloc<false>(void* ptr, std::size_t new_size, std::size_t)
273 {
274  return std::realloc(ptr, new_size);
275 }
276 
277 /*****************************************************************************
278 *** Construction/destruction of array elements ***
279 *****************************************************************************/
280 
284 template<typename T> EIGEN_DEVICE_FUNC inline void destruct_elements_of_array(T *ptr, std::size_t size)
285 {
286  // always destruct an array starting from the end.
287  if(ptr)
288  while(size) ptr[--size].~T();
289 }
290 
294 template<typename T> EIGEN_DEVICE_FUNC inline T* construct_elements_of_array(T *ptr, std::size_t size)
295 {
296  std::size_t i;
297  EIGEN_TRY
298  {
299  for (i = 0; i < size; ++i) ::new (ptr + i) T;
300  return ptr;
301  }
302  EIGEN_CATCH(...)
303  {
304  destruct_elements_of_array(ptr, i);
305  EIGEN_THROW;
306  }
307  return NULL;
308 }
309 
310 /*****************************************************************************
311 *** Implementation of aligned new/delete-like functions ***
312 *****************************************************************************/
313 
314 template<typename T>
315 EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE void check_size_for_overflow(std::size_t size)
316 {
317  if(size > std::size_t(-1) / sizeof(T))
318  throw_std_bad_alloc();
319 }
320 
325 template<typename T> EIGEN_DEVICE_FUNC inline T* aligned_new(std::size_t size)
326 {
327  check_size_for_overflow<T>(size);
328  T *result = reinterpret_cast<T*>(aligned_malloc(sizeof(T)*size));
329  EIGEN_TRY
330  {
331  return construct_elements_of_array(result, size);
332  }
333  EIGEN_CATCH(...)
334  {
335  aligned_free(result);
336  EIGEN_THROW;
337  }
338  return result;
339 }
340 
341 template<typename T, bool Align> EIGEN_DEVICE_FUNC inline T* conditional_aligned_new(std::size_t size)
342 {
343  check_size_for_overflow<T>(size);
344  T *result = reinterpret_cast<T*>(conditional_aligned_malloc<Align>(sizeof(T)*size));
345  EIGEN_TRY
346  {
347  return construct_elements_of_array(result, size);
348  }
349  EIGEN_CATCH(...)
350  {
351  conditional_aligned_free<Align>(result);
352  EIGEN_THROW;
353  }
354  return result;
355 }
356 
360 template<typename T> EIGEN_DEVICE_FUNC inline void aligned_delete(T *ptr, std::size_t size)
361 {
362  destruct_elements_of_array<T>(ptr, size);
363  Eigen::internal::aligned_free(ptr);
364 }
365 
369 template<typename T, bool Align> EIGEN_DEVICE_FUNC inline void conditional_aligned_delete(T *ptr, std::size_t size)
370 {
371  destruct_elements_of_array<T>(ptr, size);
372  conditional_aligned_free<Align>(ptr);
373 }
374 
375 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)
376 {
377  check_size_for_overflow<T>(new_size);
378  check_size_for_overflow<T>(old_size);
379  if(new_size < old_size)
380  destruct_elements_of_array(pts+new_size, old_size-new_size);
381  T *result = reinterpret_cast<T*>(conditional_aligned_realloc<Align>(reinterpret_cast<void*>(pts), sizeof(T)*new_size, sizeof(T)*old_size));
382  if(new_size > old_size)
383  {
384  EIGEN_TRY
385  {
386  construct_elements_of_array(result+old_size, new_size-old_size);
387  }
388  EIGEN_CATCH(...)
389  {
390  conditional_aligned_free<Align>(result);
391  EIGEN_THROW;
392  }
393  }
394  return result;
395 }
396 
397 
398 template<typename T, bool Align> EIGEN_DEVICE_FUNC inline T* conditional_aligned_new_auto(std::size_t size)
399 {
400  if(size==0)
401  return 0; // short-cut. Also fixes Bug 884
402  check_size_for_overflow<T>(size);
403  T *result = reinterpret_cast<T*>(conditional_aligned_malloc<Align>(sizeof(T)*size));
404  if(NumTraits<T>::RequireInitialization)
405  {
406  EIGEN_TRY
407  {
408  construct_elements_of_array(result, size);
409  }
410  EIGEN_CATCH(...)
411  {
412  conditional_aligned_free<Align>(result);
413  EIGEN_THROW;
414  }
415  }
416  return result;
417 }
418 
419 template<typename T, bool Align> inline T* conditional_aligned_realloc_new_auto(T* pts, std::size_t new_size, std::size_t old_size)
420 {
421  check_size_for_overflow<T>(new_size);
422  check_size_for_overflow<T>(old_size);
423  if(NumTraits<T>::RequireInitialization && (new_size < old_size))
424  destruct_elements_of_array(pts+new_size, old_size-new_size);
425  T *result = reinterpret_cast<T*>(conditional_aligned_realloc<Align>(reinterpret_cast<void*>(pts), sizeof(T)*new_size, sizeof(T)*old_size));
426  if(NumTraits<T>::RequireInitialization && (new_size > old_size))
427  {
428  EIGEN_TRY
429  {
430  construct_elements_of_array(result+old_size, new_size-old_size);
431  }
432  EIGEN_CATCH(...)
433  {
434  conditional_aligned_free<Align>(result);
435  EIGEN_THROW;
436  }
437  }
438  return result;
439 }
440 
441 template<typename T, bool Align> EIGEN_DEVICE_FUNC inline void conditional_aligned_delete_auto(T *ptr, std::size_t size)
442 {
443  if(NumTraits<T>::RequireInitialization)
444  destruct_elements_of_array<T>(ptr, size);
445  conditional_aligned_free<Align>(ptr);
446 }
447 
448 /****************************************************************************/
449 
467 template<int Alignment, typename Scalar, typename Index>
468 EIGEN_DEVICE_FUNC inline Index first_aligned(const Scalar* array, Index size)
469 {
470  const Index ScalarSize = sizeof(Scalar);
471  const Index AlignmentSize = Alignment / ScalarSize;
472  const Index AlignmentMask = AlignmentSize-1;
473 
474  if(AlignmentSize<=1)
475  {
476  // Either the requested alignment if smaller than a scalar, or it exactly match a 1 scalar
477  // so that all elements of the array have the same alignment.
478  return 0;
479  }
480  else if( (UIntPtr(array) & (sizeof(Scalar)-1)) || (Alignment%ScalarSize)!=0)
481  {
482  // The array is not aligned to the size of a single scalar, or the requested alignment is not a multiple of the scalar size.
483  // Consequently, no element of the array is well aligned.
484  return size;
485  }
486  else
487  {
488  Index first = (AlignmentSize - (Index((UIntPtr(array)/sizeof(Scalar))) & AlignmentMask)) & AlignmentMask;
489  return (first < size) ? first : size;
490  }
491 }
492 
495 template<typename Scalar, typename Index>
496 EIGEN_DEVICE_FUNC inline Index first_default_aligned(const Scalar* array, Index size)
497 {
498  typedef typename packet_traits<Scalar>::type DefaultPacketType;
499  return first_aligned<unpacket_traits<DefaultPacketType>::alignment>(array, size);
500 }
501 
504 template<typename Index>
505 inline Index first_multiple(Index size, Index base)
506 {
507  return ((size+base-1)/base)*base;
508 }
509 
510 // std::copy is much slower than memcpy, so let's introduce a smart_copy which
511 // use memcpy on trivial types, i.e., on types that does not require an initialization ctor.
512 template<typename T, bool UseMemcpy> struct smart_copy_helper;
513 
514 template<typename T> EIGEN_DEVICE_FUNC void smart_copy(const T* start, const T* end, T* target)
515 {
516  smart_copy_helper<T,!NumTraits<T>::RequireInitialization>::run(start, end, target);
517 }
518 
519 template<typename T> struct smart_copy_helper<T,true> {
520  EIGEN_DEVICE_FUNC static inline void run(const T* start, const T* end, T* target)
521  {
522  IntPtr size = IntPtr(end)-IntPtr(start);
523  if(size==0) return;
524  eigen_internal_assert(start!=0 && end!=0 && target!=0);
525  EIGEN_USING_STD(memcpy)
526  memcpy(target, start, size);
527  }
528 };
529 
530 template<typename T> struct smart_copy_helper<T,false> {
531  EIGEN_DEVICE_FUNC static inline void run(const T* start, const T* end, T* target)
532  { std::copy(start, end, target); }
533 };
534 
535 // intelligent memmove. falls back to std::memmove for POD types, uses std::copy otherwise.
536 template<typename T, bool UseMemmove> struct smart_memmove_helper;
537 
538 template<typename T> void smart_memmove(const T* start, const T* end, T* target)
539 {
540  smart_memmove_helper<T,!NumTraits<T>::RequireInitialization>::run(start, end, target);
541 }
542 
543 template<typename T> struct smart_memmove_helper<T,true> {
544  static inline void run(const T* start, const T* end, T* target)
545  {
546  IntPtr size = IntPtr(end)-IntPtr(start);
547  if(size==0) return;
548  eigen_internal_assert(start!=0 && end!=0 && target!=0);
549  std::memmove(target, start, size);
550  }
551 };
552 
553 template<typename T> struct smart_memmove_helper<T,false> {
554  static inline void run(const T* start, const T* end, T* target)
555  {
556  if (UIntPtr(target) < UIntPtr(start))
557  {
558  std::copy(start, end, target);
559  }
560  else
561  {
562  std::ptrdiff_t count = (std::ptrdiff_t(end)-std::ptrdiff_t(start)) / sizeof(T);
563  std::copy_backward(start, end, target + count);
564  }
565  }
566 };
567 
568 
569 /*****************************************************************************
570 *** Implementation of runtime stack allocation (falling back to malloc) ***
571 *****************************************************************************/
572 
573 // you can overwrite Eigen's default behavior regarding alloca by defining EIGEN_ALLOCA
574 // to the appropriate stack allocation function
575 #if ! defined EIGEN_ALLOCA && ! defined EIGEN_GPU_COMPILE_PHASE
576  #if EIGEN_OS_LINUX || EIGEN_OS_MAC || (defined alloca)
577  #define EIGEN_ALLOCA alloca
578  #elif EIGEN_COMP_MSVC
579  #define EIGEN_ALLOCA _alloca
580  #endif
581 #endif
582 
583 // With clang -Oz -mthumb, alloca changes the stack pointer in a way that is
584 // not allowed in Thumb2. -DEIGEN_STACK_ALLOCATION_LIMIT=0 doesn't work because
585 // the compiler still emits bad code because stack allocation checks use "<=".
586 // TODO: Eliminate after https://bugs.llvm.org/show_bug.cgi?id=23772
587 // is fixed.
588 #if defined(__clang__) && defined(__thumb__)
589  #undef EIGEN_ALLOCA
590 #endif
591 
592 // This helper class construct the allocated memory, and takes care of destructing and freeing the handled data
593 // at destruction time. In practice this helper class is mainly useful to avoid memory leak in case of exceptions.
594 template<typename T> class aligned_stack_memory_handler : noncopyable
595 {
596  public:
597  /* Creates a stack_memory_handler responsible for the buffer \a ptr of size \a size.
598  * Note that \a ptr can be 0 regardless of the other parameters.
599  * This constructor takes care of constructing/initializing the elements of the buffer if required by the scalar type T (see NumTraits<T>::RequireInitialization).
600  * In this case, the buffer elements will also be destructed when this handler will be destructed.
601  * Finally, if \a dealloc is true, then the pointer \a ptr is freed.
602  **/
603  EIGEN_DEVICE_FUNC
604  aligned_stack_memory_handler(T* ptr, std::size_t size, bool dealloc)
605  : m_ptr(ptr), m_size(size), m_deallocate(dealloc)
606  {
607  if(NumTraits<T>::RequireInitialization && m_ptr)
608  Eigen::internal::construct_elements_of_array(m_ptr, size);
609  }
610  EIGEN_DEVICE_FUNC
611  ~aligned_stack_memory_handler()
612  {
613  if(NumTraits<T>::RequireInitialization && m_ptr)
614  Eigen::internal::destruct_elements_of_array<T>(m_ptr, m_size);
615  if(m_deallocate)
616  Eigen::internal::aligned_free(m_ptr);
617  }
618  protected:
619  T* m_ptr;
620  std::size_t m_size;
621  bool m_deallocate;
622 };
623 
624 #ifdef EIGEN_ALLOCA
625 
626 template<typename Xpr, int NbEvaluations,
627  bool MapExternalBuffer = nested_eval<Xpr,NbEvaluations>::Evaluate && Xpr::MaxSizeAtCompileTime==Dynamic
628  >
629 struct local_nested_eval_wrapper
630 {
631  static const bool NeedExternalBuffer = false;
632  typedef typename Xpr::Scalar Scalar;
633  typedef typename nested_eval<Xpr,NbEvaluations>::type ObjectType;
634  ObjectType object;
635 
636  EIGEN_DEVICE_FUNC
637  local_nested_eval_wrapper(const Xpr& xpr, Scalar* ptr) : object(xpr)
638  {
639  EIGEN_UNUSED_VARIABLE(ptr);
640  eigen_internal_assert(ptr==0);
641  }
642 };
643 
644 template<typename Xpr, int NbEvaluations>
645 struct local_nested_eval_wrapper<Xpr,NbEvaluations,true>
646 {
647  static const bool NeedExternalBuffer = true;
648  typedef typename Xpr::Scalar Scalar;
649  typedef typename plain_object_eval<Xpr>::type PlainObject;
650  typedef Map<PlainObject,EIGEN_DEFAULT_ALIGN_BYTES> ObjectType;
651  ObjectType object;
652 
653  EIGEN_DEVICE_FUNC
654  local_nested_eval_wrapper(const Xpr& xpr, Scalar* ptr)
655  : object(ptr==0 ? reinterpret_cast<Scalar*>(Eigen::internal::aligned_malloc(sizeof(Scalar)*xpr.size())) : ptr, xpr.rows(), xpr.cols()),
656  m_deallocate(ptr==0)
657  {
658  if(NumTraits<Scalar>::RequireInitialization && object.data())
659  Eigen::internal::construct_elements_of_array(object.data(), object.size());
660  object = xpr;
661  }
662 
663  EIGEN_DEVICE_FUNC
664  ~local_nested_eval_wrapper()
665  {
666  if(NumTraits<Scalar>::RequireInitialization && object.data())
667  Eigen::internal::destruct_elements_of_array(object.data(), object.size());
668  if(m_deallocate)
669  Eigen::internal::aligned_free(object.data());
670  }
671 
672 private:
673  bool m_deallocate;
674 };
675 
676 #endif // EIGEN_ALLOCA
677 
678 template<typename T> class scoped_array : noncopyable
679 {
680  T* m_ptr;
681 public:
682  explicit scoped_array(std::ptrdiff_t size)
683  {
684  m_ptr = new T[size];
685  }
686  ~scoped_array()
687  {
688  delete[] m_ptr;
689  }
690  T& operator[](std::ptrdiff_t i) { return m_ptr[i]; }
691  const T& operator[](std::ptrdiff_t i) const { return m_ptr[i]; }
692  T* &ptr() { return m_ptr; }
693  const T* ptr() const { return m_ptr; }
694  operator const T*() const { return m_ptr; }
695 };
696 
697 template<typename T> void swap(scoped_array<T> &a,scoped_array<T> &b)
698 {
699  std::swap(a.ptr(),b.ptr());
700 }
701 
702 } // end namespace internal
703 
729 #ifdef EIGEN_ALLOCA
730 
731  #if EIGEN_DEFAULT_ALIGN_BYTES>0
732  // We always manually re-align the result of EIGEN_ALLOCA.
733  // If alloca is already aligned, the compiler should be smart enough to optimize away the re-alignment.
734  #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)))
735  #else
736  #define EIGEN_ALIGNED_ALLOCA(SIZE) EIGEN_ALLOCA(SIZE)
737  #endif
738 
739  #define ei_declare_aligned_stack_constructed_variable(TYPE,NAME,SIZE,BUFFER) \
740  Eigen::internal::check_size_for_overflow<TYPE>(SIZE); \
741  TYPE* NAME = (BUFFER)!=0 ? (BUFFER) \
742  : reinterpret_cast<TYPE*>( \
743  (sizeof(TYPE)*SIZE<=EIGEN_STACK_ALLOCATION_LIMIT) ? EIGEN_ALIGNED_ALLOCA(sizeof(TYPE)*SIZE) \
744  : Eigen::internal::aligned_malloc(sizeof(TYPE)*SIZE) ); \
745  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)
746 
747 
748  #define ei_declare_local_nested_eval(XPR_T,XPR,N,NAME) \
749  Eigen::internal::local_nested_eval_wrapper<XPR_T,N> EIGEN_CAT(NAME,_wrapper)(XPR, reinterpret_cast<typename XPR_T::Scalar*>( \
750  ( (Eigen::internal::local_nested_eval_wrapper<XPR_T,N>::NeedExternalBuffer) && ((sizeof(typename XPR_T::Scalar)*XPR.size())<=EIGEN_STACK_ALLOCATION_LIMIT) ) \
751  ? EIGEN_ALIGNED_ALLOCA( sizeof(typename XPR_T::Scalar)*XPR.size() ) : 0 ) ) ; \
752  typename Eigen::internal::local_nested_eval_wrapper<XPR_T,N>::ObjectType NAME(EIGEN_CAT(NAME,_wrapper).object)
753 
754 #else
755 
756  #define ei_declare_aligned_stack_constructed_variable(TYPE,NAME,SIZE,BUFFER) \
757  Eigen::internal::check_size_for_overflow<TYPE>(SIZE); \
758  TYPE* NAME = (BUFFER)!=0 ? BUFFER : reinterpret_cast<TYPE*>(Eigen::internal::aligned_malloc(sizeof(TYPE)*SIZE)); \
759  Eigen::internal::aligned_stack_memory_handler<TYPE> EIGEN_CAT(NAME,_stack_memory_destructor)((BUFFER)==0 ? NAME : 0,SIZE,true)
760 
761 
762 #define ei_declare_local_nested_eval(XPR_T,XPR,N,NAME) typename Eigen::internal::nested_eval<XPR_T,N>::type NAME(XPR)
763 
764 #endif
765 
766 
767 /*****************************************************************************
768 *** Implementation of EIGEN_MAKE_ALIGNED_OPERATOR_NEW [_IF] ***
769 *****************************************************************************/
770 
771 #if EIGEN_HAS_CXX17_OVERALIGN
772 
773 // C++17 -> no need to bother about alignment anymore :)
774 
775 #define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_NOTHROW(NeedsToAlign)
776 #define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(NeedsToAlign)
777 #define EIGEN_MAKE_ALIGNED_OPERATOR_NEW
778 #define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF_VECTORIZABLE_FIXED_SIZE(Scalar,Size)
779 
780 #else
781 
782 #if EIGEN_MAX_ALIGN_BYTES!=0
783  #define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_NOTHROW(NeedsToAlign) \
784  void* operator new(std::size_t size, const std::nothrow_t&) EIGEN_NO_THROW { \
785  EIGEN_TRY { return Eigen::internal::conditional_aligned_malloc<NeedsToAlign>(size); } \
786  EIGEN_CATCH (...) { return 0; } \
787  }
788  #define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(NeedsToAlign) \
789  void *operator new(std::size_t size) { \
790  return Eigen::internal::conditional_aligned_malloc<NeedsToAlign>(size); \
791  } \
792  void *operator new[](std::size_t size) { \
793  return Eigen::internal::conditional_aligned_malloc<NeedsToAlign>(size); \
794  } \
795  void operator delete(void * ptr) EIGEN_NO_THROW { Eigen::internal::conditional_aligned_free<NeedsToAlign>(ptr); } \
796  void operator delete[](void * ptr) EIGEN_NO_THROW { Eigen::internal::conditional_aligned_free<NeedsToAlign>(ptr); } \
797  void operator delete(void * ptr, std::size_t /* sz */) EIGEN_NO_THROW { Eigen::internal::conditional_aligned_free<NeedsToAlign>(ptr); } \
798  void operator delete[](void * ptr, std::size_t /* sz */) EIGEN_NO_THROW { Eigen::internal::conditional_aligned_free<NeedsToAlign>(ptr); } \
799  /* in-place new and delete. since (at least afaik) there is no actual */ \
800  /* memory allocated we can safely let the default implementation handle */ \
801  /* this particular case. */ \
802  static void *operator new(std::size_t size, void *ptr) { return ::operator new(size,ptr); } \
803  static void *operator new[](std::size_t size, void* ptr) { return ::operator new[](size,ptr); } \
804  void operator delete(void * memory, void *ptr) EIGEN_NO_THROW { return ::operator delete(memory,ptr); } \
805  void operator delete[](void * memory, void *ptr) EIGEN_NO_THROW { return ::operator delete[](memory,ptr); } \
806  /* nothrow-new (returns zero instead of std::bad_alloc) */ \
807  EIGEN_MAKE_ALIGNED_OPERATOR_NEW_NOTHROW(NeedsToAlign) \
808  void operator delete(void *ptr, const std::nothrow_t&) EIGEN_NO_THROW { \
809  Eigen::internal::conditional_aligned_free<NeedsToAlign>(ptr); \
810  } \
811  typedef void eigen_aligned_operator_new_marker_type;
812 #else
813  #define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(NeedsToAlign)
814 #endif
815 
816 #define EIGEN_MAKE_ALIGNED_OPERATOR_NEW EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(true)
817 #define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF_VECTORIZABLE_FIXED_SIZE(Scalar,Size) \
818  EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(bool( \
819  ((Size)!=Eigen::Dynamic) && \
820  (((EIGEN_MAX_ALIGN_BYTES>=16) && ((sizeof(Scalar)*(Size))%(EIGEN_MAX_ALIGN_BYTES )==0)) || \
821  ((EIGEN_MAX_ALIGN_BYTES>=32) && ((sizeof(Scalar)*(Size))%(EIGEN_MAX_ALIGN_BYTES/2)==0)) || \
822  ((EIGEN_MAX_ALIGN_BYTES>=64) && ((sizeof(Scalar)*(Size))%(EIGEN_MAX_ALIGN_BYTES/4)==0)) )))
823 
824 #endif
825 
826 /****************************************************************************/
827 
852 template<class T>
853 class aligned_allocator : public std::allocator<T>
854 {
855 public:
856  typedef std::size_t size_type;
857  typedef std::ptrdiff_t difference_type;
858  typedef T* pointer;
859  typedef const T* const_pointer;
860  typedef T& reference;
861  typedef const T& const_reference;
862  typedef T value_type;
863 
864  template<class U>
865  struct rebind
866  {
867  typedef aligned_allocator<U> other;
868  };
869 
870  aligned_allocator() : std::allocator<T>() {}
871 
872  aligned_allocator(const aligned_allocator& other) : std::allocator<T>(other) {}
873 
874  template<class U>
875  aligned_allocator(const aligned_allocator<U>& other) : std::allocator<T>(other) {}
876 
877  ~aligned_allocator() {}
878 
879  #if EIGEN_COMP_GNUC_STRICT && EIGEN_GNUC_AT_LEAST(7,0)
880  // In gcc std::allocator::max_size() is bugged making gcc triggers a warning:
881  // eigen/Eigen/src/Core/util/Memory.h:189:12: warning: argument 1 value '18446744073709551612' exceeds maximum object size 9223372036854775807
882  // See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87544
883  size_type max_size() const {
884  return (std::numeric_limits<std::ptrdiff_t>::max)()/sizeof(T);
885  }
886  #endif
887 
888  pointer allocate(size_type num, const void* /*hint*/ = 0)
889  {
890  internal::check_size_for_overflow<T>(num);
891  return static_cast<pointer>( internal::aligned_malloc(num * sizeof(T)) );
892  }
893 
894  void deallocate(pointer p, size_type /*num*/)
895  {
896  internal::aligned_free(p);
897  }
898 };
899 
900 //---------- Cache sizes ----------
901 
902 #if !defined(EIGEN_NO_CPUID)
903 # if EIGEN_COMP_GNUC && EIGEN_ARCH_i386_OR_x86_64
904 # if defined(__PIC__) && EIGEN_ARCH_i386
905  // Case for x86 with PIC
906 # define EIGEN_CPUID(abcd,func,id) \
907  __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));
908 # elif defined(__PIC__) && EIGEN_ARCH_x86_64
909  // Case for x64 with PIC. In theory this is only a problem with recent gcc and with medium or large code model, not with the default small code model.
910  // However, we cannot detect which code model is used, and the xchg overhead is negligible anyway.
911 # define EIGEN_CPUID(abcd,func,id) \
912  __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));
913 # else
914  // Case for x86_64 or x86 w/o PIC
915 # define EIGEN_CPUID(abcd,func,id) \
916  __asm__ __volatile__ ("cpuid": "=a" (abcd[0]), "=b" (abcd[1]), "=c" (abcd[2]), "=d" (abcd[3]) : "0" (func), "2" (id) );
917 # endif
918 # elif EIGEN_COMP_MSVC
919 # if (EIGEN_COMP_MSVC > 1500) && EIGEN_ARCH_i386_OR_x86_64
920 # define EIGEN_CPUID(abcd,func,id) __cpuidex((int*)abcd,func,id)
921 # endif
922 # endif
923 #endif
924 
925 namespace internal {
926 
927 #ifdef EIGEN_CPUID
928 
929 inline bool cpuid_is_vendor(int abcd[4], const int vendor[3])
930 {
931  return abcd[1]==vendor[0] && abcd[3]==vendor[1] && abcd[2]==vendor[2];
932 }
933 
934 inline void queryCacheSizes_intel_direct(int& l1, int& l2, int& l3)
935 {
936  int abcd[4];
937  l1 = l2 = l3 = 0;
938  int cache_id = 0;
939  int cache_type = 0;
940  do {
941  abcd[0] = abcd[1] = abcd[2] = abcd[3] = 0;
942  EIGEN_CPUID(abcd,0x4,cache_id);
943  cache_type = (abcd[0] & 0x0F) >> 0;
944  if(cache_type==1||cache_type==3) // data or unified cache
945  {
946  int cache_level = (abcd[0] & 0xE0) >> 5; // A[7:5]
947  int ways = (abcd[1] & 0xFFC00000) >> 22; // B[31:22]
948  int partitions = (abcd[1] & 0x003FF000) >> 12; // B[21:12]
949  int line_size = (abcd[1] & 0x00000FFF) >> 0; // B[11:0]
950  int sets = (abcd[2]); // C[31:0]
951 
952  int cache_size = (ways+1) * (partitions+1) * (line_size+1) * (sets+1);
953 
954  switch(cache_level)
955  {
956  case 1: l1 = cache_size; break;
957  case 2: l2 = cache_size; break;
958  case 3: l3 = cache_size; break;
959  default: break;
960  }
961  }
962  cache_id++;
963  } while(cache_type>0 && cache_id<16);
964 }
965 
966 inline void queryCacheSizes_intel_codes(int& l1, int& l2, int& l3)
967 {
968  int abcd[4];
969  abcd[0] = abcd[1] = abcd[2] = abcd[3] = 0;
970  l1 = l2 = l3 = 0;
971  EIGEN_CPUID(abcd,0x00000002,0);
972  unsigned char * bytes = reinterpret_cast<unsigned char *>(abcd)+2;
973  bool check_for_p2_core2 = false;
974  for(int i=0; i<14; ++i)
975  {
976  switch(bytes[i])
977  {
978  case 0x0A: l1 = 8; break; // 0Ah data L1 cache, 8 KB, 2 ways, 32 byte lines
979  case 0x0C: l1 = 16; break; // 0Ch data L1 cache, 16 KB, 4 ways, 32 byte lines
980  case 0x0E: l1 = 24; break; // 0Eh data L1 cache, 24 KB, 6 ways, 64 byte lines
981  case 0x10: l1 = 16; break; // 10h data L1 cache, 16 KB, 4 ways, 32 byte lines (IA-64)
982  case 0x15: l1 = 16; break; // 15h code L1 cache, 16 KB, 4 ways, 32 byte lines (IA-64)
983  case 0x2C: l1 = 32; break; // 2Ch data L1 cache, 32 KB, 8 ways, 64 byte lines
984  case 0x30: l1 = 32; break; // 30h code L1 cache, 32 KB, 8 ways, 64 byte lines
985  case 0x60: l1 = 16; break; // 60h data L1 cache, 16 KB, 8 ways, 64 byte lines, sectored
986  case 0x66: l1 = 8; break; // 66h data L1 cache, 8 KB, 4 ways, 64 byte lines, sectored
987  case 0x67: l1 = 16; break; // 67h data L1 cache, 16 KB, 4 ways, 64 byte lines, sectored
988  case 0x68: l1 = 32; break; // 68h data L1 cache, 32 KB, 4 ways, 64 byte lines, sectored
989  case 0x1A: l2 = 96; break; // code and data L2 cache, 96 KB, 6 ways, 64 byte lines (IA-64)
990  case 0x22: l3 = 512; break; // code and data L3 cache, 512 KB, 4 ways (!), 64 byte lines, dual-sectored
991  case 0x23: l3 = 1024; break; // code and data L3 cache, 1024 KB, 8 ways, 64 byte lines, dual-sectored
992  case 0x25: l3 = 2048; break; // code and data L3 cache, 2048 KB, 8 ways, 64 byte lines, dual-sectored
993  case 0x29: l3 = 4096; break; // code and data L3 cache, 4096 KB, 8 ways, 64 byte lines, dual-sectored
994  case 0x39: l2 = 128; break; // code and data L2 cache, 128 KB, 4 ways, 64 byte lines, sectored
995  case 0x3A: l2 = 192; break; // code and data L2 cache, 192 KB, 6 ways, 64 byte lines, sectored
996  case 0x3B: l2 = 128; break; // code and data L2 cache, 128 KB, 2 ways, 64 byte lines, sectored
997  case 0x3C: l2 = 256; break; // code and data L2 cache, 256 KB, 4 ways, 64 byte lines, sectored
998  case 0x3D: l2 = 384; break; // code and data L2 cache, 384 KB, 6 ways, 64 byte lines, sectored
999  case 0x3E: l2 = 512; break; // code and data L2 cache, 512 KB, 4 ways, 64 byte lines, sectored
1000  case 0x40: l2 = 0; break; // no integrated L2 cache (P6 core) or L3 cache (P4 core)
1001  case 0x41: l2 = 128; break; // code and data L2 cache, 128 KB, 4 ways, 32 byte lines
1002  case 0x42: l2 = 256; break; // code and data L2 cache, 256 KB, 4 ways, 32 byte lines
1003  case 0x43: l2 = 512; break; // code and data L2 cache, 512 KB, 4 ways, 32 byte lines
1004  case 0x44: l2 = 1024; break; // code and data L2 cache, 1024 KB, 4 ways, 32 byte lines
1005  case 0x45: l2 = 2048; break; // code and data L2 cache, 2048 KB, 4 ways, 32 byte lines
1006  case 0x46: l3 = 4096; break; // code and data L3 cache, 4096 KB, 4 ways, 64 byte lines
1007  case 0x47: l3 = 8192; break; // code and data L3 cache, 8192 KB, 8 ways, 64 byte lines
1008  case 0x48: l2 = 3072; break; // code and data L2 cache, 3072 KB, 12 ways, 64 byte lines
1009  case 0x49: if(l2!=0) l3 = 4096; else {check_for_p2_core2=true; l3 = l2 = 4096;} break;// code and data L3 cache, 4096 KB, 16 ways, 64 byte lines (P4) or L2 for core2
1010  case 0x4A: l3 = 6144; break; // code and data L3 cache, 6144 KB, 12 ways, 64 byte lines
1011  case 0x4B: l3 = 8192; break; // code and data L3 cache, 8192 KB, 16 ways, 64 byte lines
1012  case 0x4C: l3 = 12288; break; // code and data L3 cache, 12288 KB, 12 ways, 64 byte lines
1013  case 0x4D: l3 = 16384; break; // code and data L3 cache, 16384 KB, 16 ways, 64 byte lines
1014  case 0x4E: l2 = 6144; break; // code and data L2 cache, 6144 KB, 24 ways, 64 byte lines
1015  case 0x78: l2 = 1024; break; // code and data L2 cache, 1024 KB, 4 ways, 64 byte lines
1016  case 0x79: l2 = 128; break; // code and data L2 cache, 128 KB, 8 ways, 64 byte lines, dual-sectored
1017  case 0x7A: l2 = 256; break; // code and data L2 cache, 256 KB, 8 ways, 64 byte lines, dual-sectored
1018  case 0x7B: l2 = 512; break; // code and data L2 cache, 512 KB, 8 ways, 64 byte lines, dual-sectored
1019  case 0x7C: l2 = 1024; break; // code and data L2 cache, 1024 KB, 8 ways, 64 byte lines, dual-sectored
1020  case 0x7D: l2 = 2048; break; // code and data L2 cache, 2048 KB, 8 ways, 64 byte lines
1021  case 0x7E: l2 = 256; break; // code and data L2 cache, 256 KB, 8 ways, 128 byte lines, sect. (IA-64)
1022  case 0x7F: l2 = 512; break; // code and data L2 cache, 512 KB, 2 ways, 64 byte lines
1023  case 0x80: l2 = 512; break; // code and data L2 cache, 512 KB, 8 ways, 64 byte lines
1024  case 0x81: l2 = 128; break; // code and data L2 cache, 128 KB, 8 ways, 32 byte lines
1025  case 0x82: l2 = 256; break; // code and data L2 cache, 256 KB, 8 ways, 32 byte lines
1026  case 0x83: l2 = 512; break; // code and data L2 cache, 512 KB, 8 ways, 32 byte lines
1027  case 0x84: l2 = 1024; break; // code and data L2 cache, 1024 KB, 8 ways, 32 byte lines
1028  case 0x85: l2 = 2048; break; // code and data L2 cache, 2048 KB, 8 ways, 32 byte lines
1029  case 0x86: l2 = 512; break; // code and data L2 cache, 512 KB, 4 ways, 64 byte lines
1030  case 0x87: l2 = 1024; break; // code and data L2 cache, 1024 KB, 8 ways, 64 byte lines
1031  case 0x88: l3 = 2048; break; // code and data L3 cache, 2048 KB, 4 ways, 64 byte lines (IA-64)
1032  case 0x89: l3 = 4096; break; // code and data L3 cache, 4096 KB, 4 ways, 64 byte lines (IA-64)
1033  case 0x8A: l3 = 8192; break; // code and data L3 cache, 8192 KB, 4 ways, 64 byte lines (IA-64)
1034  case 0x8D: l3 = 3072; break; // code and data L3 cache, 3072 KB, 12 ways, 128 byte lines (IA-64)
1035 
1036  default: break;
1037  }
1038  }
1039  if(check_for_p2_core2 && l2 == l3)
1040  l3 = 0;
1041  l1 *= 1024;
1042  l2 *= 1024;
1043  l3 *= 1024;
1044 }
1045 
1046 inline void queryCacheSizes_intel(int& l1, int& l2, int& l3, int max_std_funcs)
1047 {
1048  if(max_std_funcs>=4)
1049  queryCacheSizes_intel_direct(l1,l2,l3);
1050  else
1051  queryCacheSizes_intel_codes(l1,l2,l3);
1052 }
1053 
1054 inline void queryCacheSizes_amd(int& l1, int& l2, int& l3)
1055 {
1056  int abcd[4];
1057  abcd[0] = abcd[1] = abcd[2] = abcd[3] = 0;
1058  EIGEN_CPUID(abcd,0x80000005,0);
1059  l1 = (abcd[2] >> 24) * 1024; // C[31:24] = L1 size in KB
1060  abcd[0] = abcd[1] = abcd[2] = abcd[3] = 0;
1061  EIGEN_CPUID(abcd,0x80000006,0);
1062  l2 = (abcd[2] >> 16) * 1024; // C[31;16] = l2 cache size in KB
1063  l3 = ((abcd[3] & 0xFFFC000) >> 18) * 512 * 1024; // D[31;18] = l3 cache size in 512KB
1064 }
1065 #endif
1066 
1069 inline void queryCacheSizes(int& l1, int& l2, int& l3)
1070 {
1071  #ifdef EIGEN_CPUID
1072  int abcd[4];
1073  const int GenuineIntel[] = {0x756e6547, 0x49656e69, 0x6c65746e};
1074  const int AuthenticAMD[] = {0x68747541, 0x69746e65, 0x444d4163};
1075  const int AMDisbetter_[] = {0x69444d41, 0x74656273, 0x21726574}; // "AMDisbetter!"
1076 
1077  // identify the CPU vendor
1078  EIGEN_CPUID(abcd,0x0,0);
1079  int max_std_funcs = abcd[1];
1080  if(cpuid_is_vendor(abcd,GenuineIntel))
1081  queryCacheSizes_intel(l1,l2,l3,max_std_funcs);
1082  else if(cpuid_is_vendor(abcd,AuthenticAMD) || cpuid_is_vendor(abcd,AMDisbetter_))
1083  queryCacheSizes_amd(l1,l2,l3);
1084  else
1085  // by default let's use Intel's API
1086  queryCacheSizes_intel(l1,l2,l3,max_std_funcs);
1087 
1088  // here is the list of other vendors:
1089 // ||cpuid_is_vendor(abcd,"VIA VIA VIA ")
1090 // ||cpuid_is_vendor(abcd,"CyrixInstead")
1091 // ||cpuid_is_vendor(abcd,"CentaurHauls")
1092 // ||cpuid_is_vendor(abcd,"GenuineTMx86")
1093 // ||cpuid_is_vendor(abcd,"TransmetaCPU")
1094 // ||cpuid_is_vendor(abcd,"RiseRiseRise")
1095 // ||cpuid_is_vendor(abcd,"Geode by NSC")
1096 // ||cpuid_is_vendor(abcd,"SiS SiS SiS ")
1097 // ||cpuid_is_vendor(abcd,"UMC UMC UMC ")
1098 // ||cpuid_is_vendor(abcd,"NexGenDriven")
1099  #else
1100  l1 = l2 = l3 = -1;
1101  #endif
1102 }
1103 
1106 inline int queryL1CacheSize()
1107 {
1108  int l1(-1), l2, l3;
1109  queryCacheSizes(l1,l2,l3);
1110  return l1;
1111 }
1112 
1115 inline int queryTopLevelCacheSize()
1116 {
1117  int l1, l2(-1), l3(-1);
1118  queryCacheSizes(l1,l2,l3);
1119  return (std::max)(l2,l3);
1120 }
1121 
1122 } // end namespace internal
1123 
1124 } // end namespace Eigen
1125 
1126 #endif // EIGEN_MEMORY_H
Namespace containing all symbols from the Eigen library.
Definition: Core:129
STL compatible allocator to use with types requiring a non standrad alignment.
Definition: Memory.h:853
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:42
Definition: Eigen_Colamd.h:50
const int Dynamic
Definition: Constants.h:21