Search code examples
c++coptimizationinstruction-set

Do I need to make multiple executables for targeting different instruction sets?


Consider I have a program to do AES operations.

Some advanced CPUs have AES-NI instruction set, and other CPUs don't have.

Must I compile my program into two executables: A_with_aes_ni.exe and B_without_aes_ni.exe ?


Solution

  • What you want is called a CPU dispatcher. Agner Fog has 10 pages of text on this in chapter 13 "Making critical code in multiple versions for different instruction sets" of his Optimizing C++ manual . He discusses doing this both with GCC and ICC.

    You only need one executable but you need to compile two different object files with and without AES enabled. Then the dispatcher determines what instruction set is available and chooses the code path based on that.

    I tried to do this with MSVC2010 cpu dispatcher for visual studio for AVX and SSE but did not succeed. I suspect I could get it working now though.

    Edit: In Agner Fog's vectorclass he has a file dispatch_example.cpp and instrset_detech.cpp which should have most of what you need to make a dispatcher. You still need to figure out how to detect if a CPU has AES. You need to augment the intrset_detect.cpp file. According to wikipedia when you read CPUID bit 23 in register ECX is set if the CPU has AES. Wikipedia also has code examples to read CPUID (besides instrset_detech.cpp - another good example is at https://github.com/Mysticial/Flops in the file cpuid.c)