Martin@438: // empty line needed here Martin@445: Number.prototype.add32 = function(x) { return (this + x) | 0; }; Martin@445: Number.prototype.sub32 = function(x) { return (this - x) | 0; }; Martin@445: Number.prototype.mul32 = function(x) { Martin@445: return (((this * (x >> 16)) << 16) + this * (x & 0xFFFF)) | 0; Martin@438: }; Martin@439: Martin@445: Number.prototype.toInt8 = function() { return (this << 24) >> 24; }; Martin@582: Number.prototype.toInt16 = function() { return (this << 16) >> 16; }; Martin@582: Martin@594: Number.prototype.next32 = function(low) { Martin@594: if (this === 0) { Martin@594: return low; Martin@594: } Martin@594: var l = new Number(low); Martin@594: l.hi = this; Martin@594: return l; Martin@582: }; Martin@582: Martin@594: Number.prototype.high32 = function() { Martin@594: return this.hi ? this.hi : (Math.floor(this / 0xFFFFFFFF)) | 0; Martin@594: }; Martin@594: Number.prototype.toInt32 = function() { return this | 0; }; Martin@594: Number.prototype.toFP = function() { Martin@594: return this.hi ? this.hi * 0xFFFFFFFF + this : this; Martin@594: }; Martin@594: Number.prototype.toLong = function() { Martin@594: var hi = (this > 0xFFFFFFFF) ? (Math.floor(this / 0xFFFFFFFF)) | 0 : 0; Martin@594: return hi.next32(this % 0xFFFFFFFF); Martin@594: } Martin@594: Martin@594: Number.prototype.add64 = function(x) { Martin@594: var low = this + x; Martin@594: carry = 0; Martin@594: if (low > 0xFFFFFFFF) { Martin@594: carry = 1; Martin@594: low -= 0xFFFFFFFF; // should not here be also -1? Martin@594: } Martin@594: var hi = (this.high32() + x.high32() + carry) & 0xFFFFFFFF; Martin@594: return hi.next32(low); Martin@582: }; Martin@582: Martin@594: Number.prototype.div64 = function(x) { Martin@594: var low = Math.floor(this.toFP() / x.toFP()); // TODO: not exact enough Martin@594: if (low > 0xFFFFFFFF) { Martin@594: var hi = Math.floor(low / 0xFFFFFFFF) | 0; Martin@594: return hi.next32(low % 0xFFFFFFFF); Martin@582: } Martin@594: return low; Martin@582: }; Martin@582: Martin@594: Number.prototype.shl64 = function(x) { Martin@594: if (x > 32) { Martin@594: var hi = (this << (x - 32)) & 0xFFFFFFFF; Martin@594: return hi.next32(0); Martin@594: } else { Martin@594: var hi = (this.high32() << x) & 0xFFFFFFFF; Martin@594: var low_reminder = this >> x; Martin@594: hi |= low_reminder; Martin@594: var low = this << x; Martin@594: return hi.next32(low); Martin@594: } Martin@582: }; Martin@582: Martin@594: Number.prototype.compare64 = function(x) { Martin@582: if (this.hi == x.hi) { Martin@594: return (this == x) ? 0 : ((this < x) ? -1 : 1); Martin@582: } Martin@582: return (this.hi < x.hi) ? -1 : 1; Martin@582: };