C/C++ usage of special CPU features
Asked Answered
R

9

7

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.

Rouault answered 17/5, 2010 at 21:39 Comment(2)
In your next Stack Overflow post, please add blank lines between your questions as this allows people to identify the questions easier (especially when developing answers for you).Kike
Consider that even a basic add is implemented differently on different processor families. The compiler goes through the same basic motions when choosing between PPC output and Intel output as it would when choosing between SSE output and non-SSE output.Disturbing
M
4

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)

Moltke answered 17/5, 2010 at 21:43 Comment(0)
A
2

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.

Acotyledon answered 17/5, 2010 at 21:46 Comment(0)
K
1

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.

Kike answered 17/5, 2010 at 21:43 Comment(0)
U
1

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.

Unmoving answered 17/5, 2010 at 21:46 Comment(1)
IMHO, I would rather die slowly in hell with C than to use Java to get CPU optimizations.Statehood
S
1

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);

Southwestwardly answered 17/5, 2010 at 21:55 Comment(0)
C
0

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.

Coons answered 17/5, 2010 at 21:44 Comment(0)
L
0

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.
Linville answered 17/5, 2010 at 21:46 Comment(2)
"When the compiler itself is compiled" Uhm, why does this need to happen as early as when compiling the compiler?Viviyan
@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.Linville
S
0

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.

Sexagenarian answered 17/5, 2010 at 21:48 Comment(0)
P
0

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

Patella answered 17/5, 2010 at 22:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.