cpu.pp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. {$ifdef freebsd} // FreeBSD 7/8 have binutils version that don't support cmpxchg16b
  16. // Unless overridebinutils is defined (for ports usage), use db instead of the instruction
  17. {$ifndef overridebinutils}
  18. {$define oldbinutils}
  19. {$endif}
  20. {$endif}
  21. uses
  22. sysutils;
  23. function InterlockedCompareExchange128Support : boolean;inline;
  24. function AESSupport : boolean;inline;
  25. function AVXSupport : boolean;inline;
  26. function AVX2Support: boolean;inline;
  27. function FMASupport: boolean;inline;
  28. var
  29. is_sse3_cpu : boolean = false;
  30. function InterlockedCompareExchange128(var Target: Int128Rec; NewValue: Int128Rec; Comperand: Int128Rec): Int128Rec;
  31. implementation
  32. {$asmmode att}
  33. var
  34. _AESSupport,
  35. _AVXSupport,
  36. _InterlockedCompareExchange128Support,
  37. _AVX2Support,
  38. _FMASupport : boolean;
  39. function InterlockedCompareExchange128(var Target: Int128Rec; NewValue: Int128Rec; Comperand: Int128Rec): Int128Rec; assembler;
  40. {
  41. win64:
  42. rcx ... pointer to result
  43. rdx ... target
  44. r8 ... NewValue
  45. r9 ... Comperand
  46. }
  47. {$ifdef win64}
  48. asm
  49. pushq %rbx
  50. { store result pointer for later use }
  51. pushq %rcx
  52. { load new value }
  53. movq (%r8),%rbx
  54. movq 8(%r8),%rcx
  55. { save target pointer for later use }
  56. movq %rdx,%r8
  57. { load comperand }
  58. movq (%r9),%rax
  59. movq 8(%r9),%rdx
  60. {$ifdef oldbinutils}
  61. .byte 0xF0,0x49,0x0F,0xC7,0x08
  62. {$else}
  63. lock cmpxchg16b (%r8)
  64. {$endif}
  65. { restore result pointer }
  66. popq %rcx
  67. { store result }
  68. movq %rax,(%rcx)
  69. movq %rdx,8(%rcx)
  70. popq %rbx
  71. end;
  72. {$else win64}
  73. {
  74. linux:
  75. rdi ... target
  76. [rsi:rdx] ... NewValue
  77. [rcx:r8] ... Comperand
  78. [rdx:rax] ... result
  79. }
  80. asm
  81. pushq %rbx
  82. movq %rsi,%rbx // new value low
  83. movq %rcx,%rax // comperand low
  84. movq %rdx,%rcx // new value high
  85. movq %r8,%rdx // comperand high
  86. {$ifdef oldbinutils}
  87. .byte 0xF0,0x48,0x0F,0xC7,0x0F
  88. {$else}
  89. lock cmpxchg16b (%rdi)
  90. {$endif}
  91. popq %rbx
  92. end;
  93. {$endif win64}
  94. function XGETBV(i : dword) : int64;assembler;
  95. asm
  96. {$ifndef win64}
  97. movq %rdi,%rcx
  98. {$endif win64}
  99. // older FPCs don't know the xgetbv opcode
  100. .byte 0x0f,0x01,0xd0
  101. andl $0xffffffff,%eax
  102. shlq $32,%rdx
  103. orq %rdx,%rax
  104. end;
  105. procedure SetupSupport;
  106. var
  107. _ecx,
  108. _ebx : longint;
  109. begin
  110. asm
  111. movl $0x00000001,%eax
  112. cpuid
  113. movl %ecx,_ecx
  114. end ['rax','rbx','rcx','rdx'];
  115. _InterlockedCompareExchange128Support:=(_ecx and $2000)<>0;
  116. _AESSupport:=(_ecx and $2000000)<>0;
  117. _AVXSupport:=
  118. { XGETBV suspport? }
  119. ((_ecx and $08000000)<>0) and
  120. { xmm and ymm state enabled? }
  121. ((XGETBV(0) and %110)=%110) and
  122. { avx supported? }
  123. ((_ecx and $10000000)<>0);
  124. is_sse3_cpu:=(_ecx and $1)<>0;
  125. _FMASupport:=_AVXSupport and ((_ecx and $1000)<>0);
  126. asm
  127. movl $7,%eax
  128. movl $0,%ecx
  129. cpuid
  130. movl %ebx,_ebx
  131. end ['rax','rbx','rcx','rdx'];
  132. _AVX2Support:=_AVXSupport and ((_ebx and $20)<>0);
  133. end;
  134. function InterlockedCompareExchange128Support : boolean;inline;
  135. begin
  136. result:=_InterlockedCompareExchange128Support;
  137. end;
  138. function AESSupport : boolean;inline;
  139. begin
  140. result:=_AESSupport;
  141. end;
  142. function AVXSupport: boolean;inline;
  143. begin
  144. result:=_AVXSupport;
  145. end;
  146. function AVX2Support: boolean;inline;
  147. begin
  148. result:=_AVX2Support;
  149. end;
  150. function FMASupport: boolean;inline;
  151. begin
  152. result:=_FMASupport;
  153. end;
  154. begin
  155. SetupSupport;
  156. end.