Skip to content

FR: Fold floating-point subtractions under FASTMATH.

While trying to understand what #39782 (closed) wanted, I noticed that these examples indeed don’t fold.

{$mode objfpc} {$h+}
{$optimization level1} {$optimization level2} {$optimization level3} {$optimization level4} {$optimization regvar} {$optimization fastmath}
label A0, A1, B0, B1, C0, C1;

var
	x, y: single;
	dontDrop: string;

begin
	x := random;
A0:
	y := x + 1 + 2 + 3 + 4 + 5 + 6; // folds
A1:
	writestr(dontDrop, y);
	writeln('x + 1 + 2 + 3 + 4 + 5 + 6: ', CodePointer(@A1) - CodePointer(@A0), ' b');

B0:
	y := x + 1 - 2 + 3 - 4 + 5 - 6; // didn't fold at the time of making the issue, now folds
B1:
	writestr(dontDrop, y);
	writeln('x + 1 - 2 + 3 - 4 + 5 - 6: ', CodePointer(@B1) - CodePointer(@B0), ' b');

C0:
	y := x - 1 - 2 - 3 - 4 - 5 - 6; // didn't fold at the time of making the issue, now folds
C1:
	writestr(dontDrop, y);
	writeln('x - 1 - 2 - 3 - 4 - 5 - 6: ', CodePointer(@C1) - CodePointer(@C0), ' b');
end.

x + 1 + 2 + 3 + 4 + 5 + 6: 15 b
x + 1 - 2 + 3 - 4 + 5 - 6: 55 b (15 b since fcb5531f)
x - 1 - 2 - 3 - 4 - 5 - 6: 55 b (15 b since 94665a40)

Edited by Rika
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information