cpu.pp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2000 by Florian Klaempfl
  4. This unit contains some routines to get informations about the
  5. processor
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. {$mode objfpc}
  13. {$goto on}
  14. {$IFNDEF FPC_DOTTEDUNITS}
  15. unit cpu;
  16. {$ENDIF FPC_DOTTEDUNITS}
  17. interface
  18. { returns true, if the processor supports the cpuid instruction }
  19. function cpuid_support : boolean;
  20. type
  21. TCpuidResult = record
  22. eax, ebx, ecx, edx: uint32;
  23. end;
  24. function CPUID(in_eax: uint32; in_ecx: uint32 = 0): TCpuidResult; inline;
  25. function CPUBrandString: shortstring;
  26. { returns true, if floating point is done by an emulator }
  27. function floating_point_emulation : boolean;
  28. { returns the contents of the cr0 register }
  29. function cr0 : longint;
  30. function TSCSupport: boolean;inline;
  31. function MMXSupport: boolean;inline;
  32. function CMOVSupport: boolean;inline;
  33. function InterlockedCompareExchange128Support: boolean;
  34. function AESSupport: boolean;inline;
  35. function AVXSupport: boolean;inline;
  36. function AVX2Support: boolean;inline;
  37. function AVX512FSupport: boolean;inline;
  38. function AVX512DQSupport: boolean;inline;
  39. function AVX512IFMASupport: boolean;inline;
  40. function AVX512PFSupport: boolean;inline;
  41. function AVX512ERSupport: boolean;inline;
  42. function AVX512CDSupport: boolean;inline;
  43. function AVX512BWSupport: boolean;inline;
  44. function AVX512VLSupport: boolean;inline;
  45. function AVX512VBMISupport: boolean;inline;
  46. function AVX512VBMI2Support: boolean;inline;
  47. function AVX512VNNISupport: boolean;inline;
  48. function VAESSupport: boolean;inline;
  49. function VCLMULSupport: boolean;inline;
  50. function AVX512BITALGSupport: boolean;inline;
  51. function RDSEEDSupport: boolean;inline;
  52. function ADXSupport: boolean;inline;
  53. function SHASupport: boolean;inline;
  54. function FMASupport: boolean;inline;
  55. function POPCNTSupport: boolean;inline;
  56. function LZCNTSupport: boolean;inline;
  57. function SSE3Support: boolean;inline;
  58. function SSSE3Support: boolean;inline;
  59. function SSE41Support: boolean;inline;
  60. function SSE42Support: boolean;inline;
  61. function MOVBESupport: boolean;inline;
  62. function F16CSupport: boolean;inline;
  63. function RDRANDSupport: boolean;inline;
  64. function RTMSupport: boolean;inline;
  65. function BMI1Support: boolean;inline;
  66. function BMI2Support: boolean;inline;
  67. var
  68. is_sse3_cpu : boolean = false;
  69. function InterlockedCompareExchange128(var Target: Int128Rec; NewValue: Int128Rec; Comperand: Int128Rec): Int128Rec;
  70. implementation
  71. {$ASMMODE INTEL}
  72. var
  73. data: record
  74. cpuid1, cpuid7: TCpuidResult;
  75. AVXSupport,
  76. LZCNTSupport: boolean;
  77. end;
  78. {$ASMMODE ATT}
  79. function InterlockedCompareExchange128(var Target: Int128Rec; NewValue: Int128Rec; Comperand: Int128Rec): Int128Rec;
  80. begin
  81. {$if FPC_FULLVERSION >= 30101}
  82. {$ifndef FPC_PIC}
  83. if RTMSupport then
  84. begin
  85. asm
  86. {$ifdef USE_REAL_INSTRUCTIONS}
  87. .Lretry:
  88. xbegin .Lretry
  89. {$else}
  90. { 3d: c7 f8 fa ff ff ff xbegin }
  91. .byte 0xc7,0xf8, 0xfa, 0xff, 0xff, 0xff
  92. {$endif}
  93. end;
  94. Result:=Target;
  95. if (Result.Lo=Comperand.Lo) and (Result.Hi=Comperand.Hi) then
  96. Target:=NewValue;
  97. asm
  98. {$ifdef USE_REAL_INSTRUCTIONS}
  99. xend
  100. {$else}
  101. { 8a: 0f 01 d5 xend }
  102. .byte 0x0f, 0x01, 0xd5
  103. {$endif}
  104. end;
  105. end
  106. else
  107. {$endif FPC_PIC}
  108. {$endif FPC_FULLVERSION >= 30101}
  109. RunError(217);
  110. end;
  111. {$ASMMODE INTEL}
  112. function cpuid_support : boolean;assembler;
  113. {
  114. Check if the ID-flag can be changed, if changed then CpuID is supported.
  115. Tested under go32v1 and Linux on c6x86 with CpuID enabled and disabled (PFV)
  116. }
  117. asm
  118. push ebx
  119. pushfd
  120. pushfd
  121. pop eax
  122. mov ebx,eax
  123. xor eax,200000h
  124. push eax
  125. popfd
  126. pushfd
  127. pop eax
  128. popfd
  129. and eax,200000h
  130. and ebx,200000h
  131. cmp eax,ebx
  132. setnz al
  133. pop ebx
  134. end;
  135. procedure CPUID(in_eax: uint32; in_ecx: uint32; out res: TCpuidResult); assembler; nostackframe;
  136. // eax = in_eax, edx = in_ecx, ecx = res
  137. asm
  138. push ebx
  139. push esi
  140. mov esi, ecx // esi = res
  141. mov ecx, edx // ecx = in_ecx
  142. cpuid
  143. mov TCpuidResult.eax[esi], eax
  144. mov TCpuidResult.ebx[esi], ebx
  145. mov TCpuidResult.ecx[esi], ecx
  146. mov TCpuidResult.edx[esi], edx
  147. pop esi
  148. pop ebx
  149. end;
  150. function CPUID(in_eax: uint32; in_ecx: uint32 = 0): TCpuidResult;
  151. begin
  152. CPUID(in_eax, in_ecx, result);
  153. end;
  154. function CPUBrandString: shortstring;
  155. begin
  156. if not cpuid_support or (CPUID($80000000).eax<$80000004) then
  157. exit('');
  158. TCpuidResult(pointer(@result[1])^):=CPUID($80000002);
  159. TCpuidResult(pointer(@result[17])^):=CPUID($80000003);
  160. TCpuidResult(pointer(@result[33])^):=CPUID($80000004);
  161. result[49]:=#0;
  162. result[0]:=chr(length(PAnsiChar(@result[1])));
  163. end;
  164. function cr0 : longint;assembler;
  165. asm
  166. {$ifdef USE_REAL_INSTRUCTIONS}
  167. mov eax,cr0
  168. {$else}
  169. DB 0Fh,20h,0C0h
  170. {$endif}
  171. { mov eax,cr0
  172. special registers are not allowed in the assembler
  173. parsers }
  174. end;
  175. function floating_point_emulation : boolean;
  176. begin
  177. {!!!! I don't know currently the position of the EM flag }
  178. { $4 after Ralf Brown's list }
  179. floating_point_emulation:=(cr0 and $4)<>0;
  180. end;
  181. {$ASMMODE ATT}
  182. function XGETBV(i : dword) : int64;assembler;
  183. asm
  184. movl %eax,%ecx
  185. {$ifdef USE_REAL_INSTRUCTIONS}
  186. xgetbv
  187. {$else}
  188. // older FPCs don't know the xgetbv opcode
  189. .byte 0x0f,0x01,0xd0
  190. {$endif}
  191. end;
  192. procedure SetupSupport;
  193. var
  194. maxcpuidvalue : longint;
  195. begin
  196. if cpuid_support then
  197. begin
  198. maxcpuidvalue:=CPUID(0).eax;
  199. CPUID(1, 0, data.cpuid1);
  200. if maxcpuidvalue>=7 then
  201. CPUID(7, 0, data.cpuid7);
  202. is_sse3_cpu:=(data.cpuid1.ecx and (1 shl 0))<>0;
  203. data.AVXSupport:=
  204. { cpuid(1).ecx[27]: XGETBV support, cpuid(1).ecx[28]: AVX support }
  205. (data.cpuid1.ecx shr 27 and %11=%11) and
  206. { xmm and ymm state enabled? }
  207. ((XGETBV(0) and %110)=%110);
  208. data.LZCNTSupport:=(CPUID($80000001).ecx and (1 shl 5))<>0;
  209. end;
  210. end;
  211. function InterlockedCompareExchange128Support : boolean;
  212. begin
  213. { 32 Bit CPUs have no 128 Bit interlocked exchange support,
  214. but it can simulated using RTM }
  215. result:=RTMSupport;
  216. end;
  217. function TSCSupport: boolean;
  218. begin
  219. result:=(data.cpuid1.edx and (1 shl 4))<>0;
  220. end;
  221. function MMXSupport: boolean;
  222. begin
  223. result:=(data.cpuid1.edx and (1 shl 23))<>0;
  224. end;
  225. function CMOVSupport : boolean;
  226. begin
  227. result:=(data.cpuid1.edx and (1 shl 15))<>0;
  228. end;
  229. function AESSupport : boolean;
  230. begin
  231. result:=(data.cpuid1.ecx and (1 shl 25))<>0;
  232. end;
  233. function AVXSupport: boolean;inline;
  234. begin
  235. result:=data.AVXSupport;
  236. end;
  237. function AVX2Support: boolean;inline;
  238. begin
  239. result:=data.AVXSupport and ((data.cpuid7.ebx and (1 shl 5))<>0);
  240. end;
  241. function AVX512FSupport: boolean;inline;
  242. begin
  243. result:=(data.cpuid7.ebx and (1 shl 16))<>0;
  244. end;
  245. function AVX512DQSupport: boolean;inline;
  246. begin
  247. result:=(data.cpuid7.ebx and (1 shl 17))<>0;
  248. end;
  249. function AVX512IFMASupport: boolean;inline;
  250. begin
  251. result:=(data.cpuid7.ebx and (1 shl 21))<>0;
  252. end;
  253. function AVX512PFSupport: boolean;inline;
  254. begin
  255. result:=(data.cpuid7.ebx and (1 shl 26))<>0;
  256. end;
  257. function AVX512ERSupport: boolean;inline;
  258. begin
  259. result:=(data.cpuid7.ebx and (1 shl 27))<>0;
  260. end;
  261. function AVX512CDSupport: boolean;inline;
  262. begin
  263. result:=(data.cpuid7.ebx and (1 shl 28))<>0;
  264. end;
  265. function AVX512BWSupport: boolean;inline;
  266. begin
  267. result:=(data.cpuid7.ebx and (1 shl 30))<>0;
  268. end;
  269. function AVX512VLSupport: boolean;inline;
  270. begin
  271. result:=(data.cpuid7.ebx and (1 shl 31))<>0;
  272. end;
  273. function AVX512VBMISupport: boolean;inline;
  274. begin
  275. result:=(data.cpuid7.ecx and (1 shl 1))<>0;
  276. end;
  277. function AVX512VBMI2Support: boolean;inline;
  278. begin
  279. result:=(data.cpuid7.ecx and (1 shl 6))<>0;
  280. end;
  281. function VAESSupport: boolean;inline;
  282. begin
  283. result:=(data.cpuid7.ecx and (1 shl 9))<>0;
  284. end;
  285. function VCLMULSupport: boolean;inline;
  286. begin
  287. result:=(data.cpuid7.ecx and (1 shl 10))<>0;
  288. end;
  289. function AVX512VNNISupport: boolean;inline;
  290. begin
  291. result:=(data.cpuid7.ecx and (1 shl 11))<>0;
  292. end;
  293. function AVX512BITALGSupport: boolean;inline;
  294. begin
  295. result:=(data.cpuid7.ecx and (1 shl 12))<>0;
  296. end;
  297. function RDSEEDSupport: boolean;inline;
  298. begin
  299. result:=(data.cpuid7.ebx and (1 shl 18))<>0;
  300. end;
  301. function ADXSupport: boolean;inline;
  302. begin
  303. result:=(data.cpuid7.ebx and (1 shl 19))<>0;
  304. end;
  305. function SHASupport: boolean;inline;
  306. begin
  307. result:=(data.cpuid7.ebx and (1 shl 29))<>0;
  308. end;
  309. function FMASupport: boolean;inline;
  310. begin
  311. result:=data.AVXSupport and ((data.cpuid1.ecx and (1 shl 12))<>0);
  312. end;
  313. function POPCNTSupport: boolean;inline;
  314. begin
  315. result:=(data.cpuid1.ecx and (1 shl 23))<>0;
  316. end;
  317. function LZCNTSupport: boolean;inline;
  318. begin
  319. result:=data.LZCNTSupport;
  320. end;
  321. function SSE3Support: boolean;inline;
  322. begin
  323. result:=(data.cpuid1.ecx and (1 shl 0))<>0;
  324. end;
  325. function SSSE3Support: boolean;inline;
  326. begin
  327. result:=(data.cpuid1.ecx and (1 shl 9))<>0;
  328. end;
  329. function SSE41Support: boolean;inline;
  330. begin
  331. result:=(data.cpuid1.ecx and (1 shl 19))<>0;
  332. end;
  333. function SSE42Support: boolean;inline;
  334. begin
  335. result:=(data.cpuid1.ecx and (1 shl 20))<>0;
  336. end;
  337. function MOVBESupport: boolean;inline;
  338. begin
  339. result:=(data.cpuid1.ecx and (1 shl 22))<>0;
  340. end;
  341. function F16CSupport: boolean;inline;
  342. begin
  343. result:=(data.cpuid1.ecx and (1 shl 29))<>0;
  344. end;
  345. function RDRANDSupport: boolean;inline;
  346. begin
  347. result:=(data.cpuid1.ecx and (1 shl 30))<>0;
  348. end;
  349. function RTMSupport: boolean;inline;
  350. begin
  351. result:=((data.cpuid7.ebx and (1 shl 11))<>0) and (data.cpuid7.edx and (1 shl 11)=0 {RTM_ALWAYS_ABORT});
  352. end;
  353. function BMI1Support: boolean;inline;
  354. begin
  355. result:=(data.cpuid7.ebx and (1 shl 3))<>0;
  356. end;
  357. function BMI2Support: boolean;inline;
  358. begin
  359. result:=(data.cpuid7.ebx and (1 shl 8))<>0;
  360. end;
  361. begin
  362. SetupSupport;
  363. end.