Fixed cpuid reported supported on unsupported compilers

Former-commit-id: 4f5fc2251882ae04d9131242d0f9d7f8a3a3e6dc
This commit is contained in:
Lynix 2014-04-21 10:43:46 +02:00
parent 531fa2ce84
commit 55fe71ee21
1 changed files with 48 additions and 44 deletions

View File

@ -14,16 +14,16 @@
void NzHardwareInfoImpl::Cpuid(nzUInt32 code, nzUInt32 result[4]) void NzHardwareInfoImpl::Cpuid(nzUInt32 code, nzUInt32 result[4])
{ {
#if defined(NAZARA_COMPILER_MSVC) #if defined(NAZARA_COMPILER_MSVC)
__cpuid(reinterpret_cast<int*>(result), static_cast<int>(code)); // Visual propose une fonction intrinsèque pour le cpuid __cpuid(reinterpret_cast<int*>(result), static_cast<int>(code)); // Visual propose une fonction intrinsèque pour le cpuid
#elif defined(NAZARA_COMPILER_CLANG) || defined(NAZARA_COMPILER_GCC) || defined(NAZARA_COMPILER_INTEL) #elif defined(NAZARA_COMPILER_CLANG) || defined(NAZARA_COMPILER_GCC) || defined(NAZARA_COMPILER_INTEL)
// Source: http://stackoverflow.com/questions/1666093/cpuid-implementations-in-c // Source: http://stackoverflow.com/questions/1666093/cpuid-implementations-in-c
asm volatile ("cpuid" // Besoin d'être volatile ? asm volatile ("cpuid" // Besoin d'être volatile ?
: "=a" (result[0]), "=b" (result[1]), "=c" (result[2]), "=d" (result[3]) // output : "=a" (result[0]), "=b" (result[1]), "=c" (result[2]), "=d" (result[3]) // output
: "a" (code), "c" (0)); // input : "a" (code), "c" (0)); // input
#else #else
NazaraInternalError("Cpuid has been called although it is not supported"); NazaraInternalError("Cpuid has been called although it is not supported");
#endif #endif
} }
unsigned int NzHardwareInfoImpl::GetProcessorCount() unsigned int NzHardwareInfoImpl::GetProcessorCount()
@ -37,49 +37,53 @@ unsigned int NzHardwareInfoImpl::GetProcessorCount()
bool NzHardwareInfoImpl::IsCpuidSupported() bool NzHardwareInfoImpl::IsCpuidSupported()
{ {
#if defined(NAZARA_COMPILER_MSVC)
#ifdef NAZARA_PLATFORM_x64 #ifdef NAZARA_PLATFORM_x64
return true; // Toujours supporté sur un processeur 64 bits return true; // Toujours supporté sur un processeur 64 bits
#else #else
#if defined(NAZARA_COMPILER_MSVC) int supported;
int supported; __asm
__asm {
{ pushfd
pushfd pop eax
pop eax mov ecx, eax
mov ecx, eax xor eax, 0x200000
xor eax, 0x200000 push eax
push eax popfd
popfd pushfd
pushfd pop eax
pop eax xor eax, ecx
xor eax, ecx mov supported, eax
mov supported, eax push ecx
push ecx popfd
popfd };
};
return supported != 0; return supported != 0;
#elif defined(NAZARA_COMPILER_CLANG) || defined(NAZARA_COMPILER_GCC) || defined(NAZARA_COMPILER_INTEL) #endif // NAZARA_PLATFORM_x64
int supported; #elif defined(NAZARA_COMPILER_CLANG) || defined(NAZARA_COMPILER_GCC) || defined(NAZARA_COMPILER_INTEL)
asm volatile (" pushfl\n" #ifdef NAZARA_PLATFORM_x64
" pop %%eax\n" return true; // Toujours supporté sur un processeur 64 bits
" mov %%eax, %%ecx\n" #else
" xor $0x200000, %%eax\n" int supported;
" push %%eax\n" asm volatile (" pushfl\n"
" popfl\n" " pop %%eax\n"
" pushfl\n" " mov %%eax, %%ecx\n"
" pop %%eax\n" " xor $0x200000, %%eax\n"
" xor %%ecx, %%eax\n" " push %%eax\n"
" mov %%eax, %0\n" " popfl\n"
" push %%ecx\n" " pushfl\n"
" popfl" " pop %%eax\n"
: "=m" (supported) // output " xor %%ecx, %%eax\n"
: // input " mov %%eax, %0\n"
: "eax", "ecx", "memory"); // clobbered register " push %%ecx\n"
" popfl"
: "=m" (supported) // output
: // input
: "eax", "ecx", "memory"); // clobbered register
return supported != 0; return supported != 0;
#else #endif // NAZARA_PLATFORM_x64
return false; #else
#endif return false;
#endif #endif
} }