Motoko's BigInt implementation on Wasm

I have a question for the Motoko devs:

Does Motoko use GMP for BigInts currently? I ask because GMP is perhaps the fastest BigInt library out there, but it doesn’t port easily to Wasm (because Wasm doesn’t expose carry bits, for one). If the default integer type in Motoko is arbitrary precision, seems like Wasm needs some new features to make Int calculations run fast.

3 Likes

Hi @Nick,

I have implemented “compact BigNums” which means we can have near native speed arithmetic on Int (and Nat) types, as long the value(s) fit(s) into 30 bits (31 with sign). When the number is out of this range, we fall back to a heap-based representation in which the arithmetics is performed by LibTomMath. It is a pretty solid numerics library which has some nice features. We have decided not to use GMP for size and licensing reasons.
If you require native speed arithmetics (trapping or overwrapping) consider resorting to Nat32/64 (positive) and Int32/64 (signed) Word32/64 (wrapping, machine words).

3 Likes

Ah, I see, that sounds like a reasonable compromise. Thanks for the answer!