Skip to content

Commit 6e6321c

Browse files
fsb4000StephanTLavavejCaseyCarter
authored
P1007R3 assume_aligned() (#1306)
Co-authored-by: Stephan T. Lavavej <[email protected]> Co-authored-by: Casey Carter <[email protected]>
1 parent 648f2b1 commit 6e6321c

File tree

6 files changed

+73
-7
lines changed

6 files changed

+73
-7
lines changed

stl/inc/memory

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3687,15 +3687,17 @@ inline void* align(size_t _Bound, size_t _Size, void*& _Ptr, size_t& _Space) noe
36873687
return _Ptr;
36883688
}
36893689

3690-
#if _HAS_CXX20 && 0
3690+
#if _HAS_CXX20
36913691
template <size_t _Nx, class _Ty>
3692-
_NODISCARD constexpr _Ty* assume_aligned(_Ty* _Ptr) noexcept /* strengthened */ {
3693-
// this enforces the requirement that _Nx be a power of two
3694-
__builtin_assume_aligned(_Ptr, _Nx);
3695-
3696-
return _Ptr;
3692+
_NODISCARD constexpr _Ty* assume_aligned(_Ty* const _Ptr) noexcept /* strengthened */ {
3693+
if (_STD is_constant_evaluated()) {
3694+
return _Ptr;
3695+
} else {
3696+
// this enforces the requirement that _Nx be a power of two
3697+
return static_cast<_Ty*>(__builtin_assume_aligned(_Ptr, _Nx));
3698+
}
36973699
}
3698-
#endif // _HAS_CXX20 && 0
3700+
#endif // _HAS_CXX20
36993701

37003702
// SPIN LOCKS
37013703
_EXTERN_C

stl/inc/yvals_core.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@
177177
// P0966R1 string::reserve() Should Not Shrink
178178
// P1001R2 execution::unseq
179179
// P1006R1 constexpr For pointer_traits<T*>::pointer_to()
180+
// P1007R3 assume_aligned()
180181
// P1023R0 constexpr For std::array Comparisons
181182
// P1024R3 Enhancing span Usability
182183
// P1032R1 Miscellaneous constexpr
@@ -1150,6 +1151,7 @@
11501151
#define __cpp_lib_atomic_value_initialization 201911L
11511152

11521153
#if _HAS_CXX20
1154+
#define __cpp_lib_assume_aligned 201811L
11531155
#define __cpp_lib_atomic_flag_test 201907L
11541156
#define __cpp_lib_atomic_float 201711L
11551157
#define __cpp_lib_atomic_lock_free_type_aliases 201907L

tests/std/test.lst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,7 @@ tests\P0898R3_identity
348348
tests\P0912R5_coroutine
349349
tests\P0919R3_heterogeneous_unordered_lookup
350350
tests\P0966R1_string_reserve_should_not_shrink
351+
tests\P1007R3_assume_aligned
351352
tests\P1023R0_constexpr_for_array_comparisons
352353
tests\P1032R1_miscellaneous_constexpr
353354
tests\P1135R6_atomic_flag_test
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Copyright (c) Microsoft Corporation.
2+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3+
4+
RUNALL_INCLUDE ..\usual_latest_matrix.lst
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3+
4+
#include <assert.h>
5+
#include <memory>
6+
#include <stddef.h>
7+
8+
constexpr void can_change_value_via_assume_aligned(int* p) {
9+
int* p1 = std::assume_aligned<256>(p);
10+
p1[0] = 10;
11+
p1[1] = -15;
12+
}
13+
14+
constexpr int can_read_value_via_assume_aligned_and_sum_it(int const* p, size_t size) {
15+
int const* p1 = std::assume_aligned<256>(p);
16+
int result = 0;
17+
for (size_t i = 0; i < size; ++i) {
18+
result += p1[i];
19+
}
20+
return result;
21+
}
22+
23+
constexpr bool assume_aligned_can_change_value() {
24+
alignas(256) int arr[2] = {0, 0};
25+
can_change_value_via_assume_aligned(&arr[0]);
26+
assert(arr[0] == 10);
27+
assert(arr[1] == -15);
28+
return true;
29+
}
30+
31+
constexpr bool assume_aligned_can_read_value() {
32+
alignas(256) constexpr int arr[3] = {10, 11, 9};
33+
assert(can_read_value_via_assume_aligned_and_sum_it(arr, 3) == 30);
34+
return true;
35+
}
36+
37+
int main() {
38+
assume_aligned_can_change_value();
39+
static_assert(assume_aligned_can_change_value());
40+
41+
assume_aligned_can_read_value();
42+
static_assert(assume_aligned_can_read_value());
43+
}

tests/std/tests/VSO_0157762_feature_test_macros/test.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,20 @@ STATIC_ASSERT(__cpp_lib_array_constexpr == 201803L);
8383
STATIC_ASSERT(__cpp_lib_as_const == 201510L);
8484
#endif
8585

86+
#if _HAS_CXX20
87+
#ifndef __cpp_lib_assume_aligned
88+
#error __cpp_lib_assume_aligned is not defined
89+
#elif __cpp_lib_assume_aligned != 201811L
90+
#error __cpp_lib_assume_aligned is not 201811L
91+
#else
92+
STATIC_ASSERT(__cpp_lib_assume_aligned == 201811L);
93+
#endif
94+
#else
95+
#ifdef __cpp_lib_assume_aligned
96+
#error __cpp_lib_assume_aligned is defined
97+
#endif
98+
#endif
99+
86100
#if _HAS_CXX20
87101
#ifndef __cpp_lib_atomic_flag_test
88102
#error __cpp_lib_atomic_flag_test is not defined

0 commit comments

Comments
 (0)