OSDN Git Service

2008-05-27 Thomas Quinot <quinot@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / uintp.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                                U I N T P                                 --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --          Copyright (C) 1992-2008, Free Software Foundation, Inc.         --
10 --                                                                          --
11 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
12 -- terms of the  GNU General Public License as published  by the Free Soft- --
13 -- ware  Foundation;  either version 2,  or (at your option) any later ver- --
14 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
17 -- for  more details.  You should have  received  a copy of the GNU General --
18 -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
19 -- to  the  Free Software Foundation,  51  Franklin  Street,  Fifth  Floor, --
20 -- Boston, MA 02110-1301, USA.                                              --
21 --                                                                          --
22 -- As a special exception,  if other files  instantiate  generics from this --
23 -- unit, or you link  this unit with other files  to produce an executable, --
24 -- this  unit  does not  by itself cause  the resulting  executable  to  be --
25 -- covered  by the  GNU  General  Public  License.  This exception does not --
26 -- however invalidate  any other reasons why  the executable file  might be --
27 -- covered by the  GNU Public License.                                      --
28 --                                                                          --
29 -- GNAT was originally developed  by the GNAT team at  New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
31 --                                                                          --
32 ------------------------------------------------------------------------------
33
34 with Output;  use Output;
35 with Tree_IO; use Tree_IO;
36
37 with GNAT.HTable; use GNAT.HTable;
38
39 package body Uintp is
40
41    ------------------------
42    -- Local Declarations --
43    ------------------------
44
45    Uint_Int_First : Uint := Uint_0;
46    --  Uint value containing Int'First value, set by Initialize. The initial
47    --  value of Uint_0 is used for an assertion check that ensures that this
48    --  value is not used before it is initialized. This value is used in the
49    --  UI_Is_In_Int_Range predicate, and it is right that this is a host value,
50    --  since the issue is host representation of integer values.
51
52    Uint_Int_Last : Uint;
53    --  Uint value containing Int'Last value set by Initialize
54
55    UI_Power_2 : array (Int range 0 .. 64) of Uint;
56    --  This table is used to memoize exponentiations by powers of 2. The Nth
57    --  entry, if set, contains the Uint value 2 ** N. Initially UI_Power_2_Set
58    --  is zero and only the 0'th entry is set, the invariant being that all
59    --  entries in the range 0 .. UI_Power_2_Set are initialized.
60
61    UI_Power_2_Set : Nat;
62    --  Number of entries set in UI_Power_2;
63
64    UI_Power_10 : array (Int range 0 .. 64) of Uint;
65    --  This table is used to memoize exponentiations by powers of 10 in the
66    --  same manner as described above for UI_Power_2.
67
68    UI_Power_10_Set : Nat;
69    --  Number of entries set in UI_Power_10;
70
71    Uints_Min   : Uint;
72    Udigits_Min : Int;
73    --  These values are used to make sure that the mark/release mechanism does
74    --  not destroy values saved in the U_Power tables or in the hash table used
75    --  by UI_From_Int. Whenever an entry is made in either of these tables,
76    --  Uints_Min and Udigits_Min are updated to protect the entry, and Release
77    --  never cuts back beyond these minimum values.
78
79    Int_0 : constant Int := 0;
80    Int_1 : constant Int := 1;
81    Int_2 : constant Int := 2;
82    --  These values are used in some cases where the use of numeric literals
83    --  would cause ambiguities (integer vs Uint).
84
85    ----------------------------
86    -- UI_From_Int Hash Table --
87    ----------------------------
88
89    --  UI_From_Int uses a hash table to avoid duplicating entries and wasting
90    --  storage. This is particularly important for complex cases of back
91    --  annotation.
92
93    subtype Hnum is Nat range 0 .. 1022;
94
95    function Hash_Num (F : Int) return Hnum;
96    --  Hashing function
97
98    package UI_Ints is new Simple_HTable (
99      Header_Num => Hnum,
100      Element    => Uint,
101      No_Element => No_Uint,
102      Key        => Int,
103      Hash       => Hash_Num,
104      Equal      => "=");
105
106    -----------------------
107    -- Local Subprograms --
108    -----------------------
109
110    function Direct (U : Uint) return Boolean;
111    pragma Inline (Direct);
112    --  Returns True if U is represented directly
113
114    function Direct_Val (U : Uint) return Int;
115    --  U is a Uint for is represented directly. The returned result is the
116    --  value represented.
117
118    function GCD (Jin, Kin : Int) return Int;
119    --  Compute GCD of two integers. Assumes that Jin >= Kin >= 0
120
121    procedure Image_Out
122      (Input     : Uint;
123       To_Buffer : Boolean;
124       Format    : UI_Format);
125    --  Common processing for UI_Image and UI_Write, To_Buffer is set True for
126    --  UI_Image, and false for UI_Write, and Format is copied from the Format
127    --  parameter to UI_Image or UI_Write.
128
129    procedure Init_Operand (UI : Uint; Vec : out UI_Vector);
130    pragma Inline (Init_Operand);
131    --  This procedure puts the value of UI into the vector in canonical
132    --  multiple precision format. The parameter should be of the correct size
133    --  as determined by a previous call to N_Digits (UI). The first digit of
134    --  Vec contains the sign, all other digits are always non- negative. Note
135    --  that the input may be directly represented, and in this case Vec will
136    --  contain the corresponding one or two digit value. The low bound of Vec
137    --  is always 1.
138
139    function Least_Sig_Digit (Arg : Uint) return Int;
140    pragma Inline (Least_Sig_Digit);
141    --  Returns the Least Significant Digit of Arg quickly. When the given Uint
142    --  is less than 2**15, the value returned is the input value, in this case
143    --  the result may be negative. It is expected that any use will mask off
144    --  unnecessary bits. This is used for finding Arg mod B where B is a power
145    --  of two. Hence the actual base is irrelevant as long as it is a power of
146    --  two.
147
148    procedure Most_Sig_2_Digits
149      (Left      : Uint;
150       Right     : Uint;
151       Left_Hat  : out Int;
152       Right_Hat : out Int);
153    --  Returns leading two significant digits from the given pair of Uint's.
154    --  Mathematically: returns Left / (Base ** K) and Right / (Base ** K) where
155    --  K is as small as possible S.T. Right_Hat < Base * Base. It is required
156    --  that Left > Right for the algorithm to work.
157
158    function N_Digits (Input : Uint) return Int;
159    pragma Inline (N_Digits);
160    --  Returns number of "digits" in a Uint
161
162    function Sum_Digits (Left : Uint; Sign : Int) return Int;
163    --  If Sign = 1 return the sum of the "digits" of Abs (Left). If the total
164    --  has more then one digit then return Sum_Digits of total.
165
166    function Sum_Double_Digits (Left : Uint; Sign : Int) return Int;
167    --  Same as above but work in New_Base = Base * Base
168
169    procedure UI_Div_Rem
170      (Left, Right       : Uint;
171       Quotient          : out Uint;
172       Remainder         : out Uint;
173       Discard_Quotient  : Boolean;
174       Discard_Remainder : Boolean);
175    --  Compute Euclidean division of Left by Right, and return Quotient and
176    --  signed Remainder (Left rem Right).
177    --
178    --    If Discard_Quotient is True, Quotient is left unchanged.
179    --    If Discard_Remainder is True, Remainder is left unchanged.
180
181    function Vector_To_Uint
182      (In_Vec   : UI_Vector;
183       Negative : Boolean) return Uint;
184    --  Functions that calculate values in UI_Vectors, call this function to
185    --  create and return the Uint value. In_Vec contains the multiple precision
186    --  (Base) representation of a non-negative value. Leading zeroes are
187    --  permitted. Negative is set if the desired result is the negative of the
188    --  given value. The result will be either the appropriate directly
189    --  represented value, or a table entry in the proper canonical format is
190    --  created and returned.
191    --
192    --  Note that Init_Operand puts a signed value in the result vector, but
193    --  Vector_To_Uint is always presented with a non-negative value. The
194    --  processing of signs is something that is done by the caller before
195    --  calling Vector_To_Uint.
196
197    ------------
198    -- Direct --
199    ------------
200
201    function Direct (U : Uint) return Boolean is
202    begin
203       return Int (U) <= Int (Uint_Direct_Last);
204    end Direct;
205
206    ----------------
207    -- Direct_Val --
208    ----------------
209
210    function Direct_Val (U : Uint) return Int is
211    begin
212       pragma Assert (Direct (U));
213       return Int (U) - Int (Uint_Direct_Bias);
214    end Direct_Val;
215
216    ---------
217    -- GCD --
218    ---------
219
220    function GCD (Jin, Kin : Int) return Int is
221       J, K, Tmp : Int;
222
223    begin
224       pragma Assert (Jin >= Kin);
225       pragma Assert (Kin >= Int_0);
226
227       J := Jin;
228       K := Kin;
229       while K /= Uint_0 loop
230          Tmp := J mod K;
231          J := K;
232          K := Tmp;
233       end loop;
234
235       return J;
236    end GCD;
237
238    --------------
239    -- Hash_Num --
240    --------------
241
242    function Hash_Num (F : Int) return Hnum is
243    begin
244       return Standard."mod" (F, Hnum'Range_Length);
245    end Hash_Num;
246
247    ---------------
248    -- Image_Out --
249    ---------------
250
251    procedure Image_Out
252      (Input     : Uint;
253       To_Buffer : Boolean;
254       Format    : UI_Format)
255    is
256       Marks  : constant Uintp.Save_Mark := Uintp.Mark;
257       Base   : Uint;
258       Ainput : Uint;
259
260       Digs_Output : Natural := 0;
261       --  Counts digits output. In hex mode, but not in decimal mode, we
262       --  put an underline after every four hex digits that are output.
263
264       Exponent : Natural := 0;
265       --  If the number is too long to fit in the buffer, we switch to an
266       --  approximate output format with an exponent. This variable records
267       --  the exponent value.
268
269       function Better_In_Hex return Boolean;
270       --  Determines if it is better to generate digits in base 16 (result
271       --  is true) or base 10 (result is false). The choice is purely a
272       --  matter of convenience and aesthetics, so it does not matter which
273       --  value is returned from a correctness point of view.
274
275       procedure Image_Char (C : Character);
276       --  Internal procedure to output one character
277
278       procedure Image_Exponent (N : Natural);
279       --  Output non-zero exponent. Note that we only use the exponent form in
280       --  the buffer case, so we know that To_Buffer is true.
281
282       procedure Image_Uint (U : Uint);
283       --  Internal procedure to output characters of non-negative Uint
284
285       -------------------
286       -- Better_In_Hex --
287       -------------------
288
289       function Better_In_Hex return Boolean is
290          T16 : constant Uint := Uint_2 ** Int'(16);
291          A   : Uint;
292
293       begin
294          A := UI_Abs (Input);
295
296          --  Small values up to 2**16 can always be in decimal
297
298          if A < T16 then
299             return False;
300          end if;
301
302          --  Otherwise, see if we are a power of 2 or one less than a power
303          --  of 2. For the moment these are the only cases printed in hex.
304
305          if A mod Uint_2 = Uint_1 then
306             A := A + Uint_1;
307          end if;
308
309          loop
310             if A mod T16 /= Uint_0 then
311                return False;
312
313             else
314                A := A / T16;
315             end if;
316
317             exit when A < T16;
318          end loop;
319
320          while A > Uint_2 loop
321             if A mod Uint_2 /= Uint_0 then
322                return False;
323
324             else
325                A := A / Uint_2;
326             end if;
327          end loop;
328
329          return True;
330       end Better_In_Hex;
331
332       ----------------
333       -- Image_Char --
334       ----------------
335
336       procedure Image_Char (C : Character) is
337       begin
338          if To_Buffer then
339             if UI_Image_Length + 6 > UI_Image_Max then
340                Exponent := Exponent + 1;
341             else
342                UI_Image_Length := UI_Image_Length + 1;
343                UI_Image_Buffer (UI_Image_Length) := C;
344             end if;
345          else
346             Write_Char (C);
347          end if;
348       end Image_Char;
349
350       --------------------
351       -- Image_Exponent --
352       --------------------
353
354       procedure Image_Exponent (N : Natural) is
355       begin
356          if N >= 10 then
357             Image_Exponent (N / 10);
358          end if;
359
360          UI_Image_Length := UI_Image_Length + 1;
361          UI_Image_Buffer (UI_Image_Length) :=
362            Character'Val (Character'Pos ('0') + N mod 10);
363       end Image_Exponent;
364
365       ----------------
366       -- Image_Uint --
367       ----------------
368
369       procedure Image_Uint (U : Uint) is
370          H : constant array (Int range 0 .. 15) of Character :=
371                "0123456789ABCDEF";
372
373       begin
374          if U >= Base then
375             Image_Uint (U / Base);
376          end if;
377
378          if Digs_Output = 4 and then Base = Uint_16 then
379             Image_Char ('_');
380             Digs_Output := 0;
381          end if;
382
383          Image_Char (H (UI_To_Int (U rem Base)));
384
385          Digs_Output := Digs_Output + 1;
386       end Image_Uint;
387
388    --  Start of processing for Image_Out
389
390    begin
391       if Input = No_Uint then
392          Image_Char ('?');
393          return;
394       end if;
395
396       UI_Image_Length := 0;
397
398       if Input < Uint_0 then
399          Image_Char ('-');
400          Ainput := -Input;
401       else
402          Ainput := Input;
403       end if;
404
405       if Format = Hex
406         or else (Format = Auto and then Better_In_Hex)
407       then
408          Base := Uint_16;
409          Image_Char ('1');
410          Image_Char ('6');
411          Image_Char ('#');
412          Image_Uint (Ainput);
413          Image_Char ('#');
414
415       else
416          Base := Uint_10;
417          Image_Uint (Ainput);
418       end if;
419
420       if Exponent /= 0 then
421          UI_Image_Length := UI_Image_Length + 1;
422          UI_Image_Buffer (UI_Image_Length) := 'E';
423          Image_Exponent (Exponent);
424       end if;
425
426       Uintp.Release (Marks);
427    end Image_Out;
428
429    -------------------
430    -- Init_Operand --
431    -------------------
432
433    procedure Init_Operand (UI : Uint; Vec : out UI_Vector) is
434       Loc : Int;
435
436       pragma Assert (Vec'First = Int'(1));
437
438    begin
439       if Direct (UI) then
440          Vec (1) := Direct_Val (UI);
441
442          if Vec (1) >= Base then
443             Vec (2) := Vec (1) rem Base;
444             Vec (1) := Vec (1) / Base;
445          end if;
446
447       else
448          Loc := Uints.Table (UI).Loc;
449
450          for J in 1 .. Uints.Table (UI).Length loop
451             Vec (J) := Udigits.Table (Loc + J - 1);
452          end loop;
453       end if;
454    end Init_Operand;
455
456    ----------------
457    -- Initialize --
458    ----------------
459
460    procedure Initialize is
461    begin
462       Uints.Init;
463       Udigits.Init;
464
465       Uint_Int_First := UI_From_Int (Int'First);
466       Uint_Int_Last  := UI_From_Int (Int'Last);
467
468       UI_Power_2 (0) := Uint_1;
469       UI_Power_2_Set := 0;
470
471       UI_Power_10 (0) := Uint_1;
472       UI_Power_10_Set := 0;
473
474       Uints_Min := Uints.Last;
475       Udigits_Min := Udigits.Last;
476
477       UI_Ints.Reset;
478    end Initialize;
479
480    ---------------------
481    -- Least_Sig_Digit --
482    ---------------------
483
484    function Least_Sig_Digit (Arg : Uint) return Int is
485       V : Int;
486
487    begin
488       if Direct (Arg) then
489          V := Direct_Val (Arg);
490
491          if V >= Base then
492             V := V mod Base;
493          end if;
494
495          --  Note that this result may be negative
496
497          return V;
498
499       else
500          return
501            Udigits.Table
502             (Uints.Table (Arg).Loc + Uints.Table (Arg).Length - 1);
503       end if;
504    end Least_Sig_Digit;
505
506    ----------
507    -- Mark --
508    ----------
509
510    function Mark return Save_Mark is
511    begin
512       return (Save_Uint => Uints.Last, Save_Udigit => Udigits.Last);
513    end Mark;
514
515    -----------------------
516    -- Most_Sig_2_Digits --
517    -----------------------
518
519    procedure Most_Sig_2_Digits
520      (Left      : Uint;
521       Right     : Uint;
522       Left_Hat  : out Int;
523       Right_Hat : out Int)
524    is
525    begin
526       pragma Assert (Left >= Right);
527
528       if Direct (Left) then
529          Left_Hat  := Direct_Val (Left);
530          Right_Hat := Direct_Val (Right);
531          return;
532
533       else
534          declare
535             L1 : constant Int :=
536                    Udigits.Table (Uints.Table (Left).Loc);
537             L2 : constant Int :=
538                    Udigits.Table (Uints.Table (Left).Loc + 1);
539
540          begin
541             --  It is not so clear what to return when Arg is negative???
542
543             Left_Hat := abs (L1) * Base + L2;
544          end;
545       end if;
546
547       declare
548          Length_L : constant Int := Uints.Table (Left).Length;
549          Length_R : Int;
550          R1 : Int;
551          R2 : Int;
552          T  : Int;
553
554       begin
555          if Direct (Right) then
556             T := Direct_Val (Left);
557             R1 := abs (T / Base);
558             R2 := T rem Base;
559             Length_R := 2;
560
561          else
562             R1 := abs (Udigits.Table (Uints.Table (Right).Loc));
563             R2 := Udigits.Table (Uints.Table (Right).Loc + 1);
564             Length_R := Uints.Table (Right).Length;
565          end if;
566
567          if Length_L = Length_R then
568             Right_Hat := R1 * Base + R2;
569          elsif Length_L = Length_R + Int_1 then
570             Right_Hat := R1;
571          else
572             Right_Hat := 0;
573          end if;
574       end;
575    end Most_Sig_2_Digits;
576
577    ---------------
578    -- N_Digits --
579    ---------------
580
581    --  Note: N_Digits returns 1 for No_Uint
582
583    function N_Digits (Input : Uint) return Int is
584    begin
585       if Direct (Input) then
586          if Direct_Val (Input) >= Base then
587             return 2;
588          else
589             return 1;
590          end if;
591
592       else
593          return Uints.Table (Input).Length;
594       end if;
595    end N_Digits;
596
597    --------------
598    -- Num_Bits --
599    --------------
600
601    function Num_Bits (Input : Uint) return Nat is
602       Bits : Nat;
603       Num  : Nat;
604
605    begin
606       --  Largest negative number has to be handled specially, since it is in
607       --  Int_Range, but we cannot take the absolute value.
608
609       if Input = Uint_Int_First then
610          return Int'Size;
611
612       --  For any other number in Int_Range, get absolute value of number
613
614       elsif UI_Is_In_Int_Range (Input) then
615          Num := abs (UI_To_Int (Input));
616          Bits := 0;
617
618       --  If not in Int_Range then initialize bit count for all low order
619       --  words, and set number to high order digit.
620
621       else
622          Bits := Base_Bits * (Uints.Table (Input).Length - 1);
623          Num  := abs (Udigits.Table (Uints.Table (Input).Loc));
624       end if;
625
626       --  Increase bit count for remaining value in Num
627
628       while Types.">" (Num, 0) loop
629          Num := Num / 2;
630          Bits := Bits + 1;
631       end loop;
632
633       return Bits;
634    end Num_Bits;
635
636    ---------
637    -- pid --
638    ---------
639
640    procedure pid (Input : Uint) is
641    begin
642       UI_Write (Input, Decimal);
643       Write_Eol;
644    end pid;
645
646    ---------
647    -- pih --
648    ---------
649
650    procedure pih (Input : Uint) is
651    begin
652       UI_Write (Input, Hex);
653       Write_Eol;
654    end pih;
655
656    -------------
657    -- Release --
658    -------------
659
660    procedure Release (M : Save_Mark) is
661    begin
662       Uints.Set_Last   (Uint'Max (M.Save_Uint,   Uints_Min));
663       Udigits.Set_Last (Int'Max  (M.Save_Udigit, Udigits_Min));
664    end Release;
665
666    ----------------------
667    -- Release_And_Save --
668    ----------------------
669
670    procedure Release_And_Save (M : Save_Mark; UI : in out Uint) is
671    begin
672       if Direct (UI) then
673          Release (M);
674
675       else
676          declare
677             UE_Len : constant Pos := Uints.Table (UI).Length;
678             UE_Loc : constant Int := Uints.Table (UI).Loc;
679
680             UD : constant Udigits.Table_Type (1 .. UE_Len) :=
681                    Udigits.Table (UE_Loc .. UE_Loc + UE_Len - 1);
682
683          begin
684             Release (M);
685
686             Uints.Append ((Length => UE_Len, Loc => Udigits.Last + 1));
687             UI := Uints.Last;
688
689             for J in 1 .. UE_Len loop
690                Udigits.Append (UD (J));
691             end loop;
692          end;
693       end if;
694    end Release_And_Save;
695
696    procedure Release_And_Save (M : Save_Mark; UI1, UI2 : in out Uint) is
697    begin
698       if Direct (UI1) then
699          Release_And_Save (M, UI2);
700
701       elsif Direct (UI2) then
702          Release_And_Save (M, UI1);
703
704       else
705          declare
706             UE1_Len : constant Pos := Uints.Table (UI1).Length;
707             UE1_Loc : constant Int := Uints.Table (UI1).Loc;
708
709             UD1 : constant Udigits.Table_Type (1 .. UE1_Len) :=
710                     Udigits.Table (UE1_Loc .. UE1_Loc + UE1_Len - 1);
711
712             UE2_Len : constant Pos := Uints.Table (UI2).Length;
713             UE2_Loc : constant Int := Uints.Table (UI2).Loc;
714
715             UD2 : constant Udigits.Table_Type (1 .. UE2_Len) :=
716                     Udigits.Table (UE2_Loc .. UE2_Loc + UE2_Len - 1);
717
718          begin
719             Release (M);
720
721             Uints.Append ((Length => UE1_Len, Loc => Udigits.Last + 1));
722             UI1 := Uints.Last;
723
724             for J in 1 .. UE1_Len loop
725                Udigits.Append (UD1 (J));
726             end loop;
727
728             Uints.Append ((Length => UE2_Len, Loc => Udigits.Last + 1));
729             UI2 := Uints.Last;
730
731             for J in 1 .. UE2_Len loop
732                Udigits.Append (UD2 (J));
733             end loop;
734          end;
735       end if;
736    end Release_And_Save;
737
738    ----------------
739    -- Sum_Digits --
740    ----------------
741
742    --  This is done in one pass
743
744    --  Mathematically: assume base congruent to 1 and compute an equivalent
745    --  integer to Left.
746
747    --  If Sign = -1 return the alternating sum of the "digits"
748
749    --     D1 - D2 + D3 - D4 + D5 ...
750
751    --  (where D1 is Least Significant Digit)
752
753    --  Mathematically: assume base congruent to -1 and compute an equivalent
754    --  integer to Left.
755
756    --  This is used in Rem and Base is assumed to be 2 ** 15
757
758    --  Note: The next two functions are very similar, any style changes made
759    --  to one should be reflected in both.  These would be simpler if we
760    --  worked base 2 ** 32.
761
762    function Sum_Digits (Left : Uint; Sign : Int) return Int is
763    begin
764       pragma Assert (Sign = Int_1 or Sign = Int (-1));
765
766       --  First try simple case;
767
768       if Direct (Left) then
769          declare
770             Tmp_Int : Int := Direct_Val (Left);
771
772          begin
773             if Tmp_Int >= Base then
774                Tmp_Int := (Tmp_Int / Base) +
775                   Sign * (Tmp_Int rem Base);
776
777                   --  Now Tmp_Int is in [-(Base - 1) .. 2 * (Base - 1)]
778
779                if Tmp_Int >= Base then
780
781                   --  Sign must be 1
782
783                   Tmp_Int := (Tmp_Int / Base) + 1;
784
785                end if;
786
787                --  Now Tmp_Int is in [-(Base - 1) .. (Base - 1)]
788
789             end if;
790
791             return Tmp_Int;
792          end;
793
794       --  Otherwise full circuit is needed
795
796       else
797          declare
798             L_Length : constant Int := N_Digits (Left);
799             L_Vec    : UI_Vector (1 .. L_Length);
800             Tmp_Int  : Int;
801             Carry    : Int;
802             Alt      : Int;
803
804          begin
805             Init_Operand (Left, L_Vec);
806             L_Vec (1) := abs L_Vec (1);
807             Tmp_Int := 0;
808             Carry := 0;
809             Alt := 1;
810
811             for J in reverse 1 .. L_Length loop
812                Tmp_Int := Tmp_Int + Alt * (L_Vec (J) + Carry);
813
814                --  Tmp_Int is now between [-2 * Base + 1 .. 2 * Base - 1],
815                --  since old Tmp_Int is between [-(Base - 1) .. Base - 1]
816                --  and L_Vec is in [0 .. Base - 1] and Carry in [-1 .. 1]
817
818                if Tmp_Int >= Base then
819                   Tmp_Int := Tmp_Int - Base;
820                   Carry := 1;
821
822                elsif Tmp_Int <= -Base then
823                   Tmp_Int := Tmp_Int + Base;
824                   Carry := -1;
825
826                else
827                   Carry := 0;
828                end if;
829
830                --  Tmp_Int is now between [-Base + 1 .. Base - 1]
831
832                Alt := Alt * Sign;
833             end loop;
834
835             Tmp_Int := Tmp_Int + Alt * Carry;
836
837             --  Tmp_Int is now between [-Base .. Base]
838
839             if Tmp_Int >= Base then
840                Tmp_Int := Tmp_Int - Base + Alt * Sign * 1;
841
842             elsif Tmp_Int <= -Base then
843                Tmp_Int := Tmp_Int + Base + Alt * Sign * (-1);
844             end if;
845
846             --  Now Tmp_Int is in [-(Base - 1) .. (Base - 1)]
847
848             return Tmp_Int;
849          end;
850       end if;
851    end Sum_Digits;
852
853    -----------------------
854    -- Sum_Double_Digits --
855    -----------------------
856
857    --  Note: This is used in Rem, Base is assumed to be 2 ** 15
858
859    function Sum_Double_Digits (Left : Uint; Sign : Int) return Int is
860    begin
861       --  First try simple case;
862
863       pragma Assert (Sign = Int_1 or Sign = Int (-1));
864
865       if Direct (Left) then
866          return Direct_Val (Left);
867
868       --  Otherwise full circuit is needed
869
870       else
871          declare
872             L_Length      : constant Int := N_Digits (Left);
873             L_Vec         : UI_Vector (1 .. L_Length);
874             Most_Sig_Int  : Int;
875             Least_Sig_Int : Int;
876             Carry         : Int;
877             J             : Int;
878             Alt           : Int;
879
880          begin
881             Init_Operand (Left, L_Vec);
882             L_Vec (1) := abs L_Vec (1);
883             Most_Sig_Int := 0;
884             Least_Sig_Int := 0;
885             Carry := 0;
886             Alt := 1;
887             J := L_Length;
888
889             while J > Int_1 loop
890                Least_Sig_Int := Least_Sig_Int + Alt * (L_Vec (J) + Carry);
891
892                --  Least is in [-2 Base + 1 .. 2 * Base - 1]
893                --  Since L_Vec in [0 .. Base - 1] and Carry in [-1 .. 1]
894                --  and old Least in [-Base + 1 .. Base - 1]
895
896                if Least_Sig_Int >= Base then
897                   Least_Sig_Int := Least_Sig_Int - Base;
898                   Carry := 1;
899
900                elsif Least_Sig_Int <= -Base then
901                   Least_Sig_Int := Least_Sig_Int + Base;
902                   Carry := -1;
903
904                else
905                   Carry := 0;
906                end if;
907
908                --  Least is now in [-Base + 1 .. Base - 1]
909
910                Most_Sig_Int := Most_Sig_Int + Alt * (L_Vec (J - 1) + Carry);
911
912                --  Most is in [-2 Base + 1 .. 2 * Base - 1]
913                --  Since L_Vec in [0 ..  Base - 1] and Carry in  [-1 .. 1]
914                --  and old Most in [-Base + 1 .. Base - 1]
915
916                if Most_Sig_Int >= Base then
917                   Most_Sig_Int := Most_Sig_Int - Base;
918                   Carry := 1;
919
920                elsif Most_Sig_Int <= -Base then
921                   Most_Sig_Int := Most_Sig_Int + Base;
922                   Carry := -1;
923                else
924                   Carry := 0;
925                end if;
926
927                --  Most is now in [-Base + 1 .. Base - 1]
928
929                J := J - 2;
930                Alt := Alt * Sign;
931             end loop;
932
933             if J = Int_1 then
934                Least_Sig_Int := Least_Sig_Int + Alt * (L_Vec (J) + Carry);
935             else
936                Least_Sig_Int := Least_Sig_Int + Alt * Carry;
937             end if;
938
939             if Least_Sig_Int >= Base then
940                Least_Sig_Int := Least_Sig_Int - Base;
941                Most_Sig_Int := Most_Sig_Int + Alt * 1;
942
943             elsif Least_Sig_Int <= -Base then
944                Least_Sig_Int := Least_Sig_Int + Base;
945                Most_Sig_Int := Most_Sig_Int + Alt * (-1);
946             end if;
947
948             if Most_Sig_Int >= Base then
949                Most_Sig_Int := Most_Sig_Int - Base;
950                Alt := Alt * Sign;
951                Least_Sig_Int :=
952                  Least_Sig_Int + Alt * 1; -- cannot overflow again
953
954             elsif Most_Sig_Int <= -Base then
955                Most_Sig_Int := Most_Sig_Int + Base;
956                Alt := Alt * Sign;
957                Least_Sig_Int :=
958                  Least_Sig_Int + Alt * (-1); --  cannot overflow again.
959             end if;
960
961             return Most_Sig_Int * Base + Least_Sig_Int;
962          end;
963       end if;
964    end Sum_Double_Digits;
965
966    ---------------
967    -- Tree_Read --
968    ---------------
969
970    procedure Tree_Read is
971    begin
972       Uints.Tree_Read;
973       Udigits.Tree_Read;
974
975       Tree_Read_Int (Int (Uint_Int_First));
976       Tree_Read_Int (Int (Uint_Int_Last));
977       Tree_Read_Int (UI_Power_2_Set);
978       Tree_Read_Int (UI_Power_10_Set);
979       Tree_Read_Int (Int (Uints_Min));
980       Tree_Read_Int (Udigits_Min);
981
982       for J in 0 .. UI_Power_2_Set loop
983          Tree_Read_Int (Int (UI_Power_2 (J)));
984       end loop;
985
986       for J in 0 .. UI_Power_10_Set loop
987          Tree_Read_Int (Int (UI_Power_10 (J)));
988       end loop;
989
990    end Tree_Read;
991
992    ----------------
993    -- Tree_Write --
994    ----------------
995
996    procedure Tree_Write is
997    begin
998       Uints.Tree_Write;
999       Udigits.Tree_Write;
1000
1001       Tree_Write_Int (Int (Uint_Int_First));
1002       Tree_Write_Int (Int (Uint_Int_Last));
1003       Tree_Write_Int (UI_Power_2_Set);
1004       Tree_Write_Int (UI_Power_10_Set);
1005       Tree_Write_Int (Int (Uints_Min));
1006       Tree_Write_Int (Udigits_Min);
1007
1008       for J in 0 .. UI_Power_2_Set loop
1009          Tree_Write_Int (Int (UI_Power_2 (J)));
1010       end loop;
1011
1012       for J in 0 .. UI_Power_10_Set loop
1013          Tree_Write_Int (Int (UI_Power_10 (J)));
1014       end loop;
1015
1016    end Tree_Write;
1017
1018    -------------
1019    -- UI_Abs --
1020    -------------
1021
1022    function UI_Abs (Right : Uint) return Uint is
1023    begin
1024       if Right < Uint_0 then
1025          return -Right;
1026       else
1027          return Right;
1028       end if;
1029    end UI_Abs;
1030
1031    -------------
1032    -- UI_Add --
1033    -------------
1034
1035    function UI_Add (Left : Int; Right : Uint) return Uint is
1036    begin
1037       return UI_Add (UI_From_Int (Left), Right);
1038    end UI_Add;
1039
1040    function UI_Add (Left : Uint; Right : Int) return Uint is
1041    begin
1042       return UI_Add (Left, UI_From_Int (Right));
1043    end UI_Add;
1044
1045    function UI_Add (Left : Uint; Right : Uint) return Uint is
1046    begin
1047       --  Simple cases of direct operands and addition of zero
1048
1049       if Direct (Left) then
1050          if Direct (Right) then
1051             return UI_From_Int (Direct_Val (Left) + Direct_Val (Right));
1052
1053          elsif Int (Left) = Int (Uint_0) then
1054             return Right;
1055          end if;
1056
1057       elsif Direct (Right) and then Int (Right) = Int (Uint_0) then
1058          return Left;
1059       end if;
1060
1061       --  Otherwise full circuit is needed
1062
1063       declare
1064          L_Length   : constant Int := N_Digits (Left);
1065          R_Length   : constant Int := N_Digits (Right);
1066          L_Vec      : UI_Vector (1 .. L_Length);
1067          R_Vec      : UI_Vector (1 .. R_Length);
1068          Sum_Length : Int;
1069          Tmp_Int    : Int;
1070          Carry      : Int;
1071          Borrow     : Int;
1072          X_Bigger   : Boolean := False;
1073          Y_Bigger   : Boolean := False;
1074          Result_Neg : Boolean := False;
1075
1076       begin
1077          Init_Operand (Left, L_Vec);
1078          Init_Operand (Right, R_Vec);
1079
1080          --  At least one of the two operands is in multi-digit form.
1081          --  Calculate the number of digits sufficient to hold result.
1082
1083          if L_Length > R_Length then
1084             Sum_Length := L_Length + 1;
1085             X_Bigger := True;
1086          else
1087             Sum_Length := R_Length + 1;
1088
1089             if R_Length > L_Length then
1090                Y_Bigger := True;
1091             end if;
1092          end if;
1093
1094          --  Make copies of the absolute values of L_Vec and R_Vec into X and Y
1095          --  both with lengths equal to the maximum possibly needed. This makes
1096          --  looping over the digits much simpler.
1097
1098          declare
1099             X      : UI_Vector (1 .. Sum_Length);
1100             Y      : UI_Vector (1 .. Sum_Length);
1101             Tmp_UI : UI_Vector (1 .. Sum_Length);
1102
1103          begin
1104             for J in 1 .. Sum_Length - L_Length loop
1105                X (J) := 0;
1106             end loop;
1107
1108             X (Sum_Length - L_Length + 1) := abs L_Vec (1);
1109
1110             for J in 2 .. L_Length loop
1111                X (J + (Sum_Length - L_Length)) := L_Vec (J);
1112             end loop;
1113
1114             for J in 1 .. Sum_Length - R_Length loop
1115                Y (J) := 0;
1116             end loop;
1117
1118             Y (Sum_Length - R_Length + 1) := abs R_Vec (1);
1119
1120             for J in 2 .. R_Length loop
1121                Y (J + (Sum_Length - R_Length)) := R_Vec (J);
1122             end loop;
1123
1124             if (L_Vec (1) < Int_0) = (R_Vec (1) < Int_0) then
1125
1126                --  Same sign so just add
1127
1128                Carry := 0;
1129                for J in reverse 1 .. Sum_Length loop
1130                   Tmp_Int := X (J) + Y (J) + Carry;
1131
1132                   if Tmp_Int >= Base then
1133                      Tmp_Int := Tmp_Int - Base;
1134                      Carry := 1;
1135                   else
1136                      Carry := 0;
1137                   end if;
1138
1139                   X (J) := Tmp_Int;
1140                end loop;
1141
1142                return Vector_To_Uint (X, L_Vec (1) < Int_0);
1143
1144             else
1145                --  Find which one has bigger magnitude
1146
1147                if not (X_Bigger or Y_Bigger) then
1148                   for J in L_Vec'Range loop
1149                      if abs L_Vec (J) > abs R_Vec (J) then
1150                         X_Bigger := True;
1151                         exit;
1152                      elsif abs R_Vec (J) > abs L_Vec (J) then
1153                         Y_Bigger := True;
1154                         exit;
1155                      end if;
1156                   end loop;
1157                end if;
1158
1159                --  If they have identical magnitude, just return 0, else swap
1160                --  if necessary so that X had the bigger magnitude. Determine
1161                --  if result is negative at this time.
1162
1163                Result_Neg := False;
1164
1165                if not (X_Bigger or Y_Bigger) then
1166                   return Uint_0;
1167
1168                elsif Y_Bigger then
1169                   if R_Vec (1) < Int_0 then
1170                      Result_Neg := True;
1171                   end if;
1172
1173                   Tmp_UI := X;
1174                   X := Y;
1175                   Y := Tmp_UI;
1176
1177                else
1178                   if L_Vec (1) < Int_0 then
1179                      Result_Neg := True;
1180                   end if;
1181                end if;
1182
1183                --  Subtract Y from the bigger X
1184
1185                Borrow := 0;
1186
1187                for J in reverse 1 .. Sum_Length loop
1188                   Tmp_Int := X (J) - Y (J) + Borrow;
1189
1190                   if Tmp_Int < Int_0 then
1191                      Tmp_Int := Tmp_Int + Base;
1192                      Borrow := -1;
1193                   else
1194                      Borrow := 0;
1195                   end if;
1196
1197                   X (J) := Tmp_Int;
1198                end loop;
1199
1200                return Vector_To_Uint (X, Result_Neg);
1201
1202             end if;
1203          end;
1204       end;
1205    end UI_Add;
1206
1207    --------------------------
1208    -- UI_Decimal_Digits_Hi --
1209    --------------------------
1210
1211    function UI_Decimal_Digits_Hi (U : Uint) return Nat is
1212    begin
1213       --  The maximum value of a "digit" is 32767, which is 5 decimal digits,
1214       --  so an N_Digit number could take up to 5 times this number of digits.
1215       --  This is certainly too high for large numbers but it is not worth
1216       --  worrying about.
1217
1218       return 5 * N_Digits (U);
1219    end UI_Decimal_Digits_Hi;
1220
1221    --------------------------
1222    -- UI_Decimal_Digits_Lo --
1223    --------------------------
1224
1225    function UI_Decimal_Digits_Lo (U : Uint) return Nat is
1226    begin
1227       --  The maximum value of a "digit" is 32767, which is more than four
1228       --  decimal digits, but not a full five digits. The easily computed
1229       --  minimum number of decimal digits is thus 1 + 4 * the number of
1230       --  digits. This is certainly too low for large numbers but it is not
1231       --  worth worrying about.
1232
1233       return 1 + 4 * (N_Digits (U) - 1);
1234    end UI_Decimal_Digits_Lo;
1235
1236    ------------
1237    -- UI_Div --
1238    ------------
1239
1240    function UI_Div (Left : Int; Right : Uint) return Uint is
1241    begin
1242       return UI_Div (UI_From_Int (Left), Right);
1243    end UI_Div;
1244
1245    function UI_Div (Left : Uint; Right : Int) return Uint is
1246    begin
1247       return UI_Div (Left, UI_From_Int (Right));
1248    end UI_Div;
1249
1250    function UI_Div (Left, Right : Uint) return Uint is
1251       Quotient  : Uint;
1252       Remainder : Uint;
1253       pragma Warnings (Off, Remainder);
1254    begin
1255       UI_Div_Rem
1256         (Left, Right,
1257          Quotient, Remainder,
1258          Discard_Quotient  => False,
1259          Discard_Remainder => True);
1260       return Quotient;
1261    end UI_Div;
1262
1263    ----------------
1264    -- UI_Div_Rem --
1265    ----------------
1266
1267    procedure UI_Div_Rem
1268      (Left, Right       : Uint;
1269       Quotient          : out Uint;
1270       Remainder         : out Uint;
1271       Discard_Quotient  : Boolean;
1272       Discard_Remainder : Boolean)
1273    is
1274       pragma Warnings (Off, Quotient);
1275       pragma Warnings (Off, Remainder);
1276    begin
1277       pragma Assert (Right /= Uint_0);
1278
1279       --  Cases where both operands are represented directly
1280
1281       if Direct (Left) and then Direct (Right) then
1282          declare
1283             DV_Left  : constant Int := Direct_Val (Left);
1284             DV_Right : constant Int := Direct_Val (Right);
1285
1286          begin
1287             if not Discard_Quotient then
1288                Quotient := UI_From_Int (DV_Left / DV_Right);
1289             end if;
1290
1291             if not Discard_Remainder then
1292                Remainder := UI_From_Int (DV_Left rem DV_Right);
1293             end if;
1294
1295             return;
1296          end;
1297       end if;
1298
1299       declare
1300          L_Length    : constant Int := N_Digits (Left);
1301          R_Length    : constant Int := N_Digits (Right);
1302          Q_Length    : constant Int := L_Length - R_Length + 1;
1303          L_Vec       : UI_Vector (1 .. L_Length);
1304          R_Vec       : UI_Vector (1 .. R_Length);
1305          D           : Int;
1306          Remainder_I : Int;
1307          Tmp_Divisor : Int;
1308          Carry       : Int;
1309          Tmp_Int     : Int;
1310          Tmp_Dig     : Int;
1311
1312          procedure UI_Div_Vector
1313            (L_Vec     : UI_Vector;
1314             R_Int     : Int;
1315             Quotient  : out UI_Vector;
1316             Remainder : out Int);
1317          pragma Inline (UI_Div_Vector);
1318          --  Specialised variant for case where the divisor is a single digit
1319
1320          procedure UI_Div_Vector
1321            (L_Vec     : UI_Vector;
1322             R_Int     : Int;
1323             Quotient  : out UI_Vector;
1324             Remainder : out Int)
1325          is
1326             Tmp_Int : Int;
1327
1328          begin
1329             Remainder := 0;
1330             for J in L_Vec'Range loop
1331                Tmp_Int := Remainder * Base + abs L_Vec (J);
1332                Quotient (Quotient'First + J - L_Vec'First) := Tmp_Int / R_Int;
1333                Remainder := Tmp_Int rem R_Int;
1334             end loop;
1335
1336             if L_Vec (L_Vec'First) < Int_0 then
1337                Remainder := -Remainder;
1338             end if;
1339          end UI_Div_Vector;
1340
1341       --  Start of processing for UI_Div_Rem
1342
1343       begin
1344          --  Result is zero if left operand is shorter than right
1345
1346          if L_Length < R_Length then
1347             if not Discard_Quotient then
1348                Quotient := Uint_0;
1349             end if;
1350             if not Discard_Remainder then
1351                Remainder := Left;
1352             end if;
1353             return;
1354          end if;
1355
1356          Init_Operand (Left, L_Vec);
1357          Init_Operand (Right, R_Vec);
1358
1359          --  Case of right operand is single digit. Here we can simply divide
1360          --  each digit of the left operand by the divisor, from most to least
1361          --  significant, carrying the remainder to the next digit (just like
1362          --  ordinary long division by hand).
1363
1364          if R_Length = Int_1 then
1365             Tmp_Divisor := abs R_Vec (1);
1366
1367             declare
1368                Quotient_V : UI_Vector (1 .. L_Length);
1369
1370             begin
1371                UI_Div_Vector (L_Vec, Tmp_Divisor, Quotient_V, Remainder_I);
1372
1373                if not Discard_Quotient then
1374                   Quotient :=
1375                     Vector_To_Uint
1376                       (Quotient_V, (L_Vec (1) < Int_0 xor R_Vec (1) < Int_0));
1377                end if;
1378
1379                if not Discard_Remainder then
1380                   Remainder := UI_From_Int (Remainder_I);
1381                end if;
1382                return;
1383             end;
1384          end if;
1385
1386          --  The possible simple cases have been exhausted. Now turn to the
1387          --  algorithm D from the section of Knuth mentioned at the top of
1388          --  this package.
1389
1390          Algorithm_D : declare
1391             Dividend     : UI_Vector (1 .. L_Length + 1);
1392             Divisor      : UI_Vector (1 .. R_Length);
1393             Quotient_V   : UI_Vector (1 .. Q_Length);
1394             Divisor_Dig1 : Int;
1395             Divisor_Dig2 : Int;
1396             Q_Guess      : Int;
1397
1398          begin
1399             --  [ NORMALIZE ] (step D1 in the algorithm). First calculate the
1400             --  scale d, and then multiply Left and Right (u and v in the book)
1401             --  by d to get the dividend and divisor to work with.
1402
1403             D := Base / (abs R_Vec (1) + 1);
1404
1405             Dividend (1) := 0;
1406             Dividend (2) := abs L_Vec (1);
1407
1408             for J in 3 .. L_Length + Int_1 loop
1409                Dividend (J) := L_Vec (J - 1);
1410             end loop;
1411
1412             Divisor (1) := abs R_Vec (1);
1413
1414             for J in Int_2 .. R_Length loop
1415                Divisor (J) := R_Vec (J);
1416             end loop;
1417
1418             if D > Int_1 then
1419
1420                --  Multiply Dividend by D
1421
1422                Carry := 0;
1423                for J in reverse Dividend'Range loop
1424                   Tmp_Int      := Dividend (J) * D + Carry;
1425                   Dividend (J) := Tmp_Int rem Base;
1426                   Carry        := Tmp_Int / Base;
1427                end loop;
1428
1429                --  Multiply Divisor by d
1430
1431                Carry := 0;
1432                for J in reverse Divisor'Range loop
1433                   Tmp_Int      := Divisor (J) * D + Carry;
1434                   Divisor (J)  := Tmp_Int rem Base;
1435                   Carry        := Tmp_Int / Base;
1436                end loop;
1437             end if;
1438
1439             --  Main loop of long division algorithm
1440
1441             Divisor_Dig1 := Divisor (1);
1442             Divisor_Dig2 := Divisor (2);
1443
1444             for J in Quotient_V'Range loop
1445
1446                --  [ CALCULATE Q (hat) ] (step D3 in the algorithm)
1447
1448                Tmp_Int := Dividend (J) * Base + Dividend (J + 1);
1449
1450                --  Initial guess
1451
1452                if Dividend (J) = Divisor_Dig1 then
1453                   Q_Guess := Base - 1;
1454                else
1455                   Q_Guess := Tmp_Int / Divisor_Dig1;
1456                end if;
1457
1458                --  Refine the guess
1459
1460                while Divisor_Dig2 * Q_Guess >
1461                      (Tmp_Int - Q_Guess * Divisor_Dig1) * Base +
1462                                                           Dividend (J + 2)
1463                loop
1464                   Q_Guess := Q_Guess - 1;
1465                end loop;
1466
1467                --  [ MULTIPLY & SUBTRACT ] (step D4). Q_Guess * Divisor is
1468                --  subtracted from the remaining dividend.
1469
1470                Carry := 0;
1471                for K in reverse Divisor'Range loop
1472                   Tmp_Int := Dividend (J + K) - Q_Guess * Divisor (K) + Carry;
1473                   Tmp_Dig := Tmp_Int rem Base;
1474                   Carry   := Tmp_Int / Base;
1475
1476                   if Tmp_Dig < Int_0 then
1477                      Tmp_Dig := Tmp_Dig + Base;
1478                      Carry   := Carry - 1;
1479                   end if;
1480
1481                   Dividend (J + K) := Tmp_Dig;
1482                end loop;
1483
1484                Dividend (J) := Dividend (J) + Carry;
1485
1486                --  [ TEST REMAINDER ] & [ ADD BACK ] (steps D5 and D6)
1487
1488                --  Here there is a slight difference from the book: the last
1489                --  carry is always added in above and below (cancelling each
1490                --  other). In fact the dividend going negative is used as
1491                --  the test.
1492
1493                --  If the Dividend went negative, then Q_Guess was off by
1494                --  one, so it is decremented, and the divisor is added back
1495                --  into the relevant portion of the dividend.
1496
1497                if Dividend (J) < Int_0 then
1498                   Q_Guess := Q_Guess - 1;
1499
1500                   Carry := 0;
1501                   for K in reverse Divisor'Range loop
1502                      Tmp_Int := Dividend (J + K) + Divisor (K) + Carry;
1503
1504                      if Tmp_Int >= Base then
1505                         Tmp_Int := Tmp_Int - Base;
1506                         Carry := 1;
1507                      else
1508                         Carry := 0;
1509                      end if;
1510
1511                      Dividend (J + K) := Tmp_Int;
1512                   end loop;
1513
1514                   Dividend (J) := Dividend (J) + Carry;
1515                end if;
1516
1517                --  Finally we can get the next quotient digit
1518
1519                Quotient_V (J) := Q_Guess;
1520             end loop;
1521
1522             --  [ UNNORMALIZE ] (step D8)
1523
1524             if not Discard_Quotient then
1525                Quotient := Vector_To_Uint
1526                  (Quotient_V, (L_Vec (1) < Int_0 xor R_Vec (1) < Int_0));
1527             end if;
1528
1529             if not Discard_Remainder then
1530                declare
1531                   Remainder_V : UI_Vector (1 .. R_Length);
1532                   Discard_Int : Int;
1533                   pragma Warnings (Off, Discard_Int);
1534                begin
1535                   UI_Div_Vector
1536                     (Dividend (Dividend'Last - R_Length + 1 .. Dividend'Last),
1537                      D,
1538                      Remainder_V, Discard_Int);
1539                   Remainder := Vector_To_Uint (Remainder_V, L_Vec (1) < Int_0);
1540                end;
1541             end if;
1542          end Algorithm_D;
1543       end;
1544    end UI_Div_Rem;
1545
1546    ------------
1547    -- UI_Eq --
1548    ------------
1549
1550    function UI_Eq (Left : Int; Right : Uint) return Boolean is
1551    begin
1552       return not UI_Ne (UI_From_Int (Left), Right);
1553    end UI_Eq;
1554
1555    function UI_Eq (Left : Uint; Right : Int) return Boolean is
1556    begin
1557       return not UI_Ne (Left, UI_From_Int (Right));
1558    end UI_Eq;
1559
1560    function UI_Eq (Left : Uint; Right : Uint) return Boolean is
1561    begin
1562       return not UI_Ne (Left, Right);
1563    end UI_Eq;
1564
1565    --------------
1566    -- UI_Expon --
1567    --------------
1568
1569    function UI_Expon (Left : Int; Right : Uint) return Uint is
1570    begin
1571       return UI_Expon (UI_From_Int (Left), Right);
1572    end UI_Expon;
1573
1574    function UI_Expon (Left : Uint; Right : Int) return Uint is
1575    begin
1576       return UI_Expon (Left, UI_From_Int (Right));
1577    end UI_Expon;
1578
1579    function UI_Expon (Left : Int; Right : Int) return Uint is
1580    begin
1581       return UI_Expon (UI_From_Int (Left), UI_From_Int (Right));
1582    end UI_Expon;
1583
1584    function UI_Expon (Left : Uint; Right : Uint) return Uint is
1585    begin
1586       pragma Assert (Right >= Uint_0);
1587
1588       --  Any value raised to power of 0 is 1
1589
1590       if Right = Uint_0 then
1591          return Uint_1;
1592
1593       --  0 to any positive power is 0
1594
1595       elsif Left = Uint_0 then
1596          return Uint_0;
1597
1598       --  1 to any power is 1
1599
1600       elsif Left = Uint_1 then
1601          return Uint_1;
1602
1603       --  Any value raised to power of 1 is that value
1604
1605       elsif Right = Uint_1 then
1606          return Left;
1607
1608       --  Cases which can be done by table lookup
1609
1610       elsif Right <= Uint_64 then
1611
1612          --  2 ** N for N in 2 .. 64
1613
1614          if Left = Uint_2 then
1615             declare
1616                Right_Int : constant Int := Direct_Val (Right);
1617
1618             begin
1619                if Right_Int > UI_Power_2_Set then
1620                   for J in UI_Power_2_Set + Int_1 .. Right_Int loop
1621                      UI_Power_2 (J) := UI_Power_2 (J - Int_1) * Int_2;
1622                      Uints_Min := Uints.Last;
1623                      Udigits_Min := Udigits.Last;
1624                   end loop;
1625
1626                   UI_Power_2_Set := Right_Int;
1627                end if;
1628
1629                return UI_Power_2 (Right_Int);
1630             end;
1631
1632          --  10 ** N for N in 2 .. 64
1633
1634          elsif Left = Uint_10 then
1635             declare
1636                Right_Int : constant Int := Direct_Val (Right);
1637
1638             begin
1639                if Right_Int > UI_Power_10_Set then
1640                   for J in UI_Power_10_Set + Int_1 .. Right_Int loop
1641                      UI_Power_10 (J) := UI_Power_10 (J - Int_1) * Int (10);
1642                      Uints_Min := Uints.Last;
1643                      Udigits_Min := Udigits.Last;
1644                   end loop;
1645
1646                   UI_Power_10_Set := Right_Int;
1647                end if;
1648
1649                return UI_Power_10 (Right_Int);
1650             end;
1651          end if;
1652       end if;
1653
1654       --  If we fall through, then we have the general case (see Knuth 4.6.3)
1655
1656       declare
1657          N       : Uint := Right;
1658          Squares : Uint := Left;
1659          Result  : Uint := Uint_1;
1660          M       : constant Uintp.Save_Mark := Uintp.Mark;
1661
1662       begin
1663          loop
1664             if (Least_Sig_Digit (N) mod Int_2) = Int_1 then
1665                Result := Result * Squares;
1666             end if;
1667
1668             N := N / Uint_2;
1669             exit when N = Uint_0;
1670             Squares := Squares *  Squares;
1671          end loop;
1672
1673          Uintp.Release_And_Save (M, Result);
1674          return Result;
1675       end;
1676    end UI_Expon;
1677
1678    ----------------
1679    -- UI_From_CC --
1680    ----------------
1681
1682    function UI_From_CC (Input : Char_Code) return Uint is
1683    begin
1684       return UI_From_Dint (Dint (Input));
1685    end UI_From_CC;
1686
1687    ------------------
1688    -- UI_From_Dint --
1689    ------------------
1690
1691    function UI_From_Dint (Input : Dint) return Uint is
1692    begin
1693
1694       if Dint (Min_Direct) <= Input and then Input <= Dint (Max_Direct) then
1695          return Uint (Dint (Uint_Direct_Bias) + Input);
1696
1697       --  For values of larger magnitude, compute digits into a vector and call
1698       --  Vector_To_Uint.
1699
1700       else
1701          declare
1702             Max_For_Dint : constant := 5;
1703             --  Base is defined so that 5 Uint digits is sufficient to hold the
1704             --  largest possible Dint value.
1705
1706             V : UI_Vector (1 .. Max_For_Dint);
1707
1708             Temp_Integer : Dint;
1709
1710          begin
1711             for J in V'Range loop
1712                V (J) := 0;
1713             end loop;
1714
1715             Temp_Integer := Input;
1716
1717             for J in reverse V'Range loop
1718                V (J) := Int (abs (Temp_Integer rem Dint (Base)));
1719                Temp_Integer := Temp_Integer / Dint (Base);
1720             end loop;
1721
1722             return Vector_To_Uint (V, Input < Dint'(0));
1723          end;
1724       end if;
1725    end UI_From_Dint;
1726
1727    -----------------
1728    -- UI_From_Int --
1729    -----------------
1730
1731    function UI_From_Int (Input : Int) return Uint is
1732       U : Uint;
1733
1734    begin
1735       if Min_Direct <= Input and then Input <= Max_Direct then
1736          return Uint (Int (Uint_Direct_Bias) + Input);
1737       end if;
1738
1739       --  If already in the hash table, return entry
1740
1741       U := UI_Ints.Get (Input);
1742
1743       if U /= No_Uint then
1744          return U;
1745       end if;
1746
1747       --  For values of larger magnitude, compute digits into a vector and call
1748       --  Vector_To_Uint.
1749
1750       declare
1751          Max_For_Int : constant := 3;
1752          --  Base is defined so that 3 Uint digits is sufficient to hold the
1753          --  largest possible Int value.
1754
1755          V : UI_Vector (1 .. Max_For_Int);
1756
1757          Temp_Integer : Int;
1758
1759       begin
1760          for J in V'Range loop
1761             V (J) := 0;
1762          end loop;
1763
1764          Temp_Integer := Input;
1765
1766          for J in reverse V'Range loop
1767             V (J) := abs (Temp_Integer rem Base);
1768             Temp_Integer := Temp_Integer / Base;
1769          end loop;
1770
1771          U := Vector_To_Uint (V, Input < Int_0);
1772          UI_Ints.Set (Input, U);
1773          Uints_Min := Uints.Last;
1774          Udigits_Min := Udigits.Last;
1775          return U;
1776       end;
1777    end UI_From_Int;
1778
1779    ------------
1780    -- UI_GCD --
1781    ------------
1782
1783    --  Lehmer's algorithm for GCD
1784
1785    --  The idea is to avoid using multiple precision arithmetic wherever
1786    --  possible, substituting Int arithmetic instead. See Knuth volume II,
1787    --  Algorithm L (page 329).
1788
1789    --  We use the same notation as Knuth (U_Hat standing for the obvious!)
1790
1791    function UI_GCD (Uin, Vin : Uint) return Uint is
1792       U, V : Uint;
1793       --  Copies of Uin and Vin
1794
1795       U_Hat, V_Hat : Int;
1796       --  The most Significant digits of U,V
1797
1798       A, B, C, D, T, Q, Den1, Den2 : Int;
1799
1800       Tmp_UI : Uint;
1801       Marks  : constant Uintp.Save_Mark := Uintp.Mark;
1802       Iterations : Integer := 0;
1803
1804    begin
1805       pragma Assert (Uin >= Vin);
1806       pragma Assert (Vin >= Uint_0);
1807
1808       U := Uin;
1809       V := Vin;
1810
1811       loop
1812          Iterations := Iterations + 1;
1813
1814          if Direct (V) then
1815             if V = Uint_0 then
1816                return U;
1817             else
1818                return
1819                  UI_From_Int (GCD (Direct_Val (V), UI_To_Int (U rem V)));
1820             end if;
1821          end if;
1822
1823          Most_Sig_2_Digits (U, V, U_Hat, V_Hat);
1824          A := 1;
1825          B := 0;
1826          C := 0;
1827          D := 1;
1828
1829          loop
1830             --  We might overflow and get division by zero here. This just
1831             --  means we cannot take the single precision step
1832
1833             Den1 := V_Hat + C;
1834             Den2 := V_Hat + D;
1835             exit when (Den1 * Den2) = Int_0;
1836
1837             --  Compute Q, the trial quotient
1838
1839             Q := (U_Hat + A) / Den1;
1840
1841             exit when Q /= ((U_Hat + B) / Den2);
1842
1843             --  A single precision step Euclid step will give same answer as a
1844             --  multiprecision one.
1845
1846             T := A - (Q * C);
1847             A := C;
1848             C := T;
1849
1850             T := B - (Q * D);
1851             B := D;
1852             D := T;
1853
1854             T := U_Hat - (Q * V_Hat);
1855             U_Hat := V_Hat;
1856             V_Hat := T;
1857
1858          end loop;
1859
1860          --  Take a multiprecision Euclid step
1861
1862          if B = Int_0 then
1863
1864             --  No single precision steps take a regular Euclid step
1865
1866             Tmp_UI := U rem V;
1867             U := V;
1868             V := Tmp_UI;
1869
1870          else
1871             --  Use prior single precision steps to compute this Euclid step
1872
1873             --  For constructs such as:
1874             --  sqrt_2: constant :=  1.41421_35623_73095_04880_16887_24209_698;
1875             --  sqrt_eps: constant long_float := long_float( 1.0 / sqrt_2)
1876             --    ** long_float'machine_mantissa;
1877             --
1878             --  we spend 80% of our time working on this step. Perhaps we need
1879             --  a special case Int / Uint dot product to speed things up. ???
1880
1881             --  Alternatively we could increase the single precision iterations
1882             --  to handle Uint's of some small size ( <5 digits?). Then we
1883             --  would have more iterations on small Uint. On the code above, we
1884             --  only get 5 (on average) single precision iterations per large
1885             --  iteration. ???
1886
1887             Tmp_UI := (UI_From_Int (A) * U) + (UI_From_Int (B) * V);
1888             V := (UI_From_Int (C) * U) + (UI_From_Int (D) * V);
1889             U := Tmp_UI;
1890          end if;
1891
1892          --  If the operands are very different in magnitude, the loop will
1893          --  generate large amounts of short-lived data, which it is worth
1894          --  removing periodically.
1895
1896          if Iterations > 100 then
1897             Release_And_Save (Marks, U, V);
1898             Iterations := 0;
1899          end if;
1900       end loop;
1901    end UI_GCD;
1902
1903    ------------
1904    -- UI_Ge --
1905    ------------
1906
1907    function UI_Ge (Left : Int; Right : Uint) return Boolean is
1908    begin
1909       return not UI_Lt (UI_From_Int (Left), Right);
1910    end UI_Ge;
1911
1912    function UI_Ge (Left : Uint; Right : Int) return Boolean is
1913    begin
1914       return not UI_Lt (Left, UI_From_Int (Right));
1915    end UI_Ge;
1916
1917    function UI_Ge (Left : Uint; Right : Uint) return Boolean is
1918    begin
1919       return not UI_Lt (Left, Right);
1920    end UI_Ge;
1921
1922    ------------
1923    -- UI_Gt --
1924    ------------
1925
1926    function UI_Gt (Left : Int; Right : Uint) return Boolean is
1927    begin
1928       return UI_Lt (Right, UI_From_Int (Left));
1929    end UI_Gt;
1930
1931    function UI_Gt (Left : Uint; Right : Int) return Boolean is
1932    begin
1933       return UI_Lt (UI_From_Int (Right), Left);
1934    end UI_Gt;
1935
1936    function UI_Gt (Left : Uint; Right : Uint) return Boolean is
1937    begin
1938       return UI_Lt (Left => Right, Right => Left);
1939    end UI_Gt;
1940
1941    ---------------
1942    -- UI_Image --
1943    ---------------
1944
1945    procedure UI_Image (Input : Uint; Format : UI_Format := Auto) is
1946    begin
1947       Image_Out (Input, True, Format);
1948    end UI_Image;
1949
1950    -------------------------
1951    -- UI_Is_In_Int_Range --
1952    -------------------------
1953
1954    function UI_Is_In_Int_Range (Input : Uint) return Boolean is
1955    begin
1956       --  Make sure we don't get called before Initialize
1957
1958       pragma Assert (Uint_Int_First /= Uint_0);
1959
1960       if Direct (Input) then
1961          return True;
1962       else
1963          return Input >= Uint_Int_First
1964            and then Input <= Uint_Int_Last;
1965       end if;
1966    end UI_Is_In_Int_Range;
1967
1968    ------------
1969    -- UI_Le --
1970    ------------
1971
1972    function UI_Le (Left : Int; Right : Uint) return Boolean is
1973    begin
1974       return not UI_Lt (Right, UI_From_Int (Left));
1975    end UI_Le;
1976
1977    function UI_Le (Left : Uint; Right : Int) return Boolean is
1978    begin
1979       return not UI_Lt (UI_From_Int (Right), Left);
1980    end UI_Le;
1981
1982    function UI_Le (Left : Uint; Right : Uint) return Boolean is
1983    begin
1984       return not UI_Lt (Left => Right, Right => Left);
1985    end UI_Le;
1986
1987    ------------
1988    -- UI_Lt --
1989    ------------
1990
1991    function UI_Lt (Left : Int; Right : Uint) return Boolean is
1992    begin
1993       return UI_Lt (UI_From_Int (Left), Right);
1994    end UI_Lt;
1995
1996    function UI_Lt (Left : Uint; Right : Int) return Boolean is
1997    begin
1998       return UI_Lt (Left, UI_From_Int (Right));
1999    end UI_Lt;
2000
2001    function UI_Lt (Left : Uint; Right : Uint) return Boolean is
2002    begin
2003       --  Quick processing for identical arguments
2004
2005       if Int (Left) = Int (Right) then
2006          return False;
2007
2008       --  Quick processing for both arguments directly represented
2009
2010       elsif Direct (Left) and then Direct (Right) then
2011          return Int (Left) < Int (Right);
2012
2013       --  At least one argument is more than one digit long
2014
2015       else
2016          declare
2017             L_Length : constant Int := N_Digits (Left);
2018             R_Length : constant Int := N_Digits (Right);
2019
2020             L_Vec : UI_Vector (1 .. L_Length);
2021             R_Vec : UI_Vector (1 .. R_Length);
2022
2023          begin
2024             Init_Operand (Left, L_Vec);
2025             Init_Operand (Right, R_Vec);
2026
2027             if L_Vec (1) < Int_0 then
2028
2029                --  First argument negative, second argument non-negative
2030
2031                if R_Vec (1) >= Int_0 then
2032                   return True;
2033
2034                --  Both arguments negative
2035
2036                else
2037                   if L_Length /= R_Length then
2038                      return L_Length > R_Length;
2039
2040                   elsif L_Vec (1) /= R_Vec (1) then
2041                      return L_Vec (1) < R_Vec (1);
2042
2043                   else
2044                      for J in 2 .. L_Vec'Last loop
2045                         if L_Vec (J) /= R_Vec (J) then
2046                            return L_Vec (J) > R_Vec (J);
2047                         end if;
2048                      end loop;
2049
2050                      return False;
2051                   end if;
2052                end if;
2053
2054             else
2055                --  First argument non-negative, second argument negative
2056
2057                if R_Vec (1) < Int_0 then
2058                   return False;
2059
2060                --  Both arguments non-negative
2061
2062                else
2063                   if L_Length /= R_Length then
2064                      return L_Length < R_Length;
2065                   else
2066                      for J in L_Vec'Range loop
2067                         if L_Vec (J) /= R_Vec (J) then
2068                            return L_Vec (J) < R_Vec (J);
2069                         end if;
2070                      end loop;
2071
2072                      return False;
2073                   end if;
2074                end if;
2075             end if;
2076          end;
2077       end if;
2078    end UI_Lt;
2079
2080    ------------
2081    -- UI_Max --
2082    ------------
2083
2084    function UI_Max (Left : Int; Right : Uint) return Uint is
2085    begin
2086       return UI_Max (UI_From_Int (Left), Right);
2087    end UI_Max;
2088
2089    function UI_Max (Left : Uint; Right : Int) return Uint is
2090    begin
2091       return UI_Max (Left, UI_From_Int (Right));
2092    end UI_Max;
2093
2094    function UI_Max (Left : Uint; Right : Uint) return Uint is
2095    begin
2096       if Left >= Right then
2097          return Left;
2098       else
2099          return Right;
2100       end if;
2101    end UI_Max;
2102
2103    ------------
2104    -- UI_Min --
2105    ------------
2106
2107    function UI_Min (Left : Int; Right : Uint) return Uint is
2108    begin
2109       return UI_Min (UI_From_Int (Left), Right);
2110    end UI_Min;
2111
2112    function UI_Min (Left : Uint; Right : Int) return Uint is
2113    begin
2114       return UI_Min (Left, UI_From_Int (Right));
2115    end UI_Min;
2116
2117    function UI_Min (Left : Uint; Right : Uint) return Uint is
2118    begin
2119       if Left <= Right then
2120          return Left;
2121       else
2122          return Right;
2123       end if;
2124    end UI_Min;
2125
2126    -------------
2127    -- UI_Mod --
2128    -------------
2129
2130    function UI_Mod (Left : Int; Right : Uint) return Uint is
2131    begin
2132       return UI_Mod (UI_From_Int (Left), Right);
2133    end UI_Mod;
2134
2135    function UI_Mod (Left : Uint; Right : Int) return Uint is
2136    begin
2137       return UI_Mod (Left, UI_From_Int (Right));
2138    end UI_Mod;
2139
2140    function UI_Mod (Left : Uint; Right : Uint) return Uint is
2141       Urem : constant Uint := Left rem Right;
2142
2143    begin
2144       if (Left < Uint_0) = (Right < Uint_0)
2145         or else Urem = Uint_0
2146       then
2147          return Urem;
2148       else
2149          return Right + Urem;
2150       end if;
2151    end UI_Mod;
2152
2153    -------------------------------
2154    -- UI_Modular_Exponentiation --
2155    -------------------------------
2156
2157    function UI_Modular_Exponentiation
2158      (B      : Uint;
2159       E      : Uint;
2160       Modulo : Uint) return Uint
2161    is
2162       M : constant Save_Mark := Mark;
2163
2164       Result   : Uint := Uint_1;
2165       Base     : Uint := B;
2166       Exponent : Uint := E;
2167
2168    begin
2169       while Exponent /= Uint_0 loop
2170          if Least_Sig_Digit (Exponent) rem Int'(2) = Int'(1) then
2171             Result := (Result * Base) rem Modulo;
2172          end if;
2173
2174          Exponent := Exponent / Uint_2;
2175          Base := (Base * Base) rem Modulo;
2176       end loop;
2177
2178       Release_And_Save (M, Result);
2179       return Result;
2180    end UI_Modular_Exponentiation;
2181
2182    ------------------------
2183    -- UI_Modular_Inverse --
2184    ------------------------
2185
2186    function UI_Modular_Inverse (N : Uint; Modulo : Uint) return Uint is
2187       M : constant Save_Mark := Mark;
2188       U : Uint;
2189       V : Uint;
2190       Q : Uint;
2191       R : Uint;
2192       X : Uint;
2193       Y : Uint;
2194       T : Uint;
2195       S : Int := 1;
2196
2197    begin
2198       U := Modulo;
2199       V := N;
2200
2201       X := Uint_1;
2202       Y := Uint_0;
2203
2204       loop
2205          UI_Div_Rem
2206            (U, V,
2207             Quotient => Q, Remainder => R,
2208             Discard_Quotient  => False,
2209             Discard_Remainder => False);
2210
2211          U := V;
2212          V := R;
2213
2214          T := X;
2215          X := Y + Q * X;
2216          Y := T;
2217          S := -S;
2218
2219          exit when R = Uint_1;
2220       end loop;
2221
2222       if S = Int'(-1) then
2223          X := Modulo - X;
2224       end if;
2225
2226       Release_And_Save (M, X);
2227       return X;
2228    end UI_Modular_Inverse;
2229
2230    ------------
2231    -- UI_Mul --
2232    ------------
2233
2234    function UI_Mul (Left : Int; Right : Uint) return Uint is
2235    begin
2236       return UI_Mul (UI_From_Int (Left), Right);
2237    end UI_Mul;
2238
2239    function UI_Mul (Left : Uint; Right : Int) return Uint is
2240    begin
2241       return UI_Mul (Left, UI_From_Int (Right));
2242    end UI_Mul;
2243
2244    function UI_Mul (Left : Uint; Right : Uint) return Uint is
2245    begin
2246       --  Simple case of single length operands
2247
2248       if Direct (Left) and then Direct (Right) then
2249          return
2250            UI_From_Dint
2251              (Dint (Direct_Val (Left)) * Dint (Direct_Val (Right)));
2252       end if;
2253
2254       --  Otherwise we have the general case (Algorithm M in Knuth)
2255
2256       declare
2257          L_Length : constant Int := N_Digits (Left);
2258          R_Length : constant Int := N_Digits (Right);
2259          L_Vec    : UI_Vector (1 .. L_Length);
2260          R_Vec    : UI_Vector (1 .. R_Length);
2261          Neg      : Boolean;
2262
2263       begin
2264          Init_Operand (Left, L_Vec);
2265          Init_Operand (Right, R_Vec);
2266          Neg := (L_Vec (1) < Int_0) xor (R_Vec (1) < Int_0);
2267          L_Vec (1) := abs (L_Vec (1));
2268          R_Vec (1) := abs (R_Vec (1));
2269
2270          Algorithm_M : declare
2271             Product : UI_Vector (1 .. L_Length + R_Length);
2272             Tmp_Sum : Int;
2273             Carry   : Int;
2274
2275          begin
2276             for J in Product'Range loop
2277                Product (J) := 0;
2278             end loop;
2279
2280             for J in reverse R_Vec'Range loop
2281                Carry := 0;
2282                for K in reverse L_Vec'Range loop
2283                   Tmp_Sum :=
2284                     L_Vec (K) * R_Vec (J) + Product (J + K) + Carry;
2285                   Product (J + K) := Tmp_Sum rem Base;
2286                   Carry := Tmp_Sum / Base;
2287                end loop;
2288
2289                Product (J) := Carry;
2290             end loop;
2291
2292             return Vector_To_Uint (Product, Neg);
2293          end Algorithm_M;
2294       end;
2295    end UI_Mul;
2296
2297    ------------
2298    -- UI_Ne --
2299    ------------
2300
2301    function UI_Ne (Left : Int; Right : Uint) return Boolean is
2302    begin
2303       return UI_Ne (UI_From_Int (Left), Right);
2304    end UI_Ne;
2305
2306    function UI_Ne (Left : Uint; Right : Int) return Boolean is
2307    begin
2308       return UI_Ne (Left, UI_From_Int (Right));
2309    end UI_Ne;
2310
2311    function UI_Ne (Left : Uint; Right : Uint) return Boolean is
2312    begin
2313       --  Quick processing for identical arguments. Note that this takes
2314       --  care of the case of two No_Uint arguments.
2315
2316       if Int (Left) = Int (Right) then
2317          return False;
2318       end if;
2319
2320       --  See if left operand directly represented
2321
2322       if Direct (Left) then
2323
2324          --  If right operand directly represented then compare
2325
2326          if Direct (Right) then
2327             return Int (Left) /= Int (Right);
2328
2329          --  Left operand directly represented, right not, must be unequal
2330
2331          else
2332             return True;
2333          end if;
2334
2335       --  Right operand directly represented, left not, must be unequal
2336
2337       elsif Direct (Right) then
2338          return True;
2339       end if;
2340
2341       --  Otherwise both multi-word, do comparison
2342
2343       declare
2344          Size      : constant Int := N_Digits (Left);
2345          Left_Loc  : Int;
2346          Right_Loc : Int;
2347
2348       begin
2349          if Size /= N_Digits (Right) then
2350             return True;
2351          end if;
2352
2353          Left_Loc  := Uints.Table (Left).Loc;
2354          Right_Loc := Uints.Table (Right).Loc;
2355
2356          for J in Int_0 .. Size - Int_1 loop
2357             if Udigits.Table (Left_Loc + J) /=
2358                Udigits.Table (Right_Loc + J)
2359             then
2360                return True;
2361             end if;
2362          end loop;
2363
2364          return False;
2365       end;
2366    end UI_Ne;
2367
2368    ----------------
2369    -- UI_Negate --
2370    ----------------
2371
2372    function UI_Negate (Right : Uint) return Uint is
2373    begin
2374       --  Case where input is directly represented. Note that since the range
2375       --  of Direct values is non-symmetrical, the result may not be directly
2376       --  represented, this is taken care of in UI_From_Int.
2377
2378       if Direct (Right) then
2379          return UI_From_Int (-Direct_Val (Right));
2380
2381       --  Full processing for multi-digit case. Note that we cannot just copy
2382       --  the value to the end of the table negating the first digit, since the
2383       --  range of Direct values is non-symmetrical, so we can have a negative
2384       --  value that is not Direct whose negation can be represented directly.
2385
2386       else
2387          declare
2388             R_Length : constant Int := N_Digits (Right);
2389             R_Vec    : UI_Vector (1 .. R_Length);
2390             Neg      : Boolean;
2391
2392          begin
2393             Init_Operand (Right, R_Vec);
2394             Neg := R_Vec (1) > Int_0;
2395             R_Vec (1) := abs R_Vec (1);
2396             return Vector_To_Uint (R_Vec, Neg);
2397          end;
2398       end if;
2399    end UI_Negate;
2400
2401    -------------
2402    -- UI_Rem --
2403    -------------
2404
2405    function UI_Rem (Left : Int; Right : Uint) return Uint is
2406    begin
2407       return UI_Rem (UI_From_Int (Left), Right);
2408    end UI_Rem;
2409
2410    function UI_Rem (Left : Uint; Right : Int) return Uint is
2411    begin
2412       return UI_Rem (Left, UI_From_Int (Right));
2413    end UI_Rem;
2414
2415    function UI_Rem (Left, Right : Uint) return Uint is
2416       Sign : Int;
2417       Tmp  : Int;
2418
2419       subtype Int1_12 is Integer range 1 .. 12;
2420
2421    begin
2422       pragma Assert (Right /= Uint_0);
2423
2424       if Direct (Right) then
2425          if Direct (Left) then
2426             return UI_From_Int (Direct_Val (Left) rem Direct_Val (Right));
2427
2428          else
2429
2430             --  Special cases when Right is less than 13 and Left is larger
2431             --  larger than one digit. All of these algorithms depend on the
2432             --  base being 2 ** 15 We work with Abs (Left) and Abs(Right)
2433             --  then multiply result by Sign (Left)
2434
2435             if (Right <= Uint_12) and then (Right >= Uint_Minus_12) then
2436
2437                if Left < Uint_0 then
2438                   Sign := -1;
2439                else
2440                   Sign := 1;
2441                end if;
2442
2443                --  All cases are listed, grouped by mathematical method It is
2444                --  not inefficient to do have this case list out of order since
2445                --  GCC sorts the cases we list.
2446
2447                case Int1_12 (abs (Direct_Val (Right))) is
2448
2449                   when 1 =>
2450                      return Uint_0;
2451
2452                   --  Powers of two are simple AND's with LS Left Digit GCC
2453                   --  will recognise these constants as powers of 2 and replace
2454                   --  the rem with simpler operations where possible.
2455
2456                   --  Least_Sig_Digit might return Negative numbers
2457
2458                   when 2 =>
2459                      return UI_From_Int (
2460                         Sign * (Least_Sig_Digit (Left) mod 2));
2461
2462                   when 4 =>
2463                      return UI_From_Int (
2464                         Sign * (Least_Sig_Digit (Left) mod 4));
2465
2466                   when 8 =>
2467                      return UI_From_Int (
2468                         Sign * (Least_Sig_Digit (Left) mod 8));
2469
2470                   --  Some number theoretical tricks:
2471
2472                   --    If B Rem Right = 1 then
2473                   --    Left Rem Right = Sum_Of_Digits_Base_B (Left) Rem Right
2474
2475                   --  Note: 2^32 mod 3 = 1
2476
2477                   when 3 =>
2478                      return UI_From_Int (
2479                         Sign * (Sum_Double_Digits (Left, 1) rem Int (3)));
2480
2481                   --  Note: 2^15 mod 7 = 1
2482
2483                   when 7 =>
2484                      return UI_From_Int (
2485                         Sign * (Sum_Digits (Left, 1) rem Int (7)));
2486
2487                   --  Note: 2^32 mod 5 = -1
2488
2489                   --  Alternating sums might be negative, but rem is always
2490                   --  positive hence we must use mod here.
2491
2492                   when 5 =>
2493                      Tmp := Sum_Double_Digits (Left, -1) mod Int (5);
2494                      return UI_From_Int (Sign * Tmp);
2495
2496                   --  Note: 2^15 mod 9 = -1
2497
2498                   --  Alternating sums might be negative, but rem is always
2499                   --  positive hence we must use mod here.
2500
2501                   when 9  =>
2502                      Tmp := Sum_Digits (Left, -1) mod Int (9);
2503                      return UI_From_Int (Sign * Tmp);
2504
2505                   --  Note: 2^15 mod 11 = -1
2506
2507                   --  Alternating sums might be negative, but rem is always
2508                   --  positive hence we must use mod here.
2509
2510                   when 11 =>
2511                      Tmp := Sum_Digits (Left, -1) mod Int (11);
2512                      return UI_From_Int (Sign * Tmp);
2513
2514                   --  Now resort to Chinese Remainder theorem to reduce 6, 10,
2515                   --  12 to previous special cases
2516
2517                   --  There is no reason we could not add more cases like these
2518                   --  if it proves useful.
2519
2520                   --  Perhaps we should go up to 16, however we have no "trick"
2521                   --  for 13.
2522
2523                   --  To find u mod m we:
2524
2525                   --  Pick m1, m2 S.T.
2526                   --     GCD(m1, m2) = 1 AND m = (m1 * m2).
2527
2528                   --  Next we pick (Basis) M1, M2 small S.T.
2529                   --     (M1 mod m1) = (M2 mod m2) = 1 AND
2530                   --     (M1 mod m2) = (M2 mod m1) = 0
2531
2532                   --  So u mod m = (u1 * M1 + u2 * M2) mod m Where u1 = (u mod
2533                   --  m1) AND u2 = (u mod m2); Under typical circumstances the
2534                   --  last mod m can be done with a (possible) single
2535                   --  subtraction.
2536
2537                   --  m1 = 2; m2 = 3; M1 = 3; M2 = 4;
2538
2539                   when 6  =>
2540                      Tmp := 3 * (Least_Sig_Digit (Left) rem 2) +
2541                               4 * (Sum_Double_Digits (Left, 1) rem 3);
2542                      return UI_From_Int (Sign * (Tmp rem 6));
2543
2544                   --  m1 = 2; m2 = 5; M1 = 5; M2 = 6;
2545
2546                   when 10 =>
2547                      Tmp := 5 * (Least_Sig_Digit (Left) rem 2) +
2548                               6 * (Sum_Double_Digits (Left, -1) mod 5);
2549                      return UI_From_Int (Sign * (Tmp rem 10));
2550
2551                   --  m1 = 3; m2 = 4; M1 = 4; M2 = 9;
2552
2553                   when 12 =>
2554                      Tmp := 4 * (Sum_Double_Digits (Left, 1) rem 3) +
2555                               9 * (Least_Sig_Digit (Left) rem 4);
2556                      return UI_From_Int (Sign * (Tmp rem 12));
2557                end case;
2558
2559             end if;
2560
2561             --  Else fall through to general case
2562
2563             --  The special case Length (Left) = Length (Right) = 1 in Div
2564             --  looks slow. It uses UI_To_Int when Int should suffice. ???
2565          end if;
2566       end if;
2567
2568       declare
2569          Remainder : Uint;
2570          Quotient  : Uint;
2571          pragma Warnings (Off, Quotient);
2572       begin
2573          UI_Div_Rem
2574            (Left, Right, Quotient, Remainder,
2575             Discard_Quotient  => True,
2576             Discard_Remainder => False);
2577          return Remainder;
2578       end;
2579    end UI_Rem;
2580
2581    ------------
2582    -- UI_Sub --
2583    ------------
2584
2585    function UI_Sub (Left : Int; Right : Uint) return Uint is
2586    begin
2587       return UI_Add (Left, -Right);
2588    end UI_Sub;
2589
2590    function UI_Sub (Left : Uint; Right : Int) return Uint is
2591    begin
2592       return UI_Add (Left, -Right);
2593    end UI_Sub;
2594
2595    function UI_Sub (Left : Uint; Right : Uint) return Uint is
2596    begin
2597       if Direct (Left) and then Direct (Right) then
2598          return UI_From_Int (Direct_Val (Left) - Direct_Val (Right));
2599       else
2600          return UI_Add (Left, -Right);
2601       end if;
2602    end UI_Sub;
2603
2604    --------------
2605    -- UI_To_CC --
2606    --------------
2607
2608    function UI_To_CC (Input : Uint) return Char_Code is
2609    begin
2610       if Direct (Input) then
2611          return Char_Code (Direct_Val (Input));
2612
2613       --  Case of input is more than one digit
2614
2615       else
2616          declare
2617             In_Length : constant Int := N_Digits (Input);
2618             In_Vec    : UI_Vector (1 .. In_Length);
2619             Ret_CC    : Char_Code;
2620
2621          begin
2622             Init_Operand (Input, In_Vec);
2623
2624             --  We assume value is positive
2625
2626             Ret_CC := 0;
2627             for Idx in In_Vec'Range loop
2628                Ret_CC := Ret_CC * Char_Code (Base) +
2629                                   Char_Code (abs In_Vec (Idx));
2630             end loop;
2631
2632             return Ret_CC;
2633          end;
2634       end if;
2635    end UI_To_CC;
2636
2637    ----------------
2638    -- UI_To_Int --
2639    ----------------
2640
2641    function UI_To_Int (Input : Uint) return Int is
2642    begin
2643       if Direct (Input) then
2644          return Direct_Val (Input);
2645
2646       --  Case of input is more than one digit
2647
2648       else
2649          declare
2650             In_Length : constant Int := N_Digits (Input);
2651             In_Vec    : UI_Vector (1 .. In_Length);
2652             Ret_Int   : Int;
2653
2654          begin
2655             --  Uints of more than one digit could be outside the range for
2656             --  Ints. Caller should have checked for this if not certain.
2657             --  Fatal error to attempt to convert from value outside Int'Range.
2658
2659             pragma Assert (UI_Is_In_Int_Range (Input));
2660
2661             --  Otherwise, proceed ahead, we are OK
2662
2663             Init_Operand (Input, In_Vec);
2664             Ret_Int := 0;
2665
2666             --  Calculate -|Input| and then negates if value is positive. This
2667             --  handles our current definition of Int (based on 2s complement).
2668             --  Is it secure enough???
2669
2670             for Idx in In_Vec'Range loop
2671                Ret_Int := Ret_Int * Base - abs In_Vec (Idx);
2672             end loop;
2673
2674             if In_Vec (1) < Int_0 then
2675                return Ret_Int;
2676             else
2677                return -Ret_Int;
2678             end if;
2679          end;
2680       end if;
2681    end UI_To_Int;
2682
2683    --------------
2684    -- UI_Write --
2685    --------------
2686
2687    procedure UI_Write (Input : Uint; Format : UI_Format := Auto) is
2688    begin
2689       Image_Out (Input, False, Format);
2690    end UI_Write;
2691
2692    ---------------------
2693    -- Vector_To_Uint --
2694    ---------------------
2695
2696    function Vector_To_Uint
2697      (In_Vec   : UI_Vector;
2698       Negative : Boolean)
2699       return     Uint
2700    is
2701       Size : Int;
2702       Val  : Int;
2703
2704    begin
2705       --  The vector can contain leading zeros. These are not stored in the
2706       --  table, so loop through the vector looking for first non-zero digit
2707
2708       for J in In_Vec'Range loop
2709          if In_Vec (J) /= Int_0 then
2710
2711             --  The length of the value is the length of the rest of the vector
2712
2713             Size := In_Vec'Last - J + 1;
2714
2715             --  One digit value can always be represented directly
2716
2717             if Size = Int_1 then
2718                if Negative then
2719                   return Uint (Int (Uint_Direct_Bias) - In_Vec (J));
2720                else
2721                   return Uint (Int (Uint_Direct_Bias) + In_Vec (J));
2722                end if;
2723
2724             --  Positive two digit values may be in direct representation range
2725
2726             elsif Size = Int_2 and then not Negative then
2727                Val := In_Vec (J) * Base + In_Vec (J + 1);
2728
2729                if Val <= Max_Direct then
2730                   return Uint (Int (Uint_Direct_Bias) + Val);
2731                end if;
2732             end if;
2733
2734             --  The value is outside the direct representation range and must
2735             --  therefore be stored in the table. Expand the table to contain
2736             --  the count and digits. The index of the new table entry will be
2737             --  returned as the result.
2738
2739             Uints.Append ((Length => Size, Loc => Udigits.Last + 1));
2740
2741             if Negative then
2742                Val := -In_Vec (J);
2743             else
2744                Val := +In_Vec (J);
2745             end if;
2746
2747             Udigits.Append (Val);
2748
2749             for K in 2 .. Size loop
2750                Udigits.Append (In_Vec (J + K - 1));
2751             end loop;
2752
2753             return Uints.Last;
2754          end if;
2755       end loop;
2756
2757       --  Dropped through loop only if vector contained all zeros
2758
2759       return Uint_0;
2760    end Vector_To_Uint;
2761
2762 end Uintp;