Order-of-operations optimization with degrees/radians conversion

This commit is contained in:
Jack Andersen 2016-04-14 17:00:51 -10:00
parent 69b384e48c
commit e284c2de31
3 changed files with 28 additions and 27 deletions

View File

@ -6,6 +6,7 @@
#undef M_PI #undef M_PI
#define M_PI 3.14159265358979323846 /* pi */ #define M_PI 3.14159265358979323846 /* pi */
#define M_PIF 3.14159265358979323846f /* pi */
#undef M_PI_2 #undef M_PI_2
#define M_PI_2 1.57079632679489661923 /* pi/2 */ #define M_PI_2 1.57079632679489661923 /* pi/2 */
#undef M_PI_4 #undef M_PI_4
@ -55,10 +56,10 @@ inline T max(T a, T b) { return a > b ? a : b; }
template<typename T> template<typename T>
inline T clamp(T a, T val, T b) {return max<T>(a, min<T>(b, val));} inline T clamp(T a, T val, T b) {return max<T>(a, min<T>(b, val));}
inline float radToDeg(float rad) {return rad * 180.f / M_PI;} inline float radToDeg(float rad) {return rad * (180.f / M_PIF);}
inline float degToRad(float deg) {return deg * M_PI / 180.f;} inline float degToRad(float deg) {return deg * (M_PIF / 180.f);}
inline double radToDeg(double rad) {return rad * 180.0 / M_PI;} inline double radToDeg(double rad) {return rad * (180.0 / M_PI);}
inline double degToRad(double deg) {return deg * M_PI / 180.0;} inline double degToRad(double deg) {return deg * (M_PI / 180.0);}
CVector3f baryToWorld(const CVector3f& p0, const CVector3f& p1, const CVector3f& p2, const CVector3f& bary); CVector3f baryToWorld(const CVector3f& p0, const CVector3f& p1, const CVector3f& p2, const CVector3f& bary);

View File

@ -11,8 +11,8 @@ const CVector3f CVector3f::skNegOne = CVector3f(-1.0);
const CVector3f CVector3f::skZero; const CVector3f CVector3f::skZero;
const CVector3f kUpVec(0.0, 0.0, 1.0); const CVector3f kUpVec(0.0, 0.0, 1.0);
const CVector3f kRadToDegVec(180.0f / M_PI); const CVector3f kRadToDegVec(180.0f / M_PIF);
const CVector3f kDegToRadVec(M_PI / 180.0f); const CVector3f kDegToRadVec(M_PIF / 180.0f);
float CVector3f::getAngleDiff(const CVector3f& a, const CVector3f& b) float CVector3f::getAngleDiff(const CVector3f& a, const CVector3f& b)
{ {

View File

@ -160,7 +160,7 @@ float fastArcCosF(float val)
* the approximation below won't provide any benefit, * the approximation below won't provide any benefit,
* and we simply fall back to the standard implementation * and we simply fall back to the standard implementation
*/ */
if (std::fabs(val) >= 0.925000011920929) if (std::fabs(val) >= 0.925000011920929f)
return std::acos(val); return std::acos(val);
/* Fast Arc Cosine approximation using Taylor Polynomials /* Fast Arc Cosine approximation using Taylor Polynomials
@ -215,47 +215,47 @@ int ceilingPowerOfTwo(int x)
float fastCosF(float val) float fastCosF(float val)
{ {
if (std::fabs(val) > M_PI) if (std::fabs(val) > M_PIF)
{ {
float rVal = float(uint32_t(val)); float rVal = float(uint32_t(val));
val = -((rVal * val) - 6.2831855); val = -((rVal * val) - 6.2831855f);
if (val <= M_PI && val < -M_PI) if (val <= M_PIF && val < -M_PIF)
val += 6.2831855; val += 6.2831855f;
else else
val -= 6.2831855; val -= 6.2831855f;
} }
float sq = val * val; float sq = val * val;
float b = sq * sq; float b = sq * sq;
val = sq + -0.4999803; val = sq + -0.4999803f;
val = (b * val) + 0.041620344; val = (b * val) + 0.041620344f;
b = b * sq; b = b * sq;
val = (b * val) + -0.0013636103; val = (b * val) + -0.0013636103f;
b = b * sq; b = b * sq;
val = (b * val) + 0.000020169435; val = (b * val) + 0.000020169435f;
return val; return val;
} }
float fastSinF(float val) float fastSinF(float val)
{ {
if (std::fabs(val) > M_PI) if (std::fabs(val) > M_PIF)
{ {
float rVal = float(uint32_t(val)); float rVal = float(uint32_t(val));
val = -((rVal * val) - 6.2831855); val = -((rVal * val) - 6.2831855f);
if (val <= M_PI && val < -M_PI) if (val <= M_PIF && val < -M_PIF)
val += 6.2831855; val += 6.2831855f;
else else
val -= 6.2831855; val -= 6.2831855f;
} }
float sq = val * val; float sq = val * val;
float ret = val * 0.99980587; float ret = val * 0.99980587f;
val = val * sq; val = val * sq;
ret = (val * ret) + -0.16621658; ret = (val * ret) + -0.16621658f;
val = val * sq; val = val * sq;
ret = (val * ret) + 0.0080871079; ret = (val * ret) + 0.0080871079f;
val = val * sq; val = val * sq;
ret = (val * ret) + -0.00015297699; ret = (val * ret) + -0.00015297699f;
return ret; return ret;
} }
@ -263,7 +263,7 @@ float getCatmullRomSplinePoint(float a, float b, float c, float d, float t)
{ {
if (t <= 0.0f) if (t <= 0.0f)
return b; return b;
if (t >= 1.0) if (t >= 1.0f)
return c; return c;
const float t2 = t * t; const float t2 = t * t;
@ -279,7 +279,7 @@ CVector3f getCatmullRomSplinePoint(const CVector3f& a, const CVector3f& b, const
{ {
if (t <= 0.0f) if (t <= 0.0f)
return b; return b;
if (t >= 1.0) if (t >= 1.0f)
return c; return c;
const float t2 = t * t; const float t2 = t * t;