author | Martin Soch <Martin.Soch@oracle.com> |
Sun, 27 Jan 2013 10:19:02 +0100 | |
branch | arithmetic |
changeset 593 | b42911b78a16 |
parent 582 | emul/src/main/resources/org/apidesign/vm4brwsr/emul/java_lang_Number.js@8e546d108658 |
parent 555 | emul/src/main/resources/org/apidesign/vm4brwsr/emul/java_lang_Number.js@cde0c2d7794e |
child 594 | 035fcbd7a33c |
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@582 | 11 |
var Long = function(low, hi) { |
Martin@582 | 12 |
this.low = low; |
Martin@582 | 13 |
this.hi = hi; |
Martin@582 | 14 |
}; |
Martin@582 | 15 |
|
Martin@582 | 16 |
function LongFromNumber(x) { |
Martin@582 | 17 |
return new Long(x % 0xFFFFFFFF, Math.floor(x / 0xFFFFFFFF)); |
Martin@582 | 18 |
}; |
Martin@582 | 19 |
|
Martin@582 | 20 |
function MakeLong(x) { |
Martin@582 | 21 |
if ((x.hi == undefined) && (x.low == undefined)) { |
Martin@582 | 22 |
return LongFromNumber(x); |
Martin@582 | 23 |
} |
Martin@582 | 24 |
return x; |
Martin@582 | 25 |
}; |
Martin@582 | 26 |
|
Martin@582 | 27 |
Long.prototype.toInt32 = function() { return this.low | 0; }; |
Martin@582 | 28 |
Long.prototype.toFP = function() { return this.hi * 0xFFFFFFFF + this.low; }; |
Martin@582 | 29 |
|
Martin@582 | 30 |
Long.prototype.toString = function() { |
Martin@582 | 31 |
return String(this.toFP()); |
Martin@582 | 32 |
}; |
Martin@582 | 33 |
|
Martin@582 | 34 |
Long.prototype.valueOf = function() { |
Martin@582 | 35 |
return this.toFP(); |
Martin@582 | 36 |
}; |
Martin@582 | 37 |
|
Martin@582 | 38 |
Long.prototype.compare64 = function(x) { |
Martin@582 | 39 |
if (this.hi == x.hi) { |
Martin@582 | 40 |
return (this.low == x.low) ? 0 : ((this.low < x.low) ? -1 : 1); |
Martin@582 | 41 |
} |
Martin@582 | 42 |
return (this.hi < x.hi) ? -1 : 1; |
Martin@582 | 43 |
}; |
Martin@582 | 44 |
|
Martin@582 | 45 |
Long.prototype.add64 = function(x) { |
Martin@582 | 46 |
low = this.low + x.low; |
Martin@582 | 47 |
carry = 0; |
Martin@582 | 48 |
if (low > 0xFFFFFFFF) { |
Martin@582 | 49 |
carry = 1; |
Martin@582 | 50 |
low -= 0xFFFFFFFF; |
Martin@582 | 51 |
} |
Martin@582 | 52 |
hi = (this.hi + x.hi + carry) & 0xFFFFFFFF; |
Martin@582 | 53 |
return new Long(low, hi); |
Martin@582 | 54 |
}; |
Martin@582 | 55 |
|
Martin@582 | 56 |
Long.prototype.div64 = function(x) { |
Martin@582 | 57 |
return LongFromNumber(Math.floor(this.toFP() / x.toFP())); |
Martin@582 | 58 |
}; |
Martin@582 | 59 |
|
Martin@582 | 60 |
Long.prototype.shl64 = function(x) { |
Martin@582 | 61 |
if (x > 32) { |
Martin@582 | 62 |
hi = (this.low << (x - 32)) & 0xFFFFFFFF; |
Martin@582 | 63 |
low = 0; |
Martin@582 | 64 |
} else { |
Martin@582 | 65 |
hi = (this.hi << x) & 0xFFFFFFFF; |
Martin@582 | 66 |
low_reminder = this.low >> x; |
Martin@582 | 67 |
hi |= low_reminder; |
Martin@582 | 68 |
low = this.low << x; |
Martin@582 | 69 |
} |
Martin@582 | 70 |
return new Long(low, hi); |
Martin@582 | 71 |
}; |