Re: Fast computation of parity

From: Mok-Kong Shen (mok-kong.shen_at_t-online.de)
Date: 12/31/03


Date: Wed, 31 Dec 2003 13:13:58 +0100


Mok-Kong Shen wrote:
>
> Alexis Machado wrote:
> >
> > "Mok-Kong Shen" <mok-kong.shen@t-online.de> wrote:
........
> >
> > An alternative :
> >
> > MOV EAX, w
> > MOV EBX, EAX
> > BSWAP EAX
> > XOR EAX, EBX
> > XOR AL, AH
> > SETPO AL /* AL=1 if parity is odd, AL=0 otherwise */
> > AND EAX, 1 /* return EAX ... */
>
> I tried it with VC++. It's slower. (BTW, I don't
> yet know how to run under gcc a group of asm statements
> (grouped by {} and with labels within it as is my code
> above) and should appreciate help.)

I just found with VC++ that a combination of your
first part with my last part of asm seems to be the best:

/* Returns 0 if even parity, else 1 */
/* Ignore compiler warning */
int Parityasm(unsigned int w)
{
__asm
{
  mov eax,w
  mov ebx,eax
  bswap eax
  xor eax,ebx
  xor al,ah
  jp pr
  mov eax,1
  jmp ee
pr:
  mov eax,0
ee:
}
}

M. K. Shen



Relevant Pages