Skip to content

Commit

Permalink
optimize unsigned / to >=
Browse files Browse the repository at this point in the history
  • Loading branch information
WalterBright committed Sep 9, 2017
1 parent b6b8dbf commit ebba9c6
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/ddmd/backend/cgelem.c
Original file line number Diff line number Diff line change
Expand Up @@ -1434,13 +1434,20 @@ STATIC elem * elbitwise(elem *e, goal_t goal)
ELCONST(e1->E1,1) &&
tysize(e->E1->Ety) <= REGSIZE)
{
int sz = tysize(e->E1->Ety);
e->Eoper = OPbtst;
e->Ety = TYbool;
e->E1 = e2;
e->E2 = e1->E2;
e->E2->Ety = e->E1->Ety;
e1->E2 = NULL;
el_free(e1);
if (sz >= 2)
e = el_una(OPu8_16, TYushort, e);
if (sz >= 4)
e = el_una(OPu16_32, TYulong, e);
if (sz >= 8)
e = el_una(OPu32_64, TYullong, e);
return optelem(e, goal);
}
}
Expand Down Expand Up @@ -2398,6 +2405,15 @@ STATIC elem * eldiv(elem *e, goal_t goal)
}
}

/* Convert if(e1/e2) to if(e1>=e2) iff unsigned division.
*/
if (goal == GOALflags && uns && e->Eoper == OPdiv)
{
e->Eoper = OPge;
e->Ety = TYbool;
return e;
}

/* TODO: (i*c1)/c2 => i*(c1/c2) if (c1%c2)==0
* TODO: i/(x?c1:c2) => i>>(x?log2(c1):log2(c2)) if c1 and c2 are powers of 2
*/
Expand Down Expand Up @@ -2431,6 +2447,7 @@ STATIC elem * eldiv(elem *e, goal_t goal)
e->Eoper = OPremquo;
e = el_una(op, tym, e);
e->E1->Ety = (sz == 2) ? TYlong : (sz == 4) ? TYllong : TYcent;
return e;
}
}
}
Expand Down Expand Up @@ -5516,7 +5533,6 @@ STATIC elem * optelem(elem *e, goal_t goal)
elem *doptelem(elem *e, goal_t goal)
{
//printf("doptelem(e = %p, goal = x%x)\n", e, goal);

assert(!PARSER);
do
{ again = false;
Expand Down
16 changes: 16 additions & 0 deletions test/runnable/mars1.d
Original file line number Diff line number Diff line change
Expand Up @@ -1662,6 +1662,21 @@ void testeqeqranges()

////////////////////////////////////////////////////////////////////////

void testdivcmp()
{
// https://github.com/dlang/dmd/pull/7128
static bool foo(uint a, uint b)
{
return cast(bool)(a / b); // convert / to >=
}

assert(!foo(3, 4));
assert(foo(4, 4));
assert(foo(5, 4));
}

////////////////////////////////////////////////////////////////////////

int main()
{
testgoto();
Expand Down Expand Up @@ -1720,6 +1735,7 @@ int main()
test5();
test6();
testeqeqranges();
testdivcmp();
printf("Success\n");
return 0;
}

0 comments on commit ebba9c6

Please sign in to comment.