How do programs utilize new CPU instructions (e.g. AVX-512, Intel Quick Sync Video) while still being able to run on older CPUs?


New instruction sets can improve performance but it is difficult to distribute your program if most people’s computers cannot run the program. When compiling an executable to distribute, it either has code that uses or doesn’t certain CPU instructions. The only way around it I can think of is compiling multiple libraries for every function that may use a new instruction set, one with the new instruction set and other without. Then you detect which CPU you have and dynamically load the proper library at runtime.

In: 2

You are basically describing exactly what they do. There is an instruction called CPUID which can be used to get the features supported by the CPU. This is then used to switch between different versions of the code. It is not necessarily the entire library, maybe just a function or two. So it is generally not much work to keep two version.

You don’t need to do it at runtime. You do it when you compile the code for a target processor. The compiler will only use instructions that the target can execute. Any other instructions are replaced with functions that emulate it.

You have it basically right. If a program has to run on both new and older CPUs, they check the CPU capabilities at run time and run code with or without the new extensions as appropriate.

Sometimes code is written so that it requires certain extensions, for instance MMX is pretty much standard these days. For these programs, they don’t include non-MMX versions of libraries and such. They may perform a check when the program launches to display an error message, or if no check is done then the program will crash when attempting to run the unsupported code.