mozilla
Your Search Results

    Math.imul Redirect 1

    This is an experimental technology, part of the Harmony (ECMAScript 6) proposal.
    Because this technology's specification has not stabilized, check the compatibility table for usage in various browsers. Also note that the syntax and behavior of an experimental technology is subject to change in future version of browsers as the spec changes.

    Summary

    The Math.imul() function returns the result of the C-like 32-bit multiplication of the two parameters.

    Syntax

    Math.imul(a, b)

    Parameters

    a
    First number.
    b
    Second number.

    Description

    Math.imul allows for fast 32-bit integer multiplication with C-like semantics. This feature is useful for projects like Emscripten.  Because imul is a static method of Math, you always use it as Math.imul(), rather than as a method of a Math object you created.

    Examples

    Math.imul(2, 4)   // 8
    Math.imul(-1, 8)  //-8
    Math.imul(-2, -2) // 4
    Math.imul(0xffffffff, 5) //-5
    Math.imul(0xfffffffe, 5) //-10
    

    Polyfill

    This can be emulated with the following function:

    function imul(a, b) {
      var ah  = (a >>> 16) & 0xffff;
      var al = a & 0xffff;
      var bh  = (b >>> 16) & 0xffff;
      var bl = b & 0xffff;
      // the shift by 0 fixes the sign on the high part
      // the final |0 converts the unsigned value into a signed value
      return ((al * bl) + (((ah * bl + al * bh) << 16) >>> 0)|0);
    }
    

    Specifications

    Specification Status Comment
    ECMAScript Language Specification 6th Edition (ECMA-262) Draft Initial definition

    Browser compatibility

    Feature Firefox (Gecko) Chrome Internet Explorer Opera Safari
    Basic support 20 (20) 28 ? 16 WebKit bug 115143
    Feature Firefox Mobile (Gecko) Android Chrome for Android IE Mobile Opera Mobile Safari Mobile
    Basic support 20.0 (20) ? ? ? ? WebKit bug 115143

     

    Document Tags and Contributors

    Contributors to this page: Sheppy
    Last updated by: Sheppy,