433e41fbbb6fb1f4abbed2ef7dd80ad267e36018
[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 #else  /* __KERNEL_GPU__ */
36
37 #ifdef __KERNEL_OPENCL__
38
39 /* Float atomics implementation credits:
40  *   http://suhorukov.blogspot.in/2011/12/opencl-11-atomic-operations-on-floating.html
41  */
42 ccl_device_inline void atomic_add_and_fetch_float(volatile ccl_global float *source,
43                                         const float operand)
44 {
45         union {
46                 unsigned int int_value;
47                 float float_value;
48         } new_value;
49         union {
50                 unsigned int int_value;
51                 float float_value;
52         } prev_value;
53         do {
54                 prev_value.float_value = *source;
55                 new_value.float_value = prev_value.float_value + operand;
56         } while(atomic_cmpxchg((volatile ccl_global unsigned int *)source,
57                                prev_value.int_value,
58                                new_value.int_value) != prev_value.int_value);
59 }
60
61 #endif  /* __KERNEL_OPENCL__ */
62
63 #endif  /* __KERNEL_GPU__ */
64
65 #endif /* __UTIL_ATOMIC_H__ */