This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Re: [patch] fold-const.c: Fold (A & ~3) - (A & 3) into (A ^ 3) - 3.
- From: Kazu Hirata <kazu at cs dot umass dot edu>
- To: kenner at vlsi1 dot ultra dot nyu dot edu
- Cc: segher at d12relay02 dot megacenter dot de dot ibm dot com, gcc-patches at gcc dot gnu dot org
- Date: Tue, 30 Sep 2003 08:40:01 -0400 (EDT)
- Subject: Re: [patch] fold-const.c: Fold (A & ~3) - (A & 3) into (A ^ 3) - 3.
- References: <10309301201.AA05354@vlsi1.ultra.nyu.edu>
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