author | Martin Soch <Martin.Soch@oracle.com> |
Mon, 28 Jan 2013 00:15:38 +0100 | |
branch | arithmetic |
changeset 594 | 035fcbd7a33c |
parent 593 | b42911b78a16 |
child 607 | 9e4d0019a9eb |
permissions | -rw-r--r-- |
Martin@438 | 1 |
// empty line needed here |
Martin@445 | 2 |
Number.prototype.add32 = function(x) { return (this + x) | 0; }; |
Martin@445 | 3 |
Number.prototype.sub32 = function(x) { return (this - x) | 0; }; |
Martin@445 | 4 |
Number.prototype.mul32 = function(x) { |
Martin@445 | 5 |
return (((this * (x >> 16)) << 16) + this * (x & 0xFFFF)) | 0; |
Martin@438 | 6 |
}; |
Martin@439 | 7 |
|
Martin@445 | 8 |
Number.prototype.toInt8 = function() { return (this << 24) >> 24; }; |
Martin@582 | 9 |
Number.prototype.toInt16 = function() { return (this << 16) >> 16; }; |
Martin@582 | 10 |
|
Martin@594 | 11 |
Number.prototype.next32 = function(low) { |
Martin@594 | 12 |
if (this === 0) { |
Martin@594 | 13 |
return low; |
Martin@594 | 14 |
} |
Martin@594 | 15 |
var l = new Number(low); |
Martin@594 | 16 |
l.hi = this; |
Martin@594 | 17 |
return l; |
Martin@582 | 18 |
}; |
Martin@582 | 19 |
|
Martin@594 | 20 |
Number.prototype.high32 = function() { |
Martin@594 | 21 |
return this.hi ? this.hi : (Math.floor(this / 0xFFFFFFFF)) | 0; |
Martin@594 | 22 |
}; |
Martin@594 | 23 |
Number.prototype.toInt32 = function() { return this | 0; }; |
Martin@594 | 24 |
Number.prototype.toFP = function() { |
Martin@594 | 25 |
return this.hi ? this.hi * 0xFFFFFFFF + this : this; |
Martin@594 | 26 |
}; |
Martin@594 | 27 |
Number.prototype.toLong = function() { |
Martin@594 | 28 |
var hi = (this > 0xFFFFFFFF) ? (Math.floor(this / 0xFFFFFFFF)) | 0 : 0; |
Martin@594 | 29 |
return hi.next32(this % 0xFFFFFFFF); |
Martin@594 | 30 |
} |
Martin@594 | 31 |
|
Martin@594 | 32 |
Number.prototype.add64 = function(x) { |
Martin@594 | 33 |
var low = this + x; |
Martin@594 | 34 |
carry = 0; |
Martin@594 | 35 |
if (low > 0xFFFFFFFF) { |
Martin@594 | 36 |
carry = 1; |
Martin@594 | 37 |
low -= 0xFFFFFFFF; // should not here be also -1? |
Martin@594 | 38 |
} |
Martin@594 | 39 |
var hi = (this.high32() + x.high32() + carry) & 0xFFFFFFFF; |
Martin@594 | 40 |
return hi.next32(low); |
Martin@582 | 41 |
}; |
Martin@582 | 42 |
|
Martin@594 | 43 |
Number.prototype.div64 = function(x) { |
Martin@594 | 44 |
var low = Math.floor(this.toFP() / x.toFP()); // TODO: not exact enough |
Martin@594 | 45 |
if (low > 0xFFFFFFFF) { |
Martin@594 | 46 |
var hi = Math.floor(low / 0xFFFFFFFF) | 0; |
Martin@594 | 47 |
return hi.next32(low % 0xFFFFFFFF); |
Martin@582 | 48 |
} |
Martin@594 | 49 |
return low; |
Martin@582 | 50 |
}; |
Martin@582 | 51 |
|
Martin@594 | 52 |
Number.prototype.shl64 = function(x) { |
Martin@594 | 53 |
if (x > 32) { |
Martin@594 | 54 |
var hi = (this << (x - 32)) & 0xFFFFFFFF; |
Martin@594 | 55 |
return hi.next32(0); |
Martin@594 | 56 |
} else { |
Martin@594 | 57 |
var hi = (this.high32() << x) & 0xFFFFFFFF; |
Martin@594 | 58 |
var low_reminder = this >> x; |
Martin@594 | 59 |
hi |= low_reminder; |
Martin@594 | 60 |
var low = this << x; |
Martin@594 | 61 |
return hi.next32(low); |
Martin@594 | 62 |
} |
Martin@582 | 63 |
}; |
Martin@582 | 64 |
|
Martin@594 | 65 |
Number.prototype.compare64 = function(x) { |
Martin@582 | 66 |
if (this.hi == x.hi) { |
Martin@594 | 67 |
return (this == x) ? 0 : ((this < x) ? -1 : 1); |
Martin@582 | 68 |
} |
Martin@582 | 69 |
return (this.hi < x.hi) ? -1 : 1; |
Martin@582 | 70 |
}; |