rt/emul/mini/src/main/resources/org/apidesign/vm4brwsr/emul/lang/java_lang_Number.js
1 // empty line needed here
3 (function(numberPrototype) {
4 numberPrototype.add32 = function(x) {
7 numberPrototype.sub32 = function(x) {
10 numberPrototype.mul32 = function(x) {
11 return (((this * (x >> 16)) << 16) + this * (x & 0xFFFF)) | 0;
13 numberPrototype.neg32 = function() {
17 numberPrototype.toInt8 = function() {
18 return (this << 24) >> 24;
20 numberPrototype.toInt16 = function() {
21 return (this << 16) >> 16;
24 var __m32 = 0xFFFFFFFF;
26 numberPrototype.next32 = function(low) {
30 var l = new Number(low);
35 numberPrototype.high32 = function() {
36 return this.hi ? this.hi : (Math.floor(this / (__m32 + 1))) | 0;
38 numberPrototype.toInt32 = function() {
41 numberPrototype.toFP = function() {
42 return this.hi ? this.hi * (__m32 + 1) + this : this;
44 numberPrototype.toLong = function() {
45 var hi = (this / (__m32 + 1)) | 0;
46 var low = (this % (__m32 + 1)) | 0;
55 return hi.next32(low);
58 numberPrototype.toExactString = function() {
60 // check for Long.MIN_VALUE
61 if ((this.hi == (0x80000000 | 0)) && (this == 0)) {
62 return '-9223372036854775808';
65 var a = [6, 9, 2, 7, 6, 9, 4, 9, 2, 4];
68 var neg = this.hi < 0;
77 for (var i = 0; i < a.length; i++) {
79 var low_digit = low % 10;
80 digit = (res % 10) + low_digit;
82 low = Math.floor(low / 10);
83 res = Math.floor(res / 10);
89 s = String(digit).concat(s);
91 s = String(res).concat(s).replace(/^0+/, '');
92 return (neg ? '-' : '').concat(s);
97 numberPrototype.add64 = function(x) {
104 var hi = (this.high32() + x.high32() + carry) | 0;
105 return hi.next32(low);
108 numberPrototype.sub64 = function(x) {
115 var hi = (this.high32() - x.high32() - carry) | 0;
116 return hi.next32(low);
119 numberPrototype.mul64 = function(x) {
120 var low = this.mul32(x);
121 low += (low < 0) ? (__m32 + 1) : 0;
122 // first count upper 32 bits of (this.low * x.low)
126 for (var i = 0; i < 32; i++) {
128 hi_hi += this >>> 16;
129 hi_low += this & 0xFFFF
132 hi_low += (hi_hi & 1) ? 0x8000 : 0;
136 var hi = (hi_hi << 16) + hi_low;
138 var m1 = this.high32().mul32(x);
139 var m2 = this.mul32(x.high32());
140 hi = hi.add32(m1).add32(m2);
142 return hi.next32(low);
145 numberPrototype.and64 = function(x) {
147 low += (low < 0) ? (__m32 + 1) : 0;
148 if (this.hi && x.hi) {
149 var hi = this.hi & x.hi;
150 return hi.next32(low);
156 numberPrototype.or64 = function(x) {
158 low += (low < 0) ? (__m32 + 1) : 0;
159 if (this.hi || x.hi) {
160 var hi = this.hi | x.hi;
161 return hi.next32(low);
167 numberPrototype.xor64 = function(x) {
169 low += (low < 0) ? (__m32 + 1) : 0;
170 if (this.hi || x.hi) {
171 var hi = this.hi ^ x.hi;
172 return hi.next32(low);
178 numberPrototype.shl64 = function(x) {
180 if (x == 0) return this;
182 var hi = this << (x - 32);
185 var hi = this.high32() << x;
186 var low_reminder = this >> (32 - x);
189 low += (low < 0) ? (__m32 + 1) : 0;
190 return hi.next32(low);
194 numberPrototype.shr64 = function(x) {
196 if (x == 0) return this;
198 var low = this.high32() >> (x - 32);
199 low += (low < 0) ? (__m32 + 1) : 0;
202 var low = this >>> x;
203 var hi_reminder = this.high32() << (32 - x);
205 low += (low < 0) ? (__m32 + 1) : 0;
206 var hi = this.high32() >> x;
207 return hi.next32(low);
211 numberPrototype.ushr64 = function(x) {
213 if (x == 0) return this;
215 var low = this.high32() >>> (x - 32);
216 low += (low < 0) ? (__m32 + 1) : 0;
219 var low = this >>> x;
220 var hi_reminder = this.high32() << (32 - x);
222 low += (low < 0) ? (__m32 + 1) : 0;
223 var hi = this.high32() >>> x;
224 return hi.next32(low);
228 numberPrototype.compare = function(x) {
232 return (this < x) ? -1 : 1;
236 numberPrototype.compare64 = function(x) {
237 if (this.high32() === x.high32()) {
238 return (this < x) ? -1 : ((this > x) ? 1 : 0);
240 return (this.high32() < x.high32()) ? -1 : 1;
243 numberPrototype.neg64 = function() {
244 var hi = this.high32();
246 if ((hi === 0) && (low < 0)) {
251 low += (low < 0) ? (__m32 + 1) : 0;
252 var ret = hi.next32(low);
256 function __handleDivByZero() {
257 var exception = new vm.java_lang_ArithmeticException;
258 vm.java_lang_ArithmeticException(false).constructor
259 .cons__VLjava_lang_String_2.call(exception, "/ by zero");
264 function __Int64(hi32, lo32) {
265 this.hi32 = hi32 | 0;
266 this.lo32 = lo32 | 0;
268 this.get32 = function(bitIndex) {
272 var selector = bitIndex >>> 5;
290 var shift = bitIndex & 31;
295 return (v1 << (32 - shift)) | (v0 >>> shift);
298 this.get16 = function(bitIndex) {
299 return this.get32(bitIndex) & 0xffff;
302 this.set16 = function(bitIndex, value) {
304 var shift = bitIndex & 15;
305 var svalue = (value & 0xffff) << shift;
306 var smask = 0xffff << shift;
307 var selector = bitIndex >>> 4;
312 this.lo32 = (this.lo32 & ~(smask >>> 16))
316 this.lo32 = (this.lo32 & ~smask) | svalue;
319 this.lo32 = (this.lo32 & ~(smask << 16))
321 this.hi32 = (this.hi32 & ~(smask >>> 16))
325 this.hi32 = (this.hi32 & ~smask) | svalue;
328 this.hi32 = (this.hi32 & ~(smask << 16))
334 this.getDigit = function(index, shift) {
335 return this.get16((index << 4) - shift);
338 this.getTwoDigits = function(index, shift) {
339 return this.get32(((index - 1) << 4) - shift);
342 this.setDigit = function(index, shift, value) {
343 this.set16((index << 4) - shift, value);
346 this.countSignificantDigits = function() {
350 if (this.hi32 === 0) {
351 if (this.lo32 === 0) {
356 remaining = this.lo32;
359 remaining = this.hi32;
366 return (remaining < 65536) ? sd - 1 : sd;
369 this.toNumber = function() {
370 var lo32 = this.lo32;
375 return this.hi32.next32(lo32);
379 function __countLeadingZeroes16(number) {
392 if (number < 16384) {
397 return (number < 32768) ? nlz + 1 : nlz;
400 // q = u / v; r = u - q * v;
402 function __div64(q, r, u, v) {
403 var m = u.countSignificantDigits();
404 var n = v.countSignificantDigits();
409 // v has single digit
410 var vd = v.getDigit(0, 0);
412 for (var i = m - 1; i >= 0; --i) {
413 var ui = (carry << 16) | u.getDigit(i, 0);
417 var qi = (ui / vd) | 0;
418 q.setDigit(i, 0, qi);
419 carry = ui - qi * vd;
435 var nrm = __countLeadingZeroes16(v.getDigit(n - 1, 0));
437 var vd1 = v.getDigit(n - 1, nrm);
438 var vd0 = v.getDigit(n - 2, nrm);
439 for (var j = m - n; j >= 0; --j) {
440 // Calculate qj estimate
441 var ud21 = r.getTwoDigits(j + n, nrm);
442 var ud2 = ud21 >>> 16;
447 var qest = (ud2 === vd1) ? 0xFFFF : ((ud21 / vd1) | 0);
448 var rest = ud21 - qest * vd1;
450 // 0 <= (qest - qj) <= 2
452 // Refine qj estimate
453 var ud0 = r.getDigit(j + n - 2, nrm);
454 while ((qest * vd0) > ((rest * 0x10000) + ud0)) {
459 // 0 <= (qest - qj) <= 1
461 // Multiply and subtract
463 for (var i = 0; i < n; ++i) {
464 var vi = qest * v.getDigit(i, nrm);
465 var ui = r.getDigit(i + j, nrm) - carry - (vi & 0xffff);
466 r.setDigit(i + j, nrm, ui);
467 carry = (vi >>> 16) - (ui >> 16);
469 var uj = ud2 - carry;
477 for (var i = 0; i < n; ++i) {
478 var ui = r.getDigit(i + j, nrm) + v.getDigit(i, nrm)
480 r.setDigit(i + j, nrm, ui);
486 q.setDigit(j, 0, qest);
487 r.setDigit(j + n, nrm, uj);
491 numberPrototype.div32 = function(x) {
496 return (this / x) | 0;
499 numberPrototype.mod32 = function(x) {
507 numberPrototype.div64 = function(x) {
508 var negateResult = false;
511 if ((this.high32() & 0x80000000) != 0) {
513 negateResult = !negateResult;
518 if ((x.high32() & 0x80000000) != 0) {
520 negateResult = !negateResult;
525 if ((v == 0) && (v.high32() === 0)) {
529 if (u.high32() === 0) {
530 if (v.high32() === 0) {
531 var result = (u / v) | 0;
532 return negateResult ? result.neg64() : result;
538 var u64 = new __Int64(u.high32(), u);
539 var v64 = new __Int64(v.high32(), v);
540 var q64 = new __Int64(0, 0);
541 var r64 = new __Int64(0, 0);
543 __div64(q64, r64, u64, v64);
545 var result = q64.toNumber();
546 return negateResult ? result.neg64() : result;
549 numberPrototype.mod64 = function(x) {
550 var negateResult = false;
553 if ((this.high32() & 0x80000000) != 0) {
555 negateResult = !negateResult;
560 if ((x.high32() & 0x80000000) != 0) {
566 if ((v == 0) && (v.high32() === 0)) {
570 if (u.high32() === 0) {
571 var result = (v.high32() === 0) ? (u % v) : u;
572 return negateResult ? result.neg64() : result;
575 var u64 = new __Int64(u.high32(), u);
576 var v64 = new __Int64(v.high32(), v);
577 var q64 = new __Int64(0, 0);
578 var r64 = new __Int64(0, 0);
580 __div64(q64, r64, u64, v64);
582 var result = r64.toNumber();
583 return negateResult ? result.neg64() : result;
585 })(Number.prototype);
587 vm.java_lang_Number(false);