alpaka
Abstraction Library for Parallel Kernel Acceleration
Loading...
Searching...
No Matches
Alignment.hpp
Go to the documentation of this file.
1/* Copyright 2025 René Widera
2 * SPDX-License-Identifier: MPL-2.0
3 */
4
5#pragma once
6
7#include <cstdint>
8#include <limits>
9#include <type_traits>
10
11namespace alpaka
12{
13 /** @brief Strongly typed and constexpr representation of a byte-alignment of memory
14 *
15 * @details
16 * The number of bytes is stored at compile-time using a value template parameter. Therefore, alignments should
17 * always be declared `constexpr`. If no explicit alignment is given, a default will be set.
18 *
19 * To use the alignment, the Alignment::get() function can be called for a given type parameter, returning either
20 * the object's set alignment, or the given type's alignment, if the default was used.
21 *
22 * @tparam T_byte The number of bytes in uint32_t.
23 */
24 template<uint32_t T_byte = std::numeric_limits<uint32_t>::max()>
25 struct Alignment
26 {
27 /** Get the byte-alignment of a given type when using this alignment.
28 *
29 * @details
30 * Trying to use an alignment with a smaller value than the alignment of the given `T_Type` results in a failed
31 * `static_assert`.
32 *
33 * @tparam T_Type The type for which to get the alignment.
34 * @return If T_byte is not specifically set: alignment of T_Type, else: value of T_byte
35 */
36 template<typename T_Type>
37 static consteval uint32_t get()
38 {
39 // auto alignment
40 if constexpr(T_byte == std::numeric_limits<uint32_t>::max())
41 return static_cast<uint32_t>(alignof(T_Type));
42 else
43 {
44 static_assert(
45 value >= alignof(T_Type),
46 "tried to use alignment that is smaller than the alignment of the type it's for");
47 return value;
48 }
49 }
50
51 private:
52 static consteval uint32_t get()
53 {
54 return value;
55 }
56
57 static constexpr uint32_t value = T_byte;
58 };
59
61
62 namespace trait
63 {
64 template<typename T_Type>
65 struct IsAlignment : std::false_type
66 {
67 };
68
69 template<uint32_t T_byte>
70 struct IsAlignment<Alignment<T_byte>> : std::true_type
71 {
72 };
73 } // namespace trait
74
75 template<typename T_Type>
77
78 namespace concepts
79 {
80 /** @brief Concept to check for an alignment object
81 *
82 * @details
83 * An alignment represents a byte alignment of memory. The class is used for strong typing.
84 * For more information, refer to the struct alpaka::Alignment or the general documentation.
85 *
86 * @todo link to alignment documentation in the general docs
87 */
88 template<typename T>
90 } // namespace concepts
91} // namespace alpaka
Concept to check for an alignment object.
Definition Alignment.hpp:89
main alpaka namespace.
Definition alpaka.hpp:76
Alignment<> AutoAligned
Definition Alignment.hpp:60
constexpr bool isAlignment_v
Definition Alignment.hpp:76
Strongly typed and constexpr representation of a byte-alignment of memory.
Definition Alignment.hpp:26
static consteval uint32_t get()
Definition Alignment.hpp:52
static consteval uint32_t get()
Get the byte-alignment of a given type when using this alignment.
Definition Alignment.hpp:37
static constexpr uint32_t value
Definition Alignment.hpp:57