Andrea Arteaga
2016-03-23 00:15:53 UTC
The C++ standard defines a strict operator precedence and associativity, so
for instance a+b+c must be interpreted by the compiler as (a+b)+c and not
as a+(b+c); a compiler which interprets the expression according to the
second way is not strictly standard compliant (a few compilers, like GCC
and ICC, allow to specify a flag which disables all those optimizations
which can cause reassociation like in this case).
With C++11 the new mathematical function FMA is introduced, which allows to
perform operations like a*b+c in a single hardware instruction. The proper
language construct to use a FMA is the function std::fma(a, b, c), but, to
my knowledge, the compiler can also interpret the expression a*b+c as an
FMA and generate the same instruction. In this case the group */+ used in
that expression can be considered as a single ternary operator instead of a
couple of binary operators.
The question is whether such an interpretation is allowed, prescribed or
regulated by the standard. What happen in the case of the expression a*b +
c*d? Is the compiler allowed to reformulate it as fma(a, b, c*d) and as fma(c,
d, a*b)?
I'm particularly interested because suddenly, with the introduction of
FMAs, the very same C++ code can be interpreted in different ways by the
compilers leading to potentially different results depending on which
instruction set is selected at compile time. Before, the strict operators
precedence and associativity rules prevented such differences due to
floating point arithmetic.
Please let me know if some of my assumptions are wrong or if my
understanding of the standard is misleading.
for instance a+b+c must be interpreted by the compiler as (a+b)+c and not
as a+(b+c); a compiler which interprets the expression according to the
second way is not strictly standard compliant (a few compilers, like GCC
and ICC, allow to specify a flag which disables all those optimizations
which can cause reassociation like in this case).
With C++11 the new mathematical function FMA is introduced, which allows to
perform operations like a*b+c in a single hardware instruction. The proper
language construct to use a FMA is the function std::fma(a, b, c), but, to
my knowledge, the compiler can also interpret the expression a*b+c as an
FMA and generate the same instruction. In this case the group */+ used in
that expression can be considered as a single ternary operator instead of a
couple of binary operators.
The question is whether such an interpretation is allowed, prescribed or
regulated by the standard. What happen in the case of the expression a*b +
c*d? Is the compiler allowed to reformulate it as fma(a, b, c*d) and as fma(c,
d, a*b)?
I'm particularly interested because suddenly, with the introduction of
FMAs, the very same C++ code can be interpreted in different ways by the
compilers leading to potentially different results depending on which
instruction set is selected at compile time. Before, the strict operators
precedence and associativity rules prevented such differences due to
floating point arithmetic.
Please let me know if some of my assumptions are wrong or if my
understanding of the standard is misleading.
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-discussion+***@isocpp.org.
To post to this group, send email to std-***@isocpp.org.
Visit this group at https://groups.google.com/a/isocpp.org/group/std-discussion/.
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-discussion+***@isocpp.org.
To post to this group, send email to std-***@isocpp.org.
Visit this group at https://groups.google.com/a/isocpp.org/group/std-discussion/.