]> icculus.org git repositories - taylor/freespace2.git/blob - src/math/floating.cpp
The Great Newline Fix
[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.2  2002/05/07 03:16:46  theoddone33
11  * The Great Newline Fix
12  *
13  * Revision 1.1.1.1  2002/05/03 03:28:09  root
14  * Initial import.
15  *
16  * 
17  * 2     10/07/98 10:53a Dave
18  * Initial checkin.
19  * 
20  * 1     10/07/98 10:49a Dave
21  * 
22  * 13    2/26/98 3:28p John
23  * Changed all sqrt's to use fl_sqrt.  Took out isqrt function
24  * 
25  * 12    1/30/98 12:25p Mike
26  * Make frand() not return 1.0, which can cause overflow when indexing
27  * into arrays.
28  * 
29  * 11    1/26/98 10:43p Mike
30  * Make ships not all zoom away from an impending shockwave at the same
31  * time.  Based on ai class and randomness
32  * 
33  * 10    1/20/98 9:47a Mike
34  * Suppress optimized compiler warnings.
35  * Some secondary weapon work.
36  * 
37  * 9     1/17/98 3:32p Mike
38  * Add rand_range(), returns random float in min..max.
39  * 
40  * 8     9/09/97 11:07a Sandeep
41  * fixed warning level 4
42  * 
43  * 7     8/05/97 10:18a Lawrance
44  * my_rand() being used temporarily instead of rand()
45  * 
46  * 6     2/17/97 5:18p John
47  * Added a bunch of RCS headers to a bunch of old files that don't have
48  * them.
49  *
50  * $NoKeywords: $
51  */
52
53 #include <stdlib.h>
54 #include <math.h>
55
56 #include "pstypes.h"
57 #include "floating.h"
58 #include "timer.h"
59
60 #define LOOKUP_BITS     6
61 #define EXP_POS         23
62 #define EXP_BIAS                127
63 typedef float FLOAT;
64
65 #define LOOKUP_POS      (EXP_POS-LOOKUP_BITS)
66 #define SEED_POS                (EXP_POS-8)
67 #define TABLE_SIZE      (2<<LOOKUP_BITS)
68 #define LOOKUP_MASK     (TABLE_SIZE-1)
69 #define GET_EXP(a)      (((a) >> EXP_POS) & 0xFF )
70 #define SET_EXP(a)      ((a) << EXP_POS )
71 #define GET_EMANT(a)    (((a) >> LOOKUP_POS) & LOOKUP_MASK )
72 #define SET_MANTSEED(a) (((unsigned long)(a)) << SEED_POS )
73
74 static unsigned char iSqrt[TABLE_SIZE];
75 static int iSqrt_inited = 0;
76
77 int fl_magic = 0x59C00000;              //representation of 2^51 + 2^52
78 const float *p_fl_magic = (const float *)&fl_magic;
79
80 union _flint {
81         unsigned long   i;
82         float                           f;
83 } fi, fo;
84
85 /*
86 static void MakeInverseSqrtLookupTable()
87 {
88         long f;
89         unsigned char *h;
90         union _flint fi, fo;
91
92         iSqrt_inited = 1;
93         for ( f=0, h=iSqrt; f < TABLE_SIZE; f++ )       {
94                 fi.i = ((EXP_BIAS-1)<<EXP_POS) | (f<<LOOKUP_POS);
95                 fo.f = 1.0f / fl_sqrt(fi.f);
96                 *h++ = (unsigned char)(((fo.i + (1<<(SEED_POS-2))) >>SEED_POS ) & 0xFF);
97         }
98         iSqrt[ TABLE_SIZE / 2 ] = 0xFF;
99 }
100 */
101
102 // HACK!
103 float fl_isqrt_c( float x )
104 {
105 //      unsigned long a = ((union _flint *)(&x))->i;
106 //      float arg = x;
107 //      union _flint seed;
108 //      FLOAT r;
109
110         int t1, t2, t3;
111         t1 = timer_get_microseconds();
112         float r1 =  1.0f / (float)sqrt((double)x);
113         t2 = timer_get_microseconds();
114 //      float r2 = fl_isqrt_asm(x);
115         t3 = timer_get_microseconds();  
116
117         return r1;
118
119
120 /*      if ( !iSqrt_inited )
121                 MakeInverseSqrtLookupTable();
122
123         seed.i = SET_EXP(((3*EXP_BIAS-1) - GET_EXP(a)) >> 1 ) | SET_MANTSEED(iSqrt[GET_EMANT(a)]);
124         r = seed.f;
125         r = (3.0f - r * r * arg ) * r * 0.5f;
126         r = (3.0f - r * r * arg ) * r * 0.5f;
127         return r;
128 */
129 }
130
131 // rounds off a floating point number to a multiple of some number
132 float fl_roundoff(float x, int multiple)
133 {
134         float half = (float) multiple / 2.0f;
135
136         if (x < 0)
137                 half = -half;
138
139         x += half;
140         return (float) (((int) x / multiple) * multiple);
141 }
142
143
144 //      Return random value in range 0.0..1.0- (1.0- means the closest number less than 1.0)
145 float frand()
146 {
147         float rval;
148         rval = ((float) myrand()) / (RAND_MAX + 1);
149         return rval;
150 }
151
152 //      Return a floating point number in the range min..max.
153 float frand_range(float min, float max)
154 {
155         float   rval;
156         
157         rval = frand();
158         rval = rval * (max - min) + min;
159
160         return rval;
161 }
162
163 //      Call this in the frame interval to get TRUE chance times per second.
164 //      If you want it to return TRUE 3 times per second, call it in the frame interval like so:
165 //              rand_chance(flFrametime, 3.0f);
166 int rand_chance(float frametime, float chance)  //      default value for chance = 1.0f.
167 {
168         while (--chance > 0.0f)
169                 if (frand() < frametime)
170                         return 1;
171
172         return frand() < (frametime * (chance + 1.0f));
173 }
174
175 /*fix fl2f( float x )
176 {
177         float nf;
178         nf = x*65536.0f + 8390656.0f;
179         return ((*((int *)&nf)) & 0x7FFFFF)-2048;
180 }
181 */
182
183
184 /*
185 >#define  S  65536.0
186 >#define  MAGIC  (((S * S * 16) + (S*.5)) * S)
187 >
188 >#pragma inline float2int;
189 >
190 >ulong float2int( float d )
191 >{
192 >  double dtemp = MAGIC + d;
193 >  return (*(ulong *)&dtemp) - 0x80000000;
194 >}
195
196 */