Skip to content

Commit 1f8ead6

Browse files
committed
xrCore: detect cpuid feature from q4a
1 parent 05ddf65 commit 1f8ead6

File tree

1 file changed

+49
-6
lines changed

1 file changed

+49
-6
lines changed

src/xrCore/cpuid.cpp

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,34 @@ unsgined int query_processor_info(processor_info* pinfo)
1919

2020
#undef _CPUID_DEBUG
2121

22-
#if defined(WINDOWS)
22+
void nativeCpuId(int regs[4], int i)
23+
{
24+
#ifdef WINDOWS
25+
__cpuid((int *)regs, (int)i);
26+
#elif defined(LINUX) && defined(GCC)
27+
__cpuid((int)i, (int *)regs);
28+
#elif defined(LINUX)
29+
asm volatile("cpuid" :
30+
"=eax" (regs[0]),
31+
"=ebx" (regs[1]),
32+
"=ecx" (regs[2]),
33+
"=edx" (regs[3])
34+
: "eax" (i));
35+
#else
36+
#error Cpuid is not implemented
37+
#endif
38+
}
39+
40+
#ifndef WINDOWS
41+
#include <thread>
42+
43+
void __cpuidex(int regs[4], int i, int j)
44+
{
45+
nativeCpuId(regs, i);
46+
}
47+
#endif
48+
49+
#ifdef WINDOWS
2350
DWORD countSetBits(ULONG_PTR bitMask)
2451
{
2552
DWORD LSHIFT = sizeof(ULONG_PTR) * 8 - 1;
@@ -41,7 +68,6 @@ unsigned int query_processor_info(processor_info* pinfo)
4168
{
4269
ZeroMemory(pinfo, sizeof(processor_info));
4370

44-
#if defined(WINDOWS)
4571
std::bitset<32> f_1_ECX;
4672
std::bitset<32> f_1_EDX;
4773
/*std::bitset<32> f_7_EBX;
@@ -52,7 +78,7 @@ unsigned int query_processor_info(processor_info* pinfo)
5278
xr_vector<std::array<int, 4>> data;
5379
std::array<int, 4> cpui;
5480

55-
__cpuid(cpui.data(), 0);
81+
nativeCpuId(cpui.data(), 0);
5682
const int nIds = cpui[0];
5783

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

86-
__cpuid(cpui.data(), 0x80000000);
112+
nativeCpuId(cpui.data(), 0x80000000);
87113
const int nExIds_ = cpui[0];
88114
data.clear();
89115

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

123-
__cpuid(cpui.data(), 1);
149+
nativeCpuId(cpui.data(), 1);
124150

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

132158
// Calculate available processors
159+
#ifdef WINDOWS
133160
ULONG_PTR pa_mask_save, sa_mask_stub = 0;
134161
GetProcessAffinityMask(GetCurrentProcess(), &pa_mask_save, &sa_mask_stub);
162+
#elif defined(LINUX)
163+
unsigned int pa_mask_save = 0;
164+
cpu_set_t my_set;
165+
CPU_ZERO(&my_set);
166+
sched_getaffinity(0, sizeof(cpu_set_t), &my_set);
167+
pa_mask_save = CPU_COUNT(&my_set);
168+
#else
169+
#warning "No Function to obtain process affinity"
170+
unsigned int pa_mask_save = 0;
171+
#endif // WINDOWS
135172

173+
#ifdef WINDOWS
136174
DWORD returnedLength = 0;
137175
DWORD byteOffset = 0;
138176
GetLogicalProcessorInformation(nullptr, &returnedLength);
@@ -162,14 +200,19 @@ unsigned int query_processor_info(processor_info* pinfo)
162200
byteOffset += sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION);
163201
ptr++;
164202
}
203+
#elif defined(LINUX)
204+
int logicalProcessorCount = std::thread::hardware_concurrency();
205+
206+
int processorCoreCount = sysconf(_SC_NPROCESSORS_ONLN);
207+
#endif
208+
165209

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

168212
// All logical processors
169213
pinfo->n_threads = logicalProcessorCount;
170214
pinfo->affinity_mask = pa_mask_save;
171215
pinfo->n_cores = processorCoreCount;
172-
#endif
173216

174217
return pinfo->features;
175218
}

0 commit comments

Comments
 (0)