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 / sunc / x86.h
1 /* 
2  * Copyright (c) 1991-1994 by Xerox Corporation.  All rights reserved.
3  * Copyright (c) 1996-1999 by Silicon Graphics.  All rights reserved.
4  * Copyright (c) 1999-2003 by Hewlett-Packard Company. All rights reserved.
5  *
6  *
7  * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
8  * OR IMPLIED.  ANY USE IS AT YOUR OWN RISK.
9  *
10  * Permission is hereby granted to use or copy this program
11  * for any purpose,  provided the above notices are retained on all copies.
12  * Permission to modify the code and to distribute modified code is granted,
13  * provided the above notices are retained, and a notice that the code was
14  * modified is included with the above copyright notice.
15  *
16  * Some of the machine specific code was borrowed from our GC distribution.
17  */
18
19 /* The following really assume we have a 486 or better.                 */
20
21 #include "../all_aligned_atomic_load_store.h"
22
23 /* Real X86 implementations, except for some old WinChips, appear       */
24 /* to enforce ordering between memory operations, EXCEPT that a later   */
25 /* read can pass earlier writes, presumably due to the visible          */
26 /* presence of store buffers.                                           */
27 /* We ignore both the WinChips, and the fact that the official specs    */
28 /* seem to be much weaker (and arguably too weak to be usable).         */
29
30 #include "../ordered_except_wr.h"
31
32 #include "../test_and_set_t_is_char.h"
33
34 #include "../standard_ao_double_t.h"
35
36 #if defined(AO_USE_PENTIUM4_INSTRS)
37 AO_INLINE void
38 AO_nop_full(void)
39 {
40   __asm__ __volatile__("mfence" : : : "memory");
41 }
42
43 #define AO_HAVE_nop_full
44
45 #else
46
47 /* We could use the cpuid instruction.  But that seems to be slower     */
48 /* than the default implementation based on test_and_set_full.  Thus    */
49 /* we omit that bit of misinformation here.                             */
50
51 #endif
52
53 /* As far as we can tell, the lfence and sfence instructions are not    */
54 /* currently needed or useful for cached memory accesses.               */
55
56 /* Really only works for 486 and later */
57 AO_INLINE AO_t
58 AO_fetch_and_add_full (volatile AO_t *p, AO_t incr)
59 {
60   AO_t result;
61
62   __asm__ __volatile__ ("lock; xaddl %0, %1" :
63                         "=r" (result), "=m" (*p) : "0" (incr) /* , "m" (*p) */
64                         : "memory");
65   return result;
66 }
67
68 #define AO_HAVE_fetch_and_add_full
69
70 AO_INLINE unsigned char
71 AO_char_fetch_and_add_full (volatile unsigned char *p, unsigned char incr)
72 {
73   unsigned char result;
74
75   __asm__ __volatile__ ("lock; xaddb %0, %1" :
76                         "=q" (result), "=m" (*p) : "0" (incr) /* , "m" (*p) */
77                         : "memory");
78   return result;
79 }
80
81 #define AO_HAVE_char_fetch_and_add_full
82
83 AO_INLINE unsigned short
84 AO_short_fetch_and_add_full (volatile unsigned short *p, unsigned short incr)
85 {
86   unsigned short result;
87
88   __asm__ __volatile__ ("lock; xaddw %0, %1" :
89                         "=r" (result), "=m" (*p) : "0" (incr) /* , "m" (*p) */
90                         : "memory");
91   return result;
92 }
93
94 #define AO_HAVE_short_fetch_and_add_full
95
96 /* Really only works for 486 and later */
97 AO_INLINE void
98 AO_or_full (volatile AO_t *p, AO_t incr)
99 {
100   __asm__ __volatile__ ("lock; orl %1, %0" :
101                         "=m" (*p) : "r" (incr) /* , "m" (*p) */
102                         : "memory");
103 }
104
105 #define AO_HAVE_or_full
106
107 AO_INLINE AO_TS_VAL_t
108 AO_test_and_set_full(volatile AO_TS_t *addr)
109 {
110   AO_TS_t oldval;
111   /* Note: the "xchg" instruction does not need a "lock" prefix */
112   /* Note 2: "xchgb" is not recognized by Sun CC assembler yet. */
113   __asm__ __volatile__("xchgl %0, %1"
114                 : "=q"(oldval), "=m"(*addr)
115                 : "0"(0xff) /* , "m"(*addr) */
116                 : "memory");
117   return (AO_TS_VAL_t)oldval;
118 }
119
120 #define AO_HAVE_test_and_set_full
121
122 /* Returns nonzero if the comparison succeeded. */
123 AO_INLINE int
124 AO_compare_and_swap_full(volatile AO_t *addr,
125                              AO_t old, AO_t new_val) 
126 {
127   char result;
128   __asm__ __volatile__("lock; cmpxchgl %2, %0; setz %1"
129                        : "=m"(*addr), "=q"(result)
130                        : "r" (new_val), "a"(old) : "memory");
131   return (int) result;
132 }
133
134 #define AO_HAVE_compare_and_swap_full
135
136 /* Returns nonzero if the comparison succeeded. */
137 /* Really requires at least a Pentium.          */
138 AO_INLINE int
139 AO_compare_double_and_swap_double_full(volatile AO_double_t *addr,
140                                        AO_t old_val1, AO_t old_val2,
141                                        AO_t new_val1, AO_t new_val2) 
142 {
143   char result;
144   /* FIXME: not tested */
145   #if __PIC__
146   /* If PIC is turned on, we can't use %ebx as it is reserved for the
147      GOT pointer.  We can save and restore %ebx because GCC won't be
148      using it for anything else (such as any of the m operands) */
149   __asm__ __volatile__("pushl %%ebx;"   /* save ebx used for PIC GOT ptr */
150                        "movl %6,%%ebx;" /* move new_val2 to %ebx */
151                        "lock; cmpxchg8b %0; setz %1;"
152                        "pop %%ebx;"     /* restore %ebx */
153                        : "=m"(*addr), "=q"(result)
154                        : "m"(*addr), "d" (old_val2), "a" (old_val1),
155                          "c" (new_val2), "m" (new_val1) : "memory");
156   #else
157   /* We can't just do the same thing in non-PIC mode, because GCC
158    * might be using %ebx as the memory operand.  We could have ifdef'd
159    * in a clobber, but there's no point doing the push/pop if we don't
160    * have to. */
161   __asm__ __volatile__("lock; cmpxchg8b %0; setz %1;"
162                        : "=m"(*addr), "=q"(result)
163                        : /* "m"(*addr), */ "d" (old_val2), "a" (old_val1),
164                          "c" (new_val2), "b" (new_val1) : "memory");
165   #endif
166   return (int) result;
167 }
168
169 #define AO_HAVE_compare_double_and_swap_double_full
170
171 #include "../ao_t_is_int.h"