1 | // This file is part of Eigen, a lightweight C++ template library
|
---|
2 | // for linear algebra.
|
---|
3 | //
|
---|
4 | // Copyright (C) 2008-2010 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 | //
|
---|
10 | // This Source Code Form is subject to the terms of the Mozilla
|
---|
11 | // Public License v. 2.0. If a copy of the MPL was not distributed
|
---|
12 | // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
---|
13 |
|
---|
14 |
|
---|
15 | /*****************************************************************************
|
---|
16 | *** Platform checks for aligned malloc functions ***
|
---|
17 | *****************************************************************************/
|
---|
18 |
|
---|
19 | #ifndef EIGEN_MEMORY_H
|
---|
20 | #define EIGEN_MEMORY_H
|
---|
21 |
|
---|
22 | #ifndef EIGEN_MALLOC_ALREADY_ALIGNED
|
---|
23 |
|
---|
24 | // Try to determine automatically if malloc is already aligned.
|
---|
25 |
|
---|
26 | // On 64-bit systems, glibc's malloc returns 16-byte-aligned pointers, see:
|
---|
27 | // http://www.gnu.org/s/libc/manual/html_node/Aligned-Memory-Blocks.html
|
---|
28 | // This is true at least since glibc 2.8.
|
---|
29 | // This leaves the question how to detect 64-bit. According to this document,
|
---|
30 | // http://gcc.fyxm.net/summit/2003/Porting%20to%2064%20bit.pdf
|
---|
31 | // page 114, "[The] LP64 model [...] is used by all 64-bit UNIX ports" so it's indeed
|
---|
32 | // quite safe, at least within the context of glibc, to equate 64-bit with LP64.
|
---|
33 | #if defined(__GLIBC__) && ((__GLIBC__>=2 && __GLIBC_MINOR__ >= 8) || __GLIBC__>2) \
|
---|
34 | && defined(__LP64__) && ! defined( __SANITIZE_ADDRESS__ )
|
---|
35 | #define EIGEN_GLIBC_MALLOC_ALREADY_ALIGNED 1
|
---|
36 | #else
|
---|
37 | #define EIGEN_GLIBC_MALLOC_ALREADY_ALIGNED 0
|
---|
38 | #endif
|
---|
39 |
|
---|
40 | // FreeBSD 6 seems to have 16-byte aligned malloc
|
---|
41 | // See http://svn.freebsd.org/viewvc/base/stable/6/lib/libc/stdlib/malloc.c?view=markup
|
---|
42 | // FreeBSD 7 seems to have 16-byte aligned malloc except on ARM and MIPS architectures
|
---|
43 | // See http://svn.freebsd.org/viewvc/base/stable/7/lib/libc/stdlib/malloc.c?view=markup
|
---|
44 | #if defined(__FreeBSD__) && !defined(__arm__) && !defined(__mips__)
|
---|
45 | #define EIGEN_FREEBSD_MALLOC_ALREADY_ALIGNED 1
|
---|
46 | #else
|
---|
47 | #define EIGEN_FREEBSD_MALLOC_ALREADY_ALIGNED 0
|
---|
48 | #endif
|
---|
49 |
|
---|
50 | #if defined(__APPLE__) \
|
---|
51 | || defined(_WIN64) \
|
---|
52 | || EIGEN_GLIBC_MALLOC_ALREADY_ALIGNED \
|
---|
53 | || EIGEN_FREEBSD_MALLOC_ALREADY_ALIGNED
|
---|
54 | #define EIGEN_MALLOC_ALREADY_ALIGNED 1
|
---|
55 | #else
|
---|
56 | #define EIGEN_MALLOC_ALREADY_ALIGNED 0
|
---|
57 | #endif
|
---|
58 |
|
---|
59 | #endif
|
---|
60 |
|
---|
61 | // See bug 554 (http://eigen.tuxfamily.org/bz/show_bug.cgi?id=554)
|
---|
62 | // It seems to be unsafe to check _POSIX_ADVISORY_INFO without including unistd.h first.
|
---|
63 | // Currently, let's include it only on unix systems:
|
---|
64 | #if defined(__unix__) || defined(__unix)
|
---|
65 | #include <unistd.h>
|
---|
66 | #if ((defined __QNXNTO__) || (defined _GNU_SOURCE) || (defined __PGI) || ((defined _XOPEN_SOURCE) && (_XOPEN_SOURCE >= 600))) && (defined _POSIX_ADVISORY_INFO) && (_POSIX_ADVISORY_INFO > 0)
|
---|
67 | #define EIGEN_HAS_POSIX_MEMALIGN 1
|
---|
68 | #endif
|
---|
69 | #endif
|
---|
70 |
|
---|
71 | #ifndef EIGEN_HAS_POSIX_MEMALIGN
|
---|
72 | #define EIGEN_HAS_POSIX_MEMALIGN 0
|
---|
73 | #endif
|
---|
74 |
|
---|
75 | #ifdef EIGEN_VECTORIZE_SSE
|
---|
76 | #define EIGEN_HAS_MM_MALLOC 1
|
---|
77 | #else
|
---|
78 | #define EIGEN_HAS_MM_MALLOC 0
|
---|
79 | #endif
|
---|
80 |
|
---|
81 | namespace Eigen {
|
---|
82 |
|
---|
83 | namespace internal {
|
---|
84 |
|
---|
85 | inline void throw_std_bad_alloc()
|
---|
86 | {
|
---|
87 | #ifdef EIGEN_EXCEPTIONS
|
---|
88 | throw std::bad_alloc();
|
---|
89 | #else
|
---|
90 | std::size_t huge = -1;
|
---|
91 | new int[huge];
|
---|
92 | #endif
|
---|
93 | }
|
---|
94 |
|
---|
95 | /*****************************************************************************
|
---|
96 | *** Implementation of handmade aligned functions ***
|
---|
97 | *****************************************************************************/
|
---|
98 |
|
---|
99 | /* ----- Hand made implementations of aligned malloc/free and realloc ----- */
|
---|
100 |
|
---|
101 | /** \internal Like malloc, but the returned pointer is guaranteed to be 16-byte aligned.
|
---|
102 | * Fast, but wastes 16 additional bytes of memory. Does not throw any exception.
|
---|
103 | */
|
---|
104 | inline void* handmade_aligned_malloc(std::size_t size)
|
---|
105 | {
|
---|
106 | void *original = std::malloc(size+16);
|
---|
107 | if (original == 0) return 0;
|
---|
108 | void *aligned = reinterpret_cast<void*>((reinterpret_cast<std::size_t>(original) & ~(std::size_t(15))) + 16);
|
---|
109 | *(reinterpret_cast<void**>(aligned) - 1) = original;
|
---|
110 | return aligned;
|
---|
111 | }
|
---|
112 |
|
---|
113 | /** \internal Frees memory allocated with handmade_aligned_malloc */
|
---|
114 | inline void handmade_aligned_free(void *ptr)
|
---|
115 | {
|
---|
116 | if (ptr) std::free(*(reinterpret_cast<void**>(ptr) - 1));
|
---|
117 | }
|
---|
118 |
|
---|
119 | /** \internal
|
---|
120 | * \brief Reallocates aligned memory.
|
---|
121 | * Since we know that our handmade version is based on std::realloc
|
---|
122 | * we can use std::realloc to implement efficient reallocation.
|
---|
123 | */
|
---|
124 | inline void* handmade_aligned_realloc(void* ptr, std::size_t size, std::size_t = 0)
|
---|
125 | {
|
---|
126 | if (ptr == 0) return handmade_aligned_malloc(size);
|
---|
127 | void *original = *(reinterpret_cast<void**>(ptr) - 1);
|
---|
128 | std::ptrdiff_t previous_offset = static_cast<char *>(ptr)-static_cast<char *>(original);
|
---|
129 | original = std::realloc(original,size+16);
|
---|
130 | if (original == 0) return 0;
|
---|
131 | void *aligned = reinterpret_cast<void*>((reinterpret_cast<std::size_t>(original) & ~(std::size_t(15))) + 16);
|
---|
132 | void *previous_aligned = static_cast<char *>(original)+previous_offset;
|
---|
133 | if(aligned!=previous_aligned)
|
---|
134 | std::memmove(aligned, previous_aligned, size);
|
---|
135 |
|
---|
136 | *(reinterpret_cast<void**>(aligned) - 1) = original;
|
---|
137 | return aligned;
|
---|
138 | }
|
---|
139 |
|
---|
140 | /*****************************************************************************
|
---|
141 | *** Implementation of generic aligned realloc (when no realloc can be used)***
|
---|
142 | *****************************************************************************/
|
---|
143 |
|
---|
144 | void* aligned_malloc(std::size_t size);
|
---|
145 | void aligned_free(void *ptr);
|
---|
146 |
|
---|
147 | /** \internal
|
---|
148 | * \brief Reallocates aligned memory.
|
---|
149 | * Allows reallocation with aligned ptr types. This implementation will
|
---|
150 | * always create a new memory chunk and copy the old data.
|
---|
151 | */
|
---|
152 | inline void* generic_aligned_realloc(void* ptr, size_t size, size_t old_size)
|
---|
153 | {
|
---|
154 | if (ptr==0)
|
---|
155 | return aligned_malloc(size);
|
---|
156 |
|
---|
157 | if (size==0)
|
---|
158 | {
|
---|
159 | aligned_free(ptr);
|
---|
160 | return 0;
|
---|
161 | }
|
---|
162 |
|
---|
163 | void* newptr = aligned_malloc(size);
|
---|
164 | if (newptr == 0)
|
---|
165 | {
|
---|
166 | #ifdef EIGEN_HAS_ERRNO
|
---|
167 | errno = ENOMEM; // according to the standard
|
---|
168 | #endif
|
---|
169 | return 0;
|
---|
170 | }
|
---|
171 |
|
---|
172 | if (ptr != 0)
|
---|
173 | {
|
---|
174 | std::memcpy(newptr, ptr, (std::min)(size,old_size));
|
---|
175 | aligned_free(ptr);
|
---|
176 | }
|
---|
177 |
|
---|
178 | return newptr;
|
---|
179 | }
|
---|
180 |
|
---|
181 | /*****************************************************************************
|
---|
182 | *** Implementation of portable aligned versions of malloc/free/realloc ***
|
---|
183 | *****************************************************************************/
|
---|
184 |
|
---|
185 | #ifdef EIGEN_NO_MALLOC
|
---|
186 | inline void check_that_malloc_is_allowed()
|
---|
187 | {
|
---|
188 | eigen_assert(false && "heap allocation is forbidden (EIGEN_NO_MALLOC is defined)");
|
---|
189 | }
|
---|
190 | #elif defined EIGEN_RUNTIME_NO_MALLOC
|
---|
191 | inline bool is_malloc_allowed_impl(bool update, bool new_value = false)
|
---|
192 | {
|
---|
193 | static bool value = true;
|
---|
194 | if (update == 1)
|
---|
195 | value = new_value;
|
---|
196 | return value;
|
---|
197 | }
|
---|
198 | inline bool is_malloc_allowed() { return is_malloc_allowed_impl(false); }
|
---|
199 | inline bool set_is_malloc_allowed(bool new_value) { return is_malloc_allowed_impl(true, new_value); }
|
---|
200 | inline void check_that_malloc_is_allowed()
|
---|
201 | {
|
---|
202 | eigen_assert(is_malloc_allowed() && "heap allocation is forbidden (EIGEN_RUNTIME_NO_MALLOC is defined and g_is_malloc_allowed is false)");
|
---|
203 | }
|
---|
204 | #else
|
---|
205 | inline void check_that_malloc_is_allowed()
|
---|
206 | {}
|
---|
207 | #endif
|
---|
208 |
|
---|
209 | /** \internal Allocates \a size bytes. The returned pointer is guaranteed to have 16 bytes alignment.
|
---|
210 | * On allocation error, the returned pointer is null, and std::bad_alloc is thrown.
|
---|
211 | */
|
---|
212 | inline void* aligned_malloc(size_t size)
|
---|
213 | {
|
---|
214 | check_that_malloc_is_allowed();
|
---|
215 |
|
---|
216 | void *result;
|
---|
217 | #if !EIGEN_ALIGN
|
---|
218 | result = std::malloc(size);
|
---|
219 | #elif EIGEN_MALLOC_ALREADY_ALIGNED
|
---|
220 | result = std::malloc(size);
|
---|
221 | #elif EIGEN_HAS_POSIX_MEMALIGN
|
---|
222 | if(posix_memalign(&result, 16, size)) result = 0;
|
---|
223 | #elif EIGEN_HAS_MM_MALLOC
|
---|
224 | result = _mm_malloc(size, 16);
|
---|
225 | #elif defined(_MSC_VER) && (!defined(_WIN32_WCE))
|
---|
226 | result = _aligned_malloc(size, 16);
|
---|
227 | #else
|
---|
228 | result = handmade_aligned_malloc(size);
|
---|
229 | #endif
|
---|
230 |
|
---|
231 | if(!result && size)
|
---|
232 | throw_std_bad_alloc();
|
---|
233 |
|
---|
234 | return result;
|
---|
235 | }
|
---|
236 |
|
---|
237 | /** \internal Frees memory allocated with aligned_malloc. */
|
---|
238 | inline void aligned_free(void *ptr)
|
---|
239 | {
|
---|
240 | #if !EIGEN_ALIGN
|
---|
241 | std::free(ptr);
|
---|
242 | #elif EIGEN_MALLOC_ALREADY_ALIGNED
|
---|
243 | std::free(ptr);
|
---|
244 | #elif EIGEN_HAS_POSIX_MEMALIGN
|
---|
245 | std::free(ptr);
|
---|
246 | #elif EIGEN_HAS_MM_MALLOC
|
---|
247 | _mm_free(ptr);
|
---|
248 | #elif defined(_MSC_VER) && (!defined(_WIN32_WCE))
|
---|
249 | _aligned_free(ptr);
|
---|
250 | #else
|
---|
251 | handmade_aligned_free(ptr);
|
---|
252 | #endif
|
---|
253 | }
|
---|
254 |
|
---|
255 | /**
|
---|
256 | * \internal
|
---|
257 | * \brief Reallocates an aligned block of memory.
|
---|
258 | * \throws std::bad_alloc on allocation failure
|
---|
259 | **/
|
---|
260 | inline void* aligned_realloc(void *ptr, size_t new_size, size_t old_size)
|
---|
261 | {
|
---|
262 | EIGEN_UNUSED_VARIABLE(old_size);
|
---|
263 |
|
---|
264 | void *result;
|
---|
265 | #if !EIGEN_ALIGN
|
---|
266 | result = std::realloc(ptr,new_size);
|
---|
267 | #elif EIGEN_MALLOC_ALREADY_ALIGNED
|
---|
268 | result = std::realloc(ptr,new_size);
|
---|
269 | #elif EIGEN_HAS_POSIX_MEMALIGN
|
---|
270 | result = generic_aligned_realloc(ptr,new_size,old_size);
|
---|
271 | #elif EIGEN_HAS_MM_MALLOC
|
---|
272 | // The defined(_mm_free) is just here to verify that this MSVC version
|
---|
273 | // implements _mm_malloc/_mm_free based on the corresponding _aligned_
|
---|
274 | // functions. This may not always be the case and we just try to be safe.
|
---|
275 | #if defined(_MSC_VER) && (!defined(_WIN32_WCE)) && defined(_mm_free)
|
---|
276 | result = _aligned_realloc(ptr,new_size,16);
|
---|
277 | #else
|
---|
278 | result = generic_aligned_realloc(ptr,new_size,old_size);
|
---|
279 | #endif
|
---|
280 | #elif defined(_MSC_VER) && (!defined(_WIN32_WCE))
|
---|
281 | result = _aligned_realloc(ptr,new_size,16);
|
---|
282 | #else
|
---|
283 | result = handmade_aligned_realloc(ptr,new_size,old_size);
|
---|
284 | #endif
|
---|
285 |
|
---|
286 | if (!result && new_size)
|
---|
287 | throw_std_bad_alloc();
|
---|
288 |
|
---|
289 | return result;
|
---|
290 | }
|
---|
291 |
|
---|
292 | /*****************************************************************************
|
---|
293 | *** Implementation of conditionally aligned functions ***
|
---|
294 | *****************************************************************************/
|
---|
295 |
|
---|
296 | /** \internal Allocates \a size bytes. If Align is true, then the returned ptr is 16-byte-aligned.
|
---|
297 | * On allocation error, the returned pointer is null, and a std::bad_alloc is thrown.
|
---|
298 | */
|
---|
299 | template<bool Align> inline void* conditional_aligned_malloc(size_t size)
|
---|
300 | {
|
---|
301 | return aligned_malloc(size);
|
---|
302 | }
|
---|
303 |
|
---|
304 | template<> inline void* conditional_aligned_malloc<false>(size_t size)
|
---|
305 | {
|
---|
306 | check_that_malloc_is_allowed();
|
---|
307 |
|
---|
308 | void *result = std::malloc(size);
|
---|
309 | if(!result && size)
|
---|
310 | throw_std_bad_alloc();
|
---|
311 | return result;
|
---|
312 | }
|
---|
313 |
|
---|
314 | /** \internal Frees memory allocated with conditional_aligned_malloc */
|
---|
315 | template<bool Align> inline void conditional_aligned_free(void *ptr)
|
---|
316 | {
|
---|
317 | aligned_free(ptr);
|
---|
318 | }
|
---|
319 |
|
---|
320 | template<> inline void conditional_aligned_free<false>(void *ptr)
|
---|
321 | {
|
---|
322 | std::free(ptr);
|
---|
323 | }
|
---|
324 |
|
---|
325 | template<bool Align> inline void* conditional_aligned_realloc(void* ptr, size_t new_size, size_t old_size)
|
---|
326 | {
|
---|
327 | return aligned_realloc(ptr, new_size, old_size);
|
---|
328 | }
|
---|
329 |
|
---|
330 | template<> inline void* conditional_aligned_realloc<false>(void* ptr, size_t new_size, size_t)
|
---|
331 | {
|
---|
332 | return std::realloc(ptr, new_size);
|
---|
333 | }
|
---|
334 |
|
---|
335 | /*****************************************************************************
|
---|
336 | *** Construction/destruction of array elements ***
|
---|
337 | *****************************************************************************/
|
---|
338 |
|
---|
339 | /** \internal Constructs the elements of an array.
|
---|
340 | * The \a size parameter tells on how many objects to call the constructor of T.
|
---|
341 | */
|
---|
342 | template<typename T> inline T* construct_elements_of_array(T *ptr, size_t size)
|
---|
343 | {
|
---|
344 | for (size_t i=0; i < size; ++i) ::new (ptr + i) T;
|
---|
345 | return ptr;
|
---|
346 | }
|
---|
347 |
|
---|
348 | /** \internal Destructs the elements of an array.
|
---|
349 | * The \a size parameters tells on how many objects to call the destructor of T.
|
---|
350 | */
|
---|
351 | template<typename T> inline void destruct_elements_of_array(T *ptr, size_t size)
|
---|
352 | {
|
---|
353 | // always destruct an array starting from the end.
|
---|
354 | if(ptr)
|
---|
355 | while(size) ptr[--size].~T();
|
---|
356 | }
|
---|
357 |
|
---|
358 | /*****************************************************************************
|
---|
359 | *** Implementation of aligned new/delete-like functions ***
|
---|
360 | *****************************************************************************/
|
---|
361 |
|
---|
362 | template<typename T>
|
---|
363 | EIGEN_ALWAYS_INLINE void check_size_for_overflow(size_t size)
|
---|
364 | {
|
---|
365 | if(size > size_t(-1) / sizeof(T))
|
---|
366 | throw_std_bad_alloc();
|
---|
367 | }
|
---|
368 |
|
---|
369 | /** \internal Allocates \a size objects of type T. The returned pointer is guaranteed to have 16 bytes alignment.
|
---|
370 | * On allocation error, the returned pointer is undefined, but a std::bad_alloc is thrown.
|
---|
371 | * The default constructor of T is called.
|
---|
372 | */
|
---|
373 | template<typename T> inline T* aligned_new(size_t size)
|
---|
374 | {
|
---|
375 | check_size_for_overflow<T>(size);
|
---|
376 | T *result = reinterpret_cast<T*>(aligned_malloc(sizeof(T)*size));
|
---|
377 | return construct_elements_of_array(result, size);
|
---|
378 | }
|
---|
379 |
|
---|
380 | template<typename T, bool Align> inline T* conditional_aligned_new(size_t size)
|
---|
381 | {
|
---|
382 | check_size_for_overflow<T>(size);
|
---|
383 | T *result = reinterpret_cast<T*>(conditional_aligned_malloc<Align>(sizeof(T)*size));
|
---|
384 | return construct_elements_of_array(result, size);
|
---|
385 | }
|
---|
386 |
|
---|
387 | /** \internal Deletes objects constructed with aligned_new
|
---|
388 | * The \a size parameters tells on how many objects to call the destructor of T.
|
---|
389 | */
|
---|
390 | template<typename T> inline void aligned_delete(T *ptr, size_t size)
|
---|
391 | {
|
---|
392 | destruct_elements_of_array<T>(ptr, size);
|
---|
393 | aligned_free(ptr);
|
---|
394 | }
|
---|
395 |
|
---|
396 | /** \internal Deletes objects constructed with conditional_aligned_new
|
---|
397 | * The \a size parameters tells on how many objects to call the destructor of T.
|
---|
398 | */
|
---|
399 | template<typename T, bool Align> inline void conditional_aligned_delete(T *ptr, size_t size)
|
---|
400 | {
|
---|
401 | destruct_elements_of_array<T>(ptr, size);
|
---|
402 | conditional_aligned_free<Align>(ptr);
|
---|
403 | }
|
---|
404 |
|
---|
405 | template<typename T, bool Align> inline T* conditional_aligned_realloc_new(T* pts, size_t new_size, size_t old_size)
|
---|
406 | {
|
---|
407 | check_size_for_overflow<T>(new_size);
|
---|
408 | check_size_for_overflow<T>(old_size);
|
---|
409 | if(new_size < old_size)
|
---|
410 | destruct_elements_of_array(pts+new_size, old_size-new_size);
|
---|
411 | T *result = reinterpret_cast<T*>(conditional_aligned_realloc<Align>(reinterpret_cast<void*>(pts), sizeof(T)*new_size, sizeof(T)*old_size));
|
---|
412 | if(new_size > old_size)
|
---|
413 | construct_elements_of_array(result+old_size, new_size-old_size);
|
---|
414 | return result;
|
---|
415 | }
|
---|
416 |
|
---|
417 |
|
---|
418 | template<typename T, bool Align> inline T* conditional_aligned_new_auto(size_t size)
|
---|
419 | {
|
---|
420 | if(size==0)
|
---|
421 | return 0; // short-cut. Also fixes Bug 884
|
---|
422 | check_size_for_overflow<T>(size);
|
---|
423 | T *result = reinterpret_cast<T*>(conditional_aligned_malloc<Align>(sizeof(T)*size));
|
---|
424 | if(NumTraits<T>::RequireInitialization)
|
---|
425 | construct_elements_of_array(result, size);
|
---|
426 | return result;
|
---|
427 | }
|
---|
428 |
|
---|
429 | template<typename T, bool Align> inline T* conditional_aligned_realloc_new_auto(T* pts, size_t new_size, size_t old_size)
|
---|
430 | {
|
---|
431 | check_size_for_overflow<T>(new_size);
|
---|
432 | check_size_for_overflow<T>(old_size);
|
---|
433 | if(NumTraits<T>::RequireInitialization && (new_size < old_size))
|
---|
434 | destruct_elements_of_array(pts+new_size, old_size-new_size);
|
---|
435 | T *result = reinterpret_cast<T*>(conditional_aligned_realloc<Align>(reinterpret_cast<void*>(pts), sizeof(T)*new_size, sizeof(T)*old_size));
|
---|
436 | if(NumTraits<T>::RequireInitialization && (new_size > old_size))
|
---|
437 | construct_elements_of_array(result+old_size, new_size-old_size);
|
---|
438 | return result;
|
---|
439 | }
|
---|
440 |
|
---|
441 | template<typename T, bool Align> inline void conditional_aligned_delete_auto(T *ptr, 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 |
|
---|
450 | /** \internal Returns the index of the first element of the array that is well aligned for vectorization.
|
---|
451 | *
|
---|
452 | * \param array the address of the start of the array
|
---|
453 | * \param size the size of the array
|
---|
454 | *
|
---|
455 | * \note If no element of the array is well aligned, the size of the array is returned. Typically,
|
---|
456 | * for example with SSE, "well aligned" means 16-byte-aligned. If vectorization is disabled or if the
|
---|
457 | * packet size for the given scalar type is 1, then everything is considered well-aligned.
|
---|
458 | *
|
---|
459 | * \note If the scalar type is vectorizable, we rely on the following assumptions: sizeof(Scalar) is a
|
---|
460 | * power of 2, the packet size in bytes is also a power of 2, and is a multiple of sizeof(Scalar). On the
|
---|
461 | * other hand, we do not assume that the array address is a multiple of sizeof(Scalar), as that fails for
|
---|
462 | * example with Scalar=double on certain 32-bit platforms, see bug #79.
|
---|
463 | *
|
---|
464 | * There is also the variant first_aligned(const MatrixBase&) defined in DenseCoeffsBase.h.
|
---|
465 | */
|
---|
466 | template<typename Scalar, typename Index>
|
---|
467 | static inline Index first_aligned(const Scalar* array, Index size)
|
---|
468 | {
|
---|
469 | static const Index PacketSize = packet_traits<Scalar>::size;
|
---|
470 | static const Index PacketAlignedMask = PacketSize-1;
|
---|
471 |
|
---|
472 | if(PacketSize==1)
|
---|
473 | {
|
---|
474 | // Either there is no vectorization, or a packet consists of exactly 1 scalar so that all elements
|
---|
475 | // of the array have the same alignment.
|
---|
476 | return 0;
|
---|
477 | }
|
---|
478 | else if(size_t(array) & (sizeof(Scalar)-1))
|
---|
479 | {
|
---|
480 | // There is vectorization for this scalar type, but the array is not aligned to the size of a single scalar.
|
---|
481 | // Consequently, no element of the array is well aligned.
|
---|
482 | return size;
|
---|
483 | }
|
---|
484 | else
|
---|
485 | {
|
---|
486 | return std::min<Index>( (PacketSize - (Index((size_t(array)/sizeof(Scalar))) & PacketAlignedMask))
|
---|
487 | & PacketAlignedMask, size);
|
---|
488 | }
|
---|
489 | }
|
---|
490 |
|
---|
491 | /** \internal Returns the smallest integer multiple of \a base and greater or equal to \a size
|
---|
492 | */
|
---|
493 | template<typename Index>
|
---|
494 | inline static Index first_multiple(Index size, Index base)
|
---|
495 | {
|
---|
496 | return ((size+base-1)/base)*base;
|
---|
497 | }
|
---|
498 |
|
---|
499 | // std::copy is much slower than memcpy, so let's introduce a smart_copy which
|
---|
500 | // use memcpy on trivial types, i.e., on types that does not require an initialization ctor.
|
---|
501 | template<typename T, bool UseMemcpy> struct smart_copy_helper;
|
---|
502 |
|
---|
503 | template<typename T> void smart_copy(const T* start, const T* end, T* target)
|
---|
504 | {
|
---|
505 | smart_copy_helper<T,!NumTraits<T>::RequireInitialization>::run(start, end, target);
|
---|
506 | }
|
---|
507 |
|
---|
508 | template<typename T> struct smart_copy_helper<T,true> {
|
---|
509 | static inline void run(const T* start, const T* end, T* target)
|
---|
510 | {
|
---|
511 | std::ptrdiff_t size = std::ptrdiff_t(end)-std::ptrdiff_t(start);
|
---|
512 | if(size==0) return;
|
---|
513 | eigen_internal_assert(start!=0 && end!=0 && target!=0);
|
---|
514 | memcpy(target, start, size);
|
---|
515 | }
|
---|
516 | };
|
---|
517 |
|
---|
518 | template<typename T> struct smart_copy_helper<T,false> {
|
---|
519 | static inline void run(const T* start, const T* end, T* target)
|
---|
520 | { std::copy(start, end, target); }
|
---|
521 | };
|
---|
522 |
|
---|
523 | /*****************************************************************************
|
---|
524 | *** Implementation of runtime stack allocation (falling back to malloc) ***
|
---|
525 | *****************************************************************************/
|
---|
526 |
|
---|
527 | // you can overwrite Eigen's default behavior regarding alloca by defining EIGEN_ALLOCA
|
---|
528 | // to the appropriate stack allocation function
|
---|
529 | #ifndef EIGEN_ALLOCA
|
---|
530 | #if (defined __linux__) || (defined __APPLE__) || (defined alloca)
|
---|
531 | #define EIGEN_ALLOCA alloca
|
---|
532 | #elif defined(_MSC_VER)
|
---|
533 | #define EIGEN_ALLOCA _alloca
|
---|
534 | #endif
|
---|
535 | #endif
|
---|
536 |
|
---|
537 | // This helper class construct the allocated memory, and takes care of destructing and freeing the handled data
|
---|
538 | // at destruction time. In practice this helper class is mainly useful to avoid memory leak in case of exceptions.
|
---|
539 | template<typename T> class aligned_stack_memory_handler
|
---|
540 | {
|
---|
541 | public:
|
---|
542 | /* Creates a stack_memory_handler responsible for the buffer \a ptr of size \a size.
|
---|
543 | * Note that \a ptr can be 0 regardless of the other parameters.
|
---|
544 | * This constructor takes care of constructing/initializing the elements of the buffer if required by the scalar type T (see NumTraits<T>::RequireInitialization).
|
---|
545 | * In this case, the buffer elements will also be destructed when this handler will be destructed.
|
---|
546 | * Finally, if \a dealloc is true, then the pointer \a ptr is freed.
|
---|
547 | **/
|
---|
548 | aligned_stack_memory_handler(T* ptr, size_t size, bool dealloc)
|
---|
549 | : m_ptr(ptr), m_size(size), m_deallocate(dealloc)
|
---|
550 | {
|
---|
551 | if(NumTraits<T>::RequireInitialization && m_ptr)
|
---|
552 | Eigen::internal::construct_elements_of_array(m_ptr, size);
|
---|
553 | }
|
---|
554 | ~aligned_stack_memory_handler()
|
---|
555 | {
|
---|
556 | if(NumTraits<T>::RequireInitialization && m_ptr)
|
---|
557 | Eigen::internal::destruct_elements_of_array<T>(m_ptr, m_size);
|
---|
558 | if(m_deallocate)
|
---|
559 | Eigen::internal::aligned_free(m_ptr);
|
---|
560 | }
|
---|
561 | protected:
|
---|
562 | T* m_ptr;
|
---|
563 | size_t m_size;
|
---|
564 | bool m_deallocate;
|
---|
565 | };
|
---|
566 |
|
---|
567 | } // end namespace internal
|
---|
568 |
|
---|
569 | /** \internal
|
---|
570 | * Declares, allocates and construct an aligned buffer named NAME of SIZE elements of type TYPE on the stack
|
---|
571 | * if SIZE is smaller than EIGEN_STACK_ALLOCATION_LIMIT, and if stack allocation is supported by the platform
|
---|
572 | * (currently, this is Linux and Visual Studio only). Otherwise the memory is allocated on the heap.
|
---|
573 | * The allocated buffer is automatically deleted when exiting the scope of this declaration.
|
---|
574 | * If BUFFER is non null, then the declared variable is simply an alias for BUFFER, and no allocation/deletion occurs.
|
---|
575 | * Here is an example:
|
---|
576 | * \code
|
---|
577 | * {
|
---|
578 | * ei_declare_aligned_stack_constructed_variable(float,data,size,0);
|
---|
579 | * // use data[0] to data[size-1]
|
---|
580 | * }
|
---|
581 | * \endcode
|
---|
582 | * The underlying stack allocation function can controlled with the EIGEN_ALLOCA preprocessor token.
|
---|
583 | */
|
---|
584 | #ifdef EIGEN_ALLOCA
|
---|
585 |
|
---|
586 | #if defined(__arm__) || defined(_WIN32)
|
---|
587 | #define EIGEN_ALIGNED_ALLOCA(SIZE) reinterpret_cast<void*>((reinterpret_cast<size_t>(EIGEN_ALLOCA(SIZE+16)) & ~(size_t(15))) + 16)
|
---|
588 | #else
|
---|
589 | #define EIGEN_ALIGNED_ALLOCA EIGEN_ALLOCA
|
---|
590 | #endif
|
---|
591 |
|
---|
592 | #define ei_declare_aligned_stack_constructed_variable(TYPE,NAME,SIZE,BUFFER) \
|
---|
593 | Eigen::internal::check_size_for_overflow<TYPE>(SIZE); \
|
---|
594 | TYPE* NAME = (BUFFER)!=0 ? (BUFFER) \
|
---|
595 | : reinterpret_cast<TYPE*>( \
|
---|
596 | (sizeof(TYPE)*SIZE<=EIGEN_STACK_ALLOCATION_LIMIT) ? EIGEN_ALIGNED_ALLOCA(sizeof(TYPE)*SIZE) \
|
---|
597 | : Eigen::internal::aligned_malloc(sizeof(TYPE)*SIZE) ); \
|
---|
598 | 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)
|
---|
599 |
|
---|
600 | #else
|
---|
601 |
|
---|
602 | #define ei_declare_aligned_stack_constructed_variable(TYPE,NAME,SIZE,BUFFER) \
|
---|
603 | Eigen::internal::check_size_for_overflow<TYPE>(SIZE); \
|
---|
604 | TYPE* NAME = (BUFFER)!=0 ? BUFFER : reinterpret_cast<TYPE*>(Eigen::internal::aligned_malloc(sizeof(TYPE)*SIZE)); \
|
---|
605 | Eigen::internal::aligned_stack_memory_handler<TYPE> EIGEN_CAT(NAME,_stack_memory_destructor)((BUFFER)==0 ? NAME : 0,SIZE,true)
|
---|
606 |
|
---|
607 | #endif
|
---|
608 |
|
---|
609 |
|
---|
610 | /*****************************************************************************
|
---|
611 | *** Implementation of EIGEN_MAKE_ALIGNED_OPERATOR_NEW [_IF] ***
|
---|
612 | *****************************************************************************/
|
---|
613 |
|
---|
614 | #if EIGEN_ALIGN
|
---|
615 | #ifdef EIGEN_EXCEPTIONS
|
---|
616 | #define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_NOTHROW(NeedsToAlign) \
|
---|
617 | void* operator new(size_t size, const std::nothrow_t&) throw() { \
|
---|
618 | try { return Eigen::internal::conditional_aligned_malloc<NeedsToAlign>(size); } \
|
---|
619 | catch (...) { return 0; } \
|
---|
620 | }
|
---|
621 | #else
|
---|
622 | #define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_NOTHROW(NeedsToAlign) \
|
---|
623 | void* operator new(size_t size, const std::nothrow_t&) throw() { \
|
---|
624 | return Eigen::internal::conditional_aligned_malloc<NeedsToAlign>(size); \
|
---|
625 | }
|
---|
626 | #endif
|
---|
627 |
|
---|
628 | #define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(NeedsToAlign) \
|
---|
629 | void *operator new(size_t size) { \
|
---|
630 | return Eigen::internal::conditional_aligned_malloc<NeedsToAlign>(size); \
|
---|
631 | } \
|
---|
632 | void *operator new[](size_t size) { \
|
---|
633 | return Eigen::internal::conditional_aligned_malloc<NeedsToAlign>(size); \
|
---|
634 | } \
|
---|
635 | void operator delete(void * ptr) throw() { Eigen::internal::conditional_aligned_free<NeedsToAlign>(ptr); } \
|
---|
636 | void operator delete[](void * ptr) throw() { Eigen::internal::conditional_aligned_free<NeedsToAlign>(ptr); } \
|
---|
637 | void operator delete(void * ptr, std::size_t /* sz */) throw() { Eigen::internal::conditional_aligned_free<NeedsToAlign>(ptr); } \
|
---|
638 | void operator delete[](void * ptr, std::size_t /* sz */) throw() { Eigen::internal::conditional_aligned_free<NeedsToAlign>(ptr); } \
|
---|
639 | /* in-place new and delete. since (at least afaik) there is no actual */ \
|
---|
640 | /* memory allocated we can safely let the default implementation handle */ \
|
---|
641 | /* this particular case. */ \
|
---|
642 | static void *operator new(size_t size, void *ptr) { return ::operator new(size,ptr); } \
|
---|
643 | static void *operator new[](size_t size, void* ptr) { return ::operator new[](size,ptr); } \
|
---|
644 | void operator delete(void * memory, void *ptr) throw() { return ::operator delete(memory,ptr); } \
|
---|
645 | void operator delete[](void * memory, void *ptr) throw() { return ::operator delete[](memory,ptr); } \
|
---|
646 | /* nothrow-new (returns zero instead of std::bad_alloc) */ \
|
---|
647 | EIGEN_MAKE_ALIGNED_OPERATOR_NEW_NOTHROW(NeedsToAlign) \
|
---|
648 | void operator delete(void *ptr, const std::nothrow_t&) throw() { \
|
---|
649 | Eigen::internal::conditional_aligned_free<NeedsToAlign>(ptr); \
|
---|
650 | } \
|
---|
651 | typedef void eigen_aligned_operator_new_marker_type;
|
---|
652 | #else
|
---|
653 | #define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(NeedsToAlign)
|
---|
654 | #endif
|
---|
655 |
|
---|
656 | #define EIGEN_MAKE_ALIGNED_OPERATOR_NEW EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(true)
|
---|
657 | #define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF_VECTORIZABLE_FIXED_SIZE(Scalar,Size) \
|
---|
658 | EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(bool(((Size)!=Eigen::Dynamic) && ((sizeof(Scalar)*(Size))%16==0)))
|
---|
659 |
|
---|
660 | /****************************************************************************/
|
---|
661 |
|
---|
662 |
|
---|
663 | /** \class aligned_allocator
|
---|
664 | * \ingroup Core_Module
|
---|
665 | *
|
---|
666 | * \brief STL compatible allocator to use with with 16 byte aligned types
|
---|
667 | *
|
---|
668 | * Example:
|
---|
669 | * \code
|
---|
670 | * // Matrix4f requires 16 bytes alignment:
|
---|
671 | * std::map< int, Matrix4f, std::less<int>,
|
---|
672 | * aligned_allocator<std::pair<const int, Matrix4f> > > my_map_mat4;
|
---|
673 | * // Vector3f does not require 16 bytes alignment, no need to use Eigen's allocator:
|
---|
674 | * std::map< int, Vector3f > my_map_vec3;
|
---|
675 | * \endcode
|
---|
676 | *
|
---|
677 | * \sa \blank \ref TopicStlContainers.
|
---|
678 | */
|
---|
679 | template<class T>
|
---|
680 | class aligned_allocator : public std::allocator<T>
|
---|
681 | {
|
---|
682 | public:
|
---|
683 | typedef size_t size_type;
|
---|
684 | typedef std::ptrdiff_t difference_type;
|
---|
685 | typedef T* pointer;
|
---|
686 | typedef const T* const_pointer;
|
---|
687 | typedef T& reference;
|
---|
688 | typedef const T& const_reference;
|
---|
689 | typedef T value_type;
|
---|
690 |
|
---|
691 | template<class U>
|
---|
692 | struct rebind
|
---|
693 | {
|
---|
694 | typedef aligned_allocator<U> other;
|
---|
695 | };
|
---|
696 |
|
---|
697 | aligned_allocator() : std::allocator<T>() {}
|
---|
698 |
|
---|
699 | aligned_allocator(const aligned_allocator& other) : std::allocator<T>(other) {}
|
---|
700 |
|
---|
701 | template<class U>
|
---|
702 | aligned_allocator(const aligned_allocator<U>& other) : std::allocator<T>(other) {}
|
---|
703 |
|
---|
704 | ~aligned_allocator() {}
|
---|
705 |
|
---|
706 | pointer allocate(size_type num, const void* /*hint*/ = 0)
|
---|
707 | {
|
---|
708 | internal::check_size_for_overflow<T>(num);
|
---|
709 | return static_cast<pointer>( internal::aligned_malloc(num * sizeof(T)) );
|
---|
710 | }
|
---|
711 |
|
---|
712 | void deallocate(pointer p, size_type /*num*/)
|
---|
713 | {
|
---|
714 | internal::aligned_free(p);
|
---|
715 | }
|
---|
716 | };
|
---|
717 |
|
---|
718 | //---------- Cache sizes ----------
|
---|
719 |
|
---|
720 | #if !defined(EIGEN_NO_CPUID)
|
---|
721 | # if defined(__GNUC__) && ( defined(__i386__) || defined(__x86_64__) )
|
---|
722 | # if defined(__PIC__) && defined(__i386__)
|
---|
723 | // Case for x86 with PIC
|
---|
724 | # define EIGEN_CPUID(abcd,func,id) \
|
---|
725 | __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));
|
---|
726 | # elif defined(__PIC__) && defined(__x86_64__)
|
---|
727 | // 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.
|
---|
728 | // However, we cannot detect which code model is used, and the xchg overhead is negligible anyway.
|
---|
729 | # define EIGEN_CPUID(abcd,func,id) \
|
---|
730 | __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));
|
---|
731 | # else
|
---|
732 | // Case for x86_64 or x86 w/o PIC
|
---|
733 | # define EIGEN_CPUID(abcd,func,id) \
|
---|
734 | __asm__ __volatile__ ("cpuid": "=a" (abcd[0]), "=b" (abcd[1]), "=c" (abcd[2]), "=d" (abcd[3]) : "0" (func), "2" (id) );
|
---|
735 | # endif
|
---|
736 | # elif defined(_MSC_VER)
|
---|
737 | # if (_MSC_VER > 1500) && ( defined(_M_IX86) || defined(_M_X64) )
|
---|
738 | # define EIGEN_CPUID(abcd,func,id) __cpuidex((int*)abcd,func,id)
|
---|
739 | # endif
|
---|
740 | # endif
|
---|
741 | #endif
|
---|
742 |
|
---|
743 | namespace internal {
|
---|
744 |
|
---|
745 | #ifdef EIGEN_CPUID
|
---|
746 |
|
---|
747 | inline bool cpuid_is_vendor(int abcd[4], const int vendor[3])
|
---|
748 | {
|
---|
749 | return abcd[1]==vendor[0] && abcd[3]==vendor[1] && abcd[2]==vendor[2];
|
---|
750 | }
|
---|
751 |
|
---|
752 | inline void queryCacheSizes_intel_direct(int& l1, int& l2, int& l3)
|
---|
753 | {
|
---|
754 | int abcd[4];
|
---|
755 | l1 = l2 = l3 = 0;
|
---|
756 | int cache_id = 0;
|
---|
757 | int cache_type = 0;
|
---|
758 | do {
|
---|
759 | abcd[0] = abcd[1] = abcd[2] = abcd[3] = 0;
|
---|
760 | EIGEN_CPUID(abcd,0x4,cache_id);
|
---|
761 | cache_type = (abcd[0] & 0x0F) >> 0;
|
---|
762 | if(cache_type==1||cache_type==3) // data or unified cache
|
---|
763 | {
|
---|
764 | int cache_level = (abcd[0] & 0xE0) >> 5; // A[7:5]
|
---|
765 | int ways = (abcd[1] & 0xFFC00000) >> 22; // B[31:22]
|
---|
766 | int partitions = (abcd[1] & 0x003FF000) >> 12; // B[21:12]
|
---|
767 | int line_size = (abcd[1] & 0x00000FFF) >> 0; // B[11:0]
|
---|
768 | int sets = (abcd[2]); // C[31:0]
|
---|
769 |
|
---|
770 | int cache_size = (ways+1) * (partitions+1) * (line_size+1) * (sets+1);
|
---|
771 |
|
---|
772 | switch(cache_level)
|
---|
773 | {
|
---|
774 | case 1: l1 = cache_size; break;
|
---|
775 | case 2: l2 = cache_size; break;
|
---|
776 | case 3: l3 = cache_size; break;
|
---|
777 | default: break;
|
---|
778 | }
|
---|
779 | }
|
---|
780 | cache_id++;
|
---|
781 | } while(cache_type>0 && cache_id<16);
|
---|
782 | }
|
---|
783 |
|
---|
784 | inline void queryCacheSizes_intel_codes(int& l1, int& l2, int& l3)
|
---|
785 | {
|
---|
786 | int abcd[4];
|
---|
787 | abcd[0] = abcd[1] = abcd[2] = abcd[3] = 0;
|
---|
788 | l1 = l2 = l3 = 0;
|
---|
789 | EIGEN_CPUID(abcd,0x00000002,0);
|
---|
790 | unsigned char * bytes = reinterpret_cast<unsigned char *>(abcd)+2;
|
---|
791 | bool check_for_p2_core2 = false;
|
---|
792 | for(int i=0; i<14; ++i)
|
---|
793 | {
|
---|
794 | switch(bytes[i])
|
---|
795 | {
|
---|
796 | case 0x0A: l1 = 8; break; // 0Ah data L1 cache, 8 KB, 2 ways, 32 byte lines
|
---|
797 | case 0x0C: l1 = 16; break; // 0Ch data L1 cache, 16 KB, 4 ways, 32 byte lines
|
---|
798 | case 0x0E: l1 = 24; break; // 0Eh data L1 cache, 24 KB, 6 ways, 64 byte lines
|
---|
799 | case 0x10: l1 = 16; break; // 10h data L1 cache, 16 KB, 4 ways, 32 byte lines (IA-64)
|
---|
800 | case 0x15: l1 = 16; break; // 15h code L1 cache, 16 KB, 4 ways, 32 byte lines (IA-64)
|
---|
801 | case 0x2C: l1 = 32; break; // 2Ch data L1 cache, 32 KB, 8 ways, 64 byte lines
|
---|
802 | case 0x30: l1 = 32; break; // 30h code L1 cache, 32 KB, 8 ways, 64 byte lines
|
---|
803 | case 0x60: l1 = 16; break; // 60h data L1 cache, 16 KB, 8 ways, 64 byte lines, sectored
|
---|
804 | case 0x66: l1 = 8; break; // 66h data L1 cache, 8 KB, 4 ways, 64 byte lines, sectored
|
---|
805 | case 0x67: l1 = 16; break; // 67h data L1 cache, 16 KB, 4 ways, 64 byte lines, sectored
|
---|
806 | case 0x68: l1 = 32; break; // 68h data L1 cache, 32 KB, 4 ways, 64 byte lines, sectored
|
---|
807 | case 0x1A: l2 = 96; break; // code and data L2 cache, 96 KB, 6 ways, 64 byte lines (IA-64)
|
---|
808 | case 0x22: l3 = 512; break; // code and data L3 cache, 512 KB, 4 ways (!), 64 byte lines, dual-sectored
|
---|
809 | case 0x23: l3 = 1024; break; // code and data L3 cache, 1024 KB, 8 ways, 64 byte lines, dual-sectored
|
---|
810 | case 0x25: l3 = 2048; break; // code and data L3 cache, 2048 KB, 8 ways, 64 byte lines, dual-sectored
|
---|
811 | case 0x29: l3 = 4096; break; // code and data L3 cache, 4096 KB, 8 ways, 64 byte lines, dual-sectored
|
---|
812 | case 0x39: l2 = 128; break; // code and data L2 cache, 128 KB, 4 ways, 64 byte lines, sectored
|
---|
813 | case 0x3A: l2 = 192; break; // code and data L2 cache, 192 KB, 6 ways, 64 byte lines, sectored
|
---|
814 | case 0x3B: l2 = 128; break; // code and data L2 cache, 128 KB, 2 ways, 64 byte lines, sectored
|
---|
815 | case 0x3C: l2 = 256; break; // code and data L2 cache, 256 KB, 4 ways, 64 byte lines, sectored
|
---|
816 | case 0x3D: l2 = 384; break; // code and data L2 cache, 384 KB, 6 ways, 64 byte lines, sectored
|
---|
817 | case 0x3E: l2 = 512; break; // code and data L2 cache, 512 KB, 4 ways, 64 byte lines, sectored
|
---|
818 | case 0x40: l2 = 0; break; // no integrated L2 cache (P6 core) or L3 cache (P4 core)
|
---|
819 | case 0x41: l2 = 128; break; // code and data L2 cache, 128 KB, 4 ways, 32 byte lines
|
---|
820 | case 0x42: l2 = 256; break; // code and data L2 cache, 256 KB, 4 ways, 32 byte lines
|
---|
821 | case 0x43: l2 = 512; break; // code and data L2 cache, 512 KB, 4 ways, 32 byte lines
|
---|
822 | case 0x44: l2 = 1024; break; // code and data L2 cache, 1024 KB, 4 ways, 32 byte lines
|
---|
823 | case 0x45: l2 = 2048; break; // code and data L2 cache, 2048 KB, 4 ways, 32 byte lines
|
---|
824 | case 0x46: l3 = 4096; break; // code and data L3 cache, 4096 KB, 4 ways, 64 byte lines
|
---|
825 | case 0x47: l3 = 8192; break; // code and data L3 cache, 8192 KB, 8 ways, 64 byte lines
|
---|
826 | case 0x48: l2 = 3072; break; // code and data L2 cache, 3072 KB, 12 ways, 64 byte lines
|
---|
827 | 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
|
---|
828 | case 0x4A: l3 = 6144; break; // code and data L3 cache, 6144 KB, 12 ways, 64 byte lines
|
---|
829 | case 0x4B: l3 = 8192; break; // code and data L3 cache, 8192 KB, 16 ways, 64 byte lines
|
---|
830 | case 0x4C: l3 = 12288; break; // code and data L3 cache, 12288 KB, 12 ways, 64 byte lines
|
---|
831 | case 0x4D: l3 = 16384; break; // code and data L3 cache, 16384 KB, 16 ways, 64 byte lines
|
---|
832 | case 0x4E: l2 = 6144; break; // code and data L2 cache, 6144 KB, 24 ways, 64 byte lines
|
---|
833 | case 0x78: l2 = 1024; break; // code and data L2 cache, 1024 KB, 4 ways, 64 byte lines
|
---|
834 | case 0x79: l2 = 128; break; // code and data L2 cache, 128 KB, 8 ways, 64 byte lines, dual-sectored
|
---|
835 | case 0x7A: l2 = 256; break; // code and data L2 cache, 256 KB, 8 ways, 64 byte lines, dual-sectored
|
---|
836 | case 0x7B: l2 = 512; break; // code and data L2 cache, 512 KB, 8 ways, 64 byte lines, dual-sectored
|
---|
837 | case 0x7C: l2 = 1024; break; // code and data L2 cache, 1024 KB, 8 ways, 64 byte lines, dual-sectored
|
---|
838 | case 0x7D: l2 = 2048; break; // code and data L2 cache, 2048 KB, 8 ways, 64 byte lines
|
---|
839 | case 0x7E: l2 = 256; break; // code and data L2 cache, 256 KB, 8 ways, 128 byte lines, sect. (IA-64)
|
---|
840 | case 0x7F: l2 = 512; break; // code and data L2 cache, 512 KB, 2 ways, 64 byte lines
|
---|
841 | case 0x80: l2 = 512; break; // code and data L2 cache, 512 KB, 8 ways, 64 byte lines
|
---|
842 | case 0x81: l2 = 128; break; // code and data L2 cache, 128 KB, 8 ways, 32 byte lines
|
---|
843 | case 0x82: l2 = 256; break; // code and data L2 cache, 256 KB, 8 ways, 32 byte lines
|
---|
844 | case 0x83: l2 = 512; break; // code and data L2 cache, 512 KB, 8 ways, 32 byte lines
|
---|
845 | case 0x84: l2 = 1024; break; // code and data L2 cache, 1024 KB, 8 ways, 32 byte lines
|
---|
846 | case 0x85: l2 = 2048; break; // code and data L2 cache, 2048 KB, 8 ways, 32 byte lines
|
---|
847 | case 0x86: l2 = 512; break; // code and data L2 cache, 512 KB, 4 ways, 64 byte lines
|
---|
848 | case 0x87: l2 = 1024; break; // code and data L2 cache, 1024 KB, 8 ways, 64 byte lines
|
---|
849 | case 0x88: l3 = 2048; break; // code and data L3 cache, 2048 KB, 4 ways, 64 byte lines (IA-64)
|
---|
850 | case 0x89: l3 = 4096; break; // code and data L3 cache, 4096 KB, 4 ways, 64 byte lines (IA-64)
|
---|
851 | case 0x8A: l3 = 8192; break; // code and data L3 cache, 8192 KB, 4 ways, 64 byte lines (IA-64)
|
---|
852 | case 0x8D: l3 = 3072; break; // code and data L3 cache, 3072 KB, 12 ways, 128 byte lines (IA-64)
|
---|
853 |
|
---|
854 | default: break;
|
---|
855 | }
|
---|
856 | }
|
---|
857 | if(check_for_p2_core2 && l2 == l3)
|
---|
858 | l3 = 0;
|
---|
859 | l1 *= 1024;
|
---|
860 | l2 *= 1024;
|
---|
861 | l3 *= 1024;
|
---|
862 | }
|
---|
863 |
|
---|
864 | inline void queryCacheSizes_intel(int& l1, int& l2, int& l3, int max_std_funcs)
|
---|
865 | {
|
---|
866 | if(max_std_funcs>=4)
|
---|
867 | queryCacheSizes_intel_direct(l1,l2,l3);
|
---|
868 | else
|
---|
869 | queryCacheSizes_intel_codes(l1,l2,l3);
|
---|
870 | }
|
---|
871 |
|
---|
872 | inline void queryCacheSizes_amd(int& l1, int& l2, int& l3)
|
---|
873 | {
|
---|
874 | int abcd[4];
|
---|
875 | abcd[0] = abcd[1] = abcd[2] = abcd[3] = 0;
|
---|
876 | EIGEN_CPUID(abcd,0x80000005,0);
|
---|
877 | l1 = (abcd[2] >> 24) * 1024; // C[31:24] = L1 size in KB
|
---|
878 | abcd[0] = abcd[1] = abcd[2] = abcd[3] = 0;
|
---|
879 | EIGEN_CPUID(abcd,0x80000006,0);
|
---|
880 | l2 = (abcd[2] >> 16) * 1024; // C[31;16] = l2 cache size in KB
|
---|
881 | l3 = ((abcd[3] & 0xFFFC000) >> 18) * 512 * 1024; // D[31;18] = l3 cache size in 512KB
|
---|
882 | }
|
---|
883 | #endif
|
---|
884 |
|
---|
885 | /** \internal
|
---|
886 | * Queries and returns the cache sizes in Bytes of the L1, L2, and L3 data caches respectively */
|
---|
887 | inline void queryCacheSizes(int& l1, int& l2, int& l3)
|
---|
888 | {
|
---|
889 | #ifdef EIGEN_CPUID
|
---|
890 | int abcd[4];
|
---|
891 | const int GenuineIntel[] = {0x756e6547, 0x49656e69, 0x6c65746e};
|
---|
892 | const int AuthenticAMD[] = {0x68747541, 0x69746e65, 0x444d4163};
|
---|
893 | const int AMDisbetter_[] = {0x69444d41, 0x74656273, 0x21726574}; // "AMDisbetter!"
|
---|
894 |
|
---|
895 | // identify the CPU vendor
|
---|
896 | EIGEN_CPUID(abcd,0x0,0);
|
---|
897 | int max_std_funcs = abcd[1];
|
---|
898 | if(cpuid_is_vendor(abcd,GenuineIntel))
|
---|
899 | queryCacheSizes_intel(l1,l2,l3,max_std_funcs);
|
---|
900 | else if(cpuid_is_vendor(abcd,AuthenticAMD) || cpuid_is_vendor(abcd,AMDisbetter_))
|
---|
901 | queryCacheSizes_amd(l1,l2,l3);
|
---|
902 | else
|
---|
903 | // by default let's use Intel's API
|
---|
904 | queryCacheSizes_intel(l1,l2,l3,max_std_funcs);
|
---|
905 |
|
---|
906 | // here is the list of other vendors:
|
---|
907 | // ||cpuid_is_vendor(abcd,"VIA VIA VIA ")
|
---|
908 | // ||cpuid_is_vendor(abcd,"CyrixInstead")
|
---|
909 | // ||cpuid_is_vendor(abcd,"CentaurHauls")
|
---|
910 | // ||cpuid_is_vendor(abcd,"GenuineTMx86")
|
---|
911 | // ||cpuid_is_vendor(abcd,"TransmetaCPU")
|
---|
912 | // ||cpuid_is_vendor(abcd,"RiseRiseRise")
|
---|
913 | // ||cpuid_is_vendor(abcd,"Geode by NSC")
|
---|
914 | // ||cpuid_is_vendor(abcd,"SiS SiS SiS ")
|
---|
915 | // ||cpuid_is_vendor(abcd,"UMC UMC UMC ")
|
---|
916 | // ||cpuid_is_vendor(abcd,"NexGenDriven")
|
---|
917 | #else
|
---|
918 | l1 = l2 = l3 = -1;
|
---|
919 | #endif
|
---|
920 | }
|
---|
921 |
|
---|
922 | /** \internal
|
---|
923 | * \returns the size in Bytes of the L1 data cache */
|
---|
924 | inline int queryL1CacheSize()
|
---|
925 | {
|
---|
926 | int l1(-1), l2, l3;
|
---|
927 | queryCacheSizes(l1,l2,l3);
|
---|
928 | return l1;
|
---|
929 | }
|
---|
930 |
|
---|
931 | /** \internal
|
---|
932 | * \returns the size in Bytes of the L2 or L3 cache if this later is present */
|
---|
933 | inline int queryTopLevelCacheSize()
|
---|
934 | {
|
---|
935 | int l1, l2(-1), l3(-1);
|
---|
936 | queryCacheSizes(l1,l2,l3);
|
---|
937 | return (std::max)(l2,l3);
|
---|
938 | }
|
---|
939 |
|
---|
940 | } // end namespace internal
|
---|
941 |
|
---|
942 | } // end namespace Eigen
|
---|
943 |
|
---|
944 | #endif // EIGEN_MEMORY_H
|
---|