cpu.pp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. var
  26. is_sse3_cpu : boolean = false;
  27. function InterlockedCompareExchange128(var Target: Int128Rec; NewValue: Int128Rec; Comperand: Int128Rec): Int128Rec;
  28. implementation
  29. var
  30. _AESSupport,
  31. _InterlockedCompareExchange128Support : boolean;
  32. function InterlockedCompareExchange128Support : boolean;inline;
  33. begin
  34. result:=_InterlockedCompareExchange128Support;
  35. end;
  36. function AESSupport : boolean;inline;
  37. begin
  38. result:=_AESSupport;
  39. end;
  40. function InterlockedCompareExchange128(var Target: Int128Rec; NewValue: Int128Rec; Comperand: Int128Rec): Int128Rec; assembler;
  41. {
  42. win64:
  43. rcx ... pointer to result
  44. rdx ... target
  45. r8 ... NewValue
  46. r9 ... Comperand
  47. }
  48. {$ifdef win64}
  49. asm
  50. pushq %rbx
  51. { store result pointer for later use }
  52. pushq %rcx
  53. { load new value }
  54. movq (%r8),%rbx
  55. movq 8(%r8),%rcx
  56. { save target pointer for later use }
  57. movq %rdx,%r8
  58. { load comperand }
  59. movq (%r9),%rax
  60. movq 8(%r9),%rdx
  61. {$ifdef oldbinutils}
  62. .byte 0xF0,0x49,0x0F,0xC7,0x08
  63. {$else}
  64. lock cmpxchg16b (%r8)
  65. {$endif}
  66. { restore result pointer }
  67. popq %rcx
  68. { store result }
  69. movq %rax,(%rcx)
  70. movq %rdx,8(%rcx)
  71. popq %rbx
  72. end;
  73. {$else win64}
  74. {
  75. linux:
  76. rdi ... target
  77. [rsi:rdx] ... NewValue
  78. [rcx:r8] ... Comperand
  79. [rdx:rax] ... result
  80. }
  81. asm
  82. pushq %rbx
  83. movq %rsi,%rbx // new value low
  84. movq %rcx,%rax // comperand low
  85. movq %rdx,%rcx // new value high
  86. movq %r8,%rdx // comperand high
  87. {$ifdef oldbinutils}
  88. .byte 0xF0,0x48,0x0F,0xC7,0x0F
  89. {$else}
  90. lock cmpxchg16b (%rdi)
  91. {$endif}
  92. popq %rbx
  93. end;
  94. {$endif win64}
  95. procedure SetupSupport;
  96. var
  97. _ecx : longint;
  98. begin
  99. asm
  100. pushq %rbx
  101. movl $0x00000001,%eax
  102. cpuid
  103. movl %ecx,_ecx
  104. popq %rbx
  105. end;
  106. _InterlockedCompareExchange128Support:=(_ecx and $2000)<>0;
  107. _AESSupport:=(_ecx and $2000000)<>0;
  108. is_sse3_cpu:=(_ecx and $1)<>0;
  109. end;
  110. begin
  111. SetupSupport;
  112. end.