commit b057d1fdf510c655916a99184421b0197c5da056
parent b8ad553523ba96ddf4c363befb1a5397b18af440
Author: Roberto E. Vargas Caballero <k0ga@shike2.net>
Date: Fri, 30 Jan 2026 10:42:53 +0100
cc1: Be careful about float optimizations
As described in 5.1.2.3p14:
Rearrangement for floating-point expressions is often restricted
because of limitations in precision as well as range. The
implementation cannot generally apply the mathematical associative
rules for addition or multiplication, nor the distributive rule,
because of roundoff error, even in the absence of overflow and
underflow. Likewise, implementations cannot generally replace
decimal constants in order to rearrange expressions. In the
following fragment, rearrangements suggested by mathematical
rules for real numbers are often not valid.
Diffstat:
1 file changed, 7 insertions(+), 3 deletions(-)
diff --git a/src/cmd/scc-cc/cc1/fold.c b/src/cmd/scc-cc/cc1/fold.c
@@ -719,6 +719,7 @@ static Node *
xxsimplify(Node *np)
{
int op;
+ int isfloat = np->type->op == FLOAT;
np->left = xsimplify(np->left);
np->right = xsimplify(np->right);
@@ -756,10 +757,13 @@ repeat:
np = fold(np);
break;
default:
- commutative(np);
- associative(np);
+ if (!isfloat) {
+ commutative(np);
+ associative(np);
+ }
np = fold(np);
- np = identity(np);
+ if (!isfloat)
+ np = identity(np);
reduce(np);
break;
}