Difference in gcc -ffp-contract options

后端 未结 1 999
灰色年华
灰色年华 2020-12-18 04:56

I have a question regarding the -ffp-contract flag in GNU GCC (see https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html).

The flag documentation is

相关标签:
1条回答
  • 2020-12-18 05:46

    In C89, FP contraction is disallowed. Starting with C99, an implementation may do FP contraction of expressions by default, but would then be required to provide a #pragma FP_CONTRACT which can be toggled to influence contraction behavior.

    So the GCC switch was supposed to be:

    • -ffp-contract=off: Don't do contraction. Ignore #pragma FP_CONTRACT. This is the default for -std=c89.
    • -ffp-contract=on: Enable contraction by default and honor #pragma FP_CONTRACT. This would be the default for -std=c99 and above.
    • -ffp-contract=fast: This is the GCC default, even without any fast-math options. We don't claim ISO conformance in fast-math mode, so it's ok to always contract, even separate expressions (See Marc Glisse's comment).

    Unfortunately, #pragma FP_CONTRACT is not yet implemented in GCC, so for now -ffp-contract=on does what is necessary to stay ISO-conforming: nothing. i.e. on = off, because the only other behaviour GCC knows how to implement (fast) is too aggressive for on.

    You'd have to dig into the sources (or mailing list) to see what kind of contractions GCC is able to do, but it doesn't have to be limited to FMA.

    See GCC and Clang on Godbolt for a simple testcase of one expression vs. 2 statements. Clang defaults to -ffp-contract=off but supports on and fast. GCC only supports off and fast.


    What the C11 standard has to say about #pragma FP_CONTRACT:

    §6.5¶8: A floating expression may be contracted , that is, evaluated as though it were a single operation, thereby omitting rounding errors implied by the source code and the expression evaluation method.89) The FP_CONTRACT pragma in provides a way to disallow contracted expressions. Otherwise, whether and how expressions are contracted is implementation-defined.90)

    89) The intermediate operations in the contracted expression are evaluated as if to infinite range and precision, while the final operation is rounded to the format determined by the expression evaluation method. A contracted expression might also omit the raising of floating-point exceptions.

    90) This license is specifically intended to allow implementations to exploit fast machine instructions that combine multiple C operators. As contractions potentially undermine predictability, and can even decrease accuracy for containing expressions, their use needs to be well-defined and clearly documented.

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