rt/emul/mini/src/main/resources/org/apidesign/vm4brwsr/emul/lang/java_lang_Number.js
Frequent access to mod32 operation defined on the Number.prototype is slowing things down significanty.
1 // empty line needed here
2 (function(numberPrototype) {
3 numberPrototype.add32 = function(x) {
6 numberPrototype.sub32 = function(x) {
9 numberPrototype.mul32 = function(x) {
10 return (((this * (x >> 16)) << 16) + this * (x & 0xFFFF)) | 0;
12 numberPrototype.neg32 = function() {
16 numberPrototype.toInt8 = function() {
17 return (this << 24) >> 24;
19 numberPrototype.toInt16 = function() {
20 return (this << 16) >> 16;
23 var __m32 = 0xFFFFFFFF;
25 numberPrototype.next32 = function(low) {
29 var l = new Number(low);
34 numberPrototype.high32 = function() {
35 return this.hi ? this.hi : (Math.floor(this / (__m32 + 1))) | 0;
37 numberPrototype.toInt32 = function() {
40 numberPrototype.toFP = function() {
41 return this.hi ? this.hi * (__m32 + 1) + this : this;
43 numberPrototype.toLong = function() {
44 var hi = (this / (__m32 + 1)) | 0;
45 var low = (this % (__m32 + 1)) | 0;
54 return hi.next32(low);
57 numberPrototype.toExactString = function() {
59 // check for Long.MIN_VALUE
60 if ((this.hi == (0x80000000 | 0)) && (this == 0)) {
61 return '-9223372036854775808';
64 var a = [6, 9, 2, 7, 6, 9, 4, 9, 2, 4];
67 var neg = this.hi < 0;
76 for (var i = 0; i < a.length; i++) {
78 var low_digit = low % 10;
79 digit = (res % 10) + low_digit;
81 low = Math.floor(low / 10);
82 res = Math.floor(res / 10);
88 s = String(digit).concat(s);
90 s = String(res).concat(s).replace(/^0+/, '');
91 return (neg ? '-' : '').concat(s);
96 numberPrototype.add64 = function(x) {
103 var hi = (this.high32() + x.high32() + carry) | 0;
104 return hi.next32(low);
107 numberPrototype.sub64 = function(x) {
114 var hi = (this.high32() - x.high32() - carry) | 0;
115 return hi.next32(low);
118 numberPrototype.mul64 = function(x) {
119 var low = this.mul32(x);
120 low += (low < 0) ? (__m32 + 1) : 0;
121 // first count upper 32 bits of (this.low * x.low)
125 for (var i = 0; i < 32; i++) {
127 hi_hi += this >>> 16;
128 hi_low += this & 0xFFFF
131 hi_low += (hi_hi & 1) ? 0x8000 : 0;
135 var hi = (hi_hi << 16) + hi_low;
137 var m1 = this.high32().mul32(x);
138 var m2 = this.mul32(x.high32());
139 hi = hi.add32(m1).add32(m2);
141 return hi.next32(low);
144 numberPrototype.and64 = function(x) {
146 low += (low < 0) ? (__m32 + 1) : 0;
147 if (this.hi && x.hi) {
148 var hi = this.hi & x.hi;
149 return hi.next32(low);
155 numberPrototype.or64 = function(x) {
157 low += (low < 0) ? (__m32 + 1) : 0;
158 if (this.hi || x.hi) {
159 var hi = this.hi | x.hi;
160 return hi.next32(low);
166 numberPrototype.xor64 = function(x) {
168 low += (low < 0) ? (__m32 + 1) : 0;
169 if (this.hi || x.hi) {
170 var hi = this.hi ^ x.hi;
171 return hi.next32(low);
177 numberPrototype.shl64 = function(x) {
179 if (x == 0) return this;
181 var hi = this << (x - 32);
184 var hi = this.high32() << x;
185 var low_reminder = this >> (32 - x);
188 low += (low < 0) ? (__m32 + 1) : 0;
189 return hi.next32(low);
193 numberPrototype.shr64 = function(x) {
195 if (x == 0) return this;
197 var low = this.high32() >> (x - 32);
198 low += (low < 0) ? (__m32 + 1) : 0;
201 var low = this >>> x;
202 var hi_reminder = this.high32() << (32 - x);
204 low += (low < 0) ? (__m32 + 1) : 0;
205 var hi = this.high32() >> x;
206 return hi.next32(low);
210 numberPrototype.ushr64 = function(x) {
212 if (x == 0) return this;
214 var low = this.high32() >>> (x - 32);
215 low += (low < 0) ? (__m32 + 1) : 0;
218 var low = this >>> x;
219 var hi_reminder = this.high32() << (32 - x);
221 low += (low < 0) ? (__m32 + 1) : 0;
222 var hi = this.high32() >>> x;
223 return hi.next32(low);
227 // keeping for compatibility with generated bck2brwsr.js library files
228 // not used since 0.14
229 numberPrototype.compare = function(x) {
233 return (this < x) ? -1 : 1;
237 numberPrototype.compare64 = function(x) {
238 if (this.high32() === x.high32()) {
239 return (this < x) ? -1 : ((this > x) ? 1 : 0);
241 return (this.high32() < x.high32()) ? -1 : 1;
244 numberPrototype.neg64 = function() {
245 var hi = this.high32();
247 if ((hi === 0) && (low < 0)) {
252 low += (low < 0) ? (__m32 + 1) : 0;
253 var ret = hi.next32(low);
257 function __handleDivByZero() {
258 var exception = new vm.java_lang_ArithmeticException;
259 vm.java_lang_ArithmeticException(false).constructor
260 .cons__VLjava_lang_String_2.call(exception, "/ by zero");
265 function __Int64(hi32, lo32) {
266 this.hi32 = hi32 | 0;
267 this.lo32 = lo32 | 0;
269 this.get32 = function(bitIndex) {
273 var selector = bitIndex >>> 5;
291 var shift = bitIndex & 31;
296 return (v1 << (32 - shift)) | (v0 >>> shift);
299 this.get16 = function(bitIndex) {
300 return this.get32(bitIndex) & 0xffff;
303 this.set16 = function(bitIndex, value) {
305 var shift = bitIndex & 15;
306 var svalue = (value & 0xffff) << shift;
307 var smask = 0xffff << shift;
308 var selector = bitIndex >>> 4;
313 this.lo32 = (this.lo32 & ~(smask >>> 16))
317 this.lo32 = (this.lo32 & ~smask) | svalue;
320 this.lo32 = (this.lo32 & ~(smask << 16))
322 this.hi32 = (this.hi32 & ~(smask >>> 16))
326 this.hi32 = (this.hi32 & ~smask) | svalue;
329 this.hi32 = (this.hi32 & ~(smask << 16))
335 this.getDigit = function(index, shift) {
336 return this.get16((index << 4) - shift);
339 this.getTwoDigits = function(index, shift) {
340 return this.get32(((index - 1) << 4) - shift);
343 this.setDigit = function(index, shift, value) {
344 this.set16((index << 4) - shift, value);
347 this.countSignificantDigits = function() {
351 if (this.hi32 === 0) {
352 if (this.lo32 === 0) {
357 remaining = this.lo32;
360 remaining = this.hi32;
367 return (remaining < 65536) ? sd - 1 : sd;
370 this.toNumber = function() {
371 var lo32 = this.lo32;
376 return this.hi32.next32(lo32);
380 function __countLeadingZeroes16(number) {
393 if (number < 16384) {
398 return (number < 32768) ? nlz + 1 : nlz;
401 // q = u / v; r = u - q * v;
403 function __div64(q, r, u, v) {
404 var m = u.countSignificantDigits();
405 var n = v.countSignificantDigits();
410 // v has single digit
411 var vd = v.getDigit(0, 0);
413 for (var i = m - 1; i >= 0; --i) {
414 var ui = (carry << 16) | u.getDigit(i, 0);
418 var qi = (ui / vd) | 0;
419 q.setDigit(i, 0, qi);
420 carry = ui - qi * vd;
436 var nrm = __countLeadingZeroes16(v.getDigit(n - 1, 0));
438 var vd1 = v.getDigit(n - 1, nrm);
439 var vd0 = v.getDigit(n - 2, nrm);
440 for (var j = m - n; j >= 0; --j) {
441 // Calculate qj estimate
442 var ud21 = r.getTwoDigits(j + n, nrm);
443 var ud2 = ud21 >>> 16;
448 var qest = (ud2 === vd1) ? 0xFFFF : ((ud21 / vd1) | 0);
449 var rest = ud21 - qest * vd1;
451 // 0 <= (qest - qj) <= 2
453 // Refine qj estimate
454 var ud0 = r.getDigit(j + n - 2, nrm);
455 while ((qest * vd0) > ((rest * 0x10000) + ud0)) {
460 // 0 <= (qest - qj) <= 1
462 // Multiply and subtract
464 for (var i = 0; i < n; ++i) {
465 var vi = qest * v.getDigit(i, nrm);
466 var ui = r.getDigit(i + j, nrm) - carry - (vi & 0xffff);
467 r.setDigit(i + j, nrm, ui);
468 carry = (vi >>> 16) - (ui >> 16);
470 var uj = ud2 - carry;
478 for (var i = 0; i < n; ++i) {
479 var ui = r.getDigit(i + j, nrm) + v.getDigit(i, nrm)
481 r.setDigit(i + j, nrm, ui);
487 q.setDigit(j, 0, qest);
488 r.setDigit(j + n, nrm, uj);
492 numberPrototype.div32 = function(x) {
497 return (this / x) | 0;
500 myNum.mod32 = function(a, 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);