Mail Archives: djgpp/1997/07/10/09:26:14
On Thu, Jul 10, 1997 at 03:21:13PM +0530, Chirayu Krishnappa (chirayu AT poboxes DOT com) wrote:
>
> hi,
>
> i need to find out if a 4 byte (default) integer has an even number of 1's
> in its binary representation or not. I need to operate on 15Mb data and do
> it fast. shifts (<<) and & is quite slow. is there some lib. function to
> do this? what is the fastest way to get it done?
You could do an inline assembly operation like or'ing the integer
to itself and examine the PF (parity flag) of the EFLAGS CPU
register afterwards, e.g. by a JPE (conditional jump short on parity even
PF=1).
roughly like this:
static int a=1;
static int p;
main()
{
asm volatile ( \
"movl _a,%eax
orl %eax,%eax
jpe 0f
movl $1,_p
jmp 1f
0: movl $0,_p
1: " );
printf("%d\n",p);
a=0;
asm volatile ( \
"movl _a,%eax
orl %eax,%eax
jpe 0f
movl $1,_p
jmp 1f
0: movl $0,_p
1: " );
printf("%d\n",p);
}
There needs to be done some cleanup of course since the flags
are clobbered and it would be better to deal with local/dynamic
variables. But you get the idea.
Also read:
http://www.rt66.com/~brennan/djgpp/djgpp_asm.html
>
> thanks.
>
>
>
> Chirayu Krishnappa:
> ------------------
> email: chirayu AT poboxes DOT com
> Phone: +91 80 3332616.
> -------------------------------------------------------------------------------
--
--Chris Christoph P. U. Kukulies kuku AT gil DOT physik DOT rwth-aachen DOT de
- Raw text -