rt/emul/mini/src/main/resources/org/apidesign/vm4brwsr/emul/lang/java_lang_Number.js
1 // empty line needed here
3 (function(numberPrototype) {
8 return (((x * (y >> 16)) << 16) + x * (y & 0xFFFF)) | 0;
10 var __m32 = 0xFFFFFFFF;
12 numberPrototype.next32 = function(low) {
16 var l = new Number(low);
21 numberPrototype.high32 = function() {
22 return this.hi ? this.hi : (Math.floor(this / (__m32 + 1))) | 0;
24 numberPrototype.toFP = function() {
25 return this.hi ? this.hi * (__m32 + 1) + this : this;
27 numberPrototype.toLong = function() {
28 var hi = (this / (__m32 + 1)) | 0;
29 var low = (this % (__m32 + 1)) | 0;
38 return hi.next32(low);
41 numberPrototype.toExactString = function() {
43 // check for Long.MIN_VALUE
44 if ((this.hi == (0x80000000 | 0)) && (this == 0)) {
45 return '-9223372036854775808';
48 var a = [6, 9, 2, 7, 6, 9, 4, 9, 2, 4];
51 var neg = this.hi < 0;
60 for (var i = 0; i < a.length; i++) {
62 var low_digit = low % 10;
63 digit = (res % 10) + low_digit;
65 low = Math.floor(low / 10);
66 res = Math.floor(res / 10);
72 s = String(digit).concat(s);
74 s = String(res).concat(s).replace(/^0+/, '');
75 return (neg ? '-' : '').concat(s);
80 numberPrototype.add64 = function(x) {
87 var hi = (this.high32() + x.high32() + carry) | 0;
88 return hi.next32(low);
91 numberPrototype.sub64 = function(x) {
98 var hi = (this.high32() - x.high32() - carry) | 0;
99 return hi.next32(low);
102 numberPrototype.mul64 = function(x) {
103 var low = mul32(this, x);
104 low += (low < 0) ? (__m32 + 1) : 0;
105 // first count upper 32 bits of (this.low * x.low)
109 for (var i = 0; i < 32; i++) {
111 hi_hi += this >>> 16;
112 hi_low += this & 0xFFFF
115 hi_low += (hi_hi & 1) ? 0x8000 : 0;
119 var hi = (hi_hi << 16) + hi_low;
121 var m1 = mul32(this.high32(), x);
122 var m2 = mul32(this, x.high32());
123 hi = add32(add32(hi, m1), m2);
125 return hi.next32(low);
128 numberPrototype.and64 = function(x) {
130 low += (low < 0) ? (__m32 + 1) : 0;
131 if (this.hi && x.hi) {
132 var hi = this.hi & x.hi;
133 return hi.next32(low);
139 numberPrototype.or64 = function(x) {
141 low += (low < 0) ? (__m32 + 1) : 0;
142 if (this.hi || x.hi) {
143 var hi = this.hi | x.hi;
144 return hi.next32(low);
150 numberPrototype.xor64 = function(x) {
152 low += (low < 0) ? (__m32 + 1) : 0;
153 if (this.hi || x.hi) {
154 var hi = this.hi ^ x.hi;
155 return hi.next32(low);
161 numberPrototype.shl64 = function(x) {
163 if (x == 0) return this;
165 var hi = this << (x - 32);
168 var hi = this.high32() << x;
169 var low_reminder = this >> (32 - x);
172 low += (low < 0) ? (__m32 + 1) : 0;
173 return hi.next32(low);
177 numberPrototype.shr64 = function(x) {
179 if (x == 0) return this;
181 var low = this.high32() >> (x - 32);
182 low += (low < 0) ? (__m32 + 1) : 0;
185 var low = this >>> x;
186 var hi_reminder = this.high32() << (32 - x);
188 low += (low < 0) ? (__m32 + 1) : 0;
189 var hi = this.high32() >> x;
190 return hi.next32(low);
194 numberPrototype.ushr64 = 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.compare64 = function(x) {
212 if (this.high32() === x.high32()) {
213 return (this < x) ? -1 : ((this > x) ? 1 : 0);
215 return (this.high32() < x.high32()) ? -1 : 1;
218 numberPrototype.neg64 = function() {
219 var hi = this.high32();
221 if ((hi === 0) && (low < 0)) {
226 low += (low < 0) ? (__m32 + 1) : 0;
227 var ret = hi.next32(low);
231 function __handleDivByZero() {
232 var exception = new vm.java_lang_ArithmeticException;
233 vm.java_lang_ArithmeticException(false).constructor
234 .cons__VLjava_lang_String_2.call(exception, "/ by zero");
239 function __Int64(hi32, lo32) {
240 this.hi32 = hi32 | 0;
241 this.lo32 = lo32 | 0;
243 this.get32 = function(bitIndex) {
247 var selector = bitIndex >>> 5;
265 var shift = bitIndex & 31;
270 return (v1 << (32 - shift)) | (v0 >>> shift);
273 this.get16 = function(bitIndex) {
274 return this.get32(bitIndex) & 0xffff;
277 this.set16 = function(bitIndex, value) {
279 var shift = bitIndex & 15;
280 var svalue = (value & 0xffff) << shift;
281 var smask = 0xffff << shift;
282 var selector = bitIndex >>> 4;
287 this.lo32 = (this.lo32 & ~(smask >>> 16))
291 this.lo32 = (this.lo32 & ~smask) | svalue;
294 this.lo32 = (this.lo32 & ~(smask << 16))
296 this.hi32 = (this.hi32 & ~(smask >>> 16))
300 this.hi32 = (this.hi32 & ~smask) | svalue;
303 this.hi32 = (this.hi32 & ~(smask << 16))
309 this.getDigit = function(index, shift) {
310 return this.get16((index << 4) - shift);
313 this.getTwoDigits = function(index, shift) {
314 return this.get32(((index - 1) << 4) - shift);
317 this.setDigit = function(index, shift, value) {
318 this.set16((index << 4) - shift, value);
321 this.countSignificantDigits = function() {
325 if (this.hi32 === 0) {
326 if (this.lo32 === 0) {
331 remaining = this.lo32;
334 remaining = this.hi32;
341 return (remaining < 65536) ? sd - 1 : sd;
344 this.toNumber = function() {
345 var lo32 = this.lo32;
350 return this.hi32.next32(lo32);
354 function __countLeadingZeroes16(number) {
367 if (number < 16384) {
372 return (number < 32768) ? nlz + 1 : nlz;
375 // q = u / v; r = u - q * v;
377 function __div64(q, r, u, v) {
378 var m = u.countSignificantDigits();
379 var n = v.countSignificantDigits();
384 // v has single digit
385 var vd = v.getDigit(0, 0);
387 for (var i = m - 1; i >= 0; --i) {
388 var ui = (carry << 16) | u.getDigit(i, 0);
392 var qi = (ui / vd) | 0;
393 q.setDigit(i, 0, qi);
394 carry = ui - qi * vd;
410 var nrm = __countLeadingZeroes16(v.getDigit(n - 1, 0));
412 var vd1 = v.getDigit(n - 1, nrm);
413 var vd0 = v.getDigit(n - 2, nrm);
414 for (var j = m - n; j >= 0; --j) {
415 // Calculate qj estimate
416 var ud21 = r.getTwoDigits(j + n, nrm);
417 var ud2 = ud21 >>> 16;
422 var qest = (ud2 === vd1) ? 0xFFFF : ((ud21 / vd1) | 0);
423 var rest = ud21 - qest * vd1;
425 // 0 <= (qest - qj) <= 2
427 // Refine qj estimate
428 var ud0 = r.getDigit(j + n - 2, nrm);
429 while ((qest * vd0) > ((rest * 0x10000) + ud0)) {
434 // 0 <= (qest - qj) <= 1
436 // Multiply and subtract
438 for (var i = 0; i < n; ++i) {
439 var vi = qest * v.getDigit(i, nrm);
440 var ui = r.getDigit(i + j, nrm) - carry - (vi & 0xffff);
441 r.setDigit(i + j, nrm, ui);
442 carry = (vi >>> 16) - (ui >> 16);
444 var uj = ud2 - carry;
452 for (var i = 0; i < n; ++i) {
453 var ui = r.getDigit(i + j, nrm) + v.getDigit(i, nrm)
455 r.setDigit(i + j, nrm, ui);
461 q.setDigit(j, 0, qest);
462 r.setDigit(j + n, nrm, uj);
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;
545 var b = numberPrototype['__bit64'] = {};
546 b.add64 = function(x,y) { return x.add64(y); };
547 b.sub64 = function(x,y) { return x.sub64(y); };
548 b.mul64 = function(x,y) { return x.mul64(y); };
549 b.div64 = function(x,y) { return x.div64(y); };
550 b.mod64 = function(x,y) { return x.mod64(y); };
551 b.and64 = function(x,y) { return x.and64(y); };
552 b.or64 = function(x,y) { return x.or64(y); };
553 b.xor64 = function(x,y) { return x.xor64(y); };
554 b.neg64 = function(x) { return x.neg64(); };
555 b.shl64 = function(x,y) { return x.shl64(y); };
556 b.shr64 = function(x,y) { return x.shr64(y); };
557 b.ushr64 = function(x,y) { return x.ushr64(y); };
558 b.compare64 = function(x,y) { return y.compare64(x); };
559 })(Number.prototype);
561 vm.java_lang_Number(false);