rt/emul/mini/src/main/resources/org/apidesign/vm4brwsr/emul/lang/java_lang_Number.js
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;
7 Number.prototype.neg32 = function() { return (-this) | 0; };
9 Number.prototype.toInt8 = function() { return (this << 24) >> 24; };
10 Number.prototype.toInt16 = function() { return (this << 16) >> 16; };
12 var __m32 = 0xFFFFFFFF;
14 Number.prototype.next32 = function(low) {
18 var l = new Number(low);
23 Number.prototype.high32 = function() {
24 return this.hi ? this.hi : (Math.floor(this / (__m32+1))) | 0;
26 Number.prototype.toInt32 = function() { return this | 0; };
27 Number.prototype.toFP = function() {
28 return this.hi ? this.hi * (__m32+1) + this : this;
30 Number.prototype.toLong = function() {
31 var hi = (this / (__m32+1)) | 0;
32 var low = (this % (__m32+1)) | 0;
41 return hi.next32(low);
44 Number.prototype.toExactString = function() {
46 // check for Long.MIN_VALUE
47 if ((this.hi == (0x80000000 | 0)) && (this == 0)) {
48 return '-9223372036854775808';
51 var a = [ 6,9,2,7,6,9,4,9,2,4 ];
54 var neg = this.hi < 0;
63 for (var i = 0; i < a.length; i++) {
65 var low_digit = low % 10;
66 digit = (res % 10) + low_digit;
68 low = Math.floor(low / 10);
69 res = Math.floor(res / 10);
75 s = String(digit).concat(s);
77 s = String(res).concat(s).replace(/^0+/, '');
78 return (neg ? '-' : '').concat(s);
83 Number.prototype.add64 = function(x) {
90 var hi = (this.high32() + x.high32() + carry) | 0;
91 return hi.next32(low);
94 Number.prototype.sub64 = function(x) {
101 var hi = (this.high32() - x.high32() - carry) | 0;
102 return hi.next32(low);
105 Number.prototype.mul64 = function(x) {
106 var low = this.mul32(x);
107 low += (low < 0) ? (__m32+1) : 0;
108 // first count upper 32 bits of (this.low * x.low)
112 for (var i = 0; i < 32; i++) {
114 hi_hi += this >>> 16;
115 hi_low += this & 0xFFFF
118 hi_low += (hi_hi & 1) ? 0x8000 : 0;
122 var hi = (hi_hi << 16) + hi_low;
124 var m1 = this.high32().mul32(x);
125 var m2 = this.mul32(x.high32());
126 hi = hi.add32(m1).add32(m2);
128 return hi.next32(low);
131 Number.prototype.and64 = function(x) {
133 low += (low < 0) ? (__m32+1) : 0;
134 if (this.hi && x.hi) {
135 var hi = this.hi & x.hi;
136 return hi.next32(low);
141 Number.prototype.or64 = function(x) {
143 low += (low < 0) ? (__m32+1) : 0;
144 if (this.hi || x.hi) {
145 var hi = this.hi | x.hi;
146 return hi.next32(low);
151 Number.prototype.xor64 = function(x) {
153 low += (low < 0) ? (__m32+1) : 0;
154 if (this.hi || x.hi) {
155 var hi = this.hi ^ x.hi;
156 return hi.next32(low);
161 Number.prototype.shl64 = function(x) {
163 var hi = this << (x - 32);
166 var hi = this.high32() << x;
167 var low_reminder = this >> (32 - x);
170 low += (low < 0) ? (__m32+1) : 0;
171 return hi.next32(low);
175 Number.prototype.shr64 = function(x) {
177 var low = this.high32() >> (x - 32);
178 low += (low < 0) ? (__m32+1) : 0;
182 var hi_reminder = this.high32() << (32 - x);
184 low += (low < 0) ? (__m32+1) : 0;
185 var hi = this.high32() >> x;
186 return hi.next32(low);
190 Number.prototype.ushr64 = function(x) {
192 var low = this.high32() >>> (x - 32);
193 low += (low < 0) ? (__m32+1) : 0;
196 var low = this >>> x;
197 var hi_reminder = this.high32() << (32 - x);
199 low += (low < 0) ? (__m32+1) : 0;
200 var hi = this.high32() >>> x;
201 return hi.next32(low);
205 Number.prototype.compare64 = function(x) {
206 if (this.high32() === x.high32()) {
207 return (this < x) ? -1 : ((this > x) ? 1 : 0);
209 return (this.high32() < x.high32()) ? -1 : 1;
212 Number.prototype.neg64 = function() {
213 var hi = this.high32();
215 if ((hi === 0) && (low < 0)) { return -low; }
218 low += (low < 0) ? (__m32+1) : 0;
219 var ret = hi.next32(low);
223 (function(numberPrototype) {
224 function __handleDivByZero() {
225 var exception = new vm.java_lang_ArithmeticException;
226 vm.java_lang_ArithmeticException(false).constructor
227 .cons__VLjava_lang_String_2.call(exception, "/ by zero");
232 function __Int64(hi32, lo32) {
233 this.hi32 = hi32 | 0;
234 this.lo32 = lo32 | 0;
236 this.get32 = function(bitIndex) {
240 var selector = bitIndex >>> 5;
258 var shift = bitIndex & 31;
263 return (v1 << (32 - shift)) | (v0 >>> shift);
266 this.get16 = function(bitIndex) {
267 return this.get32(bitIndex) & 0xffff;
270 this.set16 = function(bitIndex, value) {
272 var shift = bitIndex & 15;
273 var svalue = (value & 0xffff) << shift;
274 var smask = 0xffff << shift;
275 var selector = bitIndex >>> 4;
280 this.lo32 = (this.lo32 & ~(smask >>> 16))
284 this.lo32 = (this.lo32 & ~smask) | svalue;
287 this.lo32 = (this.lo32 & ~(smask << 16))
289 this.hi32 = (this.hi32 & ~(smask >>> 16))
293 this.hi32 = (this.hi32 & ~smask) | svalue;
296 this.hi32 = (this.hi32 & ~(smask << 16))
302 this.getDigit = function(index, shift) {
303 return this.get16((index << 4) - shift);
306 this.getTwoDigits = function(index, shift) {
307 return this.get32(((index - 1) << 4) - shift);
310 this.setDigit = function(index, shift, value) {
311 this.set16((index << 4) - shift, value);
314 this.countSignificantDigits = function() {
318 if (this.hi32 === 0) {
319 if (this.lo32 === 0) {
324 remaining = this.lo32;
327 remaining = this.hi32;
334 return (remaining < 65536) ? sd - 1 : sd;
337 this.toNumber = function() {
338 var lo32 = this.lo32;
343 return this.hi32.next32(lo32);
347 function __countLeadingZeroes16(number) {
360 if (number < 16384) {
365 return (number < 32768) ? nlz + 1 : nlz;
368 // q = u / v; r = u - q * v;
370 function __div64(q, r, u, v) {
371 var m = u.countSignificantDigits();
372 var n = v.countSignificantDigits();
377 // v has single digit
378 var vd = v.getDigit(0, 0);
380 for (var i = m - 1; i >= 0; --i) {
381 var ui = (carry << 16) | u.getDigit(i, 0);
385 var qi = (ui / vd) | 0;
386 q.setDigit(i, 0, qi);
387 carry = ui - qi * vd;
403 var nrm = __countLeadingZeroes16(v.getDigit(n - 1, 0));
405 var vd1 = v.getDigit(n - 1, nrm);
406 var vd0 = v.getDigit(n - 2, nrm);
407 for (var j = m - n; j >= 0; --j) {
408 // Calculate qj estimate
409 var ud21 = r.getTwoDigits(j + n, nrm);
410 var ud2 = ud21 >>> 16;
415 var qest = (ud2 === vd1) ? 0xFFFF : ((ud21 / vd1) | 0);
416 var rest = ud21 - qest * vd1;
418 // 0 <= (qest - qj) <= 2
420 // Refine qj estimate
421 var ud0 = r.getDigit(j + n - 2, nrm);
422 while ((qest * vd0) > ((rest * 0x10000) + ud0)) {
427 // 0 <= (qest - qj) <= 1
429 // Multiply and subtract
431 for (var i = 0; i < n; ++i) {
432 var vi = qest * v.getDigit(i, nrm);
433 var ui = r.getDigit(i + j, nrm) - carry - (vi & 0xffff);
434 r.setDigit(i + j, nrm, ui);
435 carry = (vi >>> 16) - (ui >> 16);
437 var uj = ud2 - carry;
445 for (var i = 0; i < n; ++i) {
446 var ui = r.getDigit(i + j, nrm) + v.getDigit(i, nrm)
448 r.setDigit(i + j, nrm, ui);
454 q.setDigit(j, 0, qest);
455 r.setDigit(j + n, nrm, uj);
459 numberPrototype.div32 = function(x) {
464 return (this / x) | 0;
467 numberPrototype.mod32 = function(x) {
475 numberPrototype.div64 = function(x) {
476 var negateResult = false;
479 if ((this.high32() & 0x80000000) != 0) {
481 negateResult = !negateResult;
486 if ((x.high32() & 0x80000000) != 0) {
488 negateResult = !negateResult;
493 if ((v == 0) && (v.high32() === 0)) {
497 if (u.high32() === 0) {
498 if (v.high32() === 0) {
499 var result = (u / v) | 0;
500 return negateResult ? result.neg64() : result;
506 var u64 = new __Int64(u.high32(), u);
507 var v64 = new __Int64(v.high32(), v);
508 var q64 = new __Int64(0, 0);
509 var r64 = new __Int64(0, 0);
511 __div64(q64, r64, u64, v64);
513 var result = q64.toNumber();
514 return negateResult ? result.neg64() : result;
517 numberPrototype.mod64 = function(x) {
518 var negateResult = false;
521 if ((this.high32() & 0x80000000) != 0) {
523 negateResult = !negateResult;
528 if ((x.high32() & 0x80000000) != 0) {
534 if ((v == 0) && (v.high32() === 0)) {
538 if (u.high32() === 0) {
539 var result = (v.high32() === 0) ? (u % v) : u;
540 return negateResult ? result.neg64() : result;
543 var u64 = new __Int64(u.high32(), u);
544 var v64 = new __Int64(v.high32(), v);
545 var q64 = new __Int64(0, 0);
546 var r64 = new __Int64(0, 0);
548 __div64(q64, r64, u64, v64);
550 var result = r64.toNumber();
551 return negateResult ? result.neg64() : result;
553 })(Number.prototype);
555 vm.java_lang_Number(false);