Skip to content

Commit

Permalink
Making compliant with LLVM style, other minor changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
Logikable committed Jan 23, 2024
1 parent e2ed34f commit 41843bb
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 183 deletions.
12 changes: 11 additions & 1 deletion SingleSource/UnitTests/Atomic/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
# These tests output numerical values to improve debuggability.
# Setting FP_TOLERANCE causes the test checker to use fpcmp to compare
# test and expected output.
# Because the nonatomic output may vary wildly even in a passing run,
# we set FP_TOLERANCE high so the test will never fail due to
# differing numerical results.
# The tests each have their own correctness checking and will fail
# properly if something is actually wrong.
set(FP_TOLERANCE 1000000)

# Link the Clang built libatomic.
execute_process(COMMAND ${CMAKE_C_COMPILER} --print-file-name=libclang_rt.atomic.so
OUTPUT_VARIABLE _path_to_libatomic
OUTPUT_STRIP_TRAILING_WHITESPACE)
get_filename_component(_libatomic_dir ${_path_to_libatomic} DIRECTORY)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${_path_to_libatomic} -Wl,-rpath=${_libatomic_dir}")
add_link_options("LINKER:${_path_to_libatomic},-rpath=${_libatomic_dir}")

llvm_singlesource()
34 changes: 13 additions & 21 deletions SingleSource/UnitTests/Atomic/big_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,26 +50,24 @@
#include "util.h"

static constexpr int kBigSize = 10;
struct big {
struct big_t {
int v[kBigSize];
};

// The big struct cmpxchg test is identical to the numeric cmpxchg test, except
// each element of the underlying array is incremented.
void looper_big_cmpxchg(big *abig, big &bbig, int success_model,
void looper_big_cmpxchg(big_t *abig, big_t &bbig, int success_model,
int fail_model) {
for (int n = 0; n < kIterations; ++n) {
big desired, expected = {};
big_t desired, expected = {};
do {
desired = expected;
for (int k = 0; k < kBigSize; ++k) {
for (int k = 0; k < kBigSize; ++k)
desired.v[k]++;
}
} while (!__atomic_compare_exchange(abig, &expected, &desired, true,
success_model, fail_model));
for (int k = 0; k < kBigSize; ++k) {
for (int k = 0; k < kBigSize; ++k)
bbig.v[k]++;
}
}
}

Expand All @@ -78,32 +76,26 @@ void test_big_cmpxchg() {

for (int success_model : atomic_compare_exchange_models) {
for (int fail_model : atomic_compare_exchange_models) {
big abig = {};
big bbig = {};
for (int n = 0; n < kThreads; ++n) {
big_t abig = {};
big_t bbig = {};
for (int n = 0; n < kThreads; ++n)
pool.emplace_back(looper_big_cmpxchg, &abig, std::ref(bbig),
success_model, fail_model);
}
for (int n = 0; n < kThreads; ++n) {
for (int n = 0; n < kThreads; ++n)
pool[n].join();
}
pool.clear();
std::cout << "CMPXCHG: ";
std::cout << "atomic: ";
for (int n = 0; n < kBigSize; ++n) {
for (int n = 0; n < kBigSize; ++n)
std::cout << abig.v[n] << " ";
}
std::cout << "\n ";
std::cout << "nonatomic: ";
for (int n = 0; n < kBigSize; ++n) {
for (int n = 0; n < kBigSize; ++n)
std::cout << bbig.v[n] << " ";
}
std::cout << "\n";
for (int n = 0; n < kBigSize; ++n) {
if (lt(abig.v[n], bbig.v[n]) || abig.v[n] != kExpected) {
for (int n = 0; n < kBigSize; ++n)
if (lt(abig.v[n], bbig.v[n]) || abig.v[n] != kExpected)
fail();
}
}
}
}
}
Expand Down
28 changes: 10 additions & 18 deletions SingleSource/UnitTests/Atomic/float_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,28 +61,23 @@ void test_float_scalar_xchg() {
for (int model : atomic_exchange_models) {
T afloat = 0;
T ffloat = 0;
for (int n = 0; n < kThreads; ++n) {
for (int n = 0; n < kThreads; ++n)
pool.emplace_back(looper_numeric_xchg_atomic<T>, &afloat, model);
}
for (int n = 0; n < kThreads; ++n) {
for (int n = 0; n < kThreads; ++n)
pool[n].join();
}
pool.clear();
for (int n = 0; n < kThreads; ++n) {
for (int n = 0; n < kThreads; ++n)
pool.emplace_back(looper_numeric_xchg_nonatomic<T>, std::ref(ffloat),
model);
}
for (int n = 0; n < kThreads; ++n) {
for (int n = 0; n < kThreads; ++n)
pool[n].join();
}
pool.clear();
std::cout << "SCALAR (FETCH ADD): "
<< "atomic: " << afloat << " "
<< "nonatomic: " << ffloat << "\n";
if (lt(afloat, ffloat) || afloat < expected * (1 - kEpsilon) ||
afloat > expected * (1 + kEpsilon)) {
afloat > expected * (1 + kEpsilon))
fail();
}
}
}

Expand All @@ -97,26 +92,23 @@ void test_float_scalar_cmpxchg() {
for (int fail_model : atomic_compare_exchange_models) {
T afloat = 0;
T ffloat = 0;
for (int n = 0; n < kThreads; ++n) {
for (int n = 0; n < kThreads; ++n)
pool.emplace_back(looper_numeric_cmpxchg<T>, &afloat, std::ref(ffloat),
success_model, fail_model);
}
for (int n = 0; n < kThreads; ++n) {
for (int n = 0; n < kThreads; ++n)
pool[n].join();
}
pool.clear();
std::cout << "SCALAR (FETCH ADD): "
<< "atomic: " << afloat << " "
<< "nonatomic: " << ffloat << "\n";
if (lt(afloat, ffloat) || afloat < expected * (1 - kEpsilon) ||
afloat > expected * (1 + kEpsilon)) {
afloat > expected * (1 + kEpsilon))
fail();
}
}
}
}

void test_float() {
void test_floating_point() {
printf("Testing float\n");
test_float_scalar_xchg<float>();
test_float_scalar_cmpxchg<float>();
Expand All @@ -130,6 +122,6 @@ int main() {
printf("%d threads; %d iterations each; total of %d\n", kThreads, kIterations,
kExpected);

test_float();
test_floating_point();
printf("PASSED\n");
}
60 changes: 20 additions & 40 deletions SingleSource/UnitTests/Atomic/int_aligned_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,19 +71,16 @@ void test_int_fetch_add(T &aint, T &iint) {
for (int model : atomic_fetch_models) {
aint = 0;
iint = 0;
for (int n = 0; n < kThreads; ++n) {
for (int n = 0; n < kThreads; ++n)
pool.emplace_back(looper_int_fetch_add<T>, &aint, std::ref(iint), model);
}
for (int n = 0; n < kThreads; ++n) {
for (int n = 0; n < kThreads; ++n)
pool[n].join();
}
pool.clear();
std::cout << "FETCH ADD: "
<< "atomic: " << aint << " "
<< "nonatomic: " << iint << "\n";
if (lt(aint, iint) || aint != val * kExpected) {
if (lt(aint, iint) || aint != val * kExpected)
fail();
}
}
}

Expand All @@ -103,19 +100,16 @@ void test_int_fetch_sub(T &aint, T &iint) {
for (int model : atomic_fetch_models) {
aint = val * kExpected;
iint = val * kExpected;
for (int n = 0; n < kThreads; ++n) {
for (int n = 0; n < kThreads; ++n)
pool.emplace_back(looper_int_fetch_sub<T>, &aint, std::ref(iint), model);
}
for (int n = 0; n < kThreads; ++n) {
for (int n = 0; n < kThreads; ++n)
pool[n].join();
}
pool.clear();
std::cout << "FETCH SUB: "
<< "atomic: " << aint << " "
<< "nonatomic: " << iint << "\n";
if (lt(iint, aint) || aint != 0) {
if (lt(iint, aint) || aint != 0)
fail();
}
}
}

Expand Down Expand Up @@ -154,20 +148,17 @@ void test_int_fetch_and(T &aint, T &iint) {
for (int model : atomic_fetch_models) {
T acnt = 0, icnt = 0;
aint = ~0, iint = ~0;
for (int n = 0; n < kThreads; ++n) {
for (int n = 0; n < kThreads; ++n)
pool.emplace_back(looper_int_fetch_and<T>, n, &aint, std::ref(iint),
&acnt, &icnt, model);
}
for (int n = 0; n < kThreads; ++n) {
for (int n = 0; n < kThreads; ++n)
pool[n].join();
}
pool.clear();
std::cout << "FETCH AND: "
<< "atomic: " << acnt << " "
<< "nonatomic: " << icnt << "\n";
if (acnt != kExpected) {
if (acnt != kExpected)
fail();
}
}
}

Expand Down Expand Up @@ -199,20 +190,17 @@ void test_int_fetch_or(T &aint, T &iint) {
for (int model : atomic_fetch_models) {
T acnt = 0, icnt = 0;
aint = 0, iint = 0;
for (int n = 0; n < kThreads; ++n) {
for (int n = 0; n < kThreads; ++n)
pool.emplace_back(looper_int_fetch_or<T>, n, &aint, std::ref(iint),
&acnt, &icnt, model);
}
for (int n = 0; n < kThreads; ++n) {
for (int n = 0; n < kThreads; ++n)
pool[n].join();
}
pool.clear();
std::cout << "FETCH OR: "
<< "atomic: " << acnt << " "
<< "nonatomic: " << icnt << "\n";
if (acnt != kExpected) {
if (acnt != kExpected)
fail();
}
}
}

Expand All @@ -230,19 +218,16 @@ void test_int_fetch_xor(T &aint, T &iint) {
for (int model : atomic_fetch_models) {
aint = 0;
iint = 0;
for (int n = 0; n < kThreads; ++n) {
for (int n = 0; n < kThreads; ++n)
pool.emplace_back(looper_int_fetch_xor<T>, &aint, std::ref(iint), model);
}
for (int n = 0; n < kThreads; ++n) {
for (int n = 0; n < kThreads; ++n)
pool[n].join();
}
pool.clear();
std::cout << "FETCH XOR: "
<< "atomic: " << aint << " "
<< "nonatomic: " << iint << "\n";
if (aint != 0) {
if (aint != 0)
fail();
}
}
}

Expand All @@ -253,26 +238,21 @@ void test_int_xchg(T &aint, T &iint) {
for (int model : atomic_exchange_models) {
aint = 0;
iint = 0;
for (int n = 0; n < kThreads; ++n) {
for (int n = 0; n < kThreads; ++n)
pool.emplace_back(looper_numeric_xchg_atomic<T>, &aint, model);
}
for (int n = 0; n < kThreads; ++n) {
for (int n = 0; n < kThreads; ++n)
pool[n].join();
}
pool.clear();
for (int n = 0; n < kThreads; ++n) {
for (int n = 0; n < kThreads; ++n)
pool.emplace_back(looper_numeric_xchg_nonatomic<T>, std::ref(iint),
model);
}
for (int n = 0; n < kThreads; ++n) {
for (int n = 0; n < kThreads; ++n)
pool[n].join();
}
pool.clear();
std::cout << "XCHG: ";
print_int(aint, iint);
if (lt(aint, iint) || aint != val * kExpected) {
if (lt(aint, iint) || aint != val * kExpected)
fail();
}
}
}

Expand Down
Loading

0 comments on commit 41843bb

Please sign in to comment.