boehm-gc: revert all CACAO-specific modifications; this is now an exact copy of the...
[cacao.git] / src / mm / boehm-gc / libatomic_ops-1.2 / src / atomic_ops / sysdeps / msftc / x86_64.h
1 /*
2  * Copyright (c) 2003 Hewlett-Packard Development Company, L.P.
3  * 
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  * 
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  * 
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20  * SOFTWARE. 
21  */
22
23 /* The following really assume we have a 486 or better. */
24 /* If ASSUME_WINDOWS98 is defined, we assume Windows 98 or newer.       */
25
26 #include "../all_aligned_atomic_load_store.h"
27
28 /* Real X86 implementations, except for some old WinChips, appear       */
29 /* to enforce ordering between memory operations, EXCEPT that a later   */
30 /* read can pass earlier writes, presumably due to the visible          */
31 /* presence of store buffers.                                           */
32 /* We ignore both the WinChips, and the fact that the official specs    */
33 /* seem to be much weaker (and arguably too weak to be usable).         */
34
35 #include "../ordered_except_wr.h"
36
37 #if 0
38 FIXME: Need to reimplement testandset
39
40 #include "../test_and_set_t_is_char.h"
41
42 #else
43
44 #include "../test_and_set_t_is_ao_t.h"
45
46 #endif
47
48 #include <windows.h>
49         /* Seems like over-kill, but that's what MSDN recommends.       */
50         /* And apparently winbase.h is not always self-contained.       */
51
52
53 #include <intrin.h>
54
55 #pragma intrinsic (_ReadWriteBarrier)
56
57 #ifdef __cplusplus
58 extern "C" {
59 #endif
60
61 LONGLONG __cdecl _InterlockedIncrement64(LONGLONG volatile *Addend);
62 LONGLONG __cdecl _InterlockedDecrement64(LONGLONG volatile *Addend);
63 LONGLONG __cdecl _InterlockedExchangeAdd64(LONGLONG volatile* Target,
64                                            LONGLONG Addend);
65 LONGLONG __cdecl _InterlockedExchange64(LONGLONG volatile* Target,
66                                         LONGLONG Value);
67 LONGLONG __cdecl _InterlockedCompareExchange64(LONGLONG volatile* Dest,
68                                                LONGLONG Exchange,
69                                                LONGLONG Comp);
70
71 #ifdef __cplusplus
72 }
73 #endif
74
75 #pragma intrinsic (_InterlockedIncrement64)
76 #pragma intrinsic (_InterlockedDecrement64)
77 #pragma intrinsic (_InterlockedExchange64)
78 #pragma intrinsic (_InterlockedExchangeAdd64)
79 #pragma intrinsic (_InterlockedCompareExchange64)
80
81 /* As far as we can tell, the lfence and sfence instructions are not    */
82 /* currently needed or useful for cached memory accesses.               */
83
84 /* Unfortunately mfence doesn't exist everywhere.               */
85 /* IsProcessorFeaturePresent(PF_COMPARE_EXCHANGE128) is         */
86 /* probably a conservative test for it?                         */
87
88 #if defined(AO_USE_PENTIUM4_INSTRS)
89
90 AO_INLINE void
91 AO_nop_full()
92 {
93   __asm { mfence }
94 }
95
96 #define AO_HAVE_nop_full
97
98 #else
99
100 /* We could use the cpuid instruction.  But that seems to be slower     */
101 /* than the default implementation based on test_and_set_full.  Thus    */
102 /* we omit that bit of misinformation here.                             */
103
104 #endif
105
106 AO_INLINE AO_t
107 AO_fetch_and_add_full (volatile AO_t *p, AO_t incr)
108 {
109   return _InterlockedExchangeAdd64((LONGLONG volatile *)p, (LONGLONG)incr);
110 }
111
112 #define AO_HAVE_fetch_and_add_full
113
114 AO_INLINE AO_t
115 AO_fetch_and_add1_full (volatile AO_t *p)
116 {
117   return _InterlockedIncrement64((LONGLONG volatile *)p) - 1;
118 }
119
120 #define AO_HAVE_fetch_and_add1_full
121
122 AO_INLINE AO_t
123 AO_fetch_and_sub1_full (volatile AO_t *p)
124 {
125   return _InterlockedDecrement64((LONGLONG volatile *)p) + 1;
126 }
127
128 #define AO_HAVE_fetch_and_sub1_full
129
130 AO_INLINE int
131 AO_compare_and_swap_full(volatile AO_t *addr,
132                          AO_t old, AO_t new_val) 
133 {
134     return _InterlockedCompareExchange64((LONGLONG volatile *)addr,
135                                          (LONGLONG)new_val, (LONGLONG)old)
136            == (LONGLONG)old;
137 }
138
139 #define AO_HAVE_compare_and_swap_full
140
141 #if 0
142 FIXME: (__asm not supported)
143 AO_INLINE AO_TS_VAL_t
144 AO_test_and_set_full(volatile AO_TS_t *addr)
145 {
146     __asm
147     {
148         mov     eax,AO_TS_SET           ;
149         mov     ebx,addr                ;
150         xchg    byte ptr [ebx],al       ;
151     }
152 }
153
154 #define AO_HAVE_test_and_set_full
155
156 FIXME: (__asm not supported)
157 NEC LE-IT: Don't have a working Win64 environment here at the moment.
158 AO_compare_double_and_swap_double_full needs implementation for Win64
159 But there is no _InterlockedCompareExchange128 in the WinAPI, so we
160 need basically whats given below.
161 Also see gcc/x86_64.h for partial old opteron workaround:
162
163 #ifndef AO_CASDOUBLE_MISSING
164
165 AO_INLINE int
166 AO_compare_double_and_swap_double_full(volatile AO_double_t *addr,
167                                        AO_t old_val1, AO_t old_val2,
168                                        AO_t new_val1, AO_t new_val2)
169 {
170         char result;
171         __asm
172         {
173                 mov     rdx,QWORD PTR [old_val]
174                 mov     rax,QWORD PTR [old_val + 8]
175                 mov     rcx,QWORD PTR [new_val]
176                 mov     rbx,QWORD PTR [new_val + 8]
177                 lock cmpxchg16b [addr]
178                 setz result;
179         }
180         return result;
181 }
182 #endif // AO_CASDOUBLE_MISSING
183 #define AO_HAVE_compare_double_and_swap_double_full
184
185 #endif /* 0 */
186