Cycles: Add more atomic operations
[blender-staging.git] / intern / cycles / util / util_atomic.h
1 /*
2  * Copyright 2014 Blender Foundation
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #ifndef __UTIL_ATOMIC_H__
18 #define __UTIL_ATOMIC_H__
19
20 #ifndef __KERNEL_GPU__
21
22 /* Using atomic ops header from Blender. */
23 #include "atomic_ops.h"
24
25 ATOMIC_INLINE void atomic_update_max_z(size_t *maximum_value, size_t value)
26 {
27         size_t prev_value = *maximum_value;
28         while(prev_value < value) {
29                 if(atomic_cas_z(maximum_value, prev_value, value) != prev_value) {
30                         break;
31                 }
32         }
33 }
34
35 #define atomic_add_and_fetch_float(p, x) atomic_add_and_fetch_fl((p), (x))
36
37 #define atomic_fetch_and_inc_uint32(p) atomic_fetch_and_add_uint32((p), 1)
38
39 #define CCL_LOCAL_MEM_FENCE 0
40 #define ccl_barrier(flags) (void)0
41
42 #else  /* __KERNEL_GPU__ */
43
44 #ifdef __KERNEL_OPENCL__
45
46 /* Float atomics implementation credits:
47  *   http://suhorukov.blogspot.in/2011/12/opencl-11-atomic-operations-on-floating.html
48  */
49 ccl_device_inline float atomic_add_and_fetch_float(volatile ccl_global float *source,
50                                         const float operand)
51 {
52         union {
53                 unsigned int int_value;
54                 float float_value;
55         } new_value;
56         union {
57                 unsigned int int_value;
58                 float float_value;
59         } prev_value;
60         do {
61                 prev_value.float_value = *source;
62                 new_value.float_value = prev_value.float_value + operand;
63         } while(atomic_cmpxchg((volatile ccl_global unsigned int *)source,
64                                prev_value.int_value,
65                                new_value.int_value) != prev_value.int_value);
66         return new_value.float_value;
67 }
68
69 #define atomic_fetch_and_add_uint32(p, x) atomic_add((p), (x))
70 #define atomic_fetch_and_inc_uint32(p) atomic_inc((p))
71
72 #define CCL_LOCAL_MEM_FENCE CLK_LOCAL_MEM_FENCE
73 #define ccl_barrier(flags) barrier(flags)
74
75 #endif  /* __KERNEL_OPENCL__ */
76
77 #ifdef __KERNEL_CUDA__
78
79 #define atomic_add_and_fetch_float(p, x) (atomicAdd((float*)(p), (float)(x)) + (float)(x))
80
81 #define atomic_fetch_and_add_uint32(p, x) atomicAdd((unsigned int*)(p), (unsigned int)(x))
82 #define atomic_fetch_and_inc_uint32(p) atomic_fetch_and_add_uint32((p), 1)
83
84 #define CCL_LOCAL_MEM_FENCE
85 #define ccl_barrier(flags) __syncthreads()
86
87 #endif  /* __KERNEL_CUDA__ */
88
89 #endif  /* __KERNEL_GPU__ */
90
91 #endif /* __UTIL_ATOMIC_H__ */