[libcxx-commits] [libcxx] [libc++][In progress] Floating Point Atomic (PR #67799)

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Fri Oct 20 10:35:53 PDT 2023


================
@@ -0,0 +1,119 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef TEST_STD_ATOMICS_ATOMICS_TYPES_FLOAT_TEST_HELPER_H
+#define TEST_STD_ATOMICS_ATOMICS_TYPES_FLOAT_TEST_HELPER_H
+
+#include <atomic>
+#include <cassert>
+#include <cmath>
+#include <thread>
+#include <vector>
+
+#include "test_macros.h"
+#include "make_test_thread.h"
+
+template <class T>
+bool approximately_equals(T x, T y) {
+  T epsilon = 0.001;
+  return std::abs(x - y) < epsilon;
+}
+
+// Test that all threads see the exact same sequence of events
+// Test will pass 100% if store_op and load_op are correctly
+// affecting the memory with seq_cst order
+template <class T, template <class> class MaybeVolatile, class StoreOp, class LoadOp>
+void test_seq_cst(StoreOp store_op, LoadOp load_op) {
+#ifndef TEST_HAS_NO_THREADS
+  for (int i = 0; i < 100; ++i) {
+    T old_value = 0.0;
+    T new_value = 1.0;
+
+    MaybeVolatile<std::atomic<T>> x(old_value);
+    MaybeVolatile<std::atomic<T>> y(old_value);
+
+    std::atomic_bool x_update_first(false);
+    std::atomic_bool y_update_first(false);
----------------
ldionne wrote:

```suggestion
    std::atomic_bool x_updated_first(false);
    std::atomic_bool y_updated_first(false);
```

https://github.com/llvm/llvm-project/pull/67799


More information about the libcxx-commits mailing list