Page 2 of 2 FirstFirst 12
Results 11 to 20 of 20

Thread: Fixed point math

  1. #11

    Fixed point math

    When I used trunc it gave me slight incorrect values converting back, but maybe that's ok for you.
    <br /><br />There are a lot of people who are dead while they are still alive. I want to be alive until the day I die.<br />-= Paulo Coelho =-

  2. #12

    Fixed point math

    Comparing C and Pascal results, all seems ok. I have preferred to use trunc because AFAIK it should be faster than round, at least on delphi/win32. On fpc/arm it should be faster too, I suppose :think:
    Get your fpc4gba copy now!
    Get your fpc4nds copy now!

  3. #13

    Fixed point math

    I think Round will be much faster (twice?) than Trunc because Trunc has to reset the FPU. Try each a couple of million times in a loop and see.

  4. #14

    Fixed point math

    Round() much faster.
    I also thought that Trunc() faster then Round() and now have to change a lot of code.

  5. #15

    Fixed point math

    Calling Trunc is doing exactly what the C code does. In C, typecasting float to integer causes automatic truncing (rounding to nearest integer towards zero, this is exactly what the Trunc in Pascal does).

    BTW, what speed differences do you guys get with Delphi/win32 between Round and Trunc ?

    I tested with FPC 2.0.4, Linux, i386, and got

    Code:
    Test Single&#58;
    Trunc time&#58;       4.35
    Round time&#58;       3.37
    Test Double&#58;
    Trunc time&#58;       4.58
    Round time&#58;       3.37
    Test Extended&#58;
    Trunc time&#58;       5.82
    Round time&#58;       5.08
    which means that Round is indeed faster, but not terribly (and it matters only if you really do an awful lot of these calls, so it does not necessarily matter).

  6. #16

    Fixed point math

    Quote Originally Posted by pziko
    I think Round will be much faster (twice?) than Trunc because Trunc has to reset the FPU. Try each a couple of million times in a loop and see.
    Uhm... I'm working on ARM cpu, where the FPU is not present. I'm wondering about the speed in this case :think:
    Get your fpc4gba copy now!
    Get your fpc4nds copy now!

  7. #17

    Fixed point math

    These might do a fair chunk of what you need. I've been playing around recently with fixed point math in C using templated classes. It is easier when the precision is the same for both fixed point numbers, but there are calculations for when both fixed point numbers have different precisions and you want the result in a third precision.

    Edit: The FixedToInt function went missing, and the IntToFixed function had a typo.

    Code:
    type
      Fixed = Integer;
    
    const
      Prec = 12;
    
    function IntToFixed&#40;Value&#58; Integer&#41;&#58; Fixed;
    begin
      IntToFixed &#58;= Value shl Prec;
    end;
    
    function FixedToInt&#40;Value&#58; Fixed&#41;&#58; Integer;
    begin
      FixedToInt &#58;= Value shr Prec;
    end;
    
    function FloatToFixed&#40;Value&#58; Single&#41;&#58; Fixed;
    begin
      FloatToFixed &#58;= Round&#40;Value * &#40;1 shl Prec&#41;&#41;;
    end;
    
    function FixedToFloat&#40;Value&#58; Fixed&#41;&#58; Single;
    begin
      &#123; Multiply by 1.0 to convert to floating point before the divide &#125;
      FixedToFloat &#58;= &#40;Value * 1.0&#41; / &#40;&#40;1 shl Prec&#41; * 1.0&#41;;
    end;
    
    function FixedAdd&#40;A, B&#58; Fixed&#41;&#58; Fixed;
    begin
      FixedAdd &#58;= A + B;
    end;
    
    function FixedSub&#40;A, B&#58; Fixed&#41;&#58; Fixed;
    begin
      FixedSub &#58;= A - B;
    end;
    
    function FixedMul&#40;A, B&#58; Fixed&#41;&#58; Fixed;
    begin
      FixedMul &#58;= &#40;A * B&#41; shr Prec;
    end;
    
    function FixedDiv&#40;A, B&#58; Fixed&#41;&#58; Fixed;
    begin
      FixedDiv &#58;= &#40;A shl Prec&#41; / B;
    end;

  8. #18

    Fixed point math

    Quote Originally Posted by Legolas
    Uhm... I'm working on ARM cpu, where the FPU is not present. I'm wondering about the speed in this case :think:
    In that case Trunc() may be faster. It needs testing.

  9. #19

    Fixed point math

    Sly: thanks a lot! That's gold for me
    Get your fpc4gba copy now!
    Get your fpc4nds copy now!

  10. #20

    Fixed point math

    I hope they help. They were coded in my head as I haven't actually done fixed-point in Pascal before (only done it in C). So I hope they work.

Page 2 of 2 FirstFirst 12

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •