Fedor Indutny (2014-07-02T10:37:15.000Z)
domenic at domenicdenicola.com (2014-07-06T18:06:41.948Z)
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][1] 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? [1]: https://github.com/indutny/bn.js/blob/95e0623aaa637a3a6a87f9a7b30aafed758aa4eb/lib/bn.js#L590-L599
domenic at domenicdenicola.com (2014-07-06T18:06:20.674Z)
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][0] 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? [0]: https://github.com/indutny/bn.js/blob/95e0623aaa637a3a6a87f9a7b30aafed758aa4eb/lib/bn.js#L590-L599
domenic at domenicdenicola.com (2014-07-06T18:06:05.207Z)
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][0] 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? [0]: https://github.com/indutny/bn.js/blob/95e0623aaa637a3a6a87f9a7b30aafed758aa4eb/lib/bn.js#L590-L599