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