Skip to content

Commit 8d68219

Browse files
authored
Move SIMD CPU-detection assembly from HlpSimd.pas to dedicated .inc files (#56)
- Move SIMD CPU-detection assembly from HlpSimd.pas to dedicated .inc files Extract the inline CpuIdQuery and XGetBvQuery assembly bodies into Include/Simd/CpuDetect/CpuIdQuery.inc and XGetBvQuery.inc, replacing the dual FPC/Delphi asm blocks with {$I} includes.
1 parent 533cc56 commit 8d68219

3 files changed

Lines changed: 66 additions & 59 deletions

File tree

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// CPUID query: executes CPUID with leaf=ALeaf, subleaf=ASubLeaf,
2+
// 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.
6+
{$IFDEF FPC}
7+
assembler; nostackframe;
8+
asm
9+
push rbx
10+
{$IFDEF MSWINDOWS}
11+
mov eax, ecx
12+
mov ecx, edx
13+
cpuid
14+
mov dword ptr [r8], eax
15+
mov dword ptr [r8 + 4], ebx
16+
mov dword ptr [r8 + 8], ecx
17+
mov dword ptr [r8 + 12], edx
18+
{$ELSE}
19+
mov eax, edi
20+
mov ecx, esi
21+
mov r8, rdx
22+
cpuid
23+
mov dword ptr [r8], eax
24+
mov dword ptr [r8 + 4], ebx
25+
mov dword ptr [r8 + 8], ecx
26+
mov dword ptr [r8 + 12], edx
27+
{$ENDIF}
28+
pop rbx
29+
{$ELSE}
30+
asm
31+
.PUSHNV RBX
32+
mov eax, ecx
33+
mov ecx, edx
34+
cpuid
35+
mov dword ptr [r8], eax
36+
mov dword ptr [r8 + 4], ebx
37+
mov dword ptr [r8 + 8], ecx
38+
mov dword ptr [r8 + 12], edx
39+
{$ENDIF}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// XGETBV query: executes XGETBV with ECX=0, stores EAX:EDX into the
2+
// UInt64 at AResult.
3+
// Expects MS x64 ABI: rcx = AResult.
4+
// On FPC non-Windows (System V ABI), remaps rdi -> rcx.
5+
{$IFDEF FPC}
6+
assembler; nostackframe;
7+
asm
8+
{$IFDEF MSWINDOWS}
9+
mov r8, rcx
10+
{$ELSE}
11+
mov r8, rdi
12+
{$ENDIF}
13+
xor ecx, ecx
14+
xgetbv
15+
mov dword ptr [r8], eax
16+
mov dword ptr [r8 + 4], edx
17+
{$ELSE}
18+
asm
19+
.noframe
20+
mov r8, rcx
21+
xor ecx, ecx
22+
xgetbv
23+
mov dword ptr [r8], eax
24+
mov dword ptr [r8 + 4], edx
25+
{$ENDIF}

HashLib/src/Utils/HlpSimd.pas

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

39-
{$IFDEF FPC}
4039
procedure CpuIdQuery(ALeaf, ASubLeaf: UInt32; AResult: Pointer);
41-
assembler; nostackframe;
42-
asm
43-
push rbx
44-
{$IFDEF MSWINDOWS}
45-
mov eax, ecx
46-
mov ecx, edx
47-
cpuid
48-
mov dword ptr [r8], eax
49-
mov dword ptr [r8 + 4], ebx
50-
mov dword ptr [r8 + 8], ecx
51-
mov dword ptr [r8 + 12], edx
52-
{$ELSE}
53-
mov eax, edi
54-
mov ecx, esi
55-
mov r8, rdx
56-
cpuid
57-
mov dword ptr [r8], eax
58-
mov dword ptr [r8 + 4], ebx
59-
mov dword ptr [r8 + 8], ecx
60-
mov dword ptr [r8 + 12], edx
61-
{$ENDIF}
62-
pop rbx
40+
{$I ..\Include\Simd\CpuDetect\CpuIdQuery.inc}
6341
end;
64-
{$ELSE}
65-
procedure CpuIdQuery(ALeaf, ASubLeaf: UInt32; AResult: Pointer);
66-
asm
67-
.PUSHNV RBX
68-
mov eax, ecx
69-
mov ecx, edx
70-
cpuid
71-
mov dword ptr [r8], eax
72-
mov dword ptr [r8 + 4], ebx
73-
mov dword ptr [r8 + 8], ecx
74-
mov dword ptr [r8 + 12], edx
75-
end;
76-
{$ENDIF}
7742

78-
{$IFDEF FPC}
7943
procedure XGetBvQuery(AResult: Pointer);
80-
assembler; nostackframe;
81-
asm
82-
{$IFDEF MSWINDOWS}
83-
mov r8, rcx
84-
{$ELSE}
85-
mov r8, rdi
86-
{$ENDIF}
87-
xor ecx, ecx
88-
xgetbv
89-
mov dword ptr [r8], eax
90-
mov dword ptr [r8 + 4], edx
44+
{$I ..\Include\Simd\CpuDetect\XGetBvQuery.inc}
9145
end;
92-
{$ELSE}
93-
procedure XGetBvQuery(AResult: Pointer);
94-
asm
95-
.noframe
96-
mov r8, rcx
97-
xor ecx, ecx
98-
xgetbv
99-
mov dword ptr [r8], eax
100-
mov dword ptr [r8 + 4], edx
101-
end;
102-
{$ENDIF}
10346

10447
{$ENDIF HASHLIB_X86_64_ASM}
10548

0 commit comments

Comments
 (0)