Skip to content

Commit

Permalink
switch to the OpenCL 1.x atomics for more portability
Browse files Browse the repository at this point in the history
  • Loading branch information
bashbaug committed Oct 1, 2024
1 parent 83aeec8 commit 656647f
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions samples/16_floatatomics/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,15 @@ float atomic_add_f(volatile global float* addr, float val)
// This is the traditional fallback that uses a compare and exchange loop.
// It is much slower, but it supports returning the previous value.
//#pragma message("using slow emulated float atomics")
volatile global atomic_float* faddr = (volatile global atomic_float*)addr;
float old;
float new;
volatile global int* iaddr = (volatile global int*)addr;
int old;
int check;
do {
old = atomic_load_explicit(faddr, memory_order_relaxed);
new = old + val;
} while (!atomic_compare_exchange_strong_explicit(faddr, &old, new, memory_order_relaxed, memory_order_relaxed));
return old;
old = atomic_or(iaddr, 0); // emulated atomic load
int new = as_int(as_float(old) + val);
check = atomic_cmpxchg(iaddr, old, new);
} while (check != old);
return as_float(old);
#endif
}
Expand Down

0 comments on commit 656647f

Please sign in to comment.