[Bug tree-optimization/49552] missed optimization: test for zero remainder after division by a constant.

2019-02-09 Thread amonakov at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=49552

Alexander Monakov  changed:

   What|Removed |Added

 Status|NEW |RESOLVED
 CC||amonakov at gcc dot gnu.org
 Resolution|--- |DUPLICATE

--- Comment #3 from Alexander Monakov  ---
This was implemented for gcc-9 via PR 82853; it seems this bug was overlooked
in the renewed discussion.

*** This bug has been marked as a duplicate of bug 82853 ***

[Bug tree-optimization/49552] missed optimization: test for zero remainder after division by a constant.

2011-06-28 Thread wouter.vermaelen at scarlet dot be
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=49552

--- Comment #2 from Wouter Vermaelen  
2011-06-28 12:22:11 UTC ---
> Confirmed.  Is this possible for all constant moduli?

It is. I recommend you get a copy of the book I mentioned before. The theory
behind the transformation is much better explained there than I could ever do
here. But I'll try to give a recipe to construct the routine for a specific
constant:
(all examples are for 32-bit, but it should be easy enough to generalize)

There are 3 different cases:

(x % C) == 0

* 'x' is unsigned, 'C' is odd:

return (x * Cinv) <= (0x / C);

Where Cinv is the multiplicative inverse of C  (C * Cinv = 1 (modulo pow(2,
32))). Cinv is the same 'magic number' as is used to optimize exact-division
(division where it's known that the remainder will be zero).



* 'x' is unsigned, 'C' is even:

Split 'C' in an odd factor and a power of two.

C = Codd * Ceven
where Ceven = pow(2, k)

Now we test that 'x' is both divisible by 'Codd' and 'Ceven'.

return !(x & (Ceven - 1)) && ((x * Codd_inv) <= (0x / Codd))

When a rotate-right instruction is available, the expression above can be
rewritten so that it only requires one test:

return rotateRight(x * Codd_inv, k) <= (0x / C); // unsigned
comparison



* 'x' is signed, (C can be odd or even)

(I admit, I don't fully understand the theory behind this transformation, so
I'll only give the final result).

constexpr unsigned A = (0x7fff / Codd) & -(1 << k);
constexpr unsigned B = k ? (A >> (k - 1)) : (A << 1);
return rotateRight((x * Codd_inv) + A, k) <= B;   // unsigned comparison


[Bug tree-optimization/49552] missed optimization: test for zero remainder after division by a constant.

2011-06-28 Thread rguenth at gcc dot gnu.org
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=49552

Richard Guenther  changed:

   What|Removed |Added

 Status|UNCONFIRMED |NEW
   Last reconfirmed||2011.06.28 09:20:40
 Ever Confirmed|0   |1

--- Comment #1 from Richard Guenther  2011-06-28 
09:20:40 UTC ---
Confirmed.  Is this possible for all constant moduli?