# Fastest way to get power of two

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

## 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 on other sites
Calculating it would be faster than using a lookup table.

##### 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 on other sites
You don't appear to have understood the context of the question. You can't bitshift in a shader.

##### 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 on other sites
Quote:
 Original post by 51monIs 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 on other sites

Hopefully this works.

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

##### Share on other sites
Quote:
 Original post by Anonymous Posteranswer = (number Hopefully this works.If not, its answer isequalto number shiftleft (shift binaryand 31)

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

##### 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.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 9
• 9
• 9
• 34
• 16
• ### Forum Statistics

• Total Topics
634123
• Total Posts
3015654
×