Skip to content

Commit f1f2363

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

File tree

6 files changed

+78
-3
lines changed

6 files changed

+78
-3
lines changed

stl/inc/memory

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3079,15 +3079,24 @@ 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+
#if 1 // TRANSITION, DevCom-1190972
3086+
if (_STD is_constant_evaluated()) {
3087+
return _Ptr;
3088+
}
3089+
(void) __builtin_assume_aligned(_Ptr, _Nx);
3090+
3091+
return _Ptr;
3092+
#else
30853093
// this enforces the requirement that _Nx be a power of two
3086-
__builtin_assume_aligned(_Ptr, _Nx);
3094+
(void) __builtin_assume_aligned(_Ptr, _Nx);
30873095

30883096
return _Ptr;
3097+
#endif // TRANSITION, DevCom-1190972
30893098
}
3090-
#endif // _HAS_CXX20 && 0
3099+
#endif // _HAS_CXX20
30913100

30923101
// SPIN LOCKS
30933102
_EXTERN_C

stl/inc/yvals_core.h

Lines changed: 2 additions & 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 assume_aligned()
179180
// P1023R0 constexpr For std::array Comparisons
180181
// P1024R3 Enhancing span Usability
181182
// P1032R1 Miscellaneous constexpr
@@ -1142,6 +1143,7 @@
11421143
#define __cpp_lib_atomic_value_initialization 201911L
11431144

11441145
#if _HAS_CXX20
1146+
#define __cpp_lib_assume_aligned 201811L
11451147
#define __cpp_lib_atomic_flag_test 201907L
11461148
#define __cpp_lib_atomic_float 201711L
11471149
#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
@@ -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: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
static 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+
static constexpr int can_read_constexpr_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+
24+
static void assume_aligned_can_change_value() {
25+
alignas(256) int arr[2] = {0, 0};
26+
can_change_value_via_assume_aligned(&arr[0]);
27+
assert(arr[0] == 10);
28+
assert(arr[1] == -15);
29+
}
30+
31+
static constexpr void constexpr_assume_aligned_can_read_value() {
32+
alignas(256) constexpr int arr[3] = {10, 11, 9};
33+
static_assert(can_read_constexpr_value_via_assume_aligned_and_sum_it(arr, 3) == 30);
34+
}
35+
36+
static void assume_aligned_can_read_value() {
37+
alignas(256) constexpr int arr[3] = {10, 11, 9};
38+
assert(can_read_constexpr_value_via_assume_aligned_and_sum_it(arr, 3) == 30);
39+
}
40+
41+
int main() {
42+
assume_aligned_can_change_value();
43+
constexpr_assume_aligned_can_read_value();
44+
assume_aligned_can_read_value();
45+
}

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)