cpu.pp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. unit cpu;
  14. interface
  15. uses
  16. sysutils;
  17. { returns true, if the processor supports the cpuid instruction }
  18. function cpuid_support : boolean;
  19. { returns true, if floating point is done by an emulator }
  20. function floating_point_emulation : boolean;
  21. { returns the contents of the cr0 register }
  22. function cr0 : longint;
  23. function InterlockedCompareExchange128Support : boolean;
  24. function AESSupport : boolean;inline;
  25. function AVXSupport: boolean;inline;
  26. function AVX2Support: boolean;inline;
  27. function FMASupport: boolean;inline;
  28. function POPCNTSupport: boolean;inline;
  29. function SSE41Support: boolean;inline;
  30. function SSE42Support: boolean;inline;
  31. function MOVBESupport: boolean;inline;
  32. function F16CSupport: boolean;inline;
  33. function RDRANDSupport: boolean;inline;
  34. var
  35. is_sse3_cpu : boolean = false;
  36. function InterlockedCompareExchange128(var Target: Int128Rec; NewValue: Int128Rec; Comperand: Int128Rec): Int128Rec;
  37. implementation
  38. {$ASMMODE INTEL}
  39. var
  40. _AVXSupport,
  41. _AVX2Support,
  42. _AESSupport,
  43. _FMASupport,
  44. _POPCNTSupport,
  45. _SSE41Support,
  46. _SSE42Support,
  47. _MOVBESupport,
  48. _F16CSupport,
  49. _RDRANDSupport: boolean;
  50. function InterlockedCompareExchange128(var Target: Int128Rec; NewValue: Int128Rec; Comperand: Int128Rec): Int128Rec;
  51. begin
  52. RunError(217);
  53. end;
  54. function cpuid_support : boolean;assembler;
  55. {
  56. Check if the ID-flag can be changed, if changed then CpuID is supported.
  57. Tested under go32v1 and Linux on c6x86 with CpuID enabled and disabled (PFV)
  58. }
  59. asm
  60. push ebx
  61. pushfd
  62. pushfd
  63. pop eax
  64. mov ebx,eax
  65. xor eax,200000h
  66. push eax
  67. popfd
  68. pushfd
  69. pop eax
  70. popfd
  71. and eax,200000h
  72. and ebx,200000h
  73. cmp eax,ebx
  74. setnz al
  75. pop ebx
  76. end;
  77. function cr0 : longint;assembler;
  78. asm
  79. DB 0Fh,20h,0C0h
  80. { mov eax,cr0
  81. special registers are not allowed in the assembler
  82. parsers }
  83. end;
  84. function floating_point_emulation : boolean;
  85. begin
  86. {!!!! I don't know currently the position of the EM flag }
  87. { $4 after Ralf Brown's list }
  88. floating_point_emulation:=(cr0 and $4)<>0;
  89. end;
  90. {$ASMMODE ATT}
  91. function XGETBV(i : dword) : int64;assembler;
  92. asm
  93. movl %eax,%ecx
  94. // older FPCs don't know the xgetbv opcode
  95. .byte 0x0f,0x01,0xd0
  96. end;
  97. procedure SetupSupport;
  98. var
  99. _ecx,_ebx : longint;
  100. begin
  101. is_sse3_cpu:=false;
  102. if cpuid_support then
  103. begin
  104. asm
  105. pushl %ebx
  106. movl $1,%eax
  107. cpuid
  108. movl %ecx,_ecx
  109. popl %ebx
  110. end;
  111. _AESSupport:=(_ecx and $2000000)<>0;
  112. _POPCNTSupport:=(_ecx and $800000)<>0;
  113. _SSE41Support:=(_ecx and $80000)<>0;
  114. _SSE42Support:=(_ecx and $100000)<>0;
  115. _MOVBESupport:=(_ecx and $400000)<>0;
  116. _F16CSupport:=(_ecx and $20000000)<>0;
  117. _RDRANDSupport:=(_ecx and $40000000)<>0;
  118. _AVXSupport:=
  119. { XGETBV suspport? }
  120. ((_ecx and $08000000)<>0) and
  121. { xmm and ymm state enabled? }
  122. ((XGETBV(0) and %110)=%110) and
  123. { avx supported? }
  124. ((_ecx and $10000000)<>0);
  125. is_sse3_cpu:=(_ecx and $1)<>0;
  126. _FMASupport:=_AVXSupport and ((_ecx and $1000)<>0);
  127. asm
  128. pushl %ebx
  129. movl $7,%eax
  130. movl $0,%ecx
  131. cpuid
  132. movl %ebx,_ebx
  133. popl %ebx
  134. end;
  135. _AVX2Support:=_AVXSupport and ((_ebx and $20)<>0);
  136. end;
  137. end;
  138. function InterlockedCompareExchange128Support : boolean;
  139. begin
  140. { 32 Bit CPUs have no 128 Bit interlocked exchange support }
  141. result:=false;
  142. end;
  143. function AESSupport : boolean;
  144. begin
  145. result:=_AESSupport;
  146. end;
  147. function AVXSupport: boolean;inline;
  148. begin
  149. result:=_AVXSupport;
  150. end;
  151. function AVX2Support: boolean;inline;
  152. begin
  153. result:=_AVX2Support;
  154. end;
  155. function FMASupport: boolean;inline;
  156. begin
  157. result:=_FMASupport;
  158. end;
  159. function POPCNTSupport: boolean;inline;
  160. begin
  161. result:=_POPCNTSupport;
  162. end;
  163. function SSE41Support: boolean;inline;
  164. begin
  165. result:=_SSE41Support;
  166. end;
  167. function SSE42Support: boolean;inline;
  168. begin
  169. result:=_SSE42Support;
  170. end;
  171. function MOVBESupport: boolean;inline;
  172. begin
  173. result:=_MOVBESupport;
  174. end;
  175. function F16CSupport: boolean;inline;
  176. begin
  177. result:=_F16CSupport;
  178. end;
  179. function RDRANDSupport: boolean;inline;
  180. begin
  181. result:=_RDRANDSupport;
  182. end;
  183. begin
  184. SetupSupport;
  185. end.