www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/07/13/19:39:20

Date: Mon, 14 Jul 1997 11:36:40 +0000
From: Bill Currie <billc AT blackmagic DOT tait DOT co DOT nz>
Subject: Re: odd or even # of 1's (was: no subject)
To: Nate Eldredge <eldredge AT ap DOT net>, djgpp AT delorie DOT com, chirayu AT poboxes DOT com,
kuku AT gilberto DOT physik DOT rwth-aachen DOT de
Reply-to: billc AT blackmagic DOT tait DOT co DOT nz
Message-id: <33CA0F48.2BBA@blackmagic.tait.co.nz>
Organization: Tait Electronics NZ
MIME-version: 1.0
References: <199707120535 DOT WAA15640 AT adit DOT ap DOT net>
<33C9EBB4 DOT 35D3 AT blackmagic DOT tait DOT co DOT nz>

Bill Currie wrote:
> 
> Nate Eldredge wrote:
> > So the parity flag only shows the parity of the low byte. It would be
> > theoretically possible to check each byte of the int with this scheme and
> > then combine your results, but it would doubtless be ugly and probably
> > slower than the shift method.
> 
> Bummer! Just verified it too.

Okay, here's a new one (tested and it works):
-----------------------------
int has_odd(unsigned long value)
{
  char result;
  unsigned long junk;
  asm(
   "orl %k1,%k1\n"
   "setpe %b0\n"
   "shrl $8,%k1\n"
   "setpe %b1\n"
   "xorb %b1,%b0\n"
   "shrl $8,%k1\n"
   "setpe %b1\n"
   "xorb %b1,%b0\n"
   "shrl $8,%k1\n"
   "setpe %b1\n"
   "xorb %b1,%b0\n"
   :"=&q"(result),
	"=g"(junk)
   :"1"(value)
  );
  return result;
}

int main()
{
  int i;
  for (i=0; i<123456; i++) {
    printf("%d - %s\n",i,has_odd(i)?"odd":"even");
  }
  return 0;
}
--------------------------------------------
Bill
-- 
Leave others their otherness.

- Raw text -


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