Created
September 22, 2012 06:16
-
-
Save syshack/3765334 to your computer and use it in GitHub Desktop.
BigInteger
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| BigInteger.prototype.Clamp = bnpClamp; | |
| function bnpClamp() { | |
| var c = this.s&this.DM; | |
| while(this.t > 0 && this[this.t-1] == c) --this.t; | |
| } | |
| BigInteger.prototype.fromString = bnpFromString; | |
| var this.DB = 28 | |
| var dbits = 28; | |
| BigInteger.prototype.DB = dbits; | |
| BigInteger.prototype.DM = ((1<<dbits)-1); | |
| BigInteger.prototype.DV = (1<<dbits); | |
| var BI_FP = 52; | |
| BigInteger.prototype.FV = Math.pow(2,BI_FP); | |
| BigInteger.prototype.F1 = BI_FP-dbits; | |
| BigInteger.prototype.F2 = 2*dbits-BI_FP; | |
| BigInteger.ZERO = nbv(0); | |
| BigInteger.ONE = nbv(1); | |
| function nbv(i) { var r = nbi(); r.fromInt(i); return r; } | |
| function nbi() { return new BigInteger(null); } | |
| BigInteger.prototype.subTo = bnpSubTo; | |
| function bnpSubTo(a,r) { | |
| var i = 0, c = 0, m = Math.min(a.t,this.t); | |
| while(i < m) { | |
| c += this[i]-a[i]; | |
| r[i++] = c&this.DM; | |
| c >>= this.DB; | |
| } | |
| if(a.t < this.t) { | |
| c -= a.s; | |
| while(i < this.t) { | |
| c += this[i]; | |
| r[i++] = c&this.DM; | |
| c >>= this.DB; | |
| } | |
| c += this.s; | |
| } | |
| else { | |
| c += this.s; | |
| while(i < a.t) { | |
| c -= a[i]; | |
| r[i++] = c&this.DM; | |
| c >>= this.DB; | |
| } | |
| c -= a.s; | |
| } | |
| r.s = (c<0)?-1:0; | |
| if(c < -1) r[i++] = this.DV+c; | |
| else if(c > 0) r[i++] = c; | |
| r.t = i; | |
| r.clamp(); | |
| } | |
| function bnpFromInt(x) { | |
| this.t = 1; | |
| this.s = (x<0)?-1:0; | |
| if(x > 0) this[0] = x; | |
| else if(x < -1) this[0] = x+DV; | |
| else this.t = 0; | |
| } | |
| function bnpFromString(s,b) { | |
| var k; | |
| if(b == 16) k = 4; | |
| else if(b == 8) k = 3; | |
| else if(b == 256) k = 8; // byte array | |
| else if(b == 2) k = 1; | |
| else if(b == 32) k = 5; | |
| else if(b == 4) k = 2; | |
| else { this.fromRadix(s,b); return; } | |
| this.t = 0; | |
| this.s = 0; | |
| var i = s.length, mi = false, sh = 0; | |
| while(--i >= 0) { | |
| var x = (k==8)?s[i]&0xff:intAt(s,i); | |
| if(x < 0) { | |
| if(s.charAt(i) == "-") mi = true; | |
| continue; | |
| } | |
| mi = false; | |
| if(sh == 0) | |
| this[this.t++] = x; | |
| else if(sh+k > this.DB) { | |
| this[this.t-1] |= (x&((1<<(this.DB-sh))-1))<<sh; | |
| this[this.t++] = (x>>(this.DB-sh)); | |
| } | |
| else | |
| this[this.t-1] |= x<<sh; | |
| sh += k; | |
| if(sh >= this.DB) sh -= this.DB; | |
| } | |
| if(k == 8 && (s[0]&0x80) != 0) { | |
| this.s = -1; | |
| if(sh > 0) this[this.t-1] |= ((1<<(this.DB-sh))-1)<<sh; | |
| } | |
| this.clamp(); | |
| if(mi) BigInteger.ZERO.subTo(this,this); | |
| } | |
| function parseBigInt(str,r) { | |
| return new BigInteger(str,r); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment