mozilla

Revision 349511 of Math.imul()

  • Revision slug: JavaScript/Reference/Global_Objects/Math/imul
  • Revision title: Math.imul
  • Revision id: 349511
  • Created:
  • Creator: berkerpeksag
  • Is current revision? No
  • Comment Use Mozilla's JavaScript style in polyfill.

Revision Content

{{ gecko_minversion_header("20.0") }}{{ Non-standard_header() }}

Summary

This operations returns the result of the C-like 32-bit multiplication of the two parameters.

Method of Math
Implemented in JavaScript 1.8.5+
ECMAScript Edition None, but proposed.

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.

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 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
    return (al * bl) + (((ah * bl + al * bh) << 16) >>> 0);
}

Browser compatibility

{{ CompatibilityTable() }}

Feature Firefox (Gecko) Chrome Internet Explorer Opera Safari
Basic support {{ CompatGeckoDesktop("20") }} {{ CompatUnknown() }} {{ CompatUnknown() }} {{ CompatUnknown() }} {{ CompatUnknown() }}
Feature Firefox Mobile (Gecko) Android IE Mobile Opera Mobile Safari Mobile
Basic support {{ CompatGeckoMobile("20") }} {{ CompatUnknown() }} {{ CompatUnknown() }} {{ CompatUnknown() }} {{ CompatUnknown() }}

 

Revision Source

<p>{{ gecko_minversion_header("20.0") }}{{ Non-standard_header() }}</p>
<h2 id="Summary" name="Summary">Summary</h2>
<p>This operations returns the result of the C-like 32-bit multiplication of the two parameters.</p>
<table class="standard-table">
  <thead>
    <tr>
      <th class="header" colspan="2">Method of <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Math"><code>Math</code></a></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Implemented in</td>
      <td>JavaScript 1.8.5+</td>
    </tr>
    <tr>
      <td>ECMAScript Edition</td>
      <td>None, but <a href="https://mail.mozilla.org/pipermail/es-discuss/2012-November/026126.html" title="https://mail.mozilla.org/pipermail/es-discuss/2012-November/026126.html">proposed</a>.</td>
    </tr>
  </tbody>
</table>
<h2 id="Syntax">Syntax</h2>
<pre class="syntaxbox">
<code>Math.imul(a, b)</code></pre>
<h3 id="Parameters" name="Parameters">Parameters</h3>
<dl>
  <dt>
    <code>a</code></dt>
  <dd>
    First number.</dd>
  <dt>
    <code>b</code></dt>
  <dd>
    Second number.</dd>
</dl>
<h2 id="Description">Description</h2>
<p><code>Math.imul</code> allows for fast 32-bit integer multiplication with C-like semantics. This feature is useful for projects like <a href="http://en.wikipedia.org/wiki/Emscripten" title="http://en.wikipedia.org/wiki/Emscripten">Emscripten</a>.</p>
<h2 id="Examples">Examples</h2>
<pre class="brush: js">
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
</pre>
<h2 id="Polyfill">Polyfill</h2>
<p>This can be emulated with following function:</p>
<pre class="brush: js">
function imul(a, b) {
    var ah  = (a &gt;&gt;&gt; 16) &amp; 0xffff;
    var al = a &amp; 0xffff;
    var bh  = (b &gt;&gt;&gt; 16) &amp; 0xffff;
    var bl = b &amp; 0xffff;
    // the shift by 0 fixes the sign on the high part
    return (al * bl) + (((ah * bl + al * bh) &lt;&lt; 16) &gt;&gt;&gt; 0);
}
</pre>
<h2 id="Browser_compatibility">Browser compatibility</h2>
<p>{{ CompatibilityTable() }}</p>
<div id="compat-desktop">
  <table class="compat-table">
    <tbody>
      <tr>
        <th>Feature</th>
        <th>Firefox (Gecko)</th>
        <th>Chrome</th>
        <th>Internet Explorer</th>
        <th>Opera</th>
        <th>Safari</th>
      </tr>
      <tr>
        <td>Basic support</td>
        <td>{{ CompatGeckoDesktop("20") }}</td>
        <td>{{ CompatUnknown() }}</td>
        <td>{{ CompatUnknown() }}</td>
        <td>{{ CompatUnknown() }}</td>
        <td>{{ CompatUnknown() }}</td>
      </tr>
    </tbody>
  </table>
</div>
<div id="compat-mobile">
  <table class="compat-table">
    <tbody>
      <tr>
        <th>Feature</th>
        <th>Firefox Mobile (Gecko)</th>
        <th>Android</th>
        <th>IE Mobile</th>
        <th>Opera Mobile</th>
        <th>Safari Mobile</th>
      </tr>
      <tr>
        <td>Basic support</td>
        <td>{{ CompatGeckoMobile("20") }}</td>
        <td>{{ CompatUnknown() }}</td>
        <td>{{ CompatUnknown() }}</td>
        <td>{{ CompatUnknown() }}</td>
        <td>{{ CompatUnknown() }}</td>
      </tr>
    </tbody>
  </table>
</div>
<p>&nbsp;</p>
Revert to this revision