Skip to content

Multiplication by −1 does not fold with surrounding multipliers (incl. other −1s).

Derived from this topic. Slightly reminds me of my old report, but might be a separate issue.

This code:

var
	x: integer;
begin
	x := 2 + random(0);
	x := x * -3 * 4 * -5 * -1 * -1 * -1 * -5 * 6 * -7; // (A)
	x := -3 * 4 * -5 * -1 * -1 * -1 * -5 * 6 * -7 * x; // (B)
	writeln(x);
end.

Compiles as follows:

; x := x * -3 * 4 * -5 * -1 * -1 * -1 * -5 * 6 * -7;
imul ebx,eax,$3C
neg  ebx
neg  ebx
neg  ebx
imul ebx,ebx,$000000D2
; x := -3 * 4 * -5 * -1 * -1 * -1 * -5 * 6 * -7 * x;
imul ebx,ebx,$FFFFCEC8

Each -1 became a separate neg. Since multiplication is commutative, there is no reason for these expressions to fold differently.

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