cpu.pp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. asm
  41. pushq %rbx
  42. { store result pointer for later use }
  43. pushq %rcx
  44. { load new value }
  45. movq (%r8),%rbx
  46. movq 8(%r8),%rcx
  47. { save target pointer for later use }
  48. movq %rdx,%r8
  49. { load comperand }
  50. movq (%r9),%rax
  51. movq 8(%r9),%rdx
  52. {$ifdef oldbinutils}
  53. .byte 0xF0,0x49,0x0F,0xC7,0x08
  54. {$else}
  55. lock cmpxchg16b (%r8)
  56. {$endif}
  57. { restore result pointer }
  58. popq %rcx
  59. { store result }
  60. movq %rax,(%rcx)
  61. movq %rdx,8(%rcx)
  62. popq %rbx
  63. end;
  64. procedure SetupSupport;
  65. var
  66. _ecx : longint;
  67. begin
  68. asm
  69. pushq %rbx
  70. movl $0x00000001,%eax
  71. cpuid
  72. movl %ecx,_ecx
  73. popq %rbx
  74. end;
  75. _InterlockedCompareExchange128Support:=(_ecx and $2000)<>0;
  76. end;
  77. begin
  78. SetupSupport;
  79. end.