cpu.pp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 InterlockedCompareExchange128(var Target: Int128Rec; NewValue: Int128Rec; Comperand: Int128Rec): Int128Rec;
  26. implementation
  27. var
  28. _AESSupport,
  29. _InterlockedCompareExchange128Support : boolean;
  30. function InterlockedCompareExchange128Support : boolean;inline;
  31. begin
  32. result:=_InterlockedCompareExchange128Support;
  33. end;
  34. function AESSupport : boolean;inline;
  35. begin
  36. result:=_AESSupport;
  37. end;
  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. procedure SetupSupport;
  94. var
  95. _ecx : longint;
  96. begin
  97. asm
  98. pushq %rbx
  99. movl $0x00000001,%eax
  100. cpuid
  101. movl %ecx,_ecx
  102. popq %rbx
  103. end;
  104. _InterlockedCompareExchange128Support:=(_ecx and $2000)<>0;
  105. _AESSupport:=(_ecx and $2000000)<>0;
  106. end;
  107. begin
  108. SetupSupport;
  109. end.