11 #ifndef INT_COMPILER_MSVC 20 return Shift ? (Value << Shift) | (Value >> (8 - Shift)) : Value;
27 return Shift ? (Value >> Shift) | (Value << (8 - Shift)) : Value;
30 static inline uint16_t
34 return Shift ? (Value << Shift) | (Value >> (16 - Shift)) : Value;
37 static inline uint16_t
41 return Shift ? (Value >> Shift) | (Value << (16 - Shift)) : Value;
53 *Index = __builtin_ctzl(Mask);
62 *Index = 31 - __builtin_clzl(Mask);
66 static inline uint32_t
69 return __builtin_popcount(Value);
73 _bittest(int32_t
const *BitBase, int32_t BitPos)
75 return (*BitBase >> BitPos) & 1;
81 uint8_t _Res = (*BitBase >> BitPos) & 1;
82 *BitBase = *BitBase ^ (1 << BitPos);
89 uint8_t _Res = (*BitBase >> BitPos) & 1;
90 *BitBase = *BitBase & ~(1 << BitPos);
97 uint8_t _Res = (*BitBase >> BitPos) & 1;
98 *BitBase = *BitBase | (1 << BitPos);
103 static inline uint8_t
108 *Index = __builtin_ctzll(Mask);
112 static inline uint8_t
117 *Index = 63 - __builtin_clzll(Mask);
121 static inline uint64_t
124 return __builtin_popcountll(Value);
127 static inline uint8_t
130 return (*BitBase >> BitPos) & 1;
133 static inline uint8_t
136 uint8_t Res = (*BitBase >> BitPos) & 1;
137 *BitBase = *BitBase ^ (1ll << BitPos);
141 static inline uint8_t
144 uint8_t Res = (*BitBase >> BitPos) & 1;
145 *BitBase = *BitBase & ~(1ll << BitPos);
149 static inline uint8_t
152 uint8_t Res = (*BitBase >> BitPos) & 1;
153 *BitBase = *BitBase | (1ll << BitPos);
163 #ifdef INT_COMPILER_CLANG 164 #define __ptr_to_addr_space(__addr_space_nbr, __type, offset) \ 165 ((volatile __type __attribute__((__address_space__(__addr_space_nbr)))*) \ 168 static inline uint8_t
169 __readgsbyte(uint64_t offset)
171 return *__ptr_to_addr_space(256, uint8_t, offset);
174 static inline uint16_t
175 __readgsword(uint64_t offset)
177 return *__ptr_to_addr_space(256, uint16_t, offset);
180 static inline uint32_t
181 __readgsdword(uint64_t offset)
183 return *__ptr_to_addr_space(256, uint32_t, offset);
186 static inline uint64_t
187 __readgsqword(uint64_t offset)
189 return *__ptr_to_addr_space(256, uint64_t, offset);
192 #undef __ptr_to_addr_space 200 __movsb(uint8_t *dst, uint8_t
const *src,
size_t n)
202 __asm__(
"rep movsb" :
"+D"(dst),
"+S"(src),
"+c"(n));
206 __movsd(uint32_t *dst, uint32_t
const *src,
size_t n)
208 __asm__(
"rep movsl" :
"+D"(dst),
"+S"(src),
"+c"(n));
212 __movsw(uint16_t *dst, uint16_t
const *src,
size_t n)
214 __asm__(
"rep movsh" :
"+D"(dst),
"+S"(src),
"+c"(n));
220 __asm__(
"rep stosb" :
"+D"(dst),
"+c"(n) :
"a"(x));
226 __asm__(
"rep stosl" :
"+D"(dst),
"+c"(n) :
"a"(x));
232 __asm__(
"rep stosh" :
"+D"(dst),
"+c"(n) :
"a"(x));
236 __movsq(uint64_t *dst, uint64_t
const *src,
size_t n)
238 __asm__(
"rep movsq" :
"+D"(dst),
"+S"(src),
"+c"(n));
244 __asm__(
"rep stosq" :
"+D"(dst),
"+c"(n) :
"a"(x));
253 return (
void *)((int8_t *)__builtin_frame_address(0) +
sizeof(
void *));
259 return __builtin_return_address(0);
265 __asm__(
"cpuid" :
"=a"(info[0]),
"=b" (info[1]),
"=c"(info[2]),
"=d"(info[3])
272 __asm__(
"cpuid" :
"=a"(info[0]),
"=b" (info[1]),
"=c"(info[2]),
"=d"(info[3])
273 :
"a"(level),
"c"(ecx));
276 static inline uint64_t
279 uint32_t __eax, __edx;
280 __asm__(
"xgetbv" :
"=a" (__eax),
"=d" (__edx) :
"c" (xcr_no));
281 return ((uint64_t)__edx << 32) | __eax;
287 __asm__
volatile (
"hlt");
295 #define _mm_prefetch(p, i) __builtin_prefetch(p, 0, i) 297 #ifndef INT_COMPILER_CLANG 302 __asm__ __volatile__(
"pause");
305 static inline uint64_t
308 return __builtin_ia32_rdtsc();
317 static inline uint64_t
328 __asm__(
"rdmsr" :
"=d"(edx),
"=a"(eax) :
"c"(reg));
329 return (((uint64_t)edx) << 32) | (uint64_t)eax;
332 static inline uint64_t
336 __asm__ __volatile__(
"mov %%cr0, %0" :
"=q"(cr0_val) : :
"memory");
340 static inline uint64_t
344 __asm__ __volatile__(
"mov %%cr3, %0" :
"=q"(cr3_val) : :
"memory");
348 static inline uint64_t
352 __asm__ __volatile__(
"mov %%cr4, %0" :
"=q"(cr4_val) : :
"memory");
356 static inline uint64_t
360 __asm__ __volatile__(
"mov %%cr8, %0" :
"=q"(cr8_val) : :
"memory");
367 __asm__(
"mov %0, %%cr0" : :
"q"(cr0_val) :
"memory");
373 __asm__(
"mov %0, %%cr3" : :
"q"(cr3_val) :
"memory");
379 __asm__(
"mov %0, %%cr4" : :
"q"(cr4_val) :
"memory");
385 __asm__(
"mov %0, %%cr8" : :
"q"(cr8_val) :
"memory");
391 __asm__ __volatile__(
"invlpg (%0)" : :
"b"(Address) :
"memory");
394 static inline uint8_t
397 int32_t _PrevVal = __atomic_fetch_or(BitBase, 1l << BitPos, __ATOMIC_SEQ_CST);
398 return (_PrevVal >> BitPos) & 1;
401 static inline uint8_t
404 int32_t _PrevVal = __atomic_fetch_and(BitBase, ~(1l << BitPos), __ATOMIC_SEQ_CST);
405 return (_PrevVal >> BitPos) & 1;
408 static inline uint8_t
411 int64_t _PrevVal = __atomic_fetch_or(BitBase, 1ll << BitPos, __ATOMIC_SEQ_CST);
412 return (_PrevVal >> BitPos) & 1;
422 return __atomic_fetch_add(Addend, Value, __ATOMIC_SEQ_CST);
425 static inline int16_t
428 return __atomic_fetch_add(Addend, Value, __ATOMIC_SEQ_CST);
432 static inline int64_t
435 return __atomic_fetch_add(Addend, Value, __ATOMIC_SEQ_CST);
442 static inline int32_t
445 return __atomic_add_fetch(Value, 1, __ATOMIC_SEQ_CST);
448 static inline int16_t
451 return __atomic_add_fetch(Value, 1, __ATOMIC_SEQ_CST);
454 static inline int64_t
457 return __atomic_add_fetch(Value, 1, __ATOMIC_SEQ_CST);
464 static inline int32_t
467 return __atomic_sub_fetch(Value, 1, __ATOMIC_SEQ_CST);
470 static inline int16_t
473 return __atomic_sub_fetch(Value, 1, __ATOMIC_SEQ_CST);
476 static inline int64_t
479 return __atomic_sub_fetch(Value, 1, __ATOMIC_SEQ_CST);
489 return __atomic_and_fetch(Value, Mask, __ATOMIC_SEQ_CST);
492 static inline int16_t
495 return __atomic_and_fetch(Value, Mask, __ATOMIC_SEQ_CST);
498 static inline int32_t
501 return __atomic_and_fetch(Value, Mask, __ATOMIC_SEQ_CST);
504 static inline int64_t
507 return __atomic_and_fetch(Value, Mask, __ATOMIC_SEQ_CST);
517 return __atomic_or_fetch(Value, Mask, __ATOMIC_SEQ_CST);
520 static inline int16_t
523 return __atomic_or_fetch(Value, Mask, __ATOMIC_SEQ_CST);
526 static inline int32_t
529 return __atomic_or_fetch(Value, Mask, __ATOMIC_SEQ_CST);
532 static inline int64_t
535 return __atomic_or_fetch(Value, Mask, __ATOMIC_SEQ_CST);
545 return __atomic_xor_fetch(Value, Mask, __ATOMIC_SEQ_CST);
548 static inline int16_t
551 return __atomic_xor_fetch(Value, Mask, __ATOMIC_SEQ_CST);
554 static inline int32_t
557 return __atomic_xor_fetch(Value, Mask, __ATOMIC_SEQ_CST);
560 static inline int64_t
563 return __atomic_xor_fetch(Value, Mask, __ATOMIC_SEQ_CST);
570 static inline int32_t
573 __atomic_exchange(Target, &Value, &Value, __ATOMIC_SEQ_CST);
580 __atomic_exchange(Target, &Value, &Value, __ATOMIC_SEQ_CST);
584 static inline int16_t
587 __atomic_exchange(Target, &Value, &Value, __ATOMIC_SEQ_CST);
591 static inline int64_t
594 __atomic_exchange(Target, &Value, &Value, __ATOMIC_SEQ_CST);
605 __atomic_compare_exchange(Destination, &Comparand, &Exchange, 0,
606 __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST);
610 static inline int16_t
613 __atomic_compare_exchange(Destination, &Comparand, &Exchange, 0,
614 __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST);
618 static inline int32_t
621 __atomic_compare_exchange(Destination, &Comparand, &Exchange, 0,
622 __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST);
626 static inline int64_t
629 __atomic_compare_exchange(Destination, &Comparand, &Exchange, 0,
630 __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST);
645 __attribute__((__deprecated__(
"use other intrinsics or C++11 atomics instead")))
648 __asm__
volatile (
"" : : :
"memory");
652 __attribute__((__deprecated__(
"use other intrinsics or C++11 atomics instead")))
655 __asm__
volatile (
"" : : :
"memory");
659 __attribute__((__deprecated__(
"use other intrinsics or C++11 atomics instead")))
662 __asm__
volatile (
"" : : :
"memory");
668 __asm__
volatile(
"lock orq $0, (%%rsp)" : : :
"memory");
671 #endif // INT_COMPILER_MSVC 673 #endif // _INTRINSICS_H_ static int16_t _InterlockedXor16(int16_t volatile *Value, int16_t Mask)
static void __writecr8(uint64_t cr8_val)
static int16_t _InterlockedDecrement16(int16_t volatile *Value)
static int64_t _InterlockedExchange64(int64_t volatile *Target, int64_t Value)
static int64_t _InterlockedXor64(int64_t volatile *Value, int64_t Mask)
static uint8_t _bittestandreset64(int64_t *BitBase, int64_t BitPos)
static int32_t _InterlockedIncrement(int32_t volatile *Value)
static int16_t _InterlockedCompareExchange16(int16_t volatile *Destination, int16_t Exchange, int16_t Comparand)
static int64_t _InterlockedAnd64(int64_t volatile *Value, int64_t Mask)
static void __stosb(uint8_t *dst, uint8_t x, size_t n)
static uint8_t _BitScanForward64(uint32_t *Index, uint64_t Mask)
static uint8_t _bittestandcomplement(int32_t *BitBase, int32_t BitPos)
static uint8_t _bittestandset64(int64_t *BitBase, int64_t BitPos)
static void * _AddressOfReturnAddress(void)
static int32_t _InterlockedDecrement(int32_t volatile *Value)
static uint32_t _mm_popcnt_u32(uint32_t Value)
static uint16_t _rotr16(uint16_t Value, uint8_t Shift)
static uint8_t _interlockedbittestandreset(int32_t volatile *BitBase, int32_t BitPos)
static int64_t _InterlockedIncrement64(int64_t volatile *Value)
static uint8_t _bittestandset(int32_t *BitBase, int32_t BitPos)
static uint64_t __readcr8(void)
static uint8_t _rotl8(uint8_t Value, uint8_t Shift)
static uint8_t _interlockedbittestandset(int32_t volatile *BitBase, int32_t BitPos)
static int8_t _InterlockedAnd8(int8_t volatile *Value, int8_t Mask)
static uint64_t __readmsr(uint32_t reg)
static void __stosw(uint16_t *dst, uint16_t x, size_t n)
static int16_t _InterlockedOr16(int16_t volatile *Value, int16_t Mask)
static void __invlpg(void *Address)
static void __movsb(uint8_t *dst, uint8_t const *src, size_t n)
static uint8_t _BitScanForward(uint32_t *Index, uint32_t Mask)
static int8_t _InterlockedCompareExchange8(int8_t volatile *Destination, int8_t Exchange, int8_t Comparand)
static int8_t _InterlockedExchangeAdd8(int8_t volatile *Addend, int8_t Value)
static int64_t _InterlockedDecrement64(int64_t volatile *Value)
static void __writecr0(uint64_t cr0_val)
static uint8_t _bittest64(int64_t const *BitBase, int64_t BitPos)
static uint8_t _interlockedbittestandset64(int64_t volatile *BitBase, int64_t BitPos)
static void __faststorefence(void)
static int32_t _InterlockedExchange(int32_t volatile *Target, int32_t Value)
static uint8_t _bittestandreset(int32_t *BitBase, int32_t BitPos)
static void __stosd(uint32_t *dst, uint32_t x, size_t n)
static uint64_t _xgetbv(uint32_t xcr_no)
static int8_t _InterlockedXor8(int8_t volatile *Value, int8_t Mask)
static void __cpuidex(int32_t info[4], int32_t level, int32_t ecx)
static void * _ReturnAddress(void)
static void __movsd(uint32_t *dst, uint32_t const *src, size_t n)
static void _ReadWriteBarrier(void)
static void _mm_pause(void)
static void __writecr3(uint64_t cr3_val)
static void _WriteBarrier(void)
static uint8_t _BitScanReverse(uint32_t *Index, uint32_t Mask)
static uint64_t __readcr0(void)
static int32_t _InterlockedOr(int32_t volatile *Value, int32_t Mask)
static int8_t _InterlockedExchange8(int8_t volatile *Target, int8_t Value)
static void __cpuid(int32_t info[4], int32_t level)
static void _ReadBarrier(void)
static uint64_t __rdtsc(void)
static int64_t _InterlockedExchangeAdd64(int64_t volatile *Addend, int64_t Value)
static uint64_t _mm_popcnt_u64(uint64_t Value)
static int16_t _InterlockedIncrement16(int16_t volatile *Value)
static int16_t _InterlockedExchangeAdd16(int16_t volatile *Addend, int16_t Value)
static int64_t _InterlockedCompareExchange64(int64_t volatile *Destination, int64_t Exchange, int64_t Comparand)
static uint16_t _rtol16(uint16_t Value, uint8_t Shift)
static uint64_t __readcr3(void)
static uint8_t _rotr8(uint8_t Value, uint8_t Shift)
static uint8_t _bittestandcomplement64(int64_t *BitBase, int64_t BitPos)
static uint8_t _BitScanReverse64(uint32_t *Index, uint64_t Mask)
static void __movsw(uint16_t *dst, uint16_t const *src, size_t n)
static uint8_t _bittest(int32_t const *BitBase, int32_t BitPos)
static int8_t _InterlockedOr8(int8_t volatile *Value, int8_t Mask)
static void __movsq(uint64_t *dst, uint64_t const *src, size_t n)
static int64_t _InterlockedOr64(int64_t volatile *Value, int64_t Mask)
static int32_t _InterlockedAnd(int32_t volatile *Value, int32_t Mask)
static int16_t _InterlockedExchange16(int16_t volatile *Target, int16_t Value)
static uint64_t __readcr4(void)
static void __stosq(uint64_t *dst, uint64_t x, size_t n)
static void * _InterlockedCompareExchangePointer(void volatile **Destination, void *Exchange, void *Comparand)
static int16_t _InterlockedAnd16(int16_t volatile *Value, int16_t Mask)
static int32_t _InterlockedXor(int32_t volatile *Value, int32_t Mask)
static int32_t _InterlockedCompareExchange(int32_t volatile *Destination, int32_t Exchange, int32_t Comparand)
static void __writecr4(uint64_t cr4_val)