Comparing version 1.0.0-alpha0 to 1.0.0-alpha1
47
index.js
'use strict'; | ||
const LO_FIELD = 0x10000000000000; | ||
function A64(num) { | ||
@@ -15,4 +13,4 @@ this.hi = 0; | ||
A64.prototype.from = function from(num) { | ||
this.hi = (num / 0x100000000) >>> 0; | ||
this.lo = num >>> 0; | ||
this.hi = (num / 0x100000000) | 0; | ||
this.lo = num | 0; | ||
}; | ||
@@ -22,4 +20,4 @@ | ||
const res = new A64(); | ||
res.hi = this.hi >>> 0; | ||
res.lo = this.lo >>> 0; | ||
res.hi = this.hi | 0; | ||
res.lo = this.lo | 0; | ||
return res; | ||
@@ -36,12 +34,19 @@ }; | ||
A64.prototype.toString = function toString() { | ||
return pad8(this.hi.toString(16)) + pad8(this.lo.toString(16)); | ||
const hi = this.hi >>> 0; | ||
const lo = this.lo >>> 0; | ||
return pad8(hi.toString(16)) + pad8(lo.toString(16)); | ||
}; | ||
A64.prototype.iadd = function iadd(other) { | ||
const lo = (this.lo + other.lo) >>> 0; | ||
const carry = (lo < this.lo) | (lo < other.lo); | ||
const selfLo = this.lo | 0; | ||
const otherLo = other.lo | 0; | ||
const selfHi = this.hi | 0; | ||
const otherHi = other.hi | 0; | ||
this.hi = ((this.hi + other.hi) | 0 + carry) >>> 0; | ||
this.lo = lo; | ||
const lo = (selfLo + otherLo) | 0; | ||
const carry = (lo < selfLo) | (lo < otherLo); | ||
this.hi = ((selfHi + otherHi) | 0 + carry) | 0; | ||
this.lo = lo | 0; | ||
return this; | ||
@@ -51,8 +56,20 @@ }; | ||
A64.prototype.imul = function imul(other) { | ||
const hi = (Math.imul(this.hi, other.lo) + Math.imul(other.hi, this.lo)) | 0; | ||
const carry = Math.floor((this.lo * other.lo) / 0x100000000) | 0; | ||
const selfLo = this.lo | 0; | ||
const otherLo = other.lo | 0; | ||
const selfHi = this.hi | 0; | ||
const otherHi = other.hi | 0; | ||
this.hi = (hi + carry) >>> 0; | ||
this.lo = Math.imul(this.lo, other.lo) >>> 0; | ||
const hi = (Math.imul(selfHi, otherLo) + Math.imul(otherHi, selfLo)) | 0; | ||
let carry = ((selfLo * otherLo) / 0x100000000) | 0; | ||
if (selfLo <0 && otherLo < 0) | ||
carry = (carry + otherLo + selfLo) | 0; | ||
else if (otherLo < 0) | ||
carry = (carry + selfLo - 1) | 0; | ||
else if (selfLo < 0) | ||
carry = (carry + otherLo - 1) | 0; | ||
this.hi = (hi + carry) | 0; | ||
this.lo = Math.imul(selfLo, otherLo) | 0; | ||
return this; | ||
@@ -59,0 +76,0 @@ }; |
{ | ||
"name": "awesome64", | ||
"version": "1.0.0-alpha0", | ||
"version": "1.0.0-alpha1", | ||
"description": "Awesome Int64 num implementation", | ||
@@ -26,2 +26,3 @@ "main": "index.js", | ||
"devDependencies": { | ||
"benchmark": "^2.1.4", | ||
"bn.js": "^4.11.6", | ||
@@ -28,0 +29,0 @@ "mocha": "^3.4.2" |
@@ -35,3 +35,3 @@ 'use strict'; | ||
it('should cross-check against BN', () => { | ||
for (let i = 0; i < 1e4; i++) { | ||
for (let i = 0; i < 1e5; i++) { | ||
const a = new A64(); | ||
@@ -70,4 +70,18 @@ const b = new A64(); | ||
it('should mul with negative self.lo', () => { | ||
const a = new A64(0xeffffeda); | ||
const b = new A64(0x1fffffff); | ||
assert.equal(a.mul(b).toString(), '1dffffda50000126'); | ||
}); | ||
it('should mul with negative self.lo and other.lo', () => { | ||
const a = new A64(0xeffffeda); | ||
const b = new A64(0xeffffeda); | ||
assert.equal(a.mul(b).toString(), 'e0fffdd8c00151a4'); | ||
}); | ||
it('should cross-check against BN', () => { | ||
for (let i = 0; i < 1e4; i++) { | ||
for (let i = 0; i < 1e5; i++) { | ||
const a = new A64(); | ||
@@ -74,0 +88,0 @@ const b = new A64(); |
7217
6
185
3