-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlong.js
242 lines (220 loc) · 5.42 KB
/
long.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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
//
// long.js
//
// Kopiluwak. Copyright (c) 2020 Ben Zotto
//
function KLInt64(bytes) {
this.storage = [];
for (let i = 0; i < 8; i++) {
if (typeof bytes[i] == 'number' && bytes[i] >= 0 && bytes[i] < 256) {
this.storage[i] = bytes[i];
} else {
this.storage[i] = 0;
}
}
this.isZero = function() {
for (let i = 0; i < 8; i++) {
if (this.storage[i] != 0) {
return false;
}
}
return true;
}
this.isNegative = function() {
return (this.storage[0] & 0x80) != 0;
}
this.lowWord = function() {
return ((this.storage[4] << 24) | (this.storage[5] << 16) | (this.storage[6] << 8) | this.storage[7]) >>> 0;
}
this.highWord = function() {
return ((this.storage[0] << 24) | (this.storage[1] << 16) | (this.storage[2] << 8) | this.storage[3]) >>> 0;
}
this.isEqualTo = function(other) {
for (let i = 0; i < 8; i++) {
if (this.storage[i] != other.storage[i]) {
return false;
}
}
return true;
}
this.asHexString = function() {
let str = "0x";
for (let i = 0; i < 8; i++) {
let hex = Number(this.storage[i]).toString(16);
str += (hex.length == 1) ? "0" + hex : hex;
}
return str;
}
this.asBytes = function() {
return this.storage.slice();
}
this.countLeadingZeroes = function() {
let count = 0;
for (let i = 0; i < 8; i++) {
let byte = this.storage[i];
if (byte == 0) {
count += 8;
} else {
let t = 0x80;
while ((byte & t) == 0) {
t = t >>> 1;
count++;
}
}
}
return count;
}
}
const KLInt64Zero = new KLInt64([0, 0, 0, 0, 0, 0, 0, 0]);
const KLInt64One= new KLInt64([0, 0, 0, 0, 0, 0, 0, 1]);
const KLInt64NegativeOne = new KLInt64([0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]);
const KLInt64MinValue = new KLInt64([0x80, 0, 0, 0, 0, 0, 0, 0]);
const KLInt64MaxValue = new KLInt64([0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]);
function KLInt64FromNumber(num) {
let sign = num < 0;
if (num < 0) {
num = -num;
}
num = Math.trunc(num);
let output = [];
for (let i = 7; i >= 0; i--) {
let byte = 0;
for (let j = 0; j < 8; j++) {
if (num > 0) {
let bit = num & 0x1;
byte = byte | (bit << j);
num = Math.trunc(num / 2);
}
}
output[i] = byte;
}
let result = new KLInt64(output);
if (sign) {
result = KLInt64Negated(result);
}
return result;
}
function KLInt64Add(int1, int2) {
let output = [];
let carry = 0;
for (let i = 7; i >= 0; i--) {
let byte1 = int1.storage[i];
let byte2 = int2.storage[i];
let sum = byte1 + byte2 + carry;
carry = (sum & 0x100) != 0 ? 1 : 0;
output[i] = sum & 0xFF;
}
return new KLInt64(output);
}
function KLInt64Negated(int) {
let output = [];
let carry = 1;
for (let i = 7; i >= 0; i--) {
let byte = int.storage[i];
let inv = ~byte & 0xFF;
let res = inv + carry;
carry = (res & 0x100) != 0 ? 1 : 0;
output[i] = res & 0xFF;
}
return new KLInt64(output);
}
function KLInt64Subtract(int1, int2) {
let negative2 = KLInt64Negated(int2);
return KLInt64Add(int1, negative2);
}
function KLInt64LogicalShiftLeft(int, s) {
if (s < 0 || s > 63) {
return KLInt64Zero;
}
let offsetLower = Math.trunc(s/8);
let offsetUpper = offsetLower + 1;
let upperOverhang = s % 8;
let output = [];
for (let i = 0; i < 8; i++) {
let inb = int.storage[i] << upperOverhang;
let destLower = i - offsetLower;
let destUpper = i - offsetUpper;
if (destUpper >= 0) {
let outb = output[destUpper];
if (outb == undefined) { outb = 0; }
outb = outb | ((inb >> 8) & 0xFF);
output[destUpper] = outb;
}
if (destLower >= 0) {
let outb = output[destLower];
if (outb == undefined) { outb = 0; }
outb = outb | (inb & 0xFF);
output[destLower] = outb;
}
}
return new KLInt64(output);
}
function KLInt64ArithmeticShiftRight(int, s) {
return KLInt64ShiftRight(int, s, true);
}
function KLInt64ShiftRight(int, s, arithmetic) {
if (arithmetic == undefined) { arithmetic = false; }
if (s < 0 || s > 63) {
return KLInt64Zero;
}
let shiftBytes = Math.trunc(s/8);
let shiftBits = s % 8
let output = [];
if (arithmetic && int.isNegative()) {
for (let i = 0; i < shiftBytes; i++) {
output[i] = 0xFF;
}
let leftoverByte = (0xFF00 >> shiftBits) & 0xFF;
output[shiftBytes] = leftoverByte;
}
let offsetLower = shiftBytes + 1;
let offsetUpper = offsetLower - 1;
let lowerOverhang = shiftBits;
for (let i = 0; i < 8; i++) {
let inb = int.storage[i] << (8 - lowerOverhang);
let destLower = i + offsetLower;
let destUpper = i + offsetUpper;
if (destUpper < 8) {
let outb = output[destUpper];
if (outb == undefined) { outb = 0; }
outb = outb | ((inb >> 8) & 0xFF);
output[destUpper] = outb;
}
if (destLower < 8) {
let outb = output[destLower];
if (outb == undefined) { outb = 0; }
outb = outb | (inb & 0xFF);
output[destLower] = outb;
}
}
return new KLInt64(output);
}
function KLInt64Multiply(int1, int2) {
let a = int1;
let b = int2;
let result = KLInt64Zero;
while (!b.isZero()) {
if ((b.storage[7] & 0x01) > 0) {
result = KLInt64Add(result, a);
}
a = KLInt64LogicalShiftLeft(a, 1);
b = KLInt64ShiftRight(b, 1, false);
}
return result;
}
function KLInt64BitwiseAnd(int1, int2) {
let output = [];
for (let i = 0; i < 8; i++) {
let byte = int1.storage[i] & int2.storage[i];
output[i] = byte;
}
return new KLInt64(output);
}
function KLInt64BitwiseOr(int1, int2) {
let output = [];
for (let i = 0; i < 8; i++) {
let byte = int1.storage[i] | int2.storage[i];
output[i] = byte;
}
return new KLInt64(output);
}