Math.mulx

# Fedor Indutny (11 years ago)

While working on bn.js, I have found one bottleneck in integer multiplication which could be solved with introduction of a new Math functions.

In multiply operation the most expensive part is obviously an integer multiplication. EcmaScript could operate with 51bit numbers without the loose of precision, but the whole thing requires compilers to translate SMIs (small integers) into a floating point values and operate on them.

Introduction of Math.mulxHigh and Math.mulxLow with a help of compiler could make this limitation go away. Namely, a compiler could coalesce these two calls into a one mulx instruction, making it possible to implement big numbers with a 32 bit limb.

The code sample is following:

var hi = Math.mulxHigh(a, b);  // High 32 bits of the multiplication result
var lo = Math.mulxLow(a, b);  // Low 32 bits of the multiplication result

And this could be translated to a just:

movx rax, rbx, rcx

on IA32 platform. On the platforms that do not have a single instruction for this operation, the resulting code should still be faster, because it could avoid integer -> floating point -> integer conversion.

I'm open to any suggestions. What do you think?

# Fedor Indutny (11 years ago)

Actually, I just discovered Vyacheslav's post on this list.

Looks like my proposal has not much to offer, comparing to his.

Thanks anyway!