forked from fasterthanlime/jsmad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mad.js
55 lines (42 loc) · 1.38 KB
/
mad.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/* Global namespace */
Mad = {};
Mad.recoverable = function (error) {
return (error & 0xff00) != 0;
};
// credit: http:https://blog.stevenlevithan.com/archives/fast-string-multiply
Mad.mul = function (str, num) {
var i = Math.ceil(Math.log(num) / Math.LN2), res = str;
do {
res += res;
} while (0 < --i);
return res.slice(0, str.length * num);
};
Mad.memcpy = function (dst, dstOffset, src, srcOffset, length) {
// this is a pretty weird memcpy actually - it constructs a new version of dst, because we have no other way to do it
return dst.slice(0, dstOffset) + src.slice(srcOffset, srcOffset + length) + dst.slice(dstOffset + length);
};
Mad.rshift = function (num, bits) {
return Math.floor(num / Math.pow(2, bits));
};
Mad.lshiftU32 = function (num, bits) {
return Mad.bitwiseAnd(Mad.lshift(num, bits), 4294967295 /* 2^32 - 1 */);
};
Mad.lshift = function (num, bits) {
return num * Math.pow(2, bits);
};
Mad.bitwiseOr = function (a, b) {
var w = 2147483648; // 2^31
var aHI = (a / w) << 0;
var aLO = a % w;
var bHI = (b / w) << 0;
var bLO = b % w;
return ((aHI | bHI) * w + (aLO | bLO));
};
Mad.bitwiseAnd = function (a, b) {
var w = 2147483648; // 2^31
var aHI = (a / w) << 0;
var aLO = a % w;
var bHI = (b / w) << 0;
var bLO = b % w;
return ((aHI & bHI) * w + (aLO & bLO));
};