Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)?
like image 493
Stefan Moosbrugger Avatar asked Apr 11 '17 17:04

Stefan Moosbrugger


People also ask

Which GCC option should be used?

When you invoke GCC, it normally does preprocessing, compilation, assembly and linking. The "overall options" allow you to stop this process at an intermediate stage. For example, the -c option says not to run the linker. Then the output consists of object files output by the assembler.

Does order of GCC flags matter?

Order does matter when you use several options of the same kind; for example, if you specify -L more than once, the directories are searched in the order specified. Also, the placement of the -l option is significant.


1 Answers

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.

like image 78
a3f Avatar answered Sep 20 '22 12:09

a3f