Re: Stupid newbie question - determine if integer is even or odd
Re: Stupid newbie question - determine if integer is even or odd
- Subject: Re: Stupid newbie question - determine if integer is even or odd
- From: Steve Bird <email@hidden>
- Date: Tue, 13 Aug 2002 19:54:56 -0400
On Tuesday, August 13, 2002, at 07:46 PM, John Hvrnkvist wrote:
On Wednesday, August 14, 2002, at 01:28 AM, Steve Bird wrote:
<Obsessed cycle-counter>
I'd just like to point out that even though everyone else who answered
this question did a mod , I did not see a single answer of
if (myVal & 0x1) {
}
--- ANDing is most often faster than division. It pains me to see a
division whose result (quotient) is discarded. That's just cruel. There
IS such a thing as the Society for the Prevention of Cruelty to
Computers,
you know.
</Obsessed cycle-counter>
<compiler writer>
With simple things like this, using modulo should be OK as long as you
make it clear what you are up to.
Notice how gcc recognizes the pattern and turns it into a logical
operation:
int odd1(int i)
{
return (i % 2);
}
compiles to:
_odd1:
mr r4,r3
srawi r0,r4,1
addze r0,r0
slwi r2,r0,1
subf r3,r2,r4
blr
int odd2(int i)
{
return ((i%2) != 0);
}
compiles to:
_odd2:
rlwinm r3,r3,0,31,31
blr
int odd3(int i)
{
return (i & 1);
}
compiles to:
_odd3:
rlwinm r3,r3,0,31,31
blr
int odd4(int i)
{
return ((i % 2)?1:0);
}
compiles to:
_odd4:
rlwinm r3,r3,0,31,31
blr
</compiler writer>
/John
<Obsessed cycle-counter>
Ah yes. This is a fine job of extracting the kernel from the nut and
discarding the chaff.
However, I'll vote for not feeding chaff to compilers everytime.
They eat cycles, too.
It takes time to recognize those patterns.
</Obsessed cycle-counter>
----------------------------------------------------------------
Steve Bird
Culverson Software - Elegant software that is a pleasure to use.
www.Culverson.com (toll free) 1-877-676-8175
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.