This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [patch] fold-const.c: Fold (A & ~3) - (A & 3) into (A ^ 3) - 3.


Hi Richard and Segher,

>     This actually works for any B:
> 
> I didn't think so, but perhaps.
> 
>     (A & ~B) - (A & B) = (A & ~B) + ~(A & B) + 1        (1)
> -->                    = (A & ~B) + (~A & B) + ~B + 1   (2)
>                        = (A & ~B) + (~A & B) - B        (3)
>                        = (A ^ B) - B.                   (4)
> 
> OK, that last step is true because you can prove the addition has no
> carries.  But I don't follow the marked step.

I think this works for any B.

By the marked step, do you mean a step from (2) to (3)?  Isn't it
always true that ~B + 1 = -B for any B?

For a step from (1) to (2), a bit-wise truth table suffices because
(~A&B)+~B generates no carry in any bit.

A B ~(A&B) (~A&B) ~B (~A&B)+~B 
------------------------------
0 0   1      0    1      1
0 1   1      1    0      1
1 0   1      0    1      1
1 1   0      0    0      0

Kazu Hirata


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]