Skip to content

Commit

Permalink
Simplify a+-b and a--b (#354)
Browse files Browse the repository at this point in the history
  • Loading branch information
laurentlb authored Apr 21, 2024
1 parent b53f9d9 commit d8b025a
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 4 deletions.
6 changes: 6 additions & 0 deletions src/rewriter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@ module private RewriterImpl =
| FunCall(Op "-", [Int (i1, su)]) -> Int (-i1, su)
| FunCall(Op "-", [FunCall(Op "-", [e])]) -> e
| FunCall(Op "+", [e]) -> e
// e1 - - e2 -> e1 + e2
| FunCall(Op "-", [e1; FunCall(Op "-", [e2])]) ->
FunCall(Op "+", [e1; e2]) |> env.fExpr env
// e1 + - e2 -> e1 - e2
| FunCall(Op "+", [e1; FunCall(Op "-", [e2])]) ->
FunCall(Op "-", [e1; e2]) |> env.fExpr env

| FunCall(Op ",", [e1; FunCall(Op ",", [e2; e3])]) ->
FunCall(Op ",", [env.fExpr env (FunCall(Op ",", [e1; e2])); e3])
Expand Down
6 changes: 3 additions & 3 deletions tests/compression_results.log
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ controllable-machinery.frag 7708 => 1220.329
ed-209.frag 7766 => 1341.089
elevated.hlsl 3405 => 603.218
endeavour.frag 2605 => 534.116
from-the-seas-to-the-stars.frag 14292 => 2328.094
from-the-seas-to-the-stars.frag 14252 => 2317.681
frozen-wasteland.frag 4578 => 806.452
kinder_painter.frag 2865 => 445.014
leizex.frag 2298 => 510.191
Expand All @@ -17,8 +17,8 @@ orchard.frag 5537 => 1022.773
oscars_chair.frag 4651 => 986.364
robin.frag 6295 => 1052.336
slisesix.frag 4573 => 931.855
terrarium.frag 3624 => 745.827
terrarium.frag 3611 => 744.367
the_real_party_is_in_your_pocket.frag 12111 => 1794.687
valley_ball.glsl 4386 => 888.496
yx_long_way_from_home.frag 2947 => 606.406
Total: 135069 => 23627.076
Total: 135016 => 23615.203
3 changes: 2 additions & 1 deletion tests/unit/simplify.expected
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#version 330

int i;
float bar(float x)
{
float a=6.;
Expand All @@ -19,7 +20,7 @@ float baz(float a)
b+=sin(a);
float c=b+5.;
c+=sin(b);
return-c+-c+c;
return-c-c+c;
}
out vec3 output;
void notMain(float x)
Expand Down
2 changes: 2 additions & 0 deletions tests/unit/simplify.frag
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#version 330

int i;

float bar(float x)
{
float a = x;
Expand Down

0 comments on commit d8b025a

Please sign in to comment.