[libcxx-commits] [libcxx] [libc++] Implement C++20 atomic_ref (PR #76647)
Damien L-G via libcxx-commits
libcxx-commits at lists.llvm.org
Wed Feb 7 05:00:06 PST 2024
================
@@ -0,0 +1,335 @@
+// -*- C++ -*-
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+// Kokkos v. 4.0
+// Copyright (2022) National Technology & Engineering
+// Solutions of Sandia, LLC (NTESS).
+//
+// Under the terms of Contract DE-NA0003525 with NTESS,
+// the U.S. Government retains certain rights in this software.
+//
+//===---------------------------------------------------------------------===//
+
+#ifndef _LIBCPP___ATOMIC_ATOMIC_REF_H
+#define _LIBCPP___ATOMIC_ATOMIC_REF_H
+
+#include <__assert>
+#include <__atomic/atomic_sync.h>
+#include <__atomic/check_memory_order.h>
+#include <__atomic/to_gcc_order.h>
+#include <__concepts/arithmetic.h>
+#include <__concepts/same_as.h>
+#include <__config>
+#include <__memory/addressof.h>
+#include <__type_traits/has_unique_object_representation.h>
+#include <__type_traits/is_trivially_copyable.h>
+#include <cstddef>
+#include <cstdint>
+
+#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
+# pragma GCC system_header
+#endif
+
+_LIBCPP_PUSH_MACROS
+#include <__undef_macros>
+
+_LIBCPP_BEGIN_NAMESPACE_STD
+
+#if _LIBCPP_STD_VER >= 20
+
+template <class _Tp>
+struct __atomic_ref_base {
+ _Tp* __ptr_;
+
+ // whether the object pointed to has padding AND we will actually ignore them
+ static constexpr bool __might_have_padding =
+# if __has_builtin(__builtin_clear_padding)
+ !has_unique_object_representations_v<_Tp> && !same_as<_Tp, float> && !same_as<_Tp, double>;
----------------
dalg24 wrote:
Do you prefer this instead?
```diff
diff --git a/libcxx/include/__atomic/atomic_ref.h b/libcxx/include/__atomic/atomic_ref.h
index 126fa60e1816..f376051a902e 100644
--- a/libcxx/include/__atomic/atomic_ref.h
+++ b/libcxx/include/__atomic/atomic_ref.h
@@ -45,20 +45,10 @@ template <class _Tp>
struct __atomic_ref_base {
_Tp* __ptr_;
- // whether the object pointed to has padding AND we will actually ignore them
- static constexpr bool __might_have_padding =
-# if __has_builtin(__builtin_clear_padding)
- !has_unique_object_representations_v<_Tp> && !same_as<_Tp, float> && !same_as<_Tp, double>;
-# else
- false;
-# endif
-
_LIBCPP_HIDE_FROM_ABI static _Tp* __clear_padding(_Tp& __val) noexcept {
_Tp* __ptr = std::addressof(__val);
# if __has_builtin(__builtin_clear_padding)
- if constexpr (__might_have_padding) {
- __builtin_clear_padding(__ptr);
- }
+ __builtin_clear_padding(__ptr);
# endif
return __ptr;
}
@@ -109,7 +99,13 @@ struct __atomic_ref_base {
_LIBCPP_HIDE_FROM_ABI static bool __compare_exchange(
_Tp* __ptr, _Tp* __expected, _Tp* __desired, bool __is_weak, int __success, int __failure) noexcept {
- if constexpr (!__might_have_padding) {
+ if constexpr (
+# if __has_builtin(__builtin_clear_padding)
+ has_unique_object_representations_v<_Tp> || same_as<_Tp, float> || same_as<_Tp, double>;
+# else
+ true
+# endif
+ ) {
return __atomic_compare_exchange(__ptr, __expected, __desired, __is_weak, __success, __failure);
} else { // _Tp has padding bits and __builtin_clear_padding is available
__clear_padding(*__desired);
https://github.com/llvm/llvm-project/pull/76647
More information about the libcxx-commits
mailing list