Hi,

Thanks for your comments!

Segher Boessenkool <seg...@kernel.crashing.org> writes:

> Hi!
>
> On Wed, Jun 07, 2023 at 04:21:11PM +0800, Jiufu Guo wrote:
>> This patch tries to optimize "(X - N * M) / N" to "X / N - M".
>> For C code, "/" towards zero (trunc_div), and "X - N * M" maybe
>> wrap/overflow/underflow. So, it is valid that "X - N * M" does
>> not cross zero and does not wrap/overflow/underflow.
>
> Is it ever valid semi-generally when N does not divide X?

It is valid only if there is no wrap/overflow/underflow, and the sign
of "X" and "X-N*M" are the same.  Under this condition, N,M and X can be
any value. 

>
> Say X=5, N=2, M=3.  Then the original expression evaluates to 0, but the
> new one to -1.  Whenever one of the divisions rounds up and the other
> rounds down you have this problem.
You are right.  Since '/' is always towards zero, so, 'X' and 'X-N*M'
should have the same sign bit.  Otherwise, one rounds up, the other
rounds down, then the transform is invalid.

BR,
Jeff (Jiufu Guo)
>
>
> Segher

Reply via email to