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 __Int64(hi32, lo32) {
216 this.hi32 = hi32 | 0;
217 this.lo32 = lo32 | 0;
219 this.get32 = function(bitIndex) {
223 var selector = bitIndex >>> 5;
241 var shift = bitIndex & 31;
246 return (v1 << (32 - shift)) | (v0 >>> shift);
249 this.get16 = function(bitIndex) {
250 return this.get32(bitIndex) & 0xffff;
253 this.set16 = function(bitIndex, value) {
255 var shift = bitIndex & 15;
256 var svalue = (value & 0xffff) << shift;
257 var smask = 0xffff << shift;
258 var selector = bitIndex >>> 4;
263 this.lo32 = (this.lo32 & ~(smask >>> 16))
267 this.lo32 = (this.lo32 & ~smask) | svalue;
270 this.lo32 = (this.lo32 & ~(smask << 16))
272 this.hi32 = (this.hi32 & ~(smask >>> 16))
276 this.hi32 = (this.hi32 & ~smask) | svalue;
279 this.hi32 = (this.hi32 & ~(smask << 16))
285 this.getDigit = function(index, shift) {
286 return this.get16((index << 4) - shift);
289 this.getTwoDigits = function(index, shift) {
290 return this.get32(((index - 1) << 4) - shift);
293 this.setDigit = function(index, shift, value) {
294 this.set16((index << 4) - shift, value);
297 this.countSignificantDigits = function() {
301 if (this.hi32 === 0) {
302 if (this.lo32 === 0) {
307 remaining = this.lo32;
310 remaining = this.hi32;
317 return (remaining < 65536) ? sd - 1 : sd;
320 this.toNumber = function() {
321 var lo32 = this.lo32;
326 return this.hi32.next32(lo32);
330 function __countLeadingZeroes16(number) {
343 if (number < 16384) {
348 return (number < 32768) ? nlz + 1 : nlz;
351 // q = u / v; r = u - q * v;
353 function __div64(q, r, u, v) {
354 var m = u.countSignificantDigits();
355 var n = v.countSignificantDigits();
360 // v has single digit
361 var vd = v.getDigit(0, 0);
363 for (var i = m - 1; i >= 0; --i) {
364 var ui = (carry << 16) | u.getDigit(i, 0);
368 var qi = (ui / vd) | 0;
369 q.setDigit(i, 0, qi);
370 carry = ui - qi * vd;
386 var nrm = __countLeadingZeroes16(v.getDigit(n - 1, 0));
388 var vd1 = v.getDigit(n - 1, nrm);
389 var vd0 = v.getDigit(n - 2, nrm);
390 for (var j = m - n; j >= 0; --j) {
391 // Calculate qj estimate
392 var ud21 = r.getTwoDigits(j + n, nrm);
393 var ud2 = ud21 >>> 16;
398 var qest = (ud2 === vd1) ? 0xFFFF : ((ud21 / vd1) | 0);
399 var rest = ud21 - qest * vd1;
401 // 0 <= (qest - qj) <= 2
403 // Refine qj estimate
404 var ud0 = r.getDigit(j + n - 2, nrm);
405 while ((qest * vd0) > ((rest * 0x10000) + ud0)) {
410 // 0 <= (qest - qj) <= 1
412 // Multiply and subtract
414 for (var i = 0; i < n; ++i) {
415 var vi = qest * v.getDigit(i, nrm);
416 var ui = r.getDigit(i + j, nrm) - carry - (vi & 0xffff);
417 r.setDigit(i + j, nrm, ui);
418 carry = (vi >>> 16) - (ui >> 16);
420 var uj = ud2 - carry;
428 for (var i = 0; i < n; ++i) {
429 var ui = r.getDigit(i + j, nrm) + v.getDigit(i, nrm)
431 r.setDigit(i + j, nrm, ui);
437 q.setDigit(j, 0, qest);
438 r.setDigit(j + n, nrm, uj);
442 numberPrototype.div64 = function(x) {
443 var negateResult = false;
446 if ((this.high32() & 0x80000000) != 0) {
448 negateResult = !negateResult;
453 if ((x.high32() & 0x80000000) != 0) {
455 negateResult = !negateResult;
460 if ((v === 0) && (v.high32() === 0)) {
464 if (u.high32() === 0) {
465 if (v.high32() === 0) {
466 var result = (u / v) | 0;
467 return negateResult ? result.neg64() : result;
473 var u64 = new __Int64(u.high32(), u);
474 var v64 = new __Int64(v.high32(), v);
475 var q64 = new __Int64(0, 0);
476 var r64 = new __Int64(0, 0);
478 __div64(q64, r64, u64, v64);
480 var result = q64.toNumber();
481 return negateResult ? result.neg64() : result;
484 numberPrototype.mod64 = function(x) {
485 var negateResult = false;
488 if ((this.high32() & 0x80000000) != 0) {
490 negateResult = !negateResult;
495 if ((x.high32() & 0x80000000) != 0) {
501 if ((v === 0) && (v.high32() === 0)) {
505 if (u.high32() === 0) {
506 var result = (v.high32() === 0) ? (u % v) : u;
507 return negateResult ? result.neg64() : result;
510 var u64 = new __Int64(u.high32(), u);
511 var v64 = new __Int64(v.high32(), v);
512 var q64 = new __Int64(0, 0);
513 var r64 = new __Int64(0, 0);
515 __div64(q64, r64, u64, v64);
517 var result = r64.toNumber();
518 return negateResult ? result.neg64() : result;
520 })(Number.prototype);