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;
8 Number.prototype.toInt8 = function() { return (this << 24) >> 24; };
9 Number.prototype.toInt16 = function() { return (this << 16) >> 16; };
11 var __m32 = 0xFFFFFFFF;
13 Number.prototype.next32 = function(low) {
17 var l = new Number(low);
22 Number.prototype.high32 = function() {
23 return this.hi ? this.hi : (Math.floor(this / (__m32+1))) | 0;
25 Number.prototype.toInt32 = function() { return this | 0; };
26 Number.prototype.toFP = function() {
27 return this.hi ? this.hi * (__m32+1) + this : this;
29 Number.prototype.toLong = function() {
30 var hi = (this > __m32) ? (Math.floor(this / (__m32+1))) | 0 : 0;
31 return hi.next32(Math.floor(this % (__m32+1)));
34 Number.prototype.toExactString = function() {
37 var a = [ 6,9,2,7,6,9,4,9,2,4 ];
40 var neg = this.hi < 0;
49 for (var i = 0; i < a.length; i++) {
51 var low_digit = low % 10;
52 digit = (res % 10) + low_digit;
54 low = Math.floor(low / 10);
55 res = Math.floor(res / 10);
61 s = String(digit).concat(s);
63 return (neg ? '-' : '').concat(res).concat(s);
68 Number.prototype.add64 = function(x) {
75 var hi = (this.high32() + x.high32() + carry) | 0;
76 return hi.next32(low);
79 Number.prototype.sub64 = function(x) {
86 var hi = (this.high32() - x.high32() - carry) | 0;
87 return hi.next32(low);
90 Number.prototype.mul64 = function(x) {
91 var low = this.mul32(x);
92 low += (low < 0) ? (__m32+1) : 0;
93 // first count upper 32 bits of (this.low * x.low)
97 for (var i = 0; i < 32; i++) {
100 hi_low += this & 0xFFFF
103 hi_low += (hi_hi & 1) ? 0x8000 : 0;
107 var hi = (hi_hi << 16) + hi_low;
109 var m1 = this.high32().mul32(x);
110 var m2 = this.mul32(x.high32());
111 hi = hi.add32(m1).add32(m2);
113 return hi.next32(low);
116 Number.prototype.and64 = function(x) {
118 low += (low < 0) ? (__m32+1) : 0;
119 if (this.hi && x.hi) {
120 var hi = this.hi & x.hi;
121 return hi.next32(low);
126 Number.prototype.or64 = function(x) {
128 low += (low < 0) ? (__m32+1) : 0;
129 if (this.hi || x.hi) {
130 var hi = this.hi | x.hi;
131 return hi.next32(low);
136 Number.prototype.xor64 = function(x) {
138 low += (low < 0) ? (__m32+1) : 0;
139 if (this.hi || x.hi) {
140 var hi = this.hi ^ x.hi;
141 return hi.next32(low);
146 Number.prototype.shl64 = function(x) {
148 var hi = this << (x - 32);
151 var hi = this.high32() << x;
152 var low_reminder = this >> (32 - x);
155 low += (low < 0) ? (__m32+1) : 0;
156 return hi.next32(low);
160 Number.prototype.shr64 = function(x) {
162 var low = this.high32() >> (x - 32);
163 low += (low < 0) ? (__m32+1) : 0;
167 var hi_reminder = this.high32() << (32 - x);
169 low += (low < 0) ? (__m32+1) : 0;
170 var hi = this.high32() >> x;
171 return hi.next32(low);
175 Number.prototype.ushr64 = function(x) {
177 var low = this.high32() >>> (x - 32);
178 low += (low < 0) ? (__m32+1) : 0;
181 var low = this >>> x;
182 var hi_reminder = this.high32() << (32 - x);
184 low += (low < 0) ? (__m32+1) : 0;
185 var hi = this.high32() >>> x;
186 return hi.next32(low);
190 Number.prototype.compare64 = function(x) {
191 if (this.hi == x.hi) {
192 return (this == x) ? 0 : ((this < x) ? -1 : 1);
194 return (this.hi < x.hi) ? -1 : 1;
197 Number.prototype.neg64 = function() {
198 var hi = this.high32();
200 if ((hi === 0) && (low < 0)) { return -low; }
203 low += (low < 0) ? (__m32+1) : 0;
204 var ret = hi.next32(low);
208 (function(numberPrototype) {
209 function __Int64(hi32, lo32) {
210 this.hi32 = hi32 | 0;
211 this.lo32 = lo32 | 0;
213 this.get32 = function(bitIndex) {
217 var selector = bitIndex >>> 5;
235 var shift = bitIndex & 31;
240 return (v1 << (32 - shift)) | (v0 >>> shift);
243 this.get16 = function(bitIndex) {
244 return this.get32(bitIndex) & 0xffff;
247 this.set16 = function(bitIndex, value) {
249 var shift = bitIndex & 15;
250 var svalue = (value & 0xffff) << shift;
251 var smask = 0xffff << shift;
252 var selector = bitIndex >>> 4;
257 this.lo32 = (this.lo32 & ~(smask >>> 16))
261 this.lo32 = (this.lo32 & ~smask) | svalue;
264 this.lo32 = (this.lo32 & ~(smask << 16))
266 this.hi32 = (this.hi32 & ~(smask >>> 16))
270 this.hi32 = (this.hi32 & ~smask) | svalue;
273 this.hi32 = (this.hi32 & ~(smask << 16))
279 this.getDigit = function(index, shift) {
280 return this.get16((index << 4) - shift);
283 this.getTwoDigits = function(index, shift) {
284 return this.get32(((index - 1) << 4) - shift);
287 this.setDigit = function(index, shift, value) {
288 this.set16((index << 4) - shift, value);
291 this.countSignificantDigits = function() {
295 if (this.hi32 === 0) {
296 if (this.lo32 === 0) {
301 remaining = this.lo32;
304 remaining = this.hi32;
311 return (remaining < 65536) ? sd - 1 : sd;
314 this.toNumber = function() {
315 var lo32 = this.lo32;
320 return this.hi32.next32(lo32);
324 function __countLeadingZeroes16(number) {
337 if (number < 16384) {
342 return (number < 32768) ? nlz + 1 : nlz;
345 // q = u / v; r = u - q * v;
347 function __div64(q, r, u, v) {
348 var m = u.countSignificantDigits();
349 var n = v.countSignificantDigits();
354 // v has single digit
355 var vd = v.getDigit(0, 0);
357 for (var i = m - 1; i >= 0; --i) {
358 var ui = (carry << 16) | u.getDigit(i, 0);
362 var qi = (ui / vd) | 0;
363 q.setDigit(i, 0, qi);
364 carry = ui - qi * vd;
380 var nrm = __countLeadingZeroes16(v.getDigit(n - 1, 0));
382 var vd1 = v.getDigit(n - 1, nrm);
383 var vd0 = v.getDigit(n - 2, nrm);
384 for (var j = m - n; j >= 0; --j) {
385 // Calculate qj estimate
386 var ud21 = r.getTwoDigits(j + n, nrm);
387 var ud2 = ud21 >>> 16;
392 var qest = (ud2 === vd1) ? 0xFFFF : ((ud21 / vd1) | 0);
393 var rest = ud21 - qest * vd1;
395 // 0 <= (qest - qj) <= 2
397 // Refine qj estimate
398 var ud0 = r.getDigit(j + n - 2, nrm);
399 while ((qest * vd0) > ((rest * 0x10000) + ud0)) {
404 // 0 <= (qest - qj) <= 1
406 // Multiply and subtract
408 for (var i = 0; i < n; ++i) {
409 var vi = qest * v.getDigit(i, nrm);
410 var ui = r.getDigit(i + j, nrm) - carry - (vi & 0xffff);
411 r.setDigit(i + j, nrm, ui);
412 carry = (vi >>> 16) - (ui >> 16);
414 var uj = ud2 - carry;
422 for (var i = 0; i < n; ++i) {
423 var ui = r.getDigit(i + j, nrm) + v.getDigit(i, nrm)
425 r.setDigit(i + j, nrm, ui);
431 q.setDigit(j, 0, qest);
432 r.setDigit(j + n, nrm, uj);
436 numberPrototype.div64 = function(x) {
437 var negateResult = false;
440 if ((this.high32() & 0x80000000) != 0) {
442 negateResult = !negateResult;
447 if ((x.high32() & 0x80000000) != 0) {
449 negateResult = !negateResult;
454 if ((v === 0) && (v.high32() === 0)) {
458 if (u.high32() === 0) {
459 if (v.high32() === 0) {
460 var result = (u / v) | 0;
461 return negateResult ? result.neg64() : result;
467 var u64 = new __Int64(u.high32(), u);
468 var v64 = new __Int64(v.high32(), v);
469 var q64 = new __Int64(0, 0);
470 var r64 = new __Int64(0, 0);
472 __div64(q64, r64, u64, v64);
474 var result = q64.toNumber();
475 return negateResult ? result.neg64() : result;
478 numberPrototype.mod64 = function(x) {
479 var negateResult = false;
482 if ((this.high32() & 0x80000000) != 0) {
484 negateResult = !negateResult;
489 if ((x.high32() & 0x80000000) != 0) {
495 if ((v === 0) && (v.high32() === 0)) {
499 if (u.high32() === 0) {
500 var result = (v.high32() === 0) ? (u % v) : u;
501 return negateResult ? result.neg64() : result;
504 var u64 = new __Int64(u.high32(), u);
505 var v64 = new __Int64(v.high32(), v);
506 var q64 = new __Int64(0, 0);
507 var r64 = new __Int64(0, 0);
509 __div64(q64, r64, u64, v64);
511 var result = r64.toNumber();
512 return negateResult ? result.neg64() : result;
514 })(Number.prototype);