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() {
36 // check for Long.MIN_VALUE
37 if ((this.hi == (0x80000000 | 0)) && (this == 0)) {
38 return '-9223372036854775808';
41 var a = [ 6,9,2,7,6,9,4,9,2,4 ];
44 var neg = this.hi < 0;
53 for (var i = 0; i < a.length; i++) {
55 var low_digit = low % 10;
56 digit = (res % 10) + low_digit;
58 low = Math.floor(low / 10);
59 res = Math.floor(res / 10);
65 s = String(digit).concat(s);
67 s = String(res).concat(s).replace(/^0+/, '');
68 return (neg ? '-' : '').concat(s);
73 Number.prototype.add64 = function(x) {
80 var hi = (this.high32() + x.high32() + carry) | 0;
81 return hi.next32(low);
84 Number.prototype.sub64 = function(x) {
91 var hi = (this.high32() - x.high32() - carry) | 0;
92 return hi.next32(low);
95 Number.prototype.mul64 = function(x) {
96 var low = this.mul32(x);
97 low += (low < 0) ? (__m32+1) : 0;
98 // first count upper 32 bits of (this.low * x.low)
102 for (var i = 0; i < 32; i++) {
104 hi_hi += this >>> 16;
105 hi_low += this & 0xFFFF
108 hi_low += (hi_hi & 1) ? 0x8000 : 0;
112 var hi = (hi_hi << 16) + hi_low;
114 var m1 = this.high32().mul32(x);
115 var m2 = this.mul32(x.high32());
116 hi = hi.add32(m1).add32(m2);
118 return hi.next32(low);
121 Number.prototype.and64 = function(x) {
123 low += (low < 0) ? (__m32+1) : 0;
124 if (this.hi && x.hi) {
125 var hi = this.hi & x.hi;
126 return hi.next32(low);
131 Number.prototype.or64 = function(x) {
133 low += (low < 0) ? (__m32+1) : 0;
134 if (this.hi || x.hi) {
135 var hi = this.hi | x.hi;
136 return hi.next32(low);
141 Number.prototype.xor64 = function(x) {
143 low += (low < 0) ? (__m32+1) : 0;
144 if (this.hi || x.hi) {
145 var hi = this.hi ^ x.hi;
146 return hi.next32(low);
151 Number.prototype.shl64 = function(x) {
153 var hi = this << (x - 32);
156 var hi = this.high32() << x;
157 var low_reminder = this >> (32 - x);
160 low += (low < 0) ? (__m32+1) : 0;
161 return hi.next32(low);
165 Number.prototype.shr64 = function(x) {
167 var low = this.high32() >> (x - 32);
168 low += (low < 0) ? (__m32+1) : 0;
172 var hi_reminder = this.high32() << (32 - x);
174 low += (low < 0) ? (__m32+1) : 0;
175 var hi = this.high32() >> x;
176 return hi.next32(low);
180 Number.prototype.ushr64 = function(x) {
182 var low = this.high32() >>> (x - 32);
183 low += (low < 0) ? (__m32+1) : 0;
186 var low = this >>> x;
187 var hi_reminder = this.high32() << (32 - x);
189 low += (low < 0) ? (__m32+1) : 0;
190 var hi = this.high32() >>> x;
191 return hi.next32(low);
195 Number.prototype.compare64 = function(x) {
196 if (this.high32() === x.high32()) {
197 return (this < x) ? -1 : ((this > x) ? 1 : 0);
199 return (this.high32() < x.high32()) ? -1 : 1;
202 Number.prototype.neg64 = function() {
203 var hi = this.high32();
205 if ((hi === 0) && (low < 0)) { return -low; }
208 low += (low < 0) ? (__m32+1) : 0;
209 var ret = hi.next32(low);
213 (function(numberPrototype) {
214 function __Int64(hi32, lo32) {
215 this.hi32 = hi32 | 0;
216 this.lo32 = lo32 | 0;
218 this.get32 = function(bitIndex) {
222 var selector = bitIndex >>> 5;
240 var shift = bitIndex & 31;
245 return (v1 << (32 - shift)) | (v0 >>> shift);
248 this.get16 = function(bitIndex) {
249 return this.get32(bitIndex) & 0xffff;
252 this.set16 = function(bitIndex, value) {
254 var shift = bitIndex & 15;
255 var svalue = (value & 0xffff) << shift;
256 var smask = 0xffff << shift;
257 var selector = bitIndex >>> 4;
262 this.lo32 = (this.lo32 & ~(smask >>> 16))
266 this.lo32 = (this.lo32 & ~smask) | svalue;
269 this.lo32 = (this.lo32 & ~(smask << 16))
271 this.hi32 = (this.hi32 & ~(smask >>> 16))
275 this.hi32 = (this.hi32 & ~smask) | svalue;
278 this.hi32 = (this.hi32 & ~(smask << 16))
284 this.getDigit = function(index, shift) {
285 return this.get16((index << 4) - shift);
288 this.getTwoDigits = function(index, shift) {
289 return this.get32(((index - 1) << 4) - shift);
292 this.setDigit = function(index, shift, value) {
293 this.set16((index << 4) - shift, value);
296 this.countSignificantDigits = function() {
300 if (this.hi32 === 0) {
301 if (this.lo32 === 0) {
306 remaining = this.lo32;
309 remaining = this.hi32;
316 return (remaining < 65536) ? sd - 1 : sd;
319 this.toNumber = function() {
320 var lo32 = this.lo32;
325 return this.hi32.next32(lo32);
329 function __countLeadingZeroes16(number) {
342 if (number < 16384) {
347 return (number < 32768) ? nlz + 1 : nlz;
350 // q = u / v; r = u - q * v;
352 function __div64(q, r, u, v) {
353 var m = u.countSignificantDigits();
354 var n = v.countSignificantDigits();
359 // v has single digit
360 var vd = v.getDigit(0, 0);
362 for (var i = m - 1; i >= 0; --i) {
363 var ui = (carry << 16) | u.getDigit(i, 0);
367 var qi = (ui / vd) | 0;
368 q.setDigit(i, 0, qi);
369 carry = ui - qi * vd;
385 var nrm = __countLeadingZeroes16(v.getDigit(n - 1, 0));
387 var vd1 = v.getDigit(n - 1, nrm);
388 var vd0 = v.getDigit(n - 2, nrm);
389 for (var j = m - n; j >= 0; --j) {
390 // Calculate qj estimate
391 var ud21 = r.getTwoDigits(j + n, nrm);
392 var ud2 = ud21 >>> 16;
397 var qest = (ud2 === vd1) ? 0xFFFF : ((ud21 / vd1) | 0);
398 var rest = ud21 - qest * vd1;
400 // 0 <= (qest - qj) <= 2
402 // Refine qj estimate
403 var ud0 = r.getDigit(j + n - 2, nrm);
404 while ((qest * vd0) > ((rest * 0x10000) + ud0)) {
409 // 0 <= (qest - qj) <= 1
411 // Multiply and subtract
413 for (var i = 0; i < n; ++i) {
414 var vi = qest * v.getDigit(i, nrm);
415 var ui = r.getDigit(i + j, nrm) - carry - (vi & 0xffff);
416 r.setDigit(i + j, nrm, ui);
417 carry = (vi >>> 16) - (ui >> 16);
419 var uj = ud2 - carry;
427 for (var i = 0; i < n; ++i) {
428 var ui = r.getDigit(i + j, nrm) + v.getDigit(i, nrm)
430 r.setDigit(i + j, nrm, ui);
436 q.setDigit(j, 0, qest);
437 r.setDigit(j + n, nrm, uj);
441 numberPrototype.div64 = function(x) {
442 var negateResult = false;
445 if ((this.high32() & 0x80000000) != 0) {
447 negateResult = !negateResult;
452 if ((x.high32() & 0x80000000) != 0) {
454 negateResult = !negateResult;
459 if ((v === 0) && (v.high32() === 0)) {
463 if (u.high32() === 0) {
464 if (v.high32() === 0) {
465 var result = (u / v) | 0;
466 return negateResult ? result.neg64() : result;
472 var u64 = new __Int64(u.high32(), u);
473 var v64 = new __Int64(v.high32(), v);
474 var q64 = new __Int64(0, 0);
475 var r64 = new __Int64(0, 0);
477 __div64(q64, r64, u64, v64);
479 var result = q64.toNumber();
480 return negateResult ? result.neg64() : result;
483 numberPrototype.mod64 = function(x) {
484 var negateResult = false;
487 if ((this.high32() & 0x80000000) != 0) {
489 negateResult = !negateResult;
494 if ((x.high32() & 0x80000000) != 0) {
500 if ((v === 0) && (v.high32() === 0)) {
504 if (u.high32() === 0) {
505 var result = (v.high32() === 0) ? (u % v) : u;
506 return negateResult ? result.neg64() : result;
509 var u64 = new __Int64(u.high32(), u);
510 var v64 = new __Int64(v.high32(), v);
511 var q64 = new __Int64(0, 0);
512 var r64 = new __Int64(0, 0);
514 __div64(q64, r64, u64, v64);
516 var result = r64.toNumber();
517 return negateResult ? result.neg64() : result;
519 })(Number.prototype);