Skip to content

Commit

Permalink
Low level AVX2 detection (#103)
Browse files Browse the repository at this point in the history
* Low level AVX2 detection

* Add DLL reference and autoupdate

* Fix race condition
  • Loading branch information
dimon222 authored May 3, 2020
1 parent 9a71daf commit ab4bfc2
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 15 deletions.
43 changes: 28 additions & 15 deletions WFInfo/CustomEntrypoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.IO;
using System.Net;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -68,8 +69,7 @@ public static void Main()
Directory.CreateDirectory(app_data_tesseract_catalog + @"\x64");

Directory.CreateDirectory(appdata_tessdata_folder);

AvxSupport = HasAvxSupport();
AvxSupport = isAVX2Available();

if (!AvxSupport)
{
Expand Down Expand Up @@ -134,23 +134,36 @@ public static void Main()
}

}
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
private delegate bool isAVX2supported();

// Detect if CPU has necessary optimizations
public static bool HasAvxSupport()
public static bool isAVX2Available()
{
if (File.Exists(appPath + @"/old_as_fuck.boys"))
{
Console.WriteLine("Yup, she old");
return false;
}
try
{
return (GetEnabledXStateFeatures() & 4) != 0;
}
catch
string dll = "CustomCPUID.dll";
string path = app_data_tesseract_catalog + @"\" + dll;
string md5 = "23b463f8811480e508f10ce5e984bf2f";
if (!File.Exists(path) || GetMD5hash(path) != md5)
{
return false;
WebClient webClient = new WebClient();
webClient.DownloadFile(tesseract_hotlink_prefix + "/" + dll, path);
}
IntPtr pDll = NativeMethods.LoadLibrary(path);
IntPtr pAddressOfFunctionToCall = NativeMethods.GetProcAddress(pDll, "isAVX2supported");
isAVX2supported isAvx2Supported = (isAVX2supported)Marshal.GetDelegateForFunctionPointer(
pAddressOfFunctionToCall,
typeof(isAVX2supported));
return isAvx2Supported();
}
static class NativeMethods
{
[DllImport("kernel32.dll")]
public static extern IntPtr LoadLibrary(string dllToLoad);

[DllImport("kernel32.dll")]
public static extern IntPtr GetProcAddress(IntPtr hModule, string procedureName);

[DllImport("kernel32.dll")]
public static extern bool FreeLibrary(IntPtr hModule);
}

public static string GetMD5hash(string filePath)
Expand Down
Binary file added WFInfo/lib/CustomCPUID.dll
Binary file not shown.

0 comments on commit ab4bfc2

Please sign in to comment.