| 1 |
// Start of atomics16.h |
| 2 |
|
| 3 |
SCALAR_FUN_ATTR int16_t atomic_cmpxchg_i16_global(volatile __global int16_t *p, |
| 4 |
int16_t cmp, int16_t val); |
| 5 |
SCALAR_FUN_ATTR int16_t atomic_cmpxchg_i16_shared(volatile __local int16_t *p, |
| 6 |
int16_t cmp, int16_t val); |
| 7 |
SCALAR_FUN_ATTR int16_t atomic_add_i16_global(volatile __global int16_t *p, int16_t x); |
| 8 |
SCALAR_FUN_ATTR int16_t atomic_add_i16_shared(volatile __local int16_t *p, int16_t x); |
| 9 |
SCALAR_FUN_ATTR f16 atomic_fadd_f16_global(volatile __global uint16_t *p, f16 x); |
| 10 |
SCALAR_FUN_ATTR f16 atomic_fadd_f16_shared(volatile __local uint16_t *p, f16 x); |
| 11 |
SCALAR_FUN_ATTR int16_t atomic_smax_i16_global(volatile __global int16_t *p, int16_t x); |
| 12 |
SCALAR_FUN_ATTR int16_t atomic_smax_i16_shared(volatile __local int16_t *p, int16_t x); |
| 13 |
SCALAR_FUN_ATTR int16_t atomic_smin_i16_global(volatile __global int16_t *p, int16_t x); |
| 14 |
SCALAR_FUN_ATTR int16_t atomic_smin_i16_shared(volatile __local int16_t *p, int16_t x); |
| 15 |
SCALAR_FUN_ATTR uint16_t atomic_umax_i16_global(volatile __global uint16_t *p, uint16_t x); |
| 16 |
SCALAR_FUN_ATTR uint16_t atomic_umax_i16_shared(volatile __local uint16_t *p, uint16_t x); |
| 17 |
SCALAR_FUN_ATTR uint16_t atomic_umin_i16_global(volatile __global uint16_t *p, uint16_t x); |
| 18 |
SCALAR_FUN_ATTR uint16_t atomic_umin_i16_shared(volatile __local uint16_t *p, uint16_t x); |
| 19 |
SCALAR_FUN_ATTR int16_t atomic_and_i16_global(volatile __global int16_t *p, int16_t x); |
| 20 |
SCALAR_FUN_ATTR int16_t atomic_and_i16_shared(volatile __local int16_t *p, int16_t x); |
| 21 |
SCALAR_FUN_ATTR int16_t atomic_or_i16_global(volatile __global int16_t *p, int16_t x); |
| 22 |
SCALAR_FUN_ATTR int16_t atomic_or_i16_shared(volatile __local int16_t *p, int16_t x); |
| 23 |
SCALAR_FUN_ATTR int16_t atomic_xor_i16_global(volatile __global int16_t *p, int16_t x); |
| 24 |
SCALAR_FUN_ATTR int16_t atomic_xor_i16_shared(volatile __local int16_t *p, int16_t x); |
| 25 |
|
| 26 |
SCALAR_FUN_ATTR int16_t atomic_cmpxchg_i16_global(volatile __global int16_t *p, |
| 27 |
int16_t cmp, int16_t val) { |
| 28 |
int offset = ((uintptr_t)p >> 1 & 1); |
| 29 |
volatile __global int32_t *p32 = (volatile __global int32_t*)((uintptr_t)p & ~0x3); |
| 30 |
|
| 31 |
int shift = offset * 16; |
| 32 |
int32_t mask = 0xffff << shift; |
| 33 |
int32_t shifted_val = val << shift; |
| 34 |
int32_t shifted_cmp = cmp << shift; |
| 35 |
|
| 36 |
uint32_t old = shifted_cmp; |
| 37 |
uint32_t upd = shifted_val; |
| 38 |
uint32_t got; |
| 39 |
|
| 40 |
while ((got=atomic_cmpxchg_i32_global(p32, old, upd)) != old) { |
| 41 |
old = got; |
| 42 |
upd = (old & ~mask) | shifted_val; |
| 43 |
} |
| 44 |
|
| 45 |
return old >> shift; |
| 46 |
} |
| 47 |
|
| 48 |
SCALAR_FUN_ATTR int16_t atomic_cmpxchg_i16_shared(volatile __local int16_t *p, |
| 49 |
int16_t cmp, int16_t val) { |
| 50 |
int offset = ((uintptr_t)p >> 1 & 1); |
| 51 |
volatile __local int32_t *p32 = (volatile __local int32_t*)((uintptr_t)p & ~0x3); |
| 52 |
|
| 53 |
int shift = offset * 16; |
| 54 |
int32_t mask = 0xffff << shift; |
| 55 |
int32_t shifted_val = val << shift; |
| 56 |
int32_t shifted_cmp = cmp << shift; |
| 57 |
|
| 58 |
uint32_t old = shifted_cmp; |
| 59 |
uint32_t upd = shifted_val; |
| 60 |
uint32_t got; |
| 61 |
|
| 62 |
while ((got=atomic_cmpxchg_i32_shared(p32, old, upd)) != old) { |
| 63 |
old = got; |
| 64 |
upd = (old & ~mask) | shifted_val; |
| 65 |
} |
| 66 |
|
| 67 |
return old >> shift; |
| 68 |
} |
| 69 |
|
| 70 |
// Convenience macro for arithmetic. |
| 71 |
#define DEFINE_16BIT_ATOMIC(name, T, op) \ |
| 72 |
SCALAR_FUN_ATTR T \ |
| 73 |
atomic_##name##_i16_global(volatile __global T *p, T val) { \ |
| 74 |
int offset = ((uintptr_t)p >> 1 & 1); \ |
| 75 |
volatile __global int32_t *p32 = (volatile __global int32_t*)((uintptr_t)p & ~0x3); \ |
| 76 |
int shift = offset * 16; \ |
| 77 |
int32_t mask = 0xffff << shift; \ |
| 78 |
int32_t old = 0; \ |
| 79 |
int32_t upd = mask & (op(old >> shift, val) << shift); \ |
| 80 |
int32_t saw; \ |
| 81 |
while ((saw=atomic_cmpxchg_i32_global(p32, old, upd)) != old) { \ |
| 82 |
old = saw; \ |
| 83 |
upd = (old & ~mask) | ((op(old >> shift, val)) << shift); \ |
| 84 |
} \ |
| 85 |
return old >> shift; \ |
| 86 |
} \ |
| 87 |
SCALAR_FUN_ATTR T \ |
| 88 |
atomic_##name##_i16_shared(volatile __local T *p, T val) { \ |
| 89 |
int offset = ((uintptr_t)p >> 1 & 1); \ |
| 90 |
volatile __local int32_t *p32 = (volatile __local int32_t*)((uintptr_t)p & ~0x3); \ |
| 91 |
int shift = offset * 16; \ |
| 92 |
int32_t mask = 0xffff << shift; \ |
| 93 |
int32_t old = 0; \ |
| 94 |
int32_t upd = mask & ((op(old >> shift, val)) << shift); \ |
| 95 |
int32_t saw; \ |
| 96 |
while ((saw=atomic_cmpxchg_i32_shared(p32, old, upd)) != old) { \ |
| 97 |
old = saw; \ |
| 98 |
upd = (old & ~mask) | ((op(old >> shift, val)) << shift); \ |
| 99 |
} \ |
| 100 |
return old >> shift; \ |
| 101 |
} |
| 102 |
|
| 103 |
DEFINE_16BIT_ATOMIC(add, int16_t, add16); |
| 104 |
DEFINE_16BIT_ATOMIC(smax, int16_t, smax16); |
| 105 |
DEFINE_16BIT_ATOMIC(smin, int16_t, smin16); |
| 106 |
DEFINE_16BIT_ATOMIC(umax, uint16_t, umax16); |
| 107 |
DEFINE_16BIT_ATOMIC(umin, uint16_t, umin16); |
| 108 |
|
| 109 |
SCALAR_FUN_ATTR int16_t atomic_and_i16_global(volatile __global int16_t *p, int16_t val) { |
| 110 |
volatile __global int32_t *p32 = (volatile __global int32_t*)((uintptr_t)p & ~0x3); |
| 111 |
int shift = ((uintptr_t)p >> 1 & 1) * 16; |
| 112 |
int32_t mask = 0xffff << shift; |
| 113 |
return atomic_and_i32_global(p32, ~mask | (val<<shift)) >> shift; |
| 114 |
} |
| 115 |
|
| 116 |
SCALAR_FUN_ATTR int16_t atomic_and_i16_shared(volatile __local int16_t *p, int16_t val) { |
| 117 |
volatile __local int32_t *p32 = (volatile __local int32_t*)((uintptr_t)p & ~0x3); |
| 118 |
int shift = ((uintptr_t)p >> 1 & 1) * 16; |
| 119 |
int32_t mask = 0xffff << shift; |
| 120 |
return atomic_and_i32_shared(p32, ~mask | (val<<shift)) >> shift; |
| 121 |
} |
| 122 |
|
| 123 |
SCALAR_FUN_ATTR int16_t atomic_or_i16_global(volatile __global int16_t *p, int16_t val) { |
| 124 |
volatile __global int32_t *p32 = (volatile __global int32_t*)((uintptr_t)p & ~0x3); |
| 125 |
int shift = ((uintptr_t)p >> 1 & 1) * 16; |
| 126 |
return atomic_or_i32_global(p32, (uint16_t)val<<shift) >> shift; |
| 127 |
} |
| 128 |
|
| 129 |
SCALAR_FUN_ATTR int16_t atomic_or_i16_shared(volatile __local int16_t *p, int16_t val) { |
| 130 |
volatile __local int32_t *p32 = (volatile __local int32_t*)((uintptr_t)p & ~0x3); |
| 131 |
int shift = ((uintptr_t)p >> 1 & 1) * 16; |
| 132 |
return atomic_or_i32_shared(p32, (uint16_t)val<<shift) >> shift; |
| 133 |
} |
| 134 |
|
| 135 |
SCALAR_FUN_ATTR int16_t atomic_xor_i16_global(volatile __global int16_t *p, int16_t val) { |
| 136 |
volatile __global int32_t *p32 = (volatile __global int32_t*)((uintptr_t)p & ~0x3); |
| 137 |
int shift = ((uintptr_t)p >> 1 & 1) * 16; |
| 138 |
return atomic_xor_i32_global(p32, (uint16_t)val<<shift) >> shift; |
| 139 |
} |
| 140 |
|
| 141 |
SCALAR_FUN_ATTR int16_t atomic_xor_i16_shared(volatile __local int16_t *p, int16_t val) { |
| 142 |
volatile __local int32_t *p32 = (volatile __local int32_t*)((uintptr_t)p & ~0x3); |
| 143 |
int shift = ((uintptr_t)p >> 1 & 1) * 16; |
| 144 |
return atomic_xor_i32_shared(p32, (uint16_t)val<<shift) >> shift; |
| 145 |
} |
| 146 |
|
| 147 |
SCALAR_FUN_ATTR f16 atomic_fadd_f16_global(volatile __global uint16_t *p, f16 val) { |
| 148 |
int offset = ((uintptr_t)p >> 1 & 1); |
| 149 |
volatile __global int32_t *p32 = (volatile __global int32_t*)((uintptr_t)p & ~0x3); |
| 150 |
int shift = offset * 16; |
| 151 |
int32_t mask = 0xffff << shift; |
| 152 |
int32_t old = 0; |
| 153 |
int32_t upd = mask & ((int32_t)fptobits_f16_i16(val) << shift); |
| 154 |
int32_t saw; |
| 155 |
while ((saw=atomic_cmpxchg_i32_global(p32, old, upd)) != old) { |
| 156 |
old = saw; |
| 157 |
upd = (old & ~mask) | (int32_t)fptobits_f16_i16(bitstofp_i16_f16((uint32_t)old >> shift) + val) << shift; |
| 158 |
} |
| 159 |
return bitstofp_i16_f16((uint32_t)old >> shift); |
| 160 |
} |
| 161 |
|
| 162 |
SCALAR_FUN_ATTR f16 atomic_fadd_f16_shared(volatile __local uint16_t *p, f16 val) { |
| 163 |
int offset = ((uintptr_t)p >> 1 & 1); |
| 164 |
volatile __local int32_t *p32 = (volatile __local int32_t*)((uintptr_t)p & ~0x3); |
| 165 |
int shift = offset * 16; |
| 166 |
int32_t mask = 0xffff << shift; |
| 167 |
int32_t old = 0; |
| 168 |
int32_t upd = mask & ((int32_t)fptobits_f16_i16(val) << shift); |
| 169 |
int32_t saw; |
| 170 |
while ((saw=atomic_cmpxchg_i32_shared(p32, old, upd)) != old) { |
| 171 |
old = saw; |
| 172 |
upd = (old & ~mask) | (int32_t)fptobits_f16_i16(bitstofp_i16_f16((uint32_t)old >> shift) + val) << shift; |
| 173 |
} |
| 174 |
return bitstofp_i16_f16((uint32_t)old >> shift); |
| 175 |
} |
| 176 |
|
| 177 |
// End of atomics16.h |