![]() |
| [[ Home | Forums | 3D Engines Database | Wiki | Articles/Tutorials | Game Dev Jobs | IRC Chat Network | Contact Us ]] |
|
|
#1 |
|
New Member
Join Date: Aug 2008
Location: Montreal, Canada
Posts: 3
|
We often need to compute the log2 value for a particular number: finding the required power of two size of a texture, computing how many bits we need to send over the network for a particular value range, etc.
Here's my implementation, taking advantage of the FPU internal representation: converting the number into floating-point effectively converts it to the sign-exponent-mantissa representation of a float: we only need to extract the exponent and we're done: Code:
|
|
|
|
|
|
#2 |
|
Senior Member
Join Date: Aug 2004
Location: Ghent, Belgium
Posts: 1,056
|
While that's vastly faster than the standard log2 function, there's an even faster approach using a processor independent intrinsic in Visual C++:
Code:
|
|
|
|
|
|
#3 | |
|
New Member
Join Date: Aug 2008
Location: Montreal, Canada
Posts: 3
|
Quote:
Ahh, I didn't think of using BSR, thanks for the tip! JF |
|
|
|
|
|
|
#4 | |
|
Member
Join Date: Jun 2005
Posts: 62
|
You need to keep in mind Nicks version is undefined for 0.
Slightly slower, but more accurate version for floats (posted on flipcode many moons ago.) Code:
Quote:
|
|
|
|
|
|
|
#5 |
|
Senior Member
Join Date: Sep 2005
Location: Hamburg / Germany
Posts: 597
|
Be aware that casting float* to int* and similar stuff will break the strict aliasing rule and you'll run into serious trouble if you want to compile your code with newer GCC-compilers.
One way around this is to use unions, another is to include char* as a type during the cast. This one makes a magical connection between the two addresses and tells the optimizer to not assume that floats and ints never share the same memory space. Compiles start to become aggressive when it comes to aliasing.. I'm sure the next VS will be much more aggressive as well.
___________________________________________
My music: http://myspace.com/planetarchh <-- my music My stuff: torus.untergrund.net <-- some diy electronic stuff and more. |
|
|
|
|
|
#6 |
|
Member
Join Date: Jun 2005
Posts: 62
|
Nils, just out of interest, would GCC react the same way for both c++ reinterpret_cast and a c-style cast, or would it not complain about reinterpret_cast (the same response as to union type-punning) ?
|
|
|
|
|
|
#7 | |
|
Senior Member
Join Date: Aug 2004
Location: Ghent, Belgium
Posts: 1,056
|
Quote:
![]() The easiest way to always get a well-defined result is to logic OR the input with 1. But obviously the programmer should always carefully check the behavior on a per-case basis. |
|
|
|
|
|
|
#8 | |
|
Senior Member
Join Date: Aug 2004
Location: Ghent, Belgium
Posts: 1,056
|
Quote:
|
|
|
|
|
|
|
#9 | |
|
Valued Member
Join Date: Oct 2005
Posts: 247
|
Quote:
While the strict aliasing rule rarely causes problems in practice, and thus many people ignore it, it DOES cause problems sometimes, and they can be extremely hard and time-consuming to find. Always try to write the code using unions from the start.
___________________________________________
http://www.iguanademos.com/Jare |
|
|
|
|
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
|