cpu.pp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. { returns true, if the processor supports the cpuid instruction }
  16. function cpuid_support : boolean;
  17. { returns true, if floating point is done by an emulator }
  18. function floating_point_emulation : boolean;
  19. { returns the contents of the cr0 register }
  20. function cr0 : longint;
  21. function AVXSupport: boolean;inline;
  22. var
  23. is_sse3_cpu : boolean = false;
  24. implementation
  25. {$ASMMODE INTEL}
  26. var
  27. _AVXSupport : boolean;
  28. function cpuid_support : boolean;assembler;
  29. {
  30. Check if the ID-flag can be changed, if changed then CpuID is supported.
  31. Tested under go32v1 and Linux on c6x86 with CpuID enabled and disabled (PFV)
  32. }
  33. asm
  34. push ebx
  35. pushfd
  36. pushfd
  37. pop eax
  38. mov ebx,eax
  39. xor eax,200000h
  40. push eax
  41. popfd
  42. pushfd
  43. pop eax
  44. popfd
  45. and eax,200000h
  46. and ebx,200000h
  47. cmp eax,ebx
  48. setnz al
  49. pop ebx
  50. end;
  51. function cr0 : longint;assembler;
  52. asm
  53. DB 0Fh,20h,0C0h
  54. { mov eax,cr0
  55. special registers are not allowed in the assembler
  56. parsers }
  57. end;
  58. function floating_point_emulation : boolean;
  59. begin
  60. {!!!! I don't know currently the position of the EM flag }
  61. { $4 after Ralf Brown's list }
  62. floating_point_emulation:=(cr0 and $4)<>0;
  63. end;
  64. {$ASMMODE ATT}
  65. function XGETBV(i : dword) : int64;assembler;
  66. asm
  67. movl %eax,%ecx
  68. // older FPCs don't know the xgetbv opcode
  69. .byte 0x0f,0x01,0xd0
  70. end;
  71. procedure SetupSupport;
  72. var
  73. _ecx : longint;
  74. begin
  75. is_sse3_cpu:=false;
  76. if cpuid_support then
  77. begin
  78. asm
  79. pushl %ebx
  80. movl $1,%eax
  81. cpuid
  82. movl %ecx,_ecx
  83. popl %ebx
  84. end;
  85. is_sse3_cpu:=(_ecx and $1)<>0;
  86. _AVXSupport:=
  87. { XGETBV suspport? }
  88. ((_ecx and $08000000)<>0) and
  89. { xmm and ymm state enabled? }
  90. ((XGETBV(0) and %110)=%110) and
  91. { avx supported? }
  92. ((_ecx and $10000000)<>0);
  93. end;
  94. end;
  95. function AVXSupport: boolean;inline;
  96. begin
  97. result:=_AVXSupport;
  98. end;
  99. begin
  100. SetupSupport;
  101. end.