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

Math.imul #18

Merged
merged 1 commit into from
Oct 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/Math.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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