rt/emul/mini/src/main/resources/org/apidesign/vm4brwsr/emul/lang/java_lang_Number.js
Fix for bug 5408, error in shift operations on Long.
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 if (x == 0) return this;
182 var hi = this << (x - 32);
185 var hi = this.high32() << x;
186 var low_reminder = this >> (32 - x);
189 low += (low < 0) ? (__m32 + 1) : 0;
190 return hi.next32(low);
194 numberPrototype.shr64 = function(x) {
196 if (x == 0) return this;
198 var low = this.high32() >> (x - 32);
199 low += (low < 0) ? (__m32 + 1) : 0;
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.ushr64 = function(x) {
213 if (x == 0) return this;
215 var low = this.high32() >>> (x - 32);
216 low += (low < 0) ? (__m32 + 1) : 0;
219 var low = this >>> x;
220 var hi_reminder = this.high32() << (32 - x);
222 low += (low < 0) ? (__m32 + 1) : 0;
223 var hi = this.high32() >>> x;
224 return hi.next32(low);
228 numberPrototype.compare64 = function(x) {
229 if (this.high32() === x.high32()) {
230 return (this < x) ? -1 : ((this > x) ? 1 : 0);
232 return (this.high32() < x.high32()) ? -1 : 1;
235 numberPrototype.neg64 = function() {
236 var hi = this.high32();
238 if ((hi === 0) && (low < 0)) {
243 low += (low < 0) ? (__m32 + 1) : 0;
244 var ret = hi.next32(low);
248 function __handleDivByZero() {
249 var exception = new vm.java_lang_ArithmeticException;
250 vm.java_lang_ArithmeticException(false).constructor
251 .cons__VLjava_lang_String_2.call(exception, "/ by zero");
256 function __Int64(hi32, lo32) {
257 this.hi32 = hi32 | 0;
258 this.lo32 = lo32 | 0;
260 this.get32 = function(bitIndex) {
264 var selector = bitIndex >>> 5;
282 var shift = bitIndex & 31;
287 return (v1 << (32 - shift)) | (v0 >>> shift);
290 this.get16 = function(bitIndex) {
291 return this.get32(bitIndex) & 0xffff;
294 this.set16 = function(bitIndex, value) {
296 var shift = bitIndex & 15;
297 var svalue = (value & 0xffff) << shift;
298 var smask = 0xffff << shift;
299 var selector = bitIndex >>> 4;
304 this.lo32 = (this.lo32 & ~(smask >>> 16))
308 this.lo32 = (this.lo32 & ~smask) | svalue;
311 this.lo32 = (this.lo32 & ~(smask << 16))
313 this.hi32 = (this.hi32 & ~(smask >>> 16))
317 this.hi32 = (this.hi32 & ~smask) | svalue;
320 this.hi32 = (this.hi32 & ~(smask << 16))
326 this.getDigit = function(index, shift) {
327 return this.get16((index << 4) - shift);
330 this.getTwoDigits = function(index, shift) {
331 return this.get32(((index - 1) << 4) - shift);
334 this.setDigit = function(index, shift, value) {
335 this.set16((index << 4) - shift, value);
338 this.countSignificantDigits = function() {
342 if (this.hi32 === 0) {
343 if (this.lo32 === 0) {
348 remaining = this.lo32;
351 remaining = this.hi32;
358 return (remaining < 65536) ? sd - 1 : sd;
361 this.toNumber = function() {
362 var lo32 = this.lo32;
367 return this.hi32.next32(lo32);
371 function __countLeadingZeroes16(number) {
384 if (number < 16384) {
389 return (number < 32768) ? nlz + 1 : nlz;
392 // q = u / v; r = u - q * v;
394 function __div64(q, r, u, v) {
395 var m = u.countSignificantDigits();
396 var n = v.countSignificantDigits();
401 // v has single digit
402 var vd = v.getDigit(0, 0);
404 for (var i = m - 1; i >= 0; --i) {
405 var ui = (carry << 16) | u.getDigit(i, 0);
409 var qi = (ui / vd) | 0;
410 q.setDigit(i, 0, qi);
411 carry = ui - qi * vd;
427 var nrm = __countLeadingZeroes16(v.getDigit(n - 1, 0));
429 var vd1 = v.getDigit(n - 1, nrm);
430 var vd0 = v.getDigit(n - 2, nrm);
431 for (var j = m - n; j >= 0; --j) {
432 // Calculate qj estimate
433 var ud21 = r.getTwoDigits(j + n, nrm);
434 var ud2 = ud21 >>> 16;
439 var qest = (ud2 === vd1) ? 0xFFFF : ((ud21 / vd1) | 0);
440 var rest = ud21 - qest * vd1;
442 // 0 <= (qest - qj) <= 2
444 // Refine qj estimate
445 var ud0 = r.getDigit(j + n - 2, nrm);
446 while ((qest * vd0) > ((rest * 0x10000) + ud0)) {
451 // 0 <= (qest - qj) <= 1
453 // Multiply and subtract
455 for (var i = 0; i < n; ++i) {
456 var vi = qest * v.getDigit(i, nrm);
457 var ui = r.getDigit(i + j, nrm) - carry - (vi & 0xffff);
458 r.setDigit(i + j, nrm, ui);
459 carry = (vi >>> 16) - (ui >> 16);
461 var uj = ud2 - carry;
469 for (var i = 0; i < n; ++i) {
470 var ui = r.getDigit(i + j, nrm) + v.getDigit(i, nrm)
472 r.setDigit(i + j, nrm, ui);
478 q.setDigit(j, 0, qest);
479 r.setDigit(j + n, nrm, uj);
483 numberPrototype.div32 = function(x) {
488 return (this / x) | 0;
491 numberPrototype.mod32 = function(x) {
499 numberPrototype.div64 = function(x) {
500 var negateResult = false;
503 if ((this.high32() & 0x80000000) != 0) {
505 negateResult = !negateResult;
510 if ((x.high32() & 0x80000000) != 0) {
512 negateResult = !negateResult;
517 if ((v == 0) && (v.high32() === 0)) {
521 if (u.high32() === 0) {
522 if (v.high32() === 0) {
523 var result = (u / v) | 0;
524 return negateResult ? result.neg64() : result;
530 var u64 = new __Int64(u.high32(), u);
531 var v64 = new __Int64(v.high32(), v);
532 var q64 = new __Int64(0, 0);
533 var r64 = new __Int64(0, 0);
535 __div64(q64, r64, u64, v64);
537 var result = q64.toNumber();
538 return negateResult ? result.neg64() : result;
541 numberPrototype.mod64 = function(x) {
542 var negateResult = false;
545 if ((this.high32() & 0x80000000) != 0) {
547 negateResult = !negateResult;
552 if ((x.high32() & 0x80000000) != 0) {
558 if ((v == 0) && (v.high32() === 0)) {
562 if (u.high32() === 0) {
563 var result = (v.high32() === 0) ? (u % v) : u;
564 return negateResult ? result.neg64() : result;
567 var u64 = new __Int64(u.high32(), u);
568 var v64 = new __Int64(v.high32(), v);
569 var q64 = new __Int64(0, 0);
570 var r64 = new __Int64(0, 0);
572 __div64(q64, r64, u64, v64);
574 var result = r64.toNumber();
575 return negateResult ? result.neg64() : result;
577 })(Number.prototype);
579 vm.java_lang_Number(false);