Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
628 views
in Technique[技术] by (71.8m points)

c - Difference in gcc -ffp-contract options

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)?
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 STDC FP_CONTRACT (cppreference) 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 STDC FP_CONTRACT. This is the default for -std=c89.
  • -ffp-contract=on: Enable contraction by default and honor #pragma STDC 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). This is the default for -std=gnu99 and other GNU dialects of C.

Unfortunately, #pragma STDC FP_CONTRACT is not yet implemented in GCC, so for now gcc -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, as you can see. GCC only supports off and fast, with on mapping to off.


What the C11 standard has to say about #pragma STDC 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 <math.h> provides a way to disallow contracted expressions. Otherwise, whether and how expressions are contracted is implementation-defined.90)

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...