Moved math functions to be constexpr.

This commit is contained in:
Бранимир Караџић
2024-11-27 19:55:42 -08:00
parent c8ff296934
commit 7eace058a2
8 changed files with 335 additions and 317 deletions

View File

@@ -10,221 +10,6 @@
namespace bx
{
namespace
{
constexpr float kSinC2 = -0.16666667163372039794921875f;
constexpr float kSinC4 = 8.333347737789154052734375e-3f;
constexpr float kSinC6 = -1.9842604524455964565277099609375e-4f;
constexpr float kSinC8 = 2.760012648650445044040679931640625e-6f;
constexpr float kSinC10 = -2.50293279435709337121807038784027099609375e-8f;
constexpr float kCosC2 = -0.5f;
constexpr float kCosC4 = 4.166664183139801025390625e-2f;
constexpr float kCosC6 = -1.388833043165504932403564453125e-3f;
constexpr float kCosC8 = 2.47562347794882953166961669921875e-5f;
constexpr float kCosC10 = -2.59630184018533327616751194000244140625e-7f;
} // namespace
BX_CONST_FUNC float cos(float _a)
{
const float scaled = _a * 2.0f*kInvPi;
const float real = floor(scaled);
const float xx = _a - real * kPiHalf;
const int32_t bits = int32_t(real) & 3;
float c0, c2, c4, c6, c8, c10;
if (bits == 0
|| bits == 2)
{
c0 = 1.0f;
c2 = kCosC2;
c4 = kCosC4;
c6 = kCosC6;
c8 = kCosC8;
c10 = kCosC10;
}
else
{
c0 = xx;
c2 = kSinC2;
c4 = kSinC4;
c6 = kSinC6;
c8 = kSinC8;
c10 = kSinC10;
}
const float xsq = square(xx);
const float tmp0 = mad(c10, xsq, c8 );
const float tmp1 = mad(tmp0, xsq, c6 );
const float tmp2 = mad(tmp1, xsq, c4 );
const float tmp3 = mad(tmp2, xsq, c2 );
const float tmp4 = mad(tmp3, xsq, 1.0);
const float result = tmp4 * c0;
return bits == 1 || bits == 2
? -result
: result
;
}
namespace
{
constexpr float kAcosC0 = 1.5707288f;
constexpr float kAcosC1 = -0.2121144f;
constexpr float kAcosC2 = 0.0742610f;
constexpr float kAcosC3 = -0.0187293f;
} // namespace
BX_CONST_FUNC float acos(float _a)
{
const float absa = abs(_a);
const float tmp0 = mad(kAcosC3, absa, kAcosC2);
const float tmp1 = mad(tmp0, absa, kAcosC1);
const float tmp2 = mad(tmp1, absa, kAcosC0);
const float tmp3 = tmp2 * sqrt(1.0f - absa);
const float negate = float(_a < 0.0f);
const float tmp4 = tmp3 - 2.0f*negate*tmp3;
const float result = negate*kPi + tmp4;
return result;
}
namespace
{
constexpr float kAtan2C0 = -0.013480470f;
constexpr float kAtan2C1 = 0.057477314f;
constexpr float kAtan2C2 = -0.121239071f;
constexpr float kAtan2C3 = 0.195635925f;
constexpr float kAtan2C4 = -0.332994597f;
constexpr float kAtan2C5 = 0.999995630f;
} // namespace
BX_CONST_FUNC float atan2(float _y, float _x)
{
const float ax = abs(_x);
const float ay = abs(_y);
const float maxaxy = max(ax, ay);
const float minaxy = min(ax, ay);
if (maxaxy == 0.0f)
{
return _y < 0.0f ? -0.0f : 0.0f;
}
const float mxy = minaxy / maxaxy;
const float mxysq = square(mxy);
const float tmp0 = mad(kAtan2C0, mxysq, kAtan2C1);
const float tmp1 = mad(tmp0, mxysq, kAtan2C2);
const float tmp2 = mad(tmp1, mxysq, kAtan2C3);
const float tmp3 = mad(tmp2, mxysq, kAtan2C4);
const float tmp4 = mad(tmp3, mxysq, kAtan2C5);
const float tmp5 = tmp4 * mxy;
const float tmp6 = ay > ax ? kPiHalf - tmp5 : tmp5;
const float tmp7 = _x < 0.0f ? kPi - tmp6 : tmp6;
const float result = _y < 0.0f ? -tmp7 : tmp7;
return result;
}
float frexp(float _a, int32_t* _outExp)
{
const uint32_t ftob = floatToBits(_a);
const uint32_t masked0 = uint32_and(ftob, kFloatExponentMask);
const uint32_t exp0 = uint32_srl(masked0, kFloatExponentBitShift);
const uint32_t masked1 = uint32_and(ftob, kFloatSignMask | kFloatMantissaMask);
const uint32_t bits = uint32_or(masked1, UINT32_C(0x3f000000) );
const float result = bitsToFloat(bits);
*_outExp = int32_t(exp0 - 0x7e);
return result;
}
namespace
{
constexpr float kExpC0 = 1.66666666666666019037e-01f;
constexpr float kExpC1 = -2.77777777770155933842e-03f;
constexpr float kExpC2 = 6.61375632143793436117e-05f;
constexpr float kExpC3 = -1.65339022054652515390e-06f;
constexpr float kExpC4 = 4.13813679705723846039e-08f;
} // namespace
BX_CONST_FUNC float exp(float _a)
{
if (abs(_a) <= kNearZero)
{
return _a + 1.0f;
}
const float kk = round(_a*kInvLogNat2);
const float hi = _a - kk*kLogNat2Hi;
const float lo = kk*kLogNat2Lo;
const float hml = hi - lo;
const float hmlsq = square(hml);
const float tmp0 = mad(kExpC4, hmlsq, kExpC3);
const float tmp1 = mad(tmp0, hmlsq, kExpC2);
const float tmp2 = mad(tmp1, hmlsq, kExpC1);
const float tmp3 = mad(tmp2, hmlsq, kExpC0);
const float tmp4 = hml - hmlsq * tmp3;
const float tmp5 = hml*tmp4/(2.0f-tmp4);
const float tmp6 = 1.0f - ( (lo - tmp5) - hi);
const float result = ldexp(tmp6, int32_t(kk) );
return result;
}
namespace
{
constexpr float kLogC0 = 6.666666666666735130e-01f;
constexpr float kLogC1 = 3.999999999940941908e-01f;
constexpr float kLogC2 = 2.857142874366239149e-01f;
constexpr float kLogC3 = 2.222219843214978396e-01f;
constexpr float kLogC4 = 1.818357216161805012e-01f;
constexpr float kLogC5 = 1.531383769920937332e-01f;
constexpr float kLogC6 = 1.479819860511658591e-01f;
} // namespace
BX_CONST_FUNC float log(float _a)
{
int32_t exp;
float ff = frexp(_a, &exp);
if (ff < kSqrt2*0.5f)
{
ff *= 2.0f;
--exp;
}
ff -= 1.0f;
const float kk = float(exp);
const float hi = kk*kLogNat2Hi;
const float lo = kk*kLogNat2Lo;
const float ss = ff / (2.0f + ff);
const float s2 = square(ss);
const float s4 = square(s2);
const float tmp0 = mad(kLogC6, s4, kLogC4);
const float tmp1 = mad(tmp0, s4, kLogC2);
const float tmp2 = mad(tmp1, s4, kLogC0);
const float t1 = s2*tmp2;
const float tmp3 = mad(kLogC5, s4, kLogC3);
const float tmp4 = mad(tmp3, s4, kLogC1);
const float t2 = s4*tmp4;
const float t12 = t1 + t2;
const float hfsq = 0.5f*square(ff);
const float result = hi - ( (hfsq - (ss*(hfsq+t12) + lo) ) - ff);
return result;
}
void mtxLookAt(float* _result, const Vec3& _eye, const Vec3& _at, const Vec3& _up, Handedness::Enum _handedness)
{
const Vec3 view = normalize(