|
| 1 | +#ifdef cl_khr_int64_base_atomics |
| 2 | + #pragma OPENCL EXTENSION cl_khr_int64_base_atomics: enable |
| 3 | + |
| 4 | + long numba_dppl_atomic_add_i64(volatile __generic long *p, long val, int type) { |
| 5 | + long found = *p; |
| 6 | + long expected; |
| 7 | + do { |
| 8 | + expected = found; |
| 9 | + if (type == 1) { /* The address qualifier should be __local */ |
| 10 | + found = atom_cmpxchg((volatile __local ulong *)p, expected, expected + val); |
| 11 | + } else { |
| 12 | + found = atom_cmpxchg((volatile __global ulong *)p, expected, expected + val); |
| 13 | + } |
| 14 | + } while (found != expected); |
| 15 | + return found; |
| 16 | + } |
| 17 | + |
| 18 | + long numba_dppl_atomic_sub_i64(volatile __generic long *p, long val, int type) { |
| 19 | + long found = *p; |
| 20 | + long expected; |
| 21 | + do { |
| 22 | + expected = found; |
| 23 | + if (type == 1) { /* The address qualifier should be __local */ |
| 24 | + found = atom_cmpxchg((volatile __local ulong *)p, expected, expected - val); |
| 25 | + } else { |
| 26 | + found = atom_cmpxchg((volatile __global ulong *)p, expected, expected - val); |
| 27 | + } |
| 28 | + } while (found != expected); |
| 29 | + return found; |
| 30 | + } |
| 31 | + |
| 32 | + #ifdef cl_khr_fp64 |
| 33 | + #pragma OPENCL EXTENSION cl_khr_fp64: enable |
| 34 | + |
| 35 | + double numba_dppl_atomic_cmpxchg_f64(volatile __generic double *p, double cmp, double val, int type) { |
| 36 | + union { |
| 37 | + ulong u64; |
| 38 | + double f64; |
| 39 | + } cmp_union, val_union, old_union; |
| 40 | + |
| 41 | + cmp_union.f64 = cmp; |
| 42 | + val_union.f64 = val; |
| 43 | + if (type == 1) { /* The address qualifier should be __local */ |
| 44 | + old_union.u64 = atom_cmpxchg((volatile __local ulong *) p, cmp_union.u64, val_union.u64); |
| 45 | + } else { |
| 46 | + old_union.u64 = atom_cmpxchg((volatile __global ulong *) p, cmp_union.u64, val_union.u64); |
| 47 | + } |
| 48 | + return old_union.f64; |
| 49 | + } |
| 50 | + |
| 51 | + double numba_dppl_atomic_add_f64(volatile __generic double *p, double val, int type) { |
| 52 | + double found = *p; |
| 53 | + double expected; |
| 54 | + do { |
| 55 | + expected = found; |
| 56 | + found = numba_dppl_atomic_cmpxchg_f64(p, expected, expected + val, type); |
| 57 | + } while (found != expected); |
| 58 | + return found; |
| 59 | + } |
| 60 | + |
| 61 | + double numba_dppl_atomic_sub_f64(volatile __generic double *p, double val, int type) { |
| 62 | + double found = *p; |
| 63 | + double expected; |
| 64 | + do { |
| 65 | + expected = found; |
| 66 | + found = numba_dppl_atomic_cmpxchg_f64(p, expected, expected - val, type); |
| 67 | + } while (found != expected); |
| 68 | + return found; |
| 69 | + } |
| 70 | + #endif |
| 71 | +#endif |
| 72 | + |
| 73 | +float numba_dppl_atomic_cmpxchg_f32(volatile __generic float *p, float cmp, float val, int type) { |
| 74 | + union { |
| 75 | + unsigned int u32; |
| 76 | + float f32; |
| 77 | + } cmp_union, val_union, old_union; |
| 78 | + |
| 79 | + cmp_union.f32 = cmp; |
| 80 | + val_union.f32 = val; |
| 81 | + if (type == 1) { /* The address qualifier should be __local */ |
| 82 | + old_union.u32 = atomic_cmpxchg((volatile __local unsigned int *) p, cmp_union.u32, val_union.u32); |
| 83 | + } else { |
| 84 | + old_union.u32 = atomic_cmpxchg((volatile __global unsigned int *) p, cmp_union.u32, val_union.u32); |
| 85 | + } |
| 86 | + return old_union.f32; |
| 87 | +} |
| 88 | + |
| 89 | +float numba_dppl_atomic_add_f32(volatile __generic float *p, float val, int type) { |
| 90 | + float found = *p; |
| 91 | + float expected; |
| 92 | + do { |
| 93 | + expected = found; |
| 94 | + found = numba_dppl_atomic_cmpxchg_f32(p, expected, expected + val, type); |
| 95 | + } while (found != expected); |
| 96 | + return found; |
| 97 | +} |
| 98 | + |
| 99 | +int numba_dppl_atomic_add_i32(volatile __generic int *p, int val, int type) { |
| 100 | + int found = *p; |
| 101 | + int expected; |
| 102 | + do { |
| 103 | + expected = found; |
| 104 | + if (type == 1) { /* The address qualifier should be __local */ |
| 105 | + found = atomic_cmpxchg((volatile __local unsigned int *)p, expected, expected + val); |
| 106 | + } else { |
| 107 | + found = atomic_cmpxchg((volatile __global unsigned int *)p, expected, expected + val); |
| 108 | + } |
| 109 | + } while (found != expected); |
| 110 | + return found; |
| 111 | +} |
| 112 | + |
| 113 | +float numba_dppl_atomic_sub_f32(volatile __generic float *p, float val, int type) { |
| 114 | + float found = *p; |
| 115 | + float expected; |
| 116 | + do { |
| 117 | + expected = found; |
| 118 | + found = numba_dppl_atomic_cmpxchg_f32(p, expected, expected - val, type); |
| 119 | + } while (found != expected); |
| 120 | + return found; |
| 121 | +} |
| 122 | + |
| 123 | +int numba_dppl_atomic_sub_i32(volatile __generic int *p, int val, int type) { |
| 124 | + int found = *p; |
| 125 | + int expected; |
| 126 | + do { |
| 127 | + expected = found; |
| 128 | + if (type == 1) { /* The address qualifier should be __local */ |
| 129 | + found = atomic_cmpxchg((volatile __local unsigned int *)p, expected, expected - val); |
| 130 | + } else { |
| 131 | + found = atomic_cmpxchg((volatile __global unsigned int *)p, expected, expected - val); |
| 132 | + } |
| 133 | + } while (found != expected); |
| 134 | + return found; |
| 135 | +} |
0 commit comments