Skip to content

Commit b4ae0d2

Browse files
committed
[GEN] Replace CPU profiling and CPU ID ASM with intrinsic functions #508
1 parent d448ab0 commit b4ae0d2

File tree

2 files changed

+19
-79
lines changed

2 files changed

+19
-79
lines changed

Diff for: Generals/Code/Libraries/Source/WWVegas/WWDebug/wwprofile.cpp

+2-12
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
#include "wwprofile.h"
5555
#include "wwdebug.h"
5656
#include <windows.h>
57+
#include <Utility/intrin_compat.h>
5758

5859

5960

@@ -74,18 +75,7 @@ inline void WWProfile_Get_Ticks(_int64 * ticks)
7475
#ifdef _UNIX
7576
*ticks = 0;
7677
#else
77-
__asm
78-
{
79-
push edx;
80-
push ecx;
81-
mov ecx,ticks;
82-
_emit 0Fh
83-
_emit 31h
84-
mov [ecx],eax;
85-
mov [ecx+4],edx;
86-
pop ecx;
87-
pop edx;
88-
}
78+
*ticks = _rdtsc();
8979
#endif
9080
}
9181

Diff for: Generals/Code/Libraries/Source/WWVegas/WWLib/cpudetect.cpp

+17-67
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#pragma warning (disable : 4201) // Nonstandard extension - nameless struct
2525
#include <windows.h>
2626
#include "systimer.h"
27+
#include <Utility/intrin_compat.h>
2728

2829
#ifdef _UNIX
2930
# include <time.h> // for time(), localtime() and timezone variable.
@@ -127,42 +128,18 @@ const char* CPUDetectClass::Get_Processor_Manufacturer_Name()
127128

128129
static unsigned Calculate_Processor_Speed(__int64& ticks_per_second)
129130
{
130-
struct {
131-
unsigned timer0_h;
132-
unsigned timer0_l;
133-
unsigned timer1_h;
134-
unsigned timer1_l;
135-
} Time;
131+
unsigned __int64 timer0=0;
132+
unsigned __int64 timer1=0;
136133

137-
#ifdef WIN32
138-
__asm {
139-
ASM_RDTSC;
140-
mov Time.timer0_h, eax
141-
mov Time.timer0_l, edx
142-
}
143-
#elif defined(_UNIX)
144-
__asm__("rdtsc");
145-
__asm__("mov %eax, __Time.timer1_h");
146-
__asm__("mov %edx, __Time.timer1_l");
147-
#endif
134+
timer0=_rdtsc();
148135

149136
unsigned start=TIMEGETTIME();
150137
unsigned elapsed;
151138
while ((elapsed=TIMEGETTIME()-start)<200) {
152-
#ifdef WIN32
153-
__asm {
154-
ASM_RDTSC;
155-
mov Time.timer1_h, eax
156-
mov Time.timer1_l, edx
157-
}
158-
#elif defined(_UNIX)
159-
__asm__ ("rdtsc");
160-
__asm__("mov %eax, __Time.timer1_h");
161-
__asm__("mov %edx, __Time.timer1_l");
162-
#endif
139+
timer1=_rdtsc();
163140
}
164141

165-
__int64 t=*(__int64*)&Time.timer1_h-*(__int64*)&Time.timer0_h;
142+
__int64 t=timer1-timer0;
166143
ticks_per_second=(__int64)((1000.0/(double)elapsed)*(double)t); // Ticks per second
167144
return unsigned((double)t/(double)(elapsed*1000));
168145
}
@@ -826,6 +803,7 @@ void CPUDetectClass::Init_CPUID_Instruction()
826803
// because CodeWarrior seems to have problems with
827804
// the command (huh?)
828805

806+
#if defined(_MSC_VER) && _MSC_VER < 1300
829807
#ifdef WIN32
830808
__asm
831809
{
@@ -868,6 +846,10 @@ void CPUDetectClass::Init_CPUID_Instruction()
868846
__asm__(" pop %ebx");
869847
#endif
870848
HasCPUIDInstruction=!!cpuid_available;
849+
#else
850+
// TheSuperHackers @info Mauller 30/3/2020 All modern CPUs have the CPUID instruction, VS22 code will not run on a cpu that doesn't.
851+
HasCPUIDInstruction = true;
852+
#endif // defined(_MSC_VER) && _MSC_VER < 1300
871853
}
872854

873855
void CPUDetectClass::Init_Processor_Features()
@@ -941,44 +923,12 @@ bool CPUDetectClass::CPUID(
941923
{
942924
if (!Has_CPUID_Instruction()) return false; // Most processors since 486 have CPUID...
943925

944-
unsigned u_eax;
945-
unsigned u_ebx;
946-
unsigned u_ecx;
947-
unsigned u_edx;
948-
949-
#ifdef WIN32
950-
__asm
951-
{
952-
pushad
953-
mov eax, [cpuid_type]
954-
xor ebx, ebx
955-
xor ecx, ecx
956-
xor edx, edx
957-
cpuid
958-
mov [u_eax], eax
959-
mov [u_ebx], ebx
960-
mov [u_ecx], ecx
961-
mov [u_edx], edx
962-
popad
963-
}
964-
#elif defined(_UNIX)
965-
__asm__("pusha");
966-
__asm__("mov __cpuid_type, %eax");
967-
__asm__("xor %ebx, %ebx");
968-
__asm__("xor %ecx, %ecx");
969-
__asm__("xor %edx, %edx");
970-
__asm__("cpuid");
971-
__asm__("mov %eax, __u_eax");
972-
__asm__("mov %ebx, __u_ebx");
973-
__asm__("mov %ecx, __u_ecx");
974-
__asm__("mov %edx, __u_edx");
975-
__asm__("popa");
976-
#endif
977-
978-
u_eax_=u_eax;
979-
u_ebx_=u_ebx;
980-
u_ecx_=u_ecx;
981-
u_edx_=u_edx;
926+
unsigned int regs[4];
927+
cpuid(regs, cpuid_type);
928+
u_eax_ = regs[0];
929+
u_ebx_ = regs[1];
930+
u_ecx_ = regs[2];
931+
u_edx_ = regs[3];
982932

983933
return true;
984934
}

0 commit comments

Comments
 (0)