diff options
author | Jonathan Wakely <jwakely@redhat.com> | 2018-11-11 05:16:51 +0000 |
---|---|---|
committer | Jonathan Wakely <redi@gcc.gnu.org> | 2018-11-11 05:16:51 +0000 |
commit | 37b736f6bd78d9238b8557ade7d6bbdabec51d97 (patch) | |
tree | 5cb0452c0ba9dc129ab71f3e90679b6cc26044bd | |
parent | 47d591cf0055b52b71adb8e82e61002281b7035e (diff) | |
download | gcc-37b736f6bd78d9238b8557ade7d6bbdabec51d97.zip gcc-37b736f6bd78d9238b8557ade7d6bbdabec51d97.tar.gz gcc-37b736f6bd78d9238b8557ade7d6bbdabec51d97.tar.bz2 |
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.
From-SVN: r266009
-rw-r--r-- | libstdc++-v3/ChangeLog | 8 | ||||
-rw-r--r-- | libstdc++-v3/include/std/memory | 15 | ||||
-rw-r--r-- | libstdc++-v3/testsuite/20_util/assume_aligned/1.cc | 35 | ||||
-rw-r--r-- | libstdc++-v3/testsuite/20_util/assume_aligned/2_neg.cc | 30 | ||||
-rw-r--r-- | libstdc++-v3/testsuite/20_util/assume_aligned/3.cc | 34 |
5 files changed, 122 insertions, 0 deletions
diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 2a694d96..967aa5b 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -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 diff --git a/libstdc++-v3/include/std/memory b/libstdc++-v3/include/std/memory index 9689540..d7f0e8f 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 0000000..be3bed1 --- /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 0000000..f902e45 --- /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 0000000..da2a668 --- /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(); +} |