Skip to content

Fix intrin_compat.h to work for MinGW #671

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 13, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 16 additions & 12 deletions Dependencies/Utility/Utility/intrin_compat.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,12 @@ static inline uint32_t _lrotl(uint32_t value, int shift)
#ifdef _WIN32
#include <intrin.h>
#pragma intrinsic(__rdtsc)
#endif

#endif // _WIN32
#endif // _rdtsc
#ifndef _rdtsc
static inline uint64_t _rdtsc()
{
#if _WIN32
#ifdef _WIN32
return __rdtsc();
#elif defined(__has_builtin) && __has_builtin(__builtin_readcyclecounter)
return __builtin_readcyclecounter();
Expand All @@ -101,9 +102,9 @@ static inline uint64_t _rdtsc()
#error "No implementation for _rdtsc"
#endif
}
#endif
#endif // _rdtsc

#ifdef _MSC_VER
#ifdef _WIN32
#include <intrin.h>
#pragma intrinsic(_ReturnAddress)
#elif defined(__has_builtin)
Expand Down Expand Up @@ -132,17 +133,20 @@ static inline uint64_t _rdtsc()
#endif

#ifndef cpuid
#if defined(_MSC_VER)
#if (defined _M_IX86 || defined _M_X64 || defined __i386__ || defined __amd64__)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the goal of this condition?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Goal was to use dummy implementation on architectures other than x86 and x86_64. (As cpuid is instruction specific to x86) Condition was previously only used only for GCC and clang, but same applies to MSVC.

#ifdef _WIN32
#include <intrin.h>
#define cpuid(regs, cpuid_type) __cpuid(reinterpret_cast<int *>(regs), cpuid_type)
#elif (defined __clang__ || defined __GNUC__) && (defined __i386__ || defined __amd64__)
#elif (defined __clang__ || defined __GNUC__)
#include <cpuid.h>
#define cpuid(regs, cpuid_type) __cpuid(cpuid_type, regs[0], regs[1], regs[2], regs[3])
#else
/* Just return 0 for everything if its not x86 */
#endif
#endif // (defined _M_IX86 || defined _M_X64 || defined __i386__ || defined __amd64__)
#endif // cpuid
#ifndef cpuid
/* Just return 0 for everything if its not x86 or as fallback */
#include <string.h>
#define cpuid(regs, cpuid_type) memset(regs, 0, 16)
#endif
#endif //cpuid
#endif // cpuid

#endif // defined(_MSC_VER) && _MSC_VER < 1300
#endif // !(defined(_MSC_VER) && _MSC_VER < 1300)