/home/runner/work/LabNeura/LabNeura/include/labneura/utils/alignment.hpp
Line | Count | Source (jump to first uncovered line) |
1 | | #pragma once |
2 | | |
3 | | #include <cstddef> |
4 | | #include <cstdint> |
5 | | #include <cstdlib> |
6 | | |
7 | | namespace labneura { |
8 | | namespace util { |
9 | | |
10 | 1.25k | inline bool is_aligned(const void* p, std::size_t alignment) { |
11 | 1.25k | return reinterpret_cast<std::uintptr_t>(p) % alignment == 0; |
12 | 1.25k | } |
13 | | |
14 | | // Allocate 'count' elements of type T with the given alignment. |
15 | | // Returns nullptr on failure. |
16 | | template <typename T> |
17 | 2.02k | inline T* allocate_aligned(std::size_t count, std::size_t alignment = 64) { |
18 | | #if defined(_MSC_VER) |
19 | | void* p = _aligned_malloc(count * sizeof(T), alignment); |
20 | | return static_cast<T*>(p); |
21 | | #else |
22 | 2.02k | void* p = nullptr; |
23 | 2.02k | if (posix_memalign(&p, alignment, count * sizeof(T)) == 0) { |
24 | 2.02k | return static_cast<T*>(p); |
25 | 2.02k | } |
26 | | // Fallback to malloc (not guaranteed alignment) |
27 | 0 | return static_cast<T*>(std::malloc(count * sizeof(T))); |
28 | 2.02k | #endif |
29 | 2.02k | } _ZN8labneura4util16allocate_alignedIfEEPT_mm Line | Count | Source | 17 | 1.05k | inline T* allocate_aligned(std::size_t count, std::size_t alignment = 64) { | 18 | | #if defined(_MSC_VER) | 19 | | void* p = _aligned_malloc(count * sizeof(T), alignment); | 20 | | return static_cast<T*>(p); | 21 | | #else | 22 | 1.05k | void* p = nullptr; | 23 | 1.05k | if (posix_memalign(&p, alignment, count * sizeof(T)) == 0) { | 24 | 1.05k | return static_cast<T*>(p); | 25 | 1.05k | } | 26 | | // Fallback to malloc (not guaranteed alignment) | 27 | 0 | return static_cast<T*>(std::malloc(count * sizeof(T))); | 28 | 1.05k | #endif | 29 | 1.05k | } |
_ZN8labneura4util16allocate_alignedIsEEPT_mm Line | Count | Source | 17 | 436 | inline T* allocate_aligned(std::size_t count, std::size_t alignment = 64) { | 18 | | #if defined(_MSC_VER) | 19 | | void* p = _aligned_malloc(count * sizeof(T), alignment); | 20 | | return static_cast<T*>(p); | 21 | | #else | 22 | 436 | void* p = nullptr; | 23 | 436 | if (posix_memalign(&p, alignment, count * sizeof(T)) == 0) { | 24 | 436 | return static_cast<T*>(p); | 25 | 436 | } | 26 | | // Fallback to malloc (not guaranteed alignment) | 27 | 0 | return static_cast<T*>(std::malloc(count * sizeof(T))); | 28 | 436 | #endif | 29 | 436 | } |
_ZN8labneura4util16allocate_alignedIaEEPT_mm Line | Count | Source | 17 | 530 | inline T* allocate_aligned(std::size_t count, std::size_t alignment = 64) { | 18 | | #if defined(_MSC_VER) | 19 | | void* p = _aligned_malloc(count * sizeof(T), alignment); | 20 | | return static_cast<T*>(p); | 21 | | #else | 22 | 530 | void* p = nullptr; | 23 | 530 | if (posix_memalign(&p, alignment, count * sizeof(T)) == 0) { | 24 | 530 | return static_cast<T*>(p); | 25 | 530 | } | 26 | | // Fallback to malloc (not guaranteed alignment) | 27 | 0 | return static_cast<T*>(std::malloc(count * sizeof(T))); | 28 | 530 | #endif | 29 | 530 | } |
|
30 | | |
31 | 2.02k | inline void aligned_free(void* p) { |
32 | | #if defined(_MSC_VER) |
33 | | if (p) _aligned_free(p); |
34 | | #else |
35 | 2.02k | if (p) std::free(p); |
36 | 2.02k | #endif |
37 | 2.02k | } |
38 | | |
39 | | // Typed helpers for unique_ptr function-pointer deleters |
40 | 0 | inline void free_aligned_double(double* p) { aligned_free(p); } |
41 | 1.05k | inline void free_aligned_float(float* p) { aligned_free(p); } |
42 | 0 | inline void free_aligned_int16(int16_t* p) { aligned_free(p); } |
43 | 0 | inline void free_aligned_int32(int32_t* p) { aligned_free(p); } |
44 | 530 | inline void free_aligned_int8(int8_t* p) { aligned_free(p); } |
45 | | |
46 | | // Backend alignment constants |
47 | | constexpr std::size_t AVX2_ALIGN = 32; // 256-bit |
48 | | constexpr std::size_t SSE_ALIGN = 16; // 128-bit |
49 | | constexpr std::size_t NEON_ALIGN = 16; // 128-bit (safe) |
50 | | constexpr std::size_t DEFAULT_ALIGN = 64; // Preferred heap alignment |
51 | | |
52 | | } // namespace util |
53 | | } // namespace labneura |