Skip to content

Instantly share code, notes, and snippets.

@petrsm
Last active November 21, 2022 17:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save petrsm/079de9396d63e00d5994a7cc936ae9c7 to your computer and use it in GitHub Desktop.
Save petrsm/079de9396d63e00d5994a7cc936ae9c7 to your computer and use it in GitHub Desktop.
Fast pow-2 approximation
static NX_FORCEINLINE float FastPow2_P2(float p)
{
// 2^x = 2^xi * 2^xf
// pow2f - polynomial approximation of e(x) on <0, 1) range
const float pi = floorf(p);
const float pf = p - pi;
const float pow2i = UIntAsFloat((1 << 23) * ((int)pi + 127));
float pow2f = 3.4400110689651967e-1f;
pow2f = pow2f * pf + 6.5104678030290902e-1f;
pow2f = pow2f * pf + 1.0024760564002857f;
return pow2i * pow2f;
}
static NX_FORCEINLINE float FastPow2_P3(float p)
{
// 2^x = 2^xi * 2^xf
// pow2f - polynomial approximation of e(x) on <0, 1) range
const float pi = floorf(p);
const float pf = p - pi;
const float pow2i = UIntAsFloat((1 << 23) * ((int)pi + 127));
float pow2f = 7.9204240219773237e-2f;
pow2f = pow2f * pf + 2.2433836478672357e-1f;
pow2f = pow2f * pf + 6.9645739499350319e-1f;
pow2f = pow2f * pf + 9.9989296565051542e-1f;
return pow2i * pow2f;
}
static NX_FORCEINLINE float FastPow2_P4(float p)
{
// 2^x = 2^xi * 2^xf
// pow2f - polynomial approximation of e(x) on <0, 1) range
const float pi = floorf(p);
const float pf = p - pi;
const float pow2i = UIntAsFloat((1 << 23) * ((int)pi + 127));
float pow2f = 1.3697664475809267e-2f;
pow2f = pow2f * pf + 5.1690358205939469e-2f;
pow2f = pow2f * pf + 2.4163844572498163e-1f;
pow2f = pow2f * pf + 6.9296612266139567e-1f;
pow2f = pow2f * pf + 1.000003704465937f;
return pow2i * pow2f;
}
static NX_FORCEINLINE float FastPow2_P5(float p)
{
// 2^x = 2^xi * 2^xf
// pow2f - polynomial approximation of e(x) on <0, 1) range
const float pi = floorf(p);
const float pf = p - pi;
const float pow2i = UIntAsFloat((1 << 23) * ((int)pi + 127));
float pow2f = 1.8964611454333148e-3f;
pow2f = pow2f * pf + 8.9428289841091295e-3f;
pow2f = pow2f * pf + 5.5866246304520701e-2f;
pow2f = pow2f * pf + 2.4013971109076949e-1f;
pow2f = pow2f * pf + 6.9315475247516736e-1f;
pow2f = pow2f * pf + 9.9999989311082668e-1f;
return pow2i * pow2f;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment