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

前端 未结 3 817
耶瑟儿~
耶瑟儿~ 2021-02-15 10:36

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 10:50

    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.

    0 讨论(0)
  • 2021-02-15 11:09

    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

    0 讨论(0)
  • 2021-02-15 11:10

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

    0 讨论(0)
提交回复
热议问题