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 _ReturnAddress() (__builtin_return_address(0))
36 #define _ReadWriteBarrier() __sync_synchronize()
38 __INTRIN_INLINE
unsigned short _byteswap_ushort(unsigned short value
)
40 return (value
>> 8) || (value
<< 8);
43 __INTRIN_INLINE
unsigned _CountLeadingZeros(long Mask
)
45 return Mask
? __builtin_clz(Mask
) : 32;
48 __INTRIN_INLINE
unsigned _CountTrailingZeros(long Mask
)
50 return Mask
? __builtin_ctz(Mask
) : 32;
53 __INTRIN_INLINE
unsigned char _BitScanForward(unsigned long * const Index
, const unsigned long Mask
)
55 *Index
= __builtin_ctz(Mask
);
59 __INTRIN_INLINE
char _InterlockedCompareExchange8(volatile char * const Destination
, const char Exchange
, const char Comperand
)
61 return __sync_val_compare_and_swap(Destination
, Comperand
, Exchange
);
64 __INTRIN_INLINE
short _InterlockedCompareExchange16(volatile short * const Destination
, const short Exchange
, const short Comperand
)
66 return __sync_val_compare_and_swap(Destination
, Comperand
, Exchange
);
69 __INTRIN_INLINE
long _InterlockedExchangeAdd16(volatile short * const Addend
, const short Value
)
71 return __sync_fetch_and_add(Addend
, Value
);
74 __INTRIN_INLINE
long _InterlockedCompareExchange(volatile long * const dest
, const long exch
, const long comp
)
78 __asm__
__volatile__ ( "0:\n\t"
82 "swp %0, %3, [%2]\n\t"
84 "swpne %3, %0, [%2]\n\t"
87 : "=&r" (a
), "=&r" (b
)
88 : "r" (dest
), "r" (exch
), "r" (comp
)
94 __INTRIN_INLINE
long long _InterlockedCompareExchange64(volatile long long * const dest
, const long long exch
, const long long comp
)
101 if (*dest
== comp
) *dest
= exch
;
105 __INTRIN_INLINE
void * _InterlockedCompareExchangePointer(void * volatile * const Destination
, void * const Exchange
, void * const Comperand
)
107 return (void*)_InterlockedCompareExchange((volatile long* const)Destination
, (const long)Exchange
, (const long)Comperand
);
111 __INTRIN_INLINE
long _InterlockedExchangeAdd(volatile long * const dest
, const long add
)
115 __asm__
__volatile__ ( "0:\n\t"
118 "swp %2, %1, [%3]\n\t"
120 "swpne %1, %2, [%3]\n\t"
122 : "=&r" (a
), "=&r" (b
), "=&r" (c
)
123 : "r" (dest
), "r" (add
)
129 __INTRIN_INLINE
long _InterlockedExchange(volatile long * const dest
, const long exch
)
133 __asm__
__volatile__ ( "swp %0, %2, [%1]"
135 : "r" (dest
), "r" (exch
));
141 __INTRIN_INLINE
void * _InterlockedExchangePointer(void * volatile * const Target
, void * const Value
)
143 return (void *)_InterlockedExchange((volatile long * const)Target
, (const long)Value
);
148 __INTRIN_INLINE
unsigned char _BitScanReverse(unsigned long * const Index
, const unsigned long Mask
)
150 *Index
= 31 - __builtin_clz(Mask
);
154 __INTRIN_INLINE
char _InterlockedAnd8(volatile char * const value
, const char mask
)
164 y
= _InterlockedCompareExchange8(value
, x
& mask
, x
);
171 __INTRIN_INLINE
short _InterlockedAnd16(volatile short * const value
, const short mask
)
181 y
= _InterlockedCompareExchange16(value
, x
& mask
, x
);
188 __INTRIN_INLINE
long _InterlockedAnd(volatile long * const value
, const long mask
)
198 y
= _InterlockedCompareExchange(value
, x
& mask
, x
);
205 __INTRIN_INLINE
char _InterlockedOr8(volatile char * const value
, const char mask
)
215 y
= _InterlockedCompareExchange8(value
, x
| mask
, x
);
222 __INTRIN_INLINE
short _InterlockedOr16(volatile short * const value
, const short mask
)
232 y
= _InterlockedCompareExchange16(value
, x
| mask
, x
);
239 __INTRIN_INLINE
long _InterlockedOr(volatile long * const value
, const long mask
)
249 y
= _InterlockedCompareExchange(value
, x
| mask
, x
);
256 __INTRIN_INLINE
char _InterlockedXor8(volatile char * const value
, const char mask
)
266 y
= _InterlockedCompareExchange8(value
, x
^ mask
, x
);
273 __INTRIN_INLINE
short _InterlockedXor16(volatile short * const value
, const short mask
)
283 y
= _InterlockedCompareExchange16(value
, x
^ mask
, x
);
290 __INTRIN_INLINE
long _InterlockedXor(volatile long * const value
, const long mask
)
300 y
= _InterlockedCompareExchange(value
, x
^ mask
, x
);
307 __INTRIN_INLINE
long _InterlockedDecrement(volatile long * const lpAddend
)
309 return _InterlockedExchangeAdd(lpAddend
, -1) - 1;
312 __INTRIN_INLINE
long _InterlockedIncrement(volatile long * const lpAddend
)
314 return _InterlockedExchangeAdd(lpAddend
, 1) + 1;
317 __INTRIN_INLINE
long _InterlockedDecrement16(volatile short * const lpAddend
)
319 return _InterlockedExchangeAdd16(lpAddend
, -1) - 1;
322 __INTRIN_INLINE
long _InterlockedIncrement16(volatile short * const lpAddend
)
324 return _InterlockedExchangeAdd16(lpAddend
, 1) + 1;
327 __INTRIN_INLINE
long _InterlockedAddLargeStatistic(volatile long long * const Addend
, const long Value
)
333 __INTRIN_INLINE
void _disable(void)
337 "cpsid i @ __cli" : : : "memory", "cc"
341 __INTRIN_INLINE
void _enable(void)
345 "cpsie i @ __sti" : : : "memory", "cc"
349 __INTRIN_INLINE
unsigned char _interlockedbittestandset(volatile long * a
, const long b
)
351 return (_InterlockedOr(a
, 1 << b
) >> b
) & 1;
354 __INTRIN_INLINE
unsigned char _interlockedbittestandreset(volatile long * a
, const long b
)
356 return (_InterlockedAnd(a
, ~(1 << b
)) >> b
) & 1;
360 __INTRIN_INLINE
unsigned int _rotl(const unsigned int value
, int shift
)
362 return (((value
) << ((int)(shift
))) | ((value
) >> (32 - (int)(shift
))));
367 ({ ULONG __value, __arg = (a); \
368 asm ("clz\t%0, %1": "=r" (__value): "r" (__arg)); \