additional Math function
Yehuda Katz
wycats at gmail.com
Fri Nov 2 12:05:44 PDT 2012
Seems like a small surface-area with a large impact on compilers.
At first glance, looks good to me.
Curiosity: Does this overlap with Brendan's work on value objects (i.e.
will it become moot in the face of them)
-- Yehuda Katz
(ph) 718.877.1325
On Nov 2, 2012 12:49 PM, "David Herman" <dherman at mozilla.com> wrote:
> I'd like to add a Math.imul function for doing 32-bit integer
> multiplication. The use case is for compilers like Emscripten and Mandreel.
> They generate code to emulate machine operations that can be efficiently
> JIT compiled, but they don't currently have a good solution for doing
> integer multiplication. For other operations, like addition, they can use
> the composition of JS's floating-point addition and coercion to int32 via
> bitwise or:
>
> t = (a+b)|0;
>
> But you can't do the same trick with multiplication, because multiplying
> large numbers may lose low bits of precision when it overflows to double.
> So Emscripten has a compiler switch where you can do the fast-but-incorrect
> (a*b)|0 or you can do a slow-but-correct pure-JS multiply, which has to
> split the operation into two multiplications with manual carry. As it turns
> out, multiplication is the only major operation that doesn't have a clear
> implementation pattern with existing JS operators.
>
> The semantics is straightforward:
>
> Math.imul(a, b) === ToInt32((ToUint32(a) x ToUint32(b)) mod 2^32)
>
> In other words, convert the two arguments to 32-bit integers and "do what
> C does." The result is signed, simply as a convention -- signed and
> unsigned integers are isomorphic (ToUint32 and ToInt32 form the bijection
> between them). We could also consider adding Math.umul:
>
> Math.umul(a, b) === ToUint32((ToUint32(a) x ToUint32(b)) mod 2^32)
>
> but it's not strictly necessary; it's equivalent to:
>
> Math.imul(a, b)>>>0
>
> It might be nice to have Math.umul available out of the box, but for
> Emscripten and Mandreel, only one of the two is strictly necessary.
>
> At any rate, this function would be easy for JS engines to implement and
> optimize, and code generators could benefit from it immediately on engines
> that support it.
>
> I believe we could add this to the ES6 extended Math operations, because
> it's extremely simple and well-understood; there's no design necessary.
>
> Dave
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20121102/537e5e57/attachment.html>
More information about the es-discuss
mailing list