Jump to content
  • Advertisement
Sign in to follow this  
51mon

Fastest way to get power of two

This topic is 4346 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

When needing to calculate the power of two in a shader what is fastest, direct calculation, lookup table or some other way: pow(2,fPower) or tex2D(PowerOfTwoSampler, fPower * fStep + fHalfStep) Is there big difference in speed? (I'm using HLSL)

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
Calculating it would be faster than using a lookup table.

Share this post


Link to post
Share on other sites
Pow is veeeery slow.
The usual way to do it is bitshift: 2 << fPower.
But from the "f" in front of fPower I assume it is a float, so this would not work. Then a lookup table would be fine.

Lutz

Share this post


Link to post
Share on other sites
Really it depends on what your shader is doing. Calculating it with math could be faster or in some cases a texture lookup would be faster. Try both methods with your shader to see what works best in your case.

Perf could also be different depending on your hardware. Some hardware may do better with mostly math, but some other could be better at texture lookups (NV30 for example).

Share this post


Link to post
Share on other sites
Quote:
Original post by 51mon
Is there big difference in speed? (I'm using HLSL)


In asm there is an exp instruction that does what you want.
In HLSL I think it's called exp2..

LeGreg

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
answer = bit <

Share this post


Link to post
Share on other sites
Guest Anonymous Poster

answer = (number

Hopefully this works.

If not, its answer isequalto number shiftleft (shift binaryand 31)

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster

answer = (number

Hopefully this works.

If not, its answer isequalto number shiftleft (shift binaryand 31)


Quote:
Original post by jpetrie
You don't appear to have understood the context of the question. You can't bitshift in a shader.

Share this post


Link to post
Share on other sites
Use the exp2 HLSL intrinsic. Most graphic chips can compute it in one clock cycle so it's the fastest it can get and has good accuracy.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!