Browse Source

+ centralized cpu capability detection

florian 1 year ago
parent
commit
704ad21b23
2 changed files with 35 additions and 0 deletions
  1. 3 0
      rtl/x86_64/cpuh.inc
  2. 32 0
      rtl/x86_64/x86_64.inc

+ 3 - 0
rtl/x86_64/cpuh.inc

@@ -17,6 +17,9 @@
 const
   {$I cpuinnr.inc}
 
+  has_avx_support : boolean = false;
+  has_avx2_support : boolean = false;
+
 function fpc_x86_inportb(port : word) : byte;[internproc:fpc_in_x86_inportb];
 function fpc_x86_inportw(port : word) : word;[internproc:fpc_in_x86_inportw];
 function fpc_x86_inportl(port : word) : longint;[internproc:fpc_in_x86_inportl];

+ 32 - 0
rtl/x86_64/x86_64.inc

@@ -1249,6 +1249,8 @@ const
 
 {$define FPC_SYSTEM_HAS_FPC_CPUINIT}
 procedure fpc_cpuinit;
+  var
+    _eax,_ebx,_ecx : dword;
   begin
     { don't let libraries influence the FPU cw set by the host program }
     if IsLibrary then
@@ -1257,6 +1259,36 @@ procedure fpc_cpuinit;
         DefaultMXCSR:=GetMXCSR;
       end;
     SysResetFPU;
+    asm
+      xorl %eax,%eax
+      cpuid
+      movl %eax,_eax
+    end;
+    if _eax>=7 then
+      begin
+        asm
+          xorl %ecx,%ecx
+          .byte   0x0f,0x01,0xd0 { xgetbv }
+          movl %eax,_eax
+        end;
+        if (_eax and 6)=6 then
+          begin
+            asm
+              movl $1,%eax
+              xorl %ecx,%ecx
+              cpuid
+              movl %ecx,_ecx
+            end;
+            has_avx_support:=(_ecx and $10000000)<>0;
+            asm
+              movl $7,%eax
+              xorl %ecx,%ecx
+              cpuid
+              movl %ebx,_ebx
+            end;
+            has_avx2_support:=(_ebx and $20)<>0;
+          end;
+      end;
   end;
 
 {$define FPC_SYSTEM_HAS_SYSINITFPU}