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

Commit 9d9de2f

Browse files
committed
Math.imul
1 parent 9ece563 commit 9d9de2f

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

src/Math.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,27 @@ 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+
return function (b) {
36+
var ah = (a >>> 16) & 0xffff;
37+
var al = a & 0xffff;
38+
var bh = (b >>> 16) & 0xffff;
39+
var bl = b & 0xffff;
40+
// the shift by 0 fixes the sign on the high part
41+
// the final |0 converts the unsigned value into a signed value
42+
return (al * bl) + (((ah * bl + al * bh) << 16) >>> 0) | 0;
43+
};
44+
}
45+
46+
exports.imul = Math.imul ? nativeImul : emulatedImul;
47+
2748
exports.trunc = Math.trunc || function (n) {
2849
return n < 0 ? Math.ceil(n) : Math.floor(n);
2950
};

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)