Skip to content

Commit

Permalink
Cleanup misc. TMS maths helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
rollerozxa committed Mar 9, 2024
1 parent 67c6a47 commit dcda158
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 200 deletions.
4 changes: 0 additions & 4 deletions src/src/settings.cc
Expand Up @@ -13,8 +13,6 @@ struct shadow_res {int x; int y;};
static struct shadow_res shadow_resolutions[10];
static int num_shadow_resolutions;

#define log2(x) (log(x) / log(2.0))

_settings settings;

void
Expand Down Expand Up @@ -345,5 +343,3 @@ _settings::~_settings()
delete it->second;
}
}

#undef log2
2 changes: 1 addition & 1 deletion src/src/terrain.cc
Expand Up @@ -903,7 +903,7 @@ level_chunk::merge(int _x, int _y, int _z, int _w, int _h, int _d)
int ssx = flp2(min_sx);

int ss = ssy < ssx ? ssy : ssx;
int size = (int)floorf(tmath_log2(ss));
int size = (int)floorf(log2(ss));

/* mark pixels as taken */
for (sy=0; sy<ss; sy++) {
Expand Down
148 changes: 0 additions & 148 deletions src/tms/math/math.c
Expand Up @@ -97,132 +97,6 @@ void tmath_sincos( float x, float *r0, float *r1)

}

static const float __sinf_rng[2] = {
2.0 / M_PI,
M_PI / 2.0
};// ALIGN(16);

static const float __sinf_lut[4] = {
-0.00018365f, //p7
-0.16664831f, //p3
+0.00830636f, //p5
+0.99999661f, //p1
};// ALIGN(16);

float tmath_sin(float x)
{
union {
float f;
int i;
} ax;

float r, a, b, xx;
int m, n;

ax.f = fabsf(x);

//Range Reduction:
m = (int) (ax.f * __sinf_rng[0]);
ax.f = ax.f - (((float)m) * __sinf_rng[1]);

//Test Quadrant
n = m & 1;
ax.f = ax.f - n * __sinf_rng[1];
m = m >> 1;
n = n ^ m;
m = (x < 0.0);
n = n ^ m;
n = n << 31;
ax.i = ax.i ^ n;

//Taylor Polynomial (Estrins)
xx = ax.f * ax.f;
a = (__sinf_lut[0] * ax.f) * xx + (__sinf_lut[2] * ax.f);
b = (__sinf_lut[1] * ax.f) * xx + (__sinf_lut[3] * ax.f);
xx = xx * xx;
r = b + a * xx;

return r;
}

static const float __powf_rng[2] = {
1.442695041f,
0.693147180f
};

static const float __powf_lut[16] = {
-2.295614848256274, //p0 log
-2.470711633419806, //p4
-5.686926051100417, //p2
-0.165253547131978, //p6
+5.175912446351073, //p1
+0.844006986174912, //p5
+4.584458825456749, //p3
+0.014127821926000, //p7
0.9999999916728642, //p0 exp
0.04165989275009526, //p4
0.5000006143673624, //p2
0.0014122663401803872, //p6
1.000000059694879, //p1
0.008336936973260111, //p5
0.16666570253074878, //p3
0.00019578093328483123 //p7
};

float tmath_pow(float x, float n)
{
float a, b, c, d, xx;
int m;

union {
float f;
int i;
} r;

//extract exponent
r.f = x;
m = (r.i >> 23);
m = m - 127;
r.i = r.i - (m << 23);

//Taylor Polynomial (Estrins)
xx = r.f * r.f;
a = (__powf_lut[4] * r.f) + (__powf_lut[0]);
b = (__powf_lut[6] * r.f) + (__powf_lut[2]);
c = (__powf_lut[5] * r.f) + (__powf_lut[1]);
d = (__powf_lut[7] * r.f) + (__powf_lut[3]);
a = a + b * xx;
c = c + d * xx;
xx = xx * xx;
r.f = a + c * xx;

//add exponent
r.f = r.f + ((float) m) * __powf_rng[1];

r.f = r.f * n;


//Range Reduction:
m = (int) (r.f * __powf_rng[0]);
r.f = r.f - ((float) m) * __powf_rng[1];

//Taylor Polynomial (Estrins)
a = (__powf_lut[12] * r.f) + (__powf_lut[8]);
b = (__powf_lut[14] * r.f) + (__powf_lut[10]);
c = (__powf_lut[13] * r.f) + (__powf_lut[9]);
d = (__powf_lut[15] * r.f) + (__powf_lut[11]);
xx = r.f * r.f;
a = a + b * xx;
c = c + d * xx;
xx = xx* xx;
r.f = a + c * xx;

//multiply by 2 ^ m
m = m << 23;
r.i = r.i + m;

return r.f;
}

static const float __atan2f_lut[4] = {
-0.0443265554792128, //p7
Expand Down Expand Up @@ -397,9 +271,6 @@ tmat4_transpose(float *m)
m[r*4+c] = tmp[c*4+r];
}

/* gives 0 if f == 0, -1 if f < 0 and 1 if f > 0 */
#define TSIGN(f) (float)((f > 0) - (f < 0))

/**
* Set projection matrix `m`'s near plane to `plane`, to clip
* the scene against that plane.
Expand All @@ -411,17 +282,6 @@ void
tmat4_set_near_plane(float *m, tvec4 *plane)
{
float mn[] = TMAT4_IDENTITY;
/*
float dot;
tvec4 p = {
.x = (TSIGN(plane->x) + m[8]) / m[0],
.y = (TSIGN(plane->y) + m[9]) / m[5],
.z = -1.f,
.w = (1.f + m[10]) / m[14]
};
dot = tvec4_dot(&p, plane);
*/

float fz = fabsf(plane->z);

Expand All @@ -430,7 +290,6 @@ tmat4_set_near_plane(float *m, tvec4 *plane)
plane->z / fz, plane->w / fz
};
p.w -= 1;
////if (p.z < 0) tvec4_mul(&p,-1);

mn[2] = p.x;
mn[6] = p.y;
Expand All @@ -439,13 +298,6 @@ tmat4_set_near_plane(float *m, tvec4 *plane)

tmat4_multiply(mn, m);
tmat4_copy(m, mn);

/*
m[2] = p.x * (2.f/dot);
m[6] = p.y * (2.f/dot);
m[10] = p.z * (2.f/dot) + 1.f;
m[14] = p.w * (2.f/dot);
*/
}

/**
Expand Down
58 changes: 11 additions & 47 deletions src/tms/math/misc.h
Expand Up @@ -9,16 +9,9 @@ extern "C"
{
#endif

static inline float tms_modf(float a, float b)
{
return a - b*(floorf(a/b));
}

#define RANDF_MAX 2147483647.f

static inline float trandf(float min, float max)
{
return min+(float)(rand())/((float)(RANDF_MAX/(max-min)));
return min + rand() / (float)RAND_MAX * ( max - min );
}

static inline float twrapf(float x, float min, float max)
Expand All @@ -38,25 +31,16 @@ static inline float twrapf(float x, float min, float max)
return fmodf(x - min, range) + min;
}

static inline float tclampf(float x, float a, float b)
{
if (x < a) x = a;
else if (x > b) x = b;
return x;
}

static inline double tclamp(double x, double a, double b)
static inline float tclampf(float d, float min, float max)
{
if (x < a) x = a;
else if (x > b) x = b;
return x;
const float t = d < min ? min : d;
return t > max ? max : t;
}

static inline int tclampi(int x, int a, int b)
static inline int tclampi(int d, int min, int max)
{
if (x < a) x = a;
else if (x > b) x = b;
return x;
const int t = d < min ? min : d;
return t > max ? max : t;
}

static inline float tmath_adist(float a, float b)
Expand Down Expand Up @@ -84,38 +68,18 @@ static inline float tmath_adist(float a, float b)
return t[i]-a;
}

#if defined(TMS_BACKEND_ANDROID)
static double tmath_log2(double n)
{
return log(n) / log(2.);
}
#else
#define tmath_log2(n) log2(n)
#endif

#ifdef TMS_FAST_MATH

void tmath_sincos(float x, float *r0, float *r1);
float tmath_sin(float x);
static inline float tmath_cos(float x){return tmath_sin(x+M_PI_2);};
float tmath_pow(float x, float n);
float tmath_atan2(float y, float x);
float tmath_sqrt(float x);

#else
#ifdef TMS_BACKEND_MOBILE
static inline void tmath_sincos(float x, float *y, float *z) {
*y = sinf(x);
*z = cosf(x);
}
//#define tmath_sincos(x,y,z) do {*(y) = sinf(x); *(z) = cosf(x);}while(0)
//#define tmath_sincos(x,y,z) __sincosf(x,y,z)
#else

#define tmath_sincos(x,y,z) sincosf(x,y,z)
#endif
#define tmath_sin(x) sinf(x)
#define tmath_cos(x) cosf(x)
#define tmath_pow(x,n) powf(x,n)
#define tmath_atan2(y,x) atan2f(y,x)
#define tmath_sqrt(x) sqrtf(x)

#endif

static inline float tmath_atan2add(float y, float x)
Expand Down

0 comments on commit dcda158

Please sign in to comment.