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) ? (Math.floor(this / (__m32+1))) | 0 : 0;
32 return hi.next32(Math.floor(this % (__m32+1)));
35 Number.prototype.toExactString = function() {
37 // check for Long.MIN_VALUE
38 if ((this.hi == (0x80000000 | 0)) && (this == 0)) {
39 return '-9223372036854775808';
42 var a = [ 6,9,2,7,6,9,4,9,2,4 ];
45 var neg = this.hi < 0;
54 for (var i = 0; i < a.length; i++) {
56 var low_digit = low % 10;
57 digit = (res % 10) + low_digit;
59 low = Math.floor(low / 10);
60 res = Math.floor(res / 10);
66 s = String(digit).concat(s);
68 s = String(res).concat(s).replace(/^0+/, '');
69 return (neg ? '-' : '').concat(s);
74 Number.prototype.add64 = function(x) {
81 var hi = (this.high32() + x.high32() + carry) | 0;
82 return hi.next32(low);
85 Number.prototype.sub64 = function(x) {
92 var hi = (this.high32() - x.high32() - carry) | 0;
93 return hi.next32(low);
96 Number.prototype.mul64 = function(x) {
97 var low = this.mul32(x);
98 low += (low < 0) ? (__m32+1) : 0;
99 // first count upper 32 bits of (this.low * x.low)
103 for (var i = 0; i < 32; i++) {
105 hi_hi += this >>> 16;
106 hi_low += this & 0xFFFF
109 hi_low += (hi_hi & 1) ? 0x8000 : 0;
113 var hi = (hi_hi << 16) + hi_low;
115 var m1 = this.high32().mul32(x);
116 var m2 = this.mul32(x.high32());
117 hi = hi.add32(m1).add32(m2);
119 return hi.next32(low);
122 Number.prototype.and64 = function(x) {
124 low += (low < 0) ? (__m32+1) : 0;
125 if (this.hi && x.hi) {
126 var hi = this.hi & x.hi;
127 return hi.next32(low);
132 Number.prototype.or64 = function(x) {
134 low += (low < 0) ? (__m32+1) : 0;
135 if (this.hi || x.hi) {
136 var hi = this.hi | x.hi;
137 return hi.next32(low);
142 Number.prototype.xor64 = function(x) {
144 low += (low < 0) ? (__m32+1) : 0;
145 if (this.hi || x.hi) {
146 var hi = this.hi ^ x.hi;
147 return hi.next32(low);
152 Number.prototype.shl64 = function(x) {
154 var hi = this << (x - 32);
157 var hi = this.high32() << x;
158 var low_reminder = this >> (32 - x);
161 low += (low < 0) ? (__m32+1) : 0;
162 return hi.next32(low);
166 Number.prototype.shr64 = function(x) {
168 var low = this.high32() >> (x - 32);
169 low += (low < 0) ? (__m32+1) : 0;
173 var hi_reminder = this.high32() << (32 - x);
175 low += (low < 0) ? (__m32+1) : 0;
176 var hi = this.high32() >> x;
177 return hi.next32(low);
181 Number.prototype.ushr64 = function(x) {
183 var low = this.high32() >>> (x - 32);
184 low += (low < 0) ? (__m32+1) : 0;
187 var low = this >>> x;
188 var hi_reminder = this.high32() << (32 - x);
190 low += (low < 0) ? (__m32+1) : 0;
191 var hi = this.high32() >>> x;
192 return hi.next32(low);
196 Number.prototype.compare64 = function(x) {
197 if (this.high32() === x.high32()) {
198 return (this < x) ? -1 : ((this > x) ? 1 : 0);
200 return (this.high32() < x.high32()) ? -1 : 1;
203 Number.prototype.neg64 = function() {
204 var hi = this.high32();
206 if ((hi === 0) && (low < 0)) { return -low; }
209 low += (low < 0) ? (__m32+1) : 0;
210 var ret = hi.next32(low);
214 (function(numberPrototype) {
215 function __handleDivByZero() {
216 var exception = new vm.java_lang_ArithmeticException;
217 vm.java_lang_ArithmeticException(false).constructor
218 .cons__VLjava_lang_String_2.call(exception, "/ by zero");
223 function __Int64(hi32, lo32) {
224 this.hi32 = hi32 | 0;
225 this.lo32 = lo32 | 0;
227 this.get32 = function(bitIndex) {
231 var selector = bitIndex >>> 5;
249 var shift = bitIndex & 31;
254 return (v1 << (32 - shift)) | (v0 >>> shift);
257 this.get16 = function(bitIndex) {
258 return this.get32(bitIndex) & 0xffff;
261 this.set16 = function(bitIndex, value) {
263 var shift = bitIndex & 15;
264 var svalue = (value & 0xffff) << shift;
265 var smask = 0xffff << shift;
266 var selector = bitIndex >>> 4;
271 this.lo32 = (this.lo32 & ~(smask >>> 16))
275 this.lo32 = (this.lo32 & ~smask) | svalue;
278 this.lo32 = (this.lo32 & ~(smask << 16))
280 this.hi32 = (this.hi32 & ~(smask >>> 16))
284 this.hi32 = (this.hi32 & ~smask) | svalue;
287 this.hi32 = (this.hi32 & ~(smask << 16))
293 this.getDigit = function(index, shift) {
294 return this.get16((index << 4) - shift);
297 this.getTwoDigits = function(index, shift) {
298 return this.get32(((index - 1) << 4) - shift);
301 this.setDigit = function(index, shift, value) {
302 this.set16((index << 4) - shift, value);
305 this.countSignificantDigits = function() {
309 if (this.hi32 === 0) {
310 if (this.lo32 === 0) {
315 remaining = this.lo32;
318 remaining = this.hi32;
325 return (remaining < 65536) ? sd - 1 : sd;
328 this.toNumber = function() {
329 var lo32 = this.lo32;
334 return this.hi32.next32(lo32);
338 function __countLeadingZeroes16(number) {
351 if (number < 16384) {
356 return (number < 32768) ? nlz + 1 : nlz;
359 // q = u / v; r = u - q * v;
361 function __div64(q, r, u, v) {
362 var m = u.countSignificantDigits();
363 var n = v.countSignificantDigits();
368 // v has single digit
369 var vd = v.getDigit(0, 0);
371 for (var i = m - 1; i >= 0; --i) {
372 var ui = (carry << 16) | u.getDigit(i, 0);
376 var qi = (ui / vd) | 0;
377 q.setDigit(i, 0, qi);
378 carry = ui - qi * vd;
394 var nrm = __countLeadingZeroes16(v.getDigit(n - 1, 0));
396 var vd1 = v.getDigit(n - 1, nrm);
397 var vd0 = v.getDigit(n - 2, nrm);
398 for (var j = m - n; j >= 0; --j) {
399 // Calculate qj estimate
400 var ud21 = r.getTwoDigits(j + n, nrm);
401 var ud2 = ud21 >>> 16;
406 var qest = (ud2 === vd1) ? 0xFFFF : ((ud21 / vd1) | 0);
407 var rest = ud21 - qest * vd1;
409 // 0 <= (qest - qj) <= 2
411 // Refine qj estimate
412 var ud0 = r.getDigit(j + n - 2, nrm);
413 while ((qest * vd0) > ((rest * 0x10000) + ud0)) {
418 // 0 <= (qest - qj) <= 1
420 // Multiply and subtract
422 for (var i = 0; i < n; ++i) {
423 var vi = qest * v.getDigit(i, nrm);
424 var ui = r.getDigit(i + j, nrm) - carry - (vi & 0xffff);
425 r.setDigit(i + j, nrm, ui);
426 carry = (vi >>> 16) - (ui >> 16);
428 var uj = ud2 - carry;
436 for (var i = 0; i < n; ++i) {
437 var ui = r.getDigit(i + j, nrm) + v.getDigit(i, nrm)
439 r.setDigit(i + j, nrm, ui);
445 q.setDigit(j, 0, qest);
446 r.setDigit(j + n, nrm, uj);
450 numberPrototype.div32 = function(x) {
455 return (this / x) | 0;
458 numberPrototype.mod32 = function(x) {
466 numberPrototype.div64 = function(x) {
467 var negateResult = false;
470 if ((this.high32() & 0x80000000) != 0) {
472 negateResult = !negateResult;
477 if ((x.high32() & 0x80000000) != 0) {
479 negateResult = !negateResult;
484 if ((v === 0) && (v.high32() === 0)) {
488 if (u.high32() === 0) {
489 if (v.high32() === 0) {
490 var result = (u / v) | 0;
491 return negateResult ? result.neg64() : result;
497 var u64 = new __Int64(u.high32(), u);
498 var v64 = new __Int64(v.high32(), v);
499 var q64 = new __Int64(0, 0);
500 var r64 = new __Int64(0, 0);
502 __div64(q64, r64, u64, v64);
504 var result = q64.toNumber();
505 return negateResult ? result.neg64() : result;
508 numberPrototype.mod64 = function(x) {
509 var negateResult = false;
512 if ((this.high32() & 0x80000000) != 0) {
514 negateResult = !negateResult;
519 if ((x.high32() & 0x80000000) != 0) {
525 if ((v === 0) && (v.high32() === 0)) {
529 if (u.high32() === 0) {
530 var result = (v.high32() === 0) ? (u % v) : u;
531 return negateResult ? result.neg64() : result;
534 var u64 = new __Int64(u.high32(), u);
535 var v64 = new __Int64(v.high32(), v);
536 var q64 = new __Int64(0, 0);
537 var r64 = new __Int64(0, 0);
539 __div64(q64, r64, u64, v64);
541 var result = r64.toNumber();
542 return negateResult ? result.neg64() : result;
544 })(Number.prototype);