delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/07/10/09:26:14

Message-ID: <19970710152801.34479@gil.physik.rwth-aachen.de>
Date: Thu, 10 Jul 1997 15:28:01 +0200
From: Christoph Kukulies <kuku AT gilberto DOT physik DOT rwth-aachen DOT de>
To: "Chirayu Krishnappa (chirayu AT poboxes DOT com)" <chirayu AT giasbga DOT vsnl DOT net DOT in>
Cc: djgpp AT delorie DOT com
Subject: odd or even # of 1's (was: no subject)
References: <Pine DOT SV4 DOT 3 DOT 93 DOT 970710151909 DOT 18181A-100000 AT giasbga>
Mime-Version: 1.0
In-Reply-To: <Pine.SV4.3.93.970710151909.18181A-100000@giasbga>; from "Chirayu Krishnappa (chirayu@poboxes.com)" <chirayu@giasbga.vsnl.net.in> on Thu, Jul 10, 1997 at 03:21:13PM +0530

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019