cpu.pp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. var
  33. _AESSupport,
  34. _AVXSupport,
  35. _InterlockedCompareExchange128Support,
  36. _AVX2Support,
  37. _FMASupport : boolean;
  38. function InterlockedCompareExchange128(var Target: Int128Rec; NewValue: Int128Rec; Comperand: Int128Rec): Int128Rec; assembler;
  39. {
  40. win64:
  41. rcx ... pointer to result
  42. rdx ... target
  43. r8 ... NewValue
  44. r9 ... Comperand
  45. }
  46. {$ifdef win64}
  47. asm
  48. pushq %rbx
  49. { store result pointer for later use }
  50. pushq %rcx
  51. { load new value }
  52. movq (%r8),%rbx
  53. movq 8(%r8),%rcx
  54. { save target pointer for later use }
  55. movq %rdx,%r8
  56. { load comperand }
  57. movq (%r9),%rax
  58. movq 8(%r9),%rdx
  59. {$ifdef oldbinutils}
  60. .byte 0xF0,0x49,0x0F,0xC7,0x08
  61. {$else}
  62. lock cmpxchg16b (%r8)
  63. {$endif}
  64. { restore result pointer }
  65. popq %rcx
  66. { store result }
  67. movq %rax,(%rcx)
  68. movq %rdx,8(%rcx)
  69. popq %rbx
  70. end;
  71. {$else win64}
  72. {
  73. linux:
  74. rdi ... target
  75. [rsi:rdx] ... NewValue
  76. [rcx:r8] ... Comperand
  77. [rdx:rax] ... result
  78. }
  79. asm
  80. pushq %rbx
  81. movq %rsi,%rbx // new value low
  82. movq %rcx,%rax // comperand low
  83. movq %rdx,%rcx // new value high
  84. movq %r8,%rdx // comperand high
  85. {$ifdef oldbinutils}
  86. .byte 0xF0,0x48,0x0F,0xC7,0x0F
  87. {$else}
  88. lock cmpxchg16b (%rdi)
  89. {$endif}
  90. popq %rbx
  91. end;
  92. {$endif win64}
  93. function XGETBV(i : dword) : int64;assembler;
  94. asm
  95. {$ifndef win64}
  96. movq %rdi,%rcx
  97. {$endif win64}
  98. // older FPCs don't know the xgetbv opcode
  99. .byte 0x0f,0x01,0xd0
  100. andl $0xffffffff,%eax
  101. shlq $32,%rdx
  102. orq %rdx,%rax
  103. end;
  104. procedure SetupSupport;
  105. var
  106. _ecx,
  107. _ebx : longint;
  108. begin
  109. asm
  110. pushq %rbx
  111. movl $0x00000001,%eax
  112. cpuid
  113. movl %ecx,_ecx
  114. popq %rbx
  115. end;
  116. _InterlockedCompareExchange128Support:=(_ecx and $2000)<>0;
  117. _AESSupport:=(_ecx and $2000000)<>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. pushq %rbx
  129. movl $7,%eax
  130. movl $0,%ecx
  131. cpuid
  132. movl %ebx,_ebx
  133. popq %rbx
  134. end;
  135. _AVX2Support:=_AVXSupport and ((_ebx and $20)<>0);
  136. end;
  137. function InterlockedCompareExchange128Support : boolean;inline;
  138. begin
  139. result:=_InterlockedCompareExchange128Support;
  140. end;
  141. function AESSupport : boolean;inline;
  142. begin
  143. result:=_AESSupport;
  144. end;
  145. function AVXSupport: boolean;inline;
  146. begin
  147. result:=_AVXSupport;
  148. end;
  149. function AVX2Support: boolean;inline;
  150. begin
  151. result:=_AVX2Support;
  152. end;
  153. function FMASupport: boolean;inline;
  154. begin
  155. result:=_FMASupport;
  156. end;
  157. begin
  158. SetupSupport;
  159. end.