Recent

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

schuler

  • Sr. Member
  • ****
  • Posts: 337
Re: AI assisted translation of CORE-MATH to Free Pascal
« Reply #30 on: April 15, 2026, 06:00:35 pm »
STATUS UPDATE

Only 1 function now fails for the full harness test as per below logs.
On average, pas-core-math is ~2.4× faster than FPC builtins in Windows. In Linux, pas-core-math is 6-20x times faster than FPC builtins.

Code: Pascal  [Select][+][-]
  1. bpsa@Ubuntu24:~/app/pas-core-math/src/tests$ ./build.sh -dAVX2 -CfAVX2 -CpCOREI -OpCOREI
  2. bpsa@Ubuntu24:~/app/pas-core-math/src/tests$ LD_LIBRARY_PATH=/home/bpsa/app/pas-core-math/src/tests/.. /home/bpsa/app/pas-core-math/src/tests/../../bin/TestHarness32 --diag 10
  3. Compiled with AVX2.
  4. === TestHarness: comparing Pascal (pcr_*) vs C (cr_*) ===
  5.  
  6. acosf             tested=4294967296  mismatches=0  PASS
  7. acoshf            tested=4294967296  mismatches=0  PASS
  8. acospif           tested=4294967296  mismatches=0  PASS
  9. asinf             tested=4294967296  mismatches=0  PASS
  10. asinhf            tested=4294967296  mismatches=0  PASS
  11. asinpif           tested=4294967296  mismatches=0  PASS
  12. atanf             tested=4294967296  mismatches=0  PASS
  13. atanhf            tested=4294967296  mismatches=0  PASS
  14. atanpif           tested=4294967296  mismatches=0  PASS
  15. cbrtf             tested=4294967296  mismatches=0  PASS
  16. cosf              tested=4294967296  mismatches=0  PASS
  17. coshf             tested=4294967296  mismatches=0  PASS
  18. cospif            tested=4294967296  mismatches=0  PASS
  19. erff              tested=4294967296  mismatches=0  PASS
  20. erfcf             tested=4294967296  mismatches=0  PASS
  21. expf              tested=4294967296  mismatches=0  PASS
  22. exp10f            tested=4294967296  mismatches=0  PASS
  23. exp10m1f          tested=4294967296  mismatches=0  PASS
  24. exp2f             tested=4294967296  mismatches=0  PASS
  25. exp2m1f           tested=4294967296  mismatches=0  PASS
  26. expm1f            tested=4294967296  mismatches=0  PASS
  27. lgammaf           tested=4294967296  mismatches=0  PASS
  28. logf              tested=4294967296  mismatches=0  PASS
  29. log10f            tested=4294967296  mismatches=0  PASS
  30. log10p1f          tested=4294967296  mismatches=0  PASS
  31. log1pf            tested=4294967296  mismatches=0  PASS
  32. log2f             tested=4294967296  mismatches=0  PASS
  33. log2p1f           tested=4294967296  mismatches=0  PASS
  34. rsqrtf            tested=4294967296  mismatches=0  PASS
  35. sinf              tested=4294967296  mismatches=0  PASS
  36. sinhf             tested=4294967296  mismatches=0  PASS
  37. sinpif            tested=4294967296  mismatches=0  PASS
  38. tanf              tested=4294967296  mismatches=0  PASS
  39. tanhf             tested=4294967296  mismatches=0  PASS
  40. tanpif            tested=4294967296  mismatches=0  PASS
  41. tgammaf           tested=4294967296  mismatches=0  PASS
  42. atan2f            tested=  10000000  mismatches=0  PASS
  43. atan2pif          tested=  10000000  mismatches=0  PASS
  44. hypotf            tested=  10000000  mismatches=0  PASS
  45. powf              tested=  10000000  mismatches=0  PASS
  46.   [compoundf] x=$1464EDE1(1.15579717E-26) y=$69C66EBB(2.99862778E25)  C=$3FB50546(1.41422343)  P=$3F350546(0.707111716)
  47.   [compoundf] x=$1464EF8E(1.15583022E-26) y=$69C67069(2.99872693E25)  C=$3FB50643(1.41425359)  P=$3F350643(0.707126796)
  48.   [compoundf] x=$1464F13B(1.15586327E-26) y=$69C67217(2.99882608E25)  C=$3FB50741(1.41428387)  P=$3F350741(0.707141936)
  49.   [compoundf] x=$1464F2E8(1.15589631E-26) y=$69C673C5(2.99892523E25)  C=$3FB5083E(1.41431403)  P=$3F35083E(0.707157016)
  50.   [compoundf] x=$1464F495(1.15592936E-26) y=$69C67573(2.99902438E25)  C=$3FB5093C(1.41434431)  P=$3F35093C(0.707172155)
  51.   [compoundf] x=$1464F642(1.15596241E-26) y=$69C67721(2.99912353E25)  C=$3FB50A3A(1.41437459)  P=$3F350A3A(0.707187295)
  52.   [compoundf] x=$1464F7EF(1.15599546E-26) y=$69C678CF(2.99922268E25)  C=$3FB50B37(1.41440475)  P=$3F350B37(0.707202375)
  53.   [compoundf] x=$1464F99C(1.15602851E-26) y=$69C67A7D(2.99932184E25)  C=$3FB50C35(1.41443503)  P=$3F350C35(0.707217515)
  54.   [compoundf] x=$1464FB49(1.15606156E-26) y=$69C67C2B(2.99942099E25)  C=$3FB50D32(1.41446519)  P=$3F350D32(0.707232594)
  55.   [compoundf] x=$1464FCF6(1.15609461E-26) y=$69C67DD9(2.99952014E25)  C=$3FB50E30(1.41449547)  P=$3F350E30(0.707247734)
  56. compoundf         tested=  10000000  mismatches=314296  FAIL max_error=0.707247734069824219
  57. sincosf           tested=4294967296  mismatches=0  PASS
  58.  
  59. === TOTAL: 41 PASS, 1 FAIL ===
  60.  

Code: Pascal  [Select][+][-]
  1. bpsa@Ubuntu24:~/app/pas-core-math/src/tests$ LD_LIBRARY_PATH=/home/bpsa/app/pas-core-math/src/tests/.. /home/bpsa/app/pas-core-math/src/tests/../../bin/Benchmark32
  2. Compiled with AVX2.
  3. === Benchmark: 200000000 calls per function ===
  4.  
  5. acosf             C:  336.1 Mops/s  Pascal:  181.0 Mops/s  sink=MATCH
  6. acoshf            C:  126.8 Mops/s  Pascal:  165.2 Mops/s  sink=MISMATCH  FASTER! YAY!
  7. acospif           C:  220.5 Mops/s  Pascal:  230.4 Mops/s  sink=MISMATCH  TIE
  8. asinf             C:  353.4 Mops/s  Pascal:  435.7 Mops/s  sink=MATCH  FASTER! YAY!
  9. asinhf            C:  239.8 Mops/s  Pascal:  216.5 Mops/s  sink=MATCH
  10. asinpif           C:  237.2 Mops/s  Pascal:  192.3 Mops/s  sink=MISMATCH
  11. atanf             C:  321.5 Mops/s  Pascal:  322.6 Mops/s  sink=MATCH  TIE
  12. atanhf            C:  283.3 Mops/s  Pascal:  382.4 Mops/s  sink=MATCH  FASTER! YAY!
  13. atanpif           C:  380.2 Mops/s  Pascal:  426.4 Mops/s  sink=MATCH  FASTER! YAY!
  14. cbrtf             C:  223.2 Mops/s  Pascal:  173.5 Mops/s  sink=MATCH
  15. cosf              C:  219.5 Mops/s  Pascal:  119.0 Mops/s  sink=MATCH
  16. coshf             C:  351.5 Mops/s  Pascal:  390.6 Mops/s  sink=MATCH  FASTER! YAY!
  17. cospif            C:  337.3 Mops/s  Pascal:  347.8 Mops/s  sink=MATCH  TIE
  18. erff              C:  304.0 Mops/s  Pascal:  184.2 Mops/s  sink=MATCH
  19. erfcf             C:  335.0 Mops/s  Pascal:  364.3 Mops/s  sink=MATCH  FASTER! YAY!
  20. expf              C:  313.0 Mops/s  Pascal:  161.2 Mops/s  sink=MATCH
  21. exp10f            C:  292.0 Mops/s  Pascal:  289.0 Mops/s  sink=MATCH  TIE
  22. exp10m1f          C:  295.0 Mops/s  Pascal:  234.2 Mops/s  sink=MATCH
  23. exp2f             C:  339.6 Mops/s  Pascal:  351.5 Mops/s  sink=MATCH  TIE
  24. exp2m1f           C:  249.1 Mops/s  Pascal:  386.8 Mops/s  sink=MATCH  FASTER! YAY!
  25. expm1f            C:  319.5 Mops/s  Pascal:  309.1 Mops/s  sink=MATCH  TIE
  26. lgammaf           C:  125.9 Mops/s  Pascal:   34.1 Mops/s  sink=MATCH
  27. logf              C:  371.1 Mops/s  Pascal:  304.9 Mops/s  sink=MATCH
  28. log10f            C:  232.0 Mops/s  Pascal:  316.5 Mops/s  sink=MATCH  FASTER! YAY!
  29. log10p1f          C:  192.5 Mops/s  Pascal:  135.3 Mops/s  sink=MATCH
  30. log1pf            C:  284.9 Mops/s  Pascal:  244.2 Mops/s  sink=MATCH
  31. log2f             C:  307.2 Mops/s  Pascal:  285.7 Mops/s  sink=MATCH
  32. log2p1f           C:  236.7 Mops/s  Pascal:  171.4 Mops/s  sink=MATCH
  33. rsqrtf            C:  191.9 Mops/s  Pascal:  413.2 Mops/s  sink=MISMATCH  FASTER! YAY!
  34. sinf              C:  195.1 Mops/s  Pascal:  114.0 Mops/s  sink=MATCH
  35. sinhf             C:  320.0 Mops/s  Pascal:  361.0 Mops/s  sink=MATCH  FASTER! YAY!
  36. sinpif            C:  364.3 Mops/s  Pascal:  371.1 Mops/s  sink=MATCH  TIE
  37. tanf              C:  193.8 Mops/s  Pascal:  118.4 Mops/s  sink=MATCH
  38. tanhf             C:  304.4 Mops/s  Pascal:  289.9 Mops/s  sink=MATCH
  39. tanpif            C:  201.4 Mops/s  Pascal:  218.6 Mops/s  sink=MATCH  FASTER! YAY!
  40. tgammaf           C:  273.6 Mops/s  Pascal:  125.2 Mops/s  sink=MATCH
  41. atan2f            C:  256.7 Mops/s  Pascal:   89.2 Mops/s  sink=MATCH
  42. atan2pif          C:    6.9 Mops/s  Pascal:    2.0 Mops/s  sink=MATCH
  43. hypotf            C:  112.9 Mops/s  Pascal:  145.3 Mops/s  sink=MATCH  FASTER! YAY!
  44. powf              C:   73.4 Mops/s  Pascal:   78.1 Mops/s  sink=MATCH  FASTER! YAY!
  45. compoundf         C:   86.8 Mops/s  Pascal:  123.2 Mops/s  sink=MISMATCH  FASTER! YAY!
  46. sincosf           C:  211.2 Mops/s  Pascal:  109.6 Mops/s  sink=MATCH
  47.  
  48. Pascal won: 14  |  C won: 21  |  Ties (<5%): 7
  49. On average, Pascal is 1.08x slower than C (arithmetic mean over 42 functions)
  50.  

Code: Pascal  [Select][+][-]
  1. bpsa@Ubuntu24:~/app/pas-core-math/src/tests$ LD_LIBRARY_PATH=/home/bpsa/app/pas-core-math/src/tests/.. /home/bpsa/app/pas-core-math/src/tests/../../bin/BenchmarkFPC32
  2. Compiled with AVX2.
  3. === FPC vs Pascal CORE-MATH (PCM) Benchmark: 50000000 calls per function ===
  4. sinf              FPC:   46.3 Mops/s  PCM:  130.9 Mops/s  FASTER! YAY!
  5. cosf              FPC:   43.1 Mops/s  PCM:  134.8 Mops/s  FASTER! YAY!
  6. tanf              FPC:   17.2 Mops/s  PCM:  125.6 Mops/s  FASTER! YAY!
  7. asinf             FPC:    8.4 Mops/s  PCM:  431.0 Mops/s  FASTER! YAY!
  8. acosf             FPC:    8.2 Mops/s  PCM:  211.0 Mops/s  FASTER! YAY!
  9. atanf             FPC:   28.1 Mops/s  PCM:  312.5 Mops/s  FASTER! YAY!
  10. sinhf             FPC:    5.4 Mops/s  PCM:  335.6 Mops/s  FASTER! YAY!
  11. coshf             FPC:    5.5 Mops/s  PCM:  367.6 Mops/s  FASTER! YAY!
  12. tanhf             FPC:   31.6 Mops/s  PCM:  282.5 Mops/s  FASTER! YAY!
  13. asinhf            FPC:   27.5 Mops/s  PCM:  238.1 Mops/s  FASTER! YAY!
  14. acoshf            FPC:    6.3 Mops/s  PCM:  188.0 Mops/s  FASTER! YAY!
  15. atanhf            FPC:    7.0 Mops/s  PCM:  413.2 Mops/s  FASTER! YAY!
  16. expf              FPC:   17.0 Mops/s  PCM:  174.8 Mops/s  FASTER! YAY!
  17. logf              FPC:   46.4 Mops/s  PCM:  331.1 Mops/s  FASTER! YAY!
  18. log2f             FPC:   44.2 Mops/s  PCM:  375.9 Mops/s  FASTER! YAY!
  19. log10f            FPC:   10.6 Mops/s  PCM:  357.1 Mops/s  FASTER! YAY!
  20. atan2f            FPC:   11.1 Mops/s  PCM:   51.2 Mops/s  FASTER! YAY!
  21. hypotf            FPC:   49.7 Mops/s  PCM:  170.1 Mops/s  FASTER! YAY!
  22. powf              FPC:    1.0 Mops/s  PCM:   42.1 Mops/s  FASTER! YAY!
  23. sincosf           FPC:    7.4 Mops/s  PCM:   37.3 Mops/s  FASTER! YAY!
  24.  
  25. PCM won: 20  |  FPC won: 0  |  Ties (<5%): 0
  26. On average, PCM is 22.55x faster than FPC (arithmetic mean over 20 functions)
  27.  

@nanobit and @thaddy, I'll FUP your messages.

MathMan

  • Hero Member
  • *****
  • Posts: 504
Re: AI assisted translation of CORE-MATH to Free Pascal
« Reply #31 on: April 15, 2026, 07:10:10 pm »
@schuler,

Great news - many thanks for your effort! I hope that this excercise provided you with the learning experience you hoped to get?

What is the background of test harness passing and then in the benchmark there still is a sink mismatch announced?

Beside that - I went through 'compoundf' in detail. Didn't find anything that directly sprang to mind like the issue in 'powf'. However I'm still not at easy with the use of constants (mentioned as point 8 in my first analysis) - especially after I saw the fix for 'acosf'. My gut feeling is that stronger typing of constants, or replacing decimal float with exact UIntxx, will solve the issue with 'compoundf' too.
« Last Edit: April 16, 2026, 03:25:33 am by MathMan »

schuler

  • Sr. Member
  • ****
  • Posts: 337
Re: AI assisted translation of CORE-MATH to Free Pascal
« Reply #32 on: April 15, 2026, 10:42:11 pm »
 :) compoundf has just been fixed! YAY!  :)

MathMan

  • Hero Member
  • *****
  • Posts: 504
Re: AI assisted translation of CORE-MATH to Free Pascal
« Reply #33 on: April 15, 2026, 10:47:11 pm »
:) compoundf has just been fixed! YAY!  :)

Yes, congrats!

And I admit my gutt feeling was wrong - it was an inline issue! This might have already been fixed in FPC 3.3.1 - I remember that somebody mentioned fixes to inline there.

schuler

  • Sr. Member
  • ****
  • Posts: 337

nanobit

  • Full Member
  • ***
  • Posts: 189
Re: AI assisted translation of CORE-MATH to Free Pascal
« Reply #35 on: April 16, 2026, 04:50:56 pm »
@nanobit, just pushed your change:
https://github.com/joaopauloschuler/pas-core-math/commit/634f027cc2c6ebf33114955274d4db6a779389fd

No, your version (not from me) will not suffice. My version is:
Code: Pascal  [Select][+][-]
  1. var
  2.   MulLo, Temp1, Temp2: UInt64;
  3. begin
  4.   MulLo := uint64(uint32(a)) * uint64(uint32(b));
  5.   Temp1 := (a shr 32) * uint64(uint32(b)) + (MulLo shr 32);
  6.   Temp2 := uint64(uint32(a)) * (b shr 32) + uint64(uint32(Temp1));
  7.   Result.lo := (Temp2 shl 32) or (MulLo and $FFFFFFFF);
  8.   Result.hi := (a shr 32) * (b shr 32) + (Temp1 shr 32) + (Temp2 shr 32);
  9. end;
« Last Edit: April 16, 2026, 05:09:18 pm by nanobit »

LemonParty

  • Sr. Member
  • ****
  • Posts: 466
Re: AI assisted translation of CORE-MATH to Free Pascal
« Reply #36 on: April 16, 2026, 05:09:10 pm »
Hi.

I don't quite undertand what this thread testing. Can anybody give short description about what is going on?

Thank you.
Lazarus v. 4.99. FPC v. 3.3.1. Windows 11

schuler

  • Sr. Member
  • ****
  • Posts: 337
Re: AI assisted translation of CORE-MATH to Free Pascal
« Reply #37 on: April 16, 2026, 05:20:24 pm »
@nanobit, https://github.com/joaopauloschuler/pas-core-math/commit/4b2f475e2ad344c64a850bcf6dc9b085f699e598

Next time, a PR will be easier.

@lemonparty, this is a test about https://github.com/joaopauloschuler/beyond-python-smolagents at coding https://github.com/joaopauloschuler/pas-core-math .

Most of the coding was done by BPSA: https://github.com/joaopauloschuler/pas-core-math/pull/1 . Myself, GitHub Copilot and Claude Code did bugfixing and specs.

MathMan

  • Hero Member
  • *****
  • Posts: 504
Re: AI assisted translation of CORE-MATH to Free Pascal
« Reply #38 on: April 17, 2026, 12:55:30 am »
@schuler,

I think we still need a fallback implementation for pcr_fmaf, pcr_fma.

Based on this https://hal.science/hal-04575249/document I did an implementation for pcr_fma - the pcr_fmaf can be derived by replaceing Double by Single throughout. Please find attached.

Forget the simple replacement approach I mentioned above. At least the argument split function used has to be adapted for Single. I'll continue working on a solution for Single.
« Last Edit: April 17, 2026, 10:36:24 am by MathMan »

schuler

  • Sr. Member
  • ****
  • Posts: 337
Re: AI assisted translation of CORE-MATH to Free Pascal
« Reply #39 on: April 17, 2026, 03:27:56 pm »
@mathman, thank you. I will FUP.


schuler

  • Sr. Member
  • ****
  • Posts: 337
Re: AI assisted translation of CORE-MATH to Free Pascal
« Reply #41 on: April 18, 2026, 03:07:59 am »
@Thaddy,
Quote
I did my own interface to the C lib.

I will need a dev environment with windows to be able to test...


schuler

  • Sr. Member
  • ****
  • Posts: 337
Re: AI assisted translation of CORE-MATH to Free Pascal
« Reply #42 on: April 26, 2026, 02:23:39 am »

Thaddy

  • Hero Member
  • *****
  • Posts: 19156
  • Glad to be alive.
Re: AI assisted translation of CORE-MATH to Free Pascal
« Reply #43 on: April 26, 2026, 08:35:12 am »
Pulled.
objects are fine constructs. You can even initialize them with constructors.

schuler

  • Sr. Member
  • ****
  • Posts: 337
Re: AI assisted translation of CORE-MATH to Free Pascal
« Reply #44 on: April 26, 2026, 12:18:20 pm »
@Thaddy,
Thank you.

Later today, I will have an update with plenty of fixes.

 

TinyPortal © 2005-2018