From 69d5d648a9630a85a0ba9dbe0f79345152294112 Mon Sep 17 00:00:00 2001 From: florian Date: Tue, 21 Sep 2021 23:09:33 +0200 Subject: [PATCH] + more unary minus optimizations * test extended --- compiler/nmat.pas | 17 +++++++++++++++++ tests/tbs/tb0685.pp | 14 ++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/compiler/nmat.pas b/compiler/nmat.pas index ade96bbbf6..a9f8b1f790 100644 --- a/compiler/nmat.pas +++ b/compiler/nmat.pas @@ -972,6 +972,23 @@ implementation exit; end; + { + -(-left*right) or -(left*-right) => right*left + + this operation is always valid as reals do not use a two's complement representation for negative + numbers, -real means just flip the sign bit + } + if (left.nodetype=muln) and ((taddnode(left).left.nodetype=unaryminusn)) then + begin + result:=caddnode.create(muln,taddnode(left).right.getcopy,tunaryminusnode(taddnode(left).left).left.getcopy); + exit; + end; + if (left.nodetype=muln) and ((taddnode(left).right.nodetype=unaryminusn)) then + begin + result:=caddnode.create(muln,taddnode(left).left.getcopy,tunaryminusnode(taddnode(left).right).left.getcopy); + exit; + end; + { --node => node this operation is always valid as reals do not use a two's complement representation for negative numbers, -real means just flip the sign bit diff --git a/tests/tbs/tb0685.pp b/tests/tbs/tb0685.pp index 1970538db0..881adb93d3 100644 --- a/tests/tbs/tb0685.pp +++ b/tests/tbs/tb0685.pp @@ -44,5 +44,19 @@ begin if (d3<>0.0) or not(TDoubleRec(d3).Sign) then halt(7); + d1:=1.0; + d2:=2.0; + d3:=-(d2*-d1); + writeln(d3); + if (d3<>2.0) or (TDoubleRec(d3).Sign) then + halt(8); + + d1:=1.0; + d2:=2.0; + d3:=-(-d2*d1); + writeln(d3); + if (d3<>2.0) or (TDoubleRec(d3).Sign) then + halt(9); + writeln('ok'); end.