Skip to content

Commit

Permalink
xrCore: detect cpuid feature from q4a
Browse files Browse the repository at this point in the history
  • Loading branch information
eagleivg committed May 24, 2018
1 parent 05ddf65 commit 1f8ead6
Showing 1 changed file with 49 additions and 6 deletions.
55 changes: 49 additions & 6 deletions src/xrCore/cpuid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,34 @@ unsgined int query_processor_info(processor_info* pinfo)

#undef _CPUID_DEBUG

#if defined(WINDOWS)
void nativeCpuId(int regs[4], int i)
{
#ifdef WINDOWS
__cpuid((int *)regs, (int)i);
#elif defined(LINUX) && defined(GCC)
__cpuid((int)i, (int *)regs);
#elif defined(LINUX)
asm volatile("cpuid" :
"=eax" (regs[0]),
"=ebx" (regs[1]),
"=ecx" (regs[2]),
"=edx" (regs[3])
: "eax" (i));
#else
#error Cpuid is not implemented
#endif
}

#ifndef WINDOWS
#include <thread>

void __cpuidex(int regs[4], int i, int j)
{
nativeCpuId(regs, i);
}
#endif

#ifdef WINDOWS
DWORD countSetBits(ULONG_PTR bitMask)
{
DWORD LSHIFT = sizeof(ULONG_PTR) * 8 - 1;
Expand All @@ -41,7 +68,6 @@ unsigned int query_processor_info(processor_info* pinfo)
{
ZeroMemory(pinfo, sizeof(processor_info));

#if defined(WINDOWS)
std::bitset<32> f_1_ECX;
std::bitset<32> f_1_EDX;
/*std::bitset<32> f_7_EBX;
Expand All @@ -52,7 +78,7 @@ unsigned int query_processor_info(processor_info* pinfo)
xr_vector<std::array<int, 4>> data;
std::array<int, 4> cpui;

__cpuid(cpui.data(), 0);
nativeCpuId(cpui.data(), 0);
const int nIds = cpui[0];

for (int i = 0; i <= nIds; ++i)
Expand Down Expand Up @@ -83,7 +109,7 @@ unsigned int query_processor_info(processor_info* pinfo)
f_7_ECX = data[7][2];
}*/

__cpuid(cpui.data(), 0x80000000);
nativeCpuId(cpui.data(), 0x80000000);
const int nExIds_ = cpui[0];
data.clear();

Expand Down Expand Up @@ -120,7 +146,7 @@ unsigned int query_processor_info(processor_info* pinfo)
if (f_1_ECX[19]) pinfo->features |= static_cast<u32>(CpuFeature::Sse41);
if (f_1_ECX[20]) pinfo->features |= static_cast<u32>(CpuFeature::Sse42);

__cpuid(cpui.data(), 1);
nativeCpuId(cpui.data(), 1);

const bool hasMWait = (cpui[2] & 0x8) > 0;
if (hasMWait) pinfo->features |= static_cast<u32>(CpuFeature::MWait);
Expand All @@ -130,9 +156,21 @@ unsigned int query_processor_info(processor_info* pinfo)
pinfo->stepping = cpui[0] & 0xf;

// Calculate available processors
#ifdef WINDOWS
ULONG_PTR pa_mask_save, sa_mask_stub = 0;
GetProcessAffinityMask(GetCurrentProcess(), &pa_mask_save, &sa_mask_stub);
#elif defined(LINUX)
unsigned int pa_mask_save = 0;
cpu_set_t my_set;
CPU_ZERO(&my_set);
sched_getaffinity(0, sizeof(cpu_set_t), &my_set);
pa_mask_save = CPU_COUNT(&my_set);
#else
#warning "No Function to obtain process affinity"
unsigned int pa_mask_save = 0;
#endif // WINDOWS

#ifdef WINDOWS
DWORD returnedLength = 0;
DWORD byteOffset = 0;
GetLogicalProcessorInformation(nullptr, &returnedLength);
Expand Down Expand Up @@ -162,14 +200,19 @@ unsigned int query_processor_info(processor_info* pinfo)
byteOffset += sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION);
ptr++;
}
#elif defined(LINUX)
int logicalProcessorCount = std::thread::hardware_concurrency();

int processorCoreCount = sysconf(_SC_NPROCESSORS_ONLN);
#endif


if (logicalProcessorCount != processorCoreCount) pinfo->features |= static_cast<u32>(CpuFeature::HT);

// All logical processors
pinfo->n_threads = logicalProcessorCount;
pinfo->affinity_mask = pa_mask_save;
pinfo->n_cores = processorCoreCount;
#endif

return pinfo->features;
}
Expand Down

0 comments on commit 1f8ead6

Please sign in to comment.