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

Commit a41ed69

Browse files
authored
Merge pull request #18 from matthewleon/imul
Math.imul
2 parents 6f3418b + 4deba8e commit a41ed69

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

src/Math.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,28 @@ exports.exp = Math.exp;
2424

2525
exports.floor = Math.floor;
2626

27+
function nativeImul(a) {
28+
return function (b) {
29+
return Math.imul(a, b);
30+
};
31+
}
32+
33+
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/imul
34+
function emulatedImul(a) {
35+
/*jshint bitwise: false*/
36+
return function (b) {
37+
var ah = a >>> 16 & 0xffff;
38+
var al = a & 0xffff;
39+
var bh = b >>> 16 & 0xffff;
40+
var bl = b & 0xffff;
41+
// the shift by 0 fixes the sign on the high part
42+
// the final |0 converts the unsigned value into a signed value
43+
return al * bl + (ah * bl + al * bh << 16 >>> 0) | 0;
44+
};
45+
}
46+
47+
exports.imul = Math.imul ? nativeImul : emulatedImul;
48+
2749
exports.trunc = Math.trunc || function (n) {
2850
return n < 0 ? Math.ceil(n) : Math.floor(n);
2951
};

src/Math.purs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ foreign import exp :: Number -> Number
3636
-- | Returns the largest integer not larger than the argument.
3737
foreign import floor :: Number -> Number
3838

39+
-- | Returns the result of the C-like 32-bit multiplication of the two arguments.
40+
foreign import imul :: Int -> Int -> Int
41+
3942
-- | Returns the natural logarithm of a number.
4043
foreign import log :: Number -> Number
4144

0 commit comments

Comments
 (0)