Repository navigation
Expand file tree
/
Copy pathint64.js
More file actions
120 lines (92 loc) · 2.49 KB
/
Copy pathint64.js
File metadata and controls
120 lines (92 loc) · 2.49 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
function zeroFill(number, width) {
width -= number.toString().length;
if (width > 0) {
return new Array(width + (/\./.test(number) ? 2 : 1)).join("0") + number;
}
return number + "";
}
function int64(low, hi) {
this.low = low >>> 0;
this.hi = hi >>> 0;
this.backing = null;
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;
if (this.backing !== null) {
if (this.backing.byteLength < val) {
throw new Error("int64.add32inplace: overflow");
}
this.backing = new Uint8Array(
this.backing.buffer,
val,
this.backing.byteLength - val,
);
}
};
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++;
}
let ret = new int64(new_lo, new_hi);
if (this.backing !== null) {
if (this.backing.byteLength < val) {
throw new Error("int64.add32: overflow");
}
ret.backing = new Uint8Array(
this.backing.buffer,
val,
this.backing.byteLength - val,
);
}
return ret;
};
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 (radix = 16) {
let lo_str = (this.low >>> 0).toString(radix);
let hi_str = (this.hi >>> 0).toString(radix);
if (this.hi == 0) {
return lo_str;
} else {
const width = radix === 16 ? 8 : Math.ceil(32 / Math.log2(radix));
lo_str = zeroFill(lo_str, width);
}
return hi_str + lo_str;
};
return this;
}
globalThis.int64 = int64;
export { int64 };
export default int64;