Eigen  5.0.1
 
Loading...
Searching...
No Matches
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// SPDX-License-Identifier: MPL-2.0
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) && defined(__LP64__) && \
35 !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)) || (EIGEN_OS_WIN64 && (EIGEN_DEFAULT_ALIGN_BYTES == 16)) || \
52 EIGEN_GLIBC_MALLOC_ALREADY_ALIGNED || EIGEN_FREEBSD_MALLOC_ALREADY_ALIGNED
53#define EIGEN_MALLOC_ALREADY_ALIGNED 1
54#else
55#define EIGEN_MALLOC_ALREADY_ALIGNED 0
56#endif
57
58#endif
59
60#ifndef EIGEN_MALLOC_CHECK_THREAD_LOCAL
61
62// Check whether we can use the thread_local keyword to allow or disallow
63// allocating memory with per-thread granularity, by means of the
64// set_is_malloc_allowed() function.
65#ifndef EIGEN_AVOID_THREAD_LOCAL
66
67#if !defined(EIGEN_GPU_COMPILE_PHASE)
68#define EIGEN_MALLOC_CHECK_THREAD_LOCAL thread_local
69#else
70#define EIGEN_MALLOC_CHECK_THREAD_LOCAL
71#endif
72
73#else // EIGEN_AVOID_THREAD_LOCAL
74#define EIGEN_MALLOC_CHECK_THREAD_LOCAL
75#endif // EIGEN_AVOID_THREAD_LOCAL
76
77#endif
78
79// IWYU pragma: private
80#include "../InternalHeaderCheck.h"
81
82namespace Eigen {
83
84namespace internal {
85
86/*****************************************************************************
87*** Implementation of portable aligned versions of malloc/free/realloc ***
88*****************************************************************************/
89
90#ifdef EIGEN_NO_MALLOC
91EIGEN_DEVICE_FUNC inline void check_that_malloc_is_allowed() {
92 eigen_assert(false && "heap allocation is forbidden (EIGEN_NO_MALLOC is defined)");
93}
94EIGEN_DEVICE_FUNC inline void check_that_free_is_allowed() {
95 eigen_assert(false && "heap deallocation is forbidden (EIGEN_NO_MALLOC is defined)");
96}
97#elif defined EIGEN_RUNTIME_NO_MALLOC
98EIGEN_DEVICE_FUNC inline bool is_malloc_allowed_impl(bool update, bool new_value = false) {
99 EIGEN_MALLOC_CHECK_THREAD_LOCAL static bool value = true;
100 if (update == 1) value = new_value;
101 return value;
102}
103EIGEN_DEVICE_FUNC inline bool is_malloc_allowed() { return is_malloc_allowed_impl(false); }
104EIGEN_DEVICE_FUNC inline bool set_is_malloc_allowed(bool new_value) { return is_malloc_allowed_impl(true, new_value); }
105EIGEN_DEVICE_FUNC inline void check_that_malloc_is_allowed() {
106 eigen_assert(is_malloc_allowed() &&
107 "heap allocation is forbidden (EIGEN_RUNTIME_NO_MALLOC is defined and set_is_malloc_allowed is false)");
108}
109EIGEN_DEVICE_FUNC inline bool is_free_allowed_impl(bool update, bool new_value = false) {
110 EIGEN_MALLOC_CHECK_THREAD_LOCAL static bool value = true;
111 if (update == 1) value = new_value;
112 return value;
113}
114EIGEN_DEVICE_FUNC inline bool is_free_allowed() { return is_free_allowed_impl(false); }
115EIGEN_DEVICE_FUNC inline bool set_is_free_allowed(bool new_value) { return is_free_allowed_impl(true, new_value); }
116EIGEN_DEVICE_FUNC inline void check_that_free_is_allowed() {
117 eigen_assert(is_free_allowed() &&
118 "heap deallocation is forbidden (EIGEN_RUNTIME_NO_MALLOC is defined and set_is_free_allowed is false)");
119}
120#else
121EIGEN_DEVICE_FUNC inline void check_that_malloc_is_allowed() {}
122EIGEN_DEVICE_FUNC inline void check_that_free_is_allowed() {}
123#endif
124
125EIGEN_DEVICE_FUNC inline void throw_std_bad_alloc() {
126#ifdef EIGEN_EXCEPTIONS
127 throw std::bad_alloc();
128#else
129 std::size_t huge = static_cast<std::size_t>(-1);
130#if defined(EIGEN_HIPCC)
131 //
132 // calls to "::operator new" are to be treated as opaque function calls (i.e no inlining),
133 // and as a consequence the code in the #else block triggers the hipcc warning :
134 // "no overloaded function has restriction specifiers that are compatible with the ambient context"
135 //
136 // "throw_std_bad_alloc" has the EIGEN_DEVICE_FUNC attribute, so it seems that hipcc expects
137 // the same on "operator new"
138 // Reverting code back to the old version in this #if block for the hipcc compiler
139 //
140 new int[huge];
141#else
142 void* unused = ::operator new(huge);
143 EIGEN_UNUSED_VARIABLE(unused);
144#endif
145#endif
146}
147
148/*****************************************************************************
149*** Implementation of handmade aligned functions ***
150*****************************************************************************/
151
152/* ----- Hand made implementations of aligned malloc/free and realloc ----- */
153
157EIGEN_DEVICE_FUNC inline void* handmade_aligned_malloc(std::size_t size,
158 std::size_t alignment = EIGEN_DEFAULT_ALIGN_BYTES) {
159 eigen_assert(alignment >= sizeof(void*) && alignment <= 256 && (alignment & (alignment - 1)) == 0 &&
160 "Alignment must be at least sizeof(void*), less than or equal to 256, and a power of 2");
161
162 check_that_malloc_is_allowed();
163 EIGEN_USING_STD(malloc)
164 void* original = malloc(size + alignment);
165 if (original == nullptr) return nullptr;
166 std::size_t offset = alignment - (reinterpret_cast<std::size_t>(original) & (alignment - 1));
167 void* aligned = static_cast<void*>(static_cast<uint8_t*>(original) + offset);
168 // Store offset - 1, since it is guaranteed to be at least 1.
169 *(static_cast<uint8_t*>(aligned) - 1) = static_cast<uint8_t>(offset - 1);
170 return aligned;
171}
172
174EIGEN_DEVICE_FUNC inline void handmade_aligned_free(void* ptr) {
175 if (ptr != nullptr) {
176 std::size_t offset = static_cast<std::size_t>(*(static_cast<uint8_t*>(ptr) - 1)) + 1;
177 void* original = static_cast<void*>(static_cast<uint8_t*>(ptr) - offset);
178
179 check_that_free_is_allowed();
180 EIGEN_USING_STD(free)
181 free(original);
182 }
183}
184
190EIGEN_DEVICE_FUNC inline void* handmade_aligned_realloc(void* ptr, std::size_t new_size, std::size_t old_size,
191 std::size_t alignment = EIGEN_DEFAULT_ALIGN_BYTES) {
192 if (ptr == nullptr) return handmade_aligned_malloc(new_size, alignment);
193 std::size_t old_offset = static_cast<std::size_t>(*(static_cast<uint8_t*>(ptr) - 1)) + 1;
194 void* old_original = static_cast<uint8_t*>(ptr) - old_offset;
195
196 check_that_malloc_is_allowed();
197 EIGEN_USING_STD(realloc)
198 void* original = realloc(old_original, new_size + alignment);
199 if (original == nullptr) return nullptr;
200 if (original == old_original) return ptr;
201 std::size_t offset = alignment - (reinterpret_cast<std::size_t>(original) & (alignment - 1));
202 void* aligned = static_cast<void*>(static_cast<uint8_t*>(original) + offset);
203 if (offset != old_offset) {
204 const void* src = static_cast<const void*>(static_cast<uint8_t*>(original) + old_offset);
205 std::size_t count = (std::min)(new_size, old_size);
206 std::memmove(aligned, src, count);
207 }
208 // Store offset - 1, since it is guaranteed to be at least 1.
209 *(static_cast<uint8_t*>(aligned) - 1) = static_cast<uint8_t>(offset - 1);
210 return aligned;
211}
212
216EIGEN_DEVICE_FUNC inline void* aligned_malloc(std::size_t size) {
217 if (size == 0) return nullptr;
218
219 void* result;
220#if (EIGEN_DEFAULT_ALIGN_BYTES == 0) || EIGEN_MALLOC_ALREADY_ALIGNED
221
222 check_that_malloc_is_allowed();
223 EIGEN_USING_STD(malloc)
224 result = malloc(size);
225
226#if EIGEN_DEFAULT_ALIGN_BYTES == 16
227 eigen_assert((size < 16 || (std::size_t(result) % 16) == 0) &&
228 "System's malloc returned an unaligned pointer. Compile with EIGEN_MALLOC_ALREADY_ALIGNED=0 to fallback "
229 "to handmade aligned memory allocator.");
230#endif
231#else
232 result = handmade_aligned_malloc(size);
233#endif
234
235 if (!result && size) throw_std_bad_alloc();
236
237 return result;
238}
239
241EIGEN_DEVICE_FUNC inline void aligned_free(void* ptr) {
242#if (EIGEN_DEFAULT_ALIGN_BYTES == 0) || EIGEN_MALLOC_ALREADY_ALIGNED
243
244 if (ptr != nullptr) {
245 check_that_free_is_allowed();
246 EIGEN_USING_STD(free)
247 free(ptr);
248 }
249
250#else
251 handmade_aligned_free(ptr);
252#endif
253}
254
260EIGEN_DEVICE_FUNC inline void* aligned_realloc(void* ptr, std::size_t new_size, std::size_t old_size) {
261 if (ptr == nullptr) return aligned_malloc(new_size);
262 if (old_size == new_size) return ptr;
263 if (new_size == 0) {
264 aligned_free(ptr);
265 return nullptr;
266 }
267
268 void* result;
269#if (EIGEN_DEFAULT_ALIGN_BYTES == 0) || EIGEN_MALLOC_ALREADY_ALIGNED
270 EIGEN_UNUSED_VARIABLE(old_size);
271
272 check_that_malloc_is_allowed();
273 EIGEN_USING_STD(realloc)
274 result = realloc(ptr, new_size);
275#else
276 result = handmade_aligned_realloc(ptr, new_size, old_size);
277#endif
278
279 if (!result && new_size) throw_std_bad_alloc();
280
281 return result;
282}
283
284/*****************************************************************************
285*** Implementation of conditionally aligned functions ***
286*****************************************************************************/
287
291template <bool Align>
292EIGEN_DEVICE_FUNC inline void* conditional_aligned_malloc(std::size_t size) {
293 return aligned_malloc(size);
294}
295
296template <>
297EIGEN_DEVICE_FUNC inline void* conditional_aligned_malloc<false>(std::size_t size) {
298 if (size == 0) return nullptr;
299
300 check_that_malloc_is_allowed();
301 EIGEN_USING_STD(malloc)
302 void* result = malloc(size);
303
304 if (!result && size) throw_std_bad_alloc();
305 return result;
306}
307
309template <bool Align>
310EIGEN_DEVICE_FUNC inline void conditional_aligned_free(void* ptr) {
311 aligned_free(ptr);
312}
313
314template <>
315EIGEN_DEVICE_FUNC inline void conditional_aligned_free<false>(void* ptr) {
316 if (ptr != nullptr) {
317 check_that_free_is_allowed();
318 EIGEN_USING_STD(free)
319 free(ptr);
320 }
321}
322
323template <bool Align>
324EIGEN_DEVICE_FUNC inline void* conditional_aligned_realloc(void* ptr, std::size_t new_size, std::size_t old_size) {
325 return aligned_realloc(ptr, new_size, old_size);
326}
327
328template <>
329EIGEN_DEVICE_FUNC inline void* conditional_aligned_realloc<false>(void* ptr, std::size_t new_size,
330 std::size_t old_size) {
331 if (ptr == nullptr) return conditional_aligned_malloc<false>(new_size);
332 if (old_size == new_size) return ptr;
333 if (new_size == 0) {
334 conditional_aligned_free<false>(ptr);
335 return nullptr;
336 }
337
338 check_that_malloc_is_allowed();
339 EIGEN_USING_STD(realloc)
340 return realloc(ptr, new_size);
341}
342
343/*****************************************************************************
344*** Construction/destruction of array elements ***
345*****************************************************************************/
346
350template <typename T>
351EIGEN_DEVICE_FUNC inline void destruct_elements_of_array(T* ptr, std::size_t size) {
352 // always destruct an array starting from the end.
353 if (ptr)
354 while (size) ptr[--size].~T();
355}
356
360template <typename T>
361EIGEN_DEVICE_FUNC inline T* default_construct_elements_of_array(T* ptr, std::size_t size) {
362 std::size_t i = 0;
363 EIGEN_TRY {
364 for (i = 0; i < size; ++i) ::new (ptr + i) T;
365 }
366 EIGEN_CATCH(...) {
367 destruct_elements_of_array(ptr, i);
368 EIGEN_THROW;
369 }
370 return ptr;
371}
372
376template <typename T>
377EIGEN_DEVICE_FUNC inline T* copy_construct_elements_of_array(T* ptr, const T* src, std::size_t size) {
378 std::size_t i = 0;
379 EIGEN_TRY {
380 for (i = 0; i < size; ++i) ::new (ptr + i) T(*(src + i));
381 }
382 EIGEN_CATCH(...) {
383 destruct_elements_of_array(ptr, i);
384 EIGEN_THROW;
385 }
386 return ptr;
387}
388
392template <typename T>
393EIGEN_DEVICE_FUNC inline T* move_construct_elements_of_array(T* ptr, T* src, std::size_t size) {
394 std::size_t i = 0;
395 EIGEN_TRY {
396 for (i = 0; i < size; ++i) ::new (ptr + i) T(std::move(*(src + i)));
397 }
398 EIGEN_CATCH(...) {
399 destruct_elements_of_array(ptr, i);
400 EIGEN_THROW;
401 }
402 return ptr;
403}
404
405/*****************************************************************************
406*** Implementation of aligned new/delete-like functions ***
407*****************************************************************************/
408
409template <typename T>
410EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE void check_size_for_overflow(std::size_t size) {
411 constexpr std::size_t max_elements = (std::numeric_limits<std::ptrdiff_t>::max)() / sizeof(T);
412 if (size > max_elements) throw_std_bad_alloc();
413}
414
419template <typename T>
420EIGEN_DEVICE_FUNC inline T* aligned_new(std::size_t size) {
421 check_size_for_overflow<T>(size);
422 T* result = static_cast<T*>(aligned_malloc(sizeof(T) * size));
423 EIGEN_TRY { default_construct_elements_of_array(result, size); }
424 EIGEN_CATCH(...) {
425 aligned_free(result);
426 EIGEN_THROW;
427 }
428 return result;
429}
430
431template <typename T, bool Align>
432EIGEN_DEVICE_FUNC inline T* conditional_aligned_new(std::size_t size) {
433 check_size_for_overflow<T>(size);
434 T* result = static_cast<T*>(conditional_aligned_malloc<Align>(sizeof(T) * size));
435 EIGEN_TRY { default_construct_elements_of_array(result, size); }
436 EIGEN_CATCH(...) {
437 conditional_aligned_free<Align>(result);
438 EIGEN_THROW;
439 }
440 return result;
441}
442
446template <typename T>
447EIGEN_DEVICE_FUNC inline void aligned_delete(T* ptr, std::size_t size) {
448 destruct_elements_of_array<T>(ptr, size);
449 aligned_free(ptr);
450}
451
455template <typename T, bool Align>
456EIGEN_DEVICE_FUNC inline void conditional_aligned_delete(T* ptr, std::size_t size) {
457 destruct_elements_of_array<T>(ptr, size);
458 conditional_aligned_free<Align>(ptr);
459}
460
461template <typename T, bool Align>
462EIGEN_DEVICE_FUNC inline T* conditional_aligned_realloc_new(T* pts, std::size_t new_size, std::size_t old_size) {
463 check_size_for_overflow<T>(new_size);
464 check_size_for_overflow<T>(old_size);
465
466 // If elements need to be explicitly initialized, we cannot simply realloc
467 // (or memcpy) the memory block - each element needs to be reconstructed.
468 // Otherwise, objects that contain internal pointers like mpfr or
469 // AnnoyingScalar can be pointing to the wrong thing.
470 T* result = static_cast<T*>(conditional_aligned_malloc<Align>(sizeof(T) * new_size));
471 EIGEN_TRY {
472 // Move-construct initial elements.
473 std::size_t copy_size = (std::min)(old_size, new_size);
474 move_construct_elements_of_array(result, pts, copy_size);
475
476 // Default-construct remaining elements.
477 if (new_size > old_size) {
478 default_construct_elements_of_array(result + copy_size, new_size - old_size);
479 }
480
481 // Delete old elements.
482 conditional_aligned_delete<T, Align>(pts, old_size);
483 }
484 EIGEN_CATCH(...) {
485 conditional_aligned_free<Align>(result);
486 EIGEN_THROW;
487 }
488
489 return result;
490}
491
492template <typename T, bool Align>
493EIGEN_DEVICE_FUNC inline T* conditional_aligned_new_auto(std::size_t size) {
494 if (size == 0) return nullptr; // short-cut. Also fixes Bug 884
495 check_size_for_overflow<T>(size);
496 T* result = static_cast<T*>(conditional_aligned_malloc<Align>(sizeof(T) * size));
497 EIGEN_IF_CONSTEXPR (NumTraits<T>::RequireInitialization) {
498 EIGEN_TRY { default_construct_elements_of_array(result, size); }
499 EIGEN_CATCH(...) {
500 conditional_aligned_free<Align>(result);
501 EIGEN_THROW;
502 }
503 }
504 return result;
505}
506
507template <typename T, bool Align>
508EIGEN_DEVICE_FUNC inline T* conditional_aligned_realloc_new_auto(T* pts, std::size_t new_size, std::size_t old_size) {
509 EIGEN_IF_CONSTEXPR (NumTraits<T>::RequireInitialization) {
510 return conditional_aligned_realloc_new<T, Align>(pts, new_size, old_size);
511 }
512
513 check_size_for_overflow<T>(new_size);
514 check_size_for_overflow<T>(old_size);
515 return static_cast<T*>(
516 conditional_aligned_realloc<Align>(static_cast<void*>(pts), sizeof(T) * new_size, sizeof(T) * old_size));
517}
518
519template <typename T, bool Align>
520EIGEN_DEVICE_FUNC inline void conditional_aligned_delete_auto(T* ptr, std::size_t size) {
521 EIGEN_IF_CONSTEXPR (NumTraits<T>::RequireInitialization) destruct_elements_of_array<T>(ptr, size);
522 conditional_aligned_free<Align>(ptr);
523}
524
525/****************************************************************************/
526
545template <int Alignment, typename Scalar, typename Index>
546EIGEN_DEVICE_FUNC inline Index first_aligned(const Scalar* array, Index size) {
547 constexpr Index ScalarSize = sizeof(Scalar);
548 constexpr Index AlignmentSize = Alignment / ScalarSize;
549 constexpr Index AlignmentMask = AlignmentSize - 1;
550
551 EIGEN_IF_CONSTEXPR (AlignmentSize <= 1) {
552 // Either the requested alignment if smaller than a scalar, or it exactly match a 1 scalar
553 // so that all elements of the array have the same alignment.
554 return 0;
555 } else EIGEN_IF_CONSTEXPR ((Alignment % ScalarSize) != 0) {
556 // The requested alignment is not a multiple of the scalar size. Consequently, no element of the array is well
557 // aligned.
558 return size;
559 } else if (std::uintptr_t(array) & (sizeof(Scalar) - 1)) {
560 // The array is not aligned to the size of a single scalar. Consequently, no element of the array is well aligned.
561 return size;
562 } else {
563 Index first = (AlignmentSize - (Index((std::uintptr_t(array) / sizeof(Scalar))) & AlignmentMask)) & AlignmentMask;
564 return (first < size) ? first : size;
565 }
566}
567
570template <typename Scalar, typename Index>
571EIGEN_DEVICE_FUNC inline Index first_default_aligned(const Scalar* array, Index size) {
572 using DefaultPacketType = typename packet_traits<Scalar>::type;
573 return first_aligned<unpacket_traits<DefaultPacketType>::alignment>(array, size);
574}
575
578template <typename Index>
579inline Index first_multiple(Index size, Index base) {
580 return ((size + base - 1) / base) * base;
581}
582
583// std::copy is much slower than memcpy, so let's introduce a smart_copy which
584// use memcpy on trivial types, i.e., on types that does not require an initialization ctor.
585template <typename T, bool UseMemcpy>
586struct smart_copy_helper;
587
588template <typename T>
589EIGEN_DEVICE_FUNC void smart_copy(const T* start, const T* end, T* target) {
590 smart_copy_helper<T, !NumTraits<T>::RequireInitialization>::run(start, end, target);
591}
592
593template <typename T>
594struct smart_copy_helper<T, true> {
595 EIGEN_DEVICE_FUNC static inline void run(const T* start, const T* end, T* target) {
596 std::ptrdiff_t count = end - start;
597 if (count <= 0) return;
598 eigen_internal_assert(start != 0 && end != 0 && target != 0);
599 EIGEN_USING_STD(memcpy)
600 memcpy(target, start, static_cast<std::size_t>(count) * sizeof(T));
601 }
602};
603
604template <typename T>
605struct smart_copy_helper<T, false> {
606 EIGEN_DEVICE_FUNC static inline void run(const T* start, const T* end, T* target) { std::copy(start, end, target); }
607};
608
609// intelligent memmove. falls back to std::memmove for POD types, uses std::copy otherwise.
610template <typename T, bool UseMemmove>
611struct smart_memmove_helper;
612
613template <typename T>
614void smart_memmove(const T* start, const T* end, T* target) {
615 smart_memmove_helper<T, !NumTraits<T>::RequireInitialization>::run(start, end, target);
616}
617
618template <typename T>
619struct smart_memmove_helper<T, true> {
620 static inline void run(const T* start, const T* end, T* target) {
621 std::ptrdiff_t count = end - start;
622 if (count <= 0) return;
623 eigen_internal_assert(start != 0 && end != 0 && target != 0);
624 std::memmove(target, start, static_cast<std::size_t>(count) * sizeof(T));
625 }
626};
627
628template <typename T>
629struct smart_memmove_helper<T, false> {
630 static inline void run(const T* start, const T* end, T* target) {
631 if (std::uintptr_t(target) < std::uintptr_t(start)) {
632 std::copy(start, end, target);
633 } else {
634 std::ptrdiff_t count = (std::ptrdiff_t(end) - std::ptrdiff_t(start)) / sizeof(T);
635 std::copy_backward(start, end, target + count);
636 }
637 }
638};
639
640/*****************************************************************************
641*** Implementation of runtime stack allocation (falling back to malloc) ***
642*****************************************************************************/
643
644// you can overwrite Eigen's default behavior regarding alloca by defining EIGEN_ALLOCA
645// to the appropriate stack allocation function
646#if !defined EIGEN_ALLOCA && !defined EIGEN_GPU_COMPILE_PHASE
647#if EIGEN_OS_LINUX || EIGEN_OS_MAC || (defined alloca)
648#define EIGEN_ALLOCA alloca
649#elif EIGEN_COMP_MSVC
650#define EIGEN_ALLOCA _alloca
651#endif
652#endif
653
654// With clang -Oz -mthumb, alloca changes the stack pointer in a way that is
655// not allowed in Thumb2. -DEIGEN_STACK_ALLOCATION_LIMIT=0 doesn't work because
656// the compiler still emits bad code because stack allocation checks use "<=".
657// TODO: Eliminate after https://bugs.llvm.org/show_bug.cgi?id=23772
658// is fixed.
659#if defined(__clang__) && defined(__thumb__)
660#undef EIGEN_ALLOCA
661#endif
662
663// The SME GEMM kernel reads its packed panels a streaming vector at a time and slows down when they straddle
664// 64-byte lines, so SME builds align the internal temporaries to 64 bytes, heap and stack alike.
665#ifndef EIGEN_STACK_ALIGN_BYTES
666#if defined(EIGEN_VECTORIZE_SME) && EIGEN_DEFAULT_ALIGN_BYTES < 64
667#define EIGEN_STACK_ALIGN_BYTES 64
668#else
669#define EIGEN_STACK_ALIGN_BYTES EIGEN_DEFAULT_ALIGN_BYTES
670#endif
671#endif
672
674EIGEN_DEVICE_FUNC inline void* scratch_malloc(std::size_t size) {
675#if EIGEN_STACK_ALIGN_BYTES > EIGEN_DEFAULT_ALIGN_BYTES
676 void* result = handmade_aligned_malloc(size, EIGEN_STACK_ALIGN_BYTES);
677 if (!result && size) throw_std_bad_alloc();
678 return result;
679#else
680 return aligned_malloc(size);
681#endif
682}
683EIGEN_DEVICE_FUNC inline void scratch_free(void* ptr) {
684#if EIGEN_STACK_ALIGN_BYTES > EIGEN_DEFAULT_ALIGN_BYTES
685 handmade_aligned_free(ptr);
686#else
687 aligned_free(ptr);
688#endif
689}
690
692template <typename T>
693EIGEN_DEVICE_FUNC inline T* scratch_new(std::size_t size) {
694 check_size_for_overflow<T>(size);
695 T* result = static_cast<T*>(scratch_malloc(sizeof(T) * size));
696 EIGEN_TRY { default_construct_elements_of_array(result, size); }
697 EIGEN_CATCH(...) {
698 scratch_free(result);
699 EIGEN_THROW;
700 }
701 return result;
702}
703
705template <typename T>
706EIGEN_DEVICE_FUNC inline void scratch_delete(T* ptr, std::size_t size) {
707 destruct_elements_of_array<T>(ptr, size);
708 scratch_free(ptr);
709}
710
711// This helper class construct the allocated memory, and takes care of destructing and freeing the handled data
712// at destruction time. In practice this helper class is mainly useful to avoid memory leak in case of exceptions.
713template <typename T>
714class aligned_stack_memory_handler {
715 public:
716 aligned_stack_memory_handler(const aligned_stack_memory_handler&) = delete;
717 aligned_stack_memory_handler& operator=(const aligned_stack_memory_handler&) = delete;
718
719 /* Creates a stack_memory_handler responsible for the buffer \a ptr of size \a size.
720 * Note that \a ptr can be 0 regardless of the other parameters.
721 * This constructor takes care of constructing/initializing the elements of the buffer if required by the scalar type
722 *T (see NumTraits<T>::RequireInitialization). In this case, the buffer elements will also be destructed when this
723 *handler will be destructed. Finally, if \a dealloc is true, then the pointer \a ptr is freed.
724 **/
725 EIGEN_DEVICE_FUNC aligned_stack_memory_handler(T* ptr, std::size_t size, bool dealloc)
726 : m_ptr(ptr), m_size(size), m_deallocate(dealloc) {
727 EIGEN_IF_CONSTEXPR (NumTraits<T>::RequireInitialization) {
728 if (m_ptr) Eigen::internal::default_construct_elements_of_array(m_ptr, size);
729 }
730 }
731 EIGEN_DEVICE_FUNC ~aligned_stack_memory_handler() {
732 EIGEN_IF_CONSTEXPR (NumTraits<T>::RequireInitialization) {
733 if (m_ptr) Eigen::internal::destruct_elements_of_array<T>(m_ptr, m_size);
734 }
735 if (m_deallocate) Eigen::internal::scratch_free(m_ptr);
736 }
737
738 protected:
739 T* m_ptr;
740 std::size_t m_size;
741 bool m_deallocate;
742};
743
744#ifdef EIGEN_ALLOCA
745
746template <typename Xpr, int NbEvaluations,
747 bool MapExternalBuffer = nested_eval<Xpr, NbEvaluations>::Evaluate && Xpr::MaxSizeAtCompileTime == Dynamic>
748struct local_nested_eval_wrapper {
749 static constexpr bool NeedExternalBuffer = false;
750 using Scalar = typename Xpr::Scalar;
751 using ObjectType = typename nested_eval<Xpr, NbEvaluations>::type;
752 ObjectType object;
753
754 EIGEN_DEVICE_FUNC local_nested_eval_wrapper(const Xpr& xpr, Scalar* ptr) : object(xpr) {
755 EIGEN_UNUSED_VARIABLE(ptr);
756 eigen_internal_assert(ptr == 0);
757 }
758};
759
760template <typename Xpr, int NbEvaluations>
761struct local_nested_eval_wrapper<Xpr, NbEvaluations, true> {
762 static constexpr bool NeedExternalBuffer = true;
763 using Scalar = typename Xpr::Scalar;
764 using PlainObject = typename plain_object_eval<Xpr>::type;
765 using ObjectType = Map<PlainObject, EIGEN_DEFAULT_ALIGN_BYTES>;
766 ObjectType object;
767
768 EIGEN_DEVICE_FUNC local_nested_eval_wrapper(const Xpr& xpr, Scalar* ptr)
769 : object(ptr == 0 ? reinterpret_cast<Scalar*>(Eigen::internal::aligned_malloc(sizeof(Scalar) * xpr.size())) : ptr,
770 xpr.rows(), xpr.cols()),
771 m_deallocate(ptr == 0) {
772 EIGEN_IF_CONSTEXPR (NumTraits<Scalar>::RequireInitialization) {
773 if (object.data()) Eigen::internal::default_construct_elements_of_array(object.data(), object.size());
774 }
775 object = xpr;
776 }
777
778 EIGEN_DEVICE_FUNC ~local_nested_eval_wrapper() {
779 EIGEN_IF_CONSTEXPR (NumTraits<Scalar>::RequireInitialization) {
780 if (object.data()) Eigen::internal::destruct_elements_of_array(object.data(), object.size());
781 }
782 if (m_deallocate) Eigen::internal::aligned_free(object.data());
783 }
784
785 private:
786 bool m_deallocate;
787};
788
789#endif // EIGEN_ALLOCA
790
791} // end namespace internal
792
817#if defined(EIGEN_ALLOCA) && !defined(EIGEN_NO_ALLOCA)
818
819#if EIGEN_STACK_ALIGN_BYTES > 0
820// We always manually re-align the result of EIGEN_ALLOCA.
821// If alloca is already aligned, the compiler should be smart enough to optimize away the re-alignment.
822
823#if ((EIGEN_COMP_GNUC || EIGEN_COMP_CLANG) && !EIGEN_COMP_NVHPC && !EIGEN_COMP_ICC)
824#define EIGEN_ALIGNED_ALLOCA(SIZE) __builtin_alloca_with_align(SIZE, CHAR_BIT* EIGEN_STACK_ALIGN_BYTES)
825#else
826EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void* eigen_aligned_alloca_helper(void* ptr) {
827 constexpr std::uintptr_t mask = EIGEN_STACK_ALIGN_BYTES - 1;
828 std::uintptr_t ptr_int = std::uintptr_t(ptr);
829 std::uintptr_t aligned_ptr_int = (ptr_int + mask) & ~mask;
830 std::uintptr_t offset = aligned_ptr_int - ptr_int;
831 return static_cast<void*>(static_cast<uint8_t*>(ptr) + offset);
832}
833#define EIGEN_ALIGNED_ALLOCA(SIZE) eigen_aligned_alloca_helper(EIGEN_ALLOCA(SIZE + EIGEN_STACK_ALIGN_BYTES - 1))
834#endif
835
836#else
837#define EIGEN_ALIGNED_ALLOCA(SIZE) EIGEN_ALLOCA(SIZE)
838#endif
839
840#define ei_declare_aligned_stack_constructed_variable(TYPE, NAME, SIZE, BUFFER) \
841 Eigen::internal::check_size_for_overflow<TYPE>(SIZE); \
842 TYPE* NAME = (BUFFER) != 0 ? (BUFFER) \
843 : reinterpret_cast<TYPE*>((sizeof(TYPE) * (SIZE) <= EIGEN_STACK_ALLOCATION_LIMIT) \
844 ? EIGEN_ALIGNED_ALLOCA(sizeof(TYPE) * (SIZE)) \
845 : Eigen::internal::scratch_malloc(sizeof(TYPE) * (SIZE))); \
846 Eigen::internal::aligned_stack_memory_handler<TYPE> EIGEN_CAT(NAME, _stack_memory_destructor)( \
847 (BUFFER) == 0 ? NAME : 0, SIZE, sizeof(TYPE) * (SIZE) > EIGEN_STACK_ALLOCATION_LIMIT)
848
849#define ei_declare_local_nested_eval(XPR_T, XPR, N, NAME) \
850 Eigen::internal::local_nested_eval_wrapper<XPR_T, N> EIGEN_CAT(NAME, _wrapper)( \
851 XPR, reinterpret_cast<typename XPR_T::Scalar*>( \
852 ((Eigen::internal::local_nested_eval_wrapper<XPR_T, N>::NeedExternalBuffer) && \
853 ((sizeof(typename XPR_T::Scalar) * XPR.size()) <= EIGEN_STACK_ALLOCATION_LIMIT)) \
854 ? EIGEN_ALIGNED_ALLOCA(sizeof(typename XPR_T::Scalar) * XPR.size()) \
855 : 0)); \
856 typename Eigen::internal::local_nested_eval_wrapper<XPR_T, N>::ObjectType NAME(EIGEN_CAT(NAME, _wrapper).object)
857
858#else
859
860#define ei_declare_aligned_stack_constructed_variable(TYPE, NAME, SIZE, BUFFER) \
861 Eigen::internal::check_size_for_overflow<TYPE>(SIZE); \
862 TYPE* NAME = \
863 (BUFFER) != 0 ? BUFFER : reinterpret_cast<TYPE*>(Eigen::internal::scratch_malloc(sizeof(TYPE) * (SIZE))); \
864 Eigen::internal::aligned_stack_memory_handler<TYPE> EIGEN_CAT(NAME, _stack_memory_destructor)( \
865 (BUFFER) == 0 ? NAME : 0, SIZE, true)
866
867#define ei_declare_local_nested_eval(XPR_T, XPR, N, NAME) \
868 typename Eigen::internal::nested_eval<XPR_T, N>::type NAME(XPR)
869
870#endif
871
872/*****************************************************************************
873*** Implementation of EIGEN_MAKE_ALIGNED_OPERATOR_NEW [_IF] ***
874*****************************************************************************/
875
876#if EIGEN_HAS_CXX17_OVERALIGN
877
878// C++17 -> no need to bother about alignment anymore :)
879
880#define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_NOTHROW(NeedsToAlign)
881#define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(NeedsToAlign)
882#define EIGEN_MAKE_ALIGNED_OPERATOR_NEW
883#define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF_VECTORIZABLE_FIXED_SIZE(Scalar, Size)
884
885#else
886
887// HIP does not support new/delete on device.
888#if EIGEN_MAX_ALIGN_BYTES != 0 && !defined(EIGEN_HIP_DEVICE_COMPILE)
889#define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_NOTHROW(NeedsToAlign) \
890 EIGEN_DEVICE_FUNC void* operator new(std::size_t size, const std::nothrow_t&) noexcept { \
891 EIGEN_TRY { return Eigen::internal::conditional_aligned_malloc<NeedsToAlign>(size); } \
892 EIGEN_CATCH(...) { return 0; } \
893 }
894#define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(NeedsToAlign) \
895 EIGEN_DEVICE_FUNC void* operator new(std::size_t size) { \
896 return Eigen::internal::conditional_aligned_malloc<NeedsToAlign>(size); \
897 } \
898 EIGEN_DEVICE_FUNC void* operator new[](std::size_t size) { \
899 return Eigen::internal::conditional_aligned_malloc<NeedsToAlign>(size); \
900 } \
901 EIGEN_DEVICE_FUNC void operator delete(void* ptr) noexcept { \
902 Eigen::internal::conditional_aligned_free<NeedsToAlign>(ptr); \
903 } \
904 EIGEN_DEVICE_FUNC void operator delete[](void* ptr) noexcept { \
905 Eigen::internal::conditional_aligned_free<NeedsToAlign>(ptr); \
906 } \
907 EIGEN_DEVICE_FUNC void operator delete(void* ptr, std::size_t /* sz */) noexcept { \
908 Eigen::internal::conditional_aligned_free<NeedsToAlign>(ptr); \
909 } \
910 EIGEN_DEVICE_FUNC void operator delete[](void* ptr, std::size_t /* sz */) noexcept { \
911 Eigen::internal::conditional_aligned_free<NeedsToAlign>(ptr); \
912 } \
913 /* in-place new and delete. since (at least afaik) there is no actual */ \
914 /* memory allocated we can safely let the default implementation handle */ \
915 /* this particular case. */ \
916 EIGEN_DEVICE_FUNC static void* operator new(std::size_t size, void* ptr) { return ::operator new(size, ptr); } \
917 EIGEN_DEVICE_FUNC static void* operator new[](std::size_t size, void* ptr) { return ::operator new[](size, ptr); } \
918 EIGEN_DEVICE_FUNC void operator delete(void* memory, void* ptr) noexcept { return ::operator delete(memory, ptr); } \
919 EIGEN_DEVICE_FUNC void operator delete[](void* memory, void* ptr) noexcept { \
920 return ::operator delete[](memory, ptr); \
921 } \
922 /* nothrow-new (returns zero instead of std::bad_alloc) */ \
923 EIGEN_MAKE_ALIGNED_OPERATOR_NEW_NOTHROW(NeedsToAlign) \
924 EIGEN_DEVICE_FUNC void operator delete(void* ptr, const std::nothrow_t&) noexcept { \
925 Eigen::internal::conditional_aligned_free<NeedsToAlign>(ptr); \
926 } \
927 typedef void eigen_aligned_operator_new_marker_type;
928#else
929#define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(NeedsToAlign)
930#endif
931
932#define EIGEN_MAKE_ALIGNED_OPERATOR_NEW EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(true)
933#define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF_VECTORIZABLE_FIXED_SIZE(Scalar, Size) \
934 EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF( \
935 bool(((Size) != Eigen::Dynamic) && \
936 (((EIGEN_MAX_ALIGN_BYTES >= 16) && ((sizeof(Scalar) * size_t(Size)) % (EIGEN_MAX_ALIGN_BYTES) == 0)) || \
937 ((EIGEN_MAX_ALIGN_BYTES >= 32) && ((sizeof(Scalar) * size_t(Size)) % (EIGEN_MAX_ALIGN_BYTES / 2) == 0)) || \
938 ((EIGEN_MAX_ALIGN_BYTES >= 64) && ((sizeof(Scalar) * size_t(Size)) % (EIGEN_MAX_ALIGN_BYTES / 4) == 0)))))
939
940#endif
941
942/****************************************************************************/
943
968template <class T>
969class aligned_allocator {
970 public:
971 using size_type = std::size_t;
972 using difference_type = std::ptrdiff_t;
973 using pointer = T*;
974 using const_pointer = const T*;
975 using reference = T&;
976 using const_reference = const T&;
977 using value_type = T;
978
979 template <class U>
980 struct rebind {
981 using other = aligned_allocator<U>;
982 };
983
984 aligned_allocator() = default;
985
986 aligned_allocator(const aligned_allocator&) = default;
987
988 template <class U>
989 aligned_allocator(const aligned_allocator<U>&) {}
990
991 template <class U>
992 constexpr bool operator==(const aligned_allocator<U>&) const noexcept {
993 return true;
994 }
995 template <class U>
996 constexpr bool operator!=(const aligned_allocator<U>&) const noexcept {
997 return false;
998 }
999
1000#if EIGEN_COMP_GNUC_STRICT && EIGEN_GNUC_STRICT_AT_LEAST(7, 0, 0)
1001 // In gcc std::allocator::max_size() is bugged making gcc triggers a warning:
1002 // eigen/Eigen/src/Core/util/Memory.h:189:12: warning: argument 1 value '18446744073709551612' exceeds maximum object
1003 // size 9223372036854775807 See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87544
1004 size_type max_size() const { return (std::numeric_limits<std::ptrdiff_t>::max)() / sizeof(T); }
1005#endif
1006
1007 pointer allocate(size_type num, const void* /*hint*/ = 0) {
1008 internal::check_size_for_overflow<T>(num);
1009 return static_cast<pointer>(internal::aligned_malloc(num * sizeof(T)));
1010 }
1011
1012 void deallocate(pointer p, size_type /*num*/) { internal::aligned_free(p); }
1013};
1014
1015//---------- Cache sizes ----------
1016
1017#if EIGEN_OS_LINUX && !defined(EIGEN_NO_CPU_CACHE_SYSFS)
1018// Linux publishes the cache topology under sysfs on every architecture.
1019#define EIGEN_CPU_CACHE_SYSFS 1
1020#endif
1021
1022#if !defined(EIGEN_NO_CPUID)
1023#if EIGEN_COMP_GNUC && EIGEN_ARCH_i386_OR_x86_64
1024#if defined(__PIC__) && EIGEN_ARCH_i386
1025// Case for x86 with PIC
1026#define EIGEN_CPUID(abcd, func, id) \
1027 __asm__ __volatile__("xchgl %%ebx, %k1;cpuid; xchgl %%ebx,%k1" \
1028 : "=a"(abcd[0]), "=&r"(abcd[1]), "=c"(abcd[2]), "=d"(abcd[3]) \
1029 : "a"(func), "c"(id));
1030#elif defined(__PIC__) && EIGEN_ARCH_x86_64
1031// Case for x64 with PIC. In theory this is only a problem with recent gcc and with medium or large code model, not with
1032// the default small code model. However, we cannot detect which code model is used, and the xchg overhead is negligible
1033// anyway.
1034#define EIGEN_CPUID(abcd, func, id) \
1035 __asm__ __volatile__("xchg{q}\t{%%}rbx, %q1; cpuid; xchg{q}\t{%%}rbx, %q1" \
1036 : "=a"(abcd[0]), "=&r"(abcd[1]), "=c"(abcd[2]), "=d"(abcd[3]) \
1037 : "0"(func), "2"(id));
1038#else
1039// Case for x86_64 or x86 w/o PIC
1040#define EIGEN_CPUID(abcd, func, id) \
1041 __asm__ __volatile__("cpuid" : "=a"(abcd[0]), "=b"(abcd[1]), "=c"(abcd[2]), "=d"(abcd[3]) : "0"(func), "2"(id));
1042#endif
1043#elif EIGEN_COMP_MSVC
1044#if EIGEN_ARCH_i386_OR_x86_64
1045#define EIGEN_CPUID(abcd, func, id) __cpuidex((int*)abcd, func, id)
1046#endif
1047#endif
1048#endif
1049
1050namespace internal {
1051
1052#ifdef EIGEN_CPUID
1053
1054inline bool cpuid_is_vendor(int abcd[4], const int vendor[3]) {
1055 return abcd[1] == vendor[0] && abcd[3] == vendor[1] && abcd[2] == vendor[2];
1056}
1057
1058inline void queryCacheSizes_intel_direct(std::ptrdiff_t& l1, std::ptrdiff_t& l2, std::ptrdiff_t& l3) {
1059 int abcd[4];
1060 l1 = l2 = l3 = 0;
1061 int cache_id = 0;
1062 int cache_type = 0;
1063 do {
1064 abcd[0] = abcd[1] = abcd[2] = abcd[3] = 0;
1065 EIGEN_CPUID(abcd, 0x4, cache_id);
1066 cache_type = (abcd[0] & 0x0F) >> 0;
1067 if (cache_type == 1 || cache_type == 3) // data or unified cache
1068 {
1069 int cache_level = (abcd[0] & 0xE0) >> 5; // A[7:5]
1070 int ways = (abcd[1] & 0xFFC00000) >> 22; // B[31:22]
1071 int partitions = (abcd[1] & 0x003FF000) >> 12; // B[21:12]
1072 int line_size = (abcd[1] & 0x00000FFF) >> 0; // B[11:0]
1073 int sets = (abcd[2]); // C[31:0]
1074
1075 std::ptrdiff_t cache_size =
1076 static_cast<std::ptrdiff_t>(ways + 1) * (partitions + 1) * (line_size + 1) * (sets + 1);
1077
1078 switch (cache_level) {
1079 case 1:
1080 l1 = cache_size;
1081 break;
1082 case 2:
1083 l2 = cache_size;
1084 break;
1085 case 3:
1086 l3 = cache_size;
1087 break;
1088 default:
1089 break;
1090 }
1091 }
1092 cache_id++;
1093 } while (cache_type > 0 && cache_id < 16);
1094}
1095
1096inline void queryCacheSizes_intel_codes(std::ptrdiff_t& l1, std::ptrdiff_t& l2, std::ptrdiff_t& l3) {
1097 int abcd[4];
1098 abcd[0] = abcd[1] = abcd[2] = abcd[3] = 0;
1099 l1 = l2 = l3 = 0;
1100 EIGEN_CPUID(abcd, 0x00000002, 0);
1101 unsigned char* bytes = reinterpret_cast<unsigned char*>(abcd) + 2;
1102 bool check_for_p2_core2 = false;
1103 for (int i = 0; i < 14; ++i) {
1104 switch (bytes[i]) {
1105 case 0x0A:
1106 l1 = 8;
1107 break; // 0Ah data L1 cache, 8 KB, 2 ways, 32 byte lines
1108 case 0x0C:
1109 l1 = 16;
1110 break; // 0Ch data L1 cache, 16 KB, 4 ways, 32 byte lines
1111 case 0x0E:
1112 l1 = 24;
1113 break; // 0Eh data L1 cache, 24 KB, 6 ways, 64 byte lines
1114 case 0x10:
1115 l1 = 16;
1116 break; // 10h data L1 cache, 16 KB, 4 ways, 32 byte lines (IA-64)
1117 case 0x15:
1118 l1 = 16;
1119 break; // 15h code L1 cache, 16 KB, 4 ways, 32 byte lines (IA-64)
1120 case 0x2C:
1121 l1 = 32;
1122 break; // 2Ch data L1 cache, 32 KB, 8 ways, 64 byte lines
1123 case 0x30:
1124 l1 = 32;
1125 break; // 30h code L1 cache, 32 KB, 8 ways, 64 byte lines
1126 case 0x60:
1127 l1 = 16;
1128 break; // 60h data L1 cache, 16 KB, 8 ways, 64 byte lines, sectored
1129 case 0x66:
1130 l1 = 8;
1131 break; // 66h data L1 cache, 8 KB, 4 ways, 64 byte lines, sectored
1132 case 0x67:
1133 l1 = 16;
1134 break; // 67h data L1 cache, 16 KB, 4 ways, 64 byte lines, sectored
1135 case 0x68:
1136 l1 = 32;
1137 break; // 68h data L1 cache, 32 KB, 4 ways, 64 byte lines, sectored
1138 case 0x1A:
1139 l2 = 96;
1140 break; // code and data L2 cache, 96 KB, 6 ways, 64 byte lines (IA-64)
1141 case 0x22:
1142 l3 = 512;
1143 break; // code and data L3 cache, 512 KB, 4 ways (!), 64 byte lines, dual-sectored
1144 case 0x23:
1145 l3 = 1024;
1146 break; // code and data L3 cache, 1024 KB, 8 ways, 64 byte lines, dual-sectored
1147 case 0x25:
1148 l3 = 2048;
1149 break; // code and data L3 cache, 2048 KB, 8 ways, 64 byte lines, dual-sectored
1150 case 0x29:
1151 l3 = 4096;
1152 break; // code and data L3 cache, 4096 KB, 8 ways, 64 byte lines, dual-sectored
1153 case 0x39:
1154 l2 = 128;
1155 break; // code and data L2 cache, 128 KB, 4 ways, 64 byte lines, sectored
1156 case 0x3A:
1157 l2 = 192;
1158 break; // code and data L2 cache, 192 KB, 6 ways, 64 byte lines, sectored
1159 case 0x3B:
1160 l2 = 128;
1161 break; // code and data L2 cache, 128 KB, 2 ways, 64 byte lines, sectored
1162 case 0x3C:
1163 l2 = 256;
1164 break; // code and data L2 cache, 256 KB, 4 ways, 64 byte lines, sectored
1165 case 0x3D:
1166 l2 = 384;
1167 break; // code and data L2 cache, 384 KB, 6 ways, 64 byte lines, sectored
1168 case 0x3E:
1169 l2 = 512;
1170 break; // code and data L2 cache, 512 KB, 4 ways, 64 byte lines, sectored
1171 case 0x40:
1172 l2 = 0;
1173 break; // no integrated L2 cache (P6 core) or L3 cache (P4 core)
1174 case 0x41:
1175 l2 = 128;
1176 break; // code and data L2 cache, 128 KB, 4 ways, 32 byte lines
1177 case 0x42:
1178 l2 = 256;
1179 break; // code and data L2 cache, 256 KB, 4 ways, 32 byte lines
1180 case 0x43:
1181 l2 = 512;
1182 break; // code and data L2 cache, 512 KB, 4 ways, 32 byte lines
1183 case 0x44:
1184 l2 = 1024;
1185 break; // code and data L2 cache, 1024 KB, 4 ways, 32 byte lines
1186 case 0x45:
1187 l2 = 2048;
1188 break; // code and data L2 cache, 2048 KB, 4 ways, 32 byte lines
1189 case 0x46:
1190 l3 = 4096;
1191 break; // code and data L3 cache, 4096 KB, 4 ways, 64 byte lines
1192 case 0x47:
1193 l3 = 8192;
1194 break; // code and data L3 cache, 8192 KB, 8 ways, 64 byte lines
1195 case 0x48:
1196 l2 = 3072;
1197 break; // code and data L2 cache, 3072 KB, 12 ways, 64 byte lines
1198 case 0x49:
1199 if (l2 != 0)
1200 l3 = 4096;
1201 else {
1202 check_for_p2_core2 = true;
1203 l3 = l2 = 4096;
1204 }
1205 break; // code and data L3 cache, 4096 KB, 16 ways, 64 byte lines (P4) or L2 for core2
1206 case 0x4A:
1207 l3 = 6144;
1208 break; // code and data L3 cache, 6144 KB, 12 ways, 64 byte lines
1209 case 0x4B:
1210 l3 = 8192;
1211 break; // code and data L3 cache, 8192 KB, 16 ways, 64 byte lines
1212 case 0x4C:
1213 l3 = 12288;
1214 break; // code and data L3 cache, 12288 KB, 12 ways, 64 byte lines
1215 case 0x4D:
1216 l3 = 16384;
1217 break; // code and data L3 cache, 16384 KB, 16 ways, 64 byte lines
1218 case 0x4E:
1219 l2 = 6144;
1220 break; // code and data L2 cache, 6144 KB, 24 ways, 64 byte lines
1221 case 0x78:
1222 l2 = 1024;
1223 break; // code and data L2 cache, 1024 KB, 4 ways, 64 byte lines
1224 case 0x79:
1225 l2 = 128;
1226 break; // code and data L2 cache, 128 KB, 8 ways, 64 byte lines, dual-sectored
1227 case 0x7A:
1228 l2 = 256;
1229 break; // code and data L2 cache, 256 KB, 8 ways, 64 byte lines, dual-sectored
1230 case 0x7B:
1231 l2 = 512;
1232 break; // code and data L2 cache, 512 KB, 8 ways, 64 byte lines, dual-sectored
1233 case 0x7C:
1234 l2 = 1024;
1235 break; // code and data L2 cache, 1024 KB, 8 ways, 64 byte lines, dual-sectored
1236 case 0x7D:
1237 l2 = 2048;
1238 break; // code and data L2 cache, 2048 KB, 8 ways, 64 byte lines
1239 case 0x7E:
1240 l2 = 256;
1241 break; // code and data L2 cache, 256 KB, 8 ways, 128 byte lines, sect. (IA-64)
1242 case 0x7F:
1243 l2 = 512;
1244 break; // code and data L2 cache, 512 KB, 2 ways, 64 byte lines
1245 case 0x80:
1246 l2 = 512;
1247 break; // code and data L2 cache, 512 KB, 8 ways, 64 byte lines
1248 case 0x81:
1249 l2 = 128;
1250 break; // code and data L2 cache, 128 KB, 8 ways, 32 byte lines
1251 case 0x82:
1252 l2 = 256;
1253 break; // code and data L2 cache, 256 KB, 8 ways, 32 byte lines
1254 case 0x83:
1255 l2 = 512;
1256 break; // code and data L2 cache, 512 KB, 8 ways, 32 byte lines
1257 case 0x84:
1258 l2 = 1024;
1259 break; // code and data L2 cache, 1024 KB, 8 ways, 32 byte lines
1260 case 0x85:
1261 l2 = 2048;
1262 break; // code and data L2 cache, 2048 KB, 8 ways, 32 byte lines
1263 case 0x86:
1264 l2 = 512;
1265 break; // code and data L2 cache, 512 KB, 4 ways, 64 byte lines
1266 case 0x87:
1267 l2 = 1024;
1268 break; // code and data L2 cache, 1024 KB, 8 ways, 64 byte lines
1269 case 0x88:
1270 l3 = 2048;
1271 break; // code and data L3 cache, 2048 KB, 4 ways, 64 byte lines (IA-64)
1272 case 0x89:
1273 l3 = 4096;
1274 break; // code and data L3 cache, 4096 KB, 4 ways, 64 byte lines (IA-64)
1275 case 0x8A:
1276 l3 = 8192;
1277 break; // code and data L3 cache, 8192 KB, 4 ways, 64 byte lines (IA-64)
1278 case 0x8D:
1279 l3 = 3072;
1280 break; // code and data L3 cache, 3072 KB, 12 ways, 128 byte lines (IA-64)
1281
1282 default:
1283 break;
1284 }
1285 }
1286 if (check_for_p2_core2 && l2 == l3) l3 = 0;
1287 l1 *= 1024;
1288 l2 *= 1024;
1289 l3 *= 1024;
1290}
1291
1292inline void queryCacheSizes_intel(std::ptrdiff_t& l1, std::ptrdiff_t& l2, std::ptrdiff_t& l3, int max_std_funcs) {
1293 if (max_std_funcs >= 4)
1294 queryCacheSizes_intel_direct(l1, l2, l3);
1295 else if (max_std_funcs >= 2)
1296 queryCacheSizes_intel_codes(l1, l2, l3);
1297 else
1298 l1 = l2 = l3 = 0;
1299}
1300
1301inline void queryCacheSizes_amd(std::ptrdiff_t& l1, std::ptrdiff_t& l2, std::ptrdiff_t& l3) {
1302 int abcd[4];
1303 abcd[0] = abcd[1] = abcd[2] = abcd[3] = 0;
1304
1305 // First query the max supported function.
1306 EIGEN_CPUID(abcd, 0x80000000, 0);
1307 if (static_cast<numext::uint32_t>(abcd[0]) >= static_cast<numext::uint32_t>(0x80000006)) {
1308 EIGEN_CPUID(abcd, 0x80000005, 0);
1309 l1 = (abcd[2] >> 24) * 1024; // C[31:24] = L1 size in KB
1310 abcd[0] = abcd[1] = abcd[2] = abcd[3] = 0;
1311 EIGEN_CPUID(abcd, 0x80000006, 0);
1312 l2 = (abcd[2] >> 16) * 1024; // C[31;16] = l2 cache size in KB
1313 l3 = static_cast<std::ptrdiff_t>((abcd[3] & 0xFFFC000) >> 18) * 512 * 1024; // D[31;18] = l3 cache size in 512KB
1314 } else {
1315 l1 = l2 = l3 = 0;
1316 }
1317}
1318#endif
1319
1320#ifdef EIGEN_CPU_CACHE_SYSFS
1321
1323struct CpuCacheTopology {
1324 std::ptrdiff_t l1 = 0;
1325 std::ptrdiff_t l2 = 0;
1326 std::ptrdiff_t l3 = 0;
1327 std::ptrdiff_t l3_per_cpu = 0;
1328};
1329
1332template <int Size>
1333inline bool readSysfsLine(const char* root, const char* relative, char (&value)[Size]) {
1334 char path[512];
1335 const int length = std::snprintf(path, sizeof(path), "%s/%s", root, relative);
1336 if (length <= 0 || length >= static_cast<int>(sizeof(path))) return false;
1337 const int fd = ::open(path, O_RDONLY | O_CLOEXEC);
1338 if (fd < 0) return false;
1339 const ssize_t bytes_read = ::read(fd, value, Size - 1);
1340 ::close(fd);
1341 if (bytes_read <= 0) return false;
1342 value[bytes_read] = '\0';
1343 char* newline = std::strchr(value, '\n');
1344 if (newline != nullptr) {
1345 newline[1] = '\0';
1346 }
1347 return true;
1348}
1349
1351template <int Size>
1352inline bool readCpuCacheAttribute(const char* root, int cpu, int index, const char* name, char (&value)[Size]) {
1353 char relative[64];
1354 const int length = std::snprintf(relative, sizeof(relative), "cpu%d/cache/index%d/%s", cpu, index, name);
1355 return length > 0 && length < static_cast<int>(sizeof(relative)) && readSysfsLine(root, relative, value);
1356}
1357
1359inline std::ptrdiff_t parseCpuCacheSize(const char* text) {
1360 char* suffix = nullptr;
1361 const std::ptrdiff_t value = std::strtol(text, &suffix, 10);
1362 if (value <= 0) return 0;
1363 // The kernel writes kibibytes ("%uK"), but only scale on an explicit unit: reading a bare byte
1364 // count as kibibytes would overstate a cache 1024x, which is much worse than the reverse.
1365 const std::ptrdiff_t multiplier = (*suffix == 'K' || *suffix == 'k') ? 1024
1366 : (*suffix == 'M' || *suffix == 'm') ? 1024 * 1024
1367 : 1;
1368 return value * multiplier;
1369}
1370
1372inline bool isCpuListTerminator(const char* text) { return *text == '\0' || *text == '\n' || *text == '\r'; }
1373
1378template <typename Visitor>
1379inline bool parseCpuList(const char* text, Visitor&& visit) {
1380 if (isCpuListTerminator(text)) return false;
1381 // Far above the 8192 CPUs current kernels can number, so a larger id means the line is not a cpu list;
1382 // bounding it also keeps a caller that walks the ranges from visiting ids that cannot exist.
1383 const long max_cpu_id = 1 << 16;
1384 const char* cursor = text;
1385 for (;;) {
1386 char* end = nullptr;
1387 const long first = std::strtol(cursor, &end, 10);
1388 if (end == cursor || first < 0) return false;
1389 long last = first;
1390 if (*end == '-') {
1391 cursor = end + 1;
1392 last = std::strtol(cursor, &end, 10);
1393 if (end == cursor || last < first) return false;
1394 }
1395 if (last >= max_cpu_id) return false;
1396 visit(static_cast<int>(first), static_cast<int>(last));
1397 // A comma promises another range, so a list ending on one is malformed: going round the loop
1398 // lands on the terminator and fails the strtol above.
1399 if (*end == ',') {
1400 cursor = end + 1;
1401 continue;
1402 }
1403 // Anything other than a separator or the end of the line means the format is not what this
1404 // parser assumes.
1405 return isCpuListTerminator(end);
1406 }
1407}
1408
1412inline int parseCpuListCount(const char* text) {
1413 int count = 0;
1414 return parseCpuList(text, [&count](int first, int last) { count += last - first + 1; }) ? count : 0;
1415}
1416
1424inline CpuCacheTopology readCpuCacheTopologySysfs(const char* root, int cpu) {
1425 CpuCacheTopology topology;
1426 // One directory per cache, numbered contiguously from zero. The bound only guards a malformed
1427 // sysfs; it is far above what any current CPU reports.
1428 for (int index = 0; index < 16; ++index) {
1429 // A shared_cpu_list can be long on a large machine, and truncating it would undercount the
1430 // sharers and hand out too large a share.
1431 char value[512];
1432
1433 if (!readCpuCacheAttribute(root, cpu, index, "level", value)) break;
1434 const long level = std::strtol(value, nullptr, 10);
1435 std::ptrdiff_t* target =
1436 level == 1 ? &topology.l1 : (level == 2 ? &topology.l2 : (level == 3 ? &topology.l3 : nullptr));
1437 if (target == nullptr || *target > 0) continue;
1438
1439 // "Data", "Instruction", or "Unified". An instruction cache never holds the operands a product
1440 // blocks for, so it must not be mistaken for the L1 data cache.
1441 if (!readCpuCacheAttribute(root, cpu, index, "type", value) || value[0] == 'I') continue;
1442
1443 if (!readCpuCacheAttribute(root, cpu, index, "size", value)) continue;
1444 const std::ptrdiff_t size = parseCpuCacheSize(value);
1445 if (size <= 0) continue;
1446 *target = size;
1447
1448 if (level == 3 && readCpuCacheAttribute(root, cpu, index, "shared_cpu_list", value) &&
1449 std::strchr(value, '\n') != nullptr) {
1450 const int sharing = parseCpuListCount(value);
1451 if (sharing > 0) topology.l3_per_cpu = size / sharing;
1452 }
1453 }
1454 return topology;
1455}
1456
1458inline std::ptrdiff_t smallerReportedCacheSize(std::ptrdiff_t a, std::ptrdiff_t b) {
1459 return a == 0 ? b : (b == 0 ? a : (std::min)(a, b));
1460}
1461
1467template <typename IsAllowed>
1468inline CpuCacheTopology queryCpuCacheTopologySysfs(const char* root, IsAllowed&& is_allowed) {
1469 CpuCacheTopology topology;
1470 char online[512];
1471 if (!readSysfsLine(root, "online", online) || std::strchr(online, '\n') == nullptr) return topology;
1472 // Validate the whole line before acting on any range of it, so that a malformed list reads as "unknown".
1473 if (!parseCpuList(online, [](int, int) {})) return topology;
1474 parseCpuList(online, [&](int first, int last) {
1475 for (int cpu = first; cpu <= last; ++cpu) {
1476 if (!is_allowed(cpu)) continue;
1477 const CpuCacheTopology candidate = readCpuCacheTopologySysfs(root, cpu);
1478 topology.l1 = smallerReportedCacheSize(topology.l1, candidate.l1);
1479 topology.l2 = smallerReportedCacheSize(topology.l2, candidate.l2);
1480 topology.l3 = smallerReportedCacheSize(topology.l3, candidate.l3);
1481 topology.l3_per_cpu = smallerReportedCacheSize(topology.l3_per_cpu, candidate.l3_per_cpu);
1482 }
1483 });
1484 return topology;
1485}
1486
1493inline CpuCacheTopology queryCpuCacheTopologySysfs(const char* root = "/sys/devices/system/cpu") {
1494#ifdef CPU_SETSIZE
1495 // The affinity API is a GNU extension that glibc, musl and bionic expose under _GNU_SOURCE, which g++ and
1496 // clang++ predefine for C++; CPU_SETSIZE is defined exactly when it is exposed. The query fails on a machine
1497 // with more CPU ids than cpu_set_t holds.
1498 cpu_set_t allowed;
1499 if (sched_getaffinity(0, sizeof(allowed), &allowed) == 0) {
1500 return queryCpuCacheTopologySysfs(root,
1501 [&allowed](int cpu) { return cpu < CPU_SETSIZE && CPU_ISSET(cpu, &allowed); });
1502 }
1503#endif
1504 return queryCpuCacheTopologySysfs(root, [](int) { return true; });
1505}
1506
1507#endif // EIGEN_CPU_CACHE_SYSFS
1508
1512inline void queryCacheSizes(std::ptrdiff_t& l1, std::ptrdiff_t& l2, std::ptrdiff_t& l3, std::ptrdiff_t& l3_per_cpu) {
1513 l3_per_cpu = 0;
1514#ifdef EIGEN_CPUID
1515 int abcd[4];
1516 const int GenuineIntel[] = {0x756e6547, 0x49656e69, 0x6c65746e};
1517 const int AuthenticAMD[] = {0x68747541, 0x69746e65, 0x444d4163};
1518 const int AMDisbetter_[] = {0x69444d41, 0x74656273, 0x21726574}; // "AMDisbetter!"
1519 // Hygon Dhyana is a Zen-based joint-venture line that returns "HygonGenuine"
1520 // and shares AMD's CPUID layout for cache descriptors (AMD Family 17h).
1521 const int HygonGenuine[] = {0x6f677948, 0x6e65476e, 0x656e6975}; // "HygonGenuine"
1522
1523 // identify the CPU vendor
1524 EIGEN_CPUID(abcd, 0x0, 0);
1525 int max_std_funcs = abcd[0];
1526 if (cpuid_is_vendor(abcd, GenuineIntel))
1527 queryCacheSizes_intel(l1, l2, l3, max_std_funcs);
1528 else if (cpuid_is_vendor(abcd, AuthenticAMD) || cpuid_is_vendor(abcd, AMDisbetter_) ||
1529 cpuid_is_vendor(abcd, HygonGenuine))
1530 queryCacheSizes_amd(l1, l2, l3);
1531 else
1532 // by default let's use Intel's API
1533 queryCacheSizes_intel(l1, l2, l3, max_std_funcs);
1534
1535 // here is the list of other vendors:
1536 // ||cpuid_is_vendor(abcd,"VIA VIA VIA ")
1537 // ||cpuid_is_vendor(abcd,"CyrixInstead")
1538 // ||cpuid_is_vendor(abcd,"CentaurHauls")
1539 // ||cpuid_is_vendor(abcd,"GenuineTMx86")
1540 // ||cpuid_is_vendor(abcd,"TransmetaCPU")
1541 // ||cpuid_is_vendor(abcd,"RiseRiseRise")
1542 // ||cpuid_is_vendor(abcd,"Geode by NSC")
1543 // ||cpuid_is_vendor(abcd,"SiS SiS SiS ")
1544 // ||cpuid_is_vendor(abcd,"UMC UMC UMC ")
1545 // ||cpuid_is_vendor(abcd,"NexGenDriven")
1546#elif EIGEN_OS_MAC
1547 // On macOS (including Apple Silicon), use sysctlbyname to query cache sizes.
1548 // The sysctl values are 64-bit, so read into int64_t and convert.
1549 // For L1, prefer P-core (perflevel0) size since compute-heavy work like GEMM
1550 // is typically scheduled on performance cores. L1 is per-core so always safe.
1551 // For L2, use the generic hw.l2cachesize which is more conservative (reports
1552 // the smaller E-core cluster L2 on heterogeneous chips). The P-core L2 is
1553 // shared among all P-cores and would overestimate per-core capacity.
1554 {
1555 int64_t val = 0;
1556 std::size_t val_size = sizeof(val);
1557 l1 = -1;
1558 val_size = sizeof(val);
1559 if (sysctlbyname("hw.perflevel0.l1dcachesize", &val, &val_size, nullptr, 0) == 0 && val > 0)
1560 l1 = val;
1561 else {
1562 val_size = sizeof(val);
1563 if (sysctlbyname("hw.l1dcachesize", &val, &val_size, nullptr, 0) == 0) l1 = val;
1564 }
1565 l2 = -1;
1566 val_size = sizeof(val);
1567 if (sysctlbyname("hw.l2cachesize", &val, &val_size, nullptr, 0) == 0) l2 = val;
1568 l3 = -1;
1569 val_size = sizeof(val);
1570 if (sysctlbyname("hw.l3cachesize", &val, &val_size, nullptr, 0) == 0 && val > 0) l3 = val;
1571 }
1572#elif EIGEN_OS_UNIX && defined(_SC_LEVEL1_DCACHE_SIZE)
1573 // A glibc extension: POSIX specifies no cache queries, and musl defines none of these names.
1574 l1 = sysconf(_SC_LEVEL1_DCACHE_SIZE);
1575 l2 = sysconf(_SC_LEVEL2_CACHE_SIZE);
1576 l3 = sysconf(_SC_LEVEL3_CACHE_SIZE);
1577#else
1578 l1 = l2 = l3 = -1;
1579#endif
1580#ifdef EIGEN_CPU_CACHE_SYSFS
1581 // glibc answers the _SC_LEVEL*_CACHE_SIZE queries from CPUID and so only implements them on x86; every
1582 // other architecture gets 0, and musl has no such queries at all. Whatever the platform left unknown comes
1583 // from the topology Linux publishes on every architecture, as does the L3 share, so that all four numbers
1584 // describe the same CPUs.
1585 const CpuCacheTopology topology = queryCpuCacheTopologySysfs();
1586 if (l1 <= 0) l1 = topology.l1;
1587 if (l2 <= 0) l2 = topology.l2;
1588 if (l3 <= 0) l3 = topology.l3;
1589 l3_per_cpu = topology.l3_per_cpu;
1590#endif
1591}
1592
1595inline void queryCacheSizes(std::ptrdiff_t& l1, std::ptrdiff_t& l2, std::ptrdiff_t& l3) {
1596 std::ptrdiff_t l3_per_cpu;
1597 queryCacheSizes(l1, l2, l3, l3_per_cpu);
1598}
1599
1602inline std::ptrdiff_t queryL1CacheSize() {
1603 std::ptrdiff_t l1(-1), l2, l3;
1604 queryCacheSizes(l1, l2, l3);
1605 return l1;
1606}
1607
1610inline std::ptrdiff_t queryTopLevelCacheSize() {
1611 std::ptrdiff_t l1, l2(-1), l3(-1);
1612 queryCacheSizes(l1, l2, l3);
1613 return (std::max)(l2, l3);
1614}
1615
1619
1620#if !defined(EIGEN_GPU_COMPILE_PHASE) && EIGEN_COMP_CXXVER >= 20 && defined(__cpp_lib_constexpr_dynamic_alloc) && \
1621 __cpp_lib_constexpr_dynamic_alloc >= 201907L
1622using std::construct_at;
1623#else
1624template <class T, class... Args>
1625EIGEN_DEVICE_FUNC T* construct_at(T* p, Args&&... args) {
1626 return ::new (const_cast<void*>(static_cast<const volatile void*>(p))) T(std::forward<Args>(args)...);
1627}
1628#endif
1629
1635#if EIGEN_COMP_CXXVER >= 17
1636using std::destroy_at;
1637#else
1638template <class T>
1639EIGEN_DEVICE_FUNC void destroy_at(T* p) {
1640 p->~T();
1641}
1642#endif
1643
1644// FIXME(rmlarsen): Work around missing linker symbol with msan on ARM.
1645#if !defined(EIGEN_DONT_ASSUME_ALIGNED) && __has_feature(memory_sanitizer) && (EIGEN_ARCH_ARM || EIGEN_ARCH_ARM64)
1646#define EIGEN_DONT_ASSUME_ALIGNED
1647#endif
1648
1649#if !defined(EIGEN_DONT_ASSUME_ALIGNED) && defined(__cpp_lib_assume_aligned) && (__cpp_lib_assume_aligned >= 201811L)
1650template <std::size_t N, typename T>
1651EIGEN_DEVICE_FUNC constexpr T* assume_aligned(T* ptr) {
1652 return std::assume_aligned<N, T>(ptr);
1653}
1654#elif !defined(EIGEN_DONT_ASSUME_ALIGNED) && EIGEN_HAS_BUILTIN(__builtin_assume_aligned)
1655template <std::size_t N, typename T>
1656EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC T* assume_aligned(T* ptr) {
1657 return static_cast<T*>(__builtin_assume_aligned(ptr, N));
1658}
1659#else
1660template <std::size_t N, typename T>
1661EIGEN_DEVICE_FUNC constexpr T* assume_aligned(T* ptr) {
1662 return ptr;
1663}
1664#endif
1665
1666} // end namespace internal
1667
1668} // end namespace Eigen
1669
1670#endif // EIGEN_MEMORY_H