Skip to content

Commit 1e78953

Browse files
committed
deduplication in CPUDetect logic
1 parent a072cca commit 1e78953

5 files changed

Lines changed: 37 additions & 84 deletions

File tree

HashLib/src/Include/Simd/CpuDetect/CpuIdQuery.inc

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,26 @@
11
// CPUID query: executes CPUID with leaf=ALeaf, subleaf=ASubLeaf,
22
// stores EAX/EBX/ECX/EDX into the TCpuIdResult record at AResult.
3-
// RBX must be preserved (CPUID clobbers it).
4-
// Expects MS x64 ABI: ecx = ALeaf, edx = ASubLeaf, r8 = AResult.
5-
// On FPC non-Windows (System V ABI), remaps edi,esi,rdx -> ecx,edx,r8.
3+
// IA-32 (HASHLIB_I386_ASM): preserves EBX; ESI = AResult, ECX = ASubLeaf, EAX = ALeaf.
4+
// x86-64: preserves RBX; MS ABI ecx=ALeaf, edx=ASubLeaf, r8=AResult;
5+
// FPC non-Windows remaps edi, esi, rdx -> leaf, subleaf, result.
66
{$IFDEF FPC}
77
assembler; nostackframe;
8+
{$ENDIF}
89
asm
10+
{$IFDEF HASHLIB_I386_ASM}
11+
push ebx
12+
push esi
13+
mov esi, ecx
14+
mov ecx, edx
15+
cpuid
16+
mov [esi], eax
17+
mov [esi + 4], ebx
18+
mov [esi + 8], ecx
19+
mov [esi + 12], edx
20+
pop esi
21+
pop ebx
22+
{$ELSE}
23+
{$IFDEF FPC}
924
push rbx
1025
{$IFDEF MSWINDOWS}
1126
mov eax, ecx
@@ -26,8 +41,7 @@ asm
2641
mov dword ptr [r8 + 12], edx
2742
{$ENDIF}
2843
pop rbx
29-
{$ELSE}
30-
asm
44+
{$ELSE}
3145
.PUSHNV RBX
3246
mov eax, ecx
3347
mov ecx, edx
@@ -36,4 +50,5 @@ asm
3650
mov dword ptr [r8 + 4], ebx
3751
mov dword ptr [r8 + 8], ecx
3852
mov dword ptr [r8 + 12], edx
53+
{$ENDIF}
3954
{$ENDIF}

HashLib/src/Include/Simd/CpuDetect/CpuIdQuery_i386.inc

Lines changed: 0 additions & 35 deletions
This file was deleted.
Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,36 @@
11
// XGETBV query: executes XGETBV with ECX=0, stores EAX:EDX into the
22
// UInt64 at AResult.
3-
// Expects MS x64 ABI: rcx = AResult.
4-
// On FPC non-Windows (System V ABI), remaps rdi -> rcx.
3+
// IA-32: AResult in EAX at asm entry (Windows and Unix).
4+
// x86-64: MS ABI rcx=AResult; FPC non-Windows remaps rdi -> r8.
55
{$IFDEF FPC}
66
assembler; nostackframe;
7+
{$ENDIF}
78
asm
9+
{$IFDEF HASHLIB_I386_ASM}
10+
push ebx
11+
mov ebx, eax
12+
xor ecx, ecx
13+
db $0F, $01, $D0 // xgetbv
14+
mov [ebx], eax
15+
mov [ebx + 4], edx
16+
pop ebx
17+
{$ELSE}
18+
{$IFDEF FPC}
819
{$IFDEF MSWINDOWS}
920
mov r8, rcx
1021
{$ELSE}
1122
mov r8, rdi
1223
{$ENDIF}
1324
xor ecx, ecx
14-
xgetbv
25+
db $0F, $01, $D0 // xgetbv
1526
mov dword ptr [r8], eax
1627
mov dword ptr [r8 + 4], edx
17-
{$ELSE}
18-
asm
28+
{$ELSE}
1929
.noframe
2030
mov r8, rcx
2131
xor ecx, ecx
22-
xgetbv
32+
db $0F, $01, $D0 // xgetbv
2333
mov dword ptr [r8], eax
2434
mov dword ptr [r8 + 4], edx
35+
{$ENDIF}
2536
{$ENDIF}

HashLib/src/Include/Simd/CpuDetect/XGetBvQuery_i386.inc

Lines changed: 0 additions & 25 deletions
This file was deleted.

HashLib/src/Utils/HlpSimd.pas

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,6 @@ TCpuIdResult = record
3636
RegEAX, RegEBX, RegECX, RegEDX: UInt32;
3737
end;
3838

39-
{$IFDEF HASHLIB_I386_ASM}
40-
41-
procedure CpuIdQuery(ALeaf, ASubLeaf: UInt32; AResult: Pointer);
42-
{$I ..\Include\Simd\CpuDetect\CpuIdQuery_i386.inc}
43-
end;
44-
45-
procedure XGetBvQuery(AResult: Pointer);
46-
{$I ..\Include\Simd\CpuDetect\XGetBvQuery_i386.inc}
47-
end;
48-
49-
{$ELSE}
50-
5139
procedure CpuIdQuery(ALeaf, ASubLeaf: UInt32; AResult: Pointer);
5240
{$I ..\Include\Simd\CpuDetect\CpuIdQuery.inc}
5341
end;
@@ -56,7 +44,6 @@ procedure XGetBvQuery(AResult: Pointer);
5644
{$I ..\Include\Simd\CpuDetect\XGetBvQuery.inc}
5745
end;
5846

59-
{$ENDIF}
6047
{$ENDIF}
6148

6249
{ TSimd }

0 commit comments

Comments
 (0)