From: Jonathan Wakely Date: Sun, 11 Nov 2018 05:16:51 +0000 (+0000) Subject: Implement P1007R3 std::assume_aligned X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=37b736f6bd78d9238b8557ade7d6bbdabec51d97;p=gcc.git 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 --- diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 2a694d96ebd..967aa5bc53d 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,11 @@ +2018-11-11 Jonathan Wakely + + 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 PR libstdc++/87787 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 +#if __cplusplus > 201703L +# include // 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 + [[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 +// . + +// { dg-options "-std=gnu++2a" } +// { dg-do run { target c++2a } } + +#include +#include + +void +test01() +{ + int i = 0; + int* p = std::assume_aligned(&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 +// . + +// { dg-options "-std=gnu++2a" } +// { dg-do compile { target c++2a } } + +#include + +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 +// . + +// { dg-options "-std=gnu++2a -O2" } +// { dg-do compile { target c++2a } } +// { dg-final { scan-assembler-not "undefined" } } + +#include + +int* ptr(); +extern "C" void undefined(); + +void +test01() +{ + const std::size_t alignment = 4 * alignof(int); + int* p = std::assume_aligned(ptr()); + if ((std::uintptr_t)p % alignment) + undefined(); +}