Question

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?
Was it helpful?

Solution

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.

OTHER TIPS

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.

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.

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.

  1. If some program uses for example SSE instruction can it be run on CPU that doesn't support SSE?

No. But in general this often generates a trap or exception, and the trap/interrupt handler can process that if needed.

For example long time ago softwares often contain code for x87. If the x87 coprocessor exists, the instruction will run normally in hardware, but if the computer lacks an x87 coprocessor then it'll generate a trap, after that the instruction will be processed in software and return result as normal. See What is the protocol for x87 floating point emulation in MS-DOS?

First versions of Hackintosh also use this to emulate SSE2 on CPUs that don't support this instruction set. Of course the performance is terrible but it will run.

  1. 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)?

Yes. But after a few years, perhaps softwares need updates, right? For a critical performance software, a rewrite to take advantage of the new instruction set may be needed. For some others, the increase in performance may not be noticeable

  1. 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?

Depends on the compiler and options you pass to it at compile time.

Modern compilers support so they'll detect common idioms and optimize it. You just need to recompile to take advantage of the new instruction set. But for complex cases you still need to hand-optimize using SIMD intrinsics

If you use an external library, you'll automatically get a speed improvement when the library is updated to support the new instruction set, even if you don't do anything with your program

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top