-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathu128.ts
360 lines (307 loc) Β· 10.4 KB
/
u128.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
360
import { u256Safe as u256 } from './u256';
import { i128Safe } from './i128';
import { i256Safe } from './i256';
import { u128 as U128 } from '../u128';
import { u256 as U256 } from '../u256';
import { i128 as I128 } from '../i128';
import { i256 as I256 } from '../i256';
import { isPowerOverflow128, atou128 } from '../../utils';
// @external("safe_u128.spec.as", "logStr")
// declare function logStr(str: string): void;
// @external("safe_u128.spec.as", "logF64")
// declare function logF64(v: f64): void;
// export namespace safe {
class u128 extends U128 {
@inline static get Zero(): u128 { return new u128(); }
@inline static get One(): u128 { return new u128(1); }
@inline static get Min(): u128 { return new u128(); }
@inline static get Max(): u128 { return new u128(-1, -1); }
@inline
static fromString(value: string, radix: i32 = 10): u128 {
return changetype<u128>(atou128(value, radix));
}
@inline
static fromI256(value: I256): u128 {
return changetype<u128>(U128.fromI256(value));
}
@inline
static fromI256Safe(value: i256Safe): u128 {
return changetype<u128>(U128.fromI256(value));
}
@inline
static fromU256(value: u256): u128 {
return changetype<u128>(U128.fromU256(value));
}
@inline
static fromI128(value: I128): u128 {
return changetype<u128>(U128.fromI128(value));
}
@inline
static fromI128Safe(value: i128Safe): u128 {
return changetype<u128>(U128.fromI128(value));
}
@inline
static fromU128(value: u128): u128 {
return changetype<u128>(U128.fromU128(value));
}
@inline
static fromI64(value: i64): u128 {
return changetype<u128>(U128.fromI64(value));
}
@inline
static fromU64(value: u64): u128 {
return changetype<u128>(U128.fromU64(value));
}
@inline
static fromF64(value: f64): u128 {
return changetype<u128>(U128.fromF64(value));
}
@inline
static fromF32(value: f32): u128 {
return changetype<u128>(U128.fromF32(value));
}
@inline
static fromI32(value: i32): u128 {
return changetype<u128>(U128.fromI32(value));
}
@inline
static fromU32(value: u32): u128 {
return changetype<u128>(U128.fromU32(value));
}
@inline
static fromBool(value: bool): u128 {
return changetype<u128>(U128.fromBool(value));
}
@inline
static fromBits(lo1: u32, lo2: u32, hi1: u32, hi2: u32): u128 {
return changetype<u128>(U128.fromBits(lo1, lo2, hi1, hi2));
}
@inline
static fromBytes<T>(array: T, bigEndian: bool = false): u128 {
return changetype<u128>(U128.fromBytes<T>(array, bigEndian));
}
@inline
static fromBytesLE(array: u8[]): u128 {
return changetype<u128>(U128.fromBytesLE(array));
}
@inline
static fromBytesBE(array: u8[]): u128 {
return changetype<u128>(U128.fromBytesBE(array));
}
@inline
static fromUint8ArrayLE(array: Uint8Array): u128 {
return changetype<u128>(U128.fromUint8ArrayLE(array));
}
@inline
static fromUint8ArrayBE(array: Uint8Array): u128 {
return changetype<u128>(U128.fromUint8ArrayBE(array));
}
/**
* Create 128-bit unsigned integer from generic type T
* @param value
* @returns 128-bit unsigned integer
*/
@inline
static from<T>(value: T): u128 {
if (value instanceof bool) return u128.fromU64(<u64>value);
else if (value instanceof i8) return u128.fromI64(<i64>value);
else if (value instanceof u8) return u128.fromU64(<u64>value);
else if (value instanceof i16) return u128.fromI64(<i64>value);
else if (value instanceof u16) return u128.fromU64(<u64>value);
else if (value instanceof i32) return u128.fromI64(<i64>value);
else if (value instanceof u32) return u128.fromU64(<u64>value);
else if (value instanceof i64) return u128.fromI64(<i64>value);
else if (value instanceof u64) return u128.fromU64(<u64>value);
else if (value instanceof f32) return u128.fromF64(<f64>value);
else if (value instanceof f64) return u128.fromF64(<f64>value);
else if (value instanceof I128) return u128.fromI128(<I128>value);
else if (value instanceof i128Safe) return u128.fromI128(<i128Safe>value);
else if (value instanceof u128) return u128.fromU128(<u128>value);
else if (value instanceof U128) return u128.fromU128(<U128>value);
else if (value instanceof I256) return u128.fromI128(<I256>value);
else if (value instanceof i256Safe) return u128.fromI256(<i256Safe>value);
else if (value instanceof u256) return u128.fromU256(<u256>value);
else if (value instanceof u8[]) return u128.fromBytes(<u8[]>value);
else if (value instanceof Uint8Array) return u128.fromBytes(<Uint8Array>value);
else if (value instanceof String) return u128.fromString(<string>value);
else throw new TypeError("Unsupported generic type");
}
@inline @operator('|')
static or(a: u128, b: u128): u128 {
return changetype<u128>(U128.or(a, b));
}
@inline @operator('^')
static xor(a: u128, b: u128): u128 {
return changetype<u128>(U128.xor(a, b));
}
@inline @operator('&')
static and(a: u128, b: u128): u128 {
return changetype<u128>(U128.and(a, b));
}
@inline @operator('<<')
static shl(value: u128, shift: i32): u128 {
return changetype<u128>(U128.shl(value, shift));
}
@inline @operator('>>')
static shr(value: u128, shift: i32): u128 {
return changetype<u128>(U128.shr(value, shift));
}
@inline @operator('>>>')
static shr_u(value: u128, shift: i32): u128 {
return u128.shr(value, shift);
}
@inline
static rotl(value: u128, shift: i32): u128 {
return changetype<u128>(U128.rotl(value, shift));
}
@inline
static rotr(value: u128, shift: i32): u128 {
return changetype<u128>(U128.rotr(value, shift));
}
@operator.prefix('++')
preInc(): this {
if ((this.lo & this.hi) == <u64>-1) { // if this == max
throw new RangeError('Overflow during prefix incrementing');
}
super.preInc();
return this;
}
@operator.prefix('--')
preDec(): this {
if ((this.lo | this.hi) == 0) { // if this == 0
throw new RangeError('Underflow during prefix decrementing');
}
super.preDec();
return this;
}
@inline @operator.postfix('++')
postInc(): u128 {
return this.clone().preInc();
}
@inline @operator.postfix('--')
postDec(): u128 {
return this.clone().preDec();
}
@operator('+')
static add(a: u128, b: u128): u128 {
var bl = b.lo;
var lo = a.lo + bl;
var c = u64(lo < bl);
var x = a.hi;
var y = b.hi;
var hi = x + y + c;
if (((hi ^ x) & (hi ^ y)) < c) {
throw new RangeError('Overflow during addision');
}
return new u128(lo, hi);
}
@operator('-')
static sub(a: u128, b: u128): u128 {
if (a < b) throw new RangeError("Underflow during subtraction");
return changetype<u128>(
U128.sub(changetype<U128>(a), changetype<U128>(b))
);
}
@operator('*')
static mul(a: u128, b: u128): u128 {
if (a.isZero() || b.isZero()) {
return u128.Zero;
}
var s = u128.clz(a) + u128.clz(b);
if (s < 127) { // defenitely overflow
throw new RangeError("Overflow during multiplication");
}
if (s == 127) { // this may overflow or not. Need extra checks.
// See Hacker's Delight, 2nd Edition. 2β13 Overflow Detection
// @ts-ignore
let tmp = U128.mul(changetype<U128>(a), changetype<U128>(b) >> 1);
// @ts-ignore
if (tmp.hi >>> 63) { // (signed)t < 0
throw new RangeError("Overflow during multiplication");
}
// @ts-ignore
let z = tmp << 1;
if (b.lo & 1) {
// @ts-ignore
z += a;
// @ts-ignore
if (z < a) {
throw new RangeError("Overflow during multiplication");
}
}
return changetype<u128>(z);
}
return changetype<u128>(
U128.mul(changetype<U128>(a), changetype<U128>(b))
);
}
@inline @operator('/')
static div(a: u128, b: u128): u128 {
return changetype<u128>(U128.div(a, b));
}
@inline @operator('**')
static pow(base: u128, exponent: i32): u128 {
if (isPowerOverflow128(base, exponent)) {
throw new Error("Overflow during exponentiation");
}
return changetype<u128>(U128.pow(changetype<U128>(base), exponent));
}
@inline @operator('%')
static rem(a: u128, b: u128): u128 {
return changetype<u128>(U128.rem(a, b));
}
@inline
static div10(value: u128): u128 {
return changetype<u128>(U128.div10(value));
}
@inline
static rem10(value: u128): u128 {
return changetype<u128>(U128.rem10(value));
}
// compute floor(sqrt(x))
@inline
static sqrt(value: u128): u128 {
return changetype<u128>(U128.sqrt(value));
}
@inline
static muldiv(number: u128, numerator: u128, denominator: u128): u128 {
// TODO: Need implement overflow checking
return changetype<u128>(U128.muldiv(number, numerator, denominator));
}
@inline
toUnchecked(): U128 {
return changetype<U128>(this);
}
@inline
as<T>(): T {
var dummy: T;
if (dummy instanceof bool) return <T>this.toBool();
else if (dummy instanceof i8) return <T>this.toI64();
else if (dummy instanceof u8) return <T>this.toU64();
else if (dummy instanceof i16) return <T>this.toI64();
else if (dummy instanceof u16) return <T>this.toU64();
else if (dummy instanceof i32) return <T>this.toI64();
else if (dummy instanceof i64) return <T>this.toI64();
else if (dummy instanceof u32) return <T>this.toU64();
else if (dummy instanceof u64) return <T>this.toU64();
else if (dummy instanceof f32) return <T>this.toF64();
else if (dummy instanceof f64) return <T>this.toF64();
else if (dummy instanceof i128) return <T>this.toI128();
else if (dummy instanceof u128) return <T>this;
else if (dummy instanceof U128) return <T>this.toUnchecked();
else if (dummy instanceof u256) return <T>this.toU256();
else if (dummy instanceof U256) return <T>this.toU256();
else if (dummy instanceof u8[]) return <T>this.toBytes();
else if (dummy instanceof Uint8Array) return <T>this.toUint8Array();
else if (dummy instanceof String) return <T>this.toString();
else throw new TypeError('Unsupported generic type');
}
//
// unsigned div and rem already contain traps
//
@inline
clone(): u128 {
return new u128(this.lo, this.hi);
}
}
export { u128 as u128Safe };