]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - mathlib.c
moved ixtable definition and initialization code to mathlib (and added Mathlib_Init...
[xonotic/darkplaces.git] / mathlib.c
index a1e79394937b7aa4f73dc570fb40c8412f7e3f2a..7fdda6b3a5f22f93441f48b1a25071e71e4a2f97 100644 (file)
--- a/mathlib.c
+++ b/mathlib.c
@@ -26,6 +26,7 @@ void Sys_Error (char *error, ...);
 
 vec3_t vec3_origin = {0,0,0};
 int nanmask = 255<<23;
+float ixtable[4096];
 
 /*-----------------------------------------------------------------*/
 
@@ -40,7 +41,7 @@ float m_bytenormals[NUMVERTEXNORMALS][3] =
 {0.295242, 0.000000, 0.955423}, {0.442863, 0.238856, 0.864188}, 
 {0.162460, 0.262866, 0.951056}, {-0.681718, 0.147621, 0.716567}, 
 {-0.809017, 0.309017, 0.500000}, {-0.587785, 0.425325, 0.688191}, 
-{-0.850651, 0.525731, 0.000000}, {-0.864188, 0.442863, 0.238856}, 
+{-0.850651, 0.525731, 0.000000}, {-0.864188, 0.442863, 0.238856},
 {-0.716567, 0.681718, 0.147621}, {-0.688191, 0.587785, 0.425325}, 
 {-0.500000, 0.809017, 0.309017}, {-0.238856, 0.864188, 0.442863}, 
 {-0.425325, 0.688191, 0.587785}, {-0.716567, 0.681718, -0.147621}, 
@@ -60,11 +61,11 @@ float m_bytenormals[NUMVERTEXNORMALS][3] =
 {1.000000, 0.000000, 0.000000}, {0.951056, 0.162460, 0.262866}, 
 {0.850651, -0.525731, 0.000000}, {0.955423, -0.295242, 0.000000}, 
 {0.864188, -0.442863, 0.238856}, {0.951056, -0.162460, 0.262866}, 
-{0.809017, -0.309017, 0.500000}, {0.681718, -0.147621, 0.716567}, 
+{0.809017, -0.309017, 0.500000}, {0.681718, -0.147621, 0.716567},
 {0.850651, 0.000000, 0.525731}, {0.864188, 0.442863, -0.238856}, 
 {0.809017, 0.309017, -0.500000}, {0.951056, 0.162460, -0.262866}, 
 {0.525731, 0.000000, -0.850651}, {0.681718, 0.147621, -0.716567}, 
-{0.681718, -0.147621, -0.716567}, {0.850651, 0.000000, -0.525731}, 
+{0.681718, -0.147621, -0.716567}, {0.850651, 0.000000, -0.525731},
 {0.809017, -0.309017, -0.500000}, {0.864188, -0.442863, -0.238856}, 
 {0.951056, -0.162460, -0.262866}, {0.147621, 0.716567, -0.681718}, 
 {0.309017, 0.500000, -0.809017}, {0.425325, 0.688191, -0.587785}, 
@@ -82,7 +83,7 @@ float m_bytenormals[NUMVERTEXNORMALS][3] =
 {0.162460, -0.262866, -0.951056}, {0.238856, -0.864188, -0.442863}, 
 {0.500000, -0.809017, -0.309017}, {0.425325, -0.688191, -0.587785}, 
 {0.716567, -0.681718, -0.147621}, {0.688191, -0.587785, -0.425325}, 
-{0.587785, -0.425325, -0.688191}, {0.000000, -0.955423, -0.295242}, 
+{0.587785, -0.425325, -0.688191}, {0.000000, -0.955423, -0.295242},
 {0.000000, -1.000000, 0.000000}, {0.262866, -0.951056, -0.162460}, 
 {0.000000, -0.850651, 0.525731}, {0.000000, -0.955423, 0.295242}, 
 {0.238856, -0.864188, 0.442863}, {0.262866, -0.951056, 0.162460}, 
@@ -91,7 +92,7 @@ float m_bytenormals[NUMVERTEXNORMALS][3] =
 {-0.500000, -0.809017, -0.309017}, {-0.262866, -0.951056, -0.162460}, 
 {-0.850651, -0.525731, 0.000000}, {-0.716567, -0.681718, -0.147621}, 
 {-0.716567, -0.681718, 0.147621}, {-0.525731, -0.850651, 0.000000}, 
-{-0.500000, -0.809017, 0.309017}, {-0.238856, -0.864188, 0.442863}, 
+{-0.500000, -0.809017, 0.309017}, {-0.238856, -0.864188, 0.442863},
 {-0.262866, -0.951056, 0.162460}, {-0.864188, -0.442863, 0.238856}, 
 {-0.809017, -0.309017, 0.500000}, {-0.688191, -0.587785, 0.425325}, 
 {-0.681718, -0.147621, 0.716567}, {-0.442863, -0.238856, 0.864188}, 
@@ -106,7 +107,7 @@ float m_bytenormals[NUMVERTEXNORMALS][3] =
 {-0.850651, 0.000000, 0.525731}, {-0.955423, -0.295242, 0.000000}, 
 {-0.951056, -0.162460, 0.262866}, {-0.864188, 0.442863, -0.238856}, 
 {-0.951056, 0.162460, -0.262866}, {-0.809017, 0.309017, -0.500000}, 
-{-0.864188, -0.442863, -0.238856}, {-0.951056, -0.162460, -0.262866}, 
+{-0.864188, -0.442863, -0.238856}, {-0.951056, -0.162460, -0.262866},
 {-0.809017, -0.309017, -0.500000}, {-0.681718, 0.147621, -0.716567}, 
 {-0.681718, -0.147621, -0.716567}, {-0.850651, 0.000000, -0.525731}, 
 {-0.688191, 0.587785, -0.425325}, {-0.587785, 0.425325, -0.688191}, 
@@ -142,6 +143,32 @@ void ByteToNormal(byte num, vec3_t n)
                VectorClear(n) // FIXME: complain?
 }
 
+float Q_RSqrt(float number)
+{
+       float y;
+
+       if (number == 0.0f)
+               return 0.0f;
+
+       *((long *)&y) = 0x5f3759df - ((* (long *) &number) >> 1);
+       return y * (1.5f - (number * 0.5f * y * y));
+}
+
+void _VectorNormalizeFast(vec3_t v)
+{
+       float y, number;
+
+       number = DotProduct(v, v);
+
+       if (number != 0.0)
+       {
+               *((long *)&y) = 0x5f3759df - ((* (long *) &number) >> 1);
+               y = y * (1.5f - (number * 0.5f * y * y));
+
+               VectorScale(v, y, v);
+       }
+}
+
 #if 0
 // LordHavoc: no longer used at all
 void ProjectPointOnPlane( vec3_t dst, const vec3_t p, const vec3_t normal )
@@ -538,6 +565,19 @@ void BoxOnPlaneSideClassify(mplane_t *p)
        }
 }
 
+void PlaneClassify(mplane_t *p)
+{
+       if (p->normal[0] == 1)
+               p->type = 0;
+       else if (p->normal[1] == 1)
+               p->type = 1;
+       else if (p->normal[2] == 1)
+               p->type = 2;
+       else
+               p->type = 3;
+       BoxOnPlaneSideClassify(p);
+}
+
 void AngleVectors (vec3_t angles, vec3_t forward, vec3_t right, vec3_t up)
 {
        float           angle;
@@ -641,7 +681,7 @@ vec_t Length(vec3_t v)
 {
        int             i;
        float   length;
-       
+
        length = 0;
        for (i=0 ; i< 3 ; i++)
                length += v[i]*v[i];
@@ -650,6 +690,27 @@ vec_t Length(vec3_t v)
        return length;
 }
 
+/*
+// LordHavoc: fixme: do more research on gcc assembly so that qftol_minushalf and result will not be considered unused
+static double qftol_minushalf = -0.5;
+
+int qftol(double v)
+{
+       int result;
+#ifdef _MSC_VER
+       __asm
+       {
+               fld             v
+               fadd    qftol_minushalf
+               fistp   result
+       }
+#else // gcc hopefully
+       asm("fldl v\n\tfaddl qftol_minushalf\n\tfistpl result");
+#endif
+       return result;
+}
+*/
+
 // LordHavoc: renamed these to Length, and made the normal ones #define
 float VectorNormalizeLength (vec3_t v)
 {
@@ -691,14 +752,14 @@ float VectorNormalizeLength2 (vec3_t v, vec3_t dest) // LordHavoc: added to allo
 
 }
 
-void VectorInverse (vec3_t v)
+void _VectorInverse (vec3_t v)
 {
        v[0] = -v[0];
        v[1] = -v[1];
        v[2] = -v[2];
 }
 
-void VectorScale (vec3_t in, vec_t scale, vec3_t out)
+void _VectorScale (vec3_t in, vec_t scale, vec3_t out)
 {
        out[0] = in[0]*scale;
        out[1] = in[1]*scale;
@@ -828,3 +889,14 @@ int GreatestCommonDivisor (int i1, int i2)
                return GreatestCommonDivisor (i1, i2 % i1);
        }
 }
+
+
+void Mathlib_Init(void)
+{
+       int a;
+
+       // LordHavoc: setup 1.0f / N table for quick recipricols of integers
+       ixtable[0] = 0;
+       for (a = 1;a < 4096;a++)
+               ixtable[a] = 1.0f / a;
+}
\ No newline at end of file