Skip to content

Instantly share code, notes, and snippets.

@TheServer201
Last active July 12, 2018 21:41
Show Gist options
  • Save TheServer201/462de56417a3fc3ec90e0496ff6c0d87 to your computer and use it in GitHub Desktop.
Save TheServer201/462de56417a3fc3ec90e0496ff6c0d87 to your computer and use it in GitHub Desktop.
Implementation of missing lockable instructions
#include <stdbool.h>
#include <stdint.h>
typedef __int128 int128_t;
typedef unsigned __int128 uint128_t;
#define atomic_fetch_adc(object, argument) \
({ \
asm("lock adc %0, %1" \
: "+m"(*(object)), "+r"(argument) \
: \
: "memory", "cc"); \
})
#define atomic_fetch_sbb(object, argument) \
({ \
asm("lock sbb %0, %1" \
: "+m"(*(object)), "+r"(argument) \
: \
: "memory", "cc"); \
})
#if defined(__GCC_ASM_FLAG_OUTPUTS__)
#define atomic_bit_test_clear(object, argument) \
({ \
bool result; \
asm("lock btc %0, %2" \
: "+m"(*(object)), "=@ccc"(result) \
: "r"(argument) \
: "memory", "cc"); \
result; \
})
#define atomic_bit_test_reset(object, argument) \
({ \
bool result; \
asm("lock btr %0, %2" \
: "+m"(*(object)), "=@ccc"(result) \
: "r"(argument) \
: "memory", "cc"); \
result; \
})
#define atomic_bit_test_set(object, argument) \
({ \
bool result; \
asm("lock bts %0, %2" \
: "+m"(*(object)), "=@ccc"(result) \
: "r"(argument) \
: "memory", "cc"); \
result; \
})
inline bool atomic_compare_exchange_eight_bytes(uint64_t *object,
uint32_t *expected_low,
uint32_t *expected_high,
uint32_t desired_low,
uint32_t desired_high) {
bool result;
asm("lock cmpxchg8b %0"
: "+m"(*object), "+d"(expected_high), "+a"(expected_low), "=@ccz"(result)
: "c"(desired_high), "b"(desired_low)
: "memory", "cc");
return result;
}
inline bool atomic_compare_exchange_sixteen_bytes(uint128_t *object,
uint64_t *expected_low,
uint64_t *expected_high,
uint64_t desired_low,
uint64_t desired_high) {
bool result;
asm("lock cmpxchg16b %0"
: "+m"(*object), "+d"(expected_high), "+a"(expected_low), "=@ccz"(result)
: "c"(desired_high), "b"(desired_low)
: "memory", "cc");
return result;
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment