2 Compatibility <intrin.h> header for GCC -- GCC equivalents of intrinsic
3 Microsoft Visual C++ functions. Originally developed for the ReactOS
4 (<http://www.reactos.org/>) and TinyKrnl (<http://www.tinykrnl.org/>)
7 Copyright (c) 2006 KJK::Hyperion <hackbunny@reactos.com>
9 Permission is hereby granted, free of charge, to any person obtaining a
10 copy of this software and associated documentation files (the "Software"),
11 to deal in the Software without restriction, including without limitation
12 the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 and/or sell copies of the Software, and to permit persons to whom the
14 Software is furnished to do so, subject to the following conditions:
16 The above copyright notice and this permission notice shall be included in
17 all copies or substantial portions of the Software.
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 DEALINGS IN THE SOFTWARE.
28 #ifndef KJK_INTRIN_ARM_H_
29 #define KJK_INTRIN_ARM_H_
32 #error Unsupported compiler
35 #define _ReadWriteBarrier() __sync_synchronize()
37 __INTRIN_INLINE
long _InterlockedCompareExchange(volatile long * const dest
, const long exch
, const long comp
)
41 __asm__
__volatile__ ( "0:\n\t"
45 "swp %0, %3, [%2]\n\t"
47 "swpne %3, %0, [%2]\n\t"
50 : "=&r" (a
), "=&r" (b
)
51 : "r" (dest
), "r" (exch
), "r" (comp
)
57 __INTRIN_INLINE
long long _InterlockedCompareExchange64(volatile long long * const dest
, const long long exch
, const long long comp
)
64 if (*dest
== comp
) *dest
= exch
;
68 __INTRIN_INLINE
void * _InterlockedCompareExchangePointer(void * volatile * const Destination
, void * const Exchange
, void * const Comperand
)
70 return (void*)_InterlockedCompareExchange((volatile long* const)Destination
, (const long)Exchange
, (const long)Comperand
);
74 __INTRIN_INLINE
long _InterlockedExchangeAdd(volatile long * const dest
, const long add
)
78 __asm__
__volatile__ ( "0:\n\t"
81 "swp %2, %1, [%3]\n\t"
83 "swpne %1, %2, [%3]\n\t"
85 : "=&r" (a
), "=&r" (b
), "=&r" (c
)
86 : "r" (dest
), "r" (add
)
92 __INTRIN_INLINE
long _InterlockedExchange(volatile long * const dest
, const long exch
)
96 __asm__
__volatile__ ( "swp %0, %2, [%1]"
98 : "r" (dest
), "r" (exch
));
104 __INTRIN_INLINE
void * _InterlockedExchangePointer(void * volatile * const Target
, void * const Value
)
106 return _InterlockedExchange(Target
, Value
);
109 __INTRIN_INLINE
char _InterlockedAnd8(volatile char * const value
, const char mask
)
119 y
= _InterlockedCompareExchange8(value
, x
& mask
, x
);
126 __INTRIN_INLINE
short _InterlockedAnd16(volatile short * const value
, const short mask
)
136 y
= _InterlockedCompareExchange16(value
, x
& mask
, x
);
143 __INTRIN_INLINE
long _InterlockedAnd(volatile long * const value
, const long mask
)
153 y
= _InterlockedCompareExchange(value
, x
& mask
, x
);
160 __INTRIN_INLINE
char _InterlockedOr8(volatile char * const value
, const char mask
)
170 y
= _InterlockedCompareExchange8(value
, x
| mask
, x
);
177 __INTRIN_INLINE
short _InterlockedOr16(volatile short * const value
, const short mask
)
187 y
= _InterlockedCompareExchange16(value
, x
| mask
, x
);
194 __INTRIN_INLINE
long _InterlockedOr(volatile long * const value
, const long mask
)
204 y
= _InterlockedCompareExchange(value
, x
| mask
, x
);
211 __INTRIN_INLINE
char _InterlockedXor8(volatile char * const value
, const char mask
)
221 y
= _InterlockedCompareExchange8(value
, x
^ mask
, x
);
228 __INTRIN_INLINE
short _InterlockedXor16(volatile short * const value
, const short mask
)
238 y
= _InterlockedCompareExchange16(value
, x
^ mask
, x
);
245 __INTRIN_INLINE
long _InterlockedXor(volatile long * const value
, const long mask
)
255 y
= _InterlockedCompareExchange(value
, x
^ mask
, x
);
262 __INTRIN_INLINE
long _InterlockedDecrement(volatile long * const lpAddend
)
264 return _InterlockedExchangeAdd(lpAddend
, -1) - 1;
267 __INTRIN_INLINE
long _InterlockedIncrement(volatile long * const lpAddend
)
269 return _InterlockedExchangeAdd(lpAddend
, 1) + 1;
272 __INTRIN_INLINE
long _InterlockedDecrement16(volatile short * const lpAddend
)
274 return _InterlockedExchangeAdd16(lpAddend
, -1) - 1;
277 __INTRIN_INLINE
long _InterlockedIncrement16(volatile short * const lpAddend
)
279 return _InterlockedExchangeAdd16(lpAddend
, 1) + 1;
282 __INTRIN_INLINE
void _disable(void)
292 __INTRIN_INLINE
void _enable(void)
303 __INTRIN_INLINE
unsigned long _rotl(const unsigned long value
, const unsigned char shift
)
305 return (((value
) << ((int)(shift
))) | ((value
) >> (32 - (int)(shift
))));
310 ({ ULONG __value, __arg = (a); \
311 asm ("clz\t%0, %1": "=r" (__value): "r" (__arg)); \