Skip to content
This repository has been archived by the owner on Jun 5, 2022. It is now read-only.

Commit

Permalink
Math.imul
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewleon committed Nov 20, 2017
1 parent 9ece563 commit 4deba8e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/Math.js
Expand Up @@ -24,6 +24,28 @@ exports.exp = Math.exp;

exports.floor = Math.floor;

function nativeImul(a) {
return function (b) {
return Math.imul(a, b);
};
}

// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/imul
function emulatedImul(a) {
/*jshint bitwise: false*/
return function (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;
};
}

exports.imul = Math.imul ? nativeImul : emulatedImul;

exports.trunc = Math.trunc || function (n) {
return n < 0 ? Math.ceil(n) : Math.floor(n);
};
Expand Down
3 changes: 3 additions & 0 deletions src/Math.purs
Expand Up @@ -36,6 +36,9 @@ foreign import exp :: Number -> Number
-- | Returns the largest integer not larger than the argument.
foreign import floor :: Number -> Number

-- | Returns the result of the C-like 32-bit multiplication of the two arguments.
foreign import imul :: Int -> Int -> Int

-- | Returns the natural logarithm of a number.
foreign import log :: Number -> Number

Expand Down

0 comments on commit 4deba8e

Please sign in to comment.