Skip to content

Commit

Permalink
thcrap: Tweaks to CPU features
Browse files Browse the repository at this point in the history
  • Loading branch information
zero318 committed Aug 29, 2023
1 parent eb1fd29 commit ad91f48
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 18 deletions.
59 changes: 56 additions & 3 deletions thcrap/src/expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ struct CPUID_Data_t {
bool HasPKU = false;
bool HasWAITPKG = false;
bool HasAVX512VBMI2 = false;
bool HasSHSTK = false;
bool HasGFNI = false;
bool HasVAES = false;
bool HasVPCLMULQDQ = false;
Expand All @@ -141,11 +142,25 @@ struct CPUID_Data_t {
bool HasAVX512FP16 = false;
bool HasAMXTILE = false;
bool HasAMXINT8 = false;
bool HasSHA512 = false;
bool HasSM3 = false;
bool HasSM4 = false;
bool HasRAOINT = false;
bool HasAVXVNNI = false;
bool HasAVX512BF16 = false;
bool HasCMPCCXADD = false;
bool HasFRMB0 = false;
bool HasFRSB = false;
bool HasFRCSB = false;
bool HasAMXFP16 = false;
bool HasAVXIFMA = false;
bool HasAVXVNNIINT8 = false;
bool HasAVXNECONVERT = false;
bool HasAMXCOMPLEX = false;
bool HasAVXVNNIINT16 = false;
bool HasPREFETCHI = false;
bool HasAVX10 = false;
bool HasAPXF = false;
bool HasXSAVEOPT = false;
bool HasXSAVEC = false;
bool HasSYSCALL = false;
Expand Down Expand Up @@ -253,6 +268,7 @@ struct CPUID_Data_t {
HasPKU = bittest32(data[2], 3) & bittest32(data[2], 4);
HasWAITPKG = bittest32(data[2], 5);
HasAVX512VBMI2 = bittest32(data[2], 6);
HasSHSTK = bittest32(data[2], 7);
HasGFNI = bittest32(data[2], 8);
HasVAES = bittest32(data[2], 9);
HasVPCLMULQDQ = bittest32(data[2], 10);
Expand All @@ -278,11 +294,25 @@ struct CPUID_Data_t {
switch (data[0]) {
default: //case 1:
__cpuidex(data, 7, 1);
HasSHA512 = bittest32(data[0], 0);
HasSM3 = bittest32(data[0], 1);
HasSM4 = bittest32(data[0], 2);
HasRAOINT = bittest32(data[0], 3);
HasAVXVNNI = bittest32(data[0], 4);
HasAVX512BF16 = bittest32(data[0], 5);
HasCMPCCXADD = bittest32(data[0], 7);
HasFRMB0 = bittest32(data[0], 10);
HasFRSB = bittest32(data[0], 11);
HasFRCSB = bittest32(data[0], 12);
HasAMXFP16 = bittest32(data[0], 21);
HasAVXIFMA = bittest32(data[0], 23);
HasAVXVNNIINT8 = bittest32(data[3], 4);
HasAVXNECONVERT = bittest32(data[3], 5);
HasAMXCOMPLEX = bittest32(data[3], 8);
HasAVXVNNIINT16 = bittest32(data[3], 10);
HasPREFETCHI = bittest32(data[3], 14);
HasAVX10 = bittest32(data[3], 19);
HasAPXF = bittest32(data[3], 21);
case 0:;
}
case 6: case 5: case 4: case 3: case 2:
Expand All @@ -294,7 +324,11 @@ struct CPUID_Data_t {
__cpuid(data, 0x80000000);
if ((uint32_t)data[0] > 0x80000000) {
switch (data[0]) {
default: // case 0x8000001C:
default: // case 0x80000021:
__cpuid(data, 0x80000021);
HasFRSB |= bittest32(data[0], 10);
HasFRCSB |= bittest32(data[0], 11);
case 0x80000020: case 0x8000001F: case 0x8000001E: case 0x8000001D: case 0x8000001C:
__cpuid(data, 0x8000001C);
HasLWPVAL = bittest32(data[0], 1);
case 0x8000001B: case 0x8000001A: case 0x80000019: case 0x80000018: case 0x80000017: case 0x80000016:
Expand All @@ -309,6 +343,7 @@ struct CPUID_Data_t {
__cpuid(data, 0x80000001);
HasSYSCALL = bittest32(data[3], 11);
HasMMXEXT = bittest32(data[3], 22);
HasRDTSCP = bittest32(data[3], 27);
Has3DNOWEXT = bittest32(data[3], 30);
Has3DNOW = bittest32(data[3], 31);
HasLMLSAHF = bittest32(data[2], 0);
Expand Down Expand Up @@ -1125,11 +1160,14 @@ static TH_NOINLINE uint32_t GetCPUFeatureTest(const char* name, size_t name_leng
if (strnicmp(name, "avx512bitalg", name_length) == 0) return CPUID_Data.HasAVX512BITALG;
else if (strnicmp(name, "avx5124fmaps", name_length) == 0) return CPUID_Data.HasAVX5124FMAPS;
else if (strnicmp(name, "avx5124vnniw", name_length) == 0) return CPUID_Data.HasAVX5124VNNIW;
else if (strnicmp(name, "avxvnniint16", name_length) == 0) return CPUID_Data.HasAVXVNNIINT16;
else if (strnicmp(name, "avxneconvert", name_length) == 0) return CPUID_Data.HasAVXNECONVERT;
else goto InvalidCPUFeatureError;
break;
case 11:
if (strnicmp(name, "avx512vbmi1", name_length) == 0) return CPUID_Data.HasAVX512VBMI;
else if (strnicmp(name, "avx512vbmi2", name_length) == 0) return CPUID_Data.HasAVX512VBMI2;
else if (strnicmp(name, "avxvnniint8", name_length) == 0) return CPUID_Data.HasAVXVNNIINT8;
else if (strnicmp(name, "prefetchwt1", name_length) == 0) return CPUID_Data.HasPREFETCHWT1;
else goto InvalidCPUFeatureError;
break;
Expand All @@ -1143,13 +1181,16 @@ static TH_NOINLINE uint32_t GetCPUFeatureTest(const char* name, size_t name_leng
else if (strnicmp(name, "clflushopt", name_length) == 0) return CPUID_Data.HasCLFLUSHOPT;
else if (strnicmp(name, "avx512vnni", name_length) == 0) return CPUID_Data.HasAVX512VNNI;
else if (strnicmp(name, "avx512fp16", name_length) == 0) return CPUID_Data.HasAVX512FP16;
else if (strnicmp(name, "amxcomplex", name_length) == 0) return CPUID_Data.HasAMXCOMPLEX;
else goto InvalidCPUFeatureError;
break;
case 9:
if (strnicmp(name, "thcrapver", name_length) == 0) return PROJECT_VERSION;
else if (strnicmp(name, "pclmulqdq", name_length) == 0) return CPUID_Data.HasPCLMULQDQ;
else if (strnicmp(name, "movdir64b", name_length) == 0) return CPUID_Data.HasMOVDIR64B;
else if (strnicmp(name, "cmpccxadd", name_length) == 0) return CPUID_Data.HasCMPCCXADD;
else if (strnicmp(name, "prefetchw", name_length) == 0) return CPUID_Data.HasPREFETCHW;
else if (strnicmp(name, "prefetchi", name_length) == 0) return CPUID_Data.HasPREFETCHI;
else if (strnicmp(name, "serialize", name_length) == 0) return CPUID_Data.HasSERIALIZE;
else goto InvalidCPUFeatureError;
break;
Expand All @@ -1172,13 +1213,15 @@ static TH_NOINLINE uint32_t GetCPUFeatureTest(const char* name, size_t name_leng
case 7:
if (strnicmp(name, "waitpkg", name_length) == 0) return CPUID_Data.HasWAITPKG;
else if (strnicmp(name, "movdiri", name_length) == 0) return CPUID_Data.HasMOVDIRI;
else if (strnicmp(name, "avxifma", name_length) == 0) return CPUID_Data.HasAVXIFMA;
else if (strnicmp(name, "avx512f", name_length) == 0) return CPUID_Data.HasAVX512F;
else if (strnicmp(name, "mxcsrmm", name_length) == 0) return CPUID_Data.HasMXCSRMM;
else if (strnicmp(name, "lmlsahf", name_length) == 0) return CPUID_Data.HasLMLSAHF;
else if (strnicmp(name, "clflush", name_length) == 0) return CPUID_Data.HasCLFLUSH;
else if (strnicmp(name, "amxint8", name_length) == 0) return CPUID_Data.HasAMXINT8;
else if (strnicmp(name, "amxtile", name_length) == 0) return CPUID_Data.HasAMXTILE;
else if (strnicmp(name, "amxfp16", name_length) == 0) return CPUID_Data.HasAMXBF16;
else if (strnicmp(name, "amxbf16", name_length) == 0) return CPUID_Data.HasAMXBF16;
else if (strnicmp(name, "amxfp16", name_length) == 0) return CPUID_Data.HasAMXFP16;
else if (strnicmp(name, "avxvnni", name_length) == 0) return CPUID_Data.HasAVXVNNI;
else if (strnicmp(name, "mcommit", name_length) == 0) return CPUID_Data.HasMCOMMIT;
else if (strnicmp(name, "syscall", name_length) == 0) return CPUID_Data.HasSYSCALL;
Expand All @@ -1187,6 +1230,8 @@ static TH_NOINLINE uint32_t GetCPUFeatureTest(const char* name, size_t name_leng
case 6:
if (strnicmp(name, "winver", name_length) == 0) return CPUID_Data.WindowsVersion.raw;
else if (strnicmp(name, "popcnt", name_length) == 0) return CPUID_Data.HasPOPCNT;
else if (strnicmp(name, "sha512", name_length) == 0) return CPUID_Data.HasSHA512;
else if (strnicmp(name, "raoint", name_length) == 0) return CPUID_Data.HasRAOINT;
else if (strnicmp(name, "rdtscp", name_length) == 0) return CPUID_Data.HasRDTSCP;
else if (strnicmp(name, "xsavec", name_length) == 0) return CPUID_Data.HasXSAVEC;
else if (strnicmp(name, "fxsave", name_length) == 0) return CPUID_Data.HasFXSAVE;
Expand All @@ -1206,7 +1251,9 @@ static TH_NOINLINE uint32_t GetCPUFeatureTest(const char* name, size_t name_leng
else if (strnicmp(name, "sse42", name_length) == 0) return CPUID_Data.HasSSE42;
else if (strnicmp(name, "sse4a", name_length) == 0) return CPUID_Data.HasSSE4A;
else if (strnicmp(name, "movbe", name_length) == 0) return CPUID_Data.HasMOVBE;
else if (strnicmp(name, "avx10", name_length) == 0) return CPUID_Data.HasAVX10;
else if (strnicmp(name, "xsave", name_length) == 0) return CPUID_Data.HasXSAVE;
else if (strnicmp(name, "shstk", name_length) == 0) return CPUID_Data.HasSHSTK;
else if (strnicmp(name, "model", name_length) == 0) return CPUID_Data.FamilyData.raw;
else if (strnicmp(name, "3dnow", name_length) == 0) return CPUID_Data.Has3DNOW;
else if (strnicmp(name, "frmb0", name_length) == 0) return CPUID_Data.HasFRMB0;
Expand All @@ -1225,6 +1272,7 @@ static TH_NOINLINE uint32_t GetCPUFeatureTest(const char* name, size_t name_leng
else if (strnicmp(name, "fma4", name_length) == 0) return CPUID_Data.HasFMA4;
else if (strnicmp(name, "vaes", name_length) == 0) return CPUID_Data.HasVAES;
else if (strnicmp(name, "wine", name_length) == 0) return wine_version != NULL;
else if (strnicmp(name, "apxf", name_length) == 0) return CPUID_Data.HasAPXF;
else if (strnicmp(name, "cmov", name_length) == 0) return CPUID_Data.HasCMOV;
else if (strnicmp(name, "sse2", name_length) == 0) return CPUID_Data.HasSSE2;
else if (strnicmp(name, "f16c", name_length) == 0) return CPUID_Data.HasF16C;
Expand All @@ -1249,6 +1297,8 @@ static TH_NOINLINE uint32_t GetCPUFeatureTest(const char* name, size_t name_leng
else if (strnicmp(name, "tbm", name_length) == 0) return CPUID_Data.HasTBM;
else if (strnicmp(name, "sse", name_length) == 0) return CPUID_Data.HasSSE;
else if (strnicmp(name, "mmx", name_length) == 0) return CPUID_Data.HasMMX;
else if (strnicmp(name, "sm3", name_length) == 0) return CPUID_Data.HasSM3;
else if (strnicmp(name, "sm4", name_length) == 0) return CPUID_Data.HasSM4;
else if (strnicmp(name, "lwp", name_length) == 0) return CPUID_Data.HasLWP;
else if (strnicmp(name, "cet", name_length) == 0) return CPUID_Data.HasCET;
else if (strnicmp(name, "mpx", name_length) == 0) return CPUID_Data.HasMPX;
Expand Down Expand Up @@ -1427,7 +1477,7 @@ static TH_NOINLINE const char* get_patch_value_impl(const char* expr, patch_val_
switch (*++expr) {
case 'c':
if (strnicmp(expr, "codecave:", 9) == 0) {
out->type = PVT_POINTER;
out->type = is_relative ? PVT_DWORD : PVT_POINTER; // Relative offsets can only be 32 bits
out->p = GetCodecaveAddress(expr, PtrDiffStrlen(patch_val_end, expr), is_relative, data_refs);
return patch_val_end + 1;
}
Expand Down Expand Up @@ -1510,6 +1560,9 @@ static inline const char* CheckCastType(const char* expr, uint8_t* out) {
switch (*expr++ & 0xDF) {
default:
return NULL;
case 'P':
type = PVT_POINTER;
break;
case 'I':
++type;
case 'U':
Expand Down
29 changes: 14 additions & 15 deletions thcrap/src/str_to_addr.asm
Original file line number Diff line number Diff line change
Expand Up @@ -40,32 +40,31 @@
MOV EBX, 0x0A01
MOV ESI, EDX
MOVZX ECX, BYTE PTR [EDX]
LEA EAX, [ECX-0x31]
LEA EAX, [ECX-'1']
CMP AL, 9
JB is_base_ten
CMP CL, 0x30
CMP CL, '0'
JE is_leading_zero
OR CL, 0x20
CMP CL, 0x72
CMP CL, 'r'
JNE failA
XOR BL, BL
is_leading_zero:
MOVZX ECX, BYTE PTR [EDX+1]
INC ESI
LEA EAX, [ECX-0x30]
LEA EAX, [ECX-'0']
CMP AL, 10
JB is_base_ten
MOV BH, 16
OR CL, 0x20
CMP CL, 0x78
JE is_hex
CMP CL, 0x62
JE is_binary
TEST BL, BL /* Make sure that a single 0 is valid but a single R is not */
JZ failA
is_binary:
CMP CL, 'x'
JE is_zero_prefix
MOV BH, 2
is_hex:
CMP CL, 'b'
JE is_zero_prefix
TEST BL, BL /* Make sure that a single 0 is valid but a single R is not */
JZ failA
is_zero_prefix:
MOVZX ECX, BYTE PTR [EDX+2]
INC ESI
is_base_ten:
Expand All @@ -77,13 +76,13 @@ is_base_ten:
MOV EDI, EDX
XOR EAX, EAX
digit_loop: /* This loop is aligned despite having no padding */
ADD CL, 0xD0
ADD CL, -'0'
CMP CL, 10
JB is_decimal_digit
ADD CL, 0xEF
ADD CL, -('A'-'0')
CMP CL, 6
JB is_upper_hex_digit
ADD CL, 0xE0
ADD CL, -('a'-'A')
CMP CL, 6
JAE end_parse
is_upper_hex_digit:
Expand Down

0 comments on commit ad91f48

Please # to comment.