views:

331

answers:

9

Hi, I am curious, do new compilers use some extra features built into new CPUs such as MMX SSE,3DNow! and so?

I mean, in original 8086 there was even no FPU, so compiler that old cannot even use it, but new compilers can, since FPU is part of every new CPU. So, does new compilers use new features of CPU?

Or, it should be more right to ask, does new C/C++ standart library functions use new features?

Thanks for answer.

EDIT:

OK, so, if I get all of you right,even some standart operations, especially with float numbers can be done using SSE faster.

In order to use it, I must enable this feature in my compiler, if it supports it. If it does, I must be sure that targeted platform supports that features.

In case of some system libraries that require top performance, such as OpenGL, DirectX and so, this support may be supported in system.

By default, for compatibility reasons, compiler doesen´t support it, but you can add this support using special C functions delivered by, for example Intel. This should be the best way, since you can directly control wheather and when you use special features of desired platform, to write multi-CPU-support applications.

+4  A: 

gcc will support newer instructions via command line arguments. See here for more info. To quote:

GCC can take advantage of the additional instructions in the MMX, SSE, SSE2, SSE3 and 3dnow extensions of recent Intel and AMD processors. The options -mmmx, -msse, -msse2, -msse3 and -m3dnow enable the use of these extra instructions, allowing multiple words of data to be processed in parallel. The resulting executables will only run on processors supporting the appropriate extensions--on other systems they will crash with an Illegal instruction error (or similar)

Brian Agnew
+1  A: 

Compilers will aim for producing code for a minimal set of features in a processor. They also provide compilation switches that allow you to target specific processors. In this manner, they can sell more compilers (to those folks with old processors as well as the trendy folk with new ones).

You will need to study the documentation that came with your compiler.

Thomas Matthews
A: 

Varying compilers will use varying new features. Visual Studio will use SSE/2, and I believe the Intel compiler will support the very latest in CPU features. You should, of course, be wary about the market penetration of your favourite feature.
As for what your favourite standard library use, that depends on what it was compiled with. However, C++ standard library is typically compiled on-site, since it's very heavily templated, so if you enable SSE2, the C++ std libs should use it. As for the CRT, depends on what they were compiled with.

DeadMG
A: 

There are generally two ways a compiler can generate code that uses special features like these:

  1. When the compiler itself is compiled, you configure it to generate code for a particular architecture, and it can take advantage of any features it knows that architecture will have. For example, if it gcc is configured for an Intel processor new enough (or is that "not old enough"?) to contain an integrated FPU, it will generate floating-point instructions.
  2. When the compiler is invoked, flags or parameters can specify the type of features available to the processor that will run the program, and then the compiler will know it is safe to use these features. If the flags aren't present, it will generate equivalent code without using the special instructions provided by those features.
Nick Meyer
*"When the compiler itself is compiled"* Uhm, why does this need to happen as early as when *compiling* the compiler?
Viktor Sehr
@Viktor it doesn't. My point is that when you build the compiler, it knows automatically some basic things about its target platform based on whether you configure it to generate executables for 8086, Pentium, PowerPC, SPARC, whatever. If you build a compiler to generate Pentium-compatible executables, it can use special features introduced to the x86 architecture before then that weren't in the 386, for example, even though they're in the same family.
Nick Meyer
+2  A: 

These instructions are not part of any ISO C/C++ standards. They are available through compiler intrinsics, depending on the compiler used.

For MSVC, see http://msdn.microsoft.com/en-us/library/26td21ds(VS.80).aspx

For GCC, you could look at http://developer.apple.com/hardwaredrivers/ve/sse.html

AFAIK, SSE intrinsics are the same between GCC and MSVC.

rotoglup
+1  A: 

Sometimes the runtime library will contain multiple implementations of a feature, and the library will dynamically choose between implementations when the program is run. The overhead might be the cost of a function pointer call instead of a direct function call, but the benefit could be much greater when using a CPU-specific optimised function.

JIT compilers (for VM languages such as Java and C#) take this one step further and compile the bytecode for the specific CPU that it's running on. This gives your own code the benefit of specific CPU optimisation. This is one reason why Java code can actually be faster than compiled C code, because the Java JIT compiler can delay its optimisation decisions until the program is run on the actual target machine. A C compiler must make those decisions without always knowing what the target CPU is. Furthermore, JIT compilers evolve and can make your program faster over time without you having to do anything.

Greg Hewgill
A: 

If you're talking about code written in C/C++, the new features are explited if you tell to your compiler to do so. By default, your compiler probably targets "plain x86" (naturally with FPU :) ), usually optimized for the most widespread processor generation at the moment, but still able to run on older processors.

If you want the compiler to generate code also considering the new instruction sets, you should tell it to do so with the appropriate command line switch/project setting, for example for Visual C++ the option to enable SSE/SSE2 instructions generation is /arch.

Notice that many features of new instruction sets cannot be exploited directly in "normal" code, so you are usually provided with compiler intrinsics to operate on the particular datatypes native of the new instruction sets.

Matteo Italia
+1  A: 

If you use the Intel C compiler, and set sufficiently high optimisation options, you will find that some of your loops get 'vectorised', which means the compiler has rewritten them to use SSE-style instructions.

If you want to use SSE operations directly, you use the intrinsics defined in the 'xmmintrin.h' header file; say

#include <xmmintrin.h>

__m128 U, V, W; float ww[4];

V=_mm_set1_ps(1.5);

U=_mm_set_ps(0,1,2,3);

W=_mm_add_ps(U,V);

_mm_storeu_ps(ww,W);

Tom Womack
A: 

Intel provides updated CPUID example code every time they release a new cpu so that you can check for the new features and has been as long as I remember. At least this is what I found the first time I thought about this same question myself.

Using CPUID to Detect the presence of SSE 4.1 and SSE 4.2 Instruction Sets

As new compilers are released they add the new features directly like VS2010 for example. Visual C++ Code Generation in Visual Studio 2010

daveangel