j riv
j riv

Reputation: 3699

Is it possible to compile something which automatically utilises different processors?

Is it possible to compile something which automatically utilises the different processors' it runs on instruction sets?

Ideally on gcc and c, without doing it explicitly in code.

Will -mtune (alone) do it?

Upvotes: 1

Views: 56

Answers (1)

user57368
user57368

Reputation: 5765

Recent versions of gcc have -march=native, which will generate code to use all the instruction set extensions available on the machine that is running the compiler, but it does not generate a binary that detects at runtime which extensions are available. It is possible to write code that will detect which extensions are available and use different code paths based on that (this is a common technique for performance-sensitive code like game engines), but I don't know of any tools that automatically generate this code and the different versions of your code.

If you want to create a binary that can run on multiple different architectures (like x86 and ARM), you need to create something called a fat binary, or in Apple's marketing lingo, a universal binary: this requires compiling your code once for each architecture, and linking the binaries for the different architectures into the same file. This only works on operating systems that support fat binaries, because the operating system needs to know that it has to load only the segments for the right architecture.

Upvotes: 3

Related Questions