@@ -19,6 +19,33 @@ unsgined int query_processor_info(processor_info* pinfo)
1919
2020#undef _CPUID_DEBUG
2121
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+
2249DWORD countSetBits (ULONG_PTR bitMask)
2350{
2451 DWORD LSHIFT = sizeof (ULONG_PTR) * 8 - 1 ;
@@ -49,7 +76,7 @@ unsigned int query_processor_info(processor_info* pinfo)
4976 xr_vector<std::array<int , 4 >> data;
5077 std::array<int , 4 > cpui;
5178
52- __cpuid (cpui.data (), 0 );
79+ nativeCpuId (cpui.data (), 0 );
5380 const int nIds = cpui[0 ];
5481
5582 for (int i = 0 ; i <= nIds; ++i)
@@ -80,7 +107,7 @@ unsigned int query_processor_info(processor_info* pinfo)
80107 f_7_ECX = data[7][2];
81108 }*/
82109
83- __cpuid (cpui.data (), 0x80000000 );
110+ nativeCpuId (cpui.data (), 0x80000000 );
84111 const int nExIds_ = cpui[0 ];
85112 data.clear ();
86113
@@ -117,7 +144,7 @@ unsigned int query_processor_info(processor_info* pinfo)
117144 if (f_1_ECX[19 ]) pinfo->features |= static_cast <u32 >(CpuFeature::Sse41);
118145 if (f_1_ECX[20 ]) pinfo->features |= static_cast <u32 >(CpuFeature::Sse42);
119146
120- __cpuid (cpui.data (), 1 );
147+ nativeCpuId (cpui.data (), 1 );
121148
122149 const bool hasMWait = (cpui[2 ] & 0x8 ) > 0 ;
123150 if (hasMWait) pinfo->features |= static_cast <u32 >(CpuFeature::MWait);
@@ -127,8 +154,21 @@ unsigned int query_processor_info(processor_info* pinfo)
127154 pinfo->stepping = cpui[0 ] & 0xf ;
128155
129156 // Calculate available processors
157+ #ifdef WINDOWS
130158 ULONG_PTR pa_mask_save, sa_mask_stub = 0 ;
131159 GetProcessAffinityMask (GetCurrentProcess (), &pa_mask_save, &sa_mask_stub);
160+ #elif defined(LINUX)
161+ unsigned int pa_mask_save = 0 ;
162+ cpu_set_t my_set;
163+ CPU_ZERO (&my_set);
164+ sched_getaffinity (0 , sizeof (cpu_set_t ), &my_set);
165+ pa_mask_save = CPU_COUNT (&my_set);
166+ #else
167+ #warning "No Function to obtain process affinity"
168+ unsigned int pa_mask_save = 0 ;
169+ #endif // WINDOWS
170+
171+ #ifdef WINDOWS
132172
133173 DWORD returnedLength = 0 ;
134174 DWORD byteOffset = 0 ;
@@ -160,6 +200,34 @@ unsigned int query_processor_info(processor_info* pinfo)
160200 ptr++;
161201 }
162202
203+ #else // WINDOWS
204+
205+ int logicalProcessorCount = std::thread::hardware_concurrency ();
206+
207+ // not sure about processorCoreCount - is it really cores or threads
208+ // https://stackoverflow.com/questions/150355/programmatically-find-the-number-of-cores-on-a-machine
209+ int processorCoreCount = sysconf (_SC_NPROCESSORS_ONLN);
210+
211+ // 2nd implementation
212+ //
213+ // #include <hwloc.h>
214+ // // Allocate, initialize, and perform topology detection
215+ // hwloc_topology_t topology;
216+ // hwloc_topology_init(&topology);
217+ // hwloc_topology_load(topology);
218+ //
219+ // // Try to get the number of CPU cores from topology
220+ // int depth = hwloc_get_type_depth(topology, HWLOC_OBJ_CORE);
221+ // int processorCoreCount = hwloc_get_nbobjs_by_depth(topology, depth);
222+ //
223+ // // Destroy topology object and return
224+ // hwloc_topology_destroy(topology);
225+
226+ // 3rd another implementation
227+ // https://stackoverflow.com/questions/2901694/programmatically-detect-number-of-physical-processors-cores-or-if-hyper-threadin
228+
229+ #endif // WINDOWS
230+
163231 if (logicalProcessorCount != processorCoreCount) pinfo->features |= static_cast <u32 >(CpuFeature::HT);
164232
165233 // All logical processors
0 commit comments