rt/emul/mini/src/main/resources/org/apidesign/vm4brwsr/emul/lang/java_lang_Number.js
Probably faster to use the conditional operator than to call additional compact function
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 // keeping for compatibility with generated bck2brwsr.js library files
229 // not used since 0.14
230 numberPrototype.compare = function(x) {
234 return (this < x) ? -1 : 1;
238 numberPrototype.compare64 = function(x) {
239 if (this.high32() === x.high32()) {
240 return (this < x) ? -1 : ((this > x) ? 1 : 0);
242 return (this.high32() < x.high32()) ? -1 : 1;
245 numberPrototype.neg64 = function() {
246 var hi = this.high32();
248 if ((hi === 0) && (low < 0)) {
253 low += (low < 0) ? (__m32 + 1) : 0;
254 var ret = hi.next32(low);
258 function __handleDivByZero() {
259 var exception = new vm.java_lang_ArithmeticException;
260 vm.java_lang_ArithmeticException(false).constructor
261 .cons__VLjava_lang_String_2.call(exception, "/ by zero");
266 function __Int64(hi32, lo32) {
267 this.hi32 = hi32 | 0;
268 this.lo32 = lo32 | 0;
270 this.get32 = function(bitIndex) {
274 var selector = bitIndex >>> 5;
292 var shift = bitIndex & 31;
297 return (v1 << (32 - shift)) | (v0 >>> shift);
300 this.get16 = function(bitIndex) {
301 return this.get32(bitIndex) & 0xffff;
304 this.set16 = function(bitIndex, value) {
306 var shift = bitIndex & 15;
307 var svalue = (value & 0xffff) << shift;
308 var smask = 0xffff << shift;
309 var selector = bitIndex >>> 4;
314 this.lo32 = (this.lo32 & ~(smask >>> 16))
318 this.lo32 = (this.lo32 & ~smask) | svalue;
321 this.lo32 = (this.lo32 & ~(smask << 16))
323 this.hi32 = (this.hi32 & ~(smask >>> 16))
327 this.hi32 = (this.hi32 & ~smask) | svalue;
330 this.hi32 = (this.hi32 & ~(smask << 16))
336 this.getDigit = function(index, shift) {
337 return this.get16((index << 4) - shift);
340 this.getTwoDigits = function(index, shift) {
341 return this.get32(((index - 1) << 4) - shift);
344 this.setDigit = function(index, shift, value) {
345 this.set16((index << 4) - shift, value);
348 this.countSignificantDigits = function() {
352 if (this.hi32 === 0) {
353 if (this.lo32 === 0) {
358 remaining = this.lo32;
361 remaining = this.hi32;
368 return (remaining < 65536) ? sd - 1 : sd;
371 this.toNumber = function() {
372 var lo32 = this.lo32;
377 return this.hi32.next32(lo32);
381 function __countLeadingZeroes16(number) {
394 if (number < 16384) {
399 return (number < 32768) ? nlz + 1 : nlz;
402 // q = u / v; r = u - q * v;
404 function __div64(q, r, u, v) {
405 var m = u.countSignificantDigits();
406 var n = v.countSignificantDigits();
411 // v has single digit
412 var vd = v.getDigit(0, 0);
414 for (var i = m - 1; i >= 0; --i) {
415 var ui = (carry << 16) | u.getDigit(i, 0);
419 var qi = (ui / vd) | 0;
420 q.setDigit(i, 0, qi);
421 carry = ui - qi * vd;
437 var nrm = __countLeadingZeroes16(v.getDigit(n - 1, 0));
439 var vd1 = v.getDigit(n - 1, nrm);
440 var vd0 = v.getDigit(n - 2, nrm);
441 for (var j = m - n; j >= 0; --j) {
442 // Calculate qj estimate
443 var ud21 = r.getTwoDigits(j + n, nrm);
444 var ud2 = ud21 >>> 16;
449 var qest = (ud2 === vd1) ? 0xFFFF : ((ud21 / vd1) | 0);
450 var rest = ud21 - qest * vd1;
452 // 0 <= (qest - qj) <= 2
454 // Refine qj estimate
455 var ud0 = r.getDigit(j + n - 2, nrm);
456 while ((qest * vd0) > ((rest * 0x10000) + ud0)) {
461 // 0 <= (qest - qj) <= 1
463 // Multiply and subtract
465 for (var i = 0; i < n; ++i) {
466 var vi = qest * v.getDigit(i, nrm);
467 var ui = r.getDigit(i + j, nrm) - carry - (vi & 0xffff);
468 r.setDigit(i + j, nrm, ui);
469 carry = (vi >>> 16) - (ui >> 16);
471 var uj = ud2 - carry;
479 for (var i = 0; i < n; ++i) {
480 var ui = r.getDigit(i + j, nrm) + v.getDigit(i, nrm)
482 r.setDigit(i + j, nrm, ui);
488 q.setDigit(j, 0, qest);
489 r.setDigit(j + n, nrm, uj);
493 numberPrototype.div32 = function(x) {
498 return (this / x) | 0;
501 numberPrototype.mod32 = function(x) {
509 numberPrototype.div64 = function(x) {
510 var negateResult = false;
513 if ((this.high32() & 0x80000000) != 0) {
515 negateResult = !negateResult;
520 if ((x.high32() & 0x80000000) != 0) {
522 negateResult = !negateResult;
527 if ((v == 0) && (v.high32() === 0)) {
531 if (u.high32() === 0) {
532 if (v.high32() === 0) {
533 var result = (u / v) | 0;
534 return negateResult ? result.neg64() : result;
540 var u64 = new __Int64(u.high32(), u);
541 var v64 = new __Int64(v.high32(), v);
542 var q64 = new __Int64(0, 0);
543 var r64 = new __Int64(0, 0);
545 __div64(q64, r64, u64, v64);
547 var result = q64.toNumber();
548 return negateResult ? result.neg64() : result;
551 numberPrototype.mod64 = function(x) {
552 var negateResult = false;
555 if ((this.high32() & 0x80000000) != 0) {
557 negateResult = !negateResult;
562 if ((x.high32() & 0x80000000) != 0) {
568 if ((v == 0) && (v.high32() === 0)) {
572 if (u.high32() === 0) {
573 var result = (v.high32() === 0) ? (u % v) : u;
574 return negateResult ? result.neg64() : result;
577 var u64 = new __Int64(u.high32(), u);
578 var v64 = new __Int64(v.high32(), v);
579 var q64 = new __Int64(0, 0);
580 var r64 = new __Int64(0, 0);
582 __div64(q64, r64, u64, v64);
584 var result = r64.toNumber();
585 return negateResult ? result.neg64() : result;
587 })(Number.prototype);
589 vm.java_lang_Number(false);