https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97738

--- Comment #5 from Thomas Koenig <tkoenig at gcc dot gnu.org> ---
(In reply to Jakub Jelinek from comment #4)
> What about a version that still sets lowest_bit to value & -value; rather
> than 1 < ctz?

I think this would be ideal, or close to it.

> Also, I'm not sure you can safely do the (changed_bits >> ctz) >> 2 to
> changed_bits >> (ctz + 2) transformation, while because of the division one
> can count on value not being 0 (otherwise UB), value & -value can still be
> e.g. 1U << 31 and then ctz 31 too, and changed_bits >> (31 + 2) being UB,
> while
> (changed_bits >> 31) >> 2 well defined returning 0.

OK.

> So, I think we could e.g. during expansion (or isel) based on target cost
> optimize
> x / (y & -y) to x >> __builtin_ctz (y) (also assuming the optab for ctz
> exists), but anything else looks complicated.

I think this would solve the issue for the original code (which is
what people will find on the web if they google for HAKMEM 175).

Reply via email to