tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
assume_aligned.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSL-1.0
2
3#ifndef TETL_MEMORY_ASSUME_ALIGNED_HPP
4#define TETL_MEMORY_ASSUME_ALIGNED_HPP
5
6#include <etl/_config/all.hpp>
7
11
12namespace etl {
13
27template <etl::size_t N, typename T>
28[[nodiscard]] constexpr auto assume_aligned(T* ptr) -> T*
29{
30 static_assert(etl::has_single_bit(N));
31 static_assert(alignof(T) <= N);
32
33 if (not is_constant_evaluated()) {
34#if __has_builtin(__builtin_assume_aligned)
35 return static_cast<T*>(__builtin_assume_aligned(ptr, N));
36#endif
37 }
38
39 return ptr;
40}
41
42} // namespace etl
43
44#endif // TETL_MEMORY_ASSUME_ALIGNED_HPP
constexpr auto has_single_bit(UInt x) noexcept -> bool
Checks if x is an integral power of two.
Definition has_single_bit.hpp:21
constexpr auto assume_aligned(T *ptr) -> T *
Informs the implementation that the object ptr points to is aligned to at least N....
Definition assume_aligned.hpp:28
Definition adjacent_find.hpp:8
constexpr auto is_constant_evaluated() noexcept -> bool
Detects whether the function call occurs within a constant-evaluated context. Returns true if the eva...
Definition is_constant_evaluated.hpp:16