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

前端 未结 3 1278
面向向阳花
面向向阳花 2021-02-15 10:48

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/exten

3条回答
  •  不思量自难忘°
    2021-02-15 11:04

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

提交回复
热议问题