cpu.pp 3.1 KB

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