remove more macros

master
Daniel Kolesa 2015-05-01 21:09:44 +01:00
parent 367a586bf7
commit 084139b731
1 changed files with 65 additions and 36 deletions

View File

@ -161,30 +161,43 @@ namespace octa {
__OctaMsvcAtomicStore<T>::store(&a->value, v, ord); __OctaMsvcAtomicStore<T>::store(&a->value, v, ord);
} }
#undef __OCTA_MSVC_ATOMIC_STORE_N template<typename T, size_t N = sizeof(T)>
#undef __OCTA_MSVC_ATOMIC_STORE struct __OctaMsvcAtomicLoad;
#define __OCTA_MSVC_ATOMIC_LOAD_N(src, dst, ord) \ template<typename T>
*(dst) = *(src); struct __OctaMsvcAtomicLoad<T, 1> {
static inline void load(volatile T *src, T *dst)
noexcept { *dst = *src; }
};
#define __OCTA_MSVC_ATOMIC_LOAD_64(src, dst, ord) \ template<typename T>
__pragma(warning(push)) \ struct __OctaMsvcAtomicLoad<T, 2> {
__pragma(warning(disable:4047)) \ static inline void load(volatile T *src, T *dst)
*(dst) = InterlockedOr64((volatile int64_t *)(src), 0); \ noexcept { *dst = *src; }
__pragma(warning(pop)) };
#define __OCTA_MSVC_ATOMIC_LOAD(src, dst, order) \ template<typename T>
if (sizeof(*dst) == 1 || sizeof(*dst) == 2 || sizeof(*dst) == 4) { \ struct __OctaMsvcAtomicLoad<T, 4> {
__OCTA_MSVC_ATOMIC_LOAD_N(src, dst, ord); \ static inline void load(volatile T *src, T *dst)
} else if (sizeof(*dst) == 8) { \ noexcept { *dst = *src; }
__OCTA_MSVC_ATOMIC_LOAD_64(src, dst, ord); \ };
}
template<typename T>
struct __OctaMsvcAtomicLoad<T, 8> {
static inline void load(volatile T *src, T *dst)
noexcept {
#pragma warning(push)
#pragma warning(disable:4047)
*dst = InterlockedOr64((volatile int64_t *)(src), 0);
#pragma warning(pop)
}
};
template<typename T> template<typename T>
static inline T __octa_atomic_load(volatile __OctaAtomicBase<T> *a, static inline T __octa_atomic_load(volatile __OctaAtomicBase<T> *a,
MemoryOrder ord) { MemoryOrder ord) {
T r; T r;
__OCTA_MSVC_ATOMIC_LOAD(&a->value, &r, ord); __OctaMsvcAtomicLoad<T>::load(&a->value, &r);
return r; return r;
} }
@ -192,32 +205,50 @@ namespace octa {
static inline T __octa_atomic_load(__OctaAtomicBase<T> *a, static inline T __octa_atomic_load(__OctaAtomicBase<T> *a,
MemoryOrder ord) { MemoryOrder ord) {
T r; T r;
__OCTA_MSVC_ATOMIC_LOAD(&a->value, &r, ord); __OctaMsvcAtomicLoad<T>::load(&a->value, &r);
return r; return r;
} }
#undef __OCTA_MSVC_ATOMIC_LOAD_N template<typename T, size_t N = sizeof(T)>
#undef __OCTA_MSVC_ATOMIC_LOAD_64 struct __OctaMsvcAtomicExchange;
#undef __OCTA_MSVC_ATOMIC_LOAD
#define __OCTA_MSVC_ATOMIC_EXCHANGE(dst, src, ret) \ template<typename T>
if (sizeof(*dst) == 1) { \ struct __OctaMsvcAtomicExchange<T, 1> {
*(ret) = InterlockedExchange8((volatile int8_t *)(dst), (int8_t)(src)); \ static inline void exchange(volatile T *dst, T src, T *ret)
} else if (sizeof(*dst) == 2) { \ noexcept {
*(ret) = InterlockedExchange16((volatile int16_t *)(dst), (int16_t)(src)); \ *ret = InterlockedExchange8((volatile int8_t *)dst, (int8_t)src);
} else if (sizeof(*dst) == 4) { \ }
*(ret) = InterlockedExchange((volatile int32_t *)(dst), (int32_t)(src)); \ };
} else if (sizeof(*dst) == 8) { \
*(ret) = InterlockedExchange64((volatile int64_t *)(dst), (int64_t)(src)); \ template<typename T>
} else { \ struct __OctaMsvcAtomicExchange<T, 2> {
abort(); \ static inline void exchange(volatile T *dst, T src, T *ret)
} noexcept {
*ret = InterlockedExchange16((volatile int16_t *)dst, (int16_t)src);
}
};
template<typename T>
struct __OctaMsvcAtomicExchange<T, 4> {
static inline void exchange(volatile T *dst, T src, T *ret)
noexcept {
*ret = InterlockedExchange((volatile int32_t *)dst, (int32_t)src);
}
};
template<typename T>
struct __OctaMsvcAtomicExchange<T, 8> {
static inline void exchange(volatile T *dst, T src, T *ret)
noexcept {
*ret = InterlockedExchange64((volatile int64_t *)dst, (int64_t)src);
}
};
template<typename T> template<typename T>
static inline T __octa_atomic_exchange(volatile __OctaAtomicBase<T> *a, static inline T __octa_atomic_exchange(volatile __OctaAtomicBase<T> *a,
T v, memory_order) { T v, memory_order) {
T r; T r;
__OCTA_MSVC_ATOMIC_EXCHANGE(&a->value, v, &r); __OctaMsvcAtomicExchange<T>::exchange(&a->value, v, &r);
return r; return r;
} }
@ -225,12 +256,10 @@ namespace octa {
static inline T __octa_atomic_exchange(__OctaAtomicBase<T> *a, static inline T __octa_atomic_exchange(__OctaAtomicBase<T> *a,
T v, memory_order) { T v, memory_order) {
T r; T r;
__OCTA_MSVC_ATOMIC_EXCHANGE(&a->value, v, &r); __OctaMsvcAtomicExchange<T>::exchange(&a->value, v, &r);
return r; return r;
} }
#undef __OCTA_MSVC_ATOMIC_EXCHANGE
static inline bool __octa_msvc_atomic_compare_exchange8( static inline bool __octa_msvc_atomic_compare_exchange8(
volatile int8_t *dst, int8_t *exp, int8_t src volatile int8_t *dst, int8_t *exp, int8_t src
) { ) {