Recent

Author Topic: New Big Integer library is almost finished  (Read 18271 times)

ad1mt

  • Sr. Member
  • ****
  • Posts: 327
    • Mark Taylor's Home Page
Re: New Big Integer library is finished
« Reply #90 on: October 16, 2024, 09:51:15 am »
I've found the problem.

I removed the inline hint on all the functions because it was interfering with the debugging of the recent problems, and this has slowed everything down by about 50-100%.
What I'm going to do is make all the inline hints switchable with a {$define}, then they can be turned off/on easily.

This will take a day or two, because it appears that for the hint to be acted upon by the compiler, all instances of the function header must have the inline hint, not just the final one with the code inside. Also, I don't want to put inline on all the functions, just the ones where it will matter, e.g. the many "wrapper" functions with minimal code that just call another main function to do the work.
« Last Edit: October 16, 2024, 01:25:07 pm by ad1mt »

Thaddy

  • Hero Member
  • *****
  • Posts: 16177
  • Censorship about opinions does not belong here.
Re: New Big Integer library is finished
« Reply #91 on: October 16, 2024, 01:57:34 pm »
You can simply skip that and add {$inline off/on}
If {$inline on} the compiler tries do inline everything without you having to specify it and the other way around: then it only inlines what is marked inline if possible.
« Last Edit: October 16, 2024, 02:00:19 pm by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

ad1mt

  • Sr. Member
  • ****
  • Posts: 327
    • Mark Taylor's Home Page
Re: New Big Integer library is finished
« Reply #92 on: October 16, 2024, 06:02:20 pm »
You can simply skip that and add {$inline off/on}
Thanks for the tip.
I should RTFM more!   :)

ad1mt

  • Sr. Member
  • ****
  • Posts: 327
    • Mark Taylor's Home Page
Re: New Big Integer library is finished
« Reply #93 on: October 16, 2024, 10:12:58 pm »
I've just made version 4.67 available.
This has inline restored on function definitions, and a small speed improvement in the multiplication function.
The execution speed is about the same as version 4.61, maybe slightly faster.

srvaldez

  • Full Member
  • ***
  • Posts: 117
Re: New Big Integer library is finished
« Reply #94 on: October 16, 2024, 11:04:29 pm »
well done ad1mt  :D
factorial now takes .156 seconds, 4.61 took about .21 seconds, that's about a 35% increase in performance
the division of the factorial now takes 1.7 seconds, 4.61 took about 2.03 seconds, a 19% increase in performance
the only problem now is that there are quite a number of warnings of uninitialized variables when the Multi_Int unit is compiled but that happens only once so no problem, but now I also get the following warnings which I didn't get before, but this will silence it {$warn 6058 off}
Quote
speed test.pas(55,8) Note: Call to subroutine "operator Multi_Int_XV.:=(const v1:AnsiString):<record type>; Static;" marked as inline is not inlined
speed test.pas(59,8) Note: Call to subroutine "operator Multi_Int_XV.:=(const v1:Int64):<record type>; Static;" marked as inline is not inlined
speed test.pas(62,13) Note: Call to subroutine "operator Multi_Int_XV.*(const v1:Multi_Int_XV;const v2:Multi_Int_XV):<record type>; Static;" marked as inline is not inlined
speed test.pas(62,13) Note: Call to subroutine "operator Multi_Int_XV.:=(const v1:Int64):<record type>; Static;" marked as inline is not inlined
speed test.pas(85,20) Note: Call to subroutine "function Multi_Int_XV.ToStr:AnsiString;" marked as inline is not inlined
« Last Edit: October 16, 2024, 11:06:06 pm by srvaldez »

Thaddy

  • Hero Member
  • *****
  • Posts: 16177
  • Censorship about opinions does not belong here.
Re: New Big Integer library is finished
« Reply #95 on: October 17, 2024, 07:14:21 am »
{$notes off} and maybe {$hints off}
Notes and hints are usually not critical.
Both are local directives so just surround the operator declarations like so:
Code: Pascal  [Select][+][-]
  1. {$push}{$notes off}{$hints off}
  2. operator and (const a,b:Q4way):Q4way;inline;
  3. operator or (const a,b:Q4way):Q4way;inline;
  4. operator xor (const a,b:Q4way):Q4way;inline;
  5. operator not (const a:Q4way):Q4way;inline;
  6. operator in (const a,b:Q4way):Boolean;inline;
  7. {$pop}
« Last Edit: October 17, 2024, 07:44:05 am by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

ad1mt

  • Sr. Member
  • ****
  • Posts: 327
    • Mark Taylor's Home Page
Re: New Big Integer library is finished
« Reply #96 on: October 17, 2024, 10:53:36 am »
speed test.pas(55,8) Note: Call to subroutine "operator Multi_Int_XV.:=(const v1:AnsiString):<record type>; Static;" marked as inline is not inlined
These messages can be turned off. If are using Lazarus, go to compiler options -> messages, and in the list you be able to find...
Code: Text  [Select][+][-]
  1. Note: Call to subroutine "$1" marked inline is not inlined.
with a tick box. If you untick the box, that will disable just those messages.
I tried to find how to do this in the source, but couldn't see a way. Someone who is more expert than me in Free Pascal might have a suggestion   :)

The warnings about uninitialised variables were a bit more serious. They were caused by some functions having var parameters when they should have had out parameters. I've just made version 4.68 available, which fixes these.
I prefer not to turn off compiler messages... they often give valuable clues about bugs in your code that you have missed.
« Last Edit: October 17, 2024, 10:56:46 am by ad1mt »

Thaddy

  • Hero Member
  • *****
  • Posts: 16177
  • Censorship about opinions does not belong here.
Re: New Big Integer library is finished
« Reply #97 on: October 17, 2024, 10:58:34 am »
That is why adviced to do it locally and only for the routines that are "affected".
Once they can be inlined, the code need not change in any way.
i know your coding style a bit, so your operators are neatly grouped anyway.
« Last Edit: October 17, 2024, 11:03:05 am by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

srvaldez

  • Full Member
  • ***
  • Posts: 117
Re: New Big Integer library is finished
« Reply #98 on: October 18, 2024, 01:22:44 am »
here's Pi
20,000 digits in about 1 second, code borrowed from https://www.ellipsa.eu/public/fnx/fnx.html and adapted to Multi_Int
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}
  2. {$MODESWITCH NESTEDCOMMENTS+}
  3. {$warn 6058 off}
  4. {$warn 5025 off}
  5.  
  6. program speed_test;
  7. uses    sysutils
  8. ,       strutils
  9. ,       strings
  10. ,       math
  11. ,       Multi_Int
  12. ;
  13.  
  14. function ArcTanInv(const t: Multi_Int_XV; m,a: UInt64): Multi_Int_XV;
  15. var
  16.     r    : Multi_Int_XV;
  17.     s    : Multi_Int_XV;
  18.     b, k : UInt64;
  19. begin
  20.     s := t*(m*a); // s := 10**d * m * a
  21.     b := a*a + 1;
  22.     a := b + b;
  23.     k := 0;
  24.     r := 0;
  25.     repeat
  26.         s := s div b;
  27.         if s=0 then
  28.             Break;
  29.         r := r + s;
  30.         Inc(k,2);
  31.         s := s * k;
  32.         Inc(b,a);
  33.     until FALSE;
  34.     result:=r;
  35. end;
  36.  
  37. const prec = 20000; // digits of precision
  38.  
  39. var
  40.  
  41.     big_int_size,
  42.     start_time,
  43.     end_time        :int32;
  44.     delta           :double;
  45.  
  46.     t, x        :Multi_Int_XV;
  47.     n:UInt32;
  48.  
  49. begin
  50.     big_int_size:= 2 + (round(2 * prec * 3.321928094887362) div 64);
  51.     Multi_Int_Initialisation(big_int_size);
  52.  
  53.     n := 20000; // 20000 decimals
  54.  
  55.     start_time:= GetTickCount64;
  56.     // t := 10**(n+4) (+4 digits for rounding errors)
  57.     t:=10;
  58.     t := t**(n+4);
  59.     // Pi = 48 ArcTan(1/18) + 32 ArcTan(1/57) - 20 ArcTan(1/239)
  60.     x := ArcTanInv(t,48,18) + ArcTanInv(t,32,57) - ArcTanInv(t,20,239);
  61.     // suppress the 4 added digits
  62.     x := x div 100000;
  63.     end_time:= GetTickCount64;
  64.  
  65.     writeln;
  66.     writeln('Trunc(Pi * 10**' + IntToStr(n) + ')');
  67.     writeln(x.ToStr);
  68.     delta:= (end_time - start_time) / 1000;
  69.     writeln('time elapsed is ', delta:1:6, ' seconds');
  70.  
  71. end.
  72.  

ad1mt

  • Sr. Member
  • ****
  • Posts: 327
    • Mark Taylor's Home Page
Re: New Big Integer library is finished
« Reply #99 on: October 18, 2024, 07:46:30 am »
here's Pi 20,000 digits in about 1 second
But have you checked that they are the correct digits and in the correct order?   :)
The last time you did a test like this, it revealed lots of bugs in my code!
« Last Edit: October 18, 2024, 09:43:29 am by ad1mt »

ad1mt

  • Sr. Member
  • ****
  • Posts: 327
    • Mark Taylor's Home Page
Re: New Big Integer library is finished
« Reply #100 on: October 18, 2024, 08:38:53 am »
code borrowed from https://www.ellipsa.eu/public/fnx/fnx.html
I think FNX is very good. If it worked in 32bit environments, I would not have created my library.

ad1mt

  • Sr. Member
  • ****
  • Posts: 327
    • Mark Taylor's Home Page
Re: New Big Integer library is finished
« Reply #101 on: October 18, 2024, 08:48:58 am »
Code: Pascal  [Select][+][-]
  1. {$warn 6058 off}
  2.  
I tried to do that, but I could not find the message numbers anywhere!  Where did you find them?

paweld

  • Hero Member
  • *****
  • Posts: 1268
Re: New Big Integer library is finished
« Reply #102 on: October 18, 2024, 10:41:44 am »
Found here: https://gitlab.com/freepascal.org/fpc/source/-/blob/main/compiler/msg/errore.msg
If you are using Lazarus IDE you can also turn it off in the project/package options: “Compiler Options” > ”Messages”
Best regards / Pozdrawiam
paweld

ad1mt

  • Sr. Member
  • ****
  • Posts: 327
    • Mark Taylor's Home Page
Re: New Big Integer library is finished
« Reply #103 on: October 18, 2024, 11:07:21 am »
Found here: https://gitlab.com/freepascal.org/fpc/source/-/blob/main/compiler/msg/errore.msg
Thanks.
I had to do it in the Lazarus IDE, but it would be nice to be able do it in the source.

MathMan

  • Sr. Member
  • ****
  • Posts: 405
Re: New Big Integer library is finished
« Reply #104 on: October 18, 2024, 11:18:26 am »
here's Pi
20,000 digits in about 1 second, code borrowed from https://www.ellipsa.eu/public/fnx/fnx.html and adapted to Multi_Int

Nice, but it does not reveal too much if you don't tell us the CPU & frequency too.

Beside that the following arc tan series evaluation might be faster - at least it has a substantially lower Lehmer value

44*arctan( 1/57 ) + 7*arctan( 1/239 ) - 12*arctan( 1/682 ) + 24*arctan( 1/12943 )

Maybe you want to give it a try.

Cheers,
MathMan

 

TinyPortal © 2005-2018