emul/mini/src/main/resources/org/apidesign/vm4brwsr/emul/lang/java_lang_Number.js
author Martin Soch <Martin.Soch@oracle.com>
Thu, 31 Jan 2013 15:26:09 +0100
brancharithmetic
changeset 620 189f695d0b02
parent 619 5134b1d78432
child 627 4c2b92281cdc
permissions -rw-r--r--
Added subtraction for Long + tests
     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     if (this.hi && x.hi) {
    95         var hi = this.hi & x.hi;
    96         return hi.next32(low);
    97     };
    98     return low;
    99 };
   100 
   101 Number.prototype.shl64 = function(x) {
   102     if (x >= 32) {
   103         var hi = (this << (x - 32)) | 0;
   104         return hi.next32(0);
   105     } else {
   106         var hi = (this.high32() << x) | 0;
   107         var low_reminder = this >> (32 - x);
   108         hi |= low_reminder;
   109         var low = this << x;
   110         low += (low < 0) ? (__m32+1) : 0;
   111         return hi.next32(low);
   112     }
   113 };
   114 
   115 Number.prototype.shr64 = function(x) {
   116     if (x >= 32) {
   117         var low = (this.high32() >> (x - 32)) | 0;
   118         low += (low < 0) ? (__m32+1) : 0;
   119         return low;
   120     } else {
   121         var low = (this >> x) | 0;
   122         var hi_reminder = this.high32() << (32 - x);
   123         low |= hi_reminder;
   124         low += (low < 0) ? (__m32+1) : 0;
   125         var hi = this.high32() >> x;
   126         return hi.next32(low);
   127     }
   128 };
   129 
   130 Number.prototype.compare64 = function(x) {
   131     if (this.hi == x.hi) {
   132         return (this == x) ? 0 : ((this < x) ? -1 : 1);
   133     }
   134     return (this.hi < x.hi) ? -1 : 1;
   135 };