[PATCH] D33720: [PowerPC] Eliminate compares - add i64 sext/zext handling for SETNE

Guozhi Wei via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 26 13:50:50 PDT 2017


Carrot added a comment.

In https://reviews.llvm.org/D33720#789655, @Carrot wrote:

> In https://reviews.llvm.org/D33720#789251, @Carrot wrote:
>
> > In https://reviews.llvm.org/D33720#788925, @nemanjai wrote:
> >
> > > That's actually not what the SUBFE instruction does. As per the ISA:
> > >
> > >   subfe RT,RA,RB
> > >   computes
> > >   RT = ~(RA) + (RB) + CA
> > >
> > >
> > > So in your example, it will be:
> > >
> > >   ~AC + XOR + CARRY
> > >   ~(~0) + 0 + 0
> > >
> > >
> > > Which of course will just be a zero.
> >
> >
> > Then the document is wrong.
> >
> > Mathematically, subtraction is
> >  RB - RA = RB + ~RA + 1
> >  just like the description in instruction subf.
> >
> > A subtraction when considering borrowing from lower bit is
> >  RB - RA - CA = RB + ~RA + 1 - CA = RB + ~RA + !CA
>
>
> I wrote a small program to test it, you are right, the instruction subfe does
>  RT = ~(RA) + (RB) + CA
>
> so
>  RT = (RB) + ~(RA) + CA = (RB) + ~(RA) + 1 - 1 + CA = RB - RA + CA - 1
>  I can't understand the mathematical meaning of the right side. And how can it be used to implement high precision integer substraction.


I understand it now. 
It is because the CA flag is different than CF flag on x86 when set by SUB instruction.

On x86, CF flag means there is borrowing from highest bit when doing subtraction
On ppc, CA flag means carry flag of following addition

  B - A = B + ~A + 1

so CF = !CA

so        B - A - Borrow = B + ~A + 1 - CF = B + ~A + 1 - !CA = B + ~A + 1 - (1 - CA) = B + ~A + CA


Repository:
  rL LLVM

https://reviews.llvm.org/D33720





More information about the llvm-commits mailing list