Implement P1007R3 std::assume_aligned
authorJonathan Wakely <jwakely@redhat.com>
Sun, 11 Nov 2018 05:16:51 +0000 (05:16 +0000)
committerJonathan Wakely <redi@gcc.gnu.org>
Sun, 11 Nov 2018 05:16:51 +0000 (05:16 +0000)
Implement P1007R3 std::assume_aligned
* include/std/memory (assume_aligned): Implement for C++17.
* testsuite/20_util/assume_aligned/1.cc: New test.
* testsuite/20_util/assume_aligned/2_neg.cc: New test.
* testsuite/20_util/assume_aligned/3.cc: New test.

From-SVN: r266009

libstdc++-v3/ChangeLog
libstdc++-v3/include/std/memory
libstdc++-v3/testsuite/20_util/assume_aligned/1.cc [new file with mode: 0644]
libstdc++-v3/testsuite/20_util/assume_aligned/2_neg.cc [new file with mode: 0644]
libstdc++-v3/testsuite/20_util/assume_aligned/3.cc [new file with mode: 0644]

index 2a694d96ebdaaa31bd8deb4e70852f580cc1ff9e..967aa5bc53d410fc7d5df5d92c459263c4d09540 100644 (file)
@@ -1,3 +1,11 @@
+2018-11-11  Jonathan Wakely  <jwakely@redhat.com>
+
+       Implement P1007R3 std::assume_aligned
+       * include/std/memory (assume_aligned): Implement for C++17.
+       * testsuite/20_util/assume_aligned/1.cc: New test.
+       * testsuite/20_util/assume_aligned/2_neg.cc: New test.
+       * testsuite/20_util/assume_aligned/3.cc: New test.
+
 2018-11-09  Jonathan Wakely  <jwakely@redhat.com>
 
        PR libstdc++/87787
index 9689540fb817cc6b9c35cb3c1180522cf97d50d9..d7f0e8f6d21d5b0b351bb2bbf8b5bb6b8ec662f2 100644 (file)
@@ -89,6 +89,9 @@
 
 #if __cplusplus >= 201103L
 #include <cstdint>
+#if __cplusplus > 201703L
+# include <bit>                        // for ispow2
+#endif
 namespace std _GLIBCXX_VISIBILITY(default)
 {
 _GLIBCXX_BEGIN_NAMESPACE_VERSION
@@ -151,6 +154,18 @@ undeclare_no_pointers(char*, size_t) { }
 inline pointer_safety
 get_pointer_safety() noexcept { return pointer_safety::relaxed; }
 
+#if __cplusplus > 201703L
+  /// Inform the compiler that a pointer is aligned.
+  template<size_t _Align, class _Tp>
+    [[nodiscard,__gnu__::__always_inline__]]
+    constexpr _Tp* assume_aligned(_Tp* __ptr)
+    {
+      static_assert(std::ispow2(_Align));
+      _GLIBCXX_DEBUG_ASSERT((std::uintptr_t)__ptr % _Align == 0);
+      return static_cast<_Tp*>(__builtin_assume_aligned(__ptr, _Align));
+    }
+#endif // C++2a
+
 _GLIBCXX_END_NAMESPACE_VERSION
 } // namespace
 #endif // C++11
diff --git a/libstdc++-v3/testsuite/20_util/assume_aligned/1.cc b/libstdc++-v3/testsuite/20_util/assume_aligned/1.cc
new file mode 100644 (file)
index 0000000..be3bed1
--- /dev/null
@@ -0,0 +1,35 @@
+// Copyright (C) 2018 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+// { dg-options "-std=gnu++2a" }
+// { dg-do run { target c++2a } }
+
+#include <memory>
+#include <testsuite_hooks.h>
+
+void
+test01()
+{
+  int i = 0;
+  int* p = std::assume_aligned<alignof(int)>(&i);
+  VERIFY( p == &i );
+}
+
+int main()
+{
+  test01();
+}
diff --git a/libstdc++-v3/testsuite/20_util/assume_aligned/2_neg.cc b/libstdc++-v3/testsuite/20_util/assume_aligned/2_neg.cc
new file mode 100644 (file)
index 0000000..f902e45
--- /dev/null
@@ -0,0 +1,30 @@
+// Copyright (C) 2018 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+// { dg-options "-std=gnu++2a" }
+// { dg-do compile { target c++2a } }
+
+#include <memory>
+
+void
+test01()
+{
+  int i = 0;
+  std::assume_aligned<2>(&i); // { dg-warning "ignoring return value" }
+  (void) std::assume_aligned<3>(&i); // { dg-error "here" }
+  // { dg-error "static assertion failed" "" { target *-*-* } 0 }
+}
diff --git a/libstdc++-v3/testsuite/20_util/assume_aligned/3.cc b/libstdc++-v3/testsuite/20_util/assume_aligned/3.cc
new file mode 100644 (file)
index 0000000..da2a668
--- /dev/null
@@ -0,0 +1,34 @@
+// Copyright (C) 2018 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+// { dg-options "-std=gnu++2a -O2" }
+// { dg-do compile { target c++2a } }
+// { dg-final { scan-assembler-not "undefined" } }
+
+#include <memory>
+
+int* ptr();
+extern "C" void undefined();
+
+void
+test01()
+{
+  const std::size_t alignment = 4 * alignof(int);
+  int* p = std::assume_aligned<alignment>(ptr());
+  if ((std::uintptr_t)p % alignment)
+    undefined();
+}