1(*	$Id: LongInts.Mod,v 1.3 1999/09/02 13:14:52 acken Exp $	*)
2MODULE oocLongInts;
3
4 (*
5    LongInts - Simple extended integer implementation.
6    Copyright (C) 1996 Michael Griebling
7
8    This module is free software; you can redistribute it and/or modify
9    it under the terms of the GNU Lesser General Public License as
10    published by the Free Software Foundation; either version 2 of the
11    License, or (at your option) any later version.
12
13    This module is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU Lesser General Public License for more details.
17
18    You should have received a copy of the GNU Lesser General Public
19    License along with this program; if not, write to the Free Software
20    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21
22*)
23CONST
24  B*=8000H;
25
26TYPE
27  LongInt*=ARRAY 170 OF INTEGER;
28
29
30PROCEDURE MinDigit * (VAR w: LongInt) : LONGINT;
31VAR min, l: LONGINT;
32BEGIN
33  min:=1; l:=LEN(w)-1;
34  WHILE (min<l) & (w[min]=0) DO INC(min) END;
35  RETURN min
36END MinDigit;
37
38PROCEDURE MultDigit * (VAR w: LongInt; digit, k: LONGINT);
39VAR i, t, min: LONGINT;
40BEGIN
41  i:=LEN(w)-1; min:=MinDigit(w)-2;
42  REPEAT
43    t:=w[i]*digit+k;                  (* multiply *)
44    w[i]:=SHORT(t MOD B); k:=t DIV B; (* generate result & carry *)
45    DEC(i)
46  UNTIL i=min
47END MultDigit;
48
49PROCEDURE AddDigit * (VAR w: LongInt; k: LONGINT);
50VAR i, t, min: LONGINT;
51BEGIN
52  i:=LEN(w)-1; min:=MinDigit(w)-2;
53  REPEAT
54    t:=w[i]+k;                        (* add *)
55    w[i]:=SHORT(t MOD B); k:=t DIV B; (* generate result & carry *)
56    DEC(i)
57  UNTIL i=min
58END AddDigit;
59
60PROCEDURE DivDigit * (VAR w: LongInt; digit: LONGINT; VAR r: LONGINT);
61VAR j, t, m: LONGINT;
62BEGIN
63  j:=MinDigit(w)-1; r:=0; m:=LEN(w)-1;
64  REPEAT
65    t:=r*B+w[j];
66    w[j]:=SHORT(t DIV digit); r:=t MOD digit; (* generate result & remainder *)
67    INC(j)
68  UNTIL j>m
69END DivDigit;
70
71PROCEDURE TenPower * (VAR x: LongInt; power: INTEGER);
72VAR exp, i: INTEGER; d: LONGINT;
73BEGIN
74  IF power>0 THEN
75    exp:=power DIV 4; power:=power MOD 4;
76    FOR i:=1 TO exp DO MultDigit(x, 10000, 0) END;
77    FOR i:=1 TO power DO MultDigit(x, 10, 0) END
78  ELSIF power<0 THEN
79    power:=-power;
80    exp:=power DIV 4; power:=power MOD 4;
81    FOR i:=1 TO exp DO DivDigit(x, 10000, d) END;
82    FOR i:=1 TO power DO DivDigit(x, 10, d) END
83  END
84END TenPower;
85
86PROCEDURE BPower * (VAR x: LongInt; power: INTEGER);
87VAR i, lx: LONGINT;
88BEGIN
89  lx:=LEN(x);
90  IF power>0 THEN
91    FOR i:=1 TO lx-1-power DO x[i]:=x[i+power] END;
92    FOR i:=lx-power TO lx-1 DO x[i]:=0 END
93  ELSIF power<0 THEN
94    power:=-power;
95    FOR i:=lx-1-power TO 1 BY -1 DO x[i+power]:=x[i] END;
96    FOR i:=1 TO power DO x[i]:=0 END
97  END
98END BPower;
99
100
101END oocLongInts.
102