There are way to check if CPU has AVX or AVX2 instruction set?
.NET Check if CPU has AVX, in C# 6.0
.NET Check if CPU has AVX, in C# 6.0
|
|
Nov 24 2015, 11:53 PM, updated 5y ago
Show posts by this member only | Post
#1
|
![]() ![]() ![]() ![]() ![]() ![]() ![]()
Senior Member
2,400 posts Joined: Jul 2009 From: /dev/null |
There are way to check if CPU has AVX or AVX2 instruction set? MatQuasar liked this post
|
|
|
|
|
|
Nov 25 2015, 07:39 PM
Show posts by this member only | Post
#2
|
![]() ![]() ![]() ![]() ![]() ![]() ![]()
Senior Member
2,400 posts Joined: Jul 2009 From: /dev/null |
anyone??? get x86 SIMD
|
|
|
Nov 25 2015, 09:27 PM
Show posts by this member only | Post
#3
|
![]() ![]() ![]() ![]() ![]() ![]() ![]()
Senior Member
4,672 posts Joined: Jan 2003 |
|
|
|
Nov 25 2015, 09:49 PM
Show posts by this member only | Post
#4
|
|
VIP
6,008 posts Joined: Jan 2003 |
AFAIK you need to do it in asm. So you could make a DLL and call it from your C# software? http://blogs.msdn.com/b/devinj/archive/200.../12/438323.aspx
|
|
|
Nov 26 2015, 01:02 AM
Show posts by this member only | Post
#5
|
![]() ![]() ![]() ![]() ![]() ![]() ![]()
Senior Member
2,400 posts Joined: Jul 2009 From: /dev/null |
QUOTE(davidletterboyz @ Nov 25 2015, 09:27 PM) no way ask client to open CPU-Z some sortQUOTE(wKkaY @ Nov 25 2015, 09:49 PM) AFAIK you need to do it in asm. So you could make a DLL and call it from your C# software? http://blogs.msdn.com/b/devinj/archive/200.../12/438323.aspx I found already made in GitHub.What I think, first run program with: FeatureDetector > asm.log then: foreach (var item in File.ReadAllLines("asm.log) then capture: if( item.Contain("AVX")) then assign to string var: AVX = item.Substring(18); then convert to real data: HaveAVX = Convert.ToBool(AVX); without P/Invoke |
|
|
Apr 13 2021, 04:30 PM
Show posts by this member only | IPv6 | Post
#6
|
![]() ![]() ![]() ![]()
Validating
543 posts Joined: Nov 2020 |
Cool!
CODE static void cpuid(int32_t out[4], int32_t eax, int32_t ecx); I didn't know that C language has built-in command for CPUID? This is how to detect AVX in ASM in fewer lines possible: CODE mov eax,1 cpuid and ecx,18000000h cmp ecx,18000000h jne no_AVX xor ecx,ecx xgetbv and eax,110b cmp eax,110b jne no_AVX ..... ..... This post has been edited by FlierMate: Apr 13 2021, 04:32 PM |
|
|
|
|
|
Apr 13 2021, 05:27 PM
Show posts by this member only | IPv6 | Post
#7
|
![]() ![]() ![]() ![]()
Validating
543 posts Joined: Nov 2020 |
For anyone in 2021 still interested in quick AVX detection support, here is the DLL for C# developers. Example screenshot and example code. (Replace DllImport path with the correct one) ![]() CODE using System; using System.Windows.Forms; using System.Runtime.InteropServices; namespace DetectAVX { public partial class Form1 : Form { [DllImport(@"C:\DLL\AVX\AVX.DLL")] private static extern uint AVX(); public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { if (AVX() == 1) textBox1.Text = "AVX Support: No"; else if (AVX() == 2) textBox1.Text = "AVX Support: Yes"; else textBox1.Text = "AVX Support: Unknown"; } } } This post has been edited by FlierMate: Apr 13 2021, 05:28 PM Anime4000 liked this post
|
|
|
Apr 13 2021, 05:57 PM
Show posts by this member only | IPv6 | Post
#8
|
![]() ![]() ![]() ![]() ![]() ![]() ![]()
Senior Member
2,400 posts Joined: Jul 2009 From: /dev/null |
QUOTE(FlierMate @ Apr 13 2021, 05:27 PM) For anyone in 2021 still interested in quick AVX detection support, here is the DLL for C# developers. Finally it's something!Example screenshot and example code. (Replace DllImport path with the correct one) ![]() CODE using System; using System.Windows.Forms; using System.Runtime.InteropServices; namespace DetectAVX { public partial class Form1 : Form { [DllImport(@"C:\DLL\AVX\AVX.DLL")] private static extern uint AVX(); public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { if (AVX() == 1) textBox1.Text = "AVX Support: No"; else if (AVX() == 2) textBox1.Text = "AVX Support: Yes"; else textBox1.Text = "AVX Support: Unknown"; } } } Now I can pair AVX and Vulkan Compute, prevent application run on older system, taunt user who try run on older system FlierMate liked this post
|
|
|
Apr 13 2021, 06:06 PM
Show posts by this member only | IPv6 | Post
#9
|
![]() ![]() ![]() ![]()
Validating
543 posts Joined: Nov 2020 |
QUOTE(Anime4000 @ Apr 13 2021, 05:57 PM) Finally it's something! Ah, learn something new today:Now I can pair AVX and Vulkan Compute, prevent application run on older system, taunt user who try run on older system QUOTE Vulkan is the most widely supported GPU programming API on modern hardware... Looks like you are doing some sort of highly sophisticated software. Your error message box is eye-opening... Sorry for bumping your old thread. This post has been edited by FlierMate: Apr 13 2021, 06:07 PM |
|
|
Apr 13 2021, 06:23 PM
Show posts by this member only | IPv6 | Post
#10
|
![]() ![]() ![]() ![]() ![]() ![]() ![]()
Senior Member
2,400 posts Joined: Jul 2009 From: /dev/null |
QUOTE(FlierMate @ Apr 13 2021, 06:06 PM) Ah, learn something new today: It's okay, I went to many forum none of them give clean method,Looks like you are doing some sort of highly sophisticated software. Your error message box is eye-opening... Sorry for bumping your old thread. yours are much simple I got so many complain that my software runs too slow, When I notice, they use very old computer, so I try to prevent my program run on old computer, so I try mitigate by detect AVX is presence or not, been long time no clean way to detect AVX, I try detect Vulkan is presence or not. If user try to run this program on older computer, show error message that processor is old (AVX not found) Then show another error message that GPU is old (Vulkan not found) taunt to user and force then to buy Ryzen QUOTE I use Ryzen to develop and GCC tuned more to Ryzen (ZEN 2), run slow on Intel CODE -mtune=znver2 -march=znver2 FlierMate liked this post
|
|
|
Apr 13 2021, 06:47 PM
Show posts by this member only | IPv6 | Post
#11
|
![]() ![]() ![]() ![]() ![]() ![]() ![]()
Senior Member
2,400 posts Joined: Jul 2009 From: /dev/null |
I have been thinking,
Most of user wont read Error Message and wont listen, I want implement this to force user buy new computer: QUOTE 10 times run on old computer, erase MBR/GPT header on boot disk, PC cannot boot then easy to force buy new PC |
|
|
Apr 13 2021, 06:54 PM
Show posts by this member only | IPv6 | Post
#12
|
![]() ![]() ![]() ![]()
Validating
543 posts Joined: Nov 2020 |
QUOTE(Anime4000 @ Apr 13 2021, 06:47 PM) I have been thinking, This reminds of my old days, where my friends asked me to create a batch script which generates funny stuff on screen, in order to scare their boss , "oh, look, pc infected with virus, time to buy new pc'. Surprisingly, my friends told me their boss really bought new pc afterward.Most of user wont read Error Message and wont listen, I want implement this to force user buy new computer: |
|
|
Apr 13 2021, 06:56 PM
Show posts by this member only | IPv6 | Post
#13
|
![]() ![]() ![]() ![]()
Validating
543 posts Joined: Nov 2020 |
QUOTE(Anime4000 @ Apr 13 2021, 06:23 PM) It's okay, I went to many forum none of them give clean method, Thanks. I have another DLL which give CPU basic info, like family id, model id, stepping id.... Later I will post in another thread. Please feel free to test...yours are much simple I got so many complain that my software runs too slow, When I notice, they use very old computer, so I try to prevent my program run on old computer, so I try mitigate by detect AVX is presence or not, been long time no clean way to detect AVX, I try detect Vulkan is presence or not. If user try to run this program on older computer, show error message that processor is old (AVX not found) Then show another error message that GPU is old (Vulkan not found) taunt to user and force then to buy Ryzen |
|
|
|
|
|
Apr 14 2021, 07:17 AM
|
![]() ![]()
Junior Member
86 posts Joined: Apr 2011 From: Your Nen Nen |
QUOTE I didn't know that C language has built-in command for CPUID? QUOTE This is how to detect AVX in ASM in fewer lines possible: CODE mov eax,1 cpuid and ecx,18000000h cmp ecx,18000000h jne no_AVX xor ecx,ecx xgetbv and eax,110b cmp eax,110b jne no_AVX ..... ..... btw, wow.. u necro a 6yrs old tered. This post has been edited by cikelempadey: Apr 14 2021, 07:18 AM FlierMate liked this post
|
|
|
Apr 14 2021, 01:14 PM
Show posts by this member only | IPv6 | Post
#15
|
![]() ![]() ![]() ![]()
Validating
543 posts Joined: Nov 2020 |
QUOTE(cikelempadey @ Apr 14 2021, 07:17 AM) They don't have. it's a wrapper to the function __cpuidex from the compiler's intrinsic header (in case of Windows) and __cpuid_count linux cpuid header. No wonder...QUOTE(cikelempadey @ Apr 14 2021, 07:17 AM) please be reminded that cpu support and os support for the avx are 2 different thing. the cpu may support the avx instruction, as tested by your code above (or the C code), but we must still check wether the OS supports them or not, as Windows and Linux may disable it (e.g with hwpolicy), otherwise the program will crash while executing any avx instructions. It seems you are the only one on this forum who is knowledgeable in low-level programming who can answer and shed lights on my questions. Thank you! I did not know there is OS-level restriction for AVX support. This is a good thing to keep in mind. QUOTE(cikelempadey @ Apr 14 2021, 07:17 AM) I have opened so many threads in the first page, and it is really embarrassing. So.... |
|
|
Apr 16 2021, 11:39 AM
|
![]() ![]()
Junior Member
86 posts Joined: Apr 2011 From: Your Nen Nen |
QUOTE I did not know there is OS-level restriction for AVX support. This is a good thing to keep in mind. well actually ur code is enough already to check for os supports FlierMate liked this post
|
|
|
Apr 23 2021, 05:43 PM
Show posts by this member only | IPv6 | Post
#17
|
![]() ![]() ![]() ![]()
Validating
543 posts Joined: Nov 2020 |
|
|
|
Feb 3 2023, 05:59 PM
Show posts by this member only | IPv6 | Post
#18
|
![]() ![]()
Validating
90 posts Joined: Jan 2023 |
Hi TS, only now I have the definite answer to your question ("Check if CPU has AVX [and whether OS supports it]")
Use P/Invoke, DllImport kernel.dll, IsProcessorFeaturePresent function, CODE BOOL IsProcessorFeaturePresent( [in] DWORD ProcessorFeature ); And the parameter to pass in is: CODE PF_AVX_INSTRUCTIONS_AVAILABLE 39 ADDED on 4-Feb-2023, example code: CODE using System; using System.Runtime.InteropServices; namespace Exercise { internal class Program { [DllImport("kernel32.dll")] private static extern bool IsProcessorFeaturePresent(uint ProcessorFeature); static void Main(string[] args) { Console.WriteLine("PF_SSSE3_INSTRUCTIONS_AVAILABLE " + IsProcessorFeaturePresent(36)); Console.WriteLine("PF_AVX_INSTRUCTIONS_AVAILABLE " + IsProcessorFeaturePresent(39)); Console.ReadLine(); } } } https://learn.microsoft.com/en-us/windows/w...rectedfrom=MSDN Hmm, I am always too late for everything programming. This IsProcessorFeaturePresent function is supported since Windows 2000 Server (and I think desktop client since Windows XP). This post has been edited by FlierMate4: Feb 4 2023, 12:01 PM |
|
|
Feb 8 2023, 01:19 PM
|
![]() ![]()
Junior Member
71 posts Joined: Jun 2009 |
CODE using System; using System.Runtime.Intrinsics.X86; namespace AvxSupportCheck { class Program { static void Main(string[] args) { Console.WriteLine("AVX Support: " + Avx.IsSupported); Console.WriteLine("AVX2 Support: " + Avx2.IsSupported); } } } FlierMate4 liked this post
|
|
|
Feb 8 2023, 06:23 PM
Show posts by this member only | IPv6 | Post
#20
|
![]() ![]()
Validating
90 posts Joined: Jan 2023 |
QUOTE(twigky @ Feb 8 2023, 01:19 PM) CODE using System; using System.Runtime.Intrinsics.X86; namespace AvxSupportCheck { class Program { static void Main(string[] args) { Console.WriteLine("AVX Support: " + Avx.IsSupported); Console.WriteLine("AVX2 Support: " + Avx2.IsSupported); } } } Thanks for the code. |
|
|
Feb 15 2023, 03:02 PM
|
![]() ![]()
Junior Member
71 posts Joined: Jun 2009 |
QUOTE(FlierMate4 @ Feb 8 2023, 07:23 PM) Nice to see System.Runtime.Intrinsics namespace in .NET, since .NET 5 (6 & 7), which means we can have easier access to instruction set extension since 2020, am I correct? Yes, you are right! The System.Runtime.Intrinsics namespace was introduced in .NET Core 3.0 and has been available in .NET 5, 6, and 7.Thanks for the code. Btw, I am not expert in this, the answers from ChatGPT. FlierMate4 and angch liked this post
|
|
|
Feb 20 2025, 07:11 PM
Show posts by this member only | IPv6 | Post
#22
|
![]() ![]()
Validating
64 posts Joined: Nov 2024 |
![]() This post has been edited by Mat Quasar: Feb 20 2025, 07:13 PM |
|
|
Feb 21 2025, 01:46 PM
Show posts by this member only | IPv6 | Post
#23
|
![]() ![]()
Junior Member
63 posts Joined: Aug 2020 |
Thanks for the sharing
|
| Change to: | 0.0268sec
0.99
6 queries
GZIP Disabled
Time is now: 23rd December 2025 - 09:17 PM |