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
This commit is contained in:
Jonathan Wakely 2018-11-11 05:16:51 +00:00 committed by Jonathan Wakely
parent 47d591cf00
commit 37b736f6bd
5 changed files with 122 additions and 0 deletions

View 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

View 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

View File

@ -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();
}

View File

@ -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 }
}

View File

@ -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();
}