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 09:27:53 +0100
brancharithmetic
changeset 607 9e4d0019a9eb
parent 594 035fcbd7a33c
child 615 e3f671b50e93
permissions -rw-r--r--
Conversion from Long to exact string representation.
     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 Number.prototype.next32 = function(low) {
    12   if (this === 0) {
    13     return low;
    14   }
    15   var l = new Number(low);
    16   l.hi = this;
    17   return l;
    18 };
    19 
    20 Number.prototype.high32 = function() { 
    21     return this.hi ? this.hi : (Math.floor(this / 0xFFFFFFFF)) | 0;
    22 };
    23 Number.prototype.toInt32 = function() { return this | 0; };
    24 Number.prototype.toFP = function() {
    25     return this.hi ? this.hi * 0xFFFFFFFF + this : this;
    26 };
    27 Number.prototype.toLong = function() {
    28     var hi = (this > 0xFFFFFFFF) ? (Math.floor(this / 0xFFFFFFFF)) | 0 : 0;
    29     return hi.next32(this % 0xFFFFFFFF);
    30 };
    31 
    32 Number.prototype.toExactString = function() {
    33     if (this.hi) {
    34         var res = 0;
    35         var a = [ 6,9,2,7,6,9,4,9,2,4 ];
    36         var s = '';
    37         var digit;
    38         var hi = this.hi;
    39         var low = this;
    40         for (var i = 0; i < a.length; i++) {
    41             res += hi * a[i];
    42             var low_digit = low % 10;
    43             digit = (res % 10) + low_digit;
    44 
    45             low = Math.floor(low / 10);
    46             res = Math.floor(res / 10);
    47 
    48             if (digit >= 10) {
    49                 digit -= 10;
    50                 res++;
    51             }
    52             s = String(digit).concat(s);
    53         }
    54         return String(res).concat(s);
    55     }
    56     return String(this);
    57 };
    58 
    59 Number.prototype.add64 = function(x) {
    60     var low = this + x;
    61     carry = 0;
    62     if (low > 0xFFFFFFFF) {
    63         carry = 1;
    64         low -= 0xFFFFFFFF;  // should not here be also -1?
    65     }
    66     var hi = (this.high32() + x.high32() + carry) & 0xFFFFFFFF;
    67     return hi.next32(low);
    68 };
    69 
    70 Number.prototype.div64 = function(x) {
    71     var low = Math.floor(this.toFP() / x.toFP()); // TODO: not exact enough
    72     if (low > 0xFFFFFFFF) {
    73         var hi = Math.floor(low / 0xFFFFFFFF) | 0;
    74         return hi.next32(low % 0xFFFFFFFF);
    75     }
    76     return low;
    77 };
    78 
    79 Number.prototype.shl64 = function(x) {
    80     if (x > 32) {
    81         var hi = (this << (x - 32)) & 0xFFFFFFFF;
    82         return hi.next32(0);
    83     } else {
    84         var hi = (this.high32() << x) & 0xFFFFFFFF;
    85         var low_reminder = this >> x;
    86         hi |= low_reminder;
    87         var low = this << x;
    88         return hi.next32(low);
    89     }
    90 };
    91 
    92 Number.prototype.compare64 = function(x) {
    93     if (this.hi == x.hi) {
    94         return (this == x) ? 0 : ((this < x) ? -1 : 1);
    95     }
    96     return (this.hi < x.hi) ? -1 : 1;
    97 };