cpu.pp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. var
  27. is_sse3_cpu : boolean = false;
  28. function InterlockedCompareExchange128(var Target: Int128Rec; NewValue: Int128Rec; Comperand: Int128Rec): Int128Rec;
  29. implementation
  30. var
  31. _AESSupport,
  32. _AVXSupport,
  33. _InterlockedCompareExchange128Support : boolean;
  34. function InterlockedCompareExchange128Support : boolean;inline;
  35. begin
  36. result:=_InterlockedCompareExchange128Support;
  37. end;
  38. function AESSupport : boolean;inline;
  39. begin
  40. result:=_AESSupport;
  41. end;
  42. function AVXSupport: boolean;
  43. begin
  44. result:=_AVXSupport;
  45. end;
  46. function InterlockedCompareExchange128(var Target: Int128Rec; NewValue: Int128Rec; Comperand: Int128Rec): Int128Rec; assembler;
  47. {
  48. win64:
  49. rcx ... pointer to result
  50. rdx ... target
  51. r8 ... NewValue
  52. r9 ... Comperand
  53. }
  54. {$ifdef win64}
  55. asm
  56. pushq %rbx
  57. { store result pointer for later use }
  58. pushq %rcx
  59. { load new value }
  60. movq (%r8),%rbx
  61. movq 8(%r8),%rcx
  62. { save target pointer for later use }
  63. movq %rdx,%r8
  64. { load comperand }
  65. movq (%r9),%rax
  66. movq 8(%r9),%rdx
  67. {$ifdef oldbinutils}
  68. .byte 0xF0,0x49,0x0F,0xC7,0x08
  69. {$else}
  70. lock cmpxchg16b (%r8)
  71. {$endif}
  72. { restore result pointer }
  73. popq %rcx
  74. { store result }
  75. movq %rax,(%rcx)
  76. movq %rdx,8(%rcx)
  77. popq %rbx
  78. end;
  79. {$else win64}
  80. {
  81. linux:
  82. rdi ... target
  83. [rsi:rdx] ... NewValue
  84. [rcx:r8] ... Comperand
  85. [rdx:rax] ... result
  86. }
  87. asm
  88. pushq %rbx
  89. movq %rsi,%rbx // new value low
  90. movq %rcx,%rax // comperand low
  91. movq %rdx,%rcx // new value high
  92. movq %r8,%rdx // comperand high
  93. {$ifdef oldbinutils}
  94. .byte 0xF0,0x48,0x0F,0xC7,0x0F
  95. {$else}
  96. lock cmpxchg16b (%rdi)
  97. {$endif}
  98. popq %rbx
  99. end;
  100. {$endif win64}
  101. function XGETBV(i : dword) : int64;assembler;
  102. asm
  103. {$ifndef win64}
  104. movq %rdi,%rcx
  105. {$endif win64}
  106. // older FPCs don't know the xgetbv opcode
  107. .byte 0x0f,0x01,0xd0
  108. andl $0xffffffff,%eax
  109. shll $32,%rdx
  110. orq %rdx,%rax
  111. end;
  112. procedure SetupSupport;
  113. var
  114. _ecx : longint;
  115. begin
  116. asm
  117. pushq %rbx
  118. movl $0x00000001,%eax
  119. cpuid
  120. movl %ecx,_ecx
  121. popq %rbx
  122. end;
  123. _InterlockedCompareExchange128Support:=(_ecx and $2000)<>0;
  124. _AESSupport:=(_ecx and $2000000)<>0;
  125. _AVXSupport:=
  126. { XGETBV suspport? }
  127. ((_ecx and $08000000)<>0) and
  128. { xmm and ymm state enabled? }
  129. ((XGETBV(0) and %110)=%110) and
  130. { avx supported? }
  131. ((_ecx and $10000000)<>0);
  132. is_sse3_cpu:=(_ecx and $1)<>0;
  133. end;
  134. begin
  135. SetupSupport;
  136. end.