2 * $Logfile: /Freespace2/code/Math/Floating.cpp $
7 * Low-level floating point math routines
10 * Revision 1.4 2002/06/05 08:05:29 relnev
11 * stub/warning removal.
13 * reworked the sound code.
15 * Revision 1.3 2002/05/31 00:29:32 theoddone33
18 * Revision 1.2 2002/05/07 03:16:46 theoddone33
19 * The Great Newline Fix
21 * Revision 1.1.1.1 2002/05/03 03:28:09 root
25 * 2 10/07/98 10:53a Dave
28 * 1 10/07/98 10:49a Dave
30 * 13 2/26/98 3:28p John
31 * Changed all sqrt's to use fl_sqrt. Took out isqrt function
33 * 12 1/30/98 12:25p Mike
34 * Make frand() not return 1.0, which can cause overflow when indexing
37 * 11 1/26/98 10:43p Mike
38 * Make ships not all zoom away from an impending shockwave at the same
39 * time. Based on ai class and randomness
41 * 10 1/20/98 9:47a Mike
42 * Suppress optimized compiler warnings.
43 * Some secondary weapon work.
45 * 9 1/17/98 3:32p Mike
46 * Add rand_range(), returns random float in min..max.
48 * 8 9/09/97 11:07a Sandeep
49 * fixed warning level 4
51 * 7 8/05/97 10:18a Lawrance
52 * my_rand() being used temporarily instead of rand()
54 * 6 2/17/97 5:18p John
55 * Added a bunch of RCS headers to a bunch of old files that don't have
73 #define LOOKUP_POS (EXP_POS-LOOKUP_BITS)
74 #define SEED_POS (EXP_POS-8)
75 #define TABLE_SIZE (2<<LOOKUP_BITS)
76 #define LOOKUP_MASK (TABLE_SIZE-1)
77 #define GET_EXP(a) (((a) >> EXP_POS) & 0xFF )
78 #define SET_EXP(a) ((a) << EXP_POS )
79 #define GET_EMANT(a) (((a) >> LOOKUP_POS) & LOOKUP_MASK )
80 #define SET_MANTSEED(a) (((unsigned long)(a)) << SEED_POS )
82 int fl_magic = 0x59C00000; //representation of 2^51 + 2^52
83 const float *p_fl_magic = (const float *)&fl_magic;
91 static unsigned char iSqrt[TABLE_SIZE];
92 static int iSqrt_inited = 0;
94 static void MakeInverseSqrtLookupTable()
101 for ( f=0, h=iSqrt; f < TABLE_SIZE; f++ ) {
102 fi.i = ((EXP_BIAS-1)<<EXP_POS) | (f<<LOOKUP_POS);
103 fo.f = 1.0f / fl_sqrt(fi.f);
104 *h++ = (unsigned char)(((fo.i + (1<<(SEED_POS-2))) >>SEED_POS ) & 0xFF);
106 iSqrt[ TABLE_SIZE / 2 ] = 0xFF;
111 float fl_isqrt_c( float x )
113 // unsigned long a = ((union _flint *)(&x))->i;
115 // union _flint seed;
119 t1 = timer_get_microseconds();
120 float r1 = 1.0f / (float)sqrt((double)x);
121 t2 = timer_get_microseconds();
122 // float r2 = fl_isqrt_asm(x);
123 t3 = timer_get_microseconds();
128 /* if ( !iSqrt_inited )
129 MakeInverseSqrtLookupTable();
131 seed.i = SET_EXP(((3*EXP_BIAS-1) - GET_EXP(a)) >> 1 ) | SET_MANTSEED(iSqrt[GET_EMANT(a)]);
133 r = (3.0f - r * r * arg ) * r * 0.5f;
134 r = (3.0f - r * r * arg ) * r * 0.5f;
139 // rounds off a floating point number to a multiple of some number
140 float fl_roundoff(float x, int multiple)
142 float half = (float) multiple / 2.0f;
148 return (float) (((int) x / multiple) * multiple);
152 // Return random value in range 0.0..1.0- (1.0- means the closest number less than 1.0)
156 rval = fabsf(((float) myrand()) / (RAND_MAX + 1));
160 // Return a floating point number in the range min..max.
161 float frand_range(float min, float max)
166 rval = rval * (max - min) + min;
171 // Call this in the frame interval to get TRUE chance times per second.
172 // If you want it to return TRUE 3 times per second, call it in the frame interval like so:
173 // rand_chance(flFrametime, 3.0f);
174 int rand_chance(float frametime, float chance) // default value for chance = 1.0f.
176 while (--chance > 0.0f)
177 if (frand() < frametime)
180 return frand() < (frametime * (chance + 1.0f));
183 /*fix fl2f( float x )
186 nf = x*65536.0f + 8390656.0f;
187 return ((*((int *)&nf)) & 0x7FFFFF)-2048;
194 >#define MAGIC (((S * S * 16) + (S*.5)) * S)
196 >#pragma inline float2int;
198 >ulong float2int( float d )
200 > double dtemp = MAGIC + d;
201 > return (*(ulong *)&dtemp) - 0x80000000;