Skip to content

Commit c115e91

Browse files
authored
[GEN] Replace inline assembler for non VS6 builds (#508)
1 parent d27dce7 commit c115e91

File tree

12 files changed

+77
-119
lines changed

12 files changed

+77
-119
lines changed

Generals/Code/GameEngine/Include/Common/PerfTimer.h

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
#ifndef __PERFTIMER_H__
3232
#define __PERFTIMER_H__
3333

34+
#include "Utility/intrin_compat.h"
35+
3436
#if defined(_DEBUG) || defined(_INTERNAL)
3537
/*
3638
NOTE NOTE NOTE: never check this in with this enabled, since there is a nonzero time penalty
@@ -71,16 +73,7 @@ __forceinline void GetPrecisionTimer(Int64* t)
7173
#ifdef USE_QPF
7274
QueryPerformanceCounter((LARGE_INTEGER*)t);
7375
#else
74-
// CPUID is needed to force serialization of any previous instructions.
75-
__asm
76-
{
77-
// for now, I am commenting this out. It throws the timings off a bit more (up to .001%) jkmcd
78-
// CPUID
79-
RDTSC
80-
MOV ECX,[t]
81-
MOV [ECX], EAX
82-
MOV [ECX+4], EDX
83-
}
76+
*t = _rdtsc();
8477
#endif
8578
}
8679
#endif

Generals/Code/Libraries/Include/Lib/BaseType.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,10 +179,14 @@ __forceinline long fast_float2long_round(float f)
179179
{
180180
long i;
181181

182+
#if defined(_MSC_VER) && _MSC_VER < 1300
182183
__asm {
183184
fld [f]
184185
fistp [i]
185186
}
187+
#else
188+
i = lroundf(f);
189+
#endif
186190

187191
return i;
188192
}

Generals/Code/Libraries/Source/WWVegas/WW3D2/dx8wrapper.h

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,7 @@ WWINLINE unsigned int DX8Wrapper::Convert_Color(const Vector4& color)
750750

751751
WWINLINE unsigned int DX8Wrapper::Convert_Color(const Vector3& color,float alpha)
752752
{
753+
#if defined(_MSC_VER) && _MSC_VER < 1300
753754
const float scale = 255.0;
754755
unsigned int col;
755756

@@ -816,24 +817,21 @@ WWINLINE unsigned int DX8Wrapper::Convert_Color(const Vector3& color,float alpha
816817
mov col,eax
817818
}
818819
return col;
820+
#else
821+
return color.Convert_To_ARGB(alpha);
822+
#endif // defined(_MSC_VER) && _MSC_VER < 1300
819823
}
820824

821825
// ----------------------------------------------------------------------------
822826
//
823-
// Clamp color vertor to [0...1] range
827+
// Clamp color vector to [0...1] range
824828
//
825829
// ----------------------------------------------------------------------------
826830

827831
WWINLINE void DX8Wrapper::Clamp_Color(Vector4& color)
828832
{
829-
if (!CPUDetectClass::Has_CMOV_Instruction()) {
830-
for (int i=0;i<4;++i) {
831-
float f=(color[i]<0.0f) ? 0.0f : color[i];
832-
color[i]=(f>1.0f) ? 1.0f : f;
833-
}
834-
return;
835-
}
836-
833+
#if defined(_MSC_VER) && _MSC_VER < 1300
834+
if (CPUDetectClass::Has_CMOV_Instruction()) {
837835
__asm
838836
{
839837
mov esi,dword ptr color
@@ -876,6 +874,14 @@ WWINLINE void DX8Wrapper::Clamp_Color(Vector4& color)
876874
cmovnb edi,edx
877875
mov dword ptr[esi+12],edi
878876
}
877+
return;
878+
}
879+
#endif // defined(_MSC_VER) && _MSC_VER < 1300
880+
881+
for (int i=0;i<4;++i) {
882+
float f=(color[i]<0.0f) ? 0.0f : color[i];
883+
color[i]=(f>1.0f) ? 1.0f : f;
884+
}
879885
}
880886

881887
// ----------------------------------------------------------------------------

Generals/Code/Libraries/Source/WWVegas/WWDebug/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,5 @@ target_sources(g_wwdebug PRIVATE ${WWDEBUG_SRC})
1717

1818
target_link_libraries(g_wwdebug PRIVATE
1919
g_wwcommon
20+
gi_libraries_include
2021
)

Generals/Code/Libraries/Source/WWVegas/WWDebug/wwdebug.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@
4141
#ifndef WWDEBUG_H
4242
#define WWDEBUG_H
4343

44+
// TheSuperHackers @todo Recover WWDEBUG?
45+
#ifdef WWDEBUG
46+
#include <Utility/intrin_compat.h>
47+
#endif
48+
4449
// The macro MESSAGE allows user to put:
4550
// #pragma MESSAGE("Hello world")
4651
// anywhere in a source file. The message:
@@ -139,9 +144,9 @@ void WWDebug_DBWin32_Message_Handler( const char * message);
139144
** the debugger...
140145
*/
141146
#ifdef WWDEBUG
142-
#define WWDEBUG_BREAK _asm int 0x03
147+
# define WWDEBUG_BREAK __debugbreak();
143148
#else
144-
#define WWDEBUG_BREAK _asm int 0x03
149+
#define WWDEBUG_BREAK
145150
#endif
146151

147152
/*

Generals/Code/Libraries/Source/WWVegas/WWDebug/wwprofile.cpp

Lines changed: 2 additions & 12 deletions
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

Generals/Code/Libraries/Source/WWVegas/WWLib/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,4 +196,5 @@ target_sources(g_wwlib PRIVATE ${WWLIB_SRC})
196196

197197
target_link_libraries(g_wwlib PRIVATE
198198
g_wwcommon
199+
gi_libraries_include
199200
)

Generals/Code/Libraries/Source/WWVegas/WWLib/cpudetect.cpp

Lines changed: 17 additions & 67 deletions
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
}

Generals/Code/Libraries/Source/WWVegas/WWLib/lcw.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ int LCW_Uncomp(void const * source, void * dest, unsigned long )
168168
}
169169

170170

171-
#if defined(_MSC_VER)
171+
#if defined(_MSC_VER) && defined(_M_IX86)
172172

173173

174174
/***********************************************************************************************
@@ -439,6 +439,6 @@ int LCW_Comp(void const * source, void * dest, int datasize)
439439
#endif
440440
return(retval);
441441
}
442-
#endif
442+
#endif // defined(_MSC_VER) && defined(_M_IX86)
443443

444444

Generals/Code/Libraries/Source/WWVegas/WWLib/mpu.cpp

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#include "MPU.H"
4141
#include "math.h"
4242
#include <assert.h>
43+
#include <Utility/intrin_compat.h>
4344

4445
typedef union {
4546
LARGE_INTEGER LargeInt;
@@ -88,12 +89,11 @@ unsigned long Get_CPU_Clock(unsigned long & high)
8889
{
8990
int h;
9091
int l;
91-
__asm {
92-
_emit 0Fh
93-
_emit 31h
94-
mov [h],edx
95-
mov [l],eax
96-
}
92+
93+
auto tsc = _rdtsc();
94+
h = tsc >> 32;
95+
l = tsc & 0xFFFFFFFF;
96+
9797
high = h;
9898
return(l);
9999
}
@@ -126,12 +126,9 @@ static unsigned long TSC_High;
126126

127127
void RDTSC(void)
128128
{
129-
_asm
130-
{
131-
ASM_RDTSC;
132-
mov TSC_Low, eax
133-
mov TSC_High, edx
134-
}
129+
auto TSC = _rdtsc();
130+
TSC_Low = TSC & 0xFFFFFFFF;
131+
TSC_High = TSC >> 32;
135132
}
136133

137134

@@ -197,8 +194,7 @@ int Get_RDTSC_CPU_Speed(void)
197194
QueryPerformanceCounter(&t1);
198195
}
199196

200-
ASM_RDTSC;
201-
_asm mov stamp0, EAX
197+
stamp0 = _rdtsc();
202198

203199
t0.LowPart = t1.LowPart; // Reset Initial Time
204200
t0.HighPart = t1.HighPart;
@@ -211,9 +207,7 @@ int Get_RDTSC_CPU_Speed(void)
211207
QueryPerformanceCounter(&t1);
212208
}
213209

214-
ASM_RDTSC;
215-
_asm mov stamp1, EAX
216-
210+
stamp1 = _rdtsc();
217211

218212
cycles = stamp1 - stamp0; // # of cycles passed between reads
219213

0 commit comments

Comments
 (0)