-
Notifications
You must be signed in to change notification settings - Fork 4
/
int32.ts
359 lines (319 loc) · 8.58 KB
/
int32.ts
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
export class int32 {
private limbs: Uint16Array;
constructor(low: number, high: number) {
this.limbs = new Uint16Array([low, high]);
}
/**
* Returns an int32 with value 0
*
* @returns {int32}
*/
static zero(): int32 {
return new int32(0, 0);
}
/**
* Returns an int32 with value 1
*
* @returns {int32}
*/
static one(): int32 {
return new int32(1, 0);
}
/**
* Returns an int32 with the value of {int}
*
* @returns {int32}
*/
static fromInt(int: number): int32 {
return new int32(int & 0xffff, int >>> 16);
}
/**
* Returns an int32 with the value of {int}
*
* @param {number} flag
* @returns {int32}
*/
static fromFlag(flag: number): int32 {
const tmp: Int32Array = new Int32Array(1);
tmp[0] = flag;
tmp[0] *= -1;
return new int32(tmp[0] & 0xffff, tmp[0] >>> 16);
}
/**
* @returns {number}
*/
low() {
return this.limbs[0];
}
/**
* @returns {number}
*/
high() {
return this.limbs[1];
}
/**
* @returns {number}
*/
lsb() {
return this.limbs[0] & 1;
}
/**
* @returns {number}
*/
msb() {
return (this.limbs[1] >>> 15) & 1;
}
/**
* Returns the sum of {this} and {arg}.
*
* @param {number|int32} arg
* @returns {number}
*/
add(arg: number|int32): int32 {
if (arg instanceof int32) {
return int32_add_int32(this, arg);
}
return int32_add_number(this, arg);
}
/**
* Returns the result of {this & arg}.
*
* @param {number|int32} arg
* @returns {number}
*/
and(arg: number|int32): int32 {
if (arg instanceof int32) {
return int32_and_int32(this, arg);
}
return int32_and_number(this, arg);
}
/**
* Returns -1 if {this < arg}
* Returns 0 if {this == arg}
* Returns 1 if {this > arg}
*
* @param {number|int32} arg
* @returns {number}
*/
compare(arg: number|int32): int32 {
if (typeof arg === 'number') {
arg = int32.fromInt(arg);
}
return int32_compare(this, arg);
}
/**
* Is this number zero?
*
* @returns {number} 1 if true, 0 if false
*/
isZero(): number {
return int32_is_zero(this);
}
/**
* Return {this} left-shifted by {amount}.
*
* @param {number} amount
* @returns {number}
*/
lshift(amount: number): int32 {
if (amount > 31) {
return int32.zero();
}
return int32_lshift(this, amount);
}
/**
* Returns a bitwise NOT of {this}
*
* @returns {number}
*/
not(): int32 {
return int32_not(this);
}
/**
* Returns the result of {this | arg}.
*
* @param {number|int32} arg
* @returns {number}
*/
or(arg: number|int32): int32 {
if (arg instanceof int32) {
return int32_or_int32(this, arg);
}
return int32_or_number(this, arg);
}
/**
* Return {this} right-shifted by {amount}.
*
* @param {number} amount
* @returns {int32}
*/
rshift(amount: number): int32 {
if (amount > 31) {
return int32.zero();
}
return int32_rshift(this, amount);
}
/**
* Returns {this - arg}.
*
* @param {number|int32} arg
* @returns {number}
*/
sub(arg: number|int32): int32 {
if (arg instanceof int32) {
return int32_sub_int32(this, arg);
}
return int32_sub_number(this, arg);
}
/**
* Returns {this ^ arg}
*
* @param {number|int32} arg
* @returns {number}
*/
xor(arg: number|int32): int32 {
if (arg instanceof int32) {
return int32_xor_int32(this, arg);
}
return int32_xor_number(this, arg);
}
/**
* Returns the hexadecimal representation of this integer.
*
* @returns {string}
*/
toHex(): string {
const l: string = (this.low() & 0xffff)
.toString(16)
.padStart(4, '0');
const h: string = (this.high() & 0xffff)
.toString(16)
.padStart(4, '0');
return h.concat(l);
}
/**
* Subtract 1, return a new int32
*
* @returns {int32}
*/
decrement(): int32 {
const out: int32 = new int32(this.low(), this.high());
const carry: Uint32Array = new Uint32Array(1);
carry[0] = out.limbs[0] - 1;
out.limbs[0] = carry[0] & 0xffff;
out.limbs[1] = out.limbs[1] + (carry[0] >>> 16);
return out;
}
/**
* Convert to a JavaScript Number.
*
* @returns {number}
*/
toNumber(): number {
return (this.low() | (this.high() << 16));
}
/**
* MUTATES the obejct!
*
* Set both limbs to 0 in-place.
*/
wipe(): void {
this.limbs[0] ^= this.limbs[0];
this.limbs[1] ^= this.limbs[1];
}
}
function int32_add_int32(a: int32, b: int32): int32 {
let l: number = a.low() + b.low();
let h: number = a.high() + b.high() + (l >>> 16);
return new int32(l & 0xffff, h & 0xffff);
}
function int32_add_number(a: int32, b: number): int32 {
let l: number = a.low() + (b & 0xffff);
let h: number = a.high() + (b >>> 16) + (l >>> 16);
return new int32(l & 0xffff, h & 0xffff);
}
function int32_and_int32(a: int32, b: int32): int32 {
let l: number = a.low() & b.low();
let h: number = a.high() & b.high();
return new int32(l & 0xffff, h & 0xffff);
}
function int32_and_number(a: int32, b: number): int32 {
let l: number = a.low() & (b & 0xffff);
let h: number = a.high() & (b >>> 16);
return new int32(l & 0xffff, h & 0xffff);
}
function int32_compare(left, right): int32 {
const diff: int32 = right.sub(left);
/*
This borrows a trick from Thomas Pornin's CTTK library:
https://github.com/pornin/CTTK/blob/1d592024398f06c8eda1d325bdbd105ac32d92b3/inc/cttk.h#L552-L581
> If both x >= 2^31 and y >= 2^31, then we can virtually
> subtract 2^31 from both, and we are back to the first
> case. Since (y-2^31)-(x-2^31) = y-x, the direct subtraction
> is already fine.
Except (left, right, diff) := (x, y, z), respectively
*/
let gt: number = diff.xor(left.xor(right).and(left.xor(diff))).msb();
let eq: number = right.xor(left).isZero();
return int32.fromInt((gt + gt + eq)).decrement();
}
function int32_is_zero(a: int32): number {
/*
Bitiwse OR the bits of the limbs together
then subtract 1 and grab the most significant bit.
[0] - 1 >>> 31
-> 1
[1..0xffff] - 1 >>> 31
-> 0
*/
const tmp = new Uint32Array(1);
tmp[0] = (a.low() | a.high());
tmp[0]--;
return tmp[0] >>> 31;
}
function int32_lshift(a: int32, x: number) {
let l: number = (a.low() << x);
let h: number = (l >>> 16) | (a.high() << x);
return new int32(l & 0xffff, h & 0xffff);
}
function int32_not(a: int32): int32 {
let l: number = a.low() ^ 0xffff;
let h: number = a.high() ^ 0xffff;
return new int32(l & 0xffff, h & 0xffff);
}
function int32_or_int32(a: int32, b: int32): int32 {
let l: number = a.low() | b.low();
let h: number = a.high() | b.high();
return new int32(l & 0xffff, h & 0xffff);
}
function int32_or_number(a: int32, b: number): int32 {
let l: number = a.low() | (b & 0xffff);
let h: number = a.high() | (b >>> 16);
return new int32(l & 0xffff, h & 0xffff);
}
function int32_rshift(a: int32, amount: number) {
let m: number = (1 << (amount + 1)) - 1;
let h: number = (a.high() >>> amount);
let l: number = (a.low() >>> amount) | (((a.high() & m) << 16) >>> amount);
return new int32(l & 0xffff, h & 0xffff);
}
function int32_sub_int32(a: int32, b: int32): int32 {
let l: number = a.low() - b.low();
let h: number = a.high() - b.high() + (l >>> 16);
return new int32(l & 0xffff, h & 0xffff);
}
function int32_sub_number(a: int32, b: number): int32 {
let l: number = a.low() - (b & 0xffff);
let h: number = a.high() - (b >>> 16) + (l >>> 16);
return new int32(l & 0xffff, h & 0xffff);
}
function int32_xor_int32(a: int32, b: int32): int32 {
let l: number = a.low() ^ b.low();
let h: number = a.high() ^ b.high();
return new int32(l & 0xffff, h & 0xffff);
}
function int32_xor_number(a: int32, b: number): int32 {
let l: number = a.low() ^ (b & 0xffff);
let h: number = a.high() ^ (b >>> 16);
return new int32(l & 0xffff, h & 0xffff);
}