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 var hi = this << (x - 32);
183 var hi = this.high32() << x;
184 var low_reminder = this >> (32 - x);
187 low += (low < 0) ? (__m32 + 1) : 0;
188 return hi.next32(low);
192 numberPrototype.shr64 = function(x) {
194 var low = this.high32() >> (x - 32);
195 low += (low < 0) ? (__m32 + 1) : 0;
199 var hi_reminder = this.high32() << (32 - x);
201 low += (low < 0) ? (__m32 + 1) : 0;
202 var hi = this.high32() >> x;
203 return hi.next32(low);
207 numberPrototype.ushr64 = function(x) {
209 var low = this.high32() >>> (x - 32);
210 low += (low < 0) ? (__m32 + 1) : 0;
213 var low = this >>> x;
214 var hi_reminder = this.high32() << (32 - x);
216 low += (low < 0) ? (__m32 + 1) : 0;
217 var hi = this.high32() >>> x;
218 return hi.next32(low);
222 numberPrototype.compare64 = function(x) {
223 if (this.high32() === x.high32()) {
224 return (this < x) ? -1 : ((this > x) ? 1 : 0);
226 return (this.high32() < x.high32()) ? -1 : 1;
229 numberPrototype.neg64 = function() {
230 var hi = this.high32();
232 if ((hi === 0) && (low < 0)) {
237 low += (low < 0) ? (__m32 + 1) : 0;
238 var ret = hi.next32(low);
242 function __handleDivByZero() {
243 var exception = new vm.java_lang_ArithmeticException;
244 vm.java_lang_ArithmeticException(false).constructor
245 .cons__VLjava_lang_String_2.call(exception, "/ by zero");
250 function __Int64(hi32, lo32) {
251 this.hi32 = hi32 | 0;
252 this.lo32 = lo32 | 0;
254 this.get32 = function(bitIndex) {
258 var selector = bitIndex >>> 5;
276 var shift = bitIndex & 31;
281 return (v1 << (32 - shift)) | (v0 >>> shift);
284 this.get16 = function(bitIndex) {
285 return this.get32(bitIndex) & 0xffff;
288 this.set16 = function(bitIndex, value) {
290 var shift = bitIndex & 15;
291 var svalue = (value & 0xffff) << shift;
292 var smask = 0xffff << shift;
293 var selector = bitIndex >>> 4;
298 this.lo32 = (this.lo32 & ~(smask >>> 16))
302 this.lo32 = (this.lo32 & ~smask) | svalue;
305 this.lo32 = (this.lo32 & ~(smask << 16))
307 this.hi32 = (this.hi32 & ~(smask >>> 16))
311 this.hi32 = (this.hi32 & ~smask) | svalue;
314 this.hi32 = (this.hi32 & ~(smask << 16))
320 this.getDigit = function(index, shift) {
321 return this.get16((index << 4) - shift);
324 this.getTwoDigits = function(index, shift) {
325 return this.get32(((index - 1) << 4) - shift);
328 this.setDigit = function(index, shift, value) {
329 this.set16((index << 4) - shift, value);
332 this.countSignificantDigits = function() {
336 if (this.hi32 === 0) {
337 if (this.lo32 === 0) {
342 remaining = this.lo32;
345 remaining = this.hi32;
352 return (remaining < 65536) ? sd - 1 : sd;
355 this.toNumber = function() {
356 var lo32 = this.lo32;
361 return this.hi32.next32(lo32);
365 function __countLeadingZeroes16(number) {
378 if (number < 16384) {
383 return (number < 32768) ? nlz + 1 : nlz;
386 // q = u / v; r = u - q * v;
388 function __div64(q, r, u, v) {
389 var m = u.countSignificantDigits();
390 var n = v.countSignificantDigits();
395 // v has single digit
396 var vd = v.getDigit(0, 0);
398 for (var i = m - 1; i >= 0; --i) {
399 var ui = (carry << 16) | u.getDigit(i, 0);
403 var qi = (ui / vd) | 0;
404 q.setDigit(i, 0, qi);
405 carry = ui - qi * vd;
421 var nrm = __countLeadingZeroes16(v.getDigit(n - 1, 0));
423 var vd1 = v.getDigit(n - 1, nrm);
424 var vd0 = v.getDigit(n - 2, nrm);
425 for (var j = m - n; j >= 0; --j) {
426 // Calculate qj estimate
427 var ud21 = r.getTwoDigits(j + n, nrm);
428 var ud2 = ud21 >>> 16;
433 var qest = (ud2 === vd1) ? 0xFFFF : ((ud21 / vd1) | 0);
434 var rest = ud21 - qest * vd1;
436 // 0 <= (qest - qj) <= 2
438 // Refine qj estimate
439 var ud0 = r.getDigit(j + n - 2, nrm);
440 while ((qest * vd0) > ((rest * 0x10000) + ud0)) {
445 // 0 <= (qest - qj) <= 1
447 // Multiply and subtract
449 for (var i = 0; i < n; ++i) {
450 var vi = qest * v.getDigit(i, nrm);
451 var ui = r.getDigit(i + j, nrm) - carry - (vi & 0xffff);
452 r.setDigit(i + j, nrm, ui);
453 carry = (vi >>> 16) - (ui >> 16);
455 var uj = ud2 - carry;
463 for (var i = 0; i < n; ++i) {
464 var ui = r.getDigit(i + j, nrm) + v.getDigit(i, nrm)
466 r.setDigit(i + j, nrm, ui);
472 q.setDigit(j, 0, qest);
473 r.setDigit(j + n, nrm, uj);
477 numberPrototype.div32 = function(x) {
482 return (this / x) | 0;
485 numberPrototype.mod32 = function(x) {
493 numberPrototype.div64 = function(x) {
494 var negateResult = false;
497 if ((this.high32() & 0x80000000) != 0) {
499 negateResult = !negateResult;
504 if ((x.high32() & 0x80000000) != 0) {
506 negateResult = !negateResult;
511 if ((v == 0) && (v.high32() === 0)) {
515 if (u.high32() === 0) {
516 if (v.high32() === 0) {
517 var result = (u / v) | 0;
518 return negateResult ? result.neg64() : result;
524 var u64 = new __Int64(u.high32(), u);
525 var v64 = new __Int64(v.high32(), v);
526 var q64 = new __Int64(0, 0);
527 var r64 = new __Int64(0, 0);
529 __div64(q64, r64, u64, v64);
531 var result = q64.toNumber();
532 return negateResult ? result.neg64() : result;
535 numberPrototype.mod64 = function(x) {
536 var negateResult = false;
539 if ((this.high32() & 0x80000000) != 0) {
541 negateResult = !negateResult;
546 if ((x.high32() & 0x80000000) != 0) {
552 if ((v == 0) && (v.high32() === 0)) {
556 if (u.high32() === 0) {
557 var result = (v.high32() === 0) ? (u % v) : u;
558 return negateResult ? result.neg64() : result;
561 var u64 = new __Int64(u.high32(), u);
562 var v64 = new __Int64(v.high32(), v);
563 var q64 = new __Int64(0, 0);
564 var r64 = new __Int64(0, 0);
566 __div64(q64, r64, u64, v64);
568 var result = r64.toNumber();
569 return negateResult ? result.neg64() : result;
571 })(Number.prototype);
573 vm.java_lang_Number(false);