Line data Source code
1 : /* Copyright (C) 2010 Wildfire Games.
2 : *
3 : * Permission is hereby granted, free of charge, to any person obtaining
4 : * a copy of this software and associated documentation files (the
5 : * "Software"), to deal in the Software without restriction, including
6 : * without limitation the rights to use, copy, modify, merge, publish,
7 : * distribute, sublicense, and/or sell copies of the Software, and to
8 : * permit persons to whom the Software is furnished to do so, subject to
9 : * the following conditions:
10 : *
11 : * The above copyright notice and this permission notice shall be included
12 : * in all copies or substantial portions of the Software.
13 : *
14 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 : * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 : * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17 : * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 : * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19 : * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20 : * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 : */
22 :
23 : #include "precompiled.h"
24 :
25 : #if ARCH_AMD64
26 :
27 : #include "lib/sysdep/cpu.h"
28 : #include "lib/sysdep/arch/amd64/amd64.h"
29 :
30 :
31 0 : void cpu_ConfigureFloatingPoint()
32 : {
33 : // 64-bit CPUs use SSE2 for all floating-point operations, so we
34 : // don't need to change the FPU control word.
35 0 : }
36 :
37 : #if MSC_VERSION
38 :
39 : // VC 2008 and ICC 12 differ in their declaration of _Interlocked*
40 : #if ICC_VERSION
41 : typedef __int64* P64;
42 : #else
43 : typedef volatile __int64* P64;
44 : #endif
45 :
46 : bool cpu_CAS(volatile intptr_t* location, intptr_t expected, intptr_t newValue)
47 : {
48 : const intptr_t initial = _InterlockedCompareExchange64((P64)location, newValue, expected);
49 : return initial == expected;
50 : }
51 :
52 : bool cpu_CAS64(volatile i64* location, i64 expected, i64 newValue)
53 : {
54 : const i64 initial = _InterlockedCompareExchange64((P64)location, newValue, expected);
55 : return initial == expected;
56 : }
57 :
58 : intptr_t cpu_AtomicAdd(volatile intptr_t* location, intptr_t increment)
59 : {
60 : return _InterlockedExchangeAdd64((P64)location, increment);
61 : }
62 :
63 : #elif OS_MACOSX
64 :
65 : #include <libkern/OSAtomic.h>
66 :
67 : intptr_t cpu_AtomicAdd(volatile intptr_t* location, intptr_t increment)
68 : {
69 : cassert(sizeof(intptr_t) == sizeof(int64_t));
70 : return OSAtomicAdd64Barrier(increment, (volatile int64_t*)location);
71 : }
72 :
73 : bool cpu_CAS(volatile intptr_t* location, intptr_t expected, intptr_t newValue)
74 : {
75 : cassert(sizeof(intptr_t) == sizeof(void*));
76 : return OSAtomicCompareAndSwapPtrBarrier((void*)expected, (void*)newValue, (void* volatile*)location);
77 : }
78 :
79 : bool cpu_CAS64(volatile i64* location, i64 expected, i64 newValue)
80 : {
81 : return OSAtomicCompareAndSwap64Barrier(expected, newValue, location);
82 : }
83 :
84 : #elif GCC_VERSION
85 :
86 0 : intptr_t cpu_AtomicAdd(volatile intptr_t* location, intptr_t increment)
87 : {
88 0 : return __sync_fetch_and_add(location, increment);
89 : }
90 :
91 25415 : bool cpu_CAS(volatile intptr_t* location, intptr_t expected, intptr_t newValue)
92 : {
93 25415 : return __sync_bool_compare_and_swap(location, expected, newValue);
94 : }
95 :
96 0 : bool cpu_CAS64(volatile i64* location, i64 expected, i64 newValue)
97 : {
98 0 : return __sync_bool_compare_and_swap(location, expected, newValue);
99 3 : }
100 :
101 : #endif
102 :
103 : #endif // ARCH_AMD64
|