Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

New instruction sets in CPU

Every new generation of CPU introduces some sets of new instructions, i.e. MMX, 3DNOW, SSE and so on.

I've got few general questions about them:

  1. If some program uses for example SSE instruction can it be run on CPU that doesn't support SSE?
  2. If yes, does it mean that those instruction will be changed to some greater number of simpler instructions?
  3. If not, does it mean that the real performance impact of such new instructions will be after few years when most CPU will support such technology (so there won't be any incompatibilities)?
  4. When I compile a C++ program with optimizations does it mean that it'll use some of this new instructions? (I know that it depends on many factors, especially on the code, but I want some general answer). Or are they reserved mostly for programs written in asm?
like image 724
Tomek Tarczynski Avatar asked Mar 27 '10 16:03

Tomek Tarczynski


2 Answers

  1. Yes and no: The CPU will consider them to be invalid, but if the program checks if the CPU supports those instructions, then it can fallback to a version that doesn't use those instructions, allowing the program to be run anyway.
  2. The program will have to provide an alternative implementation using the more "basic" instructions, and know when to use which one.
  3. Since the program can check the CPU, the benefits can be available right now, but of course, if your users use CPUs that don't support those instructions, they won't see any benefit.
  4. This will depend entirely on the compiler and the optimizer. Some of the instructions sets may be considered old enough that the compiler will always use them unless you tell it not to, while other will be the opposite: you have to tell the compiler to use them. Whether or not it will automatically create fallbacks as well is also going to depend on the compiler.
like image 64
Michael Madsen Avatar answered Oct 19 '22 19:10

Michael Madsen


To elaborate on Michael Madsen's answer for question 4, GCC defaults to generating code for an i386 processor. It provides a flag called -march (also known as -mcpu) that determines which kinds of instructions the compiler will emit. Microsoft's cl.exe provides /arch: and /Gx flags for the same purpose.

The flag also affects how the instructions are ordered, because different CPUs can be relatively slower or faster executing a given piece of code, depending on the order in which the instructions appear.

I'm not aware of any static compiler that will create automatic feature-set fallback code. Usually that must be done explicitly by the programmer. But the good news is that that programmer need not be you; for example, the liboil library (of Optimized Inner Loops) will, at runtime, select the best code to run depending on the machine it's being run on.

like image 2
Ben Karel Avatar answered Oct 19 '22 20:10

Ben Karel