351
|
1 |
(*
|
|
2 |
* Hedgewars, a Worms style game
|
|
3 |
* Copyright (c) 2007 Andrey Korotaev <unC0Rr@gmail.com>
|
|
4 |
*
|
|
5 |
* This program is free software; you can redistribute it and/or modify
|
|
6 |
* it under the terms of the GNU General Public License as published by
|
|
7 |
* the Free Software Foundation; version 2 of the License
|
|
8 |
*
|
|
9 |
* This program is distributed in the hope that it will be useful,
|
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
12 |
* GNU General Public License for more details.
|
|
13 |
*
|
|
14 |
* You should have received a copy of the GNU General Public License
|
|
15 |
* along with this program; if not, write to the Free Software
|
|
16 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
|
|
17 |
*)
|
|
18 |
|
|
19 |
unit uFloat;
|
|
20 |
interface
|
|
21 |
|
|
22 |
{$IFDEF FPC}
|
|
23 |
{$ifdef FPC_LITTLE_ENDIAN}
|
|
24 |
type hwFloat = record
|
|
25 |
isNegative: boolean;
|
|
26 |
case byte of
|
|
27 |
0: (Frac, Round: Longword);
|
|
28 |
1: (QWordValue : QWord);
|
|
29 |
end;
|
|
30 |
{$else FPC_LITTLE_ENDIAN}
|
|
31 |
type hwFloat = record
|
|
32 |
isNegative: boolean;
|
|
33 |
case byte of
|
|
34 |
0: (Round, Frac: Longword);
|
|
35 |
1: (QWordValue : QWord);
|
|
36 |
end;
|
|
37 |
{$endif FPC_LITTLE_ENDIAN}
|
|
38 |
|
|
39 |
operator := (i: LongInt) z : hwFloat;
|
|
40 |
|
|
41 |
operator + (z1, z2: hwFloat) z : hwFloat;
|
|
42 |
operator - (z1, z2: hwFloat) z : hwFloat;
|
|
43 |
operator - (z1: hwFloat) z : hwFloat;
|
|
44 |
|
|
45 |
operator * (z1, z2: hwFloat) z : hwFloat;
|
|
46 |
operator * (z1: hwFloat; z2: LongInt) z : hwFloat;
|
|
47 |
operator / (z1, z2: hwFloat) z : hwFloat;
|
|
48 |
|
|
49 |
operator < (z1, z2: hwFloat) b : boolean;
|
|
50 |
operator > (z1, z2: hwFloat) b : boolean;
|
|
51 |
|
|
52 |
function cstr(z: hwFloat): string;
|
|
53 |
function hwRound(t: hwFloat): integer;
|
|
54 |
function hwAbs(t: hwFloat): hwFloat;
|
|
55 |
function hwSqr(t: hwFloat): hwFloat;
|
357
|
56 |
function hwSqrt(t: hwFloat): hwFloat;
|
351
|
57 |
function Distance(dx, dy: hwFloat): hwFloat;
|
357
|
58 |
function AngleSin(Angle: Longword): hwFloat;
|
|
59 |
function AngleCos(Angle: Longword): hwFloat;
|
351
|
60 |
|
|
61 |
const _1div1024: hwFloat = (isNegative: false; QWordValue: 4194304);
|
|
62 |
_1div10000: hwFloat = (isNegative: false; QWordValue: 429496);
|
|
63 |
_1div50000: hwFloat = (isNegative: false; QWordValue: 85899);
|
|
64 |
_1div100000: hwFloat = (isNegative: false; QWordValue: 42950);
|
|
65 |
_1div3: hwFloat = (isNegative: false; QWordValue: 1431655766);
|
|
66 |
hwPi: hwFloat = (isNegative: false; QWordValue: 13493037704);
|
|
67 |
_0_000004: hwFloat = (isNegative: false; QWordValue: 17179);
|
|
68 |
_0_0002: hwFloat = (isNegative: false; QWordValue: 858993);
|
|
69 |
_0_001: hwFloat = (isNegative: false; QWordValue: 4294967);
|
|
70 |
_0_003: hwFloat = (isNegative: false; QWordValue: 12884902);
|
|
71 |
_0_004: hwFloat = (isNegative: false; QWordValue: 17179869);
|
|
72 |
_0_005: hwFloat = (isNegative: false; QWordValue: 21474836);
|
|
73 |
_0_01: hwFloat = (isNegative: false; QWordValue: 42949673);
|
|
74 |
_0_02: hwFloat = (isNegative: false; QWordValue: 85899345);
|
|
75 |
_0_03: hwFloat = (isNegative: false; QWordValue: 128849018);
|
|
76 |
_0_08: hwFloat = (isNegative: false; QWordValue: 343597383);
|
|
77 |
_0_1: hwFloat = (isNegative: false; QWordValue: 429496729);
|
|
78 |
_0_15: hwFloat = (isNegative: false; QWordValue: 644245094);
|
|
79 |
_0_2: hwFloat = (isNegative: false; QWordValue: 858993459);
|
|
80 |
_0_25: hwFloat = (isNegative: false; QWordValue: 1073741824);
|
|
81 |
_0_3: hwFloat = (isNegative: false; QWordValue: 1288490189);
|
|
82 |
_0_35: hwFloat = (isNegative: false; QWordValue: 1503238553);
|
|
83 |
_0_4: hwFloat = (isNegative: false; QWordValue: 1717986918);
|
|
84 |
_0_45: hwFloat = (isNegative: false; QWordValue: 1932735283);
|
|
85 |
_0_5: hwFloat = (isNegative: false; QWordValue: 2147483648);
|
|
86 |
_0_55: hwFloat = (isNegative: false; QWordValue: 2362232012);
|
|
87 |
_0_6: hwFloat = (isNegative: false; QWordValue: 2576980377);
|
358
|
88 |
_0_7: hwFloat = (isNegative: false; QWordValue: 3006477107);
|
351
|
89 |
_0_8: hwFloat = (isNegative: false; QWordValue: 3435973837);
|
|
90 |
_0_84: hwFloat = (isNegative: false; QWordValue: 3607772528);
|
|
91 |
_0_87: hwFloat = (isNegative: false; QWordValue: 3736621547);
|
|
92 |
_0_9: hwFloat = (isNegative: false; QWordValue: 3865470566);
|
|
93 |
_0_93: hwFloat = (isNegative: false; QWordValue: 3994319585);
|
|
94 |
_0_96: hwFloat = (isNegative: false; QWordValue: 4123168604);
|
|
95 |
_0_995: hwFloat = (isNegative: false; QWordValue: 4273492459);
|
|
96 |
_0_999: hwFloat = (isNegative: false; QWordValue: 4290672328);
|
|
97 |
_1_9: hwFloat = (isNegative: false; QWordValue: 8160437862);
|
|
98 |
|
|
99 |
cLittle: hwFloat = (isNegative: false; QWordValue: 1);
|
|
100 |
cHHKick: hwFloat = (isNegative: false; QWordValue: 128849018);
|
|
101 |
{$ENDIF}
|
|
102 |
|
|
103 |
{$IFNDEF FPC}
|
|
104 |
type hwFloat = Extended;
|
|
105 |
{$ENDIF}
|
|
106 |
|
|
107 |
implementation
|
|
108 |
uses uConsts;
|
|
109 |
|
|
110 |
{$IFDEF FPC}
|
|
111 |
|
|
112 |
operator := (i: LongInt) z : hwFloat;
|
|
113 |
begin
|
|
114 |
z.isNegative:= i < 0;
|
|
115 |
z.Round:= abs(i);
|
|
116 |
z.Frac:= 0
|
|
117 |
end;
|
|
118 |
|
|
119 |
operator + (z1, z2: hwFloat) z : hwFloat;
|
|
120 |
begin
|
|
121 |
if z1.isNegative = z2.isNegative then
|
|
122 |
begin
|
|
123 |
z.isNegative:= z1.isNegative;
|
|
124 |
z.QWordValue:= z1.QWordValue + z2.QWordValue
|
|
125 |
end
|
|
126 |
else
|
|
127 |
if z1.QWordValue > z2.QWordValue then
|
|
128 |
begin
|
|
129 |
z.isNegative:= z1.isNegative;
|
|
130 |
z.QWordValue:= z1.QWordValue - z2.QWordValue
|
|
131 |
end else
|
|
132 |
begin
|
|
133 |
z.isNegative:= z2.isNegative;
|
|
134 |
z.QWordValue:= z2.QWordValue - z1.QWordValue
|
|
135 |
end
|
|
136 |
end;
|
|
137 |
|
|
138 |
operator - (z1, z2: hwFloat) z : hwFloat;
|
|
139 |
begin
|
|
140 |
if z1.isNegative = z2.isNegative then
|
|
141 |
if z1.QWordValue > z2.QWordValue then
|
|
142 |
begin
|
|
143 |
z.isNegative:= z1.isNegative;
|
|
144 |
z.QWordValue:= z1.QWordValue - z2.QWordValue
|
|
145 |
end else
|
|
146 |
begin
|
|
147 |
z.isNegative:= not z2.isNegative;
|
|
148 |
z.QWordValue:= z2.QWordValue - z1.QWordValue
|
|
149 |
end
|
|
150 |
else begin
|
|
151 |
z.isNegative:= z1.isNegative;
|
|
152 |
z.QWordValue:= z1.QWordValue + z2.QWordValue
|
|
153 |
end
|
|
154 |
end;
|
|
155 |
|
|
156 |
operator - (z1: hwFloat) z : hwFloat;
|
|
157 |
begin
|
|
158 |
z:= z1;
|
|
159 |
z.isNegative:= not z.isNegative
|
|
160 |
end;
|
|
161 |
|
|
162 |
|
|
163 |
operator * (z1, z2: hwFloat) z : hwFloat;
|
|
164 |
begin
|
|
165 |
z.isNegative:= z1.isNegative xor z2.isNegative;
|
|
166 |
z.QWordValue:= QWord(z1.Round) * z2.Frac +
|
|
167 |
QWord(z1.Frac) * z2.Round +
|
|
168 |
((QWord(z1.Frac) * z2.Frac) shr 32);
|
|
169 |
z.Round:= z.Round + QWord(z1.Round) * z2.Round;
|
|
170 |
end;
|
|
171 |
|
|
172 |
operator * (z1: hwFloat; z2: LongInt) z : hwFloat;
|
|
173 |
begin
|
|
174 |
z.isNegative:= z1.isNegative xor (z2 < 0);
|
|
175 |
z2:= abs(z2);
|
355
|
176 |
z.QWordValue:= z1.QWordValue * z2
|
351
|
177 |
end;
|
|
178 |
|
|
179 |
operator / (z1, z2: hwFloat) z : hwFloat;
|
|
180 |
var t: hwFloat;
|
|
181 |
begin
|
|
182 |
z.isNegative:= z1.isNegative xor z2.isNegative;
|
|
183 |
z.Round:= z1.QWordValue div z2.QWordValue;
|
|
184 |
t:= z1 - z2 * z.Round;
|
|
185 |
if t.QWordValue = 0 then
|
|
186 |
z.Frac:= 0
|
|
187 |
else
|
|
188 |
begin
|
|
189 |
while ((t.QWordValue and $8000000000000000) = 0) and
|
|
190 |
((z2.QWordValue and $8000000000000000) = 0) do
|
|
191 |
begin
|
|
192 |
t.QWordValue:= t.QWordValue shl 1;
|
|
193 |
z2.QWordValue:= z2.QWordValue shl 1
|
|
194 |
end;
|
|
195 |
z.Frac:= (t.QWordValue) div (z2.Round)
|
|
196 |
end
|
|
197 |
end;
|
|
198 |
|
|
199 |
operator < (z1, z2: hwFloat) b : boolean;
|
|
200 |
begin
|
|
201 |
if z1.isNegative <> z2.isNegative then
|
|
202 |
b:= z1.isNegative
|
|
203 |
else
|
|
204 |
if z1.QWordValue = z2.QWordValue then
|
|
205 |
b:= false
|
|
206 |
else
|
|
207 |
b:= (z1.QWordValue < z2.QWordValue) xor z1.isNegative
|
|
208 |
end;
|
|
209 |
|
|
210 |
operator > (z1, z2: hwFloat) b : boolean;
|
|
211 |
begin
|
|
212 |
if z1.isNegative <> z2.isNegative then
|
|
213 |
b:= z2.isNegative
|
|
214 |
else
|
|
215 |
if z1.QWordValue = z2.QWordValue then
|
|
216 |
b:= false
|
|
217 |
else
|
|
218 |
b:= (z1.QWordValue > z2.QWordValue) xor z2.isNegative
|
|
219 |
end;
|
|
220 |
|
|
221 |
function cstr(z: hwFloat): string;
|
|
222 |
var tmpstr: string;
|
|
223 |
begin
|
|
224 |
str(z.Round, cstr);
|
|
225 |
if z.Frac <> 0 then
|
|
226 |
begin
|
|
227 |
str(z.Frac / $100000000:1:15, tmpstr);
|
|
228 |
delete(tmpstr, 1, 2);
|
|
229 |
cstr:= cstr + '.' + tmpstr
|
|
230 |
end;
|
|
231 |
if z.isNegative then cstr:= '-' + cstr
|
|
232 |
end;
|
|
233 |
|
|
234 |
function hwRound(t: hwFloat): integer;
|
|
235 |
begin
|
|
236 |
if t.isNegative then hwRound:= -t.Round
|
|
237 |
else hwRound:= t.Round
|
|
238 |
end;
|
|
239 |
|
|
240 |
function hwAbs(t: hwFloat): hwFloat;
|
|
241 |
begin
|
|
242 |
hwAbs:= t;
|
|
243 |
hwAbs.isNegative:= false
|
|
244 |
end;
|
|
245 |
|
|
246 |
function hwSqr(t: hwFloat): hwFloat;
|
|
247 |
begin
|
|
248 |
hwSqr:= t * t
|
|
249 |
end;
|
|
250 |
|
357
|
251 |
function hwSqrt(t: hwFloat): hwFloat;
|
|
252 |
begin
|
|
253 |
hwSqrt.isNegative:= false;
|
|
254 |
hwSqrt.QWordValue:= Round(sqrt(1.0 / $100000000 * (t.QWordValue)) * $100000000)
|
|
255 |
end;
|
|
256 |
|
351
|
257 |
function Distance(dx, dy: hwFloat): hwFloat;
|
|
258 |
var x, y: hwFloat;
|
|
259 |
Result: hwFloat;
|
|
260 |
begin
|
|
261 |
x:= dx * dx;
|
|
262 |
y:= dy * dy;
|
|
263 |
Result:= x + y;
|
357
|
264 |
Result.QWordValue:= Round(sqrt(1.0 / $100000000 * (Result.QWordValue)) * $100000000);
|
351
|
265 |
Distance:= Result
|
|
266 |
end;
|
|
267 |
|
357
|
268 |
{$INCLUDE SinTable.inc}
|
|
269 |
|
|
270 |
function AngleSin(Angle: Longword): hwFloat;
|
351
|
271 |
begin
|
|
272 |
AngleSin.isNegative:= false;
|
357
|
273 |
if Angle < 1024 then AngleSin.QWordValue:= SinTable[Angle]
|
|
274 |
else AngleSin.QWordValue:= SinTable[2048 - Angle]
|
351
|
275 |
end;
|
|
276 |
|
357
|
277 |
function AngleCos(Angle: Longword): hwFloat;
|
351
|
278 |
var CosVal: Extended;
|
|
279 |
begin
|
357
|
280 |
AngleCos.isNegative:= Angle > 1024;
|
|
281 |
if Angle < 1024 then AngleCos.QWordValue:= SinTable[1024 - Angle]
|
|
282 |
else AngleCos.QWordValue:= SinTable[Angle - 1024]
|
351
|
283 |
end;
|
|
284 |
|
|
285 |
{$ENDIF}
|
|
286 |
|
|
287 |
end.
|