Message-ID: <19970710152801.34479@gil.physik.rwth-aachen.de> Date: Thu, 10 Jul 1997 15:28:01 +0200 From: Christoph Kukulies To: "Chirayu Krishnappa (chirayu AT poboxes DOT com)" Cc: djgpp AT delorie DOT com Subject: odd or even # of 1's (was: no subject) References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii In-Reply-To: ; from "Chirayu Krishnappa (chirayu@poboxes.com)" on Thu, Jul 10, 1997 at 03:21:13PM +0530 Precedence: bulk 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