2 * Copyright 2014 Blender Foundation
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #ifndef __UTIL_ATOMIC_H__
18 #define __UTIL_ATOMIC_H__
20 #ifndef __KERNEL_GPU__
22 /* Using atomic ops header from Blender. */
23 #include "atomic_ops.h"
25 #define atomic_add_and_fetch_float(p, x) atomic_add_and_fetch_fl((p), (x))
26 #define atomic_compare_and_swap_float(p, old_val, new_val) atomic_cas_float((p), (old_val), (new_val))
28 #define atomic_fetch_and_inc_uint32(p) atomic_fetch_and_add_uint32((p), 1)
29 #define atomic_fetch_and_dec_uint32(p) atomic_fetch_and_add_uint32((p), -1)
31 #define CCL_LOCAL_MEM_FENCE 0
32 #define ccl_barrier(flags) (void)0
34 #else /* __KERNEL_GPU__ */
36 #ifdef __KERNEL_OPENCL__
38 /* Float atomics implementation credits:
39 * http://suhorukov.blogspot.in/2011/12/opencl-11-atomic-operations-on-floating.html
41 ccl_device_inline float atomic_add_and_fetch_float(volatile ccl_global float *source,
45 unsigned int int_value;
49 unsigned int int_value;
53 prev_value.float_value = *source;
54 new_value.float_value = prev_value.float_value + operand;
55 } while(atomic_cmpxchg((volatile ccl_global unsigned int *)source,
57 new_value.int_value) != prev_value.int_value);
58 return new_value.float_value;
61 ccl_device_inline float atomic_compare_and_swap_float(volatile ccl_global float *dest,
62 const float old_val, const float new_val)
65 unsigned int int_value;
67 } new_value, prev_value, result;
68 prev_value.float_value = old_val;
69 new_value.float_value = new_val;
70 result.int_value = atomic_cmpxchg((volatile ccl_global unsigned int *)dest,
71 prev_value.int_value, new_value.int_value);
72 return result.float_value;
75 #define atomic_fetch_and_add_uint32(p, x) atomic_add((p), (x))
76 #define atomic_fetch_and_inc_uint32(p) atomic_inc((p))
77 #define atomic_fetch_and_dec_uint32(p) atomic_dec((p))
79 #define CCL_LOCAL_MEM_FENCE CLK_LOCAL_MEM_FENCE
80 #define ccl_barrier(flags) barrier(flags)
82 #endif /* __KERNEL_OPENCL__ */
84 #ifdef __KERNEL_CUDA__
86 #define atomic_add_and_fetch_float(p, x) (atomicAdd((float*)(p), (float)(x)) + (float)(x))
88 #define atomic_fetch_and_add_uint32(p, x) atomicAdd((unsigned int*)(p), (unsigned int)(x))
89 #define atomic_fetch_and_sub_uint32(p, x) atomicSub((unsigned int*)(p), (unsigned int)(x))
90 #define atomic_fetch_and_inc_uint32(p) atomic_fetch_and_add_uint32((p), 1)
91 #define atomic_fetch_and_dec_uint32(p) atomic_fetch_and_sub_uint32((p), 1)
93 ccl_device_inline float atomic_compare_and_swap_float(volatile float *dest,
94 const float old_val, const float new_val)
97 unsigned int int_value;
99 } new_value, prev_value, result;
100 prev_value.float_value = old_val;
101 new_value.float_value = new_val;
102 result.int_value = atomicCAS((unsigned int *)dest, prev_value.int_value,new_value.int_value);
103 return result.float_value;
106 #define CCL_LOCAL_MEM_FENCE
107 #define ccl_barrier(flags) __syncthreads()
109 #endif /* __KERNEL_CUDA__ */
111 #endif /* __KERNEL_GPU__ */
113 #endif /* __UTIL_ATOMIC_H__ */