hedgewars/uFloat.pas
changeset 3929 9a4bbc1f67a2
parent 3599 715a43602da8
child 4374 bcefeeabaa33
equal deleted inserted replaced
3927:6f4d3c7fa8be 3929:9a4bbc1f67a2
    20 
    20 
    21 unit uFloat;
    21 unit uFloat;
    22 interface
    22 interface
    23 
    23 
    24 {$IFDEF FPC}
    24 {$IFDEF FPC}
    25 {$INLINE ON}
       
    26 {$IFDEF ENDIAN_LITTLE}
    25 {$IFDEF ENDIAN_LITTLE}
    27 type hwFloat = record
    26 type hwFloat = record
    28                isNegative: boolean;
    27                isNegative: boolean;
    29                case byte of
    28                case byte of
    30                0: (Frac, Round: Longword);
    29                0: (Frac, Round: Longword);
    41 
    40 
    42 function int2hwFloat (const i: LongInt) : hwFloat; inline;
    41 function int2hwFloat (const i: LongInt) : hwFloat; inline;
    43 
    42 
    44 operator + (const z1, z2: hwFloat) z : hwFloat; inline;
    43 operator + (const z1, z2: hwFloat) z : hwFloat; inline;
    45 operator - (const z1, z2: hwFloat) z : hwFloat; inline;
    44 operator - (const z1, z2: hwFloat) z : hwFloat; inline;
    46 operator - (const z1: hwFloat) z : hwFloat;
    45 operator - (const z1: hwFloat) z : hwFloat; inline;
    47 
    46 
    48 operator * (const z1, z2: hwFloat) z : hwFloat;
    47 operator * (const z1, z2: hwFloat) z : hwFloat; inline;
    49 operator * (const z1: hwFloat; const z2: LongInt) z : hwFloat; inline;
    48 operator * (const z1: hwFloat; const z2: LongInt) z : hwFloat; inline;
    50 operator / (const z1: hwFloat; z2: hwFloat) z : hwFloat;
    49 operator / (const z1: hwFloat; z2: hwFloat) z : hwFloat; inline;
    51 operator / (const z1: hwFloat; const z2: LongInt) z : hwFloat;
    50 operator / (const z1: hwFloat; const z2: LongInt) z : hwFloat; inline;
    52 
    51 
    53 operator < (const z1, z2: hwFloat) b : boolean;
    52 operator < (const z1, z2: hwFloat) b : boolean; inline;
    54 operator > (const z1, z2: hwFloat) b : boolean;
    53 operator > (const z1, z2: hwFloat) b : boolean; inline;
    55 
    54 
    56 function cstr(const z: hwFloat): shortstring;
    55 function cstr(const z: hwFloat): shortstring;
    57 function hwRound(const t: hwFloat): LongInt;
    56 function hwRound(const t: hwFloat): LongInt; inline;
    58 function hwAbs(const t: hwFloat): hwFloat;
    57 function hwAbs(const t: hwFloat): hwFloat; inline;
    59 function hwSqr(const t: hwFloat): hwFloat; inline;
    58 function hwSqr(const t: hwFloat): hwFloat; inline;
    60 function hwSqrt(const t: hwFloat): hwFloat; inline;
    59 function hwSqrt(const t: hwFloat): hwFloat; inline;
    61 function Distance(const dx, dy: hwFloat): hwFloat;
    60 function Distance(const dx, dy: hwFloat): hwFloat;
    62 function DistanceI(const dx, dy: LongInt): hwFloat;
    61 function DistanceI(const dx, dy: LongInt): hwFloat;
    63 function AngleSin(const Angle: Longword): hwFloat;
    62 function AngleSin(const Angle: Longword): hwFloat;
    64 function AngleCos(const Angle: Longword): hwFloat;
    63 function AngleCos(const Angle: Longword): hwFloat;
    65 function SignAs(const num, signum: hwFloat): hwFloat;
    64 function SignAs(const num, signum: hwFloat): hwFloat; inline;
    66 
    65 
    67 {$IFDEF FPC}
    66 {$IFDEF FPC}
    68 {$J-}
    67 {$J-}
    69 {$ENDIF}
    68 {$ENDIF}
    70 {$WARNINGS OFF}
    69 {$WARNINGS OFF}
   146 {$IFNDEF FPC}
   145 {$IFNDEF FPC}
   147 type hwFloat = Extended;
   146 type hwFloat = Extended;
   148 {$ENDIF}
   147 {$ENDIF}
   149 
   148 
   150 implementation
   149 implementation
   151 uses uMisc;
   150 //uses uMisc;
   152 
   151 
   153 
   152 
   154 {$IFDEF FPC}
   153 {$IFDEF FPC}
   155 
   154 
   156 function int2hwFloat (const i: LongInt) : hwFloat;
   155 function int2hwFloat (const i: LongInt) : hwFloat;
   346 {$INCLUDE "SinTable.inc"}
   345 {$INCLUDE "SinTable.inc"}
   347 
   346 
   348 function AngleSin(const Angle: Longword): hwFloat;
   347 function AngleSin(const Angle: Longword): hwFloat;
   349 begin
   348 begin
   350 {$IFDEF DEBUGFILE}
   349 {$IFDEF DEBUGFILE}
   351 TryDo((Angle >= 0) and (Angle <= 2048), 'Sin param exceeds limits', true);
   350 //TryDo((Angle >= 0) and (Angle <= 2048), 'Sin param exceeds limits', true);
   352 {$ENDIF}
   351 {$ENDIF}
   353 AngleSin.isNegative:= false;
   352 AngleSin.isNegative:= false;
   354 if Angle < 1024 then AngleSin.QWordValue:= SinTable[Angle]
   353 if Angle < 1024 then AngleSin.QWordValue:= SinTable[Angle]
   355                 else AngleSin.QWordValue:= SinTable[2048 - Angle]
   354                 else AngleSin.QWordValue:= SinTable[2048 - Angle]
   356 end;
   355 end;
   357 
   356 
   358 function AngleCos(const Angle: Longword): hwFloat;
   357 function AngleCos(const Angle: Longword): hwFloat;
   359 begin
   358 begin
   360 {$IFDEF DEBUGFILE}
   359 {$IFDEF DEBUGFILE}
   361 TryDo((Angle >= 0) and (Angle <= 2048), 'Cos param exceeds limits', true);
   360 //TryDo((Angle >= 0) and (Angle <= 2048), 'Cos param exceeds limits', true);
   362 {$ENDIF}
   361 {$ENDIF}
   363 AngleCos.isNegative:= Angle > 1024;
   362 AngleCos.isNegative:= Angle > 1024;
   364 if Angle < 1024 then AngleCos.QWordValue:= SinTable[1024 - Angle]
   363 if Angle < 1024 then AngleCos.QWordValue:= SinTable[1024 - Angle]
   365                 else AngleCos.QWordValue:= SinTable[Angle - 1024]
   364                 else AngleCos.QWordValue:= SinTable[Angle - 1024]
   366 end;
   365 end;