8

The Intel C++ compiler provides two options for controlling floating point:

-fp-speculation (fast/safe/strict/off) -fp-model (precise/fast/strict and source/double/extended)

I think I understand what fp-model does. But what is fp-speculation and how does it relate to fp-model? I have yet to find any intel doc which explains this!

Sideshow Bob
  • 4,566
  • 5
  • 42
  • 79

3 Answers3

13

-fp-model influences how floating-point computations are carried out, and can change the numeric result (by licensing unsafe optimizations or by changing the precision at which intermediate results are evaluated).

-fp-speculation does not change the numerical results, but can effect what floating-point flags are raised by an operation (or what traps are taken if floating-point traps are enabled). 99.99% of programmers don't need care about these things, so you can probably run with the default and not worry about it.

Here's a concrete example; suppose you have the following function:

double foo(double x) {
    // lots of computation
    if (x >= 0) return sqrt(x);
    else return x;
}

sqrt is, relatively speaking, slow. It would be nice to hoist the computation of sqrt(x) like this:

double foo(double x) {
    const double sqrtx = sqrt(x);
    // lots of computation
    if (x >= 0) return sqrtx;
    else return x;
}

By doing this, we allow the computation of sqrt to proceed simultaneously with other computations, reducing the latency of our function. However, there's a problem; if x is negative, then sqrt(x) raises the invalid flag. In the original program, this could never happen, because sqrt(x) was only computed if x was non-negative. In the modified program, sqrt(x) is computed unconditionally. Thus, if x is negative, the modified program raises the invalid flag, whereas the original program did not.

The -fp-speculation flag gives you a way to tell the compiler whether or not you care about these cases, so it knows whether or not it is licensed to make such transformations.

Stephen Canon
  • 103,815
  • 19
  • 183
  • 269
  • 1
    Consider another variation, where `sqrt(x)` is evaluated unconditionally. Obviously the same optimization is applicable, but now instead of an extraneous FPU exception, we have an FPU exception occurring too early, possibly before other memory writes observable from the trap handler. Speculative execution can not only result in additional FPU exceptions but also change the timing of exceptions. – Ben Voigt Sep 14 '11 at 17:58
1

Out of order execution and speculative execution can result in extraneous exceptions or raise exceptions at the wrong time.

If that matters to you, you can use the fp-speculation option to control speculation of floating-point instructions.

For (a little bit) more information: http://software.intel.com/sites/products/documentation/hpc/compilerpro/en-us/fortran/lin/compiler_f/copts/common_options/option_fp_speculation.htm

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • 1
    The flag is controlling *software* speculation by the compiler, not hardware speculation execution. Speculative execution in an out-of-order processor *cannot* raise extraneous exceptions -- any spurious exceptions are patched up at the point of instruction retirement, so that the observable effect to any program is as though the instructions were executed in program order. – Stephen Canon Sep 14 '11 at 15:48
  • 2
    @Stephen: Yes, and the compiler CAN generate logic to patch things up also, but that slows down the program. Hence the different options to trade speed vs accurate exception behavior. – Ben Voigt Sep 14 '11 at 15:50
  • The compiler does not need to generate any code to "patch up" *hardware* speculation. That is handled in silicon. Here we are talking about the compiler doing things like hoisting long-latency operations across branch points (*software* speculation). – Stephen Canon Sep 14 '11 at 16:01
  • @Stephen: Right, speculation introduced by the hardware is also fixed up by the hardware. Instruction reordering done by the compiler (which is by nature speculative, due to asynchronous interrupts) has to be protected by the compiler (if at all). – Ben Voigt Sep 14 '11 at 17:55
-2

On Windows OS: 1.Intel compiler floating calculation 32 bit application vs 64 bit application , same code Can give to you different result!!!! No matter what flag you choose:)!!!!

2.Visual studio compiler floating calculation 32 bit vs 64 bit application , same code output same result.

user3004288
  • 17
  • 1
  • 5
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post - you can always comment on your own posts, and once you have [sufficient reputation](http://stackoverflow.com/help/privileges/comment) you will be able to comment on any post. – Scott Solmer Oct 13 '14 at 14:35
  • This is not critique or clarification . It simple facts from my expiriense and since I am working at Intel and dealing with Intel compiler I think I can write comments without "sufficient reputation" – user3004288 Oct 13 '14 at 19:42
  • The OP's question was "what is fp-speculation and how does it relate to fp-model?" Does your answer address this? If not, it would be better suited to a comment. – Scott Solmer Oct 14 '14 at 14:16