/// <summary> /// Load available SIMD extensions. /// </summary> public static void LoadSimdExtensions() { const string AssemblyPath = "OpenGL.Simd.dll"; IntPtr getCpuInformationPtr = IntPtr.Zero; if (File.Exists(AssemblyPath) == false) return; try { getCpuInformationPtr = GetProcAddress.GetAddress(AssemblyPath, "GetCpuInformation"); } catch { /* Ignore exception, leave 'getCpuInformationPtr' equals to 'IntPtr.Zero' */ } // No CPU information? Ahi ahi ahi if (getCpuInformationPtr == IntPtr.Zero) return; GetCpuInformation getCpuInformation = (GetCpuInformation)Marshal.GetDelegateForFunctionPointer(getCpuInformationPtr, typeof(GetCpuInformation)); CpuInformation cpuInfo = new CpuInformation(); getCpuInformation(ref cpuInfo); if (cpuInfo.SimdSupport != SimdTechnology.None) { FieldInfo[] fields = typeof(Memory).GetFields(BindingFlags.Static | BindingFlags.NonPublic); foreach (FieldInfo fieldInfo in fields) { // Test for SSSE3 support if ((cpuInfo.SimdSupport & SimdTechnology.SSSE3) != 0) { string entryPoint = String.Format("{0}_{1}", fieldInfo.Name, "SSSE3"); IntPtr address = GetProcAddress.GetAddress(AssemblyPath, entryPoint); if (address != IntPtr.Zero) { fieldInfo.SetValue(null, Marshal.GetDelegateForFunctionPointer(address, fieldInfo.FieldType)); continue; } } // Test for SSE3 support if ((cpuInfo.SimdSupport & SimdTechnology.SSE3) != 0) { string entryPoint = String.Format("{0}_{1}", fieldInfo.Name, "SSE3"); IntPtr address = GetProcAddress.GetAddress(AssemblyPath, entryPoint); if (address != IntPtr.Zero) { fieldInfo.SetValue(null, Marshal.GetDelegateForFunctionPointer(address, fieldInfo.FieldType)); continue; } } // Test for SSE2 support if ((cpuInfo.SimdSupport & SimdTechnology.SSE2) != 0) { string entryPoint = String.Format("{0}_{1}", fieldInfo.Name, "SSE2"); IntPtr address = GetProcAddress.GetAddress(AssemblyPath, entryPoint); if (address != IntPtr.Zero) { fieldInfo.SetValue(null, Marshal.GetDelegateForFunctionPointer(address, fieldInfo.FieldType)); continue; } } // Test for SSE support if ((cpuInfo.SimdSupport & SimdTechnology.SSE) != 0) { string entryPoint = String.Format("{0}_{1}", fieldInfo.Name, "SSE"); IntPtr address = GetProcAddress.GetAddress(AssemblyPath, entryPoint); if (address != IntPtr.Zero) { fieldInfo.SetValue(null, Marshal.GetDelegateForFunctionPointer(address, fieldInfo.FieldType)); continue; } } // Test for MMX support if ((cpuInfo.SimdSupport & SimdTechnology.MMX) != 0) { string entryPoint = String.Format("{0}_{1}", fieldInfo.Name, "MMX"); IntPtr address = GetProcAddress.GetAddress(AssemblyPath, entryPoint); if (address != IntPtr.Zero) { fieldInfo.SetValue(null, Marshal.GetDelegateForFunctionPointer(address, fieldInfo.FieldType)); continue; } } // Reset field fieldInfo.SetValue(null, null); } } }
/// <summary> /// Load available SIMD extensions. /// </summary> public static void LoadSimdExtensions() { const string AssemblyPath = "OpenGL.Simd.dll"; IntPtr getCpuInformationPtr = IntPtr.Zero; if (File.Exists(AssemblyPath) == false) { return; } try { getCpuInformationPtr = GetProcAddress.GetProcAddressOS.GetProcAddress(AssemblyPath, "GetCpuInformation"); } catch { /* Ignore exception, leave 'getCpuInformationPtr' equals to 'IntPtr.Zero' */ } // No CPU information? Ahi ahi ahi if (getCpuInformationPtr == IntPtr.Zero) { return; } GetCpuInformation getCpuInformation = (GetCpuInformation)Marshal.GetDelegateForFunctionPointer(getCpuInformationPtr, typeof(GetCpuInformation)); CpuInformation cpuInfo = new CpuInformation(); getCpuInformation(ref cpuInfo); if (cpuInfo.SimdSupport != SimdTechnology.None) { FieldInfo[] fields = typeof(Memory).GetFields(BindingFlags.Static | BindingFlags.NonPublic); foreach (FieldInfo fieldInfo in fields) { // Test for SSSE3 support if ((cpuInfo.SimdSupport & SimdTechnology.SSSE3) != 0) { string entryPoint = String.Format("{0}_{1}", fieldInfo.Name, "SSSE3"); IntPtr address = GetProcAddress.GetProcAddressOS.GetProcAddress(AssemblyPath, entryPoint); if (address != IntPtr.Zero) { fieldInfo.SetValue(null, Marshal.GetDelegateForFunctionPointer(address, fieldInfo.FieldType)); continue; } } // Test for SSE3 support if ((cpuInfo.SimdSupport & SimdTechnology.SSE3) != 0) { string entryPoint = String.Format("{0}_{1}", fieldInfo.Name, "SSE3"); IntPtr address = GetProcAddress.GetProcAddressOS.GetProcAddress(AssemblyPath, entryPoint); if (address != IntPtr.Zero) { fieldInfo.SetValue(null, Marshal.GetDelegateForFunctionPointer(address, fieldInfo.FieldType)); continue; } } // Test for SSE2 support if ((cpuInfo.SimdSupport & SimdTechnology.SSE2) != 0) { string entryPoint = String.Format("{0}_{1}", fieldInfo.Name, "SSE2"); IntPtr address = GetProcAddress.GetProcAddressOS.GetProcAddress(AssemblyPath, entryPoint); if (address != IntPtr.Zero) { fieldInfo.SetValue(null, Marshal.GetDelegateForFunctionPointer(address, fieldInfo.FieldType)); continue; } } // Test for SSE support if ((cpuInfo.SimdSupport & SimdTechnology.SSE) != 0) { string entryPoint = String.Format("{0}_{1}", fieldInfo.Name, "SSE"); IntPtr address = GetProcAddress.GetProcAddressOS.GetProcAddress(AssemblyPath, entryPoint); if (address != IntPtr.Zero) { fieldInfo.SetValue(null, Marshal.GetDelegateForFunctionPointer(address, fieldInfo.FieldType)); continue; } } // Test for MMX support if ((cpuInfo.SimdSupport & SimdTechnology.MMX) != 0) { string entryPoint = String.Format("{0}_{1}", fieldInfo.Name, "MMX"); IntPtr address = GetProcAddress.GetProcAddressOS.GetProcAddress(AssemblyPath, entryPoint); if (address != IntPtr.Zero) { fieldInfo.SetValue(null, Marshal.GetDelegateForFunctionPointer(address, fieldInfo.FieldType)); continue; } } // Reset field fieldInfo.SetValue(null, null); } } }