emul/mini/src/main/resources/org/apidesign/vm4brwsr/emul/lang/java_lang_Number.js
author Martin Soch <Martin.Soch@oracle.com>
Wed, 30 Jan 2013 22:23:21 +0100
brancharithmetic
changeset 615 e3f671b50e93
parent 607 9e4d0019a9eb
child 616 9cbf1f2ad7ee
permissions -rw-r--r--
Long tests moved (temporarily) among VM tests
Long conversion test already passing
Long version of & and >> operators added
     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;
     6 };
     7 
     8 Number.prototype.toInt8 = function()  { return (this << 24) >> 24; };
     9 Number.prototype.toInt16 = function() { return (this << 16) >> 16; };
    10 
    11 var __m32 = 0xFFFFFFFF;
    12 
    13 Number.prototype.next32 = function(low) {
    14   if (this === 0) {
    15     return low;
    16   }
    17   var l = new Number(low);
    18   l.hi = this;
    19   return l;
    20 };
    21 
    22 Number.prototype.high32 = function() { 
    23     return this.hi ? this.hi : (Math.floor(this / (__m32+1))) | 0;
    24 };
    25 Number.prototype.toInt32 = function() { return this | 0; };
    26 Number.prototype.toFP = function() {
    27     return this.hi ? this.hi * (__m32+1) + this : this;
    28 };
    29 Number.prototype.toLong = function() {
    30     var hi = (this > __m32) ? (Math.floor(this / (__m32+1))) | 0 : 0;
    31     return hi.next32(this % (__m32+1));
    32 };
    33 
    34 Number.prototype.toExactString = function() {
    35     if (this.hi) {
    36         var res = 0;
    37         var a = [ 6,9,2,7,6,9,4,9,2,4 ];
    38         var s = '';
    39         var digit;
    40         var hi = this.hi;
    41         var low = this;
    42         for (var i = 0; i < a.length; i++) {
    43             res += hi * a[i];
    44             var low_digit = low % 10;
    45             digit = (res % 10) + low_digit;
    46 
    47             low = Math.floor(low / 10);
    48             res = Math.floor(res / 10);
    49 
    50             if (digit >= 10) {
    51                 digit -= 10;
    52                 res++;
    53             }
    54             s = String(digit).concat(s);
    55         }
    56         return String(res).concat(s);
    57     }
    58     return String(this);
    59 };
    60 
    61 Number.prototype.add64 = function(x) {
    62     var low = this + x;
    63     carry = 0;
    64     if (low > __m32) {
    65         carry = 1;
    66         low -= (__m32+1);
    67     }
    68     var hi = (this.high32() + x.high32() + carry) | 0;
    69     return hi.next32(low);
    70 };
    71 
    72 Number.prototype.div64 = function(x) {
    73     var low = Math.floor(this.toFP() / x.toFP()); // TODO: not exact enough
    74     if (low > __m32) {
    75         var hi = Math.floor(low / (__m32+1)) | 0;
    76         return hi.next32(low % (__m32+1));
    77     }
    78     return low;
    79 };
    80 
    81 Number.prototype.and64 = function(x) {
    82     var low = this & x;
    83     if (this.hi && x.hi) {
    84         var hi = this.hi & x.hi;
    85         return hi.next32(low);
    86     };
    87     return low;
    88 };
    89 
    90 Number.prototype.shl64 = function(x) {
    91     if (x > 32) {
    92         var hi = (this << (x - 32)) & 0xFFFFFFFF;
    93         return hi.next32(0);
    94     } else {
    95         var hi = (this.high32() << x) & 0xFFFFFFFF;
    96         var low_reminder = this >> (32 - x);
    97         hi |= low_reminder;
    98         var low = this << x;
    99         return hi.next32(low);
   100     }
   101 };
   102 
   103 Number.prototype.shr64 = function(x) {
   104     if (x > 32) {
   105         var low = (this.high32() >> (x - 32)) & 0xFFFFFFFF;
   106         return low;
   107     } else {
   108         var low = (this >> x) & 0xFFFFFFFF;
   109         var hi_reminder = (this.high32() << (32 - x)) >> (32 - x);
   110         low |= hi_reminder;
   111         var hi = this.high32() >> x;
   112         return hi.next32(low);
   113     }
   114 };
   115 
   116 Number.prototype.compare64 = function(x) {
   117     if (this.hi == x.hi) {
   118         return (this == x) ? 0 : ((this < x) ? -1 : 1);
   119     }
   120     return (this.hi < x.hi) ? -1 : 1;
   121 };