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// SPDX-FileCopyrightText: Copyright (C) 2019 Tobias Hienzsch
3
4#ifndef TETL_MEMORY_ASSUME_ALIGNED_HPP
5#define TETL_MEMORY_ASSUME_ALIGNED_HPP
6
7#include <etl/_config/all.hpp>
8
9#include <etl/_bit/has_single_bit.hpp>
10#include <etl/_cstddef/size_t.hpp>
11#include <etl/_type_traits/is_constant_evaluated.hpp>
12
13namespace etl {
14
15/// Informs the implementation that the object ptr points to is aligned
16/// to at least N. The implementation may use this information to generate more
17/// efficient code, but it might only make this assumption if the object is
18/// accessed via the return value of assume_aligned.
19///
20/// The program is ill-formed if N is not a power of 2. The behavior is
21/// undefined if ptr does not point to an object of type T (ignoring
22/// cv-qualification at every level), or if the object's alignment is not at
23/// least N.
24///
25/// https://en.cppreference.com/w/cpp/memory/assume_aligned
26///
27/// \ingroup memory
28template <etl::size_t N, typename T>
29[[nodiscard]] constexpr auto assume_aligned(T* ptr) -> T*
30{
31 static_assert(etl::has_single_bit(N));
32 static_assert(alignof(T) <= N);
33
35#if __has_builtin(__builtin_assume_aligned)
36 return static_cast<T*>(__builtin_assume_aligned(ptr, N));
37#endif
38 }
39
40 return ptr;
41}
42
43} // namespace etl
44
45#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:22
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:29
Definition adjacent_find.hpp:9
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:17