views:

96

answers:

4

Every new generation of CPU introduces some sets of new instruction, ie.: 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 instuction will be changed to some greater number of simpler instuctions?
3) If not, does it mean that the real perfomance 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, especcialy on the code, but I want some general answer) Or are they reserved mostly for programs written in asm?

A: 

An executable that contains new instructions can only be executed on CPU's that support these new instructions. You can configure the compiler to compile for a specific CPU.

compie
Only if those instructions actually get executed. If they never get executed (for example, because they are behind a conditional jump which checks whether the current CPU supports them), then there's no problem. IOW: the CPU will trap on an illegal opcode if and only if it actually tries to execute one.
Jörg W Mittag
+6  A: 

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.

Michael Madsen
A: 

MMX has been around since 1996, SSE came out in 1999, and SSE2 debuted with the Pentium 4 in 2001. I think it's safe to assume that any CPU you'll be using has MMX and SSE, and probably SSE2. 3DNOW I think is AMD only, so don't expect those instructions to be available.

Gabe
+1  A: 

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.

Ben Karel