Implement global atomics and a memory barrier in the standard library.
This checkin provides the standard set of atomic operations and a memory barrier in the ispc standard library. Both signed and unsigned 32- and 64-bit integer types are supported.
This commit is contained in:
69
stdlib.ispc
69
stdlib.ispc
@@ -295,6 +295,75 @@ static inline uniform int packed_store_active(uniform int a[], uniform int start
|
||||
return __packed_store_active(a, start, vals, __mask);
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
// Atomics and memory barriers
|
||||
|
||||
static inline void memory_barrier() {
|
||||
__memory_barrier();
|
||||
}
|
||||
|
||||
#define DEFINE_ATOMIC_OP(TA,TB,OPA,OPB) \
|
||||
static inline TA atomic_##OPA##_global(uniform reference TA ref, TA value) { \
|
||||
memory_barrier(); \
|
||||
TA ret = __atomic_##OPB##_##TB##_global(ref, value, __mask); \
|
||||
memory_barrier(); \
|
||||
return ret; \
|
||||
}
|
||||
|
||||
DEFINE_ATOMIC_OP(int32,int32,add,add)
|
||||
DEFINE_ATOMIC_OP(int32,int32,subtract,sub)
|
||||
DEFINE_ATOMIC_OP(int32,int32,min,min)
|
||||
DEFINE_ATOMIC_OP(int32,int32,max,max)
|
||||
DEFINE_ATOMIC_OP(int32,int32,and,and)
|
||||
DEFINE_ATOMIC_OP(int32,int32,or,or)
|
||||
DEFINE_ATOMIC_OP(int32,int32,xor,xor)
|
||||
DEFINE_ATOMIC_OP(int32,int32,swap,swap)
|
||||
|
||||
// For everything but atomic min and max, we can use the same
|
||||
// implementations for unsigned as for signed.
|
||||
DEFINE_ATOMIC_OP(unsigned int32,int32,add,add)
|
||||
DEFINE_ATOMIC_OP(unsigned int32,int32,subtract,sub)
|
||||
DEFINE_ATOMIC_OP(unsigned int32,uint32,min,umin)
|
||||
DEFINE_ATOMIC_OP(unsigned int32,uint32,max,umax)
|
||||
DEFINE_ATOMIC_OP(unsigned int32,int32,and,and)
|
||||
DEFINE_ATOMIC_OP(unsigned int32,int32,or,or)
|
||||
DEFINE_ATOMIC_OP(unsigned int32,int32,xor,xor)
|
||||
DEFINE_ATOMIC_OP(unsigned int32,int32,swap,swap)
|
||||
|
||||
DEFINE_ATOMIC_OP(int64,int64,add,add)
|
||||
DEFINE_ATOMIC_OP(int64,int64,subtract,sub)
|
||||
DEFINE_ATOMIC_OP(int64,int64,min,min)
|
||||
DEFINE_ATOMIC_OP(int64,int64,max,max)
|
||||
DEFINE_ATOMIC_OP(int64,int64,and,and)
|
||||
DEFINE_ATOMIC_OP(int64,int64,or,or)
|
||||
DEFINE_ATOMIC_OP(int64,int64,xor,xor)
|
||||
DEFINE_ATOMIC_OP(int64,int64,swap,swap)
|
||||
|
||||
// For everything but atomic min and max, we can use the same
|
||||
// implementations for unsigned as for signed.
|
||||
DEFINE_ATOMIC_OP(unsigned int64,int64,add,add)
|
||||
DEFINE_ATOMIC_OP(unsigned int64,int64,subtract,sub)
|
||||
DEFINE_ATOMIC_OP(unsigned int64,uint64,min,umin)
|
||||
DEFINE_ATOMIC_OP(unsigned int64,uint64,max,umax)
|
||||
DEFINE_ATOMIC_OP(unsigned int64,int64,and,and)
|
||||
DEFINE_ATOMIC_OP(unsigned int64,int64,or,or)
|
||||
DEFINE_ATOMIC_OP(unsigned int64,int64,xor,xor)
|
||||
DEFINE_ATOMIC_OP(unsigned int64,int64,swap,swap)
|
||||
|
||||
#define ATOMIC_DECL_CMPXCHG(TA, TB) \
|
||||
static inline TA atomic_compare_exchange_global( \
|
||||
uniform reference TA ref, TA oldval, TA newval) { \
|
||||
memory_barrier(); \
|
||||
TA ret = __atomic_compare_exchange_##TB##_global(ref, oldval, newval, __mask); \
|
||||
memory_barrier(); \
|
||||
return ret; \
|
||||
}
|
||||
|
||||
ATOMIC_DECL_CMPXCHG(int32, int32)
|
||||
ATOMIC_DECL_CMPXCHG(unsigned int32, int32)
|
||||
ATOMIC_DECL_CMPXCHG(int64, int64)
|
||||
ATOMIC_DECL_CMPXCHG(unsigned int64, int64)
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
// Load/store from/to 8/16-bit types
|
||||
|
||||
|
||||
Reference in New Issue
Block a user