Core: Rewrite CPUID to preserve EBX register (Fix #73)

Former-commit-id: 19651c4e8856df2e6a9a208b5dfab1dfdde80978 [formerly d7392176b9b510939aeefaf5c853d023af211379] [formerly 36c14e52fae5966508143704557610247aaf2e22 [formerly 1901b3100ddc4621685375de77dfb7863687d1b4]]
Former-commit-id: 53af7fc2cb9ee97f16cc03db53658385dc218b71 [formerly d43f598f33684430ab3a58db1533cb108be6e26a]
Former-commit-id: 5d88e9b7ef4bc7e05060ac06678d6c141d9a88a4
This commit is contained in:
Lynix 2016-10-03 19:05:50 +02:00
parent 1555e4ffd9
commit 30e07ea2e2
2 changed files with 36 additions and 12 deletions

View File

@ -11,10 +11,22 @@ namespace Nz
void HardwareInfoImpl::Cpuid(UInt32 functionId, UInt32 subFunctionId, UInt32 registers[4]) void HardwareInfoImpl::Cpuid(UInt32 functionId, UInt32 subFunctionId, UInt32 registers[4])
{ {
#if defined(NAZARA_COMPILER_CLANG) || defined(NAZARA_COMPILER_GCC) || defined(NAZARA_COMPILER_INTEL) #if defined(NAZARA_COMPILER_CLANG) || defined(NAZARA_COMPILER_GCC) || defined(NAZARA_COMPILER_INTEL)
// Source: http://stackoverflow.com/questions/1666093/cpuid-implementations-in-c // https://en.wikipedia.org/wiki/CPUID
asm volatile ("cpuid" // Besoin d'être volatile ? asm volatile(
: "=a" (registers[0]), "=b" (registers[1]), "=c" (registers[2]), "=d" (registers[3]) // output #ifdef NAZARA_PLATFORM_x64
: "a" (functionId), "c" (subFunctionId)); // input "pushq %%rbx \n\t" // save %rbx
#else
"pushl %%ebx \n\t" // save %ebx
#endif
"cpuid \n\t"
"movl %%ebx ,%[ebx] \n\t" // write the result into output var
#ifdef NAZARA_PLATFORM_x64
"popq %%rbx \n\t"
#else
"popl %%ebx \n\t"
#endif
: "=a"(registers[0]), [ebx] "=r"(registers[1]), "=c"(registers[2]), "=d"(registers[3])
: "a"(functionId), "c" (subFunctionId));
#else #else
NazaraInternalError("Cpuid has been called although it is not supported"); NazaraInternalError("Cpuid has been called although it is not supported");
#endif #endif
@ -22,7 +34,7 @@ namespace Nz
unsigned int HardwareInfoImpl::GetProcessorCount() unsigned int HardwareInfoImpl::GetProcessorCount()
{ {
// Plus simple (et plus portable) que de passer par le CPUID // Simpler (and more portable) than using CPUID
return sysconf(_SC_NPROCESSORS_CONF); return sysconf(_SC_NPROCESSORS_CONF);
} }
@ -37,7 +49,7 @@ namespace Nz
bool HardwareInfoImpl::IsCpuidSupported() bool HardwareInfoImpl::IsCpuidSupported()
{ {
#ifdef NAZARA_PLATFORM_x64 #ifdef NAZARA_PLATFORM_x64
return true; // Toujours supporté sur un processeur 64 bits return true; // cpuid is always supported on x64 arch
#else #else
#if defined(NAZARA_COMPILER_CLANG) || defined(NAZARA_COMPILER_GCC) || defined(NAZARA_COMPILER_INTEL) #if defined(NAZARA_COMPILER_CLANG) || defined(NAZARA_COMPILER_GCC) || defined(NAZARA_COMPILER_INTEL)
int supported; int supported;

View File

@ -22,10 +22,22 @@ namespace Nz
// Visual propose une fonction intrinsèque pour le cpuid // Visual propose une fonction intrinsèque pour le cpuid
__cpuidex(reinterpret_cast<int*>(registers), static_cast<int>(functionId), static_cast<int>(subFunctionId)); __cpuidex(reinterpret_cast<int*>(registers), static_cast<int>(functionId), static_cast<int>(subFunctionId));
#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 // https://en.wikipedia.org/wiki/CPUID
asm volatile ("cpuid" // Besoin d'être volatile ? asm volatile(
: "=a" (registers[0]), "=b" (registers[1]), "=c" (registers[2]), "=d" (registers[3]) // output #ifdef NAZARA_PLATFORM_x64
: "a" (functionId), "c" (subFunctionId)); // input "pushq %%rbx \n\t" // save %rbx
#else
"pushl %%ebx \n\t" // save %ebx
#endif
"cpuid \n\t"
"movl %%ebx ,%[ebx] \n\t" // write the result into output var
#ifdef NAZARA_PLATFORM_x64
"popq %%rbx \n\t"
#else
"popl %%ebx \n\t"
#endif
: "=a"(registers[0]), [ebx] "=r"(registers[1]), "=c"(registers[2]), "=d"(registers[3]) // output
: "a"(functionId), "c" (subFunctionId)); // 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
@ -33,7 +45,7 @@ namespace Nz
unsigned int HardwareInfoImpl::GetProcessorCount() unsigned int HardwareInfoImpl::GetProcessorCount()
{ {
// Plus simple (et plus portable) que de passer par le CPUID // Simpler (and more portable) than using CPUID
SYSTEM_INFO infos; SYSTEM_INFO infos;
GetNativeSystemInfo(&infos); GetNativeSystemInfo(&infos);
@ -52,7 +64,7 @@ namespace Nz
bool HardwareInfoImpl::IsCpuidSupported() bool HardwareInfoImpl::IsCpuidSupported()
{ {
#ifdef NAZARA_PLATFORM_x64 #ifdef NAZARA_PLATFORM_x64
return true; // Toujours supporté sur un processeur 64 bits return true; // cpuid is always supported on x64 arch
#else #else
#if defined(NAZARA_COMPILER_MSVC) #if defined(NAZARA_COMPILER_MSVC)
int supported; int supported;