Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions libcxx/include/__memory/is_sufficiently_aligned.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,16 @@ _LIBCPP_BEGIN_NAMESPACE_STD
#if _LIBCPP_STD_VER >= 26

template <size_t _Alignment, class _Tp>
_LIBCPP_HIDE_FROM_ABI bool is_sufficiently_aligned(_Tp* __ptr) {
return reinterpret_cast<uintptr_t>(__ptr) % _Alignment == 0;
_LIBCPP_HIDE_FROM_ABI constexpr bool is_sufficiently_aligned(_Tp* __ptr) {
# ifdef _LIBCPP_COMPILER_CLANG_BASED
return __builtin_is_aligned(__ptr, _Alignment);
# else
if consteval {
return __builtin_constant_p(__builtin_assume_aligned(__ptr, _Alignment) != nullptr);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer simply not supporting constexpr with GCC here.
It seems better to fail loudly than to give the wrong result.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The goal was to determine whether or not it is implementable, and potentially opening a LWG issue before C++26 is shipped.

} else {
return reinterpret_cast<uintptr_t>(__ptr) % _Alignment == 0;
}
# endif
}

#endif // _LIBCPP_STD_VER >= 26
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#include "test_macros.h"

template <typename T>
void test_is_sufficiently_aligned() {
constexpr void test_is_sufficiently_aligned() {
constexpr std::size_t N = alignof(T);

alignas(8 * N) std::remove_cv_t<T> buf[5];
Expand Down Expand Up @@ -54,7 +54,7 @@ void test_is_sufficiently_aligned() {
}

template <typename T>
void check(T* p) {
constexpr void check(T* p) {
ASSERT_SAME_TYPE(bool, decltype(std::is_sufficiently_aligned<alignof(T)>(p)));
test_is_sufficiently_aligned<T>();
test_is_sufficiently_aligned<const T>();
Expand All @@ -73,7 +73,7 @@ struct alignas(1) X {
};
static_assert(sizeof(X) == 2 * alignof(X));

bool tests() {
constexpr bool test() {
char c;
int i;
long l;
Expand Down Expand Up @@ -107,7 +107,8 @@ bool tests() {
}

int main(int, char**) {
tests();
test();
static_assert(test());

return 0;
}
Loading