-
Notifications
You must be signed in to change notification settings - Fork 29
/
int64.js
86 lines (65 loc) · 1.98 KB
/
int64.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
function int64(low, hi) {
this.low = (low >>> 0);
this.hi = (hi >>> 0);
this.add32inplace = function (val) {
let new_lo = (((this.low >>> 0) + val) & 0xFFFFFFFF) >>> 0;
let new_hi = (this.hi >>> 0);
if (new_lo < this.low) {
new_hi++;
}
this.hi = new_hi;
this.low = new_lo;
}
this.add32 = function (val) {
let new_lo = (((this.low >>> 0) + val) & 0xFFFFFFFF) >>> 0;
let new_hi = (this.hi >>> 0);
if (new_lo < this.low) {
new_hi++;
}
return new int64(new_lo, new_hi);
}
this.sub32 = function (val) {
let new_lo = (((this.low >>> 0) - val) & 0xFFFFFFFF) >>> 0;
let new_hi = (this.hi >>> 0);
if (new_lo > (this.low) & 0xFFFFFFFF) {
new_hi--;
}
return new int64(new_lo, new_hi);
}
this.sub32inplace = function (val) {
let new_lo = (((this.low >>> 0) - val) & 0xFFFFFFFF) >>> 0;
let new_hi = (this.hi >>> 0);
if (new_lo > (this.low) & 0xFFFFFFFF) {
new_hi--;
}
this.hi = new_hi;
this.low = new_lo;
}
this.and32 = function (val) {
let new_lo = this.low & val;
let new_hi = this.hi;
return new int64(new_lo, new_hi);
}
this.and64 = function (vallo, valhi) {
let new_lo = this.low & vallo;
let new_hi = this.hi & valhi;
return new int64(new_lo, new_hi);
}
this.toString = function () {
let lo_str = (this.low >>> 0).toString(16);
let hi_str = (this.hi >>> 0).toString(16);
if (this.hi == 0)
return lo_str;
else
lo_str = zeroFill(lo_str, 8)
return hi_str + lo_str;
}
return this;
}
function zeroFill(number, width) {
width -= number.toString().length;
if (width > 0) {
return new Array(width + (/\./.test(number) ? 2 : 1)).join('0') + number;
}
return number + ""; // always return a string
}