cpu.pp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  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. {$IFNDEF FPC_DOTTEDUNITS}
  14. unit cpu;
  15. {$ENDIF FPC_DOTTEDUNITS}
  16. interface
  17. {$ifdef freebsd} // FreeBSD 7/8 have binutils version that don't support cmpxchg16b
  18. // Unless overridebinutils is defined (for ports usage), use db instead of the instruction
  19. {$ifndef overridebinutils}
  20. {$define oldbinutils}
  21. {$endif}
  22. {$endif}
  23. {$IFDEF FPC_DOTTEDUNITS}
  24. uses
  25. System.SysUtils;
  26. {$ELSE FPC_DOTTEDUNITS}
  27. uses
  28. sysutils;
  29. {$ENDIF FPC_DOTTEDUNITS}
  30. type
  31. TCpuidResult = record
  32. eax, ebx, ecx, edx: uint32;
  33. end;
  34. function CPUID(in_eax: uint32; in_ecx: uint32 = 0): TCpuidResult; inline;
  35. function CPUBrandString: shortstring;
  36. function InterlockedCompareExchange128Support : boolean;inline;
  37. function CMOVSupport : boolean;inline;
  38. function AESSupport : boolean;inline;
  39. function AVXSupport : boolean;inline;
  40. function AVX2Support: boolean;inline;
  41. function AVX512FSupport: boolean;inline;
  42. function AVX512DQSupport: boolean;inline;
  43. function AVX512IFMASupport: boolean;inline;
  44. function AVX512PFSupport: boolean;inline;
  45. function AVX512ERSupport: boolean;inline;
  46. function AVX512CDSupport: boolean;inline;
  47. function AVX512BWSupport: boolean;inline;
  48. function AVX512VLSupport: boolean;inline;
  49. function AVX512VBMISupport: boolean;inline;
  50. function AVX512VBMI2Support: boolean;inline;
  51. function AVX512VNNISupport: boolean;inline;
  52. function VAESSupport: boolean;inline;
  53. function VCLMULSupport: boolean;inline;
  54. function AVX512BITALGSupport: boolean;inline;
  55. function RDSEEDSupport: boolean;inline;
  56. function ADXSupport: boolean;inline;
  57. function SHASupport: boolean;inline;
  58. function FMASupport: boolean;inline;
  59. function POPCNTSupport: boolean;inline;
  60. function LZCNTSupport: boolean;inline;
  61. function SSE3Support: boolean;inline;
  62. function SSSE3Support: boolean;inline;
  63. function SSE41Support: boolean;inline;
  64. function SSE42Support: boolean;inline;
  65. function MOVBESupport: boolean;inline;
  66. function F16CSupport: boolean;inline;
  67. function RDRANDSupport: boolean;inline;
  68. function RTMSupport: boolean;inline;
  69. function BMI1Support: boolean;inline;
  70. function BMI2Support: boolean;inline;
  71. var
  72. is_sse3_cpu : boolean = false;
  73. function InterlockedCompareExchange128(var Target: Int128Rec; NewValue: Int128Rec; Comperand: Int128Rec): Int128Rec;
  74. implementation
  75. {$asmmode att}
  76. var
  77. _AESSupport,
  78. _AVXSupport,
  79. _InterlockedCompareExchange128Support,
  80. _AVX2Support,
  81. _AVX512FSupport,
  82. _AVX512DQSupport,
  83. _AVX512IFMASupport,
  84. _AVX512PFSupport,
  85. _AVX512ERSupport,
  86. _AVX512CDSupport,
  87. _AVX512BWSupport,
  88. _AVX512VLSupport,
  89. _AVX512VBMISupport,
  90. _AVX512VBMI2Support,
  91. _VAESSupport,
  92. _VCLMULSupport,
  93. _AVX512VNNISupport,
  94. _AVX512BITALGSupport,
  95. _RDSEEDSupport,
  96. _ADXSupport,
  97. _SHASupport,
  98. _FMASupport,
  99. _POPCNTSupport,
  100. _LZCNTSupport,
  101. _SSE3Support,
  102. _SSSE3Support,
  103. _SSE41Support,
  104. _SSE42Support,
  105. _MOVBESupport,
  106. _F16CSupport,
  107. _RDRANDSupport,
  108. _RTMSupport,
  109. _BMI1Support,
  110. _BMI2Support: boolean;
  111. procedure CPUID(in_eax: uint32; in_ecx: uint32; out res: TCpuidResult); assembler; nostackframe;
  112. // ^ I don't know how 16-byte result is handled in SysV, if it is returned in RDX:RAX as GCC does things become complex,
  113. // that's why this internal version with "out res" exists...
  114. // Win64: ecx = in_eax, edx = in_ecx, r8 = res.
  115. // SysV: edi = in_eax, esi = in_ecx, rdx = res.
  116. asm
  117. push %rbx
  118. {$ifndef win64}
  119. mov %rdx, %r8 // r8 = res
  120. {$endif}
  121. mov in_eax, %eax
  122. mov in_ecx, %ecx
  123. cpuid
  124. mov %eax, TCpuidResult.eax(%r8)
  125. mov %ebx, TCpuidResult.ebx(%r8)
  126. mov %ecx, TCpuidResult.ecx(%r8)
  127. mov %edx, TCpuidResult.edx(%r8)
  128. pop %rbx
  129. end;
  130. function CPUID(in_eax: uint32; in_ecx: uint32 = 0): TCpuidResult;
  131. begin
  132. CPUID(in_eax, in_ecx, result);
  133. end;
  134. function CPUBrandString: shortstring;
  135. begin
  136. if CPUID($80000000).eax<$80000004 then
  137. exit('');
  138. TCpuidResult(pointer(@result[1])^):=CPUID($80000002);
  139. TCpuidResult(pointer(@result[17])^):=CPUID($80000003);
  140. TCpuidResult(pointer(@result[33])^):=CPUID($80000004);
  141. result[49]:=#0;
  142. result[0]:=chr(length(PAnsiChar(@result[1])));
  143. end;
  144. function InterlockedCompareExchange128(var Target: Int128Rec; NewValue: Int128Rec; Comperand: Int128Rec): Int128Rec; assembler;
  145. {
  146. win64:
  147. rcx ... pointer to result
  148. rdx ... target
  149. r8 ... NewValue
  150. r9 ... Comperand
  151. }
  152. {$ifdef win64}
  153. asm
  154. pushq %rbx
  155. { store result pointer for later use }
  156. pushq %rcx
  157. { load new value }
  158. movq (%r8),%rbx
  159. movq 8(%r8),%rcx
  160. { save target pointer for later use }
  161. movq %rdx,%r8
  162. { load comperand }
  163. movq (%r9),%rax
  164. movq 8(%r9),%rdx
  165. {$ifdef oldbinutils}
  166. .byte 0xF0,0x49,0x0F,0xC7,0x08
  167. {$else}
  168. lock cmpxchg16b (%r8)
  169. {$endif}
  170. { restore result pointer }
  171. popq %rcx
  172. { store result }
  173. movq %rax,(%rcx)
  174. movq %rdx,8(%rcx)
  175. popq %rbx
  176. end;
  177. {$else win64}
  178. {
  179. linux:
  180. rdi ... target
  181. [rsi:rdx] ... NewValue
  182. [rcx:r8] ... Comperand
  183. [rdx:rax] ... result
  184. }
  185. asm
  186. pushq %rbx
  187. movq %rsi,%rbx // new value low
  188. movq %rcx,%rax // comperand low
  189. movq %rdx,%rcx // new value high
  190. movq %r8,%rdx // comperand high
  191. {$ifdef oldbinutils}
  192. .byte 0xF0,0x48,0x0F,0xC7,0x0F
  193. {$else}
  194. lock cmpxchg16b (%rdi)
  195. {$endif}
  196. popq %rbx
  197. end;
  198. {$endif win64}
  199. function XGETBV(i : dword) : int64;assembler;
  200. asm
  201. {$ifndef win64}
  202. movq %rdi,%rcx
  203. {$endif win64}
  204. // older FPCs don't know the xgetbv opcode
  205. .byte 0x0f,0x01,0xd0
  206. andl $0xffffffff,%eax
  207. shlq $32,%rdx
  208. orq %rdx,%rax
  209. end;
  210. procedure SetupSupport;
  211. var
  212. maxcpuidvalue : longint;
  213. cpuid1,cpuid7 : TCpuidResult;
  214. begin
  215. maxcpuidvalue:=CPUID(0).eax;
  216. cpuid1:=CPUID(1);
  217. _InterlockedCompareExchange128Support:=(cpuid1.ecx and $2000)<>0;
  218. _AESSupport:=(cpuid1.ecx and $2000000)<>0;
  219. _POPCNTSupport:=(cpuid1.ecx and $800000)<>0;
  220. _SSE3Support:=(cpuid1.ecx and $1)<>0;
  221. _SSSE3Support:=(cpuid1.ecx and $200)<>0;
  222. _SSE41Support:=(cpuid1.ecx and $80000)<>0;
  223. _SSE42Support:=(cpuid1.ecx and $100000)<>0;
  224. _MOVBESupport:=(cpuid1.ecx and $400000)<>0;
  225. _F16CSupport:=(cpuid1.ecx and $20000000)<>0;
  226. _RDRANDSupport:=(cpuid1.ecx and $40000000)<>0;
  227. _AVXSupport:=
  228. { XGETBV suspport? }
  229. ((cpuid1.ecx and $08000000)<>0) and
  230. { xmm and ymm state enabled? }
  231. ((XGETBV(0) and %110)=%110) and
  232. { avx supported? }
  233. ((cpuid1.ecx and $10000000)<>0);
  234. is_sse3_cpu:=(cpuid1.ecx and $1)<>0;
  235. _FMASupport:=_AVXSupport and ((cpuid1.ecx and $1000)<>0);
  236. _LZCNTSupport:=(CPUID($80000001).ecx and $20)<>0;
  237. { very early x86-64 CPUs might not support eax=7 }
  238. if maxcpuidvalue>=7 then
  239. begin
  240. cpuid7:=CPUID(7);
  241. _AVX2Support:=_AVXSupport and ((cpuid7.ebx and $20)<>0);
  242. _AVX512FSupport:=(cpuid7.ebx and $10000)<>0;
  243. _AVX512DQSupport:=(cpuid7.ebx and $20000)<>0;
  244. _RDSEEDSupport:=(cpuid7.ebx and $40000)<>0;
  245. _ADXSupport:=(cpuid7.ebx and $80000)<>0;
  246. _AVX512IFMASupport:=(cpuid7.ebx and $200000)<>0;
  247. _AVX512PFSupport:=(cpuid7.ebx and $4000000)<>0;
  248. _AVX512ERSupport:=(cpuid7.ebx and $8000000)<>0;
  249. _AVX512CDSupport:=(cpuid7.ebx and $10000000)<>0;
  250. _SHASupport:=(cpuid7.ebx and $20000000)<>0;
  251. _AVX512BWSupport:=(cpuid7.ebx and $40000000)<>0;
  252. _AVX512VLSupport:=(cpuid7.ebx and $80000000)<>0;
  253. _AVX512VBMISupport:=(cpuid7.ecx and $00000002)<>0;
  254. _AVX512VBMI2Support:=(cpuid7.ecx and $00000040)<>0;
  255. _VAESSupport:=(cpuid7.ecx and $00000200)<>0;
  256. _VCLMULSupport:=(cpuid7.ecx and $00000400)<>0;
  257. _AVX512VNNISupport:=(cpuid7.ecx and $00000800)<>0;
  258. _AVX512BITALGSupport:=(cpuid7.ecx and $00001000)<>0;
  259. _BMI1Support:=(cpuid7.ebx and $8)<>0;
  260. _BMI2Support:=(cpuid7.ebx and $100)<>0;
  261. _RTMSupport:=((cpuid7.ebx and $800)<>0) and (cpuid7.edx and (1 shl 11)=0 {RTM_ALWAYS_ABORT});
  262. end;
  263. end;
  264. function InterlockedCompareExchange128Support : boolean;inline;
  265. begin
  266. result:=_InterlockedCompareExchange128Support;
  267. end;
  268. function CMOVSupport : boolean;
  269. begin
  270. result:=true;
  271. end;
  272. function AESSupport : boolean;inline;
  273. begin
  274. result:=_AESSupport;
  275. end;
  276. function AVXSupport: boolean;inline;
  277. begin
  278. result:=_AVXSupport;
  279. end;
  280. function AVX2Support: boolean;inline;
  281. begin
  282. result:=_AVX2Support;
  283. end;
  284. function AVX512FSupport: boolean;inline;
  285. begin
  286. result:=_AVX512FSupport;
  287. end;
  288. function AVX512DQSupport: boolean;inline;
  289. begin
  290. result:=_AVX512DQSupport;
  291. end;
  292. function AVX512IFMASupport: boolean;inline;
  293. begin
  294. result:=_AVX512IFMASupport;
  295. end;
  296. function AVX512PFSupport: boolean;inline;
  297. begin
  298. result:=_AVX512PFSupport;
  299. end;
  300. function AVX512ERSupport: boolean;inline;
  301. begin
  302. result:=_AVX512ERSupport;
  303. end;
  304. function AVX512CDSupport: boolean;inline;
  305. begin
  306. result:=_AVX512CDSupport;
  307. end;
  308. function AVX512BWSupport: boolean;inline;
  309. begin
  310. result:=_AVX512BWSupport;
  311. end;
  312. function AVX512VLSupport: boolean;inline;
  313. begin
  314. result:=_AVX512VLSupport;
  315. end;
  316. function AVX512VBMISupport: boolean;inline;
  317. begin
  318. result:=_AVX512VBMISupport;
  319. end;
  320. function AVX512VBMI2Support: boolean;inline;
  321. begin
  322. result:=_AVX512VBMI2Support;
  323. end;
  324. function VAESSupport: boolean;inline;
  325. begin
  326. result:=_VAESSupport;
  327. end;
  328. function VCLMULSupport: boolean;inline;
  329. begin
  330. result:=_VCLMULSupport;
  331. end;
  332. function AVX512VNNISupport: boolean;inline;
  333. begin
  334. result:=_AVX512VNNISupport;
  335. end;
  336. function AVX512BITALGSupport: boolean;inline;
  337. begin
  338. result:=_AVX512BITALGSupport;
  339. end;
  340. function RDSEEDSupport: boolean;inline;
  341. begin
  342. result:=_RDSEEDSupport;
  343. end;
  344. function ADXSupport: boolean;inline;
  345. begin
  346. result:=_ADXSupport;
  347. end;
  348. function SHASupport: boolean;inline;
  349. begin
  350. result:=_SHASupport;
  351. end;
  352. function FMASupport: boolean;inline;
  353. begin
  354. result:=_FMASupport;
  355. end;
  356. function POPCNTSupport: boolean;inline;
  357. begin
  358. result:=_POPCNTSupport;
  359. end;
  360. function LZCNTSupport: boolean;inline;
  361. begin
  362. result:=_LZCNTSupport;
  363. end;
  364. function SSE3Support: boolean;inline;
  365. begin
  366. result:=_SSE3Support;
  367. end;
  368. function SSSE3Support: boolean;inline;
  369. begin
  370. result:=_SSSE3Support;
  371. end;
  372. function SSE41Support: boolean;inline;
  373. begin
  374. result:=_SSE41Support;
  375. end;
  376. function SSE42Support: boolean;inline;
  377. begin
  378. result:=_SSE42Support;
  379. end;
  380. function MOVBESupport: boolean;inline;
  381. begin
  382. result:=_MOVBESupport;
  383. end;
  384. function F16CSupport: boolean;inline;
  385. begin
  386. result:=_F16CSupport;
  387. end;
  388. function RDRANDSupport: boolean;inline;
  389. begin
  390. result:=_RDRANDSupport;
  391. end;
  392. function RTMSupport: boolean;inline;
  393. begin
  394. result:=_RTMSupport;
  395. end;
  396. function BMI1Support: boolean;inline;
  397. begin
  398. result:=_BMI1Support;
  399. end;
  400. function BMI2Support: boolean;inline;
  401. begin
  402. result:=_BMI2Support;
  403. end;
  404. begin
  405. SetupSupport;
  406. end.