author | Martin Soch <Martin.Soch@oracle.com> |
Wed, 30 Jan 2013 22:23:21 +0100 | |
branch | arithmetic |
changeset 615 | e3f671b50e93 |
parent 607 | 9e4d0019a9eb |
child 616 | 9cbf1f2ad7ee |
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@615 | 11 |
var __m32 = 0xFFFFFFFF; |
Martin@615 | 12 |
|
Martin@594 | 13 |
Number.prototype.next32 = function(low) { |
Martin@594 | 14 |
if (this === 0) { |
Martin@594 | 15 |
return low; |
Martin@594 | 16 |
} |
Martin@594 | 17 |
var l = new Number(low); |
Martin@594 | 18 |
l.hi = this; |
Martin@594 | 19 |
return l; |
Martin@582 | 20 |
}; |
Martin@582 | 21 |
|
Martin@594 | 22 |
Number.prototype.high32 = function() { |
Martin@615 | 23 |
return this.hi ? this.hi : (Math.floor(this / (__m32+1))) | 0; |
Martin@594 | 24 |
}; |
Martin@594 | 25 |
Number.prototype.toInt32 = function() { return this | 0; }; |
Martin@594 | 26 |
Number.prototype.toFP = function() { |
Martin@615 | 27 |
return this.hi ? this.hi * (__m32+1) + this : this; |
Martin@594 | 28 |
}; |
Martin@594 | 29 |
Number.prototype.toLong = function() { |
Martin@615 | 30 |
var hi = (this > __m32) ? (Math.floor(this / (__m32+1))) | 0 : 0; |
Martin@615 | 31 |
return hi.next32(this % (__m32+1)); |
Martin@607 | 32 |
}; |
Martin@607 | 33 |
|
Martin@607 | 34 |
Number.prototype.toExactString = function() { |
Martin@607 | 35 |
if (this.hi) { |
Martin@607 | 36 |
var res = 0; |
Martin@607 | 37 |
var a = [ 6,9,2,7,6,9,4,9,2,4 ]; |
Martin@607 | 38 |
var s = ''; |
Martin@607 | 39 |
var digit; |
Martin@607 | 40 |
var hi = this.hi; |
Martin@607 | 41 |
var low = this; |
Martin@607 | 42 |
for (var i = 0; i < a.length; i++) { |
Martin@607 | 43 |
res += hi * a[i]; |
Martin@607 | 44 |
var low_digit = low % 10; |
Martin@607 | 45 |
digit = (res % 10) + low_digit; |
Martin@607 | 46 |
|
Martin@607 | 47 |
low = Math.floor(low / 10); |
Martin@607 | 48 |
res = Math.floor(res / 10); |
Martin@607 | 49 |
|
Martin@607 | 50 |
if (digit >= 10) { |
Martin@607 | 51 |
digit -= 10; |
Martin@607 | 52 |
res++; |
Martin@607 | 53 |
} |
Martin@607 | 54 |
s = String(digit).concat(s); |
Martin@607 | 55 |
} |
Martin@607 | 56 |
return String(res).concat(s); |
Martin@607 | 57 |
} |
Martin@607 | 58 |
return String(this); |
Martin@607 | 59 |
}; |
Martin@594 | 60 |
|
Martin@594 | 61 |
Number.prototype.add64 = function(x) { |
Martin@594 | 62 |
var low = this + x; |
Martin@594 | 63 |
carry = 0; |
Martin@615 | 64 |
if (low > __m32) { |
Martin@594 | 65 |
carry = 1; |
Martin@615 | 66 |
low -= (__m32+1); |
Martin@594 | 67 |
} |
Martin@615 | 68 |
var hi = (this.high32() + x.high32() + carry) | 0; |
Martin@594 | 69 |
return hi.next32(low); |
Martin@582 | 70 |
}; |
Martin@582 | 71 |
|
Martin@594 | 72 |
Number.prototype.div64 = function(x) { |
Martin@594 | 73 |
var low = Math.floor(this.toFP() / x.toFP()); // TODO: not exact enough |
Martin@615 | 74 |
if (low > __m32) { |
Martin@615 | 75 |
var hi = Math.floor(low / (__m32+1)) | 0; |
Martin@615 | 76 |
return hi.next32(low % (__m32+1)); |
Martin@582 | 77 |
} |
Martin@594 | 78 |
return low; |
Martin@582 | 79 |
}; |
Martin@582 | 80 |
|
Martin@615 | 81 |
Number.prototype.and64 = function(x) { |
Martin@615 | 82 |
var low = this & x; |
Martin@615 | 83 |
if (this.hi && x.hi) { |
Martin@615 | 84 |
var hi = this.hi & x.hi; |
Martin@615 | 85 |
return hi.next32(low); |
Martin@615 | 86 |
}; |
Martin@615 | 87 |
return low; |
Martin@615 | 88 |
}; |
Martin@615 | 89 |
|
Martin@594 | 90 |
Number.prototype.shl64 = function(x) { |
Martin@594 | 91 |
if (x > 32) { |
Martin@594 | 92 |
var hi = (this << (x - 32)) & 0xFFFFFFFF; |
Martin@594 | 93 |
return hi.next32(0); |
Martin@594 | 94 |
} else { |
Martin@594 | 95 |
var hi = (this.high32() << x) & 0xFFFFFFFF; |
Martin@615 | 96 |
var low_reminder = this >> (32 - x); |
Martin@594 | 97 |
hi |= low_reminder; |
Martin@594 | 98 |
var low = this << x; |
Martin@594 | 99 |
return hi.next32(low); |
Martin@594 | 100 |
} |
Martin@582 | 101 |
}; |
Martin@582 | 102 |
|
Martin@615 | 103 |
Number.prototype.shr64 = function(x) { |
Martin@615 | 104 |
if (x > 32) { |
Martin@615 | 105 |
var low = (this.high32() >> (x - 32)) & 0xFFFFFFFF; |
Martin@615 | 106 |
return low; |
Martin@615 | 107 |
} else { |
Martin@615 | 108 |
var low = (this >> x) & 0xFFFFFFFF; |
Martin@615 | 109 |
var hi_reminder = (this.high32() << (32 - x)) >> (32 - x); |
Martin@615 | 110 |
low |= hi_reminder; |
Martin@615 | 111 |
var hi = this.high32() >> x; |
Martin@615 | 112 |
return hi.next32(low); |
Martin@615 | 113 |
} |
Martin@615 | 114 |
}; |
Martin@615 | 115 |
|
Martin@594 | 116 |
Number.prototype.compare64 = function(x) { |
Martin@582 | 117 |
if (this.hi == x.hi) { |
Martin@594 | 118 |
return (this == x) ? 0 : ((this < x) ? -1 : 1); |
Martin@582 | 119 |
} |
Martin@582 | 120 |
return (this.hi < x.hi) ? -1 : 1; |
Martin@582 | 121 |
}; |