Re: Fast computation of parity

From: Michael Amling (nospam_at_nospam.com)
Date: 12/31/03


Date: Wed, 31 Dec 2003 01:42:35 GMT

Arthur J. O'Dwyer wrote:
> On Tue, 30 Dec 2003, Alexis Machado wrote:
>>"Arthur J. O'Dwyer" <ajo@nospam.andrew.cmu.edu> escreveu...
>>>On Tue, 30 Dec 2003, Mok-Kong Shen wrote:
>>>>ScottD wrote:
>>>>>Here is a non-portable solution for x86:
>>>>>
>>>>>int parity (long int value)
>>>>> {
>>>>> __asm mov eax, value
>>>>> __asm or eax, eax
>>>>> __asm setp al
>>>>> __asm and eax,1
>>>>> }
>>> He's used that. The 'setp' instruction sets the value
>>>of 'eax' (the return value) to 1 if the parity flag is set,
>>>and 0 if it isn't. Then we just return that value. (The
>>>'or eax, eax' instruction just evaluates 'eax' and sets
>>>the flags accordingly.)
>>
>>I'm not sure, but the parity flag gives only the parity of the
>>least significant byte of the result ...
>
> A quick test in 'debug' shows that you are right and I am
> wrong. So ScottD's function doesn't work after all? Or am I
> missing something here?

   To get the parity of the low-order 16 bits instead of just the
low-order 8 bits, combine the above code with code I just saw somewhere,
replacing the above _asm or eax,eax with
   _asm mov ebx,eax
   _asm bswap ebx
   _asm xor eax,ebx

   Note: This may need a little tweaking from someone who, unlike me,
knows x86 assembler. I'm assuming registers eax and ebx are at least 16
bits wide. The idea is to copy register eax to register ebx, swap the
two low-order bytes of ebx, then XOR ebx back into eax and let the
original code continue to take the parity of the low-order 8 bits of
register eax.

--Mike Amling



Relevant Pages