Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is floating point speculation and how does it differ from the compiler's floating point model

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!

like image 891
Sideshow Bob Avatar asked Sep 14 '11 15:09

Sideshow Bob


2 Answers

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

like image 175
Stephen Canon Avatar answered Oct 22 '22 21:10

Stephen Canon


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

like image 1
Ben Voigt Avatar answered Oct 22 '22 19:10

Ben Voigt