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