emul/mini/src/main/resources/org/apidesign/vm4brwsr/emul/lang/java_lang_Number.js
brancharithmetic
changeset 594 035fcbd7a33c
parent 593 b42911b78a16
child 607 9e4d0019a9eb
     1.1 --- a/emul/mini/src/main/resources/org/apidesign/vm4brwsr/emul/lang/java_lang_Number.js	Sun Jan 27 10:19:02 2013 +0100
     1.2 +++ b/emul/mini/src/main/resources/org/apidesign/vm4brwsr/emul/lang/java_lang_Number.js	Mon Jan 28 00:15:38 2013 +0100
     1.3 @@ -8,64 +8,63 @@
     1.4  Number.prototype.toInt8 = function()  { return (this << 24) >> 24; };
     1.5  Number.prototype.toInt16 = function() { return (this << 16) >> 16; };
     1.6  
     1.7 -var Long = function(low, hi) {
     1.8 -    this.low = low;
     1.9 -    this.hi = hi;
    1.10 +Number.prototype.next32 = function(low) {
    1.11 +  if (this === 0) {
    1.12 +    return low;
    1.13 +  }
    1.14 +  var l = new Number(low);
    1.15 +  l.hi = this;
    1.16 +  return l;
    1.17  };
    1.18  
    1.19 -function LongFromNumber(x) {
    1.20 -    return new Long(x % 0xFFFFFFFF, Math.floor(x / 0xFFFFFFFF));
    1.21 +Number.prototype.high32 = function() { 
    1.22 +    return this.hi ? this.hi : (Math.floor(this / 0xFFFFFFFF)) | 0;
    1.23 +};
    1.24 +Number.prototype.toInt32 = function() { return this | 0; };
    1.25 +Number.prototype.toFP = function() {
    1.26 +    return this.hi ? this.hi * 0xFFFFFFFF + this : this;
    1.27 +};
    1.28 +Number.prototype.toLong = function() {
    1.29 +    var hi = (this > 0xFFFFFFFF) ? (Math.floor(this / 0xFFFFFFFF)) | 0 : 0;
    1.30 +    return hi.next32(this % 0xFFFFFFFF);
    1.31 +}
    1.32 +
    1.33 +Number.prototype.add64 = function(x) {
    1.34 +    var low = this + x;
    1.35 +    carry = 0;
    1.36 +    if (low > 0xFFFFFFFF) {
    1.37 +        carry = 1;
    1.38 +        low -= 0xFFFFFFFF;  // should not here be also -1?
    1.39 +    }
    1.40 +    var hi = (this.high32() + x.high32() + carry) & 0xFFFFFFFF;
    1.41 +    return hi.next32(low);
    1.42  };
    1.43  
    1.44 -function MakeLong(x) {
    1.45 -    if ((x.hi == undefined) && (x.low == undefined)) {
    1.46 -        return LongFromNumber(x);
    1.47 +Number.prototype.div64 = function(x) {
    1.48 +    var low = Math.floor(this.toFP() / x.toFP()); // TODO: not exact enough
    1.49 +    if (low > 0xFFFFFFFF) {
    1.50 +        var hi = Math.floor(low / 0xFFFFFFFF) | 0;
    1.51 +        return hi.next32(low % 0xFFFFFFFF);
    1.52      }
    1.53 -    return x;
    1.54 +    return low;
    1.55  };
    1.56  
    1.57 -Long.prototype.toInt32 = function() { return this.low | 0; };
    1.58 -Long.prototype.toFP = function() { return this.hi * 0xFFFFFFFF + this.low; };
    1.59 -
    1.60 -Long.prototype.toString = function() {
    1.61 -    return String(this.toFP());
    1.62 +Number.prototype.shl64 = function(x) {
    1.63 +    if (x > 32) {
    1.64 +        var hi = (this << (x - 32)) & 0xFFFFFFFF;
    1.65 +        return hi.next32(0);
    1.66 +    } else {
    1.67 +        var hi = (this.high32() << x) & 0xFFFFFFFF;
    1.68 +        var low_reminder = this >> x;
    1.69 +        hi |= low_reminder;
    1.70 +        var low = this << x;
    1.71 +        return hi.next32(low);
    1.72 +    }
    1.73  };
    1.74  
    1.75 -Long.prototype.valueOf = function() {
    1.76 -    return this.toFP();
    1.77 -};
    1.78 -
    1.79 -Long.prototype.compare64 = function(x) {
    1.80 +Number.prototype.compare64 = function(x) {
    1.81      if (this.hi == x.hi) {
    1.82 -        return (this.low == x.low) ? 0 : ((this.low < x.low) ? -1 : 1);
    1.83 +        return (this == x) ? 0 : ((this < x) ? -1 : 1);
    1.84      }
    1.85      return (this.hi < x.hi) ? -1 : 1;
    1.86  };
    1.87 -
    1.88 -Long.prototype.add64 = function(x) {
    1.89 -    low = this.low + x.low;
    1.90 -    carry = 0;
    1.91 -    if (low > 0xFFFFFFFF) {
    1.92 -        carry = 1;
    1.93 -        low -= 0xFFFFFFFF;
    1.94 -    }
    1.95 -    hi = (this.hi + x.hi + carry) & 0xFFFFFFFF;
    1.96 -    return new Long(low, hi);
    1.97 -};
    1.98 -
    1.99 -Long.prototype.div64 = function(x) {
   1.100 -    return LongFromNumber(Math.floor(this.toFP() / x.toFP()));
   1.101 -};
   1.102 -
   1.103 -Long.prototype.shl64 = function(x) {
   1.104 -    if (x > 32) {
   1.105 -        hi = (this.low << (x - 32)) & 0xFFFFFFFF;
   1.106 -        low = 0;
   1.107 -    } else {
   1.108 -        hi = (this.hi << x) & 0xFFFFFFFF;
   1.109 -        low_reminder = this.low >> x;
   1.110 -        hi |= low_reminder;
   1.111 -        low = this.low << x;
   1.112 -    }
   1.113 -    return new Long(low, hi);
   1.114 -};