Update copyright information in README and some header files
[platform/upstream/libatomic_ops.git] / src / atomic_ops / sysdeps / msftc / common32_defs.h
1 /*
2  * Copyright (c) 2003-2011 Hewlett-Packard Development Company, L.P.
3  * Copyright (c) 2009-2018 Ivan Maidanski
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy
6  * of this software and associated documentation files (the "Software"), to deal
7  * in the Software without restriction, including without limitation the rights
8  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9  * copies of the Software, and to permit persons to whom the Software is
10  * furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21  * SOFTWARE.
22  */
23
24 /* This file contains AO primitives based on VC++ built-in intrinsic    */
25 /* functions commonly available across 32-bit architectures.            */
26
27 /* This file should be included from arch-specific header files.        */
28 /* Define AO_USE_INTERLOCKED_INTRINSICS if _Interlocked primitives      */
29 /* (used below) are available as intrinsic ones for a target arch       */
30 /* (otherwise "Interlocked" functions family is used instead).          */
31 /* Define AO_ASSUME_WINDOWS98 if CAS is available.                      */
32
33 #if _MSC_VER <= 1400 || !defined(AO_USE_INTERLOCKED_INTRINSICS) \
34     || defined(_WIN32_WCE)
35 # include <windows.h>
36         /* Seems like over-kill, but that's what MSDN recommends.       */
37         /* And apparently winbase.h is not always self-contained.       */
38 #endif
39
40 #if _MSC_VER < 1310 || !defined(AO_USE_INTERLOCKED_INTRINSICS)
41
42 # define _InterlockedIncrement       InterlockedIncrement
43 # define _InterlockedDecrement       InterlockedDecrement
44 # define _InterlockedExchangeAdd     InterlockedExchangeAdd
45 # define _InterlockedCompareExchange InterlockedCompareExchange
46
47 # define AO_INTERLOCKED_VOLATILE /**/
48
49 #else /* elif _MSC_VER >= 1310 */
50
51 # if _MSC_VER >= 1400
52 #   ifndef _WIN32_WCE
53 #     include <intrin.h>
54 #   endif
55
56 # else /* elif _MSC_VER < 1400 */
57 #  ifdef __cplusplus
58      extern "C" {
59 #  endif
60    LONG __cdecl _InterlockedIncrement(LONG volatile *);
61    LONG __cdecl _InterlockedDecrement(LONG volatile *);
62    LONG __cdecl _InterlockedExchangeAdd(LONG volatile *, LONG);
63    LONG __cdecl _InterlockedCompareExchange(LONG volatile *,
64                                         LONG /* Exchange */, LONG /* Comp */);
65 #  ifdef __cplusplus
66      } /* extern "C" */
67 #  endif
68 # endif /* _MSC_VER < 1400 */
69
70 # if !defined(AO_PREFER_GENERALIZED) || !defined(AO_ASSUME_WINDOWS98)
71 #   pragma intrinsic (_InterlockedIncrement)
72 #   pragma intrinsic (_InterlockedDecrement)
73 #   pragma intrinsic (_InterlockedExchangeAdd)
74 # endif /* !AO_PREFER_GENERALIZED */
75 # pragma intrinsic (_InterlockedCompareExchange)
76
77 # define AO_INTERLOCKED_VOLATILE volatile
78
79 #endif /* _MSC_VER >= 1310 */
80
81 #if !defined(AO_PREFER_GENERALIZED) || !defined(AO_ASSUME_WINDOWS98)
82 AO_INLINE AO_t
83 AO_fetch_and_add_full(volatile AO_t *p, AO_t incr)
84 {
85   return _InterlockedExchangeAdd((long AO_INTERLOCKED_VOLATILE *)p, incr);
86 }
87 #define AO_HAVE_fetch_and_add_full
88
89 AO_INLINE AO_t
90 AO_fetch_and_add1_full(volatile AO_t *p)
91 {
92   return _InterlockedIncrement((long AO_INTERLOCKED_VOLATILE *)p) - 1;
93 }
94 #define AO_HAVE_fetch_and_add1_full
95
96 AO_INLINE AO_t
97 AO_fetch_and_sub1_full(volatile AO_t *p)
98 {
99   return _InterlockedDecrement((long AO_INTERLOCKED_VOLATILE *)p) + 1;
100 }
101 #define AO_HAVE_fetch_and_sub1_full
102 #endif /* !AO_PREFER_GENERALIZED */
103
104 #ifdef AO_ASSUME_WINDOWS98
105   AO_INLINE AO_t
106   AO_fetch_compare_and_swap_full(volatile AO_t *addr, AO_t old_val,
107                                  AO_t new_val)
108   {
109 #   ifdef AO_OLD_STYLE_INTERLOCKED_COMPARE_EXCHANGE
110       return (AO_t)_InterlockedCompareExchange(
111                                         (void *AO_INTERLOCKED_VOLATILE *)addr,
112                                         (void *)new_val, (void *)old_val);
113 #   else
114       return _InterlockedCompareExchange((long AO_INTERLOCKED_VOLATILE *)addr,
115                                          new_val, old_val);
116 #   endif
117   }
118 # define AO_HAVE_fetch_compare_and_swap_full
119 #endif /* AO_ASSUME_WINDOWS98 */
120
121 #if (_MSC_VER > 1400) && (!defined(_M_ARM) || _MSC_VER >= 1800)
122
123 # pragma intrinsic (_InterlockedAnd8)
124 # pragma intrinsic (_InterlockedCompareExchange16)
125 # pragma intrinsic (_InterlockedOr8)
126 # pragma intrinsic (_InterlockedXor8)
127
128   AO_INLINE void
129   AO_char_and_full(volatile unsigned char *p, unsigned char value)
130   {
131     _InterlockedAnd8((char volatile *)p, value);
132   }
133 # define AO_HAVE_char_and_full
134
135   AO_INLINE void
136   AO_char_or_full(volatile unsigned char *p, unsigned char value)
137   {
138     _InterlockedOr8((char volatile *)p, value);
139   }
140 # define AO_HAVE_char_or_full
141
142   AO_INLINE void
143   AO_char_xor_full(volatile unsigned char *p, unsigned char value)
144   {
145     _InterlockedXor8((char volatile *)p, value);
146   }
147 # define AO_HAVE_char_xor_full
148
149   AO_INLINE unsigned short
150   AO_short_fetch_compare_and_swap_full(volatile unsigned short *addr,
151                                        unsigned short old_val,
152                                        unsigned short new_val)
153   {
154     return _InterlockedCompareExchange16((short volatile *)addr,
155                                          new_val, old_val);
156   }
157 # define AO_HAVE_short_fetch_compare_and_swap_full
158
159 # ifndef AO_PREFER_GENERALIZED
160 #   pragma intrinsic (_InterlockedIncrement16)
161 #   pragma intrinsic (_InterlockedDecrement16)
162
163     AO_INLINE unsigned short
164     AO_short_fetch_and_add1_full(volatile unsigned short *p)
165     {
166       return _InterlockedIncrement16((short volatile *)p) - 1;
167     }
168 #   define AO_HAVE_short_fetch_and_add1_full
169
170     AO_INLINE unsigned short
171     AO_short_fetch_and_sub1_full(volatile unsigned short *p)
172     {
173       return _InterlockedDecrement16((short volatile *)p) + 1;
174     }
175 #   define AO_HAVE_short_fetch_and_sub1_full
176 # endif /* !AO_PREFER_GENERALIZED */
177 #endif /* _MSC_VER > 1400 */
178
179 #if _MSC_VER >= 1800 /* Visual Studio 2013+ */
180
181 # pragma intrinsic (_InterlockedCompareExchange8)
182
183   AO_INLINE unsigned char
184   AO_char_fetch_compare_and_swap_full(volatile unsigned char *addr,
185                                       unsigned char old_val,
186                                       unsigned char new_val)
187   {
188     return _InterlockedCompareExchange8((char volatile *)addr,
189                                         new_val, old_val);
190   }
191 # define AO_HAVE_char_fetch_compare_and_swap_full
192
193 # if !defined(AO_PREFER_GENERALIZED) && !defined(_M_ARM)
194 #   pragma intrinsic (_InterlockedExchangeAdd16)
195 #   pragma intrinsic (_InterlockedExchangeAdd8)
196
197     AO_INLINE unsigned char
198     AO_char_fetch_and_add_full(volatile unsigned char *p, unsigned char incr)
199     {
200       return _InterlockedExchangeAdd8((char volatile *)p, incr);
201     }
202 #   define AO_HAVE_char_fetch_and_add_full
203
204     AO_INLINE unsigned short
205     AO_short_fetch_and_add_full(volatile unsigned short *p,
206                                 unsigned short incr)
207     {
208       return _InterlockedExchangeAdd16((short volatile *)p, incr);
209     }
210 #   define AO_HAVE_short_fetch_and_add_full
211 # endif /* !AO_PREFER_GENERALIZED && !_M_ARM */
212 #endif /* _MSC_VER >= 1800 */