hedgewars/uFloat.pas
changeset 498 9c8b385dc9a1
parent 493 0c0ed17ad675
child 513 69e06d710d46
equal deleted inserted replaced
497:adf1aee202c6 498:9c8b385dc9a1
    34                0: (Round, Frac: Longword);
    34                0: (Round, Frac: Longword);
    35                1: (QWordValue : QWord);
    35                1: (QWordValue : QWord);
    36                end;
    36                end;
    37 {$endif FPC_LITTLE_ENDIAN}
    37 {$endif FPC_LITTLE_ENDIAN}
    38 
    38 
    39 operator := (i: LongInt) z : hwFloat;
    39 function int2hwFloat (i: LongInt) : hwFloat;
    40 
    40 
    41 operator + (z1, z2: hwFloat) z : hwFloat;
    41 operator + (z1, z2: hwFloat) z : hwFloat;
    42 operator - (z1, z2: hwFloat) z : hwFloat;
    42 operator - (z1, z2: hwFloat) z : hwFloat;
    43 operator - (z1: hwFloat) z : hwFloat;
    43 operator - (z1: hwFloat) z : hwFloat;
    44 
    44 
    45 operator * (z1, z2: hwFloat) z : hwFloat;
    45 operator * (z1, z2: hwFloat) z : hwFloat;
    46 operator * (z1: hwFloat; z2: LongInt) z : hwFloat;
    46 operator * (z1: hwFloat; z2: LongInt) z : hwFloat;
    47 operator / (z1, z2: hwFloat) z : hwFloat;
    47 operator / (z1, z2: hwFloat) z : hwFloat;
       
    48 operator / (z1: hwFloat; z2: LongInt) z : hwFloat;
    48 
    49 
    49 operator < (z1, z2: hwFloat) b : boolean;
    50 operator < (z1, z2: hwFloat) b : boolean;
    50 operator > (z1, z2: hwFloat) b : boolean;
    51 operator > (z1, z2: hwFloat) b : boolean;
    51 
    52 
    52 function cstr(z: hwFloat): string;
    53 function cstr(z: hwFloat): string;
    53 function hwRound(t: hwFloat): LongInt;
    54 function hwRound(t: hwFloat): LongInt;
    54 function hwAbs(t: hwFloat): hwFloat;
    55 function hwAbs(t: hwFloat): hwFloat;
    55 function hwSqr(t: hwFloat): hwFloat;
    56 function hwSqr(t: hwFloat): hwFloat;
    56 function hwSqrt(t: hwFloat): hwFloat;
    57 function hwSqrt(t: hwFloat): hwFloat;
    57 function Distance(dx, dy: hwFloat): hwFloat;
    58 function Distance(dx, dy: hwFloat): hwFloat;
       
    59 function DistanceI(dx, dy: LongInt): hwFloat;
    58 function AngleSin(Angle: Longword): hwFloat;
    60 function AngleSin(Angle: Longword): hwFloat;
    59 function AngleCos(Angle: Longword): hwFloat;
    61 function AngleCos(Angle: Longword): hwFloat;
       
    62 function SignAs(num, signum: hwFloat): hwFloat;
    60 
    63 
    61 const  _1div1024: hwFloat = (isNegative: false; QWordValue:     4194304);
    64 const  _1div1024: hwFloat = (isNegative: false; QWordValue:     4194304);
    62       _1div10000: hwFloat = (isNegative: false; QWordValue:      429496);
    65       _1div10000: hwFloat = (isNegative: false; QWordValue:      429496);
    63       _1div50000: hwFloat = (isNegative: false; QWordValue:       85899);
    66       _1div50000: hwFloat = (isNegative: false; QWordValue:       85899);
    64      _1div100000: hwFloat = (isNegative: false; QWordValue:       42950);
    67      _1div100000: hwFloat = (isNegative: false; QWordValue:       42950);
    93            _0_93: hwFloat = (isNegative: false; QWordValue:  3994319585);
    96            _0_93: hwFloat = (isNegative: false; QWordValue:  3994319585);
    94            _0_96: hwFloat = (isNegative: false; QWordValue:  4123168604);
    97            _0_96: hwFloat = (isNegative: false; QWordValue:  4123168604);
    95           _0_995: hwFloat = (isNegative: false; QWordValue:  4273492459);
    98           _0_995: hwFloat = (isNegative: false; QWordValue:  4273492459);
    96           _0_999: hwFloat = (isNegative: false; QWordValue:  4290672328);
    99           _0_999: hwFloat = (isNegative: false; QWordValue:  4290672328);
    97             _1_9: hwFloat = (isNegative: false; QWordValue:  8160437862);
   100             _1_9: hwFloat = (isNegative: false; QWordValue:  8160437862);
       
   101               _0: hwFloat = (isNegative: false; QWordValue:           0);
       
   102               _1: hwFloat = (isNegative: false; QWordValue:  4294967296);
       
   103               _2: hwFloat = (isNegative: false; QWordValue:  4294967296 * 2);
       
   104               _3: hwFloat = (isNegative: false; QWordValue:  4294967296 * 3);
       
   105               _4: hwFloat = (isNegative: false; QWordValue:  4294967296 * 4);
       
   106               _5: hwFloat = (isNegative: false; QWordValue:  4294967296 * 5);
       
   107               _6: hwFloat = (isNegative: false; QWordValue:  4294967296 * 6);
       
   108              _10: hwFloat = (isNegative: false; QWordValue:  4294967296 * 10);
       
   109              _16: hwFloat = (isNegative: false; QWordValue:  4294967296 * 16);
       
   110              _19: hwFloat = (isNegative: false; QWordValue:  4294967296 * 19);
       
   111              _20: hwFloat = (isNegative: false; QWordValue:  4294967296 * 20);
       
   112              _25: hwFloat = (isNegative: false; QWordValue:  4294967296 * 25);
       
   113              _30: hwFloat = (isNegative: false; QWordValue:  4294967296 * 30);
       
   114             _128: hwFloat = (isNegative: false; QWordValue:  4294967296 * 128);
       
   115             _450: hwFloat = (isNegative: false; QWordValue:  4294967296 * 450);
       
   116            _1024: hwFloat = (isNegative: false; QWordValue:  4398046511104);
       
   117            _2048: hwFloat = (isNegative: false; QWordValue:  8796093022208);
       
   118           _10000: hwFloat = (isNegative: false; QWordValue:  4294967296 * 10000);
    98 
   119 
    99          cLittle: hwFloat = (isNegative: false; QWordValue:           1);
   120          cLittle: hwFloat = (isNegative: false; QWordValue:           1);
   100          cHHKick: hwFloat = (isNegative: false; QWordValue:   128849018);
   121          cHHKick: hwFloat = (isNegative: false; QWordValue:   128849018);
   101 {$ENDIF}
   122 {$ENDIF}
   102 
   123 
   107 implementation
   128 implementation
   108 uses uConsts;
   129 uses uConsts;
   109 
   130 
   110 {$IFDEF FPC}
   131 {$IFDEF FPC}
   111 
   132 
   112 operator := (i: LongInt) z : hwFloat;
   133 function int2hwFloat (i: LongInt) : hwFloat;
   113 begin
   134 begin
   114 z.isNegative:= i < 0;
   135 int2hwFloat.isNegative:= i < 0;
   115 z.Round:= abs(i);
   136 int2hwFloat.Round:= abs(i);
   116 z.Frac:= 0
   137 int2hwFloat.Frac:= 0
   117 end;
   138 end;
   118 
   139 
   119 operator + (z1, z2: hwFloat) z : hwFloat;
   140 operator + (z1, z2: hwFloat) z : hwFloat;
   120 begin
   141 begin
   121 if z1.isNegative = z2.isNegative then
   142 if z1.isNegative = z2.isNegative then
   194          end;
   215          end;
   195    z.Frac:= (t.QWordValue) div (z2.Round)
   216    z.Frac:= (t.QWordValue) div (z2.Round)
   196    end
   217    end
   197 end;
   218 end;
   198 
   219 
       
   220 operator / (z1: hwFloat; z2: LongInt) z : hwFloat;
       
   221 begin
       
   222 z.isNegative:= z1.isNegative xor (z2 < 0);
       
   223 z2:= abs(z2);
       
   224 z.QWordValue:= z1.QWordValue div z2
       
   225 end;
       
   226 
   199 operator < (z1, z2: hwFloat) b : boolean;
   227 operator < (z1, z2: hwFloat) b : boolean;
   200 begin
   228 begin
   201 if z1.isNegative <> z2.isNegative then
   229 if z1.isNegative <> z2.isNegative then
   202    b:= z1.isNegative
   230    b:= z1.isNegative
   203 else
   231 else
   263 Result:= x + y;
   291 Result:= x + y;
   264 Result.QWordValue:= Round(sqrt(1.0 / $100000000 * (Result.QWordValue)) * $100000000);
   292 Result.QWordValue:= Round(sqrt(1.0 / $100000000 * (Result.QWordValue)) * $100000000);
   265 Distance:= Result
   293 Distance:= Result
   266 end;
   294 end;
   267 
   295 
       
   296 function DistanceI(dx, dy: LongInt): hwFloat;
       
   297 begin
       
   298 DistanceI:= Distance(int2hwFloat(dx), int2hwFloat(dy))
       
   299 end;
       
   300 
       
   301 function SignAs(num, signum: hwFloat): hwFloat;
       
   302 begin
       
   303 SignAs:= num;
       
   304 SignAs.isNegative:= signum.isNegative
       
   305 end;
       
   306 
   268 {$INCLUDE SinTable.inc}
   307 {$INCLUDE SinTable.inc}
   269 
   308 
   270 function AngleSin(Angle: Longword): hwFloat;
   309 function AngleSin(Angle: Longword): hwFloat;
   271 begin
   310 begin
   272 AngleSin.isNegative:= false;
   311 AngleSin.isNegative:= false;