/**
* @brief Fit aligned storage in buffer.
- * @ingroup memory
*
* This function tries to fit @a __size bytes of storage with alignment
* @a __align into the buffer @a __ptr of size @a __space bytes. If such
* @param __space Size of the buffer pointed to by @a __ptr.
* @return the updated pointer if the aligned storage fits, otherwise nullptr.
*
+ * @ingroup memory
*/
inline void*
align(size_t __align, size_t __size, void*& __ptr, size_t& __space) noexcept
{
-#ifdef _GLIBCXX_USE_C99_STDINT_TR1
const auto __intptr = reinterpret_cast<uintptr_t>(__ptr);
-#else
- // Cannot use std::uintptr_t so assume that std::size_t can be used instead.
- static_assert(sizeof(size_t) >= sizeof(void*),
- "std::size_t must be a suitable substitute for std::uintptr_t");
- const auto __intptr = reinterpret_cast<unsigned long long>(__ptr);
-#endif
const auto __aligned = (__intptr - 1u + __align) & -__align;
const auto __diff = __aligned - __intptr;
if ((__size + __diff) > __space)
* @tparam _Align An alignment value (i.e. a power of two)
* @tparam _Tp An object type
* @param __ptr A pointer that is aligned to _Align
+ *
+ * C++20 20.10.6 [ptr.align]
+ *
* @ingroup memory
*/
template<size_t _Align, class _Tp>
[[nodiscard,__gnu__::__always_inline__]]
- constexpr _Tp* assume_aligned(_Tp* __ptr)
+ constexpr _Tp*
+ assume_aligned(_Tp* __ptr) noexcept
{
static_assert(std::has_single_bit(_Align));
- _GLIBCXX_DEBUG_ASSERT((std::uintptr_t)__ptr % _Align == 0);
- return static_cast<_Tp*>(__builtin_assume_aligned(__ptr, _Align));
+ if (std::is_constant_evaluated())
+ return __ptr;
+ else
+ {
+ // This function is expected to be used in hot code, where
+ // __glibcxx_assert would add unwanted overhead.
+ _GLIBCXX_DEBUG_ASSERT((uintptr_t)__ptr % _Align == 0);
+ return static_cast<_Tp*>(__builtin_assume_aligned(__ptr, _Align));
+ }
}
#endif // C++2a
// with this library; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>.
-// { dg-options "-std=gnu++2a" }
+// { dg-options "-std=gnu++2a -O2" }
// { dg-do run { target c++2a } }
#include <memory>
VERIFY( p == &i );
}
+[[gnu::noipa,gnu::noinline]]
+int*
+create_aligned(std::size_t alignment, void* p, std::size_t n)
+{
+ return ::new(std::align(alignment, sizeof(int), p, n)) int(42);
+}
+
+extern "C" void undefined(); // call to this should be optimized away
+
+void
+test02()
+{
+ unsigned char buf[sizeof(int) * 128];
+ int* p = create_aligned(64, buf + 1, sizeof(buf) - 1);
+ int* q = std::assume_aligned<64>(p);
+ if ((std::uintptr_t)q % 64)
+ undefined();
+ VERIFY( p == q );
+}
+
int main()
{
test01();
+ test02();
}
--- /dev/null
+// Copyright (C) 2020 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>
+
+// PR libstdc++/97132 - assume_aligned is not constexpr
+
+constexpr bool test01()
+{
+ struct alignas(32) S { int i; };
+ S s{42};
+ int* p = std::assume_aligned<32>(&s.i);
+ *p = 48;
+ return s.i == 48;
+}
+
+static_assert( test01() );