Recent

Author Topic: AI assisted translation of CORE-MATH to Free Pascal  (Read 5133 times)

schuler

  • Sr. Member
  • ****
  • Posts: 349
Re: AI assisted translation of CORE-MATH to Free Pascal
« Reply #45 on: April 26, 2026, 05:50:08 pm »
Just pushed major performance improvements for both 32 and 64 bits:
https://github.com/joaopauloschuler/pas-core-math/pull/10

Thaddy

  • Hero Member
  • *****
  • Posts: 19268
  • Glad to be alive.
Re: AI assisted translation of CORE-MATH to Free Pascal
« Reply #46 on: April 30, 2026, 07:58:51 am »
Today's atan2 (64) on Windows11 (AVX2) seems ridiculously slow. That can't be correct.

Probably da77097 ??
Results on a low-end Ryzen 7020 laptop:
Code: Text  [Select][+][-]
  1. Compiled with AVX2.
  2. === FPC vs Pascal CORE-MATH (PCM) Benchmark64: 50000000 calls per function ===
  3.  
  4. sin               FPC:   10,0 Mops/s  PCM:   18,4 Mops/s  FASTER! YAY!
  5. cos               FPC:   10,1 Mops/s  PCM:   19,3 Mops/s  FASTER! YAY!
  6. tan               FPC:   24,5 Mops/s  PCM:   17,8 Mops/s
  7. asin              FPC:  103,1 Mops/s  PCM:  195,3 Mops/s  FASTER! YAY!
  8. acos              FPC:  100,0 Mops/s  PCM:  211,9 Mops/s  FASTER! YAY!
  9. atan              FPC:  284,1 Mops/s  PCM:  260,4 Mops/s
  10. sinh              FPC:  133,0 Mops/s  PCM:  174,8 Mops/s  FASTER! YAY!
  11. cosh              FPC:  108,0 Mops/s  PCM:  118,8 Mops/s  FASTER! YAY!
  12. tanh              FPC:  112,6 Mops/s  PCM:  102,9 Mops/s
  13. asinh             FPC:   48,7 Mops/s  PCM:  105,0 Mops/s  FASTER! YAY!
  14. acosh             FPC:  116,8 Mops/s  PCM:  151,1 Mops/s  FASTER! YAY!
  15. atanh             FPC:   45,1 Mops/s  PCM:  166,7 Mops/s  FASTER! YAY!
  16. exp               FPC:  147,5 Mops/s  PCM:  158,7 Mops/s  FASTER! YAY!
  17. log               FPC:   76,5 Mops/s  PCM:   76,8 Mops/s  TIE
  18. log2              FPC:   74,5 Mops/s  PCM:  133,0 Mops/s  FASTER! YAY!
  19. log10             FPC:   67,7 Mops/s  PCM:   72,3 Mops/s  FASTER! YAY!
  20. atan2             FPC:  122,5 Mops/s  PCM:    0,4 Mops/s  // this can't be right
  21. hypot             FPC:   77,5 Mops/s  PCM:  118,8 Mops/s  FASTER! YAY!
  22. pow               FPC:   45,7 Mops/s  PCM:   12,1 Mops/s
  23. sincos            FPC:   27,1 Mops/s  PCM:   14,0 Mops/s
  24.  
  25. PCM won: 13  |  FPC won: 6  |  Ties (<5%): 1
  26. On average, PCM is 1,36x faster than FPC (arithmetic mean over 20 functions)
  27. GlobalSink = 5953110881124657846 (prevents dead-code elimination)

Ran the executable 5 times.
« Last Edit: April 30, 2026, 08:49:13 am by Thaddy »
objects are fine constructs. You can even initialize them with constructors.

schuler

  • Sr. Member
  • ****
  • Posts: 349
Re: AI assisted translation of CORE-MATH to Free Pascal
« Reply #47 on: April 30, 2026, 09:51:41 pm »
@Thaddy,
Many thanks for your testing!

I'll FUP.

Thaddy

  • Hero Member
  • *****
  • Posts: 19268
  • Glad to be alive.
Re: AI assisted translation of CORE-MATH to Free Pascal
« Reply #48 on: May 01, 2026, 11:00:22 am »
Now it crashes in atan2.... I will wait a while.
objects are fine constructs. You can even initialize them with constructors.

schuler

  • Sr. Member
  • ****
  • Posts: 349
Re: AI assisted translation of CORE-MATH to Free Pascal
« Reply #49 on: May 01, 2026, 11:46:01 am »
In my machine, it is faster than C although slower than FPC:
Code: Pascal  [Select][+][-]
  1. atan2             C:    1.4 Mops/s  Pascal:    1.7 Mops/s  sink=MATCH  FASTER! YAY!

I pushed code yesterday. Do you know in what line it crashes please? Also, in what commit are you?

Thaddy

  • Hero Member
  • *****
  • Posts: 19268
  • Glad to be alive.
Re: AI assisted translation of CORE-MATH to Free Pascal
« Reply #50 on: May 01, 2026, 12:29:57 pm »
Code: [Select]
This mornings HEAD.
Compiled with -glh
[code=text]EAccessViolation: Access violation
  $000000010001EB93  MULTINT
  $000000010004FA1E  ATAN2ACCURATE,  line 210 of inc_64/atan2_port_64.inc
  $0000000100059AA1  PCR_ATAN2,  line 309 of inc_64/atan2_port_64.inc
  $0000000100002882  PCM_ATAN2,  line 237 of tests/Benchmarkfpc64.pas
  $0000000100001DED  BENCHBIVAR,  line 125 of tests/Benchmarkfpc64.pas
  $0000000100004E67  main,  line 274 of tests/Benchmarkfpc64.pas
  $0000000100005F96
  $0000000100014820
  $0000000100001830
  $00007FFC4D4CE957
  $00007FFC4F04427C
As long as it is slower, I keep it out of the test.

Tests without atan2:
Code: Text  [Select][+][-]
  1. benchmarkfpc64
  2. Compiled with AVX2.
  3. === FPC vs Pascal CORE-MATH (PCM) Benchmark64: 50000000 calls per function ===
  4.  
  5. sin               FPC:   10,8 Mops/s  PCM:   14,1 Mops/s  FASTER! YAY!
  6. cos               FPC:   10,4 Mops/s  PCM:   14,9 Mops/s  FASTER! YAY!
  7. tan               FPC:   25,1 Mops/s  PCM:   11,9 Mops/s
  8. asin              FPC:  104,8 Mops/s  PCM:  166,7 Mops/s  FASTER! YAY!
  9. acos              FPC:  105,0 Mops/s  PCM:  173,6 Mops/s  FASTER! YAY!
  10. atan              FPC:  211,9 Mops/s  PCM:  174,2 Mops/s
  11. sinh              FPC:  116,8 Mops/s  PCM:  196,9 Mops/s  FASTER! YAY!
  12. cosh              FPC:  108,7 Mops/s  PCM:  116,6 Mops/s  FASTER! YAY!
  13. tanh              FPC:  105,5 Mops/s  PCM:  104,6 Mops/s  TIE
  14. asinh             FPC:   43,8 Mops/s  PCM:   56,2 Mops/s  FASTER! YAY!
  15. acosh             FPC:  104,6 Mops/s  PCM:  112,4 Mops/s  FASTER! YAY!
  16. atanh             FPC:   47,8 Mops/s  PCM:  108,5 Mops/s  FASTER! YAY!
  17. exp               FPC:  137,4 Mops/s  PCM:  164,5 Mops/s  FASTER! YAY!
  18. log               FPC:   67,3 Mops/s  PCM:   49,9 Mops/s
  19. log2              FPC:   63,2 Mops/s  PCM:   70,1 Mops/s  FASTER! YAY!
  20. log10             FPC:   64,4 Mops/s  PCM:   40,9 Mops/s
  21. hypot             FPC:   71,3 Mops/s  PCM:   75,2 Mops/s  FASTER! YAY!
  22. pow               FPC:   43,1 Mops/s  PCM:    9,1 Mops/s
  23. sincos            FPC:   27,9 Mops/s  PCM:   12,5 Mops/s
  24.  
  25. PCM won: 12  |  FPC won: 6  |  Ties (<5%): 1
  26. On average, PCM is 1,11x faster than FPC (arithmetic mean over 19 functions)
  27. GlobalSink = 5987329527028110739 (prevents dead-code elimination)
  28. Heap dump by heaptrc unit of "C:\joao\src\tests\BenchmarkFPC64.exe"
  29. 736 memory blocks allocated : 41549
  30. 736 memory blocks freed : 41549
  31. 0 unfreed memory blocks : 0

One more remark:
FPC-LLVM needs testing too, since on linux it is already 5-15% faster than the default fpc.
« Last Edit: May 01, 2026, 01:13:16 pm by Thaddy »
objects are fine constructs. You can even initialize them with constructors.

schuler

  • Sr. Member
  • ****
  • Posts: 349
Re: AI assisted translation of CORE-MATH to Free Pascal
« Reply #51 on: May 01, 2026, 04:17:33 pm »
@Thaddy,
Thank you so much. I'll FUP.

schuler

  • Sr. Member
  • ****
  • Posts: 349
Re: AI assisted translation of CORE-MATH to Free Pascal
« Reply #52 on: May 02, 2026, 12:26:16 pm »
@Thaddy,
The crash was Windows related. There is a fix ready to be tested.

Thaddy

  • Hero Member
  • *****
  • Posts: 19268
  • Glad to be alive.
Re: AI assisted translation of CORE-MATH to Free Pascal
« Reply #53 on: May 02, 2026, 03:20:21 pm »
Will do.Done. Compilation fixed.
Atan2 is now 5 times faster than the original test, but still really slow compared to FPC:
Code: Text  [Select][+][-]
  1. Compiled with AVX2.
  2. === FPC vs Pascal CORE-MATH (PCM) Benchmark64: 50000000 calls per function ===
  3.  
  4. sin               FPC:   10,9 Mops/s  PCM:   14,1 Mops/s  FASTER! YAY!
  5. cos               FPC:   10,8 Mops/s  PCM:   15,0 Mops/s  FASTER! YAY!
  6. tan               FPC:   25,1 Mops/s  PCM:   11,6 Mops/s
  7. asin              FPC:  105,9 Mops/s  PCM:  166,1 Mops/s  FASTER! YAY!
  8. acos              FPC:  105,0 Mops/s  PCM:  175,4 Mops/s  FASTER! YAY!
  9. atan              FPC:  209,2 Mops/s  PCM:  175,4 Mops/s
  10. sinh              FPC:  116,3 Mops/s  PCM:  196,9 Mops/s  FASTER! YAY!
  11. cosh              FPC:  105,5 Mops/s  PCM:  116,6 Mops/s  FASTER! YAY!
  12. tanh              FPC:  106,2 Mops/s  PCM:  104,2 Mops/s  TIE
  13. asinh             FPC:   44,1 Mops/s  PCM:   55,4 Mops/s  FASTER! YAY!
  14. acosh             FPC:  105,0 Mops/s  PCM:  111,4 Mops/s  FASTER! YAY!
  15. atanh             FPC:   48,3 Mops/s  PCM:  105,0 Mops/s  FASTER! YAY!
  16. exp               FPC:  137,4 Mops/s  PCM:  162,3 Mops/s  FASTER! YAY!
  17. log               FPC:   66,2 Mops/s  PCM:   49,8 Mops/s
  18. log2              FPC:   64,6 Mops/s  PCM:   70,0 Mops/s  FASTER! YAY!
  19. log10             FPC:   64,3 Mops/s  PCM:   40,4 Mops/s
  20. atan2             FPC:  100,6 Mops/s  PCM:    1,6 Mops/s
  21. hypot             FPC:   71,6 Mops/s  PCM:   76,0 Mops/s  FASTER! YAY!
  22. pow               FPC:   43,1 Mops/s  PCM:    9,0 Mops/s
  23. sincos            FPC:   27,8 Mops/s  PCM:   12,4 Mops/s
  24.  
  25. PCM won: 12  |  FPC won: 7  |  Ties (<5%): 1
  26. On average, PCM is 1,04x faster than FPC (arithmetic mean over 20 functions)
  27. GlobalSink = 5987329527028110739 (prevents dead-code elimination)

Atan2 and Pow cause a considerable gain drop, so the average speed gain goes down quite a bit more than warranted.
« Last Edit: May 02, 2026, 03:52:48 pm by Thaddy »
objects are fine constructs. You can even initialize them with constructors.

schuler

  • Sr. Member
  • ****
  • Posts: 349
Re: AI assisted translation of CORE-MATH to Free Pascal
« Reply #54 on: May 02, 2026, 05:33:38 pm »
My recommendation will be: given that the pascal atan2 core-math version is already faster than its C version, if you need speed, do not use atan2 from core-math. atan2 from core-math is slow.

Thaddy

  • Hero Member
  • *****
  • Posts: 19268
  • Glad to be alive.
Re: AI assisted translation of CORE-MATH to Free Pascal
« Reply #55 on: May 02, 2026, 05:54:33 pm »
I know. Reason for testing is, of course, why?  ;D
objects are fine constructs. You can even initialize them with constructors.

schuler

  • Sr. Member
  • ****
  • Posts: 349
Re: AI assisted translation of CORE-MATH to Free Pascal
« Reply #56 on: May 04, 2026, 04:49:53 pm »
@Thaddy,
You are correct. Thanks to the testing, we now know a lot more than few days ago.

bytebites

  • Hero Member
  • *****
  • Posts: 787
Re: AI assisted translation of CORE-MATH to Free Pascal
« Reply #57 on: May 04, 2026, 06:16:23 pm »
Could there be more precise diffence than faster yay, which is faster and how much?

Thaddy

  • Hero Member
  • *****
  • Posts: 19268
  • Glad to be alive.
Re: AI assisted translation of CORE-MATH to Free Pascal
« Reply #58 on: May 04, 2026, 06:55:54 pm »
You did not read the docs, nor the test results?
A draw is +/- 5% so slower or faster depends on that.
The results from the tests are displayed. you can divide and multiply by 100.
objects are fine constructs. You can even initialize them with constructors.

schuler

  • Sr. Member
  • ****
  • Posts: 349
Re: AI assisted translation of CORE-MATH to Free Pascal
« Reply #59 on: May 04, 2026, 10:25:53 pm »
Food for thought: Only seven binary64 functions are integrated into the GNU libc up from release 2.43.
From: https://core-math.gitlabpages.inria.fr/
Regarding:
Quote
seven binary64 functions are integrated into the GNU libc up from release 2.43 (acosh, asinh, atanh, erf, erfc, lgamma, tgamma), and the following binary32 functions are integrated into the GNU libc up from release 2.42: acosf, acoshf, asinf, asinhf, atanf, atanhf, cbrtf, coshf, erff, erfcf, expm1f, lgammaf, log10f, log1pf, sinhf, tanf, tanhf, tgammaf, acospif, asinpif, atanpif, cospif, exp10m1f, exp2m1f, log10p1f, log2p1f, sinpif, tanpif, atan2f, atan2pif.

Thought: We can just use the functions that are in our interest and leave the other functions behind.

 

TinyPortal © 2005-2018