Skip to content

Commit ad1a0aa

Browse files
committed
enable std::assume_aligned
1 parent 5f3e912 commit ad1a0aa

File tree

5 files changed

+60
-4
lines changed

5 files changed

+60
-4
lines changed

stl/inc/memory

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3079,15 +3079,21 @@ inline void* align(size_t _Bound, size_t _Size, void*& _Ptr, size_t& _Space) noe
30793079
return _Ptr;
30803080
}
30813081

3082-
#if _HAS_CXX20 && 0
3082+
#if _HAS_CXX20
30833083
template <size_t _Nx, class _Ty>
30843084
_NODISCARD constexpr _Ty* assume_aligned(_Ty* _Ptr) noexcept /* strengthened */ {
3085-
// this enforces the requirement that _Nx be a power of two
3086-
__builtin_assume_aligned(_Ptr, _Nx);
3085+
#if 1 // TRANSITION: delete after __builtin_assume_aligned will be constexpr
3086+
if (!std::is_constant_evaluated()) {
3087+
return static_cast<_Ty*>(__builtin_assume_aligned(_Ptr, _Nx));
3088+
}
30873089

30883090
return _Ptr;
3091+
#else
3092+
// this enforces the requirement that _Nx be a power of two
3093+
return static_cast<_Ty*>(__builtin_assume_aligned(_Ptr, _Nx));
3094+
#endif // TRANSITION: delete after __builtin_assume_aligned will be constexpr
30893095
}
3090-
#endif // _HAS_CXX20 && 0
3096+
#endif // _HAS_CXX20
30913097

30923098
// SPIN LOCKS
30933099
_EXTERN_C

stl/inc/yvals_core.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@
176176
// P0966R1 string::reserve() Should Not Shrink
177177
// P1001R2 execution::unseq
178178
// P1006R1 constexpr For pointer_traits<T*>::pointer_to()
179+
// P1007R3 std::assume_aligned
179180
// P1023R0 constexpr For std::array Comparisons
180181
// P1024R3 Enhancing span Usability
181182
// P1032R1 Miscellaneous constexpr

tests/std/test.lst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,7 @@ tests\P0898R3_identity
336336
tests\P0912R5_coroutine
337337
tests\P0919R3_heterogeneous_unordered_lookup
338338
tests\P0966R1_string_reserve_should_not_shrink
339+
tests\P1007R3_assume_aligned
339340
tests\P1023R0_constexpr_for_array_comparisons
340341
tests\P1032R1_miscellaneous_constexpr
341342
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: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3+
4+
#include <cassert>
5+
#include <memory>
6+
7+
static void can_change_value_via_assume_aligned(int* p) {
8+
int* p1 = std::assume_aligned<256>(p);
9+
p1[0] = 10;
10+
p1[1] = -15;
11+
}
12+
13+
static constexpr int can_read_constexpr_value_via_assume_aligned_and_sum_it(int const* p, size_t size) {
14+
int const* p1 = std::assume_aligned<256>(p);
15+
int result = 0;
16+
for (size_t i = 0; i < size; ++i) {
17+
result += p1[i];
18+
}
19+
return result;
20+
}
21+
22+
23+
static void 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+
}
29+
30+
static constexpr void constexpr_assume_aligned_can_read_value() {
31+
alignas(256) constexpr int arr[3] = {10, 11, 9};
32+
static_assert(can_read_constexpr_value_via_assume_aligned_and_sum_it(arr, 3) == 30);
33+
}
34+
35+
static void assume_aligned_can_read_value() {
36+
alignas(256) constexpr int arr[3] = {10, 11, 9};
37+
assert(can_read_constexpr_value_via_assume_aligned_and_sum_it(arr, 3) == 30);
38+
}
39+
40+
int main() {
41+
assume_aligned_can_change_value();
42+
constexpr_assume_aligned_can_read_value();
43+
assume_aligned_can_read_value();
44+
}

0 commit comments

Comments
 (0)