Comparing version 0.2.0 to 0.2.1
105
lib/bn.js
@@ -403,31 +403,33 @@ var assert = require('assert'); | ||
res.words = new Array(2 * this.length); | ||
var r0 = 0, r1 = 0, r2 = 0; | ||
for (var k = 0; k < 2 * this.length - 1; k++) { | ||
for (var i = 0; i <= k >> 1; i++) { | ||
var j = k - i; | ||
var a = this.words[i]; | ||
for (var i = 0; i < res.words.length; i++) | ||
res.words[i] = 0; | ||
for (var i = 0; i < this.length; i++) { | ||
var a = this.words[i]; | ||
var carry = 0; | ||
for (var j = 0; j < this.length; j++) { | ||
var b = this.words[j]; | ||
var uv = a * b; | ||
var v = uv & 0xffffff; | ||
var u = (uv - v) / 0x1000000; | ||
if (i < j) { | ||
u <<= 1; | ||
r2 += u >> 24; | ||
v <<= 1; | ||
u |= v >> 24; | ||
u &= 0xffffff; | ||
v &= 0xffffff; | ||
var r = a * b + carry; | ||
var k = i + j; | ||
var lo = r & 0xffffff; | ||
var carry = (r - lo) / 0x1000000; | ||
lo = res.words[k] + lo; | ||
res.words[k] = lo & 0xffffff; | ||
carry += lo >> 24; | ||
} | ||
// Apply carry | ||
k++; | ||
assert(carry <= 0xffffff); | ||
for (; carry !== 0; k++) { | ||
if (res.words[k]) { | ||
carry += res.words[k]; | ||
res.words[k] = carry & 0xffffff; | ||
carry >>= 24; | ||
} else { | ||
res.words[k] = carry; | ||
carry = 0; | ||
} | ||
r0 += v; | ||
r1 += u + (r0 >> 24); | ||
r2 += r1 >> 24; | ||
r0 &= 0xffffff; | ||
r1 &= 0xffffff; | ||
} | ||
res.words[k] = r0; | ||
r0 = r1; | ||
r1 = r2; | ||
r2 = 0; | ||
} | ||
res.words[2 * this.length - 1] = r0; | ||
res.length = res.words.length; | ||
@@ -580,2 +582,29 @@ return res.strip(); | ||
BN.prototype._shiftDiv = function _shiftDiv(num) { | ||
// Find maximum Q, Q * num <= this | ||
var shift = Math.max(0, this.bitLength() - num.bitLength()); | ||
var max = num.shl(shift); | ||
if (shift > 0 && this.cmp(max) < 0) { | ||
max.ishr(1, shift); | ||
shift--; | ||
} | ||
var maxLen = max.bitLength(); | ||
var c = this; | ||
var r = new BN(0); | ||
while (c.cmp(num) >= 0) { | ||
assert(shift >= 0); | ||
if (c.cmp(max) >= 0) { | ||
c = c.sub(max); | ||
r.binc(shift); | ||
} | ||
var delta = Math.max(1, maxLen - c.bitLength()); | ||
max.ishr(delta, shift); | ||
maxLen -= delta; | ||
shift -= delta; | ||
} | ||
return { mod: c, div: r }; | ||
}; | ||
BN.prototype._div = function _div(num, base) { | ||
@@ -606,26 +635,4 @@ if (!(num instanceof BN)) | ||
return { div: new BN(0), mod: this }; | ||
// Find maximum Q, Q * num <= this | ||
var shift = Math.max(0, this.bitLength() - num.bitLength()); | ||
var max = num.shl(shift); | ||
if (shift > 0 && this.cmp(max) < 0) { | ||
max.ishr(1, shift); | ||
shift--; | ||
} | ||
var maxLen = max.bitLength(); | ||
var c = this; | ||
var r = new BN(0); | ||
while (c.cmp(num) >= 0) { | ||
assert(shift >= 0); | ||
if (c.cmp(max) >= 0) { | ||
c = c.sub(max); | ||
r.binc(shift); | ||
} | ||
var delta = Math.max(1, maxLen - c.bitLength()); | ||
max.ishr(delta, shift); | ||
maxLen -= delta; | ||
shift -= delta; | ||
} | ||
return { mod: c, div: r }; | ||
else | ||
return this._shiftDiv(num); | ||
}; | ||
@@ -632,0 +639,0 @@ |
{ | ||
"name": "bn.js", | ||
"version": "0.2.0", | ||
"version": "0.2.1", | ||
"description": "Big number implementation in pure javascript", | ||
@@ -5,0 +5,0 @@ "main": "lib/bn.js", |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
6919725
8
1039