tact icon indicating copy to clipboard operation
tact copied to clipboard

Port `stdlib.fc` and `mathlib.fc`'s asm functions to Tact's stdlib

Open anton-trunov opened this issue 1 year ago • 2 comments

This is going to be very easy after #768 is resolved.

It is partially addressed in these issues:

  • #496
  • #618

anton-trunov avatar Aug 31 '24 07:08 anton-trunov

We can't just port them to Tact stdlib and remove the .fc files, because functions defined there are used in code generation, e.g. src/generator/writers/writeStdlib.ts.

Gusarich avatar Sep 05 '24 07:09 Gusarich

That's right. I meant some additional functions that can be used by Tact users.

anton-trunov avatar Sep 05 '24 07:09 anton-trunov

@anton-trunov should I implement the sqrt function the same way as it works in mathlib.fc or take a different approach?

Gusarich avatar Oct 25 '24 05:10 Gusarich

@Gusarich I haven't checked how sqrt is implemented in mathlib.fc, what are the alternatives you see?

anton-trunov avatar Oct 25 '24 06:10 anton-trunov

@anton-trunov we can take this one: https://github.com/TonoxDeFi/open-contracts/blob/ebbb877e5ee6a9e506605c455c44b2b5223956f1/contracts/math/math.func#L26

or even implement a different algorithm. probably makes sense to benchmark these on different ranges of integers to see what is more gas efficient

Gusarich avatar Oct 25 '24 07:10 Gusarich

probably makes sense to benchmark these on different ranges of integers to see what is more gas efficient

agreed!

anton-trunov avatar Oct 25 '24 09:10 anton-trunov

@anton-trunov turns out both of these implementation use the same Newton-Raphson method, but the one from tonox openlib does it in constant time (hardcoded 7 iterations for any input) and also has worse initial approximation, so the mathlib implementation wins on all ranges of numbers in terms of gas, especially after a few minor optimizations.

I looked through some other algorithms for square root finding and seems that Newton-Raphson will be the best for TVM in general, so I suggest to proceed with that (from mathlib).

Gusarich avatar Nov 18 '24 03:11 Gusarich

@Gusarich Looks good, let's go with your proposal

anton-trunov avatar Nov 18 '24 08:11 anton-trunov