cpu.pp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. var
  30. is_sse3_cpu : boolean = false;
  31. function InterlockedCompareExchange128(var Target: Int128Rec; NewValue: Int128Rec; Comperand: Int128Rec): Int128Rec;
  32. implementation
  33. {$ASMMODE INTEL}
  34. var
  35. _AVXSupport,
  36. _AVX2Support,
  37. _AESSupport,
  38. _FMASupport,
  39. _POPCNTSupport : boolean;
  40. function InterlockedCompareExchange128(var Target: Int128Rec; NewValue: Int128Rec; Comperand: Int128Rec): Int128Rec;
  41. begin
  42. RunError(217);
  43. end;
  44. function cpuid_support : boolean;assembler;
  45. {
  46. Check if the ID-flag can be changed, if changed then CpuID is supported.
  47. Tested under go32v1 and Linux on c6x86 with CpuID enabled and disabled (PFV)
  48. }
  49. asm
  50. push ebx
  51. pushfd
  52. pushfd
  53. pop eax
  54. mov ebx,eax
  55. xor eax,200000h
  56. push eax
  57. popfd
  58. pushfd
  59. pop eax
  60. popfd
  61. and eax,200000h
  62. and ebx,200000h
  63. cmp eax,ebx
  64. setnz al
  65. pop ebx
  66. end;
  67. function cr0 : longint;assembler;
  68. asm
  69. DB 0Fh,20h,0C0h
  70. { mov eax,cr0
  71. special registers are not allowed in the assembler
  72. parsers }
  73. end;
  74. function floating_point_emulation : boolean;
  75. begin
  76. {!!!! I don't know currently the position of the EM flag }
  77. { $4 after Ralf Brown's list }
  78. floating_point_emulation:=(cr0 and $4)<>0;
  79. end;
  80. {$ASMMODE ATT}
  81. function XGETBV(i : dword) : int64;assembler;
  82. asm
  83. movl %eax,%ecx
  84. // older FPCs don't know the xgetbv opcode
  85. .byte 0x0f,0x01,0xd0
  86. end;
  87. procedure SetupSupport;
  88. var
  89. _ecx,_ebx : longint;
  90. begin
  91. is_sse3_cpu:=false;
  92. if cpuid_support then
  93. begin
  94. asm
  95. pushl %ebx
  96. movl $1,%eax
  97. cpuid
  98. movl %ecx,_ecx
  99. popl %ebx
  100. end;
  101. _AESSupport:=(_ecx and $2000000)<>0;
  102. _POPCNTSupport:=(_ecx and $800000)<>0;
  103. _AVXSupport:=
  104. { XGETBV suspport? }
  105. ((_ecx and $08000000)<>0) and
  106. { xmm and ymm state enabled? }
  107. ((XGETBV(0) and %110)=%110) and
  108. { avx supported? }
  109. ((_ecx and $10000000)<>0);
  110. is_sse3_cpu:=(_ecx and $1)<>0;
  111. _FMASupport:=_AVXSupport and ((_ecx and $1000)<>0);
  112. asm
  113. pushl %ebx
  114. movl $7,%eax
  115. movl $0,%ecx
  116. cpuid
  117. movl %ebx,_ebx
  118. popl %ebx
  119. end;
  120. _AVX2Support:=_AVXSupport and ((_ebx and $20)<>0);
  121. end;
  122. end;
  123. function InterlockedCompareExchange128Support : boolean;
  124. begin
  125. { 32 Bit CPUs have no 128 Bit interlocked exchange support }
  126. result:=false;
  127. end;
  128. function AESSupport : boolean;
  129. begin
  130. result:=_AESSupport;
  131. end;
  132. function AVXSupport: boolean;inline;
  133. begin
  134. result:=_AVXSupport;
  135. end;
  136. function AVX2Support: boolean;inline;
  137. begin
  138. result:=_AVX2Support;
  139. end;
  140. function FMASupport: boolean;inline;
  141. begin
  142. result:=_FMASupport;
  143. end;
  144. function POPCNTSupport: boolean;inline;
  145. begin
  146. result:=_POPCNTSupport;
  147. end;
  148. begin
  149. SetupSupport;
  150. end.