[libcxx-commits] [PATCH] D108906: Add std::assume_aligned

Gonzalo Brito Gadeschi via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Mon Aug 30 01:07:00 PDT 2021


gonzalobg created this revision.
gonzalobg added reviewers: ldionne, chandlerc, EricWF.
gonzalobg added a project: libc++.
gonzalobg requested review of this revision.
Herald added a subscriber: libcxx-commits.
Herald added a reviewer: libc++.

Implements C++20 std::assume_aligned from p1007:
http://wg21.link/p1007


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D108906

Files:
  libcxx/include/memory
  libcxx/test/libcxx/memory/assume_aligned.pass.cpp


Index: libcxx/test/libcxx/memory/assume_aligned.pass.cpp
===================================================================
--- /dev/null
+++ libcxx/test/libcxx/memory/assume_aligned.pass.cpp
@@ -0,0 +1,31 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+
+// <memory>
+// UNSUPPORTED: c++03, c++11, c++14, c++17
+
+// template<std::size_t N, typename T>
+// [[nodiscard]] T* assume_aligned(T*);
+
+#include <memory>
+
+constexpr int test() {
+  int a;
+  int* b = std::assume_aligned<4>(&a);
+  return sizeof(int);
+}
+
+int main(int, char**)
+{
+  constexpr int N = test();
+  int* a = (int*)malloc(N);
+  int* b = std::assume_aligned<4>(a);
+
+  return 0;
+}
Index: libcxx/include/memory
===================================================================
--- libcxx/include/memory
+++ libcxx/include/memory
@@ -953,6 +953,19 @@
   }
 };
 
+#if _LIBCPP_STD_VER >= 20
+template <size_t N, typename T>
+[[nodiscard]]
+_LIBCPP_CONSTEXPR
+_LIBCPP_ALWAYS_INLINE
+T* assume_aligned(T* __ptr) {
+  #if defined(__clang__) || (defined(__GNUC__) && !defined(__ICC))
+    return reinterpret_cast<T*>(__builtin_assume_aligned(__ptr));
+  #else
+    return __ptr;
+  #endif
+}
+#endif // _LIBCPP_STD_VER >= 20
 
 _LIBCPP_END_NAMESPACE_STD
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D108906.369390.patch
Type: text/x-patch
Size: 1567 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20210830/60bef83f/attachment.bin>


More information about the libcxx-commits mailing list