Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: float to int (kinda OT)



A naive compiler will branch when generating (remainder255 >= (255 + 128)); that's a comparison, and comparisons generate branches.
A smarter compiler might have a clever way of avoiding the branch, perhaps involving a subtract followed by a cntlzw (?). That's just off the top of my head; I haven't tested it.
If "alpha" is a constant across the whole image operation, you could multiply it by a cleverly-chosen constant so that "(alpha * multiplier) >> something" gives you numbers scaled to any range you prefer. This is a trick I've used in the past with good results. (The constant tends to be something wacky; I remember getting 0x8102 or 0x10203 for various operations in the past.) It only has one caveat; some PowerPCs can multiply tiny numbers (i.e. 8-bit values) a little faster than big numbers. I have no idea if this is ancient history or if G4s and G5s still have this restriction. Still, I'd rather pay an extra two cycles on the multiply than add extra instructions to the inner loop.



On Oct 21, 2004, at 6:20 PM, Keith Bauer wrote:

Wow, did I ever get hammered on that little "optimization". For those watching at home, the optimization Chris is talking about is probably this one:

tmp = alpha * red;
remainder256 = tmp & 0xFF; // get alpha * red (mod 256)
dividend = tmp >> 8; // compute alpha * red / 256
remainder255 = dividend + remainder256; // compute alpha * red (mod 255)
dividend += ((remainder255 >= (255 + 128)) & 1) + ((remainder255 >= 128) & 1); // add 0, 1, 2 depending on how large the remainder (mod 255) is


This is provably correct and even rounds correctly. And yes, it would be much, much easier in AltiVec.

No, there are faster ways of doing it, without branches.


Chris

Er, that code doesn't have any branches in it that I can see...

And if you know such optimizations, why not contribute to the thread and enlighten us by posting them rather than just sounding superior about your knowledge?

Thanks,
Keith
_______________________________________________
Do not post admin requests to the list. They will be ignored.
PerfOptimization-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/perfoptimization-dev/ email@hidden


This email sent to email@hidden

_______________________________________________ Do not post admin requests to the list. They will be ignored. PerfOptimization-dev mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/perfoptimization-dev/email@hidden

This email sent to email@hidden
References: 
 >Graphics card tricks (From: "Edward K. Chew" <email@hidden>)
 >Re: Graphics card tricks (From: Holger Bettag <email@hidden>)
 >Re: Graphics card tricks (From: Niall Dalton <email@hidden>)
 >float to int (kinda OT) (From: Ando Sonenblick <email@hidden>)
 >Re: float to int (kinda OT) (From: Brendan Younger <email@hidden>)
 >Re: float to int (kinda OT) (From: Chris Cox <email@hidden>)
 >Re: float to int (kinda OT) (From: Brendan Younger <email@hidden>)
 >Re: float to int (kinda OT) (From: Chris Cox <email@hidden>)
 >Re: float to int (kinda OT) (From: Keith Bauer <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.