1 //
2 // Copyright (c) 2017 The Khronos Group Inc.
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 #include "host_atomics.h"
17
host_atomic_thread_fence(TExplicitMemoryOrderType order)18 void host_atomic_thread_fence(TExplicitMemoryOrderType order)
19 {
20 if(order != MEMORY_ORDER_RELAXED) {
21 #if defined( _MSC_VER ) || (defined( __INTEL_COMPILER ) && defined(WIN32))
22 MemoryBarrier();
23 #elif defined(__GNUC__)
24 __sync_synchronize();
25 #else
26 log_info("Host function not implemented: host_atomic_thread_fence\n");
27 #endif
28 }
29 }
30
31 template <>
host_atomic_exchange(volatile HOST_ATOMIC_FLOAT * a,HOST_FLOAT c,TExplicitMemoryOrderType order)32 HOST_FLOAT host_atomic_exchange(volatile HOST_ATOMIC_FLOAT* a, HOST_FLOAT c, TExplicitMemoryOrderType order)
33 {
34 HOST_UINT tmp = host_atomic_exchange((volatile HOST_ATOMIC_UINT*)a, *(HOST_UINT*)&c, order);
35 return *(float*)&tmp;
36 }
37 template <>
host_atomic_exchange(volatile HOST_ATOMIC_DOUBLE * a,HOST_DOUBLE c,TExplicitMemoryOrderType order)38 HOST_DOUBLE host_atomic_exchange(volatile HOST_ATOMIC_DOUBLE* a, HOST_DOUBLE c, TExplicitMemoryOrderType order)
39 {
40 HOST_ULONG tmp = host_atomic_exchange((volatile HOST_ATOMIC_ULONG*)a, *(HOST_ULONG*)&c, order);
41 return *(double*)&tmp;
42 }
43
44 template <>
host_atomic_load(volatile HOST_ATOMIC_FLOAT * a,TExplicitMemoryOrderType order)45 HOST_FLOAT host_atomic_load(volatile HOST_ATOMIC_FLOAT* a, TExplicitMemoryOrderType order)
46 {
47 HOST_UINT tmp = host_atomic_load<HOST_ATOMIC_UINT, HOST_UINT>((volatile HOST_ATOMIC_UINT*)a, order);
48 return *(float*)&tmp;
49 }
50 template <>
host_atomic_load(volatile HOST_ATOMIC_DOUBLE * a,TExplicitMemoryOrderType order)51 HOST_DOUBLE host_atomic_load(volatile HOST_ATOMIC_DOUBLE* a, TExplicitMemoryOrderType order)
52 {
53 HOST_ULONG tmp = host_atomic_load<HOST_ATOMIC_ULONG, HOST_ULONG>((volatile HOST_ATOMIC_ULONG*)a, order);
54 return *(double*)&tmp;
55 }
56
host_atomic_flag_test_and_set(volatile HOST_ATOMIC_FLAG * a,TExplicitMemoryOrderType order)57 bool host_atomic_flag_test_and_set(volatile HOST_ATOMIC_FLAG *a, TExplicitMemoryOrderType order)
58 {
59 HOST_FLAG old = host_atomic_exchange(a, 1, order);
60 return old != 0;
61 }
62
host_atomic_flag_clear(volatile HOST_ATOMIC_FLAG * a,TExplicitMemoryOrderType order)63 void host_atomic_flag_clear(volatile HOST_ATOMIC_FLAG *a, TExplicitMemoryOrderType order)
64 {
65 host_atomic_store(a, 0, order);
66 }
67