Comparing version 0.1.7 to 0.2.0
@@ -7,3 +7,3 @@ import { u128 } from '../../assembly/integer/safe/u128'; | ||
var b = new u128(255, 100); | ||
expect<u128>(a + b).toStrictEqual(new u128(355, 355)); | ||
expect(a + b).toStrictEqual(new u128(355, 355)); | ||
}); | ||
@@ -14,3 +14,3 @@ | ||
var b = u128.Zero; | ||
expect<u128>(a + b).toStrictEqual(u128.Max); | ||
expect(a + b).toStrictEqual(u128.Max); | ||
}); | ||
@@ -21,3 +21,3 @@ | ||
var b = u128.One; | ||
expect<u128>(a + b).toStrictEqual(u128.Max); | ||
expect(a + b).toStrictEqual(u128.Max); | ||
}); | ||
@@ -28,3 +28,3 @@ | ||
var b = new u128(100, 255); | ||
expect<u128>(a - b).toStrictEqual(new u128(255, 100)); | ||
expect(a - b).toStrictEqual(new u128(255, 100)); | ||
}); | ||
@@ -35,3 +35,3 @@ | ||
var b = u128.Max; | ||
expect<u128>(a - b).toStrictEqual(u128.Zero); | ||
expect(a - b).toStrictEqual(u128.Zero); | ||
}); | ||
@@ -42,9 +42,33 @@ | ||
var b = u128.Zero; | ||
expect<u128>(a - b).toStrictEqual(u128.Max); | ||
expect(a - b).toStrictEqual(u128.Max); | ||
}); | ||
it("Should pre decrement one", () => { | ||
var a = u128.One; | ||
--a; | ||
expect(a).toStrictEqual(u128.Zero); | ||
}); | ||
it("Should pre increment zero", () => { | ||
var a = u128.Zero; | ||
++a; | ||
expect(a).toStrictEqual(u128.One); | ||
}); | ||
it("Should post increment zero", () => { | ||
var a = u128.Zero; | ||
a++; | ||
expect(a).toStrictEqual(u128.One); | ||
}); | ||
it("Should post decrement one", () => { | ||
var a = u128.One; | ||
a--; | ||
expect(a).toStrictEqual(u128.Zero); | ||
}); | ||
it("Should multiply two numbers 1", () => { | ||
var a = u128.from(43545453452); | ||
var b = u128.Zero; | ||
expect<u128>(a * b).toStrictEqual(u128.Zero); | ||
expect(a * b).toStrictEqual(u128.Zero); | ||
}); | ||
@@ -55,3 +79,3 @@ | ||
var b = u128.One; | ||
expect<u128>(a * b).toStrictEqual(a); | ||
expect(a * b).toStrictEqual(a); | ||
}); | ||
@@ -62,3 +86,3 @@ | ||
var b = u128.One; | ||
expect<u128>(a * b).toStrictEqual(a); | ||
expect(a * b).toStrictEqual(a); | ||
}); | ||
@@ -69,5 +93,12 @@ | ||
var b = u128.from(2353454354); | ||
expect<u128>(a * b).toStrictEqual(new u128(10248516654965971928, 5)); | ||
expect(a * b).toStrictEqual(new u128(10248516654965971928, 5)); | ||
}); | ||
it("Should multiply two numbers which clz(a) + clz(b) == 127 but not overflow", () => { | ||
var a = u128.from("333333333333333333333"); | ||
var b = new u128(<u64>1000000000000000000); | ||
expect(u128.clz(a) + u128.clz(b)).toBe(127); | ||
expect(a * b).toStrictEqual(u128.from("333333333333333333333000000000000000000")); | ||
}); | ||
it("Should power number 1", () => { | ||
@@ -86,27 +117,27 @@ expect<u128>(u128.Zero ** 1024).toStrictEqual(u128.Zero); | ||
it("Should power number 4", () => { | ||
expect<u128>(new u128(2) ** 127).toStrictEqual(new u128(0, 0x8000000000000000)); | ||
expect(new u128(2) ** 127).toStrictEqual(new u128(0, 0x8000000000000000)); | ||
}); | ||
it("Should power number 5", () => { | ||
expect<u128>(new u128(3) ** 80).toStrictEqual(new u128(0x3CEA59789C79D441, 0x6F32F1EF8B18A2BC)); | ||
expect(new u128(3) ** 80).toStrictEqual(new u128(0x3CEA59789C79D441, 0x6F32F1EF8B18A2BC)); | ||
}); | ||
it("Should power number 6", () => { | ||
expect<u128>(new u128(4) ** 63).toStrictEqual(new u128(0, 0x4000000000000000)); | ||
expect(new u128(4) ** 63).toStrictEqual(new u128(0, 0x4000000000000000)); | ||
}); | ||
it("Should power number 7", () => { | ||
expect<u128>(new u128(5) ** 55).toStrictEqual(new u128(0xFFF4B4E3F741CF6D, 0xD0CF4B50CFE20765)); | ||
expect(new u128(5) ** 55).toStrictEqual(new u128(0xFFF4B4E3F741CF6D, 0xD0CF4B50CFE20765)); | ||
}); | ||
it("Should power number 8", () => { | ||
expect<u128>(new u128(6) ** 49).toStrictEqual(new u128(0x4286000000000000, 0x6558E2A0921FE069)); | ||
expect(new u128(6) ** 49).toStrictEqual(new u128(0x4286000000000000, 0x6558E2A0921FE069)); | ||
}); | ||
it("Should power number 9", () => { | ||
expect<u128>(new u128(0, 1) ** 1).toStrictEqual(new u128(0, 1)); | ||
expect(new u128(0, 1) ** 1).toStrictEqual(new u128(0, 1)); | ||
}); | ||
it("Should power number 10", () => { | ||
expect<u128>(new u128(u64.MAX_VALUE) ** 2).toStrictEqual(new u128(1, 0x0FFFFFFFFFFFFFFFE)); | ||
expect(new u128(u64.MAX_VALUE) ** 2).toStrictEqual(new u128(1, 0x0FFFFFFFFFFFFFFFE)); | ||
}); | ||
@@ -117,3 +148,3 @@ }) | ||
it("Should throw when add two numbers 1", () => { | ||
expectFn(() => { | ||
expect(() => { | ||
var a = u128.One; | ||
@@ -126,3 +157,3 @@ var b = u128.Max; | ||
it("Should throw when add two numbers 2", () => { | ||
expectFn(() => { | ||
expect(() => { | ||
var a = u128.Max; | ||
@@ -135,3 +166,3 @@ var b = u128.One; | ||
it("Should throw when add two numbers 3", () => { | ||
expectFn(() => { | ||
expect(() => { | ||
var a = u128.from(-2); | ||
@@ -144,3 +175,3 @@ var b = new u128(2); | ||
it("Should throw when subtract two numbers 1", () => { | ||
expectFn(() => { | ||
expect(() => { | ||
var a = u128.Zero; | ||
@@ -153,3 +184,3 @@ var b = u128.Max; | ||
it("Should throw when subtract two numbers 2", () => { | ||
expectFn(() => { | ||
expect(() => { | ||
var a = u128.from(-2); | ||
@@ -162,3 +193,3 @@ var b = u128.Max; | ||
it("Should throw when subtract two numbers 3", () => { | ||
expectFn(() => { | ||
expect(() => { | ||
var a = u128.Zero; | ||
@@ -170,4 +201,32 @@ var b = u128.One; | ||
it("Should throw when post decrement min number", () => { | ||
expect(() => { | ||
var a = u128.Zero; | ||
!(a--); | ||
}).toThrow(); | ||
}); | ||
it("Should throw when post increment max number", () => { | ||
expect(() => { | ||
var a = u128.Max; | ||
!(a++); | ||
}).toThrow(); | ||
}); | ||
it("Should throw when pre decrement min number", () => { | ||
expect(() => { | ||
var a = u128.Zero; | ||
!(--a); | ||
}).toThrow(); | ||
}); | ||
it("Should throw when pre increment max number", () => { | ||
expect(() => { | ||
var a = u128.Max; | ||
!(++a); | ||
}).toThrow(); | ||
}); | ||
it("Should throw multiply two numbers with overflow 1", () => { | ||
expectFn(() => { | ||
expect(() => { | ||
var a = new u128(0, 1); | ||
@@ -179,3 +238,3 @@ !(a * a); | ||
it("Should throw multiply two numbers with overflow 2", () => { | ||
expectFn(() => { | ||
expect(() => { | ||
var a = new u128(1, 1); | ||
@@ -187,3 +246,3 @@ !(a * a); | ||
it("Should throw multiply two numbers with overflow 3", () => { | ||
expectFn(() => { | ||
expect(() => { | ||
var a = u128.Max; | ||
@@ -196,3 +255,3 @@ var b = u128.from(2); | ||
it("Should throw multiply two numbers with overflow 4", () => { | ||
expectFn(() => { | ||
expect(() => { | ||
var a = u128.Max; | ||
@@ -204,4 +263,12 @@ var b = u128.Max; | ||
it("Should throw multiply two numbers which clz(a) + clz(b) == 127", () => { | ||
expect(() => { | ||
let a = u128.from("511111111111111111111"); | ||
let b = new u128(<u64>1111111111111111111); | ||
!(a * b); | ||
}).toThrow(); | ||
}); | ||
it("Should throw power with overflow 1", () => { | ||
expectFn(() => { | ||
expect(() => { | ||
!(new u128(2) ** 128); | ||
@@ -212,3 +279,3 @@ }).toThrow(); | ||
it("Should throw power with overflow 2", () => { | ||
expectFn(() => { | ||
expect(() => { | ||
!(new u128(3) ** 81); | ||
@@ -219,3 +286,3 @@ }).toThrow(); | ||
it("Should throw power with overflow 3", () => { | ||
expectFn(() => { | ||
expect(() => { | ||
!(new u128(3) ** 120); | ||
@@ -226,3 +293,3 @@ }).toThrow(); | ||
it("Should throw power with overflow 4", () => { | ||
expectFn(() => { | ||
expect(() => { | ||
!(new u128(4) ** 64); | ||
@@ -233,3 +300,3 @@ }).toThrow(); | ||
it("Should throw power with overflow 5", () => { | ||
expectFn(() => { | ||
expect(() => { | ||
!(new u128(4) ** 120); | ||
@@ -240,3 +307,3 @@ }).toThrow(); | ||
it("Should throw power with overflow 6", () => { | ||
expectFn(() => { | ||
expect(() => { | ||
!(new u128(5) ** 56); | ||
@@ -247,3 +314,3 @@ }).toThrow(); | ||
it("Should throw power with overflow 7", () => { | ||
expectFn(() => { | ||
expect(() => { | ||
!(new u128(5) ** 60); | ||
@@ -254,3 +321,3 @@ }).toThrow(); | ||
it("Should throw power with overflow 8", () => { | ||
expectFn(() => { | ||
expect(() => { | ||
!(new u128(6) ** 50); | ||
@@ -261,3 +328,3 @@ }).toThrow(); | ||
it("Should throw power with overflow 9", () => { | ||
expectFn(() => { | ||
expect(() => { | ||
!(new u128(7) ** 49); | ||
@@ -268,3 +335,3 @@ }).toThrow(); | ||
it("Should throw power with overflow 10", () => { | ||
expectFn(() => { | ||
expect(() => { | ||
!(new u128(8) ** 43); | ||
@@ -275,3 +342,3 @@ }).toThrow(); | ||
it("Should throw power with overflow 11", () => { | ||
expectFn(() => { | ||
expect(() => { | ||
!(new u128(9) ** 41); | ||
@@ -282,3 +349,3 @@ }).toThrow(); | ||
it("Should throw power with overflow 12", () => { | ||
expectFn(() => { | ||
expect(() => { | ||
!(new u128(10) ** 39); | ||
@@ -289,3 +356,3 @@ }).toThrow(); | ||
it("Should throw power with overflow 13", () => { | ||
expectFn(() => { | ||
expect(() => { | ||
!(new u128(11) ** 38); | ||
@@ -296,3 +363,3 @@ }).toThrow(); | ||
it("Should throw power with overflow 14", () => { | ||
expectFn(() => { | ||
expect(() => { | ||
!(new u128(12) ** 36); | ||
@@ -303,3 +370,3 @@ }).toThrow(); | ||
it("Should throw power with overflow 15", () => { | ||
expectFn(() => { | ||
expect(() => { | ||
!(new u128(0, 1) ** 2); | ||
@@ -310,3 +377,3 @@ }).toThrow(); | ||
it("Should throw power with overflow 16", () => { | ||
expectFn(() => { | ||
expect(() => { | ||
!(new u128(u64.MAX_VALUE) ** 3); | ||
@@ -323,3 +390,3 @@ }).toThrow(); | ||
]; | ||
expect<u128>(u128.fromBytes(arr)).toStrictEqual(new u128(0x8877665544332211, 0x12ffeeddccbbaa99)); | ||
expect(u128.fromBytes(arr)).toStrictEqual(new u128(0x8877665544332211, 0x12ffeeddccbbaa99)); | ||
}); | ||
@@ -332,4 +399,4 @@ | ||
]; | ||
expect<u128>(u128.fromBytes(arr, true)).toStrictEqual(new u128(0x99aabbccddeeff12, 0x1122334455667788)); | ||
expect(u128.fromBytes(arr, true)).toStrictEqual(new u128(0x99aabbccddeeff12, 0x1122334455667788)); | ||
}); | ||
}); |
@@ -7,3 +7,3 @@ import { u128 } from '../../assembly/integer/u128'; | ||
var a = new u128(10248516654965971928, 5); | ||
expect<string>('102482237023513730008').toStrictEqual(a.toString()); | ||
expect('102482237023513730008').toStrictEqual(a.toString()); | ||
}); | ||
@@ -13,3 +13,3 @@ | ||
var a = u128.Max; | ||
expect<string>('340282366920938463463374607431768211455').toStrictEqual(a.toString()); | ||
expect('340282366920938463463374607431768211455').toStrictEqual(a.toString()); | ||
}); | ||
@@ -19,79 +19,79 @@ | ||
var a = u128.Zero; | ||
expect<string>('0').toStrictEqual(a.toString()); | ||
expect('0').toStrictEqual(a.toString()); | ||
}); | ||
it("Should convert from decimal string 1", () => { | ||
expect<u128>(u128.from('')).toStrictEqual(u128.Zero); | ||
expect(u128.from('')).toStrictEqual(u128.Zero); | ||
}); | ||
it("Should convert from decimal string 2", () => { | ||
expect<u128>(u128.from('0')).toStrictEqual(u128.Zero); | ||
expect(u128.from('0')).toStrictEqual(u128.Zero); | ||
}); | ||
it("Should convert from decimal string 3", () => { | ||
expect<u128>(u128.from('123456789')).toStrictEqual(new u128(123456789)); | ||
expect(u128.from('123456789')).toStrictEqual(new u128(123456789)); | ||
}); | ||
it("Should convert from decimal string 4", () => { | ||
expect<u128>(u128.from('340282366920938463463374607431768211455')).toStrictEqual(u128.Max); | ||
expect(u128.from('340282366920938463463374607431768211455')).toStrictEqual(u128.Max); | ||
}); | ||
it("Should convert from decimal string 5", () => { | ||
expect<u128>(u128.from('-123456789')).toStrictEqual(u128.from(-123456789)); | ||
expect(u128.from('-123456789')).toStrictEqual(u128.from(-123456789)); | ||
}); | ||
it("Should convert from decimal string 6", () => { | ||
expect<u128>(u128.from('+123456789')).toStrictEqual(new u128(123456789)); | ||
expect(u128.from('+123456789')).toStrictEqual(new u128(123456789)); | ||
}); | ||
it("Should convert from decimal string 7", () => { | ||
expect<u128>(u128.fromString('123456789', 10)).toStrictEqual(new u128(123456789)); | ||
expect(u128.fromString('123456789', 10)).toStrictEqual(new u128(123456789)); | ||
}); | ||
it("Should convert from decimal with invalid chars string 1", () => { | ||
expect<u128>(u128.from('00000123abc')).toStrictEqual(new u128(123)); | ||
expect(u128.from('00000123abc')).toStrictEqual(new u128(123)); | ||
}); | ||
it("Should convert from decimal with invalid chars string 2", () => { | ||
expect<u128>(u128.from('x00000123abc')).toStrictEqual(u128.Zero); | ||
expect(u128.from('x00000123abc')).toStrictEqual(u128.Zero); | ||
}); | ||
it("Should convert from decimal with invalid chars string 3", () => { | ||
expect<u128>(u128.from('-x')).toStrictEqual(u128.Zero); | ||
expect(u128.from('-x')).toStrictEqual(u128.Zero); | ||
}); | ||
it("Should convert from decimal with invalid chars string 4", () => { | ||
expect<u128>(u128.from('--01234')).toStrictEqual(u128.Zero); | ||
expect(u128.from('--01234')).toStrictEqual(u128.Zero); | ||
}); | ||
it("Should convert from decimal with invalid chars string 5", () => { | ||
expect<u128>(u128.from('123\u3012')).toStrictEqual(new u128(123)); | ||
expect(u128.from('123\u3012')).toStrictEqual(new u128(123)); | ||
}); | ||
it("Should convert from decimal with invalid chars string 6", () => { | ||
expect<u128>(u128.from(String.fromCodePoint(0x10000))).toStrictEqual(u128.Zero); | ||
expect(u128.from(String.fromCodePoint(0x10000))).toStrictEqual(u128.Zero); | ||
}); | ||
it("Should convert from decimal with invalid chars string 7", () => { | ||
expect<u128>(u128.from('{0123')).toStrictEqual(u128.Zero); | ||
expect(u128.from('{0123')).toStrictEqual(u128.Zero); | ||
}); | ||
it("Should convert from decimal with invalid chars string 8", () => { | ||
expect<u128>(u128.from('/0123')).toStrictEqual(u128.Zero); | ||
expect(u128.from('/0123')).toStrictEqual(u128.Zero); | ||
}); | ||
it("Should convert from hex string", () => { | ||
expect<u128>(u128.fromString('123456abcdef', 16)).toStrictEqual(new u128(0x123456abcdef)); | ||
expect(u128.fromString('123456abcdef', 16)).toStrictEqual(new u128(0x123456abcdef)); | ||
}); | ||
it("Should convert from mixed case hex string", () => { | ||
expect<u128>(u128.from('0xabcdefABCDEF')).toStrictEqual(new u128(0xabcdefABCDEF)); | ||
expect(u128.from('0xabcdefABCDEF')).toStrictEqual(new u128(0xabcdefABCDEF)); | ||
}); | ||
it("Should convert from octal string", () => { | ||
expect<u128>(u128.from('0o01234567')).toStrictEqual(u128.from(0o01234567)); | ||
expect(u128.from('0o01234567')).toStrictEqual(u128.from(0o01234567)); | ||
}); | ||
it("Should convert from binary string", () => { | ||
expect<u128>(u128.from('0b10101010101010101010')).toStrictEqual(u128.from(0b10101010101010101010)); | ||
expect(u128.from('0b10101010101010101010')).toStrictEqual(u128.from(0b10101010101010101010)); | ||
}); | ||
@@ -108,3 +108,3 @@ }); | ||
expect<u128>(u128.fromBytes(arr)).toStrictEqual(new u128(0x8877665544332211, 0x12ffeeddccbbaa99)); | ||
expect(u128.fromBytes(arr)).toStrictEqual(new u128(0x8877665544332211, 0x12ffeeddccbbaa99)); | ||
}); | ||
@@ -119,3 +119,3 @@ | ||
expect<u128>(u128.fromBytes<Uint8Array>(uint8Array)).toStrictEqual(new u128(0x8877665544332211, 0x12ffeeddccbbaa99)); | ||
expect(u128.fromBytes<Uint8Array>(uint8Array)).toStrictEqual(new u128(0x8877665544332211, 0x12ffeeddccbbaa99)); | ||
}); | ||
@@ -129,3 +129,3 @@ | ||
expect<u128>(u128.fromBytes(arr, true)).toStrictEqual(new u128(0x99aabbccddeeff12, 0x1122334455667788)); | ||
expect(u128.fromBytes(arr, true)).toStrictEqual(new u128(0x99aabbccddeeff12, 0x1122334455667788)); | ||
}); | ||
@@ -140,3 +140,3 @@ | ||
expect<u128>(u128.fromBytes<Uint8Array>(uint8Array, true)).toStrictEqual(new u128(0x99aabbccddeeff12, 0x1122334455667788)); | ||
expect(u128.fromBytes<Uint8Array>(uint8Array, true)).toStrictEqual(new u128(0x99aabbccddeeff12, 0x1122334455667788)); | ||
}); | ||
@@ -148,3 +148,3 @@ | ||
var a = u.toBytes(); | ||
expect<bool>( | ||
expect( | ||
a[0] == 0x11 && a[1] == 0x22 && a[2] == 0x33 && a[3] == 0x44 && | ||
@@ -161,3 +161,3 @@ a[4] == 0x55 && a[5] == 0x66 && a[6] == 0x77 && a[7] == 0x88 && | ||
var a = u.toUint8Array(); | ||
expect<bool>( | ||
expect( | ||
a[0] == 0x11 && a[1] == 0x22 && a[2] == 0x33 && a[3] == 0x44 && | ||
@@ -173,3 +173,3 @@ a[4] == 0x55 && a[5] == 0x66 && a[6] == 0x77 && a[7] == 0x88 && | ||
var a = u.toBytes(true); | ||
expect<bool>( | ||
expect( | ||
a[0] == 0x11 && a[1] == 0x22 && a[2] == 0x33 && a[3] == 0x44 && | ||
@@ -185,3 +185,3 @@ a[4] == 0x55 && a[5] == 0x66 && a[6] == 0x77 && a[7] == 0x88 && | ||
var a = u.toUint8Array(true); | ||
expect<bool>( | ||
expect( | ||
a[0] == 0x11 && a[1] == 0x22 && a[2] == 0x33 && a[3] == 0x44 && | ||
@@ -198,4 +198,4 @@ a[4] == 0x55 && a[5] == 0x66 && a[6] == 0x77 && a[7] == 0x88 && | ||
var a = u128.from(-1); | ||
expect<u64>(a.lo).toBe(u64.MAX_VALUE); | ||
expect<u64>(a.hi).toBe(u64.MAX_VALUE); | ||
expect(a.lo).toBe(u64.MAX_VALUE); | ||
expect(a.hi).toBe(u64.MAX_VALUE); | ||
}); | ||
@@ -205,4 +205,4 @@ | ||
var a = u128.fromI32(-1); | ||
expect<u64>(a.lo).toBe(u64.MAX_VALUE); | ||
expect<u64>(a.hi).toBe(u64.MAX_VALUE); | ||
expect(a.lo).toBe(u64.MAX_VALUE); | ||
expect(a.hi).toBe(u64.MAX_VALUE); | ||
}); | ||
@@ -212,3 +212,3 @@ | ||
var a = u128.Max; | ||
expect<u64>(a.as<u64>()).toBe(u64.MAX_VALUE); | ||
expect(a.as<u64>()).toBe(u64.MAX_VALUE); | ||
}); | ||
@@ -218,3 +218,3 @@ | ||
var a = u128.from(-123456789); | ||
expect<i64>(a.as<i64>()).toBe(-123456789); | ||
expect(a.as<i64>()).toBe(-123456789); | ||
}); | ||
@@ -224,3 +224,3 @@ | ||
var a = u128.Max; | ||
expect<i64>(a.as<i64>()).toBe(<i64>-1); | ||
expect(a.as<i64>()).toBe(<i64>-1); | ||
}); | ||
@@ -230,3 +230,3 @@ | ||
var a = u128.Max; | ||
expect<u8>(a.as<u8>()).toBe(0xFF); | ||
expect(a.as<u8>()).toBe(0xFF); | ||
}); | ||
@@ -236,3 +236,3 @@ | ||
var a = u128.Zero; | ||
expect<f64>(a.as<f64>()).toBe(0.0); | ||
expect(a.as<f64>()).toBe(0.0); | ||
}); | ||
@@ -242,3 +242,3 @@ | ||
var a = u128.One; | ||
expect<f64>(a.as<f64>()).toBe(1.0); | ||
expect(a.as<f64>()).toBe(1.0); | ||
}); | ||
@@ -248,3 +248,3 @@ | ||
var a = new u128(33333); | ||
expect<f64>(a.as<f64>()).toBe(33333.0); | ||
expect(a.as<f64>()).toBe(33333.0); | ||
}); | ||
@@ -254,3 +254,3 @@ | ||
var a = new u128(9007199254740991); | ||
expect<f64>(a.as<f64>()).toBe(9007199254740991.0); | ||
expect(a.as<f64>()).toBe(9007199254740991.0); | ||
}); | ||
@@ -260,3 +260,3 @@ | ||
var a = u128.Max; | ||
expect<f64>(a.as<f64>()).toBe(340282366920938463463374607431768211455.0); | ||
expect(a.as<f64>()).toBe(340282366920938463463374607431768211455.0); | ||
}); | ||
@@ -266,3 +266,3 @@ | ||
var a = new u128(1 << 54); | ||
expect<f64>(a.as<f64>()).toBe(18014398509481984.0); | ||
expect(a.as<f64>()).toBe(18014398509481984.0); | ||
}); | ||
@@ -272,3 +272,3 @@ | ||
var a = new u128(1 << 55); | ||
expect<f64>(a.as<f64>()).toBe(36028797018963970.0); | ||
expect(a.as<f64>()).toBe(36028797018963970.0); | ||
}); | ||
@@ -278,3 +278,3 @@ | ||
var a = new u128(1 << 56); | ||
expect<f64>(a.as<f64>()).toBe(72057594037927940.0); | ||
expect(a.as<f64>()).toBe(72057594037927940.0); | ||
}); | ||
@@ -284,3 +284,3 @@ | ||
var a = new u128(1 << 57); | ||
expect<f64>(a.as<f64>()).toBe(144115188075855870.0); | ||
expect(a.as<f64>()).toBe(144115188075855870.0); | ||
}); | ||
@@ -290,3 +290,3 @@ | ||
var a = new u128(1 << 63); | ||
expect<f64>(a.as<f64>()).toBe(9223372036854776000.0); | ||
expect(a.as<f64>()).toBe(9223372036854776000.0); | ||
}); | ||
@@ -296,3 +296,3 @@ | ||
var a = new u128(u64.MAX_VALUE); | ||
expect<f64>(a.as<f64>()).toBe(18446744073709552000.0); | ||
expect(a.as<f64>()).toBe(18446744073709552000.0); | ||
}); | ||
@@ -304,3 +304,3 @@ }); | ||
var a = u128.Zero; | ||
expect<bool>(a.isZero()).toBe(true); | ||
expect(a.isZero()).toBe(true); | ||
}); | ||
@@ -310,3 +310,3 @@ | ||
var a = u128.One; | ||
expect<bool>(!a.isZero()).toBe(true); | ||
expect(!a.isZero()).toBe(true); | ||
}); | ||
@@ -316,3 +316,3 @@ | ||
var a = new u128(1, 2); | ||
expect<u128>(~a).toStrictEqual(new u128(~1, ~2)); | ||
expect(~a).toStrictEqual(new u128(~1, ~2)); | ||
}); | ||
@@ -322,3 +322,3 @@ | ||
var a = new u128(1, 2); | ||
expect<u128>(a).toStrictEqual(+a); | ||
expect(a).toStrictEqual(+a); | ||
}); | ||
@@ -328,3 +328,3 @@ | ||
var a = u128.One; | ||
expect<bool>(!!a).toBe(true); | ||
expect(!!a).toBe(true); | ||
}); | ||
@@ -334,7 +334,7 @@ | ||
var a = u128.Zero; | ||
expect<bool>(!a).toBe(true); | ||
expect(!a).toBe(true); | ||
}); | ||
it("Should number is empty 2", () => { | ||
expect<bool>(!changetype<u128>(null)).toBe(true); | ||
expect(!changetype<u128>(null)).toBe(true); | ||
}); | ||
@@ -345,3 +345,3 @@ | ||
var b = new u128(111, 0); | ||
expect<u128>((a | b)).toStrictEqual(new u128(111, 123)); | ||
expect((a | b)).toStrictEqual(new u128(111, 123)); | ||
}); | ||
@@ -352,3 +352,3 @@ | ||
var b = new u128(111, 0); | ||
expect<u128>((a ^ b)).toStrictEqual(new u128(0, 123)); | ||
expect((a ^ b)).toStrictEqual(new u128(0, 123)); | ||
}); | ||
@@ -359,3 +359,3 @@ | ||
var b = new u128(0x00FF, 234); | ||
expect<u128>((a & b)).toStrictEqual(new u128(0, 106)); | ||
expect((a & b)).toStrictEqual(new u128(0, 106)); | ||
}); | ||
@@ -366,3 +366,3 @@ | ||
var b = new u128(100, 255); | ||
expect<u128>(a).toStrictEqual(b); | ||
expect(a).toStrictEqual(b); | ||
}); | ||
@@ -373,3 +373,3 @@ | ||
var b = new u128(1, 0); | ||
expect<bool>(a != b).toBe(true); | ||
expect(a != b).toBe(true); | ||
}); | ||
@@ -380,3 +380,3 @@ | ||
var b = new u128(50, 100); | ||
expect<bool>(b < a).toBe(true); | ||
expect(b < a).toBe(true); | ||
}); | ||
@@ -387,3 +387,3 @@ | ||
var b = new u128(100, 100); | ||
expect<bool>(!(b < a)).toBe(true); | ||
expect(!(b < a)).toBe(true); | ||
}); | ||
@@ -394,3 +394,3 @@ | ||
var b = u128.Max; | ||
expect<bool>(a < b).toBe(true); | ||
expect(a < b).toBe(true); | ||
}); | ||
@@ -401,3 +401,3 @@ | ||
var b = new u128(50, 100); | ||
expect<bool>(b <= a).toBe(true); | ||
expect(b <= a).toBe(true); | ||
}); | ||
@@ -408,3 +408,3 @@ | ||
var b = new u128(100, 100); | ||
expect<bool>(b <= a).toBe(true); | ||
expect(b <= a).toBe(true); | ||
}); | ||
@@ -415,3 +415,3 @@ | ||
var b = new u128(50, 100); | ||
expect<bool>(a > b).toBe(true); | ||
expect(a > b).toBe(true); | ||
}); | ||
@@ -422,3 +422,3 @@ | ||
var b = new u128(100, 100); | ||
expect<bool>(!(a > b)).toBe(true); | ||
expect(!(a > b)).toBe(true); | ||
}); | ||
@@ -429,3 +429,3 @@ | ||
var b = new u128(50, 100); | ||
expect<bool>(a >= b).toStrictEqual(true); | ||
expect(a >= b).toStrictEqual(true); | ||
}); | ||
@@ -436,3 +436,3 @@ | ||
var b = new u128(100, 100); | ||
expect<bool>(a >= b).toStrictEqual(true); | ||
expect(a >= b).toStrictEqual(true); | ||
}); | ||
@@ -443,3 +443,3 @@ | ||
var b = new u128(255, 100); | ||
expect<u128>(a + b).toStrictEqual(new u128(355, 355)); | ||
expect(a + b).toStrictEqual(new u128(355, 355)); | ||
}); | ||
@@ -450,3 +450,3 @@ | ||
var b = u128.One; | ||
expect<u128>(a + b).toStrictEqual(u128.Max); | ||
expect(a + b).toStrictEqual(u128.Max); | ||
}); | ||
@@ -457,3 +457,3 @@ | ||
var b = new u128(100, 255); | ||
expect<u128>(a - b).toStrictEqual(new u128(255, 100)); | ||
expect(a - b).toStrictEqual(new u128(255, 100)); | ||
}); | ||
@@ -468,3 +468,3 @@ | ||
var a = new u128(1, 0); | ||
expect<u128>(a << 65).toStrictEqual(new u128(0, 2)); | ||
expect(a << 65).toStrictEqual(new u128(0, 2)); | ||
}); | ||
@@ -474,3 +474,3 @@ | ||
var a = new u128(1, 0); | ||
expect<u128>(a << (65 + 128)).toStrictEqual(new u128(0, 2)); | ||
expect(a << (65 + 128)).toStrictEqual(new u128(0, 2)); | ||
}); | ||
@@ -480,3 +480,3 @@ | ||
var a = new u128(1, 1); | ||
expect<u128>(a << 0).toStrictEqual(a); | ||
expect(a << 0).toStrictEqual(a); | ||
}); | ||
@@ -486,3 +486,3 @@ | ||
var a = new u128(0, 100); | ||
expect<u128>(a >> 65).toStrictEqual(new u128(50)); | ||
expect(a >> 65).toStrictEqual(new u128(50)); | ||
}); | ||
@@ -492,3 +492,3 @@ | ||
var a = new u128(0, 100); | ||
expect<u128>(a >> (65 + 128)).toStrictEqual(new u128(50)); | ||
expect(a >> (65 + 128)).toStrictEqual(new u128(50)); | ||
}); | ||
@@ -498,3 +498,3 @@ | ||
var a = new u128(1, 1); | ||
expect<u128>(a >> 0).toStrictEqual(a); | ||
expect(a >> 0).toStrictEqual(a); | ||
}); | ||
@@ -505,3 +505,3 @@ | ||
var b = u128.from(2353454354); | ||
expect<u128>(a * b).toStrictEqual(new u128(10248516654965971928, 5)); | ||
expect(a * b).toStrictEqual(new u128(10248516654965971928, 5)); | ||
}); | ||
@@ -512,3 +512,3 @@ | ||
var b = u128.One; | ||
expect<u128>(a * b).toStrictEqual(a); | ||
expect(a * b).toStrictEqual(a); | ||
}); | ||
@@ -518,3 +518,3 @@ | ||
var a = new u128(0, 1); | ||
expect<u128>(a * a).toStrictEqual(u128.Zero); | ||
expect(a * a).toStrictEqual(u128.Zero); | ||
}); | ||
@@ -524,7 +524,7 @@ | ||
var a = new u128(1, 1); | ||
expect<u128>(a * a).toStrictEqual(new u128(1, 2)); | ||
expect(a * a).toStrictEqual(new u128(1, 2)); | ||
}); | ||
it("Should negative number 1", () => { | ||
expect<u128>(-new u128(2)).toStrictEqual(u128.from(-2)); | ||
expect(-new u128(2)).toStrictEqual(u128.from(-2)); | ||
}); | ||
@@ -539,3 +539,3 @@ | ||
++a; | ||
expect<u128>(a).toStrictEqual(new u128(10248516654965971929, 5)); | ||
expect(a).toStrictEqual(new u128(10248516654965971929, 5)); | ||
}); | ||
@@ -546,3 +546,3 @@ | ||
++a; | ||
expect<u128>(a).toStrictEqual(new u128(0, 1)); | ||
expect(a).toStrictEqual(new u128(0, 1)); | ||
}); | ||
@@ -553,3 +553,3 @@ | ||
++a; | ||
expect<u128>(a).toStrictEqual(u128.One); | ||
expect(a).toStrictEqual(u128.One); | ||
}); | ||
@@ -560,3 +560,3 @@ | ||
++a; | ||
expect<u128>(a).toStrictEqual(u128.Max); | ||
expect(a).toStrictEqual(u128.Max); | ||
}); | ||
@@ -567,3 +567,3 @@ | ||
--a; | ||
expect<u128>(a).toStrictEqual(new u128(10248516654965971927, 5)); | ||
expect(a).toStrictEqual(new u128(10248516654965971927, 5)); | ||
}); | ||
@@ -574,3 +574,3 @@ | ||
--a; | ||
expect<u128>(a).toStrictEqual(new u128(0xFFFFFFFFFFFFFFFF, 0)); | ||
expect(a).toStrictEqual(new u128(0xFFFFFFFFFFFFFFFF, 0)); | ||
}); | ||
@@ -581,4 +581,4 @@ | ||
var t = a++; | ||
expect<u128>(t).toStrictEqual(new u128(0xFFFFFFFFFFFFFFFF, 0)); | ||
expect<u128>(a).toStrictEqual(new u128(0, 1)); | ||
expect(t).toStrictEqual(new u128(0xFFFFFFFFFFFFFFFF, 0)); | ||
expect(a).toStrictEqual(new u128(0, 1)); | ||
}); | ||
@@ -589,4 +589,4 @@ | ||
var t = a--; | ||
expect<u128>(t).toStrictEqual(new u128(0, 1)); | ||
expect<u128>(a).toStrictEqual(new u128(0xFFFFFFFFFFFFFFFF, 0)); | ||
expect(t).toStrictEqual(new u128(0, 1)); | ||
expect(a).toStrictEqual(new u128(0xFFFFFFFFFFFFFFFF, 0)); | ||
}); | ||
@@ -598,3 +598,3 @@ }); | ||
var a = u128.Zero; | ||
expect<u128>(a ** 0).toStrictEqual(u128.One); | ||
expect(a ** 0).toStrictEqual(u128.One); | ||
}); | ||
@@ -604,3 +604,3 @@ | ||
var a = u128.One; | ||
expect<u128>(a ** -2).toStrictEqual(u128.One); | ||
expect(a ** -2).toStrictEqual(u128.One); | ||
}); | ||
@@ -610,3 +610,3 @@ | ||
var a = new u128(-1, -1); | ||
expect<u128>(a ** -2).toStrictEqual(u128.Zero); | ||
expect(a ** -2).toStrictEqual(u128.Zero); | ||
}); | ||
@@ -616,3 +616,3 @@ | ||
var a = new u128(-1, -1); | ||
expect<u128>(a ** 0).toStrictEqual(u128.One); | ||
expect(a ** 0).toStrictEqual(u128.One); | ||
}); | ||
@@ -622,5 +622,5 @@ | ||
var a = u128.Zero; | ||
expect<u128>(a ** 10).toStrictEqual(u128.Zero); | ||
expect<u128>(a ** 1).toStrictEqual(u128.Zero); | ||
expect<u128>(a ** 2).toStrictEqual(u128.Zero); | ||
expect(a ** 10).toStrictEqual(u128.Zero); | ||
expect(a ** 1).toStrictEqual(u128.Zero); | ||
expect(a ** 2).toStrictEqual(u128.Zero); | ||
}); | ||
@@ -630,3 +630,3 @@ | ||
var a = new u128(-1, -1); | ||
expect<u128>(a ** 1).toStrictEqual(a); | ||
expect(a ** 1).toStrictEqual(a); | ||
}); | ||
@@ -636,3 +636,3 @@ | ||
var a = new u128(1); | ||
expect<u128>((a ** 2)).toStrictEqual((a * a)); | ||
expect((a ** 2)).toStrictEqual((a * a)); | ||
}); | ||
@@ -642,3 +642,3 @@ | ||
var a = new u128(0xFFFFFFFF); | ||
expect<u128>((a ** 2)).toStrictEqual((a * a)); | ||
expect((a ** 2)).toStrictEqual((a * a)); | ||
}); | ||
@@ -648,3 +648,3 @@ | ||
var a = new u128(0xFFFF); | ||
expect<u128>((a ** 2)).toStrictEqual(new u128(<u64>0xFFFF * <u64>0xFFFF)); | ||
expect((a ** 2)).toStrictEqual(new u128(<u64>0xFFFF * <u64>0xFFFF)); | ||
}); | ||
@@ -654,3 +654,3 @@ | ||
var a = new u128(0xFFFF - 1); | ||
expect<u128>((a ** 2)).toStrictEqual(new u128((0xFFFF - 1) * (0xFFFF - 1))); | ||
expect((a ** 2)).toStrictEqual(new u128((0xFFFF - 1) * (0xFFFF - 1))); | ||
}); | ||
@@ -660,3 +660,3 @@ | ||
var a = new u128(0, 1); | ||
expect<u128>((a ** 2)).toStrictEqual(u128.Zero); | ||
expect((a ** 2)).toStrictEqual(u128.Zero); | ||
}); | ||
@@ -666,3 +666,3 @@ | ||
var a = new u128(0, 3); | ||
expect<u128>((a ** 2)).toStrictEqual(u128.Zero); | ||
expect((a ** 2)).toStrictEqual(u128.Zero); | ||
}); | ||
@@ -672,3 +672,3 @@ | ||
var a = new u128(0xFFFF); | ||
expect<u128>((a ** 3)).toStrictEqual(new u128(0xFFFD0002FFFF)); | ||
expect((a ** 3)).toStrictEqual(new u128(0xFFFD0002FFFF)); | ||
}); | ||
@@ -678,3 +678,3 @@ | ||
var a = new u128(12345678); | ||
expect<u128>((a ** 3)).toStrictEqual(new u128(0x017FEC50E04509B8, 0x66)); | ||
expect((a ** 3)).toStrictEqual(new u128(0x017FEC50E04509B8, 0x66)); | ||
}); | ||
@@ -684,3 +684,3 @@ | ||
var a = new u128(1 << 20); | ||
expect<u128>((a ** 4)).toStrictEqual(new u128(0, 0x10000)); | ||
expect((a ** 4)).toStrictEqual(new u128(0, 0x10000)); | ||
}); | ||
@@ -690,3 +690,3 @@ | ||
var a = new u128((1 << 40) + 1); | ||
expect<u128>((a ** 3)).toStrictEqual(new u128(0x0000030000000001, 0x100000000030000)); | ||
expect((a ** 3)).toStrictEqual(new u128(0x0000030000000001, 0x100000000030000)); | ||
}); | ||
@@ -696,3 +696,3 @@ | ||
var a = new u128(0, 1); | ||
expect<u128>((a ** 4)).toStrictEqual(u128.Zero); | ||
expect((a ** 4)).toStrictEqual(u128.Zero); | ||
}); | ||
@@ -702,3 +702,3 @@ | ||
var a = new u128(123); | ||
expect<u128>((a ** 18)).toStrictEqual(new u128(0xB8C3F9BBD49E3CD9, 0x1F3D196F2C2AF26A)); | ||
expect((a ** 18)).toStrictEqual(new u128(0xB8C3F9BBD49E3CD9, 0x1F3D196F2C2AF26A)); | ||
}); | ||
@@ -708,3 +708,3 @@ | ||
var a = new u128(2); | ||
expect<u128>((a ** 127)).toStrictEqual(new u128(0, 0x8000000000000000)); | ||
expect((a ** 127)).toStrictEqual(new u128(0, 0x8000000000000000)); | ||
}); | ||
@@ -714,3 +714,3 @@ | ||
var a = new u128(3); | ||
expect<u128>((a ** 127)).toStrictEqual(new u128(0x2AC0B180838228AB, 0x4C1D8529A9294BCC)); | ||
expect((a ** 127)).toStrictEqual(new u128(0x2AC0B180838228AB, 0x4C1D8529A9294BCC)); | ||
}); | ||
@@ -720,3 +720,3 @@ | ||
var a = new u128(2); | ||
expect<u128>((a ** 128)).toStrictEqual(u128.Zero); | ||
expect((a ** 128)).toStrictEqual(u128.Zero); | ||
}); | ||
@@ -726,3 +726,3 @@ | ||
var a = u128.Zero; | ||
expect<u128>(u128.sqrt(a)).toStrictEqual(u128.Zero); | ||
expect(u128.sqrt(a)).toStrictEqual(u128.Zero); | ||
}); | ||
@@ -732,3 +732,3 @@ | ||
var a = u128.One; | ||
expect<u128>(u128.sqrt(a)).toStrictEqual(u128.One); | ||
expect(u128.sqrt(a)).toStrictEqual(u128.One); | ||
}); | ||
@@ -738,3 +738,3 @@ | ||
var a = new u128(3); | ||
expect<u128>(u128.sqrt(a)).toStrictEqual(new u128(1)); | ||
expect(u128.sqrt(a)).toStrictEqual(new u128(1)); | ||
}); | ||
@@ -744,3 +744,3 @@ | ||
var a = new u128(4); | ||
expect<u128>(u128.sqrt(a)).toStrictEqual(new u128(2)); | ||
expect(u128.sqrt(a)).toStrictEqual(new u128(2)); | ||
}); | ||
@@ -750,3 +750,3 @@ | ||
var a = new u128(5); | ||
expect<u128>(u128.sqrt(a)).toStrictEqual(new u128(2)); | ||
expect(u128.sqrt(a)).toStrictEqual(new u128(2)); | ||
}); | ||
@@ -756,3 +756,3 @@ | ||
var a = new u128(5); | ||
expect<u128>(u128.sqrt(a)).toStrictEqual(new u128(2)); | ||
expect(u128.sqrt(a)).toStrictEqual(new u128(2)); | ||
}); | ||
@@ -762,3 +762,3 @@ | ||
var a = new u128(9); | ||
expect<u128>(u128.sqrt(a)).toStrictEqual(new u128(3)); | ||
expect(u128.sqrt(a)).toStrictEqual(new u128(3)); | ||
}); | ||
@@ -768,3 +768,3 @@ | ||
var a = new u128(64); | ||
expect<u128>(u128.sqrt(a)).toStrictEqual(new u128(8)); | ||
expect(u128.sqrt(a)).toStrictEqual(new u128(8)); | ||
}); | ||
@@ -774,3 +774,3 @@ | ||
var a = new u128(1000); | ||
expect<u128>(u128.sqrt(a)).toStrictEqual(new u128(31)); | ||
expect(u128.sqrt(a)).toStrictEqual(new u128(31)); | ||
}); | ||
@@ -780,3 +780,3 @@ | ||
var a = u128.Max; | ||
expect<u128>(u128.sqrt(a)).toStrictEqual(new u128(u64.MAX_VALUE)); | ||
expect(u128.sqrt(a)).toStrictEqual(new u128(u64.MAX_VALUE)); | ||
}); | ||
@@ -787,59 +787,59 @@ }); | ||
it("Should popcount 1", () => { | ||
expect<i32>(u128.popcnt(u128.Zero)).toBe(0); | ||
expect(u128.popcnt(u128.Zero)).toBe(0); | ||
}); | ||
it("Should popcount 2", () => { | ||
expect<i32>(u128.popcnt(new u128(1))).toBe(1); | ||
expect(u128.popcnt(new u128(1))).toBe(1); | ||
}); | ||
it("Should popcount 3", () => { | ||
expect<i32>(u128.popcnt(new u128(1, 1))).toBe(2); | ||
expect(u128.popcnt(new u128(1, 1))).toBe(2); | ||
}); | ||
it("Should popcount 4", () => { | ||
expect<i32>(u128.popcnt(new u128(0, 1))).toBe(1); | ||
expect(u128.popcnt(new u128(0, 1))).toBe(1); | ||
}); | ||
it("Should popcount 5", () => { | ||
expect<i32>(u128.popcnt(new u128(-1, -1))).toBe(128); | ||
expect(u128.popcnt(new u128(-1, -1))).toBe(128); | ||
}); | ||
it("Should count leading zeros 1", () => { | ||
expect<i32>(u128.clz(u128.Zero)).toBe(128); | ||
expect(u128.clz(u128.Zero)).toBe(128); | ||
}); | ||
it("Should count leading zeros 2", () => { | ||
expect<i32>(u128.clz(u128.One)).toBe(127); | ||
expect(u128.clz(u128.One)).toBe(127); | ||
}); | ||
it("Should count leading zeros 3", () => { | ||
expect<i32>(u128.clz(new u128(0, 1))).toBe(63); | ||
expect(u128.clz(new u128(0, 1))).toBe(63); | ||
}); | ||
it("Should count leading zeros 4", () => { | ||
expect<i32>(u128.clz(new u128(-1, -1))).toBe(0); | ||
expect(u128.clz(new u128(-1, -1))).toBe(0); | ||
}); | ||
it("Should count trailing zeros 1", () => { | ||
expect<i32>(u128.ctz(u128.Zero)).toBe(128); | ||
expect(u128.ctz(u128.Zero)).toBe(128); | ||
}); | ||
it("Should count trailing zeros 2", () => { | ||
expect<i32>(u128.ctz(u128.One)).toBe(0); | ||
expect(u128.ctz(u128.One)).toBe(0); | ||
}); | ||
it("Should count trailing zeros 3", () => { | ||
expect<i32>(u128.ctz(new u128(2))).toBe(1); | ||
expect(u128.ctz(new u128(2))).toBe(1); | ||
}); | ||
it("Should count trailing zeros 4", () => { | ||
expect<i32>(u128.ctz(new u128(0, 1))).toBe(64); | ||
expect(u128.ctz(new u128(0, 1))).toBe(64); | ||
}); | ||
it("Should count trailing zeros 5", () => { | ||
expect<i32>(u128.ctz(new u128(-1, -1))).toBe(0); | ||
expect(u128.ctz(new u128(-1, -1))).toBe(0); | ||
}); | ||
it("Should count trailing zeros 6", () => { | ||
expect<i32>(u128.ctz(new u128(0, 0x8000000000000000))).toBe(127); | ||
expect(u128.ctz(new u128(0, 0x8000000000000000))).toBe(127); | ||
}); | ||
@@ -852,3 +852,3 @@ }); | ||
let b = u128.from(2353454354); | ||
expect<u128>(a / b).toStrictEqual(u128.from(43545453453)); | ||
expect(a / b).toStrictEqual(u128.from(43545453453)); | ||
}); | ||
@@ -859,3 +859,3 @@ | ||
let b = u128.from(43545453452); | ||
expect<u128>(a / b).toStrictEqual(u128.from(2353454354)); | ||
expect(a / b).toStrictEqual(u128.from(2353454354)); | ||
}); | ||
@@ -866,3 +866,3 @@ | ||
let b = u128.from(4354545345312); | ||
expect<u128>(a / b).toStrictEqual(u128.from(9196400)); | ||
expect(a / b).toStrictEqual(u128.from(9196400)); | ||
}); | ||
@@ -873,3 +873,3 @@ | ||
let b = u128.from(43543534534534); | ||
expect<u128>(a / b).toStrictEqual(u128.from(919680)); | ||
expect(a / b).toStrictEqual(u128.from(919680)); | ||
}); | ||
@@ -880,3 +880,3 @@ | ||
let b = u128.from(43543534534534); | ||
expect<u128>(a / b).toStrictEqual(u128.from(72)); | ||
expect(a / b).toStrictEqual(u128.from(72)); | ||
}); | ||
@@ -887,3 +887,3 @@ | ||
let b = new u128(10248516654965971928, 5); | ||
expect<u128>(a / b).toStrictEqual(u128.Zero); | ||
expect(a / b).toStrictEqual(u128.Zero); | ||
}); | ||
@@ -894,3 +894,3 @@ | ||
let b = u128.One; | ||
expect<u128>(a / b).toStrictEqual(a); | ||
expect(a / b).toStrictEqual(a); | ||
}); | ||
@@ -901,3 +901,3 @@ | ||
let b = u128.from(2353454354); | ||
expect<u128>(a % b).toStrictEqual(u128.Zero); | ||
expect(a % b).toStrictEqual(u128.Zero); | ||
}); | ||
@@ -908,3 +908,3 @@ | ||
let b = u128.from(43545453452); | ||
expect<u128>(a % b).toStrictEqual(u128.Zero); | ||
expect(a % b).toStrictEqual(u128.Zero); | ||
}); | ||
@@ -915,3 +915,3 @@ | ||
let b = u128.from(43543534534534); | ||
expect<u128>(a % b).toStrictEqual(u128.from(22972907047680)); | ||
expect(a % b).toStrictEqual(u128.from(22972907047680)); | ||
}); | ||
@@ -922,3 +922,3 @@ | ||
let b = u128.from(43543534534534); | ||
expect<u128>(a % b).toStrictEqual(u128.from(17518179721730)); | ||
expect(a % b).toStrictEqual(u128.from(17518179721730)); | ||
}); | ||
@@ -929,3 +929,3 @@ | ||
let b = u128.One; | ||
expect<u128>(a % b).toStrictEqual(u128.Zero); | ||
expect(a % b).toStrictEqual(u128.Zero); | ||
}); | ||
@@ -935,3 +935,3 @@ | ||
let a = new u128(10248516654965971928, 5); | ||
expect<u128>(a % a).toStrictEqual(u128.Zero); | ||
expect(a % a).toStrictEqual(u128.Zero); | ||
}); | ||
@@ -941,3 +941,3 @@ | ||
let a = new u128(10248516654965971928, 5); | ||
expect<u128>(a / a).toStrictEqual(u128.One); | ||
expect(a / a).toStrictEqual(u128.One); | ||
}); | ||
@@ -948,3 +948,3 @@ }); | ||
it("Should throw from string with unsupported radix 1", () => { | ||
expectFn(() => { | ||
expect(() => { | ||
!(u128.fromString('0000', 1)); | ||
@@ -955,3 +955,3 @@ }).toThrow(); | ||
it("Should throw from string with unsupported radix 2", () => { | ||
expectFn(() => { | ||
expect(() => { | ||
!(u128.fromString('1234', 37)); | ||
@@ -962,3 +962,3 @@ }).toThrow(); | ||
it("Should throw from bytes with null 1", () => { | ||
expectFn(() => { | ||
expect(() => { | ||
!(u128.fromBytes(changetype<u8[]>(null))); | ||
@@ -969,3 +969,3 @@ }).toThrow(); | ||
it("Should throw from bytes with null 2", () => { | ||
expectFn(() => { | ||
expect(() => { | ||
!(u128.fromBytes(changetype<u8[]>(null), true)); | ||
@@ -976,3 +976,3 @@ }).toThrow(); | ||
it("Should throw from bytes with wrong byte array length 1", () => { | ||
expectFn(() => { | ||
expect(() => { | ||
!(u128.fromBytes<u8[]>([])); | ||
@@ -983,3 +983,3 @@ }).toThrow(); | ||
it("Should throw from bytes with wrong byte array length 2", () => { | ||
expectFn(() => { | ||
expect(() => { | ||
!(u128.fromBytes<u8[]>([], true)); | ||
@@ -990,3 +990,3 @@ }).toThrow(); | ||
it("Should throw from bytes with wrong byte array length 3", () => { | ||
expectFn(() => { | ||
expect(() => { | ||
!(u128.fromBytes<u8[]>([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17])); | ||
@@ -997,3 +997,3 @@ }).toThrow(); | ||
it("Should throw from bytes with wrong byte array length 4", () => { | ||
expectFn(() => { | ||
expect(() => { | ||
!(u128.fromBytes<u8[]>([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17], true)); | ||
@@ -1004,3 +1004,3 @@ }).toThrow(); | ||
it("Should throw from bytes with wrong byte array length 5", () => { | ||
expectFn(() => { | ||
expect(() => { | ||
!(u128.fromBytes<Uint8Array>(new Uint8Array(0))); | ||
@@ -1011,3 +1011,3 @@ }).toThrow(); | ||
it("Should throw from bytes with wrong byte array length 6", () => { | ||
expectFn(() => { | ||
expect(() => { | ||
!(u128.fromBytes<Uint8Array>(new Uint8Array(0), true)); | ||
@@ -1018,3 +1018,3 @@ }).toThrow(); | ||
it("Should throw from bytes with wrong byte array length 7", () => { | ||
expectFn(() => { | ||
expect(() => { | ||
let arr = new Uint8Array(18); | ||
@@ -1029,3 +1029,3 @@ for (let i = 0; i < 18; i++) { | ||
it("Should throw from bytes with wrong byte array length 8", () => { | ||
expectFn(() => { | ||
expect(() => { | ||
let arr = new Uint8Array(18); | ||
@@ -1040,3 +1040,3 @@ for (let i = 0; i < 18; i++) { | ||
it("Should throw when divide number by zero", () => { | ||
expectFn(() => { | ||
expect(() => { | ||
let a = new u128(1, 1); | ||
@@ -1048,3 +1048,3 @@ !(a / u128.Zero); | ||
it("Should throw when mod number by zero", () => { | ||
expectFn(() => { | ||
expect(() => { | ||
let a = new u128(1, 1); | ||
@@ -1051,0 +1051,0 @@ !(a % u128.Zero); |
@@ -7,3 +7,3 @@ import { u256 } from '../../assembly/integer/u256'; | ||
var a = new u256(10248516654965971928, 5, 0, 0); | ||
expect<string>('102482237023513730008').toStrictEqual(a.toString()); | ||
expect('102482237023513730008').toStrictEqual(a.toString()); | ||
}); | ||
@@ -13,3 +13,3 @@ | ||
var a = new u256(1, 1, 1, 1); | ||
expect<string>('6277101735386680764176071790128604879584176795969512275969').toStrictEqual(a.toString()); | ||
expect('6277101735386680764176071790128604879584176795969512275969').toStrictEqual(a.toString()); | ||
}); | ||
@@ -19,3 +19,3 @@ | ||
var a = u256.Max; | ||
expect<string>('115792089237316195423570985008687907853269984665640564039457584007913129639935').toStrictEqual(a.toString()); | ||
expect('115792089237316195423570985008687907853269984665640564039457584007913129639935').toStrictEqual(a.toString()); | ||
}); | ||
@@ -25,3 +25,3 @@ | ||
var a = u256.Zero; | ||
expect<string>('0').toStrictEqual(a.toString()); | ||
expect('0').toStrictEqual(a.toString()); | ||
}); | ||
@@ -38,3 +38,3 @@ }); | ||
]; | ||
expect<u256>(u256.fromBytes(arr)).toStrictEqual(new u256(0x8877665544332211, 0x12ffeeddccbbaa99, 0x8877665544332211, 0x12ffeeddccbbaa99)); | ||
expect(u256.fromBytes(arr)).toStrictEqual(new u256(0x8877665544332211, 0x12ffeeddccbbaa99, 0x8877665544332211, 0x12ffeeddccbbaa99)); | ||
}); | ||
@@ -50,3 +50,3 @@ | ||
expect<u256>(u256.fromBytes(arr)).toStrictEqual(new u256(0x8877665544332211, 0x12ffeeddccbbaa99, 0x8877665544332211, 0x12ffeeddccbbaa99)); | ||
expect(u256.fromBytes(arr)).toStrictEqual(new u256(0x8877665544332211, 0x12ffeeddccbbaa99, 0x8877665544332211, 0x12ffeeddccbbaa99)); | ||
}); | ||
@@ -62,3 +62,3 @@ | ||
expect<u256>(u256.fromBytes(arr, true)).toStrictEqual(new u256(0x99aabbccddeeff12, 0x1122334455667788, 0x99aabbccddeeff12, 0x1122334455667788)); | ||
expect(u256.fromBytes(arr, true)).toStrictEqual(new u256(0x99aabbccddeeff12, 0x1122334455667788, 0x99aabbccddeeff12, 0x1122334455667788)); | ||
}); | ||
@@ -74,3 +74,3 @@ | ||
expect<u256>(u256.fromBytes(arr, true)).toStrictEqual(new u256(0x99aabbccddeeff12, 0x1122334455667788, 0x99aabbccddeeff12, 0x1122334455667788)); | ||
expect(u256.fromBytes(arr, true)).toStrictEqual(new u256(0x99aabbccddeeff12, 0x1122334455667788, 0x99aabbccddeeff12, 0x1122334455667788)); | ||
}); | ||
@@ -81,3 +81,3 @@ | ||
var a = u.toBytes(); | ||
expect<bool>( | ||
expect( | ||
a[0] == 0x11 && a[1] == 0x22 && a[2] == 0x33 && a[3] == 0x44 && | ||
@@ -97,3 +97,3 @@ a[4] == 0x55 && a[5] == 0x66 && a[6] == 0x77 && a[7] == 0x88 && | ||
var a = u.toUint8Array(); | ||
expect<bool>( | ||
expect( | ||
a[0] == 0x11 && a[1] == 0x22 && a[2] == 0x33 && a[3] == 0x44 && | ||
@@ -113,3 +113,3 @@ a[4] == 0x55 && a[5] == 0x66 && a[6] == 0x77 && a[7] == 0x88 && | ||
var a = u.toBytes(true); | ||
expect<bool>( | ||
expect( | ||
a[0] == 0x11 && a[1] == 0x22 && a[2] == 0x33 && a[3] == 0x44 && | ||
@@ -129,3 +129,3 @@ a[4] == 0x55 && a[5] == 0x66 && a[6] == 0x77 && a[7] == 0x88 && | ||
var a = u.toUint8Array(true); | ||
expect<bool>( | ||
expect( | ||
a[0] == 0x11 && a[1] == 0x22 && a[2] == 0x33 && a[3] == 0x44 && | ||
@@ -146,3 +146,3 @@ a[4] == 0x55 && a[5] == 0x66 && a[6] == 0x77 && a[7] == 0x88 && | ||
var a = u256.Zero; | ||
expect<bool>(a.isZero()).toBe(true); | ||
expect(a.isZero()).toBe(true); | ||
}); | ||
@@ -152,3 +152,3 @@ | ||
var a = u256.One; | ||
expect<bool>(!a.isZero()).toBe(true); | ||
expect(!a.isZero()).toBe(true); | ||
}); | ||
@@ -158,3 +158,3 @@ | ||
var a = new u256(1, 2, 3, 4); | ||
expect<u256>(~a).toStrictEqual(new u256(~1, ~2, ~3, ~4)); | ||
expect(~a).toStrictEqual(new u256(~1, ~2, ~3, ~4)); | ||
}); | ||
@@ -164,3 +164,3 @@ | ||
var a = new u256(1, 2, 3, 4); | ||
expect<u256>(a).toStrictEqual(+a); | ||
expect(a).toStrictEqual(+a); | ||
}); | ||
@@ -170,3 +170,3 @@ | ||
var a = u256.One; | ||
expect<bool>(!!a).toBe(true); | ||
expect(!!a).toBe(true); | ||
}); | ||
@@ -176,4 +176,4 @@ | ||
var a = u256.Zero; | ||
expect<bool>(!a).toBe(true); | ||
expect(!a).toBe(true); | ||
}); | ||
}); |
export function arrayToUint8Array(arr: u8[]): Uint8Array { | ||
let len = arr.length; | ||
let res = new Uint8Array(len); | ||
// @ts-ignore | ||
memory.copy(res.dataStart, arr.dataStart, len); | ||
return res; | ||
} |
@@ -31,3 +31,3 @@ import { u256 } from './u256'; | ||
static fromI256(value: i256): u128 { | ||
return new u128(value.lo1, value.lo2); | ||
return changetype<u128>(U128.fromI256(value)); | ||
} | ||
@@ -37,3 +37,3 @@ | ||
static fromU256(value: u256): u128 { | ||
return new u128(value.lo1, value.lo2); | ||
return changetype<u128>(U128.fromU256(value)); | ||
} | ||
@@ -43,3 +43,3 @@ | ||
static fromI128(value: i128): u128 { | ||
return new u128(value.lo, value.hi); | ||
return changetype<u128>(U128.fromI128(value)); | ||
} | ||
@@ -49,3 +49,3 @@ | ||
static fromU128(value: u128): u128 { | ||
return new u128(value.lo, value.hi); | ||
return changetype<u128>(U128.fromU128(value)); | ||
} | ||
@@ -55,3 +55,3 @@ | ||
static fromI64(value: i64): u128 { | ||
return new u128(value, value >> 63); | ||
return changetype<u128>(U128.fromI64(value)); | ||
} | ||
@@ -61,17 +61,13 @@ | ||
static fromU64(value: u64): u128 { | ||
return new u128(value); | ||
return changetype<u128>(U128.fromU64(value)); | ||
} | ||
// TODO need improvement | ||
// max safe uint for f64 actually 53-bits | ||
@inline | ||
static fromF64(value: f64): u128 { | ||
return new u128(<u64>value, -(value < 0)); | ||
return changetype<u128>(U128.fromF64(value)); | ||
} | ||
// TODO need improvement | ||
// max safe int for f32 actually 23-bits | ||
@inline | ||
static fromF32(value: f32): u128 { | ||
return new u128(<u64>value, -(value < 0)); | ||
return changetype<u128>(U128.fromF32(value)); | ||
} | ||
@@ -81,4 +77,3 @@ | ||
static fromI32(value: i32): u128 { | ||
// Workaround. See issue #247 in AS repositary | ||
return new u128(<i64>value, <i64>value >> 63); | ||
return changetype<u128>(U128.fromI32(value)); | ||
} | ||
@@ -88,3 +83,3 @@ | ||
static fromU32(value: u32): u128 { | ||
return new u128(value); | ||
return changetype<u128>(U128.fromU32(value)); | ||
} | ||
@@ -94,3 +89,3 @@ | ||
static fromBool(value: bool): u128 { | ||
return new u128(<u64>value); | ||
return changetype<u128>(U128.fromBool(value)); | ||
} | ||
@@ -100,6 +95,3 @@ | ||
static fromBits(lo1: u32, lo2: u32, hi1: u32, hi2: u32): u128 { | ||
return new u128( | ||
<u64>lo1 | ((<u64>lo2) << 32), | ||
<u64>hi1 | ((<u64>hi2) << 32), | ||
); | ||
return changetype<u128>(U128.fromBits(lo1, lo2, hi1, hi2)); | ||
} | ||
@@ -109,21 +101,13 @@ | ||
static fromBytes(array: u8[], bigEndian: bool = false): u128 { | ||
return bigEndian ? u128.fromBytesBE(array) : u128.fromBytesLE(array); | ||
return changetype<u128>(U128.fromBytes(array, bigEndian)); | ||
} | ||
@inline | ||
static fromBytesLE(array: u8[]): u128 { | ||
assert(array.length && (array.length & 15) == 0); | ||
var buffer = array.dataStart; | ||
return new u128( | ||
load<u64>(buffer, 0), | ||
load<u64>(buffer, 1 * sizeof<u64>()) | ||
); | ||
return changetype<u128>(U128.fromBytesLE(array)); | ||
} | ||
@inline | ||
static fromBytesBE(array: u8[]): u128 { | ||
assert(array.length && (array.length & 15) == 0); | ||
var buffer = array.dataStart; | ||
return new u128( | ||
bswap<u64>(load<u64>(buffer, 1 * sizeof<u64>())), | ||
bswap<u64>(load<u64>(buffer, 0)) | ||
); | ||
return changetype<u128>(U128.fromBytesBE(array)); | ||
} | ||
@@ -151,2 +135,3 @@ | ||
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.fromI256(<i256>value); | ||
@@ -159,23 +144,41 @@ else if (value instanceof u256) return u128.fromU256(<u256>value); | ||
@inline @operator.prefix('++') | ||
@operator.prefix('++') | ||
preInc(): this { | ||
assert(this.lo != <u64>-1 && this.hi != <u64>-1, "Overflow during prefix incrementing"); | ||
// TODO | ||
// super.preInc(); | ||
if ((this.lo & this.hi) == <u64>-1) { // if this == max | ||
throw new Error('Overflow during prefix incrementing'); | ||
} | ||
super.preInc(); | ||
return this; | ||
} | ||
@inline @operator.prefix('--') | ||
@operator.prefix('--') | ||
preDec(): this { | ||
assert(this.hi != 0 && this.lo != 0, "Overflow during prefix decrementing"); | ||
// TODO | ||
// super.preDec(); | ||
if ((this.lo | this.hi) == 0) { // if this == 0 | ||
throw new Error('Underflow during prefix decrementing'); | ||
} | ||
super.preDec(); | ||
return this; | ||
} | ||
@inline @operator('+') | ||
@operator.postfix('++') | ||
postInc(): u128 { | ||
if ((this.lo & this.hi) == <u64>-1) { // if this == max | ||
throw new Error('Overflow during prefix incrementing'); | ||
} | ||
return this.clone().preInc(); | ||
} | ||
@operator.postfix('--') | ||
postDec(): u128 { | ||
if ((this.lo | this.hi) == 0) { // if this == 0 | ||
throw new Error('Underflow during prefix decrementing'); | ||
} | ||
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 c = u64(lo < bl); | ||
var x = a.hi; | ||
@@ -190,6 +193,7 @@ var y = b.hi; | ||
@inline @operator('-') | ||
@operator('-') | ||
static sub(a: u128, b: u128): u128 { | ||
// underflow guard | ||
assert(a >= b, "Overflow during substraction"); | ||
if (a < b) { | ||
throw new Error("Underflow during substraction"); | ||
} | ||
return changetype<u128>( | ||
@@ -200,6 +204,31 @@ U128.sub(changetype<U128>(a), changetype<U128>(b)) | ||
@inline @operator('*') | ||
@operator('*') | ||
static mul(a: u128, b: u128): u128 { | ||
// overflow guard | ||
assert(u128.clz(a) + u128.clz(b) >= 127, "Overflow during multiply"); | ||
if (a.isZero() || b.isZero()) { | ||
return u128.Zero; | ||
} | ||
var s = u128.clz(a) + u128.clz(b); | ||
if (s < 127) { // defenitely overflow | ||
throw new Error("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 Error("Overflow during multiplication"); | ||
} | ||
// @ts-ignore | ||
let z = tmp << 1; | ||
if (b.lo & 1) { | ||
// @ts-ignore | ||
z += a; | ||
// @ts-ignore | ||
if (z < a) { | ||
throw new Error("Overflow during multiplication"); | ||
} | ||
} | ||
return changetype<u128>(z); | ||
} | ||
return changetype<u128>( | ||
@@ -210,7 +239,7 @@ U128.mul(changetype<U128>(a), changetype<U128>(b)) | ||
@inline @operator('**') | ||
@operator('**') | ||
static pow(base: u128, exponent: i32): u128 { | ||
if (isPowerOverflow128(base, exponent)) | ||
throw new Error("Overflow during power"); | ||
if (isPowerOverflow128(base, exponent)) { | ||
throw new Error("Overflow during exponentiation"); | ||
} | ||
return changetype<u128>(U128.pow(changetype<U128>(base), exponent)); | ||
@@ -221,3 +250,3 @@ } | ||
toUnchecked(): U128 { | ||
return <U128>this; | ||
return changetype<U128>(this); | ||
} | ||
@@ -240,8 +269,8 @@ | ||
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.toU128()); | ||
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 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 String) return <T>this.toString(); | ||
@@ -255,2 +284,3 @@ else throw new TypeError('Unsupported generic type'); | ||
@inline | ||
clone(): u128 { | ||
@@ -257,0 +287,0 @@ return new u128(this.lo, this.hi); |
@@ -73,3 +73,3 @@ import { i128 } from './i128'; | ||
static fromF64(value: f64): u128 { | ||
return new u128(<u64>value, -(value < 0)); | ||
return new u128(<u64>value, reinterpret<i64>(value) >> 63); | ||
} | ||
@@ -81,3 +81,3 @@ | ||
static fromF32(value: f32): u128 { | ||
return new u128(<u64>value, -(value < 0)); | ||
return new u128(<u64>value, reinterpret<i32>(value) >> 31); | ||
} | ||
@@ -111,5 +111,9 @@ | ||
if (array instanceof u8[]) { | ||
return bigEndian ? u128.fromBytesBE(<u8[]>array) : u128.fromBytesLE(<u8[]>array); | ||
return bigEndian | ||
? u128.fromBytesBE(<u8[]>array) | ||
: u128.fromBytesLE(<u8[]>array); | ||
} else if (array instanceof Uint8Array) { | ||
return bigEndian ? u128.fromUint8ArrayBE(<Uint8Array>array) : u128.fromUint8ArrayLE(<Uint8Array>array); | ||
return bigEndian | ||
? u128.fromUint8ArrayBE(<Uint8Array>array) | ||
: u128.fromUint8ArrayLE(<Uint8Array>array); | ||
} else { | ||
@@ -122,5 +126,6 @@ throw new TypeError("Unsupported generic type"); | ||
assert(array.length && (array.length & 15) == 0); | ||
// @ts-ignore | ||
var buffer = array.dataStart; | ||
return new u128( | ||
load<u64>(buffer, 0), | ||
load<u64>(buffer, 0 * sizeof<u64>()), | ||
load<u64>(buffer, 1 * sizeof<u64>()) | ||
@@ -132,2 +137,3 @@ ); | ||
assert(array.length && (array.length & 15) == 0); | ||
// @ts-ignore | ||
var buffer = array.dataStart; | ||
@@ -142,6 +148,7 @@ return new u128( | ||
assert(array.length && (array.length & 15) == 0); | ||
var buffer = array.dataStart | ||
// @ts-ignore | ||
var buffer = array.dataStart; | ||
return new u128( | ||
load<u64>(buffer, 0), | ||
load<u64>(buffer, 1 * sizeof<u64>()) | ||
load<u64>(buffer, 0 * sizeof<u64>()), | ||
load<u64>(buffer, 1 * sizeof<u64>()) | ||
); | ||
@@ -152,6 +159,7 @@ } | ||
assert(array.length && (array.length & 15) == 0); | ||
var buffer = array.dataStart | ||
// @ts-ignore | ||
var buffer = array.dataStart; | ||
return new u128( | ||
bswap<u64>(load<u64>(buffer, 1 * sizeof<u64>())), | ||
bswap<u64>(load<u64>(buffer, 0 * sizeof<u64>())) | ||
bswap<u64>(load<u64>(buffer, 1 * sizeof<u64>())), | ||
bswap<u64>(load<u64>(buffer, 0 * sizeof<u64>())) | ||
); | ||
@@ -251,4 +259,4 @@ } | ||
var lo = ~this.lo, hi = ~this.hi; | ||
var cy = ((lo & 1) + (lo >> 1)) >> 63; | ||
return new u128(lo + 1, hi + cy); | ||
var lo1 = lo + 1; | ||
return new u128(lo1, hi + u64(lo1 < lo)); | ||
} | ||
@@ -258,5 +266,6 @@ | ||
preInc(): this { | ||
var tmp = this.lo + 1; | ||
this.hi += ((this.lo ^ tmp) & this.lo) >> 63; | ||
this.lo = tmp; | ||
var lo = this.lo; | ||
var lo1 = lo + 1; | ||
this.hi += u64(lo1 < lo); | ||
this.lo = lo1; | ||
return this; | ||
@@ -267,5 +276,6 @@ } | ||
preDec(): this { | ||
var tmp = this.lo - 1; | ||
this.hi -= ((this.lo ^ tmp) & tmp) >> 63; | ||
this.lo = tmp; | ||
var lo = this.lo; | ||
var lo1 = lo - 1; | ||
this.hi -= u64(lo1 > lo); | ||
this.lo = lo1; | ||
return this; | ||
@@ -427,3 +437,3 @@ } | ||
var lo = a.lo + bl; | ||
var hi = a.hi + b.hi + (<u64>(lo < bl)); | ||
var hi = a.hi + b.hi + u64(lo < bl); | ||
@@ -437,3 +447,3 @@ return new u128(lo, hi); | ||
var lo = al - b.lo; | ||
var hi = a.hi - b.hi - (<u64>(lo > al)); | ||
var hi = a.hi - b.hi - u64(lo > al); | ||
@@ -510,2 +520,3 @@ return new u128(lo, hi); | ||
let shift = <i32>(64 - clz(lo1)) * exponent; | ||
// @ts-ignore | ||
return shift < 128 ? u128.One << shift : u128.Zero; | ||
@@ -519,2 +530,3 @@ } | ||
case 2: return sqrbase; // base ^ 2 | ||
// @ts-ignore | ||
case 3: return sqrbase * base; // base ^ 2 * base | ||
@@ -531,2 +543,3 @@ case 4: return sqrbase.sqr(); // base ^ 2 * base ^ 2 | ||
case 7: | ||
// @ts-ignore | ||
if (exponent & 1) result *= tmp; | ||
@@ -536,2 +549,3 @@ exponent >>= 1; | ||
case 6: | ||
// @ts-ignore | ||
if (exponent & 1) result *= tmp; | ||
@@ -541,2 +555,3 @@ exponent >>= 1; | ||
case 5: | ||
// @ts-ignore | ||
if (exponent & 1) result *= tmp; | ||
@@ -546,2 +561,3 @@ exponent >>= 1; | ||
case 4: | ||
// @ts-ignore | ||
if (exponent & 1) result *= tmp; | ||
@@ -551,2 +567,3 @@ exponent >>= 1; | ||
case 3: | ||
// @ts-ignore | ||
if (exponent & 1) result *= tmp; | ||
@@ -556,2 +573,3 @@ exponent >>= 1; | ||
case 2: | ||
// @ts-ignore | ||
if (exponent & 1) result *= tmp; | ||
@@ -561,2 +579,3 @@ exponent >>= 1; | ||
case 1: | ||
// @ts-ignore | ||
if (exponent & 1) result *= tmp; | ||
@@ -569,2 +588,3 @@ } | ||
while (exponent > 0) { | ||
// @ts-ignore | ||
if (exponent & 1) result *= tmp; | ||
@@ -587,3 +607,5 @@ exponent >>= 1; | ||
tmp.setU64(res | add); | ||
// @ts-ignore | ||
let sqr = tmp * tmp; | ||
// @ts-ignore | ||
if (value >= sqr) { | ||
@@ -629,7 +651,15 @@ res = tmp.lo; | ||
/** | ||
* Get ordering | ||
* if a > b then result is greater than 0 | ||
* if a < b then result is lesser than 0 | ||
* if a = b then result is eqal to 0 | ||
* @param a 128-bit unsigned integer | ||
* @param b 128-bit unsigned integer | ||
* @returns 32-bit signed integer | ||
*/ | ||
@inline | ||
static cmp(a: u128, b: u128): i32 { | ||
static ord(a: u128, b: u128): i32 { | ||
var dlo: i64 = a.lo - b.lo; | ||
var dhi: i64 = a.hi - b.hi; | ||
// return <i32>(dhi != 0 ? dhi : dlo); | ||
return <i32>select<i64>(dhi, dlo, dhi != 0); | ||
@@ -673,2 +703,3 @@ } | ||
*/ | ||
@inline | ||
static sqr(value: u128): u128 { | ||
@@ -808,21 +839,2 @@ return value.clone().sqr(); | ||
// Simpler and faster alternative of "toF64" | ||
// but non-deteministic (using float point arithmetics) | ||
toF64Unsafe(): f64 { | ||
const shift = reinterpret<f64>(0x43F0000000000000); // 2 ^ 64 | ||
var lo = this.lo, hi = this.hi; | ||
if (hi >= 0) | ||
return <f64>hi * shift + <f64>lo; | ||
var rh: i64 = ~hi; | ||
var rl: u64 = ~lo; | ||
var cy = ((rl & 1) + (rl >> 1)) >> 63; | ||
rl += 1; | ||
rh += cy; | ||
return -(<f64>rh * shift + <f64>rl); | ||
} | ||
/** | ||
@@ -857,3 +869,3 @@ * Convert to 32-bit float number | ||
else if (dummy instanceof i128) return <T>this.toI128(); | ||
else if (dummy instanceof u128) return <T>this.toU128(); | ||
else if (dummy instanceof u128) return <T>this; | ||
else if (dummy instanceof u256) return <T>this.toU256(); | ||
@@ -866,12 +878,15 @@ else if (dummy instanceof u8[]) return <T>this.toBytes(); | ||
@inline | ||
private toArrayBufferLE(buffer: usize): void { | ||
store<u64>(buffer, this.lo, 0); | ||
store<u64>(buffer, this.lo, 0 * sizeof<u64>()); | ||
store<u64>(buffer, this.hi, 1 * sizeof<u64>()); | ||
} | ||
@inline | ||
private toArrayBufferBE(buffer: usize): void { | ||
store<u64>(buffer, bswap(this.hi), 0); | ||
store<u64>(buffer, bswap(this.hi), 0 * sizeof<u64>()); | ||
store<u64>(buffer, bswap(this.lo), 1 * sizeof<u64>()); | ||
} | ||
@inline | ||
private toArrayBuffer(buffer: usize, bigEndian: bool = false): void { | ||
@@ -893,4 +908,4 @@ if (bigEndian) { | ||
var result = new Array<u8>(16); | ||
var buffer = result.dataStart | ||
this.toArrayBuffer(buffer, bigEndian); | ||
// @ts-ignore | ||
this.toArrayBuffer(result.dataStart, bigEndian); | ||
return result; | ||
@@ -907,4 +922,4 @@ } | ||
var result = new Uint8Array(16); | ||
var buffer = result.dataStart | ||
this.toArrayBuffer(buffer, bigEndian); | ||
// @ts-ignore | ||
this.toArrayBuffer(result.dataStart, bigEndian); | ||
return result; | ||
@@ -933,2 +948,3 @@ } | ||
while (shift >= 0) { | ||
// @ts-ignore | ||
it >>= shift; | ||
@@ -935,0 +951,0 @@ result = result.concat(HEX_CHARS.charAt(<i32>(it.lo & 15))); |
@@ -60,2 +60,3 @@ import { CharCode } from "util/string"; | ||
@inline | ||
export function isPowerOverflow128(base: u128, exponent: i32): bool { | ||
@@ -62,0 +63,0 @@ if (!(exponent > 1 && base > u128.One)) return false; |
{ | ||
"name": "as-bignum", | ||
"version": "0.1.7", | ||
"version": "0.2.0", | ||
"description": "128 and 256 bits integer and fixed point arithmetics for AssemblyScript. Also support checking overflow/underflow", | ||
@@ -5,0 +5,0 @@ "main": "js/index.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
745130
3828