hedgewars/uFloat.pas
changeset 6580 6155187bf599
parent 6498 5678806aafca
child 6700 e04da46ee43c
--- a/hedgewars/uFloat.pas	Mon Jan 16 10:22:21 2012 +0100
+++ b/hedgewars/uFloat.pas	Tue Jan 17 09:01:31 2012 -0500
@@ -42,18 +42,18 @@
 {$IFDEF FPC}
 {$IFDEF ENDIAN_LITTLE}
 type hwFloat = record
-               isNegative: boolean;
-               case byte of
-               0: (Frac, Round: Longword);
-               1: (QWordValue : QWord);
-               end;
+    isNegative: boolean;
+    case byte of
+        0: (Frac, Round: Longword);
+        1: (QWordValue : QWord);
+        end;
 {$ELSE}
 type hwFloat = record
-               isNegative: boolean;
-               case byte of
-               0: (Round, Frac: Longword);
-               1: (QWordValue : QWord);
-               end;
+    isNegative: boolean;
+    case byte of
+    0: (Round, Frac: Longword);
+    1: (QWordValue : QWord);
+    end;
 {$ENDIF}
 
 // Returns an hwFloat that represents the value of integer parameter i
@@ -200,7 +200,8 @@
 function hwFloat2Float (const i: hwFloat) : extended;
 begin
 hwFloat2Float:= i.QWordValue / $100000000;
-if i.isNegative then hwFloat2Float:= -hwFloat2Float;
+if i.isNegative then
+    hwFloat2Float:= -hwFloat2Float;
 end;
 
 operator = (const z1, z2: hwFloat) z : boolean; inline;
@@ -212,38 +213,41 @@
 operator + (const z1, z2: hwFloat) z : hwFloat;
 begin
 if z1.isNegative = z2.isNegative then
-   begin
-   z.isNegative:= z1.isNegative;
-   z.QWordValue:= z1.QWordValue + z2.QWordValue
-   end
+    begin
+    z.isNegative:= z1.isNegative;
+    z.QWordValue:= z1.QWordValue + z2.QWordValue
+    end
 else
-   if z1.QWordValue > z2.QWordValue then
-      begin
-      z.isNegative:= z1.isNegative;
-      z.QWordValue:= z1.QWordValue - z2.QWordValue
-      end else
-      begin
-      z.isNegative:= z2.isNegative;
-      z.QWordValue:= z2.QWordValue - z1.QWordValue
-      end
+    if z1.QWordValue > z2.QWordValue then
+        begin
+        z.isNegative:= z1.isNegative;
+        z.QWordValue:= z1.QWordValue - z2.QWordValue
+        end
+    else
+        begin
+        z.isNegative:= z2.isNegative;
+        z.QWordValue:= z2.QWordValue - z1.QWordValue
+        end
 end;
 
 operator - (const z1, z2: hwFloat) z : hwFloat;
 begin
 if z1.isNegative = z2.isNegative then
-   if z1.QWordValue > z2.QWordValue then
-      begin
-      z.isNegative:= z1.isNegative;
-      z.QWordValue:= z1.QWordValue - z2.QWordValue
-      end else
-      begin
-      z.isNegative:= not z2.isNegative;
-      z.QWordValue:= z2.QWordValue - z1.QWordValue
-      end
-else begin
-     z.isNegative:= z1.isNegative;
-     z.QWordValue:= z1.QWordValue + z2.QWordValue
-     end
+    if z1.QWordValue > z2.QWordValue then
+        begin
+        z.isNegative:= z1.isNegative;
+        z.QWordValue:= z1.QWordValue - z2.QWordValue
+        end
+    else
+        begin
+        z.isNegative:= not z2.isNegative;
+        z.QWordValue:= z2.QWordValue - z1.QWordValue
+        end
+else
+    begin
+    z.isNegative:= z1.isNegative;
+    z.QWordValue:= z1.QWordValue + z2.QWordValue
+    end
 end;
 
 operator - (const z1: hwFloat) z : hwFloat;
@@ -256,9 +260,7 @@
 operator * (const z1, z2: hwFloat) z : hwFloat;
 begin
 z.isNegative:= z1.isNegative xor z2.isNegative;
-z.QWordValue:= QWord(z1.Round) * z2.Frac +
-               QWord(z1.Frac) * z2.Round +
-               ((QWord(z1.Frac) * z2.Frac) shr 32);
+z.QWordValue:= QWord(z1.Round) * z2.Frac + QWord(z1.Frac) * z2.Round + ((QWord(z1.Frac) * z2.Frac) shr 32);
 z.Round:= z.Round + QWord(z1.Round) * z2.Round;
 end;
 
@@ -275,18 +277,19 @@
 z.Round:= z1.QWordValue div z2.QWordValue;
 t:= z1 - z2 * z.Round;
 if t.QWordValue = 0 then
-   z.Frac:= 0
+    z.Frac:= 0
 else
-   begin
-   while ((t.QWordValue and $8000000000000000) = 0) and
-         ((z2.QWordValue and $8000000000000000) = 0) do
-         begin
-         t.QWordValue:= t.QWordValue shl 1;
-         z2.QWordValue:= z2.QWordValue shl 1
-         end;
-   if z2.Round > 0 then z.Frac:= (t.QWordValue) div (z2.Round)
-                   else z.Frac:= 0
-   end
+    begin
+    while ((t.QWordValue and $8000000000000000) = 0) and ((z2.QWordValue and $8000000000000000) = 0) do
+        begin
+        t.QWordValue:= t.QWordValue shl 1;
+        z2.QWordValue:= z2.QWordValue shl 1
+        end;
+    if z2.Round > 0 then
+        z.Frac:= (t.QWordValue) div (z2.Round)
+    else
+        z.Frac:= 0
+    end
 end;
 
 operator / (const z1: hwFloat; const z2: LongInt) z : hwFloat;
@@ -298,23 +301,23 @@
 operator < (const z1, z2: hwFloat) b : boolean;
 begin
 if z1.isNegative xor z2.isNegative then
-   b:= z1.isNegative
+    b:= z1.isNegative
 else
-   if z1.QWordValue = z2.QWordValue then
-      b:= false
-   else
-      b:= (z1.QWordValue < z2.QWordValue) xor z1.isNegative
+    if z1.QWordValue = z2.QWordValue then
+        b:= false
+    else
+        b:= (z1.QWordValue < z2.QWordValue) xor z1.isNegative
 end;
 
 operator > (const z1, z2: hwFloat) b : boolean;
 begin
 if z1.isNegative xor z2.isNegative then
-   b:= z2.isNegative
+    b:= z2.isNegative
 else
-   if z1.QWordValue = z2.QWordValue then
-      b:= false
-   else
-      b:= (z1.QWordValue > z2.QWordValue) xor z2.isNegative
+    if z1.QWordValue = z2.QWordValue then
+        b:= false
+    else
+        b:= (z1.QWordValue > z2.QWordValue) xor z2.isNegative
 end;
 
 function cstr(const z: hwFloat): shortstring;
@@ -322,18 +325,21 @@
 begin
 str(z.Round, cstr);
 if z.Frac <> 0 then
-   begin
-   str(z.Frac / $100000000, tmpstr);
-   delete(tmpstr, 1, 2);
-   cstr:= cstr + '.' + copy(tmpstr, 1, 10)
-   end;
-if z.isNegative then cstr:= '-' + cstr
+    begin
+    str(z.Frac / $100000000, tmpstr);
+    delete(tmpstr, 1, 2);
+    cstr:= cstr + '.' + copy(tmpstr, 1, 10)
+    end;
+if z.isNegative then
+    cstr:= '-' + cstr
 end;
 
 function hwRound(const t: hwFloat): LongInt;
 begin
-if t.isNegative then hwRound:= -(t.Round and $7FFFFFFF)
-                else hwRound:= t.Round and $7FFFFFFF
+if t.isNegative then
+    hwRound:= -(t.Round and $7FFFFFFF)
+else
+    hwRound:= t.Round and $7FFFFFFF
 end;
 
 function hwAbs(const t: hwFloat): hwFloat;
@@ -345,10 +351,7 @@
 function hwSqr(const t: hwFloat): hwFloat;
 begin
 hwSqr.isNegative:= false;
-hwSqr.QWordValue:=
-      ((QWord(t.Round) * t.Round) shl 32)
-    + QWord(t.Round) * t.Frac * 2
-    + ((QWord(t.Frac) * t.Frac) shr 32);
+hwSqr.QWordValue:= ((QWord(t.Round) * t.Round) shl 32) + QWord(t.Round) * t.Frac * 2 + ((QWord(t.Frac) * t.Frac) shr 32);
 end;
 
 function hwSqrt(const t: hwFloat): hwFloat;
@@ -358,19 +361,24 @@
 hwSqrt.isNegative:= false;
 
 if t.Round = 0 then
-   begin
-   l:= t.QWordValue;
-   r:= $100000000
-   end else
-   begin
-   l:= $100000000;
-   r:= t.QWordValue div 2 + $80000000; // r:= t / 2 + 0.5
-   if r > $FFFFFFFFFFFF then r:= $FFFFFFFFFFFF
-   end;
+    begin
+    l:= t.QWordValue;
+    r:= $100000000
+    end
+else
+    begin
+    l:= $100000000;
+    r:= t.QWordValue div 2 + $80000000; // r:= t / 2 + 0.5
+    if r > $FFFFFFFFFFFF then
+        r:= $FFFFFFFFFFFF
+    end;
 
 repeat
-  c.QWordValue:= (l + r) div 2;
-  if hwSqr(c).QWordValue > t.QWordValue then r:= c.QWordValue else l:= c.QWordValue
+    c.QWordValue:= (l + r) div 2;
+    if hwSqr(c).QWordValue > t.QWordValue then
+        r:= c.QWordValue
+    else
+        l:= c.QWordValue
 until r - l <= 1;
 
 hwSqrt.QWordValue:= l
@@ -395,7 +403,10 @@
 function hwSign(r: hwFloat): LongInt;
 begin
 // yes, we have negative zero for a reason
-if r.isNegative then hwSign:= -1 else hwSign:= 1
+if r.isNegative then
+    hwSign:= -1
+else
+    hwSign:= 1
 end;
 
 
@@ -403,21 +414,25 @@
 begin
 //TryDo((Angle >= 0) and (Angle <= 2048), 'Sin param exceeds limits', true);
 AngleSin.isNegative:= false;
-if Angle < 1024 then AngleSin.QWordValue:= SinTable[Angle]
-                else AngleSin.QWordValue:= SinTable[2048 - Angle]
+if Angle < 1024 then
+    AngleSin.QWordValue:= SinTable[Angle]
+else
+    AngleSin.QWordValue:= SinTable[2048 - Angle]
 end;
 
 function AngleCos(const Angle: Longword): hwFloat;
 begin
 //TryDo((Angle >= 0) and (Angle <= 2048), 'Cos param exceeds limits', true);
 AngleCos.isNegative:= Angle > 1024;
-if Angle < 1024 then AngleCos.QWordValue:= SinTable[1024 - Angle]
-                else AngleCos.QWordValue:= SinTable[Angle - 1024]
+if Angle < 1024 then
+    AngleCos.QWordValue:= SinTable[1024 - Angle]
+else
+    AngleCos.QWordValue:= SinTable[Angle - 1024]
 end;
 
 function isZero(const z: hwFloat): boolean; inline; 
 begin
-    isZero := z.QWordValue = 0;
+isZero := z.QWordValue = 0;
 end;
 {$ENDIF}