/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 | | #include <limits> |
7 | | #include <new> |
8 | | #include <stdexcept> |
9 | | |
10 | | #if defined(_MSC_VER) |
11 | | #include <malloc.h> |
12 | | #else |
13 | | #include <cerrno> |
14 | | #endif |
15 | | |
16 | | namespace labneura { |
17 | | namespace util { |
18 | | |
19 | 3.14k | inline std::size_t checked_round_up(std::size_t value, std::size_t multiple) { |
20 | 3.14k | if (multiple == 0) { |
21 | 0 | throw std::invalid_argument("Alignment multiple must be non-zero"); |
22 | 0 | } |
23 | | |
24 | 3.14k | const std::size_t remainder = value % multiple; |
25 | 3.14k | if (remainder == 0) { |
26 | 757 | return value; |
27 | 757 | } |
28 | | |
29 | 2.39k | const std::size_t increment = multiple - remainder; |
30 | 2.39k | if (value > std::numeric_limits<std::size_t>::max() - increment) { |
31 | 0 | throw std::bad_array_new_length(); |
32 | 0 | } |
33 | 2.39k | return value + increment; |
34 | 2.39k | } |
35 | | |
36 | 5.36k | inline bool is_aligned(const void* p, std::size_t alignment) { |
37 | 5.36k | return reinterpret_cast<std::uintptr_t>(p) % alignment == 0; |
38 | 5.36k | } |
39 | | |
40 | | // Allocate 'count' elements of type T with the given alignment. |
41 | | // Throws std::bad_array_new_length if the byte count overflows and |
42 | | // std::bad_alloc if the aligned allocation cannot be satisfied. |
43 | | template <typename T> |
44 | 4.28k | inline T* allocate_aligned(std::size_t count, std::size_t alignment = 64) { |
45 | 4.28k | if (alignment < sizeof(void*) || (alignment & (alignment - 1)) != 0) { |
46 | 0 | throw std::invalid_argument( |
47 | 0 | "Alignment must be a power of two and at least sizeof(void*)"); |
48 | 0 | } |
49 | 4.28k | if (count > std::numeric_limits<std::size_t>::max() / sizeof(T)) { |
50 | 0 | throw std::bad_array_new_length(); |
51 | 0 | } |
52 | | |
53 | | // Keep zero-sized backend buffers non-null so empty-range pointer arithmetic |
54 | | // remains well-defined. |
55 | 4.28k | const std::size_t bytes = count == 0 ? sizeof(T) : count * sizeof(T); |
56 | | #if defined(_MSC_VER) |
57 | | void* p = _aligned_malloc(bytes, alignment); |
58 | | if (p == nullptr) { |
59 | | throw std::bad_alloc(); |
60 | | } |
61 | | #else |
62 | 4.28k | void* p = nullptr; |
63 | 4.28k | const int result = posix_memalign(&p, alignment, bytes); |
64 | 4.28k | if (result == EINVAL) { |
65 | 0 | throw std::invalid_argument("Invalid aligned allocation alignment"); |
66 | 0 | } |
67 | 4.28k | if (result != 0) { |
68 | 0 | throw std::bad_alloc(); |
69 | 0 | } |
70 | 4.28k | #endif |
71 | 4.28k | return static_cast<T*>(p); |
72 | 4.28k | } _ZN8labneura4util16allocate_alignedIfEEPT_mm Line | Count | Source | 44 | 1.53k | inline T* allocate_aligned(std::size_t count, std::size_t alignment = 64) { | 45 | 1.53k | if (alignment < sizeof(void*) || (alignment & (alignment - 1)) != 0) { | 46 | 0 | throw std::invalid_argument( | 47 | 0 | "Alignment must be a power of two and at least sizeof(void*)"); | 48 | 0 | } | 49 | 1.53k | if (count > std::numeric_limits<std::size_t>::max() / sizeof(T)) { | 50 | 0 | throw std::bad_array_new_length(); | 51 | 0 | } | 52 | | | 53 | | // Keep zero-sized backend buffers non-null so empty-range pointer arithmetic | 54 | | // remains well-defined. | 55 | 1.53k | const std::size_t bytes = count == 0 ? sizeof(T) : count * sizeof(T); | 56 | | #if defined(_MSC_VER) | 57 | | void* p = _aligned_malloc(bytes, alignment); | 58 | | if (p == nullptr) { | 59 | | throw std::bad_alloc(); | 60 | | } | 61 | | #else | 62 | 1.53k | void* p = nullptr; | 63 | 1.53k | const int result = posix_memalign(&p, alignment, bytes); | 64 | 1.53k | if (result == EINVAL) { | 65 | 0 | throw std::invalid_argument("Invalid aligned allocation alignment"); | 66 | 0 | } | 67 | 1.53k | if (result != 0) { | 68 | 0 | throw std::bad_alloc(); | 69 | 0 | } | 70 | 1.53k | #endif | 71 | 1.53k | return static_cast<T*>(p); | 72 | 1.53k | } |
_ZN8labneura4util16allocate_alignedIsEEPT_mm Line | Count | Source | 44 | 1.36k | inline T* allocate_aligned(std::size_t count, std::size_t alignment = 64) { | 45 | 1.36k | if (alignment < sizeof(void*) || (alignment & (alignment - 1)) != 0) { | 46 | 0 | throw std::invalid_argument( | 47 | 0 | "Alignment must be a power of two and at least sizeof(void*)"); | 48 | 0 | } | 49 | 1.36k | if (count > std::numeric_limits<std::size_t>::max() / sizeof(T)) { | 50 | 0 | throw std::bad_array_new_length(); | 51 | 0 | } | 52 | | | 53 | | // Keep zero-sized backend buffers non-null so empty-range pointer arithmetic | 54 | | // remains well-defined. | 55 | 1.36k | const std::size_t bytes = count == 0 ? sizeof(T) : count * sizeof(T); | 56 | | #if defined(_MSC_VER) | 57 | | void* p = _aligned_malloc(bytes, alignment); | 58 | | if (p == nullptr) { | 59 | | throw std::bad_alloc(); | 60 | | } | 61 | | #else | 62 | 1.36k | void* p = nullptr; | 63 | 1.36k | const int result = posix_memalign(&p, alignment, bytes); | 64 | 1.36k | if (result == EINVAL) { | 65 | 0 | throw std::invalid_argument("Invalid aligned allocation alignment"); | 66 | 0 | } | 67 | 1.36k | if (result != 0) { | 68 | 0 | throw std::bad_alloc(); | 69 | 0 | } | 70 | 1.36k | #endif | 71 | 1.36k | return static_cast<T*>(p); | 72 | 1.36k | } |
_ZN8labneura4util16allocate_alignedIaEEPT_mm Line | Count | Source | 44 | 818 | inline T* allocate_aligned(std::size_t count, std::size_t alignment = 64) { | 45 | 818 | if (alignment < sizeof(void*) || (alignment & (alignment - 1)) != 0) { | 46 | 0 | throw std::invalid_argument( | 47 | 0 | "Alignment must be a power of two and at least sizeof(void*)"); | 48 | 0 | } | 49 | 818 | if (count > std::numeric_limits<std::size_t>::max() / sizeof(T)) { | 50 | 0 | throw std::bad_array_new_length(); | 51 | 0 | } | 52 | | | 53 | | // Keep zero-sized backend buffers non-null so empty-range pointer arithmetic | 54 | | // remains well-defined. | 55 | 818 | const std::size_t bytes = count == 0 ? sizeof(T) : count * sizeof(T); | 56 | | #if defined(_MSC_VER) | 57 | | void* p = _aligned_malloc(bytes, alignment); | 58 | | if (p == nullptr) { | 59 | | throw std::bad_alloc(); | 60 | | } | 61 | | #else | 62 | 818 | void* p = nullptr; | 63 | 818 | const int result = posix_memalign(&p, alignment, bytes); | 64 | 818 | if (result == EINVAL) { | 65 | 0 | throw std::invalid_argument("Invalid aligned allocation alignment"); | 66 | 0 | } | 67 | 818 | if (result != 0) { | 68 | 0 | throw std::bad_alloc(); | 69 | 0 | } | 70 | 818 | #endif | 71 | 818 | return static_cast<T*>(p); | 72 | 818 | } |
_ZN8labneura4util16allocate_alignedIhEEPT_mm Line | Count | Source | 44 | 565 | inline T* allocate_aligned(std::size_t count, std::size_t alignment = 64) { | 45 | 565 | if (alignment < sizeof(void*) || (alignment & (alignment - 1)) != 0) { | 46 | 0 | throw std::invalid_argument( | 47 | 0 | "Alignment must be a power of two and at least sizeof(void*)"); | 48 | 0 | } | 49 | 565 | if (count > std::numeric_limits<std::size_t>::max() / sizeof(T)) { | 50 | 0 | throw std::bad_array_new_length(); | 51 | 0 | } | 52 | | | 53 | | // Keep zero-sized backend buffers non-null so empty-range pointer arithmetic | 54 | | // remains well-defined. | 55 | 565 | const std::size_t bytes = count == 0 ? sizeof(T) : count * sizeof(T); | 56 | | #if defined(_MSC_VER) | 57 | | void* p = _aligned_malloc(bytes, alignment); | 58 | | if (p == nullptr) { | 59 | | throw std::bad_alloc(); | 60 | | } | 61 | | #else | 62 | 565 | void* p = nullptr; | 63 | 565 | const int result = posix_memalign(&p, alignment, bytes); | 64 | 565 | if (result == EINVAL) { | 65 | 0 | throw std::invalid_argument("Invalid aligned allocation alignment"); | 66 | 0 | } | 67 | 565 | if (result != 0) { | 68 | 0 | throw std::bad_alloc(); | 69 | 0 | } | 70 | 565 | #endif | 71 | 565 | return static_cast<T*>(p); | 72 | 565 | } |
|
73 | | |
74 | 4.28k | inline void aligned_free(void* p) { |
75 | | #if defined(_MSC_VER) |
76 | | if (p) _aligned_free(p); |
77 | | #else |
78 | 4.28k | if (p) std::free(p); |
79 | 4.28k | #endif |
80 | 4.28k | } |
81 | | |
82 | | // Typed helpers for unique_ptr function-pointer deleters |
83 | 0 | inline void free_aligned_double(double* p) { aligned_free(p); } |
84 | 0 | inline void free_aligned_float(float* p) { aligned_free(p); } |
85 | 0 | inline void free_aligned_int16(int16_t* p) { aligned_free(p); } |
86 | 0 | inline void free_aligned_int32(int32_t* p) { aligned_free(p); } |
87 | 0 | inline void free_aligned_int8(int8_t* p) { aligned_free(p); } |
88 | | |
89 | | // Backend alignment constants |
90 | | constexpr std::size_t AVX2_ALIGN = 32; // 256-bit |
91 | | constexpr std::size_t SSE_ALIGN = 16; // 128-bit |
92 | | constexpr std::size_t NEON_ALIGN = 16; // 128-bit (safe) |
93 | | constexpr std::size_t DEFAULT_ALIGN = 64; // Preferred heap alignment |
94 | | |
95 | | } // namespace util |
96 | | } // namespace labneura |