Skip to content

Commit 9e111d2

Browse files
committed
[c11_atomics] Add float/double type to atomic_compare_exchange test
OpenCL spec supports atomic_float and atomic_double types for atomic_compare_exchange_* builtins.
1 parent a406b34 commit 9e111d2

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

test_conformance/c11_atomics/test_atomics.cpp

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,68 @@
2323
#include <sstream>
2424
#include <vector>
2525

26+
template <>
27+
bool host_atomic_compare_exchange<float, float>(
28+
volatile float *a, float *expected, float desired,
29+
TExplicitMemoryOrderType order_success,
30+
TExplicitMemoryOrderType order_failure)
31+
{
32+
union FloatInt {
33+
float f;
34+
int i;
35+
};
36+
FloatInt a2{ *a };
37+
FloatInt expected2{ *expected };
38+
FloatInt desired2{ desired };
39+
FloatInt tmp;
40+
#if defined(_MSC_VER) || (defined(__INTEL_COMPILER) && defined(WIN32))
41+
tmp.i = InterlockedCompareExchange(&a2.i, desired2.i, expected2.i);
42+
#elif defined(__GNUC__)
43+
tmp.i = __sync_val_compare_and_swap(&a2.i, expected2.i, desired2.i);
44+
#else
45+
log_info("Host function not implemented: atomic_compare_exchange\n");
46+
tmp.i = 0;
47+
#endif
48+
if (tmp.i == expected2.i)
49+
{
50+
*a = a2.f;
51+
return true;
52+
}
53+
*expected = tmp.f;
54+
return false;
55+
}
56+
57+
template <>
58+
bool host_atomic_compare_exchange<double, double>(
59+
volatile double *a, double *expected, double desired,
60+
TExplicitMemoryOrderType order_success,
61+
TExplicitMemoryOrderType order_failure)
62+
{
63+
union DoubleInt64 {
64+
double d;
65+
int64_t i;
66+
};
67+
DoubleInt64 a2{ *a };
68+
DoubleInt64 expected2{ *expected };
69+
DoubleInt64 desired2{ desired };
70+
DoubleInt64 tmp;
71+
#if defined(_MSC_VER) || (defined(__INTEL_COMPILER) && defined(WIN32))
72+
tmp.i = InterlockedCompareExchange(&a2.i, desired2.i, expected2.i);
73+
#elif defined(__GNUC__)
74+
tmp.i = __sync_val_compare_and_swap(&a2.i, expected2.i, desired2.i);
75+
#else
76+
log_info("Host function not implemented: atomic_compare_exchange\n");
77+
tmp.i = 0;
78+
#endif
79+
if (tmp.i == expected2.i)
80+
{
81+
*a = a2.d;
82+
return true;
83+
}
84+
*expected = tmp.d;
85+
return false;
86+
}
87+
2688
template <typename HostAtomicType, typename HostDataType>
2789
class CBasicTestStore
2890
: public CBasicTestMemOrderScope<HostAtomicType, HostDataType> {
@@ -852,6 +914,14 @@ int test_atomic_compare_exchange_strong_generic(cl_device_id deviceID,
852914
TYPE_ATOMIC_ULONG, useSVM);
853915
EXECUTE_TEST(error,
854916
test_ulong.Execute(deviceID, context, queue, num_elements));
917+
CBasicTestCompareStrong<HOST_ATOMIC_FLOAT, HOST_FLOAT> test_float(
918+
TYPE_ATOMIC_FLOAT, useSVM);
919+
EXECUTE_TEST(error,
920+
test_float.Execute(deviceID, context, queue, num_elements));
921+
CBasicTestCompareStrong<HOST_ATOMIC_DOUBLE, HOST_DOUBLE> test_double(
922+
TYPE_ATOMIC_DOUBLE, useSVM);
923+
EXECUTE_TEST(error,
924+
test_double.Execute(deviceID, context, queue, num_elements));
855925
if (AtomicTypeInfo(TYPE_ATOMIC_SIZE_T).Size(deviceID) == 4)
856926
{
857927
CBasicTestCompareStrong<HOST_ATOMIC_INTPTR_T32, HOST_INTPTR_T32>
@@ -986,6 +1056,14 @@ int test_atomic_compare_exchange_weak_generic(cl_device_id deviceID,
9861056
test_long.Execute(deviceID, context, queue, num_elements));
9871057
CBasicTestCompareWeak<HOST_ATOMIC_ULONG, HOST_ULONG> test_ulong(
9881058
TYPE_ATOMIC_ULONG, useSVM);
1059+
CBasicTestCompareWeak<HOST_ATOMIC_FLOAT, HOST_FLOAT> test_float(
1060+
TYPE_ATOMIC_FLOAT, useSVM);
1061+
EXECUTE_TEST(error,
1062+
test_float.Execute(deviceID, context, queue, num_elements));
1063+
CBasicTestCompareWeak<HOST_ATOMIC_DOUBLE, HOST_DOUBLE> test_double(
1064+
TYPE_ATOMIC_DOUBLE, useSVM);
1065+
EXECUTE_TEST(error,
1066+
test_double.Execute(deviceID, context, queue, num_elements));
9891067
EXECUTE_TEST(error,
9901068
test_ulong.Execute(deviceID, context, queue, num_elements));
9911069
if (AtomicTypeInfo(TYPE_ATOMIC_SIZE_T).Size(deviceID) == 4)

0 commit comments

Comments
 (0)