emul/mini/src/main/resources/org/apidesign/vm4brwsr/emul/lang/java_lang_Number.js
Fixed error in neg64 found bu Lubo.
1 // empty line needed here
2 Number.prototype.add32 = function(x) { return (this + x) | 0; };
3 Number.prototype.sub32 = function(x) { return (this - x) | 0; };
4 Number.prototype.mul32 = function(x) {
5 return (((this * (x >> 16)) << 16) + this * (x & 0xFFFF)) | 0;
8 Number.prototype.toInt8 = function() { return (this << 24) >> 24; };
9 Number.prototype.toInt16 = function() { return (this << 16) >> 16; };
11 var __m32 = 0xFFFFFFFF;
13 Number.prototype.next32 = function(low) {
17 var l = new Number(low);
22 Number.prototype.high32 = function() {
23 return this.hi ? this.hi : (Math.floor(this / (__m32+1))) | 0;
25 Number.prototype.toInt32 = function() { return this | 0; };
26 Number.prototype.toFP = function() {
27 return this.hi ? this.hi * (__m32+1) + this : this;
29 Number.prototype.toLong = function() {
30 var hi = (this > __m32) ? (Math.floor(this / (__m32+1))) | 0 : 0;
31 return hi.next32(Math.floor(this % (__m32+1)));
34 Number.prototype.toExactString = function() {
37 var a = [ 6,9,2,7,6,9,4,9,2,4 ];
42 for (var i = 0; i < a.length; i++) {
44 var low_digit = low % 10;
45 digit = (res % 10) + low_digit;
47 low = Math.floor(low / 10);
48 res = Math.floor(res / 10);
54 s = String(digit).concat(s);
56 return String(res).concat(s);
61 Number.prototype.add64 = function(x) {
68 var hi = (this.high32() + x.high32() + carry) | 0;
69 return hi.next32(low);
72 Number.prototype.sub64 = function(x) {
79 var hi = (this.high32() - x.high32() - carry) | 0;
80 return hi.next32(low);
83 Number.prototype.mul64 = function(x) {
84 var low = this.mul32(x);
85 low += (low < 0) ? (__m32+1) : 0;
86 // first count upper 32 bits of (this.low * x.low)
90 for (var i = 0; i < 32; i++) {
93 hi_low += this & 0xFFFF
96 hi_low += (hi_hi & 1) ? 0x8000 : 0;
100 var hi = (hi_hi << 16) + hi_low;
102 var m1 = this.high32().mul32(x);
103 var m2 = this.mul32(x.high32());
104 hi = hi.add32(m1).add32(m2);
106 return hi.next32(low);
109 Number.prototype.div64 = function(x) {
110 var low = Math.floor(this.toFP() / x.toFP()); // TODO: not exact enough
112 var hi = Math.floor(low / (__m32+1)) | 0;
113 return hi.next32(low % (__m32+1));
118 Number.prototype.and64 = function(x) {
120 low += (low < 0) ? (__m32+1) : 0;
121 if (this.hi && x.hi) {
122 var hi = this.hi & x.hi;
123 return hi.next32(low);
128 Number.prototype.or64 = function(x) {
130 low += (low < 0) ? (__m32+1) : 0;
131 if (this.hi || x.hi) {
132 var hi = this.hi | x.hi;
133 return hi.next32(low);
138 Number.prototype.xor64 = function(x) {
140 low += (low < 0) ? (__m32+1) : 0;
141 if (this.hi || x.hi) {
142 var hi = this.hi ^ x.hi;
143 return hi.next32(low);
148 Number.prototype.shl64 = function(x) {
150 var hi = this << (x - 32);
153 var hi = this.high32() << x;
154 var low_reminder = this >> (32 - x);
157 low += (low < 0) ? (__m32+1) : 0;
158 return hi.next32(low);
162 Number.prototype.shr64 = function(x) {
164 var low = this.high32() >> (x - 32);
165 low += (low < 0) ? (__m32+1) : 0;
169 var hi_reminder = this.high32() << (32 - x);
171 low += (low < 0) ? (__m32+1) : 0;
172 var hi = this.high32() >> x;
173 return hi.next32(low);
177 Number.prototype.ushr64 = function(x) {
179 var low = this.high32() >>> (x - 32);
180 low += (low < 0) ? (__m32+1) : 0;
183 var low = this >>> x;
184 var hi_reminder = this.high32() << (32 - x);
186 low += (low < 0) ? (__m32+1) : 0;
187 var hi = this.high32() >>> x;
188 return hi.next32(low);
192 Number.prototype.compare64 = function(x) {
193 if (this.hi == x.hi) {
194 return (this == x) ? 0 : ((this < x) ? -1 : 1);
196 return (this.hi < x.hi) ? -1 : 1;
199 Number.prototype.neg64 = function() {
200 var hi = this.high32();
202 if ((hi === 0) && (low < 0)) { return -low; }
205 low += (low < 0) ? (__m32+1) : 0;
206 var ret = hi.next32(low);