diff mbox series

Implement P1007R3 std::assume_aligned

Message ID 20181111051524.GA8287@redhat.com
State New
Headers show
Series Implement P1007R3 std::assume_aligned | expand

Commit Message

Jonathan Wakely Nov. 11, 2018, 5:15 a.m. UTC
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.

Tested x86_64-linux, committed to trunk.
commit 777edd7fbfaa0cd6faa353316bd54e1d67b19b32
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Sun Nov 11 00:52:15 2018 +0000

    Implement P1007R3 std::assume_aligned
    
            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.
diff mbox series

Patch

diff --git a/libstdc++-v3/include/std/memory b/libstdc++-v3/include/std/memory
index 9689540fb81..d7f0e8f6d21 100644
--- a/libstdc++-v3/include/std/memory
+++ b/libstdc++-v3/include/std/memory
@@ -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
index 00000000000..be3bed14e6d
--- /dev/null
+++ b/libstdc++-v3/testsuite/20_util/assume_aligned/1.cc
@@ -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
index 00000000000..f902e45a9a7
--- /dev/null
+++ b/libstdc++-v3/testsuite/20_util/assume_aligned/2_neg.cc
@@ -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
index 00000000000..da2a6684bb9
--- /dev/null
+++ b/libstdc++-v3/testsuite/20_util/assume_aligned/3.cc
@@ -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();
+}