Skip to content
This repository has been archived by the owner on Mar 21, 2024. It is now read-only.

Commit

Permalink
Add store intrinsics for older MSVC
Browse files Browse the repository at this point in the history
  • Loading branch information
wmaxey committed Nov 19, 2020
1 parent 78034a2 commit 1e876bb
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 21 deletions.
2 changes: 1 addition & 1 deletion libcxx/include/__config
Original file line number Diff line number Diff line change
Expand Up @@ -860,7 +860,7 @@ typedef __char32_t char32_t;

// MSVC exposed __iso_volatile intrinsics beginning on 1924 for x86
#if _MSC_VER < 1924
#define _LIBCUDACXX_HAS_NO_ISO_INTRIN
#define _LIBCUDACXX_MSVC_HAS_NO_ISO_INTRIN
#endif

#define _LIBCUDACXX_UNDERLYING_TYPE(...) __underlying_type(__VA_ARGS__)
Expand Down
61 changes: 41 additions & 20 deletions libcxx/include/support/win32/atomic_msvc.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ namespace detail {

template<class _Type, detail::_enable_if_sized_as<_Type,1> = 0>
void __atomic_load_relaxed(const volatile _Type *__ptr, _Type *__ret) {
#ifdef _LIBCUDACXX_HAS_NO_ISO_INTRIN
#ifdef _LIBCUDACXX_MSVC_HAS_NO_ISO_INTRIN
__int8 __tmp = *(const volatile __int8 *)__ptr;
#else
__int8 __tmp = __iso_volatile_load8((const volatile __int8 *)__ptr);
Expand All @@ -55,26 +55,26 @@ void __atomic_load_relaxed(const volatile _Type *__ptr, _Type *__ret) {
}
template<class _Type, detail::_enable_if_sized_as<_Type,2> = 0>
void __atomic_load_relaxed(const volatile _Type *__ptr, _Type *__ret) {
#ifdef _LIBCUDACXX_HAS_NO_ISO_INTRIN
__int16 __tmp = *(const volatile __int8 *)__ptr;
#ifdef _LIBCUDACXX_MSVC_HAS_NO_ISO_INTRIN
__int16 __tmp = *(const volatile __int16 *)__ptr;
#else
__int16 __tmp = __iso_volatile_load16((const volatile __int16 *)__ptr);
#endif
*__ret = reinterpret_cast<_Type&>(__tmp);
}
template<class _Type, detail::_enable_if_sized_as<_Type,4> = 0>
void __atomic_load_relaxed(const volatile _Type *__ptr, _Type *__ret) {
#ifdef _LIBCUDACXX_HAS_NO_ISO_INTRIN
__int32 __tmp = *(const volatile __int8 *)__ptr;
#ifdef _LIBCUDACXX_MSVC_HAS_NO_ISO_INTRIN
__int32 __tmp = *(const volatile __int32 *)__ptr;
#else
__int32 __tmp = __iso_volatile_load32((const volatile __int32 *)__ptr);
#endif
*__ret = reinterpret_cast<_Type&>(__tmp);
}
template<class _Type, detail::_enable_if_sized_as<_Type,8> = 0>
void __atomic_load_relaxed(const volatile _Type *__ptr, _Type *__ret) {
#ifdef _LIBCUDACXX_HAS_NO_ISO_INTRIN
__int64 __tmp = *(const volatile __int8 *)__ptr;
#ifdef _LIBCUDACXX_MSVC_HAS_NO_ISO_INTRIN
__int64 __tmp = *(const volatile __int64 *)__ptr;
#else
__int64 __tmp = __iso_volatile_load64((const volatile __int64 *)__ptr);
#endif
Expand All @@ -92,27 +92,48 @@ void __atomic_load(const volatile _Type *__ptr, _Type *__ret, int __memorder) {
}
}


template<class _Type, detail::_enable_if_sized_as<_Type,1> = 0>
void __atomic_store_relaxed(const volatile _Type *__ptr, _Type *__val) {
__int8 __tmp = reinterpret_cast<__int8&>(*__val);
__iso_volatile_store8((volatile __int8 *)__ptr, __tmp);
void __atomic_store_relaxed(volatile _Type *__ptr, _Type *__val) {
auto __t = reinterpret_cast<__int8 *>(__val);
auto __d = reinterpret_cast<volatile __int8 *>(__ptr);
#ifdef _LIBCUDACXX_MSVC_HAS_NO_ISO_INTRIN
(void)_InterlockedExchange8(__d, *__t);
#else
__iso_volatile_store8(__d, __t);
#endif
}
template<class _Type, detail::_enable_if_sized_as<_Type,2> = 0>
void __atomic_store_relaxed(const volatile _Type *__ptr, _Type *__val) {
__int16 __tmp = reinterpret_cast<__int16&>(*__val);
__iso_volatile_store16((volatile __int16 *)__ptr, __tmp);
void __atomic_store_relaxed(volatile _Type *__ptr, _Type *__val) {
auto __t = reinterpret_cast<__int16 *>(__val);
auto __d = reinterpret_cast<volatile __int16 *>(__ptr);
#ifdef _LIBCUDACXX_MSVC_HAS_NO_ISO_INTRIN
(void)_InterlockedExchange16(__d, *__t);
#else
__iso_volatile_store16(__d, __t);
#endif
}
template<class _Type, detail::_enable_if_sized_as<_Type,4> = 0>
void __atomic_store_relaxed(const volatile _Type *__ptr, _Type *__val) {
__int32 __tmp = reinterpret_cast<__int32&>(*__val);
__iso_volatile_store32((volatile __int32 *)__ptr, __tmp);
void __atomic_store_relaxed(volatile _Type *__ptr, _Type *__val) {
auto __t = reinterpret_cast<__int32 *>(__val);
auto __d = reinterpret_cast<volatile __int32 *>(__ptr);
#ifdef _LIBCUDACXX_MSVC_HAS_NO_ISO_INTRIN
// int cannot be converted to long?...
(void)_InterlockedExchange(reinterpret_cast<volatile long *>(__d), *__t);
#else
__iso_volatile_store32(__d, __t);
#endif
}
template<class _Type, detail::_enable_if_sized_as<_Type,8> = 0>
void __atomic_store_relaxed(const volatile _Type *__ptr, _Type *__val) {
__int64 __tmp = reinterpret_cast<__int64&>(*__val);
__iso_volatile_store64((volatile __int64 *)__ptr, __tmp);
void __atomic_store_relaxed(volatile _Type *__ptr, _Type *__val) {
auto __t = reinterpret_cast<__int64 *>(__val);
auto __d = reinterpret_cast<volatile __int64 *>(__ptr);
#ifdef _LIBCUDACXX_MSVC_HAS_NO_ISO_INTRIN
(void)_InterlockedExchange64(__d, *__t);
#else
__iso_volatile_store64(__d, __t);
#endif
}

template<class _Type>
void __atomic_store(volatile _Type *__ptr, _Type *__val, int __memorder) {
switch (__memorder) {
Expand Down

0 comments on commit 1e876bb

Please sign in to comment.