emul/mini/src/main/resources/org/apidesign/vm4brwsr/emul/lang/java_lang_Number.js
author Martin Soch <Martin.Soch@oracle.com>
Fri, 01 Feb 2013 09:00:30 +0100
brancharithmetic
changeset 630 04e312a7887e
parent 629 2d537f8cd604
child 657 b42bfe334128
permissions -rw-r--r--
Support for Long unary "-"
     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 | 0;
    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(Math.floor(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.sub64 = function(x) {
    73     var low = this - x;
    74     carry = 0;
    75     if (low < 0) {
    76         carry = 1;
    77         low += (__m32+1);
    78     }
    79     var hi = (this.high32() - x.high32() - carry) | 0;
    80     return hi.next32(low);
    81 };
    82 
    83 Number.prototype.div64 = function(x) {
    84     var low = Math.floor(this.toFP() / x.toFP()); // TODO: not exact enough
    85     if (low > __m32) {
    86         var hi = Math.floor(low / (__m32+1)) | 0;
    87         return hi.next32(low % (__m32+1));
    88     }
    89     return low;
    90 };
    91 
    92 Number.prototype.and64 = function(x) {
    93     var low = this & x;
    94     low += (low < 0) ? (__m32+1) : 0;
    95     if (this.hi && x.hi) {
    96         var hi = this.hi & x.hi;
    97         return hi.next32(low);
    98     };
    99     return low;
   100 };
   101 
   102 Number.prototype.or64 = function(x) {
   103     var low = this | x;
   104     low += (low < 0) ? (__m32+1) : 0;
   105     if (this.hi || x.hi) {
   106         var hi = this.hi | x.hi;
   107         return hi.next32(low);
   108     };
   109     return low;
   110 };
   111 
   112 Number.prototype.xor64 = function(x) {
   113     var low = this ^ x;
   114     low += (low < 0) ? (__m32+1) : 0;
   115     if (this.hi || x.hi) {
   116         var hi = this.hi ^ x.hi;
   117         return hi.next32(low);
   118     };
   119     return low;
   120 };
   121 
   122 Number.prototype.shl64 = function(x) {
   123     if (x >= 32) {
   124         var hi = this << (x - 32);
   125         return hi.next32(0);
   126     } else {
   127         var hi = this.high32() << x;
   128         var low_reminder = this >> (32 - x);
   129         hi |= low_reminder;
   130         var low = this << x;
   131         low += (low < 0) ? (__m32+1) : 0;
   132         return hi.next32(low);
   133     }
   134 };
   135 
   136 Number.prototype.shr64 = function(x) {
   137     if (x >= 32) {
   138         var low = this.high32() >> (x - 32);
   139         low += (low < 0) ? (__m32+1) : 0;
   140         return low;
   141     } else {
   142         var low = this >> x;
   143         var hi_reminder = this.high32() << (32 - x);
   144         low |= hi_reminder;
   145         low += (low < 0) ? (__m32+1) : 0;
   146         var hi = this.high32() >> x;
   147         return hi.next32(low);
   148     }
   149 };
   150 
   151 Number.prototype.ushr64 = function(x) {
   152     if (x >= 32) {
   153         var low = this.high32() >>> (x - 32);
   154         low += (low < 0) ? (__m32+1) : 0;
   155         return low;
   156     } else {
   157         var low = this >>> x;
   158         var hi_reminder = this.high32() << (32 - x);
   159         low |= hi_reminder;
   160         low += (low < 0) ? (__m32+1) : 0;
   161         var hi = this.high32() >>> x;
   162         return hi.next32(low);
   163     }
   164 };
   165 
   166 Number.prototype.compare64 = function(x) {
   167     if (this.hi == x.hi) {
   168         return (this == x) ? 0 : ((this < x) ? -1 : 1);
   169     }
   170     return (this.hi < x.hi) ? -1 : 1;
   171 };
   172 
   173 Number.prototype.neg64 = function() {
   174     var hi = this.high32();
   175     var low = this;
   176     if ((hi === 0) && (low < 0)) { return -low; }
   177     hi = ~hi;
   178     low = ~low;
   179     low += (low < 0) ? (__m32+1) : 0;
   180     return hi.next32(low);
   181 };