Changeset 11722


Ignore:
Timestamp:
04/01/09 19:58:11 (14 years ago)
Author:
ehuelsmann
Message:

Make sure to create Fixnums when the LispInteger/Bignum? created is in the Fixnum range.

Note: After this commit, your .abcl files need to be recompiled as it makes the

Bignum and Fixnum constructors 'private'.

Location:
trunk/abcl/src/org/armedbear/lisp
Files:
9 edited

Legend:

Unmodified
Added
Removed
  • trunk/abcl/src/org/armedbear/lisp/Bignum.java

    r11714 r11722  
    4040  public final BigInteger value;
    4141
    42   public static Bignum getInstance(long l) {
    43     return new Bignum(l);
    44   }
    45 
    46   public Bignum(long l)
     42  private static BigInteger MOST_NEGATIVE_FIXNUM =
     43          BigInteger.valueOf(Integer.MIN_VALUE);
     44  private static BigInteger MOST_POSITIVE_FIXNUM =
     45          BigInteger.valueOf(Integer.MAX_VALUE);
     46
     47  public static LispInteger getInstance(long l) {
     48      if (Integer.MIN_VALUE <= l && l <= Integer.MAX_VALUE)
     49          return Fixnum.getInstance(l);
     50      else
     51          return new Bignum(l);
     52  }
     53
     54  public static LispInteger getInstance(BigInteger n) {
     55      if (MOST_NEGATIVE_FIXNUM.compareTo(n) < 0 ||
     56              MOST_POSITIVE_FIXNUM.compareTo(n) > 0)
     57          return new Bignum(n);
     58      else
     59          return Fixnum.getInstance(n.intValue());
     60  }
     61
     62  public static LispInteger getInstance(String s, int radix) {
     63      BigInteger value = new BigInteger(s, radix);
     64
     65      return Bignum.getInstance(value);
     66  }
     67
     68  private Bignum(long l)
    4769  {
    4870    value = BigInteger.valueOf(l);
    4971  }
    5072
    51   public Bignum(BigInteger n)
     73  private Bignum(BigInteger n)
    5274  {
    5375    value = n;
    54   }
    55 
    56   public Bignum(String s, int radix)
    57   {
    58     value = new BigInteger(s, radix);
    5976  }
    6077
  • trunk/abcl/src/org/armedbear/lisp/Fixnum.java

    r11714 r11722  
    253253    if (value >= 0)
    254254      return this;
    255     if (value > Integer.MIN_VALUE)
    256       return Fixnum.getInstance(-value);
    257     return new Bignum(-((long)Integer.MIN_VALUE));
     255    return LispInteger.getInstance(-(long)value);
    258256  }
    259257
     
    372370  public final LispObject incr()
    373371  {
    374     if (value < Integer.MAX_VALUE)
    375       return Fixnum.getInstance(value + 1);
    376     return new Bignum((long) value + 1);
     372    return LispInteger.getInstance(1 + (long)value);
    377373  }
    378374
     
    380376  public final LispObject decr()
    381377  {
    382     if (value > Integer.MIN_VALUE)
    383       return Fixnum.getInstance(value - 1);
    384     return new Bignum((long) value - 1);
     378    return LispInteger.getInstance(-1 + (long)value);
    385379  }
    386380
     
    388382  public LispObject negate()
    389383  {
    390     long result = 0L - value;
    391     if (result >= Integer.MIN_VALUE && result <= Integer.MAX_VALUE)
    392       return Fixnum.getInstance((int)result);
    393     else
    394       return new Bignum(result);
     384    return LispInteger.getInstance((-(long)value));
    395385  }
    396386
     
    398388  public LispObject add(int n)
    399389  {
    400     long result = (long) value + n;
    401     if (result >= Integer.MIN_VALUE && result <= Integer.MAX_VALUE)
    402       return Fixnum.getInstance((int)result);
    403     else
    404       return new Bignum(result);
     390    return LispInteger.getInstance((long) value + n);
    405391  }
    406392
     
    411397      {
    412398        long result = (long) value + ((Fixnum)obj).value;
    413         if (result >= Integer.MIN_VALUE && result <= Integer.MAX_VALUE)
    414           return Fixnum.getInstance((int)result);
    415         else
    416           return new Bignum(result);
     399        return LispInteger.getInstance(result);
    417400      }
    418401    if (obj instanceof Bignum)
     
    440423  public LispObject subtract(int n)
    441424  {
    442     long result = (long) value - n;
    443     if (result >= Integer.MIN_VALUE && result <= Integer.MAX_VALUE)
    444       return Fixnum.getInstance((int)result);
    445     else
    446       return new Bignum(result);
     425    return LispInteger.getInstance((long)value - n);
    447426  }
    448427
     
    479458  {
    480459    long result = (long) value * n;
    481     if (result >= Integer.MIN_VALUE && result <= Integer.MAX_VALUE)
    482       return Fixnum.getInstance((int)result);
    483     else
    484       return new Bignum(result);
     460    return LispInteger.getInstance(result);
    485461  }
    486462
     
    491467      {
    492468        long result = (long) value * ((Fixnum)obj).value;
    493         if (result >= Integer.MIN_VALUE && result <= Integer.MAX_VALUE)
    494           return Fixnum.getInstance((int)result);
    495         else
    496           return new Bignum(result);
     469        return LispInteger.getInstance(result);
    497470      }
    498471    if (obj instanceof Bignum)
     
    839812      {
    840813        n = n << shift;
    841         if (n >= Integer.MIN_VALUE && n <= Integer.MAX_VALUE)
    842           return Fixnum.getInstance((int)n);
    843         else
    844           return new Bignum(n);
     814        return LispInteger.getInstance(n);
    845815      }
    846816    // BigInteger.shiftLeft() succumbs to a stack overflow if shift
     
    962932
    963933    if (y.compareTo (BigInteger.ZERO) < 0)
    964       return (Fixnum.getInstance(1)).divideBy(this.pow(new Bignum(y.negate())));
     934      return (Fixnum.getInstance(1)).divideBy(this.pow(Bignum.getInstance(y.negate())));
    965935
    966936    if (y.compareTo(BigInteger.ZERO) == 0)
     
    988958    }
    989959
    990     return new Bignum(xy);
     960    return Bignum.getInstance(xy);
    991961  }
    992962
  • trunk/abcl/src/org/armedbear/lisp/FloatFunctions.java

    r11714 r11722  
    155155                int bits = Float.floatToIntBits(((SingleFloat)arg).value);
    156156                BigInteger big = BigInteger.valueOf(bits >> 1);
    157                 return new Bignum(big.shiftLeft(1).add(((bits & 1) == 1) ? BigInteger.ONE : BigInteger.ZERO));
     157                return Bignum.getInstance(big.shiftLeft(1).add(((bits & 1) == 1) ? BigInteger.ONE : BigInteger.ZERO));
    158158            }
    159159            if (arg instanceof DoubleFloat) {
    160160                long bits = Double.doubleToLongBits(((DoubleFloat)arg).value);
    161161                BigInteger big = BigInteger.valueOf(bits >> 1);
    162                 return new Bignum(big.shiftLeft(1).add(((bits & 1) == 1) ? BigInteger.ONE : BigInteger.ZERO));
     162                return Bignum.getInstance(big.shiftLeft(1).add(((bits & 1) == 1) ? BigInteger.ONE : BigInteger.ZERO));
    163163            }
    164164            return type_error(arg, Symbol.FLOAT);
  • trunk/abcl/src/org/armedbear/lisp/JavaObject.java

    r11691 r11722  
    135135
    136136            if (obj instanceof BigInteger)
    137                 return new Bignum((BigInteger)obj);
     137                return Bignum.getInstance((BigInteger)obj);
    138138
    139139            if (obj instanceof Short)
  • trunk/abcl/src/org/armedbear/lisp/Lisp.java

    r11715 r11722  
    882882      return Fixnum.getInstance((int)n);
    883883    else
    884       return new Bignum(n);
     884      return Bignum.getInstance(n);
    885885  }
    886886
     
    916916      return Fixnum.getInstance(n.intValue());
    917917    else
    918       return new Bignum(n);
     918      return Bignum.getInstance(n);
    919919  }
    920920
     
    12201220
    12211221  public static final LispObject UNSIGNED_BYTE_32_MAX_VALUE =
    1222     new Bignum(4294967296L);
     1222    Bignum.getInstance(4294967296L);
    12231223
    12241224  public static final LispObject getUpgradedArrayElementType(LispObject type)
     
    20892089    Symbol.MOST_POSITIVE_FIXNUM.initializeConstant(Fixnum.getInstance(Integer.MAX_VALUE));
    20902090    Symbol.MOST_NEGATIVE_FIXNUM.initializeConstant(Fixnum.getInstance(Integer.MIN_VALUE));
    2091     Symbol.MOST_POSITIVE_JAVA_LONG.initializeConstant(new Bignum(Long.MAX_VALUE));
    2092     Symbol.MOST_NEGATIVE_JAVA_LONG.initializeConstant(new Bignum(Long.MIN_VALUE));
     2091    Symbol.MOST_POSITIVE_JAVA_LONG.initializeConstant(Bignum.getInstance(Long.MAX_VALUE));
     2092    Symbol.MOST_NEGATIVE_JAVA_LONG.initializeConstant(Bignum.getInstance(Long.MIN_VALUE));
    20932093  }
    20942094
  • trunk/abcl/src/org/armedbear/lisp/LispInteger.java

    r11574 r11722  
    4444          return Fixnum.getInstance((int)l);
    4545      else
    46           return new Bignum(l);
     46          return Bignum.getInstance(l);
    4747  }
    4848
  • trunk/abcl/src/org/armedbear/lisp/RuntimeClass.java

    r11714 r11722  
    170170    }
    171171
    172     public static final Bignum makeLispObject(long i) throws ConditionThrowable
     172    public static final LispInteger makeLispObject(long i) throws ConditionThrowable
    173173    {
    174         return new Bignum(i);
     174        return Bignum.getInstance(i);
    175175    }
    176176
  • trunk/abcl/src/org/armedbear/lisp/Stream.java

    r11714 r11722  
    13571357    try
    13581358      {
    1359         return new Bignum(token, radix);
     1359        return Bignum.getInstance(token, radix);
    13601360      }
    13611361    catch (NumberFormatException e) {}
     
    14941494    try
    14951495      {
    1496         return new Bignum(s, radix);
     1496        return Bignum.getInstance(s, radix);
    14971497      }
    14981498    catch (NumberFormatException e) {}
     
    15231523    try
    15241524      {
    1525         return new Bignum(s, radix);
     1525        return Bignum.getInstance(s, radix);
    15261526      }
    15271527    catch (NumberFormatException e) {}
  • trunk/abcl/src/org/armedbear/lisp/compiler-pass2.lisp

    r11719 r11722  
    22012201   (let ((*code* *static-code*))
    22022202     (declare-field g +lisp-integer+)
    2203      (emit 'new +lisp-bignum-class+)
    2204      (emit 'dup)
    22052203     (cond ((<= most-negative-java-long n most-positive-java-long)
    22062204;;      (setf g (format nil "BIGNUM_~A~D"
     
    22082206;;          (abs n)))
    22092207      (emit 'ldc2_w (pool-long n))
    2210       (emit-invokespecial-init +lisp-bignum-class+ '("J")))
     2208      (emit-invokestatic +lisp-bignum-class+ "getInstance"
     2209                               '("J") +lisp-integer+))
    22112210   (t
    22122211    (let* ((*print-base* 10)
     
    22142213      (emit 'ldc (pool-string s))
    22152214      (emit-push-constant-int 10)
    2216       (emit-invokespecial-init +lisp-bignum-class+
    2217                                      (list +java-string+ "I")))))
     2215      (emit-invokestatic +lisp-bignum-class+ "getInstance"
     2216                               (list +java-string+ "I") +lisp-integer+))))
    22182217     (emit 'putstatic *this-class* g +lisp-integer+)
    22192218     (setf *static-code* *code*))
Note: See TracChangeset for help on using the changeset viewer.