(FILECREATED "16-Sep-86 12:31:59" {ERIS}<LISPCORE>SOURCES>LLBIGNUM.;21 40010  

      changes to:  (VARS LLBIGNUMCOMS)
                   (FNS \BIGNUM.LOGAND \BIGNUM.LOGOR \BIGNUM.LOGXOR \BIGNUM.LSH \BIGNUM.QUOTIENT 
                        \BIGNUM.REMAINDER FINITEP INFINITEP BIGNUM.DEFPRINT \BN.INTEGERLENGTH 
                        \BN.FLOAT)

      previous date: "11-Sep-86 16:34:30" {ERIS}<LISPCORE>SOURCES>LLBIGNUM.;18)


(* Copyright (c) 1985, 1986 by Xerox Corporation. All rights reserved.)

(PRETTYCOMPRINT LLBIGNUMCOMS)

(RPAQQ LLBIGNUMCOMS 
       ((COMS (DECLARE: EVAL@COMPILE DONTCOPY (RECORDS BIGNUM))
              (INITRECORDS BIGNUM)
              (CONSTANTS \BIGNUM.THETA (\BIGNUM.BETA (EXPT 2 14))
                     (\BIGNUM.BETA1 (SUB1 \BIGNUM.BETA)))
              (DECLARE: EVAL@COMPILE (ADDVARS (CHARACTERNAMES (INFINITY 8551))))
              (ADDVARS (GLOBALVARS MIN.INTEGER MAX.INTEGER \BIG.0 \BIG.1)))
        (COMS (* entries)
              (FNS \BIGNUM.COMPARE \BIGNUM.DIFFERENCE \BIGNUM.INTEGERLENGTH \BIGNUM.LOGAND 
                   \BIGNUM.LOGOR \BIGNUM.LOGXOR \BIGNUM.PLUS \BIGNUM.LSH \BIGNUM.TIMES 
                   \BIGNUM.QUOTIENT \BIGNUM.REMAINDER \BIGNUM.TO.FLOAT)
              (FNS FINITEP INFINITEP))
        (COMS (* internal functions)
              (FNS \BIGNUM.TO.INT \BN.2TH \BN.ABS \BN.DIFFERENCE \BN.DIVIDE \BN.FLOAT \BN.IGNN 
                   BIGNUM.DEFPRINT \BN.INTEGERLENGTH \BN.LOGAND \BN.LOGANDC2 \BN.LOGOR \BN.LOGXOR 
                   \BN.MINUS \BN.PLUS2 \BN.SIGN \BN.TIMES2 \BN.COMPAREN \BN.D2TH \BN.FROM.FIXP 
                   \BN.ICANON \BN.IDIVIDE \BN.ISUM0 \BN.ISUM1 \BN.MADD \BN.TO.FIXP \BN.NZEROS \BN.QRS 
                   \BN.SIGN \BN.TH2B \BN.TH2D))
        (COMS (FNS \INITBIGNUMS)
              (* MAKERATIONAL needs work)
              (* needs work: MASK.1'S MASK.0'S BITTEST BITSET BITCLEAR LOGNOT LOADBYTE DEPOSITBYTE 
                 IMODLESSP IMODPLUS IMODDIFFERENCE ROT)
              (DECLARE: DONTEVAL@LOAD DOCOPY (P (\INITBIGNUMS))))))
(DECLARE: EVAL@COMPILE DONTCOPY 
[DECLARE: EVAL@COMPILE 

(DATATYPE BIGNUM (ELEMENTS)
                 (INIT (DEFPRINT (QUOTE BIGNUM)
                              (QUOTE BIGNUM.DEFPRINT))))
]
(/DECLAREDATATYPE (QUOTE BIGNUM)
       (QUOTE (POINTER))
       (QUOTE ((BIGNUM 0 POINTER)))
       (QUOTE 2))
(DEFPRINT (QUOTE BIGNUM)
       (QUOTE BIGNUM.DEFPRINT))
)
(/DECLAREDATATYPE (QUOTE BIGNUM)
       (QUOTE (POINTER))
       (QUOTE ((BIGNUM 0 POINTER)))
       (QUOTE 2))
(DEFPRINT (QUOTE BIGNUM)
       (QUOTE BIGNUM.DEFPRINT))
(DECLARE: EVAL@COMPILE 

(RPAQQ \BIGNUM.THETA 10000)

(RPAQ \BIGNUM.BETA (EXPT 2 14))

(RPAQ \BIGNUM.BETA1 (SUB1 \BIGNUM.BETA))

(CONSTANTS \BIGNUM.THETA (\BIGNUM.BETA (EXPT 2 14))
       (\BIGNUM.BETA1 (SUB1 \BIGNUM.BETA)))
)
(DECLARE: EVAL@COMPILE 

(ADDTOVAR CHARACTERNAMES (INFINITY 8551))
)

(ADDTOVAR GLOBALVARS MIN.INTEGER MAX.INTEGER \BIG.0 \BIG.1)



(* entries)

(DEFINEQ

(\BIGNUM.COMPARE
  [LAMBDA (X Y)                                              (* lmm "15-Apr-85 17:36")
    (COND
       ((EQ X MIN.INTEGER)
        (COND
           ((EQ Y MIN.INTEGER)
            0)
           (T -1)))
       ((EQ X MAX.INTEGER)
        (COND
           ((EQ Y MAX.INTEGER)
            0)
           (T 1)))
       ((EQ Y MIN.INTEGER)
        1)
       ((EQ Y MAX.INTEGER)
        -1)
       (T (\BN.COMPAREN (\BN.FROM.FIXP X)
                 (\BN.FROM.FIXP Y])

(\BIGNUM.DIFFERENCE
  [LAMBDA (X Y)                                              (* lmm "12-Apr-85 08:38")
    (\BN.TO.FIXP (\BN.DIFFERENCE (\BN.FROM.FIXP X)
                        (\BN.FROM.FIXP Y])

(\BIGNUM.INTEGERLENGTH
  [LAMBDA (X)                                                (* lmm "12-Apr-85 08:01")
    (\BN.INTEGERLENGTH (\BN.FROM.FIXP X])

(\BIGNUM.LOGAND
  [LAMBDA (X Y)                                              (* kbr: "16-Sep-86 12:28")
    (COND
       ((OR (EQ X 0)
            (EQ Y 0))
        0)
       ((OR (INFINITEP X)
            (INFINITEP Y))
        (ERROR "Can't do logical operations with infinity"))
       ((LESSP Y 0)
        (COND
           ((LESSP X 0)
            (DIFFERENCE -1 (LOGOR (LOGNOT X)
                                  (LOGNOT Y))))
           (T (\BN.TO.FIXP (\BN.LOGANDC2 (\BN.FROM.FIXP X)
                                  (\BN.FROM.FIXP (DIFFERENCE -1 Y)))))))
       ((LESSP X 0)
        (\BIGNUM.LOGAND Y X))
       (T (\BN.TO.FIXP (\BN.LOGAND (\BN.FROM.FIXP X)
                              (\BN.FROM.FIXP Y])

(\BIGNUM.LOGOR
  [LAMBDA (X Y)                                              (* kbr: "16-Sep-86 12:29")
    (COND
       ((EQ X 0)
        Y)
       ((EQ Y 0)
        X)
       ((OR (INFINITEP X)
            (INFINITEP Y))
        (ERROR "Can't do logical operations with infinity"))
       ((AND (GREATERP X 0)
             (GREATERP Y 0))
        (\BN.TO.FIXP (\BN.LOGOR (\BN.FROM.FIXP X)
                            (\BN.FROM.FIXP Y))))
       (T                                                    (* stupid slow but maybe working 
                                                             definition. Problem is that logors of 
                                                             negatives are difficult in current 
                                                             representation)
          (DIFFERENCE -1 (LOGAND (DIFFERENCE -1 X)
                                (DIFFERENCE -1 Y])

(\BIGNUM.LOGXOR
  [LAMBDA (X Y)                                              (* kbr: "16-Sep-86 12:29")
    (COND
       ((EQ X 0)
        Y)
       ((EQ Y 0)
        X)
       ((OR (INFINITEP X)
            (INFINITEP Y))
        (ERROR "Can't do logical operations with infinity"))
       ((LESSP X 0)
        (COND
           ((LESSP Y 0)
            (LOGXOR (DIFFERENCE -1 X)
                   (DIFFERENCE -1 Y)))
           (T                                                (* stupid dumb but working definition)
              (\BIGNUM.DIFFERENCE (\BIGNUM.LOGOR X Y)
                     (\BIGNUM.LOGAND X Y)))))
       ((LESSP Y 0)
        (\BIGNUM.LOGXOR Y X))
       (T (\BN.TO.FIXP (\BN.LOGXOR (\BN.FROM.FIXP X)
                              (\BN.FROM.FIXP Y])

(\BIGNUM.PLUS
  [LAMBDA (X Y)                                              (* lmm "12-Apr-85 08:03")
    (\BN.TO.FIXP (\BN.PLUS2 (\BN.FROM.FIXP X)
                        (\BN.FROM.FIXP Y])

(\BIGNUM.LSH
  [LAMBDA (X N)                                              (* kbr: "16-Sep-86 12:29")
    (COND
       ((EQ X 0)
        0)
       ((EQ N MIN.INTEGER)
        (COND
           ((INFINITEP X)
            (ERROR "Can't shift infinity minus infinity places"))
           (T 0)))
       ((INFINITEP X)
        X)
       ((EQ N MAX.INTEGER)
        (COND
           ((EQ X 0)
            0)
           ((IGREATERP X 0)
            MAX.INTEGER)
           (T MIN.INTEGER)))
       ((IGEQ N 0)
        (\BIGNUM.TIMES X (EXPT 2 N)))
       ((IGREATERP X 0)
        (\BIGNUM.QUOTIENT X (EXPT 2 (IMINUS N))))
       (T                                                    (* RIGHTSHIFT A NEGATIVE)
          (MINUS (\BIGNUM.LSH (MINUS X)
                        N])

(\BIGNUM.TIMES
  [LAMBDA (X Y)                                              (* lmm "12-Apr-85 08:03")
    (\BN.TO.FIXP (\BN.TIMES2 (\BN.FROM.FIXP X)
                        (\BN.FROM.FIXP Y])

(\BIGNUM.QUOTIENT
  [LAMBDA (X Y)                                              (* kbr: "16-Sep-86 12:30")
    (COND
       ((EQ Y MAX.INTEGER)
        (COND
           ((INFINITEP X)
            (ERROR "Can't divide infinity by infinity"))
           (T 0)))
       ((EQ Y MIN.INTEGER)
        (COND
           ((INFINITEP X)
            (ERROR "Can't divide infinity by infinity"))
           (T 0)))
       ((EQ X MAX.INTEGER)
        (COND
           ((EQ Y 0)
            (ERROR "Can't divide infinity by 0"))
           ((IGREATERP Y 0)
            MAX.INTEGER)
           (T MIN.INTEGER)))
       ((EQ X MIN.INTEGER)
        (COND
           ((EQ Y 0)
            (ERROR "Can't divide infinity by 0"))
           ((IGREATERP Y 0)
            MIN.INTEGER)
           (T MAX.INTEGER)))
       (T (\BN.TO.FIXP (CAR (\BN.DIVIDE (\BN.FROM.FIXP X)
                                   (\BN.FROM.FIXP Y])

(\BIGNUM.REMAINDER
  [LAMBDA (X Y)                                              (* kbr: "16-Sep-86 12:30")
    (COND
       ((OR (INFINITEP X)
            (INFINITEP Y))
        (ERROR "Can't take remainder with infinity"))
       (T (\BN.TO.FIXP (CDR (\BN.DIVIDE (\BN.FROM.FIXP X)
                                   (\BN.FROM.FIXP Y])

(\BIGNUM.TO.FLOAT
  [LAMBDA (X)                                                (* lmm "12-Apr-85 08:06")
                                                             (* called by \FLOAT)
    (\BN.FLOAT (\BN.FROM.FIXP X])
)
(DEFINEQ

(FINITEP
  [LAMBDA (NUMBER)                                           (* kbr: "16-Sep-86 12:24")
    (NOT (OR (EQ NUMBER MAX.INTEGER)
             (EQ NUMBER MIN.INTEGER])

(INFINITEP
  [LAMBDA (NUMBER)                                           (* kbr: "16-Sep-86 12:25")
    (OR (EQ NUMBER MAX.INTEGER)
        (EQ NUMBER MIN.INTEGER])
)



(* internal functions)

(DEFINEQ

(\BIGNUM.TO.INT
  [LAMBDA (X)                                                (* lmm " 9-Jan-86 15:30")
    (COND
       ((NULL (CDR X))
        (CAR X))
       (T (IPLUS (CAR X)
                 (ITIMES \BIGNUM.BETA (\BIGNUM.TO.INT (CDR X])

(\BN.2TH
  [LAMBDA (A)                                                (* lmm " 9-Jan-86 15:31")
    (PROG (L B)
          [while A do (PROGN (SETQ L (\BN.QRS A \BIGNUM.THETA))
                             (SETQ A (CAR L))
                             (SETQ B (CONS (CDR L)
                                           B]
          (RETURN B])

(\BN.ABS
  [LAMBDA (U)                                                (* lmm "20-JUL-84 02:00")
    (COND
       ((ILESSP (\BN.SIGN U)
               0)
        (\BN.MINUS U))
       (T U])

(\BN.DIFFERENCE
  [LAMBDA (U V)                                              (* lmm "20-JUL-84 01:33")
    (\BN.PLUS2 U (\BN.MINUS V])

(\BN.DIVIDE
  [LAMBDA (A B FLG)                                          (* lmm " 9-Jan-86 15:33")
    (PROG (M N K SA ST C D W E F B1 B2 A1 A2 A3 QHAT C1 R1 R2 U V Q X IP BIP L1 L2)
          [COND
             ((OR (NULL A)
                  (NULL B))
              (RETURN (CONS \BIG.0 A]
          (COND
             ((CDR B)
              (GO LL1)))
          (SETQ BIP (\BN.QRS A (CAR B)))
          [RETURN (CONS (CAR BIP)
                        (AND (NEQ FLG (QUOTE QUOTIENT))
                             (\BN.FROM.FIXP (CDR BIP]
      LL1 (SETQ M (FLENGTH A))
          (SETQ N (FLENGTH B))
          (SETQ K (IDIFFERENCE M N))
          [COND
             ((ILESSP K 0)
              (RETURN (CONS \BIG.0 A]
          (SETQ SA (\BN.SIGN A))
          (SETQ U B)
          (for i from 1 to (IDIFFERENCE N 1) do (SETQ U (CDR U)))
          (SETQ C (CAR U))
          (SETQ ST 1)
          [COND
             ((ILESSP C 0)
              (PROGN (SETQ ST -1)
                     (SETQ C (IMINUS C]
          (SETQ D (IQUOTIENT \BIGNUM.BETA (IPLUS C 1)))
          (SETQ W (ITIMES SA ST))
          [SETQ A (\BN.TIMES2 A (\BN.FROM.FIXP (ITIMES SA D]
          [SETQ B (\BN.TIMES2 B (\BN.FROM.FIXP (ITIMES ST D]
          (SETQ U A)
          (SETQ L1 NIL)
          [for I from 1 to (IPLUS K 1) do (PROGN (SETQ L1 (CONS U L1))
                                                 (SETQ U (CDR U]
          (SETQ L2 L1)
          (for I from 1 to (IDIFFERENCE N 2) do (SETQ L2 (CONS U L2))
                                                (SETQ U (CDR U)))
          [COND
             ((NULL (CDR U))
              (RPLACD U (CONS 0 NIL]
          (SETQ U B)
          (for I from 1 to (IDIFFERENCE N 2) do (SETQ U (CDR U)))
          (SETQ B2 (CAR U))
          (SETQ U (CDR U))
          (SETQ B1 (CAR U))
      L10 (SETQ U (CAR L2))
          (SETQ A3 (CAR U))
          (SETQ U (CDR U))
          (SETQ A2 (CAR U))
          (SETQ U (CDR U))
          (SETQ A1 (CAR U))
          (SETQ U (CDR U))
          [COND
             ((IGEQ A1 B1)
              (SETQ QHAT \BIGNUM.BETA1))
             (T (SETQ QHAT (IQUOTIENT (IPLUS (ITIMES A1 \BIGNUM.BETA)
                                             A2)
                                  B1]
      L12 (SETQ IP (\BN.IDIVIDE (ITIMES QHAT B1)
                          \BIGNUM.BETA))
          (SETQ R1 (IDIFFERENCE A1 (CAR IP)))
          (SETQ R2 (IDIFFERENCE A2 (CDR IP)))
          [COND
             ((ILESSP R2 0)
              (PROGN (SETQ R2 (IPLUS R2 \BIGNUM.BETA))
                     (SETQ R1 (IDIFFERENCE R1 1]
          (COND
             ((IGREATERP R1 0)
              (GO L13)))
          (SETQ IP (\BN.IDIVIDE (ITIMES QHAT B2)
                          \BIGNUM.BETA))
          (SETQ R1 (IDIFFERENCE R2 (CAR IP)))
          (COND
             ((IGREATERP R1 0)
              (GO L13)))
          (SETQ R2 (IDIFFERENCE A3 (CDR IP)))
          [COND
             ((OR (ILESSP R1 0)
                  (ILESSP R2 0))
              (PROGN (SETQ QHAT (IDIFFERENCE QHAT 1))
                     (GO L12]
      L13 (SETQ U (CAR L1))
          (SETQ V B)
          (SETQ C1 0)
      L14 (SETQ E (IMINUS QHAT))
          (SETQ IP (\BN.IDIVIDE (ITIMES E (CAR V))
                          \BIGNUM.BETA))
          (SETQ V (CDR V))
          (SETQ E (CAR IP))
          (SETQ A1 (CAR U))
          (SETQ IP (\BN.IDIVIDE (IPLUS C1 (IPLUS A1 (CDR IP)))
                          \BIGNUM.BETA))
          (SETQ A1 (CDR IP))
          (SETQ C1 (CAR IP))
          [COND
             ((ILESSP A1 0)
              (PROGN (SETQ A1 (IPLUS A1 \BIGNUM.BETA))
                     (SETQ C1 (IDIFFERENCE C1 1]
          (SETQ C1 (IPLUS C1 E))
          (RPLACA U A1)
          (SETQ X U)
          (SETQ U (CDR U))
          (COND
             (V (GO L14)))
          (SETQ A1 (IPLUS (CAR U)
                          C1))
          (SETQ U (CDR U))
          (RPLACD X \BIG.0)
          (COND
             ((EQ A1 0)
              (GO L17)))
          (SETQ U (CAR L1))
          (SETQ V B)
          (SETQ C1 0)
          (SETQ QHAT (IDIFFERENCE QHAT 1))
      L16 (SETQ A1 (CAR U))
          (SETQ B1 (CAR V))
          (SETQ V (CDR V))
          (SETQ IP (\BN.IDIVIDE (IPLUS C1 (IPLUS A1 B1))
                          \BIGNUM.BETA))
          (RPLACA U (CDR IP))
          (SETQ U (CDR U))
          (COND
             (V (GO L16)))
      L17 [COND
             ((OR (NEQ QHAT 0)
                  Q)
              (SETQ Q (CONS (ITIMES W QHAT)
                            Q]
          (SETQ L1 (CDR L1))
          (SETQ U (CAR L2))
          (SETQ L2 (CDR L2))
          (COND
             (L1 (GO L10)))
          (RETURN (CONS Q (AND (NEQ FLG (QUOTE QUOTIENT))
                               (CAR (\BN.QRS A (ITIMES SA D])

(\BN.FLOAT
  [LAMBDA (X)                                                (* kbr: "16-Sep-86 12:21")
    (COND
       ((NULL X)
        0.0)
       ((LISTP X)
        (FPLUS (CAR X)
               (FTIMES \BIGNUM.BETA (\BN.FLOAT (CDR X)))))
       ((OR (EQ X (QUOTE MAX.INTEGER))
            (EQ X (QUOTE MIN.INTEGER)))
          
          (* KBR: After some consideration, I've decided that it would be best that 
          rational infinities and floating point infinities be kept distinct in the same 
          way that we consider 1 distinct from 1.0.
          This is an admission that the systems of Lisp rationals and Lisp floating point 
          numbers are two disjoint sets of Lisp expressions.
          The semantics of these expressions--what they denote--is slightly more than the 
          rational numbers we attach to them. These expressions should be viewed as 
          denoting a pair consisting of a rational number and an atom recording the type 
          of the expression. A Lisp rational X denotes the pair
          (X RATIONAL) and a Lisp floating point number X denotes
          (X FLOAT) in our mind. The FLOAT operation is an injection that changes an 
          expression denoting a pair (X RATIONAL) into an expression denoting a pair
          (X FLOAT)%. Arithmetic on these expressions is typed arithmetic with rounding 
          of the results in the case of FLOATPs according to IEEE spec.
          Let's suppose that MAX.INTEGER and MAX.FLOAT are Lisp expressions denoting the 
          respective rational and floating point infinities.
          (We now know with the advent of Common Lisp that MAX.INTEGER and MIN.INTEGER 
          should have been called MAX.RATIONAL and MIN.RATIONAL) Then rules of floating 
          point coercion can continue to make sense:
          (EQL (/ MAX.INTEGER) 0) (EQL (/ MAX.FLOAT) 0.0)
          (EQL (EXPT 2 MAX.INTEGER) MAX.INTEGER) (EQL
          (EXPT 2.0 MAX.INTEGER) MAX.FLOAT) (EQL (EXPT 2 MAX.FLOAT) MAX.FLOAT)
          (EQL (EXPT 2.0 MAX.FLOAT) MAX.FLOAT) etc.
          But if we tried to make rational infinities and floating point infinities 
          identical, then we would have to arbitrarily decide in an unnatural way whether
          (EQL (/ MAX.INTEGER) 0) or (EQL (/ MAX.FLOAT) 0.0) is true, etc.
          Recommendation: Currently Xerox Lisp does not support floating point 
          infinities. Larry Masinter added rational infinities.
          If there is a desire to add floating point infinities at some point in the 
          future, then I recommend that rational and floating point infinities be kept 
          distinct. *)
          
          (* * Error because Xerox Lisp does not support floating point infinities
          (at this time)%. *)

        (ERROR "Can't float integer infinity."))
       (T (SHOULDNT])

(\BN.IGNN
  [LAMBDA (U)                                                (* lmm " 9-Jan-86 15:30")
    (COND
       ((NULL U)
        NIL)
       ((ILESSP U \BIGNUM.BETA)
        (LIST U))
       (T (PROG (Y)
                (SETQ Y (IQUOTIENT U \BIGNUM.BETA))
                (SETQ U (IDIFFERENCE U (ITIMES Y \BIGNUM.BETA)))
                (RETURN (CONS U (\BN.FROM.FIXP Y])

(BIGNUM.DEFPRINT
  [LAMBDA (BIGN STREAM)                                      (* kbr: "16-Sep-86 12:31")
    (COND
       ((INFINITEP BIGN)                                     (* Distinguished integers 
                                                             smaller/larger than any others.
                                                             Print using "evaluate at read time" 
                                                             syntax)
        (CONS (CONCAT (CHARACTER (fetch (READTABLEP HASHMACROCHAR) of *READTABLE*))
                     ".")
              (COND
                 ((EQ BIGN MIN.INTEGER)
                  (QUOTE MIN.INTEGER))
                 (T (QUOTE MAX.INTEGER)))))
       (T (LET* ((RADIX (\CHECKRADIX *PRINT-BASE*))
                 (TH (SELECTQ RADIX
                         (10 10000)
                         (8 4096)
                         (bind (TH ← RADIX)
                               NEWTH while (LEQ (SETQ NEWTH (TIMES TH RADIX))
                                                \BIGNUM.BETA) do (SETQ TH NEWTH)
                            finally (RETURN TH))))
                 (CHARS (\BN.TH2D (bind (ELS ← (fetch (BIGNUM ELEMENTS) of BIGN))
                                        L B while ELS do (SETQ L (\BN.QRS ELS TH))
                                                         (SETQ ELS (CAR L))
                                                         (SETQ B (CONS (CDR L)
                                                                       B)) finally (RETURN B))
                               RADIX TH)))
                (COND
                   (*PRINT-RADIX*                            (* need radix qualifier)
                          (COND
                             ((AND (EQ RADIX 8)
                                   (NOT (fetch (READTABLEP COMMONLISP) of *READTABLE*)))
                              (NCONC1 CHARS (CHARCODE Q)))
                             (T (push CHARS (SELECTQ RADIX
                                                (8 (CHARCODE o))
                                                (16 (CHARCODE x))
                                                (2 (CHARCODE b))
                                                (PROGN (push CHARS (CHARCODE r))
                                                       (COND
                                                          ((IGEQ RADIX 10)
                                                           (push CHARS (IPLUS (CHARCODE 0)
                                                                              (IMOD RADIX 10)))
                                                           (SETQ RADIX (IQUOTIENT RADIX 10))))
                                                       (IPLUS RADIX (CHARCODE 0)))))
                                (push CHARS (fetch (READTABLEP HASHMACROCHAR) of *READTABLE*))))))
                (.SPACECHECK. STREAM (LENGTH CHARS))
                (for C in CHARS do (\OUTCHAR STREAM C))      (* Return T to show we have done it 
                                                             ourselves)
                T])

(\BN.INTEGERLENGTH
  [LAMBDA (X)                                                (* kbr: "16-Sep-86 12:31")
    (COND
       ((NULL X)
        0)
       ((LISTP X)
        (COND
           ((CDR X)
            (IPLUS (CONSTANT (INTEGERLENGTH (SUB1 \BIGNUM.BETA)))
                   (\BN.INTEGERLENGTH (CDR X))))
           (T (INTEGERLENGTH (CAR X)))))
       ((INFINITEP X)
        MAX.INTEGER)
       (T (SHOULDNT])

(\BN.LOGAND
  [LAMBDA (B1 B2)                                            (* lmm "20-Jul-84 11:13")
    (COND
       ((NULL B1)
        NIL)
       ((NULL B2)
        NIL)
       (T (PROG (B)
                (SETQ B (\BN.LOGAND (CDR B1)
                               (CDR B2)))
                (SETQ B1 (LOGAND (CAR B1)
                                (CAR B2)))
                (COND
                   ((AND (NULL B)
                         (EQ B1 0))
                    (RETURN B)))
                (RETURN (CONS B1 B])

(\BN.LOGANDC2
  [LAMBDA (B1 B2)                                            (* lmm "14-May-86 10:47")
    (COND
       ((NULL B1)
        NIL)
       ((NULL B2)
        B1)
       (T (PROG (B)
                (SETQ B (\BN.LOGANDC2 (CDR B1)
                               (CDR B2)))
                [SETQ B1 (LOGAND (CAR B1)
                                (DIFFERENCE -1 (CAR B2]
                (COND
                   ((AND (NULL B)
                         (EQ B1 0))
                    (RETURN B)))
                (RETURN (CONS B1 B])

(\BN.LOGOR
  [LAMBDA (B1 B2)                                            (* lmm "21-JUL-84 23:57")
    (COND
       ((NULL B1)
        B2)
       ((NULL B2)
        B1)
       (T (CONS (LOGOR (CAR B1)
                       (CAR B2))
                (\BN.LOGOR (CDR B1)
                       (CDR B2])

(\BN.LOGXOR
  [LAMBDA (B1 B2)                                            (* lmm "21-JUL-84 23:59")
    (COND
       ((NULL B1)
        B2)
       ((NULL B2)
        B1)
       (T (CONS (LOGXOR (CAR B1)
                       (CAR B2))
                (\BN.LOGXOR (CDR B1)
                       (CDR B2])

(\BN.MINUS
  (LAMBDA (U)                                                (* kbr: "11-Sep-86 15:00")
    (COND
       ((NULL U)
        NIL)
       ((LISTP U)
        (CONS (IMINUS (CAR U))
              (\BN.MINUS (CDR U))))
       ((EQ U (QUOTE MAX.INTEGER))
        (QUOTE MIN.INTEGER))
       ((EQ U (QUOTE MIN.INTEGER))
        (QUOTE MAX.INTEGER))
       (T (SHOULDNT)))))

(\BN.PLUS2
  (LAMBDA (U V)                                              (* kbr: "11-Sep-86 15:26")
    (COND
       ((NULL U)
        V)
       ((NULL V)
        U)
       ((AND (LISTP U)
             (LISTP V))
        (PROG (L)
              (SETQ L (IDIFFERENCE (FLENGTH U)
                             (FLENGTH V)))
              (COND
                 ((ILESSP L 0)
                  (SETQ U (APPEND U (\BN.NZEROS (IDIFFERENCE 0 L)))))
                 ((IGREATERP L 0)
                  (SETQ V (APPEND V (\BN.NZEROS L)))))
              (RETURN (COND
                         ((EQ (\BN.SIGN U)
                              (\BN.SIGN V))
                          (\BN.ISUM0 U V))
                         (T (\BN.ISUM1 U V))))))
       ((EQ U (QUOTE MAX.INTEGER))
        (COND
           ((EQ V (QUOTE MIN.INTEGER))
            (ERROR "Can't add plus infinity to minus infinity"))
           (T U)))
       ((EQ U (QUOTE MIN.INTEGER))
        (COND
           ((EQ V (QUOTE MAX.INTEGER))
            (ERROR "Can't add plus infinity to minus infinity"))
           (T U)))
       (T V))))

(\BN.SIGN
  (LAMBDA (U)                                                (* kbr: "11-Sep-86 15:22")
    (COND
       ((ATOM U)
        (COND
           ((NULL U)
            0)
           ((EQ U (QUOTE MAX.INTEGER))
            1)
           ((EQ U (QUOTE MIN.INTEGER))
            -1)
           (T (SHOULDNT))))
       ((IGREATERP (CAR U)
               0)
        1)
       ((ILESSP (CAR U)
               0)
        -1)
       (T (\BN.SIGN (CDR U))))))

(\BN.TIMES2
  (LAMBDA (U V)                                              (* kbr: "11-Sep-86 15:19")
    (PROG (TAIL U1 W W1 W2 L C AP BP)
          (COND
             ((NULL U)
              (COND
                 ((OR (EQ V (QUOTE MAX.INTEGER))
                      (EQ V (QUOTE MIN.INTEGER)))
                  (ERROR "Can't multiply infinity and zero."))
                 (T (RETURN NIL))))
             ((EQ U (QUOTE MAX.INTEGER))
              (COND
                 ((NULL V)
                  (ERROR "Can't multiply infinity and zero."))
                 ((EQ (\BN.SIGN V)
                      1)
                  (RETURN U))
                 (T (RETURN (QUOTE MIN.INTEGER)))))
             ((EQ U (QUOTE MIN.INTEGER))
              (COND
                 ((NULL V)
                  (ERROR "Can't multiply infinity and zero."))
                 ((EQ (\BN.SIGN V)
                      1)
                  (RETURN U))
                 (T (RETURN (QUOTE MAX.INTEGER))))))
          (SETQ TAIL (LIST 0 0))
          (SETQ L (IPLUS (FLENGTH U)
                         (IDIFFERENCE (FLENGTH V)
                                2)))
          (SETQ W TAIL)
          (for I from 1 to L do (SETQ W (CONS 0 W)))
          (SETQ W1 W)
      A   (SETQ U1 U)
          (SETQ W2 W1)
          (SETQ C 0)
      B   (SETQ AP (\BN.IDIVIDE (ITIMES (CAR U1)
                                       (CAR V))
                          \BIGNUM.BETA))
          (SETQ BP (\BN.IDIVIDE (IPLUS (CAR W2)
                                       (IPLUS (CDR AP)
                                              C))
                          \BIGNUM.BETA))
          (RPLACA W2 (CDR BP))
          (SETQ C (IPLUS (CAR AP)
                         (CAR BP)))
          (SETQ W2 (CDR W2))
          (SETQ U1 (CDR U1))
          (COND
             (U1 (GO B)))
          (RPLACA W2 C)
          (SETQ W1 (CDR W1))
          (SETQ V (CDR V))
          (COND
             (V (GO A)))
          (COND
             ((EQ C 0)
              (RPLACD TAIL NIL)))
          (RETURN W))))

(\BN.COMPAREN
  [LAMBDA (U V)                                              (* lmm "12-Apr-85 08:33")
    (PROG ((SU 0)
           (SV 0)
           (ST 0)
           (S 0))
          [COND
             [(EQ (SETQ SU (\BN.SIGN U))
                  0)
              (RETURN (IMINUS (\BN.SIGN V]
             ((EQ (SETQ SV (\BN.SIGN V))
                  0)
              (RETURN SU))
             ((NEQ (SETQ S (IDIFFERENCE SU SV))
                   0)
              (RETURN (COND
                         ((IGREATERP S 0)
                          1)
                         ((ILESSP S 0)
                          -1)
                         (T (SHOULDNT]
      A   (COND
             ((NEQ (SETQ ST (IDIFFERENCE (CAR U)
                                   (CAR V)))
                   0)
              (SETQ S ST)))
          (SETQ V (CDR V))
          (SETQ U (CDR U))
          (COND
             [(NULL U)
              (RETURN (COND
                         (V (IMINUS SU))
                         (T (COND
                               ((IGREATERP S 0)
                                1)
                               ((ILESSP S 0)
                                -1)
                               (T 0]
             (V (GO A))
             (T (RETURN SU])

(\BN.D2TH
  [LAMBDA (U)                                                (* lmm " 9-Jan-86 15:31")
    (PROG (B S V BI M AI)
          (COND
             ((NULL U)
              (RETURN B)))
          [COND
             [(OR (EQ (CAR U)
                      (QUOTE +))
                  (EQ (CAR U)
                      (QUOTE -)))
              (PROGN (SETQ S (CAR U))
                     (SETQ U (CDR U]
             (T (SETQ S (QUOTE +]
          (COND
             ((NULL U)
              (RETURN B)))
          (SETQ U (SETQ V (REVERSE U)))
      L2  (SETQ BI 0)
          (SETQ M 1)
          [while (AND U (ILESSP M \BIGNUM.THETA)) do (PROGN (SETQ AI (CAR U))
                                                            (SETQ U (CDR U))
                                                            (SETQ BI (IPLUS (ITIMES AI M)
                                                                            BI))
                                                            (SETQ M (ITIMES 10 M]
          [COND
             ((EQ S (QUOTE -))
              (SETQ BI (IMINUS BI]
          (SETQ B (CONS BI B))
          (COND
             (U (GO L2)))
          (RETURN B])

(\BN.FROM.FIXP
  (LAMBDA (U)                                                (* kbr: "11-Sep-86 14:54")
    (COND
       ((type? BIGNUM U)
        (fetch (BIGNUM ELEMENTS) of U))
       ((OR (NULL U)
            (EQ U 0))
        NIL)
       ((LISTP U)
        U)
       ((ILESSP U 0)
        (COND
           ((EQUAL U MIN.FIXP)
            (\BN.DIFFERENCE (\BN.FROM.FIXP (IPLUS U \BIGNUM.THETA))
                   (\BN.FROM.FIXP \BIGNUM.THETA)))
           (T (\BN.MINUS (\BN.IGNN (IMINUS U))))))
       (T (\BN.IGNN U)))))

(\BN.ICANON
  [LAMBDA (U SIGN)                                           (* lmm " 9-Jan-86 15:30")
    (PROG ((U0 U)
           U1
           (CARRY 0)
           B)
      A   (SETQ B (IPLUS (CAR U)
                         CARRY))
          (SETQ CARRY (COND
                         ((AND (IGREATERP SIGN 0)
                               (ILESSP B 0))
                          -1)
                         ((AND (ILESSP SIGN 0)
                               (IGREATERP B 0))
                          1)
                         (T 0)))
          (SETQ B (IDIFFERENCE B (ITIMES CARRY \BIGNUM.BETA)))
          (RPLACA U B)
          (COND
             ((NEQ B 0)
              (SETQ U1 U)))
      B   (COND
             ((SETQ U (CDR U))
              (GO A)))
          (RETURN U0])

(\BN.IDIVIDE
  [LAMBDA (A B)                                              (* lmm "20-JUL-84 01:37")
    (CONS (IQUOTIENT A B)
          (IREMAINDER A B])

(\BN.ISUM0
  [LAMBDA (U V)                                              (* lmm " 9-Jan-86 15:30")
    (PROG ((CARRY 0)
           RES BP)
      A   (SETQ BP (\BN.IDIVIDE (IPLUS (CAR U)
                                       (IPLUS (CAR V)
                                              CARRY))
                          \BIGNUM.BETA))
          (SETQ CARRY (CAR BP))
          (SETQ RES (CONS (CDR BP)
                          RES))
          (SETQ U (CDR U))
          (SETQ V (CDR V))
          (COND
             (V (GO A)))
          [COND
             ((NEQ CARRY 0)
              (SETQ RES (CONS CARRY RES]
          (RETURN (REVERSE RES])

(\BN.ISUM1
  [LAMBDA (U V)                                              (* lmm "20-JUL-84 02:22")
    (PROG (C S RES)
          (SETQ C 0)
          (SETQ S 0)
      A   (SETQ C (IPLUS (CAR U)
                         (CAR V)))
          (COND
             ((NEQ C 0)
              (SETQ S C)))
          (SETQ RES (CONS C RES))
          (SETQ U (CDR U))
          (SETQ V (CDR V))
          (COND
             (V (GO A)))
          (RETURN (COND
                     ((EQ S 0)
                      NIL)
                     (T (\BN.ICANON (DREVERSE RES)
                               (COND
                                  ((ILESSP S 0)
                                   -1)
                                  (T 1])

(\BN.MADD
  [LAMBDA (A B C)                                            (* lmm " 9-Jan-86 15:30")
    (PROG (H TT TTT IP IPP)
          (SETQ TT A)
          (SETQ H 0)
      L2  (SETQ IP (\BN.IDIVIDE (ITIMES B (CAR TT))
                          \BIGNUM.BETA))
          (SETQ IPP (\BN.IDIVIDE (IPLUS C (IPLUS (CDR IP)
                                                 H))
                           \BIGNUM.BETA))
          (RPLACA TT (CDR IPP))
          (SETQ H (CAR IP))
          (SETQ C (CAR IPP))
          (SETQ TTT TT)
          (SETQ TT (CDR TT))
          (COND
             (TT (GO L2)))
          (SETQ C (IPLUS C H))
          (COND
             ((EQ C 0)
              (RETURN A)))
          (RPLACD TTT (CONS C (CDR TTT)))
          (RETURN A])

(\BN.TO.FIXP
  (LAMBDA (X)                                                (* kbr: "11-Sep-86 14:58")
    (COND
       ((LISTP X)
        (COND
           ((OR (EQ (\BN.COMPAREN X (CONSTANT (\BN.FROM.FIXP MAX.FIXP)))
                    1)
                (EQ (\BN.COMPAREN X (CONSTANT (\BN.FROM.FIXP MIN.FIXP)))
                    -1))
            (create BIGNUM
                   ELEMENTS ← X))
           (T (\BIGNUM.TO.INT X))))
       ((NULL X)
        0)
       ((EQ X (QUOTE MAX.INTEGER))
        MAX.INTEGER)
       ((EQ X (QUOTE MIN.INTEGER))
        MIN.INTEGER)
       (T (SHOULDNT)))))

(\BN.NZEROS
  [LAMBDA (N)                                                (* lmm "20-JUL-84 02:30")
    (for I from 1 to N collect 0])

(\BN.QRS
  [LAMBDA (B I)                                              (* lmm " 9-Jan-86 15:30")
    (PROG (D CP C1 C2)
          [COND
             ((NULL B)
              (RETURN (CONS B 0]
          (COND
             ((EQ I 0)
              (ERROR " QRS DIV BY 0 ")))
          (SETQ B (REVERSE B))
          (SETQ C1 0)
      A   (SETQ C2 (CAR B))
          (SETQ CP (\BN.IDIVIDE (IPLUS (ITIMES C1 \BIGNUM.BETA)
                                       C2)
                          I))
          [COND
             ((OR D (NOT (EQ (CAR CP)
                             0)))
              (SETQ D (CONS (CAR CP)
                            D]
          (SETQ B (CDR B))
          (SETQ C1 (CDR CP))
          [COND
             ((NULL B)
              (RETURN (CONS D C1]
          (GO A])

(\BN.SIGN
  (LAMBDA (U)                                                (* kbr: "11-Sep-86 15:22")
    (COND
       ((ATOM U)
        (COND
           ((NULL U)
            0)
           ((EQ U (QUOTE MAX.INTEGER))
            1)
           ((EQ U (QUOTE MIN.INTEGER))
            -1)
           (T (SHOULDNT))))
       ((IGREATERP (CAR U)
               0)
        1)
       ((ILESSP (CAR U)
               0)
        -1)
       (T (\BN.SIGN (CDR U))))))

(\BN.TH2B
  [LAMBDA (U)                                                (* lmm " 9-Jan-86 15:31")
    (PROG (AI B)
          (COND
             ((NULL U)
              (RETURN B)))
          (SETQ AI (CAR U))
          (SETQ U (CDR U))
          (SETQ B (CONS AI B))
          [while U do (PROGN (SETQ AI (CAR U))
                             (SETQ U (CDR U))
                             (SETQ B (\BN.MADD B \BIGNUM.THETA AI]
          (RETURN B])

(\BN.TH2D
  [LAMBDA (A RADIX TH)                                       (* bvm: "15-Apr-86 14:28")
          
          (* * A is a list of integers obtained by repeatedly dividing some bignum by TH, 
          which is a power of RADIX, hopefully chosen to keep the integers small.
          The elements of A concatenated thus make up the print name of the bignum in the 
          indicated RADIX. Convert the list to a series of character codes by computing 
          the print names of each subpart)

    (OR RADIX (SETQ RADIX 10))
    (COND
       ((NULL A)
        (LIST (CHARCODE 0)))
       (T (for AI in A bind (MAXFACTOR ← (IQUOTIENT TH RADIX))
                            DIGIT RESULT
             do [for (M ← MAXFACTOR) by (IQUOTIENT M RADIX) repeatuntil (EQ M 1)
                   do (SETQ DIGIT (IQUOTIENT AI M))
                      (SETQ AI (IDIFFERENCE AI (ITIMES DIGIT M)))
                      (COND
                         ((OR RESULT (NEQ DIGIT 0))
                          (push RESULT (COND
                                          ((GEQ (SETQ DIGIT (ABS DIGIT))
                                                10)          (* Use alphabetics for digits greater 
                                                             than 9)
                                           (IPLUS (IDIFFERENCE DIGIT 10)
                                                  (CHARCODE A)))
                                          (T (IPLUS DIGIT (CHARCODE 0]
             finally (RETURN (COND
                                ((IGREATERP (CAR A)
                                        0)
                                 (REVERSE RESULT))
                                (T                           (* Negative bignum)
                                   (CONS (CHARCODE -)
                                         (REVERSE RESULT])
)
(DEFINEQ

(\INITBIGNUMS
  [LAMBDA NIL                                                (* lmm "12-Apr-85 08:13")
    (SETQ \BIG.0 (\BN.FROM.FIXP 0))
    (SETQ \BIG.1 (\BN.FROM.FIXP 1))
    (SETQ MIN.INTEGER (create BIGNUM
                             ELEMENTS ← (QUOTE MIN.INTEGER)))
    (SETQ MAX.INTEGER (create BIGNUM
                             ELEMENTS ← (QUOTE MAX.INTEGER)))
    (\SETTYPEMASK (\TYPENUMBERFROMNAME (QUOTE BIGNUM))
           (LOGOR \TT.FIXP \TT.NUMBERP \TT.ATOM])
)



(* MAKERATIONAL needs work)




(* needs work: MASK.1'S MASK.0'S BITTEST BITSET BITCLEAR LOGNOT LOADBYTE DEPOSITBYTE IMODLESSP 
IMODPLUS IMODDIFFERENCE ROT)

(DECLARE: DONTEVAL@LOAD DOCOPY 
(\INITBIGNUMS)
)
(PUTPROPS LLBIGNUM COPYRIGHT ("Xerox Corporation" 1985 1986))
(DECLARE: DONTCOPY
  (FILEMAP (NIL (2974 9263 (\BIGNUM.COMPARE 2984 . 3489) (\BIGNUM.DIFFERENCE 3491 . 3719) (
\BIGNUM.INTEGERLENGTH 3721 . 3892) (\BIGNUM.LOGAND 3894 . 4666) (\BIGNUM.LOGOR 4668 . 5629) (
\BIGNUM.LOGXOR 5631 . 6457) (\BIGNUM.PLUS 6459 . 6676) (\BIGNUM.LSH 6678 . 7482) (\BIGNUM.TIMES 7484
 . 7703) (\BIGNUM.QUOTIENT 7705 . 8642) (\BIGNUM.REMAINDER 8644 . 9015) (\BIGNUM.TO.FLOAT 9017 . 9261)
) (9264 9636 (FINITEP 9274 . 9457) (INFINITEP 9459 . 9634)) (9668 39192 (\BIGNUM.TO.INT 9678 . 9934) (
\BN.2TH 9936 . 10300) (\BN.ABS 10302 . 10511) (\BN.DIFFERENCE 10513 . 10667) (\BN.DIVIDE 10669 . 15571
) (\BN.FLOAT 15573 . 18567) (\BN.IGNN 18569 . 18959) (BIGNUM.DEFPRINT 18961 . 22207) (
\BN.INTEGERLENGTH 22209 . 22646) (\BN.LOGAND 22648 . 23188) (\BN.LOGANDC2 23190 . 23746) (\BN.LOGOR 
23748 . 24065) (\BN.LOGXOR 24067 . 24387) (\BN.MINUS 24389 . 24781) (\BN.PLUS2 24783 . 25915) (
\BN.SIGN 25917 . 26387) (\BN.TIMES2 26389 . 28487) (\BN.COMPAREN 28489 . 29782) (\BN.D2TH 29784 . 
30978) (\BN.FROM.FIXP 30980 . 31553) (\BN.ICANON 31555 . 32355) (\BN.IDIVIDE 32357 . 32522) (\BN.ISUM0
 32524 . 33185) (\BN.ISUM1 33187 . 33924) (\BN.MADD 33926 . 34705) (\BN.TO.FIXP 34707 . 35341) (
\BN.NZEROS 35343 . 35504) (\BN.QRS 35506 . 36313) (\BN.SIGN 36315 . 36785) (\BN.TH2B 36787 . 37258) (
\BN.TH2D 37260 . 39190)) (39193 39708 (\INITBIGNUMS 39203 . 39706)))))
STOP