-
-
Notifications
You must be signed in to change notification settings - Fork 91
Expand file tree
/
Copy pathHlpArmHwCapProvider.pas
More file actions
265 lines (208 loc) · 5.82 KB
/
HlpArmHwCapProvider.pas
File metadata and controls
265 lines (208 loc) · 5.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
unit HlpArmHwCapProvider;
{$I ..\Include\HashLib.inc}
interface
{$IF DEFINED(HASHLIB_ARM)}
{$IF DEFINED(HASHLIB_LINUX) OR DEFINED(HASHLIB_ANDROID) OR DEFINED(HASHLIB_BSD)}
uses
{$IFDEF FPC}
dl;
{$ELSE}
Posix.Dlfcn;
{$ENDIF}
{$IFEND}
{$IF DEFINED(HASHLIB_MSWINDOWS)}
uses
Windows;
{$IFEND}
{ ===== AArch64 HWCAP bit definitions (from asm/hwcap.h) ===== }
{ These constants are shared by Linux, Android, and BSD on AArch64. }
{$IF DEFINED(HASHLIB_LINUX) OR DEFINED(HASHLIB_ANDROID) OR DEFINED(HASHLIB_BSD)}
{$IF DEFINED(HASHLIB_AARCH64)}
const
AT_HWCAP = 16;
AT_HWCAP2 = 26;
HWCAP_ASIMD = UInt64(1) shl 1;
HWCAP_AES = UInt64(1) shl 3;
HWCAP_PMULL = UInt64(1) shl 4;
HWCAP_SHA1 = UInt64(1) shl 5;
HWCAP_SHA2 = UInt64(1) shl 6;
HWCAP_CRC32 = UInt64(1) shl 7;
HWCAP_SHA3 = UInt64(1) shl 17;
HWCAP_SHA512 = UInt64(1) shl 21;
HWCAP_SVE = UInt64(1) shl 22;
HWCAP2_SVE2 = UInt64(1) shl 1;
{$IFEND}
{ ===== ARM32 HWCAP bit definitions (from asm/hwcap.h) ===== }
{ These constants are shared by Linux, Android, and BSD on ARM32. }
{$IF DEFINED(HASHLIB_ARM32)}
const
AT_HWCAP = 16;
AT_HWCAP2 = 26;
HWCAP_NEON = UInt64(1) shl 12;
HWCAP2_AES = UInt64(1) shl 0;
HWCAP2_PMULL = UInt64(1) shl 1;
HWCAP2_SHA1 = UInt64(1) shl 2;
HWCAP2_SHA2 = UInt64(1) shl 3;
HWCAP2_CRC32 = UInt64(1) shl 4;
{$IFEND}
{$IFEND} // HASHLIB_LINUX OR HASHLIB_ANDROID OR HASHLIB_BSD
{ ===== Windows ARM64 PF_ARM_* constants ===== }
{$IF DEFINED(HASHLIB_MSWINDOWS)}
const
// Standard constants (always available)
PF_ARM_NEON_INSTRUCTIONS_AVAILABLE = 19;
PF_ARM_V8_CRYPTO_INSTRUCTIONS_AVAILABLE = 30; // Bundles AES, PMULL, SHA1, SHA256
PF_ARM_V8_CRC32_INSTRUCTIONS_AVAILABLE = 31;
// Newer constants (SDK 22621+, defined here to avoid SDK version dependency)
PF_ARM_V82_SHA3_INSTRUCTIONS_AVAILABLE = 46;
PF_ARM_V82_SHA512_INSTRUCTIONS_AVAILABLE = 47;
{$IFEND}
type
/// <summary>
/// Provides ARM hardware capability information across platforms.
/// Linux/Android: resolves getauxval via dlsym.
/// BSD: resolves elf_aux_info / _elf_aux_info via dlsym.
/// Windows: wraps IsProcessorFeaturePresent from the Windows unit.
/// </summary>
TArmHwCapProvider = class sealed
{$IF DEFINED(HASHLIB_LINUX) OR DEFINED(HASHLIB_ANDROID)}
strict private
type
TGetAuxValFunc = function(AType: UInt64): UInt64; cdecl;
strict private
class var
FGetAuxVal: TGetAuxValFunc;
private
class procedure ResolveDynamicImports(); static;
public
class function GetHwCap(): UInt64; static;
class function GetHwCap2(): UInt64; static;
{$IFEND}
{$IF DEFINED(HASHLIB_BSD)}
strict private
type
TElfAuxInfoFunc = function(AAuxType: Int32; ABuf: Pointer; ABufLen: Int32): Int32; cdecl;
strict private
class var
FElfAuxInfo: TElfAuxInfoFunc;
private
class procedure ResolveDynamicImports(); static;
public
class function GetHwCap(): UInt64; static;
class function GetHwCap2(): UInt64; static;
{$IFEND}
{$IF DEFINED(HASHLIB_MSWINDOWS)}
public
class function HasProcessorFeature(AFeature: UInt32): Boolean; static;
{$IFEND}
end;
{$IFEND} // HASHLIB_ARM
implementation
{$IF DEFINED(HASHLIB_ARM)}
{ TArmHwCapProvider }
{ ===== Linux / Android: getauxval ===== }
{$IF DEFINED(HASHLIB_LINUX) OR DEFINED(HASHLIB_ANDROID)}
class procedure TArmHwCapProvider.ResolveDynamicImports();
var
LHandle: NativeUInt;
begin
FGetAuxVal := nil;
{$IFDEF FPC}
LHandle := NativeUInt(dlopen(nil, RTLD_NOW));
{$ELSE}
LHandle := dlopen(nil, RTLD_NOW);
{$ENDIF}
if LHandle = 0 then
Exit;
try
// getauxval is available in glibc (Linux) and Bionic (Android API 18+)
FGetAuxVal := TGetAuxValFunc(dlsym(LHandle, 'getauxval'));
finally
dlclose(LHandle);
end;
end;
class function TArmHwCapProvider.GetHwCap(): UInt64;
begin
if System.Assigned(FGetAuxVal) then
Result := FGetAuxVal(AT_HWCAP)
else
Result := 0;
end;
class function TArmHwCapProvider.GetHwCap2(): UInt64;
begin
if System.Assigned(FGetAuxVal) then
Result := FGetAuxVal(AT_HWCAP2)
else
Result := 0;
end;
{$IFEND} // HASHLIB_LINUX OR HASHLIB_ANDROID
{ ===== BSD: elf_aux_info / _elf_aux_info ===== }
{$IF DEFINED(HASHLIB_BSD)}
class procedure TArmHwCapProvider.ResolveDynamicImports();
var
LHandle: NativeUInt;
begin
FElfAuxInfo := nil;
{$IFDEF FPC}
LHandle := NativeUInt(dlopen(nil, RTLD_NOW));
{$ELSE}
LHandle := dlopen(nil, RTLD_NOW);
{$ENDIF}
if LHandle = 0 then
Exit;
try
// FreeBSD exposes elf_aux_info
FElfAuxInfo := TElfAuxInfoFunc(dlsym(LHandle, 'elf_aux_info'));
if not System.Assigned(FElfAuxInfo) then
begin
// NetBSD and DragonFlyBSD use the underscore-prefixed variant
FElfAuxInfo := TElfAuxInfoFunc(dlsym(LHandle, '_elf_aux_info'));
end;
finally
dlclose(LHandle);
end;
end;
class function TArmHwCapProvider.GetHwCap(): UInt64;
var
LValue: UInt64;
begin
if System.Assigned(FElfAuxInfo) then
begin
LValue := 0;
if FElfAuxInfo(Int32(AT_HWCAP), @LValue, SizeOf(LValue)) = 0 then
Result := LValue
else
Result := 0;
end
else
Result := 0;
end;
class function TArmHwCapProvider.GetHwCap2(): UInt64;
var
LValue: UInt64;
begin
if System.Assigned(FElfAuxInfo) then
begin
LValue := 0;
if FElfAuxInfo(Int32(AT_HWCAP2), @LValue, SizeOf(LValue)) = 0 then
Result := LValue
else
Result := 0;
end
else
Result := 0;
end;
{$IFEND} // HASHLIB_BSD
{ ===== Windows: IsProcessorFeaturePresent ===== }
{$IF DEFINED(HASHLIB_MSWINDOWS)}
class function TArmHwCapProvider.HasProcessorFeature(AFeature: UInt32): Boolean;
begin
Result := IsProcessorFeaturePresent(AFeature);
end;
{$IFEND} // HASHLIB_MSWINDOWS
{$IF DEFINED(HASHLIB_LINUX) OR DEFINED(HASHLIB_ANDROID) OR DEFINED(HASHLIB_BSD)}
initialization
TArmHwCapProvider.ResolveDynamicImports;
{$IFEND}
{$IFEND} // HASHLIB_ARM
end.