]> icculus.org git repositories - taylor/freespace2.git/blob - src/math/floating.cpp
stub/warning removal.
[taylor/freespace2.git] / src / math / floating.cpp
1 /*
2  * $Logfile: /Freespace2/code/Math/Floating.cpp $
3  * $Revision$
4  * $Date$
5  * $Author$
6  *
7  * Low-level floating point math routines
8  *
9  * $Log$
10  * Revision 1.4  2002/06/05 08:05:29  relnev
11  * stub/warning removal.
12  *
13  * reworked the sound code.
14  *
15  * Revision 1.3  2002/05/31 00:29:32  theoddone33
16  * Fix frand()
17  *
18  * Revision 1.2  2002/05/07 03:16:46  theoddone33
19  * The Great Newline Fix
20  *
21  * Revision 1.1.1.1  2002/05/03 03:28:09  root
22  * Initial import.
23  *
24  * 
25  * 2     10/07/98 10:53a Dave
26  * Initial checkin.
27  * 
28  * 1     10/07/98 10:49a Dave
29  * 
30  * 13    2/26/98 3:28p John
31  * Changed all sqrt's to use fl_sqrt.  Took out isqrt function
32  * 
33  * 12    1/30/98 12:25p Mike
34  * Make frand() not return 1.0, which can cause overflow when indexing
35  * into arrays.
36  * 
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
40  * 
41  * 10    1/20/98 9:47a Mike
42  * Suppress optimized compiler warnings.
43  * Some secondary weapon work.
44  * 
45  * 9     1/17/98 3:32p Mike
46  * Add rand_range(), returns random float in min..max.
47  * 
48  * 8     9/09/97 11:07a Sandeep
49  * fixed warning level 4
50  * 
51  * 7     8/05/97 10:18a Lawrance
52  * my_rand() being used temporarily instead of rand()
53  * 
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
56  * them.
57  *
58  * $NoKeywords: $
59  */
60
61 #include <stdlib.h>
62 #include <math.h>
63
64 #include "pstypes.h"
65 #include "floating.h"
66 #include "timer.h"
67
68 #define LOOKUP_BITS     6
69 #define EXP_POS         23
70 #define EXP_BIAS                127
71 typedef float FLOAT;
72
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 )
81
82 int fl_magic = 0x59C00000;              //representation of 2^51 + 2^52
83 const float *p_fl_magic = (const float *)&fl_magic;
84
85 union _flint {
86         unsigned long   i;
87         float                           f;
88 } fi, fo;
89
90 /*
91 static unsigned char iSqrt[TABLE_SIZE];
92 static int iSqrt_inited = 0;
93
94 static void MakeInverseSqrtLookupTable()
95 {
96         long f;
97         unsigned char *h;
98         union _flint fi, fo;
99
100         iSqrt_inited = 1;
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);
105         }
106         iSqrt[ TABLE_SIZE / 2 ] = 0xFF;
107 }
108 */
109
110 // HACK!
111 float fl_isqrt_c( float x )
112 {
113 //      unsigned long a = ((union _flint *)(&x))->i;
114 //      float arg = x;
115 //      union _flint seed;
116 //      FLOAT r;
117
118         int t1, t2, t3;
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();  
124
125         return r1;
126
127
128 /*      if ( !iSqrt_inited )
129                 MakeInverseSqrtLookupTable();
130
131         seed.i = SET_EXP(((3*EXP_BIAS-1) - GET_EXP(a)) >> 1 ) | SET_MANTSEED(iSqrt[GET_EMANT(a)]);
132         r = seed.f;
133         r = (3.0f - r * r * arg ) * r * 0.5f;
134         r = (3.0f - r * r * arg ) * r * 0.5f;
135         return r;
136 */
137 }
138
139 // rounds off a floating point number to a multiple of some number
140 float fl_roundoff(float x, int multiple)
141 {
142         float half = (float) multiple / 2.0f;
143
144         if (x < 0)
145                 half = -half;
146
147         x += half;
148         return (float) (((int) x / multiple) * multiple);
149 }
150
151
152 //      Return random value in range 0.0..1.0- (1.0- means the closest number less than 1.0)
153 float frand()
154 {
155         float rval;
156         rval = fabsf(((float) myrand()) / (RAND_MAX + 1));
157         return rval;
158 }
159
160 //      Return a floating point number in the range min..max.
161 float frand_range(float min, float max)
162 {
163         float   rval;
164         
165         rval = frand();
166         rval = rval * (max - min) + min;
167
168         return rval;
169 }
170
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.
175 {
176         while (--chance > 0.0f)
177                 if (frand() < frametime)
178                         return 1;
179
180         return frand() < (frametime * (chance + 1.0f));
181 }
182
183 /*fix fl2f( float x )
184 {
185         float nf;
186         nf = x*65536.0f + 8390656.0f;
187         return ((*((int *)&nf)) & 0x7FFFFF)-2048;
188 }
189 */
190
191
192 /*
193 >#define  S  65536.0
194 >#define  MAGIC  (((S * S * 16) + (S*.5)) * S)
195 >
196 >#pragma inline float2int;
197 >
198 >ulong float2int( float d )
199 >{
200 >  double dtemp = MAGIC + d;
201 >  return (*(ulong *)&dtemp) - 0x80000000;
202 >}
203
204 */