1 /* 2 Compatibility <intrin.h> header for GCC -- GCC equivalents of intrinsic 3 Microsoft Visual C++ functions. Originally developed for the ReactOS 4 (<https://reactos.org/>) and TinyKrnl (<http://www.tinykrnl.org/>) 5 projects. 6 7 Copyright (c) 2006 KJK::Hyperion <hackbunny@reactos.com> 8 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: 15 16 The above copyright notice and this permission notice shall be included in 17 all copies or substantial portions of the Software. 18 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. 26 */ 27 28 #ifndef KJK_INTRIN_ARM_H_ 29 #define KJK_INTRIN_ARM_H_ 30 31 #ifndef __GNUC__ 32 #error Unsupported compiler 33 #endif 34 35 #define _ReturnAddress() (__builtin_return_address(0)) 36 #define _ReadWriteBarrier() __sync_synchronize() 37 38 __INTRIN_INLINE void __yield(void) { __asm__ __volatile__("yield"); } 39 40 __INTRIN_INLINE void __break(unsigned int value) { __asm__ __volatile__("bkpt %0": : "M" (value)); } 41 42 __INTRIN_INLINE unsigned short _byteswap_ushort(unsigned short value) 43 { 44 return (value >> 8) | (value << 8); 45 } 46 47 __INTRIN_INLINE unsigned _CountLeadingZeros(long Mask) 48 { 49 return Mask ? __builtin_clz(Mask) : 32; 50 } 51 52 __INTRIN_INLINE unsigned _CountTrailingZeros(long Mask) 53 { 54 return Mask ? __builtin_ctz(Mask) : 32; 55 } 56 57 __INTRIN_INLINE unsigned char _BitScanForward(unsigned long * const Index, const unsigned long Mask) 58 { 59 *Index = __builtin_ctz(Mask); 60 return Mask ? 1 : 0; 61 } 62 63 __INTRIN_INLINE char _InterlockedCompareExchange8(volatile char * const Destination, const char Exchange, const char Comperand) 64 { 65 return __sync_val_compare_and_swap(Destination, Comperand, Exchange); 66 } 67 68 __INTRIN_INLINE short _InterlockedCompareExchange16(volatile short * const Destination, const short Exchange, const short Comperand) 69 { 70 short a, b; 71 72 __asm__ __volatile__ ( "0:\n\t" 73 "ldr %1, [%2]\n\t" 74 "cmp %1, %4\n\t" 75 "bne 1f\n\t" 76 "swp %0, %3, [%2]\n\t" 77 "cmp %0, %1\n\t" 78 "swpne %3, %0, [%2]\n\t" 79 "bne 0b\n\t" 80 "1:" 81 : "=&r" (a), "=&r" (b) 82 : "r" (Destination), "r" (Exchange), "r" (Comperand) 83 : "cc", "memory"); 84 85 return a; 86 } 87 88 __INTRIN_INLINE short _InterlockedExchangeAdd16(volatile short * const Addend, const short Value) 89 { 90 short a, b, c; 91 92 __asm__ __volatile__ ( "0:\n\t" 93 "ldr %0, [%3]\n\t" 94 "add %1, %0, %4\n\t" 95 "swp %2, %1, [%3]\n\t" 96 "cmp %0, %2\n\t" 97 "swpne %1, %2, [%3]\n\t" 98 "bne 0b" 99 : "=&r" (a), "=&r" (b), "=&r" (c) 100 : "r" (Value), "r" (Addend) 101 : "cc", "memory"); 102 103 return a; 104 } 105 106 __INTRIN_INLINE long _InterlockedCompareExchange(volatile long * const dest, const long exch, const long comp) 107 { 108 long a, b; 109 110 __asm__ __volatile__ ( "0:\n\t" 111 "ldr %1, [%2]\n\t" 112 "cmp %1, %4\n\t" 113 "bne 1f\n\t" 114 "swp %0, %3, [%2]\n\t" 115 "cmp %0, %1\n\t" 116 "swpne %3, %0, [%2]\n\t" 117 "bne 0b\n\t" 118 "1:" 119 : "=&r" (a), "=&r" (b) 120 : "r" (dest), "r" (exch), "r" (comp) 121 : "cc", "memory"); 122 123 return a; 124 } 125 126 __INTRIN_INLINE long long _InterlockedCompareExchange64(volatile long long * const dest, const long long exch, const long long comp) 127 { 128 // 129 // FIXME 130 // 131 long long result; 132 result = *dest; 133 if (*dest == comp) *dest = exch; 134 return result; 135 } 136 137 __INTRIN_INLINE void * _InterlockedCompareExchangePointer(void * volatile * const Destination, void * const Exchange, void * const Comperand) 138 { 139 return (void*)_InterlockedCompareExchange((volatile long* const)Destination, (const long)Exchange, (const long)Comperand); 140 } 141 142 143 __INTRIN_INLINE long _InterlockedExchangeAdd(volatile long * const dest, const long add) 144 { 145 long a, b, c; 146 147 __asm__ __volatile__ ( "0:\n\t" 148 "ldr %0, [%3]\n\t" 149 "add %1, %0, %4\n\t" 150 "swp %2, %1, [%3]\n\t" 151 "cmp %0, %2\n\t" 152 "swpne %1, %2, [%3]\n\t" 153 "bne 0b" 154 : "=&r" (a), "=&r" (b), "=&r" (c) 155 : "r" (dest), "r" (add) 156 : "cc", "memory"); 157 158 return a; 159 } 160 161 __INTRIN_INLINE long _InterlockedExchange(volatile long * const dest, const long exch) 162 { 163 long a; 164 165 __asm__ __volatile__ ( "swp %0, %2, [%1]" 166 : "=&r" (a) 167 : "r" (dest), "r" (exch)); 168 169 return a; 170 } 171 172 173 __INTRIN_INLINE void * _InterlockedExchangePointer(void * volatile * const Target, void * const Value) 174 { 175 return (void *)_InterlockedExchange((volatile long * const)Target, (const long)Value); 176 } 177 178 179 180 __INTRIN_INLINE unsigned char _BitScanReverse(unsigned long * const Index, const unsigned long Mask) 181 { 182 *Index = 31 - __builtin_clz(Mask); 183 return Mask ? 1 : 0; 184 } 185 186 __INTRIN_INLINE char _InterlockedAnd8(volatile char * const value, const char mask) 187 { 188 char x; 189 char y; 190 191 y = *value; 192 193 do 194 { 195 x = y; 196 y = _InterlockedCompareExchange8(value, x & mask, x); 197 } 198 while(y != x); 199 200 return y; 201 } 202 203 __INTRIN_INLINE short _InterlockedAnd16(volatile short * const value, const short mask) 204 { 205 short x; 206 short y; 207 208 y = *value; 209 210 do 211 { 212 x = y; 213 y = _InterlockedCompareExchange16(value, x & mask, x); 214 } 215 while(y != x); 216 217 return y; 218 } 219 220 __INTRIN_INLINE long _InterlockedAnd(volatile long * const value, const long mask) 221 { 222 long x; 223 long y; 224 225 y = *value; 226 227 do 228 { 229 x = y; 230 y = _InterlockedCompareExchange(value, x & mask, x); 231 } 232 while(y != x); 233 234 return y; 235 } 236 237 __INTRIN_INLINE char _InterlockedOr8(volatile char * const value, const char mask) 238 { 239 char x; 240 char y; 241 242 y = *value; 243 244 do 245 { 246 x = y; 247 y = _InterlockedCompareExchange8(value, x | mask, x); 248 } 249 while(y != x); 250 251 return y; 252 } 253 254 __INTRIN_INLINE short _InterlockedOr16(volatile short * const value, const short mask) 255 { 256 short x; 257 short y; 258 259 y = *value; 260 261 do 262 { 263 x = y; 264 y = _InterlockedCompareExchange16(value, x | mask, x); 265 } 266 while(y != x); 267 268 return y; 269 } 270 271 __INTRIN_INLINE long _InterlockedOr(volatile long * const value, const long mask) 272 { 273 long x; 274 long y; 275 276 y = *value; 277 278 do 279 { 280 x = y; 281 y = _InterlockedCompareExchange(value, x | mask, x); 282 } 283 while(y != x); 284 285 return y; 286 } 287 288 __INTRIN_INLINE char _InterlockedXor8(volatile char * const value, const char mask) 289 { 290 char x; 291 char y; 292 293 y = *value; 294 295 do 296 { 297 x = y; 298 y = _InterlockedCompareExchange8(value, x ^ mask, x); 299 } 300 while(y != x); 301 302 return y; 303 } 304 305 __INTRIN_INLINE short _InterlockedXor16(volatile short * const value, const short mask) 306 { 307 short x; 308 short y; 309 310 y = *value; 311 312 do 313 { 314 x = y; 315 y = _InterlockedCompareExchange16(value, x ^ mask, x); 316 } 317 while(y != x); 318 319 return y; 320 } 321 322 __INTRIN_INLINE long _InterlockedXor(volatile long * const value, const long mask) 323 { 324 long x; 325 long y; 326 327 y = *value; 328 329 do 330 { 331 x = y; 332 y = _InterlockedCompareExchange(value, x ^ mask, x); 333 } 334 while(y != x); 335 336 return y; 337 } 338 339 __INTRIN_INLINE long _InterlockedDecrement(volatile long * const lpAddend) 340 { 341 return _InterlockedExchangeAdd(lpAddend, -1) - 1; 342 } 343 344 __INTRIN_INLINE long _InterlockedIncrement(volatile long * const lpAddend) 345 { 346 return _InterlockedExchangeAdd(lpAddend, 1) + 1; 347 } 348 349 __INTRIN_INLINE long _InterlockedDecrement16(volatile short * const lpAddend) 350 { 351 return _InterlockedExchangeAdd16(lpAddend, -1) - 1; 352 } 353 354 __INTRIN_INLINE long _InterlockedIncrement16(volatile short * const lpAddend) 355 { 356 return _InterlockedExchangeAdd16(lpAddend, 1) + 1; 357 } 358 359 __INTRIN_INLINE long _InterlockedAddLargeStatistic(volatile long long * const Addend, const long Value) 360 { 361 *Addend += Value; 362 return Value; 363 } 364 365 __INTRIN_INLINE void _disable(void) 366 { 367 __asm__ __volatile__ 368 ( 369 "cpsid i @ __cli" : : : "memory", "cc" 370 ); 371 } 372 373 __INTRIN_INLINE void _enable(void) 374 { 375 __asm__ __volatile__ 376 ( 377 "cpsie i @ __sti" : : : "memory", "cc" 378 ); 379 } 380 381 __INTRIN_INLINE unsigned char _interlockedbittestandset(volatile long * a, const long b) 382 { 383 return (_InterlockedOr(a, 1 << b) >> b) & 1; 384 } 385 386 __INTRIN_INLINE unsigned char _interlockedbittestandreset(volatile long * a, const long b) 387 { 388 return (_InterlockedAnd(a, ~(1 << b)) >> b) & 1; 389 } 390 391 #ifndef __MSVCRT__ 392 __INTRIN_INLINE unsigned int _rotl(const unsigned int value, int shift) 393 { 394 return (((value) << ((int)(shift))) | ((value) >> (32 - (int)(shift)))); 395 } 396 #endif 397 398 #define _clz(a) \ 399 ({ ULONG __value, __arg = (a); \ 400 asm ("clz\t%0, %1": "=r" (__value): "r" (__arg)); \ 401 __value; }) 402 403 #endif 404 /* EOF */ 405