SIMD has had an initialisation cost on Intel CPUs in the past. Because of this, I am looking for a way to distinguish at runtime in C++ which generation of Intel CPU is running my program.
Is there a simple way to discriminate all Intel CPUs older than Ice Lake?
NB: detecting whether the CPU running is an Intel CPU is fairly simple but sadly isn't enough for this use case.
Instead of manually checking for cpu attributes at runtime, you can let the compiler figure it out for your at load time with compiler target attributes (or function multi-versioning as GCC calls it).
Unfortunately it's not a standard feature even in between the "three big" compilers but both clang and gcc at least provide similar implementations.
There is a tutorial here that discusses workarounds for a couple of edge cases but in general this should be fairly transparent.
You can look at this code on Github but basically there are two ways to do it.
First with
targetyou can manually implement versions for each architectureSecond with
target_clonesis more convenient if all the implementations are similar in code as you select all possible architectures to version it.Once you run, the linked code will automagically select the right implementation for your target machine.
Read more: Documentation