Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Math.imul()

これは Harmony(ECMAScript 6) 提案の一部であり、実験段階の技術です。
この技術の仕様は安定していません。ブラウザ互換性の一覧表を確認してください。またこれらの構文や動作は、仕様変更などにより、新しいバージョンのブラウザでは変更される可能性があるという点に注意してください。

概要

このメソッドは 2 つの引数をとり、C 言語の様な 32 ビット乗算の結果を返します。

構文

Math.imul(a, b)

引数

a
最初の数字
b
2 番目の数字

説明

Math.imul は、 C 言語の様なセマンティクスを持つ、高速な 32 ビット整数乗算を可能にします。この機能は Emscripten のようなプロジェクトに有用です。

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

次の関数を用いる事により、imul をサポートしていない環境でもこれをエミュレートする事が出来ます。

// Math.imul の代替関数
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);
}

ブラウザ実装状況

機能 Firefox (Gecko) Chrome Internet Explorer Opera Safari
基本サポート 20 (20) 28 ? ? WebKit bug 115143
機能 Firefox Mobile (Gecko) Android Chrome for Android IE Mobile Opera Mobile Safari Mobile
基本サポート 20.0 (20) ? ? ? ? WebKit bug 115143

ドキュメントのタグと貢献者

タグ: 
 このページの貢献者: teoli, ethertank
 最終更新者: teoli,