Re: Fast computation of parity

From: Arthur J. O'Dwyer (ajo_at_nospam.andrew.cmu.edu)
Date: 12/30/03


Date: Tue, 30 Dec 2003 16:03:46 -0500 (EST)


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
> > }
>
> I attempted to use the above. However, the compiler
> complained that there is no return value. Could you fix
> that (since I know too little about asm)?

  One simple solution would be to add the lines

       __asm mov value, eax
       return value;

at the end of the function; but there's probably a compiler-
specific solution involving attributes, pragma, or whatever
ScottD's compiler uses. If you can get the compiler to stop
complaining, it *will* work -- so if it's just giving you
a warning, instead of an error, you should be able to ignore
it.

> (BTW, after
> an operation, the parity of the least significant
> byte is given in a parity flag, as far as I know.
> Could one make use of that, or have you already used
> that in the above?) Thanks.

  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.)

HTH,
-Arthur



Relevant Pages

  • Re: C/C++ Compilers Optimization Failed
    ... I used C/C++ Compiler's Optimization. ... >> xor eax, eax ... I am shocked that C/C++ Compiler did not tune optimization very well ...
    (comp.lang.asm.x86)
  • Re: missing optimization?
    ... Both, function1 and function2, have duplicated ... > movl _data, %eax ... > subl 4, %eax ... > If compiler is *newer* than 2.95, then it puts the TEST opcode. ...
    (comp.os.msdos.djgpp)
  • Re: C/C++ Compilers Optimization Failed
    ... > xor eax, eax ... If you don't like the code generated by that compiler, ... And why are you complaining in an assembly language newsgroup about ... > (Optimization is not important YET!!) ...
    (comp.lang.asm.x86)
  • Re: compiler generated output
    ... > compiler to generate code which only uses 386 instruction). ... > Thus, while replacing mov/and with movzx, the jump is still there. ... mov eax, ... Software Optimization Guide for ...
    (comp.lang.asm.x86)
  • Re: No need to optimize in assembly anymore
    ... > inlining etc. - precisely the ones that a good compiler will perform. ... Now in MSVC 7.1 it generates a single div. ... mov eax, DWORD PTR _b$ ...
    (comp.lang.asm.x86)