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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
|
{
This file is part of the Free Pascal run time library.
Copyright (c) 1999-2000 by Florian Klaempfl
This unit contains some routines to get informations about the
processor
See the file COPYING.FPC, included in this distribution,
for details about the copyright.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
**********************************************************************}
{$mode objfpc}
{$goto on}
unit cpu;
interface
uses
sysutils;
{ returns true, if the processor supports the cpuid instruction }
function cpuid_support : boolean;
{ returns true, if floating point is done by an emulator }
function floating_point_emulation : boolean;
{ returns the contents of the cr0 register }
function cr0 : longint;
function InterlockedCompareExchange128Support : boolean;
function AESSupport : boolean;inline;
function AVXSupport: boolean;inline;
function AVX2Support: boolean;inline;
function FMASupport: boolean;inline;
function POPCNTSupport: boolean;inline;
function SSE41Support: boolean;inline;
function SSE42Support: boolean;inline;
function MOVBESupport: boolean;inline;
function F16CSupport: boolean;inline;
function RDRANDSupport: boolean;inline;
function RTMSupport: boolean;inline;
var
is_sse3_cpu : boolean = false;
function InterlockedCompareExchange128(var Target: Int128Rec; NewValue: Int128Rec; Comperand: Int128Rec): Int128Rec;
implementation
{$ASMMODE INTEL}
var
_AVXSupport,
_AVX2Support,
_AESSupport,
_FMASupport,
_POPCNTSupport,
_SSE41Support,
_SSE42Support,
_MOVBESupport,
_F16CSupport,
_RDRANDSupport,
_RTMSupport: boolean;
{$ASMMODE ATT}
function InterlockedCompareExchange128(var Target: Int128Rec; NewValue: Int128Rec; Comperand: Int128Rec): Int128Rec;
begin
{$if FPC_FULLVERSION >= 30101}
{$ifndef FPC_PIC}
if _RTMSupport then
begin
asm
{$ifdef USE_REAL_INSTRUCTIONS}
.Lretry:
xbegin .Lretry
{$else}
{ 3d: c7 f8 fa ff ff ff xbegin }
.byte 0xc7,0xf8, 0xfa, 0xff, 0xff, 0xff
{$endif}
end;
Result:=Target;
if (Result.Lo=Comperand.Lo) and (Result.Hi=Comperand.Hi) then
Target:=NewValue;
asm
{$ifdef USE_REAL_INSTRUCTIONS}
xend
{$else}
{ 8a: 0f 01 d5 xend }
.byte 0x0f, 0x01, 0xd5
{$endif}
end;
end
else
{$endif FPC_PIC}
{$endif FPC_FULLVERSION >= 30101}
RunError(217);
end;
{$ASMMODE INTEL}
function cpuid_support : boolean;assembler;
{
Check if the ID-flag can be changed, if changed then CpuID is supported.
Tested under go32v1 and Linux on c6x86 with CpuID enabled and disabled (PFV)
}
asm
push ebx
pushfd
pushfd
pop eax
mov ebx,eax
xor eax,200000h
push eax
popfd
pushfd
pop eax
popfd
and eax,200000h
and ebx,200000h
cmp eax,ebx
setnz al
pop ebx
end;
function cr0 : longint;assembler;
asm
{$ifdef USE_REAL_INSTRUCTIONS}
mov eax,cr0
{$else}
DB 0Fh,20h,0C0h
{$endif}
{ mov eax,cr0
special registers are not allowed in the assembler
parsers }
end;
function floating_point_emulation : boolean;
begin
{!!!! I don't know currently the position of the EM flag }
{ $4 after Ralf Brown's list }
floating_point_emulation:=(cr0 and $4)<>0;
end;
{$ASMMODE ATT}
function XGETBV(i : dword) : int64;assembler;
asm
movl %eax,%ecx
{$ifdef USE_REAL_INSTRUCTIONS}
xgetbv
{$else}
// older FPCs don't know the xgetbv opcode
.byte 0x0f,0x01,0xd0
{$endif}
end;
procedure SetupSupport;
var
_ecx,_ebx : longint;
begin
is_sse3_cpu:=false;
if cpuid_support then
begin
asm
pushl %ebx
movl $1,%eax
cpuid
movl %ecx,_ecx
popl %ebx
end;
_AESSupport:=(_ecx and $2000000)<>0;
_POPCNTSupport:=(_ecx and $800000)<>0;
_SSE41Support:=(_ecx and $80000)<>0;
_SSE42Support:=(_ecx and $100000)<>0;
_MOVBESupport:=(_ecx and $400000)<>0;
_F16CSupport:=(_ecx and $20000000)<>0;
_RDRANDSupport:=(_ecx and $40000000)<>0;
_AVXSupport:=
{ XGETBV suspport? }
((_ecx and $08000000)<>0) and
{ xmm and ymm state enabled? }
((XGETBV(0) and %110)=%110) and
{ avx supported? }
((_ecx and $10000000)<>0);
is_sse3_cpu:=(_ecx and $1)<>0;
_FMASupport:=_AVXSupport and ((_ecx and $1000)<>0);
asm
pushl %ebx
movl $7,%eax
movl $0,%ecx
cpuid
movl %ebx,_ebx
popl %ebx
end;
_AVX2Support:=_AVXSupport and ((_ebx and $20)<>0);
_RTMSupport:=((_ebx and $800)<>0);
end;
end;
function InterlockedCompareExchange128Support : boolean;
begin
{ 32 Bit CPUs have no 128 Bit interlocked exchange support,
but it can simulated using RTM }
result:=_RTMSupport;
end;
function AESSupport : boolean;
begin
result:=_AESSupport;
end;
function AVXSupport: boolean;inline;
begin
result:=_AVXSupport;
end;
function AVX2Support: boolean;inline;
begin
result:=_AVX2Support;
end;
function FMASupport: boolean;inline;
begin
result:=_FMASupport;
end;
function POPCNTSupport: boolean;inline;
begin
result:=_POPCNTSupport;
end;
function SSE41Support: boolean;inline;
begin
result:=_SSE41Support;
end;
function SSE42Support: boolean;inline;
begin
result:=_SSE42Support;
end;
function MOVBESupport: boolean;inline;
begin
result:=_MOVBESupport;
end;
function F16CSupport: boolean;inline;
begin
result:=_F16CSupport;
end;
function RDRANDSupport: boolean;inline;
begin
result:=_RDRANDSupport;
end;
function RTMSupport: boolean;inline;
begin
result:=_RTMSupport;
end;
begin
SetupSupport;
end.
|