Difference in gcc -ffp-contract options

不问归期 提交于 2019-12-01 04:16:53

问题


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 written as follows:

-ffp-contract=off disables floating-point expression contraction. -ffp-contract=fast enables floating-point expression contraction such as forming of fused multiply-add operations if the target has native support for them. -ffp-contract=on enables floating-point expression contraction if allowed by the language standard. This is currently not implemented and treated equal to -ffp-contract=off. The default is -ffp-contract=fast.

Now the question:

  • What is the difference between fast and on?
  • Is there any other contraction example beside the FMA (or similar like the fused-mult sub)?

回答1:


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.



来源:https://stackoverflow.com/questions/43352510/difference-in-gcc-ffp-contract-options

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!