1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2008, Free Software Foundation, Inc. --
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. --
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. --
29 -- GNAT was originally developed by the GNAT team at New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc. --
32 ------------------------------------------------------------------------------
34 with Output; use Output;
35 with Tree_IO; use Tree_IO;
37 with GNAT.HTable; use GNAT.HTable;
41 ------------------------
42 -- Local Declarations --
43 ------------------------
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.
53 -- Uint value containing Int'Last value set by Initialize
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.
62 -- Number of entries set in UI_Power_2;
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.
68 UI_Power_10_Set : Nat;
69 -- Number of entries set in UI_Power_10;
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.
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).
85 ----------------------------
86 -- UI_From_Int Hash Table --
87 ----------------------------
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
93 subtype Hnum is Nat range 0 .. 1022;
95 function Hash_Num (F : Int) return Hnum;
98 package UI_Ints is new Simple_HTable (
101 No_Element => No_Uint,
106 -----------------------
107 -- Local Subprograms --
108 -----------------------
110 function Direct (U : Uint) return Boolean;
111 pragma Inline (Direct);
112 -- Returns True if U is represented directly
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.
118 function GCD (Jin, Kin : Int) return Int;
119 -- Compute GCD of two integers. Assumes that Jin >= Kin >= 0
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.
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
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
148 procedure Most_Sig_2_Digits
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.
158 function N_Digits (Input : Uint) return Int;
159 pragma Inline (N_Digits);
160 -- Returns number of "digits" in a Uint
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.
166 function Sum_Double_Digits (Left : Uint; Sign : Int) return Int;
167 -- Same as above but work in New_Base = Base * Base
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).
178 -- If Discard_Quotient is True, Quotient is left unchanged.
179 -- If Discard_Remainder is True, Remainder is left unchanged.
181 function Vector_To_Uint
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.
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.
201 function Direct (U : Uint) return Boolean is
203 return Int (U) <= Int (Uint_Direct_Last);
210 function Direct_Val (U : Uint) return Int is
212 pragma Assert (Direct (U));
213 return Int (U) - Int (Uint_Direct_Bias);
220 function GCD (Jin, Kin : Int) return Int is
224 pragma Assert (Jin >= Kin);
225 pragma Assert (Kin >= Int_0);
229 while K /= Uint_0 loop
242 function Hash_Num (F : Int) return Hnum is
244 return Standard."mod" (F, Hnum'Range_Length);
256 Marks : constant Uintp.Save_Mark := Uintp.Mark;
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.
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.
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.
275 procedure Image_Char (C : Character);
276 -- Internal procedure to output one character
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.
282 procedure Image_Uint (U : Uint);
283 -- Internal procedure to output characters of non-negative Uint
289 function Better_In_Hex return Boolean is
290 T16 : constant Uint := Uint_2 ** Int'(16);
296 -- Small values up to 2**16 can always be in decimal
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.
305 if A mod Uint_2 = Uint_1 then
310 if A mod T16 /= Uint_0 then
320 while A > Uint_2 loop
321 if A mod Uint_2 /= Uint_0 then
336 procedure Image_Char (C : Character) is
339 if UI_Image_Length + 6 > UI_Image_Max then
340 Exponent := Exponent + 1;
342 UI_Image_Length := UI_Image_Length + 1;
343 UI_Image_Buffer (UI_Image_Length) := C;
354 procedure Image_Exponent (N : Natural) is
357 Image_Exponent (N / 10);
360 UI_Image_Length := UI_Image_Length + 1;
361 UI_Image_Buffer (UI_Image_Length) :=
362 Character'Val (Character'Pos ('0') + N mod 10);
369 procedure Image_Uint (U : Uint) is
370 H : constant array (Int range 0 .. 15) of Character :=
375 Image_Uint (U / Base);
378 if Digs_Output = 4 and then Base = Uint_16 then
383 Image_Char (H (UI_To_Int (U rem Base)));
385 Digs_Output := Digs_Output + 1;
388 -- Start of processing for Image_Out
391 if Input = No_Uint then
396 UI_Image_Length := 0;
398 if Input < Uint_0 then
406 or else (Format = Auto and then Better_In_Hex)
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);
426 Uintp.Release (Marks);
433 procedure Init_Operand (UI : Uint; Vec : out UI_Vector) is
436 pragma Assert (Vec'First = Int'(1));
440 Vec (1) := Direct_Val (UI);
442 if Vec (1) >= Base then
443 Vec (2) := Vec (1) rem Base;
444 Vec (1) := Vec (1) / Base;
448 Loc := Uints.Table (UI).Loc;
450 for J in 1 .. Uints.Table (UI).Length loop
451 Vec (J) := Udigits.Table (Loc + J - 1);
460 procedure Initialize is
465 Uint_Int_First := UI_From_Int (Int'First);
466 Uint_Int_Last := UI_From_Int (Int'Last);
468 UI_Power_2 (0) := Uint_1;
471 UI_Power_10 (0) := Uint_1;
472 UI_Power_10_Set := 0;
474 Uints_Min := Uints.Last;
475 Udigits_Min := Udigits.Last;
480 ---------------------
481 -- Least_Sig_Digit --
482 ---------------------
484 function Least_Sig_Digit (Arg : Uint) return Int is
489 V := Direct_Val (Arg);
495 -- Note that this result may be negative
502 (Uints.Table (Arg).Loc + Uints.Table (Arg).Length - 1);
510 function Mark return Save_Mark is
512 return (Save_Uint => Uints.Last, Save_Udigit => Udigits.Last);
515 -----------------------
516 -- Most_Sig_2_Digits --
517 -----------------------
519 procedure Most_Sig_2_Digits
526 pragma Assert (Left >= Right);
528 if Direct (Left) then
529 Left_Hat := Direct_Val (Left);
530 Right_Hat := Direct_Val (Right);
536 Udigits.Table (Uints.Table (Left).Loc);
538 Udigits.Table (Uints.Table (Left).Loc + 1);
541 -- It is not so clear what to return when Arg is negative???
543 Left_Hat := abs (L1) * Base + L2;
548 Length_L : constant Int := Uints.Table (Left).Length;
555 if Direct (Right) then
556 T := Direct_Val (Left);
557 R1 := abs (T / Base);
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;
567 if Length_L = Length_R then
568 Right_Hat := R1 * Base + R2;
569 elsif Length_L = Length_R + Int_1 then
575 end Most_Sig_2_Digits;
581 -- Note: N_Digits returns 1 for No_Uint
583 function N_Digits (Input : Uint) return Int is
585 if Direct (Input) then
586 if Direct_Val (Input) >= Base then
593 return Uints.Table (Input).Length;
601 function Num_Bits (Input : Uint) return Nat is
606 -- Largest negative number has to be handled specially, since it is in
607 -- Int_Range, but we cannot take the absolute value.
609 if Input = Uint_Int_First then
612 -- For any other number in Int_Range, get absolute value of number
614 elsif UI_Is_In_Int_Range (Input) then
615 Num := abs (UI_To_Int (Input));
618 -- If not in Int_Range then initialize bit count for all low order
619 -- words, and set number to high order digit.
622 Bits := Base_Bits * (Uints.Table (Input).Length - 1);
623 Num := abs (Udigits.Table (Uints.Table (Input).Loc));
626 -- Increase bit count for remaining value in Num
628 while Types.">" (Num, 0) loop
640 procedure pid (Input : Uint) is
642 UI_Write (Input, Decimal);
650 procedure pih (Input : Uint) is
652 UI_Write (Input, Hex);
660 procedure Release (M : Save_Mark) is
662 Uints.Set_Last (Uint'Max (M.Save_Uint, Uints_Min));
663 Udigits.Set_Last (Int'Max (M.Save_Udigit, Udigits_Min));
666 ----------------------
667 -- Release_And_Save --
668 ----------------------
670 procedure Release_And_Save (M : Save_Mark; UI : in out Uint) is
677 UE_Len : constant Pos := Uints.Table (UI).Length;
678 UE_Loc : constant Int := Uints.Table (UI).Loc;
680 UD : constant Udigits.Table_Type (1 .. UE_Len) :=
681 Udigits.Table (UE_Loc .. UE_Loc + UE_Len - 1);
686 Uints.Append ((Length => UE_Len, Loc => Udigits.Last + 1));
689 for J in 1 .. UE_Len loop
690 Udigits.Append (UD (J));
694 end Release_And_Save;
696 procedure Release_And_Save (M : Save_Mark; UI1, UI2 : in out Uint) is
699 Release_And_Save (M, UI2);
701 elsif Direct (UI2) then
702 Release_And_Save (M, UI1);
706 UE1_Len : constant Pos := Uints.Table (UI1).Length;
707 UE1_Loc : constant Int := Uints.Table (UI1).Loc;
709 UD1 : constant Udigits.Table_Type (1 .. UE1_Len) :=
710 Udigits.Table (UE1_Loc .. UE1_Loc + UE1_Len - 1);
712 UE2_Len : constant Pos := Uints.Table (UI2).Length;
713 UE2_Loc : constant Int := Uints.Table (UI2).Loc;
715 UD2 : constant Udigits.Table_Type (1 .. UE2_Len) :=
716 Udigits.Table (UE2_Loc .. UE2_Loc + UE2_Len - 1);
721 Uints.Append ((Length => UE1_Len, Loc => Udigits.Last + 1));
724 for J in 1 .. UE1_Len loop
725 Udigits.Append (UD1 (J));
728 Uints.Append ((Length => UE2_Len, Loc => Udigits.Last + 1));
731 for J in 1 .. UE2_Len loop
732 Udigits.Append (UD2 (J));
736 end Release_And_Save;
742 -- This is done in one pass
744 -- Mathematically: assume base congruent to 1 and compute an equivalent
747 -- If Sign = -1 return the alternating sum of the "digits"
749 -- D1 - D2 + D3 - D4 + D5 ...
751 -- (where D1 is Least Significant Digit)
753 -- Mathematically: assume base congruent to -1 and compute an equivalent
756 -- This is used in Rem and Base is assumed to be 2 ** 15
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.
762 function Sum_Digits (Left : Uint; Sign : Int) return Int is
764 pragma Assert (Sign = Int_1 or Sign = Int (-1));
766 -- First try simple case;
768 if Direct (Left) then
770 Tmp_Int : Int := Direct_Val (Left);
773 if Tmp_Int >= Base then
774 Tmp_Int := (Tmp_Int / Base) +
775 Sign * (Tmp_Int rem Base);
777 -- Now Tmp_Int is in [-(Base - 1) .. 2 * (Base - 1)]
779 if Tmp_Int >= Base then
783 Tmp_Int := (Tmp_Int / Base) + 1;
787 -- Now Tmp_Int is in [-(Base - 1) .. (Base - 1)]
794 -- Otherwise full circuit is needed
798 L_Length : constant Int := N_Digits (Left);
799 L_Vec : UI_Vector (1 .. L_Length);
805 Init_Operand (Left, L_Vec);
806 L_Vec (1) := abs L_Vec (1);
811 for J in reverse 1 .. L_Length loop
812 Tmp_Int := Tmp_Int + Alt * (L_Vec (J) + Carry);
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]
818 if Tmp_Int >= Base then
819 Tmp_Int := Tmp_Int - Base;
822 elsif Tmp_Int <= -Base then
823 Tmp_Int := Tmp_Int + Base;
830 -- Tmp_Int is now between [-Base + 1 .. Base - 1]
835 Tmp_Int := Tmp_Int + Alt * Carry;
837 -- Tmp_Int is now between [-Base .. Base]
839 if Tmp_Int >= Base then
840 Tmp_Int := Tmp_Int - Base + Alt * Sign * 1;
842 elsif Tmp_Int <= -Base then
843 Tmp_Int := Tmp_Int + Base + Alt * Sign * (-1);
846 -- Now Tmp_Int is in [-(Base - 1) .. (Base - 1)]
853 -----------------------
854 -- Sum_Double_Digits --
855 -----------------------
857 -- Note: This is used in Rem, Base is assumed to be 2 ** 15
859 function Sum_Double_Digits (Left : Uint; Sign : Int) return Int is
861 -- First try simple case;
863 pragma Assert (Sign = Int_1 or Sign = Int (-1));
865 if Direct (Left) then
866 return Direct_Val (Left);
868 -- Otherwise full circuit is needed
872 L_Length : constant Int := N_Digits (Left);
873 L_Vec : UI_Vector (1 .. L_Length);
881 Init_Operand (Left, L_Vec);
882 L_Vec (1) := abs L_Vec (1);
890 Least_Sig_Int := Least_Sig_Int + Alt * (L_Vec (J) + Carry);
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]
896 if Least_Sig_Int >= Base then
897 Least_Sig_Int := Least_Sig_Int - Base;
900 elsif Least_Sig_Int <= -Base then
901 Least_Sig_Int := Least_Sig_Int + Base;
908 -- Least is now in [-Base + 1 .. Base - 1]
910 Most_Sig_Int := Most_Sig_Int + Alt * (L_Vec (J - 1) + Carry);
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]
916 if Most_Sig_Int >= Base then
917 Most_Sig_Int := Most_Sig_Int - Base;
920 elsif Most_Sig_Int <= -Base then
921 Most_Sig_Int := Most_Sig_Int + Base;
927 -- Most is now in [-Base + 1 .. Base - 1]
934 Least_Sig_Int := Least_Sig_Int + Alt * (L_Vec (J) + Carry);
936 Least_Sig_Int := Least_Sig_Int + Alt * Carry;
939 if Least_Sig_Int >= Base then
940 Least_Sig_Int := Least_Sig_Int - Base;
941 Most_Sig_Int := Most_Sig_Int + Alt * 1;
943 elsif Least_Sig_Int <= -Base then
944 Least_Sig_Int := Least_Sig_Int + Base;
945 Most_Sig_Int := Most_Sig_Int + Alt * (-1);
948 if Most_Sig_Int >= Base then
949 Most_Sig_Int := Most_Sig_Int - Base;
952 Least_Sig_Int + Alt * 1; -- cannot overflow again
954 elsif Most_Sig_Int <= -Base then
955 Most_Sig_Int := Most_Sig_Int + Base;
958 Least_Sig_Int + Alt * (-1); -- cannot overflow again.
961 return Most_Sig_Int * Base + Least_Sig_Int;
964 end Sum_Double_Digits;
970 procedure Tree_Read is
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);
982 for J in 0 .. UI_Power_2_Set loop
983 Tree_Read_Int (Int (UI_Power_2 (J)));
986 for J in 0 .. UI_Power_10_Set loop
987 Tree_Read_Int (Int (UI_Power_10 (J)));
996 procedure Tree_Write is
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);
1008 for J in 0 .. UI_Power_2_Set loop
1009 Tree_Write_Int (Int (UI_Power_2 (J)));
1012 for J in 0 .. UI_Power_10_Set loop
1013 Tree_Write_Int (Int (UI_Power_10 (J)));
1022 function UI_Abs (Right : Uint) return Uint is
1024 if Right < Uint_0 then
1035 function UI_Add (Left : Int; Right : Uint) return Uint is
1037 return UI_Add (UI_From_Int (Left), Right);
1040 function UI_Add (Left : Uint; Right : Int) return Uint is
1042 return UI_Add (Left, UI_From_Int (Right));
1045 function UI_Add (Left : Uint; Right : Uint) return Uint is
1047 -- Simple cases of direct operands and addition of zero
1049 if Direct (Left) then
1050 if Direct (Right) then
1051 return UI_From_Int (Direct_Val (Left) + Direct_Val (Right));
1053 elsif Int (Left) = Int (Uint_0) then
1057 elsif Direct (Right) and then Int (Right) = Int (Uint_0) then
1061 -- Otherwise full circuit is needed
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);
1072 X_Bigger : Boolean := False;
1073 Y_Bigger : Boolean := False;
1074 Result_Neg : Boolean := False;
1077 Init_Operand (Left, L_Vec);
1078 Init_Operand (Right, R_Vec);
1080 -- At least one of the two operands is in multi-digit form.
1081 -- Calculate the number of digits sufficient to hold result.
1083 if L_Length > R_Length then
1084 Sum_Length := L_Length + 1;
1087 Sum_Length := R_Length + 1;
1089 if R_Length > L_Length then
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.
1099 X : UI_Vector (1 .. Sum_Length);
1100 Y : UI_Vector (1 .. Sum_Length);
1101 Tmp_UI : UI_Vector (1 .. Sum_Length);
1104 for J in 1 .. Sum_Length - L_Length loop
1108 X (Sum_Length - L_Length + 1) := abs L_Vec (1);
1110 for J in 2 .. L_Length loop
1111 X (J + (Sum_Length - L_Length)) := L_Vec (J);
1114 for J in 1 .. Sum_Length - R_Length loop
1118 Y (Sum_Length - R_Length + 1) := abs R_Vec (1);
1120 for J in 2 .. R_Length loop
1121 Y (J + (Sum_Length - R_Length)) := R_Vec (J);
1124 if (L_Vec (1) < Int_0) = (R_Vec (1) < Int_0) then
1126 -- Same sign so just add
1129 for J in reverse 1 .. Sum_Length loop
1130 Tmp_Int := X (J) + Y (J) + Carry;
1132 if Tmp_Int >= Base then
1133 Tmp_Int := Tmp_Int - Base;
1142 return Vector_To_Uint (X, L_Vec (1) < Int_0);
1145 -- Find which one has bigger magnitude
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
1152 elsif abs R_Vec (J) > abs L_Vec (J) then
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.
1163 Result_Neg := False;
1165 if not (X_Bigger or Y_Bigger) then
1169 if R_Vec (1) < Int_0 then
1178 if L_Vec (1) < Int_0 then
1183 -- Subtract Y from the bigger X
1187 for J in reverse 1 .. Sum_Length loop
1188 Tmp_Int := X (J) - Y (J) + Borrow;
1190 if Tmp_Int < Int_0 then
1191 Tmp_Int := Tmp_Int + Base;
1200 return Vector_To_Uint (X, Result_Neg);
1207 --------------------------
1208 -- UI_Decimal_Digits_Hi --
1209 --------------------------
1211 function UI_Decimal_Digits_Hi (U : Uint) return Nat is
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
1218 return 5 * N_Digits (U);
1219 end UI_Decimal_Digits_Hi;
1221 --------------------------
1222 -- UI_Decimal_Digits_Lo --
1223 --------------------------
1225 function UI_Decimal_Digits_Lo (U : Uint) return Nat is
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.
1233 return 1 + 4 * (N_Digits (U) - 1);
1234 end UI_Decimal_Digits_Lo;
1240 function UI_Div (Left : Int; Right : Uint) return Uint is
1242 return UI_Div (UI_From_Int (Left), Right);
1245 function UI_Div (Left : Uint; Right : Int) return Uint is
1247 return UI_Div (Left, UI_From_Int (Right));
1250 function UI_Div (Left, Right : Uint) return Uint is
1253 pragma Warnings (Off, Remainder);
1257 Quotient, Remainder,
1258 Discard_Quotient => False,
1259 Discard_Remainder => True);
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)
1274 pragma Warnings (Off, Quotient);
1275 pragma Warnings (Off, Remainder);
1277 pragma Assert (Right /= Uint_0);
1279 -- Cases where both operands are represented directly
1281 if Direct (Left) and then Direct (Right) then
1283 DV_Left : constant Int := Direct_Val (Left);
1284 DV_Right : constant Int := Direct_Val (Right);
1287 if not Discard_Quotient then
1288 Quotient := UI_From_Int (DV_Left / DV_Right);
1291 if not Discard_Remainder then
1292 Remainder := UI_From_Int (DV_Left rem DV_Right);
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);
1312 procedure UI_Div_Vector
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
1320 procedure UI_Div_Vector
1323 Quotient : out UI_Vector;
1324 Remainder : out Int)
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;
1336 if L_Vec (L_Vec'First) < Int_0 then
1337 Remainder := -Remainder;
1341 -- Start of processing for UI_Div_Rem
1344 -- Result is zero if left operand is shorter than right
1346 if L_Length < R_Length then
1347 if not Discard_Quotient then
1350 if not Discard_Remainder then
1356 Init_Operand (Left, L_Vec);
1357 Init_Operand (Right, R_Vec);
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).
1364 if R_Length = Int_1 then
1365 Tmp_Divisor := abs R_Vec (1);
1368 Quotient_V : UI_Vector (1 .. L_Length);
1371 UI_Div_Vector (L_Vec, Tmp_Divisor, Quotient_V, Remainder_I);
1373 if not Discard_Quotient then
1376 (Quotient_V, (L_Vec (1) < Int_0 xor R_Vec (1) < Int_0));
1379 if not Discard_Remainder then
1380 Remainder := UI_From_Int (Remainder_I);
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
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);
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.
1403 D := Base / (abs R_Vec (1) + 1);
1406 Dividend (2) := abs L_Vec (1);
1408 for J in 3 .. L_Length + Int_1 loop
1409 Dividend (J) := L_Vec (J - 1);
1412 Divisor (1) := abs R_Vec (1);
1414 for J in Int_2 .. R_Length loop
1415 Divisor (J) := R_Vec (J);
1420 -- Multiply Dividend by D
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;
1429 -- Multiply Divisor by d
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;
1439 -- Main loop of long division algorithm
1441 Divisor_Dig1 := Divisor (1);
1442 Divisor_Dig2 := Divisor (2);
1444 for J in Quotient_V'Range loop
1446 -- [ CALCULATE Q (hat) ] (step D3 in the algorithm)
1448 Tmp_Int := Dividend (J) * Base + Dividend (J + 1);
1452 if Dividend (J) = Divisor_Dig1 then
1453 Q_Guess := Base - 1;
1455 Q_Guess := Tmp_Int / Divisor_Dig1;
1460 while Divisor_Dig2 * Q_Guess >
1461 (Tmp_Int - Q_Guess * Divisor_Dig1) * Base +
1464 Q_Guess := Q_Guess - 1;
1467 -- [ MULTIPLY & SUBTRACT ] (step D4). Q_Guess * Divisor is
1468 -- subtracted from the remaining dividend.
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;
1476 if Tmp_Dig < Int_0 then
1477 Tmp_Dig := Tmp_Dig + Base;
1481 Dividend (J + K) := Tmp_Dig;
1484 Dividend (J) := Dividend (J) + Carry;
1486 -- [ TEST REMAINDER ] & [ ADD BACK ] (steps D5 and D6)
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
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.
1497 if Dividend (J) < Int_0 then
1498 Q_Guess := Q_Guess - 1;
1501 for K in reverse Divisor'Range loop
1502 Tmp_Int := Dividend (J + K) + Divisor (K) + Carry;
1504 if Tmp_Int >= Base then
1505 Tmp_Int := Tmp_Int - Base;
1511 Dividend (J + K) := Tmp_Int;
1514 Dividend (J) := Dividend (J) + Carry;
1517 -- Finally we can get the next quotient digit
1519 Quotient_V (J) := Q_Guess;
1522 -- [ UNNORMALIZE ] (step D8)
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));
1529 if not Discard_Remainder then
1531 Remainder_V : UI_Vector (1 .. R_Length);
1533 pragma Warnings (Off, Discard_Int);
1536 (Dividend (Dividend'Last - R_Length + 1 .. Dividend'Last),
1538 Remainder_V, Discard_Int);
1539 Remainder := Vector_To_Uint (Remainder_V, L_Vec (1) < Int_0);
1550 function UI_Eq (Left : Int; Right : Uint) return Boolean is
1552 return not UI_Ne (UI_From_Int (Left), Right);
1555 function UI_Eq (Left : Uint; Right : Int) return Boolean is
1557 return not UI_Ne (Left, UI_From_Int (Right));
1560 function UI_Eq (Left : Uint; Right : Uint) return Boolean is
1562 return not UI_Ne (Left, Right);
1569 function UI_Expon (Left : Int; Right : Uint) return Uint is
1571 return UI_Expon (UI_From_Int (Left), Right);
1574 function UI_Expon (Left : Uint; Right : Int) return Uint is
1576 return UI_Expon (Left, UI_From_Int (Right));
1579 function UI_Expon (Left : Int; Right : Int) return Uint is
1581 return UI_Expon (UI_From_Int (Left), UI_From_Int (Right));
1584 function UI_Expon (Left : Uint; Right : Uint) return Uint is
1586 pragma Assert (Right >= Uint_0);
1588 -- Any value raised to power of 0 is 1
1590 if Right = Uint_0 then
1593 -- 0 to any positive power is 0
1595 elsif Left = Uint_0 then
1598 -- 1 to any power is 1
1600 elsif Left = Uint_1 then
1603 -- Any value raised to power of 1 is that value
1605 elsif Right = Uint_1 then
1608 -- Cases which can be done by table lookup
1610 elsif Right <= Uint_64 then
1612 -- 2 ** N for N in 2 .. 64
1614 if Left = Uint_2 then
1616 Right_Int : constant Int := Direct_Val (Right);
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;
1626 UI_Power_2_Set := Right_Int;
1629 return UI_Power_2 (Right_Int);
1632 -- 10 ** N for N in 2 .. 64
1634 elsif Left = Uint_10 then
1636 Right_Int : constant Int := Direct_Val (Right);
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;
1646 UI_Power_10_Set := Right_Int;
1649 return UI_Power_10 (Right_Int);
1654 -- If we fall through, then we have the general case (see Knuth 4.6.3)
1658 Squares : Uint := Left;
1659 Result : Uint := Uint_1;
1660 M : constant Uintp.Save_Mark := Uintp.Mark;
1664 if (Least_Sig_Digit (N) mod Int_2) = Int_1 then
1665 Result := Result * Squares;
1669 exit when N = Uint_0;
1670 Squares := Squares * Squares;
1673 Uintp.Release_And_Save (M, Result);
1682 function UI_From_CC (Input : Char_Code) return Uint is
1684 return UI_From_Dint (Dint (Input));
1691 function UI_From_Dint (Input : Dint) return Uint is
1694 if Dint (Min_Direct) <= Input and then Input <= Dint (Max_Direct) then
1695 return Uint (Dint (Uint_Direct_Bias) + Input);
1697 -- For values of larger magnitude, compute digits into a vector and call
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.
1706 V : UI_Vector (1 .. Max_For_Dint);
1708 Temp_Integer : Dint;
1711 for J in V'Range loop
1715 Temp_Integer := Input;
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);
1722 return Vector_To_Uint (V, Input < Dint'(0));
1731 function UI_From_Int (Input : Int) return Uint is
1735 if Min_Direct <= Input and then Input <= Max_Direct then
1736 return Uint (Int (Uint_Direct_Bias) + Input);
1739 -- If already in the hash table, return entry
1741 U := UI_Ints.Get (Input);
1743 if U /= No_Uint then
1747 -- For values of larger magnitude, compute digits into a vector and call
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.
1755 V : UI_Vector (1 .. Max_For_Int);
1760 for J in V'Range loop
1764 Temp_Integer := Input;
1766 for J in reverse V'Range loop
1767 V (J) := abs (Temp_Integer rem Base);
1768 Temp_Integer := Temp_Integer / Base;
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;
1783 -- Lehmer's algorithm for GCD
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).
1789 -- We use the same notation as Knuth (U_Hat standing for the obvious!)
1791 function UI_GCD (Uin, Vin : Uint) return Uint is
1793 -- Copies of Uin and Vin
1796 -- The most Significant digits of U,V
1798 A, B, C, D, T, Q, Den1, Den2 : Int;
1801 Marks : constant Uintp.Save_Mark := Uintp.Mark;
1802 Iterations : Integer := 0;
1805 pragma Assert (Uin >= Vin);
1806 pragma Assert (Vin >= Uint_0);
1812 Iterations := Iterations + 1;
1819 UI_From_Int (GCD (Direct_Val (V), UI_To_Int (U rem V)));
1823 Most_Sig_2_Digits (U, V, U_Hat, V_Hat);
1830 -- We might overflow and get division by zero here. This just
1831 -- means we cannot take the single precision step
1835 exit when Den1 = Int_0 or else Den2 = Int_0;
1837 -- Compute Q, the trial quotient
1839 Q := (U_Hat + A) / Den1;
1841 exit when Q /= ((U_Hat + B) / Den2);
1843 -- A single precision step Euclid step will give same answer as a
1844 -- multiprecision one.
1854 T := U_Hat - (Q * V_Hat);
1860 -- Take a multiprecision Euclid step
1864 -- No single precision steps take a regular Euclid step
1871 -- Use prior single precision steps to compute this Euclid step
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;
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. ???
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
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);
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.
1896 if Iterations > 100 then
1897 Release_And_Save (Marks, U, V);
1907 function UI_Ge (Left : Int; Right : Uint) return Boolean is
1909 return not UI_Lt (UI_From_Int (Left), Right);
1912 function UI_Ge (Left : Uint; Right : Int) return Boolean is
1914 return not UI_Lt (Left, UI_From_Int (Right));
1917 function UI_Ge (Left : Uint; Right : Uint) return Boolean is
1919 return not UI_Lt (Left, Right);
1926 function UI_Gt (Left : Int; Right : Uint) return Boolean is
1928 return UI_Lt (Right, UI_From_Int (Left));
1931 function UI_Gt (Left : Uint; Right : Int) return Boolean is
1933 return UI_Lt (UI_From_Int (Right), Left);
1936 function UI_Gt (Left : Uint; Right : Uint) return Boolean is
1938 return UI_Lt (Left => Right, Right => Left);
1945 procedure UI_Image (Input : Uint; Format : UI_Format := Auto) is
1947 Image_Out (Input, True, Format);
1950 -------------------------
1951 -- UI_Is_In_Int_Range --
1952 -------------------------
1954 function UI_Is_In_Int_Range (Input : Uint) return Boolean is
1956 -- Make sure we don't get called before Initialize
1958 pragma Assert (Uint_Int_First /= Uint_0);
1960 if Direct (Input) then
1963 return Input >= Uint_Int_First
1964 and then Input <= Uint_Int_Last;
1966 end UI_Is_In_Int_Range;
1972 function UI_Le (Left : Int; Right : Uint) return Boolean is
1974 return not UI_Lt (Right, UI_From_Int (Left));
1977 function UI_Le (Left : Uint; Right : Int) return Boolean is
1979 return not UI_Lt (UI_From_Int (Right), Left);
1982 function UI_Le (Left : Uint; Right : Uint) return Boolean is
1984 return not UI_Lt (Left => Right, Right => Left);
1991 function UI_Lt (Left : Int; Right : Uint) return Boolean is
1993 return UI_Lt (UI_From_Int (Left), Right);
1996 function UI_Lt (Left : Uint; Right : Int) return Boolean is
1998 return UI_Lt (Left, UI_From_Int (Right));
2001 function UI_Lt (Left : Uint; Right : Uint) return Boolean is
2003 -- Quick processing for identical arguments
2005 if Int (Left) = Int (Right) then
2008 -- Quick processing for both arguments directly represented
2010 elsif Direct (Left) and then Direct (Right) then
2011 return Int (Left) < Int (Right);
2013 -- At least one argument is more than one digit long
2017 L_Length : constant Int := N_Digits (Left);
2018 R_Length : constant Int := N_Digits (Right);
2020 L_Vec : UI_Vector (1 .. L_Length);
2021 R_Vec : UI_Vector (1 .. R_Length);
2024 Init_Operand (Left, L_Vec);
2025 Init_Operand (Right, R_Vec);
2027 if L_Vec (1) < Int_0 then
2029 -- First argument negative, second argument non-negative
2031 if R_Vec (1) >= Int_0 then
2034 -- Both arguments negative
2037 if L_Length /= R_Length then
2038 return L_Length > R_Length;
2040 elsif L_Vec (1) /= R_Vec (1) then
2041 return L_Vec (1) < R_Vec (1);
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);
2055 -- First argument non-negative, second argument negative
2057 if R_Vec (1) < Int_0 then
2060 -- Both arguments non-negative
2063 if L_Length /= R_Length then
2064 return L_Length < R_Length;
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);
2084 function UI_Max (Left : Int; Right : Uint) return Uint is
2086 return UI_Max (UI_From_Int (Left), Right);
2089 function UI_Max (Left : Uint; Right : Int) return Uint is
2091 return UI_Max (Left, UI_From_Int (Right));
2094 function UI_Max (Left : Uint; Right : Uint) return Uint is
2096 if Left >= Right then
2107 function UI_Min (Left : Int; Right : Uint) return Uint is
2109 return UI_Min (UI_From_Int (Left), Right);
2112 function UI_Min (Left : Uint; Right : Int) return Uint is
2114 return UI_Min (Left, UI_From_Int (Right));
2117 function UI_Min (Left : Uint; Right : Uint) return Uint is
2119 if Left <= Right then
2130 function UI_Mod (Left : Int; Right : Uint) return Uint is
2132 return UI_Mod (UI_From_Int (Left), Right);
2135 function UI_Mod (Left : Uint; Right : Int) return Uint is
2137 return UI_Mod (Left, UI_From_Int (Right));
2140 function UI_Mod (Left : Uint; Right : Uint) return Uint is
2141 Urem : constant Uint := Left rem Right;
2144 if (Left < Uint_0) = (Right < Uint_0)
2145 or else Urem = Uint_0
2149 return Right + Urem;
2153 -------------------------------
2154 -- UI_Modular_Exponentiation --
2155 -------------------------------
2157 function UI_Modular_Exponentiation
2160 Modulo : Uint) return Uint
2162 M : constant Save_Mark := Mark;
2164 Result : Uint := Uint_1;
2166 Exponent : Uint := E;
2169 while Exponent /= Uint_0 loop
2170 if Least_Sig_Digit (Exponent) rem Int'(2) = Int'(1) then
2171 Result := (Result * Base) rem Modulo;
2174 Exponent := Exponent / Uint_2;
2175 Base := (Base * Base) rem Modulo;
2178 Release_And_Save (M, Result);
2180 end UI_Modular_Exponentiation;
2182 ------------------------
2183 -- UI_Modular_Inverse --
2184 ------------------------
2186 function UI_Modular_Inverse (N : Uint; Modulo : Uint) return Uint is
2187 M : constant Save_Mark := Mark;
2207 Quotient => Q, Remainder => R,
2208 Discard_Quotient => False,
2209 Discard_Remainder => False);
2219 exit when R = Uint_1;
2222 if S = Int'(-1) then
2226 Release_And_Save (M, X);
2228 end UI_Modular_Inverse;
2234 function UI_Mul (Left : Int; Right : Uint) return Uint is
2236 return UI_Mul (UI_From_Int (Left), Right);
2239 function UI_Mul (Left : Uint; Right : Int) return Uint is
2241 return UI_Mul (Left, UI_From_Int (Right));
2244 function UI_Mul (Left : Uint; Right : Uint) return Uint is
2246 -- Simple case of single length operands
2248 if Direct (Left) and then Direct (Right) then
2251 (Dint (Direct_Val (Left)) * Dint (Direct_Val (Right)));
2254 -- Otherwise we have the general case (Algorithm M in Knuth)
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);
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));
2270 Algorithm_M : declare
2271 Product : UI_Vector (1 .. L_Length + R_Length);
2276 for J in Product'Range loop
2280 for J in reverse R_Vec'Range loop
2282 for K in reverse L_Vec'Range loop
2284 L_Vec (K) * R_Vec (J) + Product (J + K) + Carry;
2285 Product (J + K) := Tmp_Sum rem Base;
2286 Carry := Tmp_Sum / Base;
2289 Product (J) := Carry;
2292 return Vector_To_Uint (Product, Neg);
2301 function UI_Ne (Left : Int; Right : Uint) return Boolean is
2303 return UI_Ne (UI_From_Int (Left), Right);
2306 function UI_Ne (Left : Uint; Right : Int) return Boolean is
2308 return UI_Ne (Left, UI_From_Int (Right));
2311 function UI_Ne (Left : Uint; Right : Uint) return Boolean is
2313 -- Quick processing for identical arguments. Note that this takes
2314 -- care of the case of two No_Uint arguments.
2316 if Int (Left) = Int (Right) then
2320 -- See if left operand directly represented
2322 if Direct (Left) then
2324 -- If right operand directly represented then compare
2326 if Direct (Right) then
2327 return Int (Left) /= Int (Right);
2329 -- Left operand directly represented, right not, must be unequal
2335 -- Right operand directly represented, left not, must be unequal
2337 elsif Direct (Right) then
2341 -- Otherwise both multi-word, do comparison
2344 Size : constant Int := N_Digits (Left);
2349 if Size /= N_Digits (Right) then
2353 Left_Loc := Uints.Table (Left).Loc;
2354 Right_Loc := Uints.Table (Right).Loc;
2356 for J in Int_0 .. Size - Int_1 loop
2357 if Udigits.Table (Left_Loc + J) /=
2358 Udigits.Table (Right_Loc + J)
2372 function UI_Negate (Right : Uint) return Uint is
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.
2378 if Direct (Right) then
2379 return UI_From_Int (-Direct_Val (Right));
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.
2388 R_Length : constant Int := N_Digits (Right);
2389 R_Vec : UI_Vector (1 .. R_Length);
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);
2405 function UI_Rem (Left : Int; Right : Uint) return Uint is
2407 return UI_Rem (UI_From_Int (Left), Right);
2410 function UI_Rem (Left : Uint; Right : Int) return Uint is
2412 return UI_Rem (Left, UI_From_Int (Right));
2415 function UI_Rem (Left, Right : Uint) return Uint is
2419 subtype Int1_12 is Integer range 1 .. 12;
2422 pragma Assert (Right /= Uint_0);
2424 if Direct (Right) then
2425 if Direct (Left) then
2426 return UI_From_Int (Direct_Val (Left) rem Direct_Val (Right));
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)
2435 if (Right <= Uint_12) and then (Right >= Uint_Minus_12) then
2437 if Left < Uint_0 then
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.
2447 case Int1_12 (abs (Direct_Val (Right))) is
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.
2456 -- Least_Sig_Digit might return Negative numbers
2459 return UI_From_Int (
2460 Sign * (Least_Sig_Digit (Left) mod 2));
2463 return UI_From_Int (
2464 Sign * (Least_Sig_Digit (Left) mod 4));
2467 return UI_From_Int (
2468 Sign * (Least_Sig_Digit (Left) mod 8));
2470 -- Some number theoretical tricks:
2472 -- If B Rem Right = 1 then
2473 -- Left Rem Right = Sum_Of_Digits_Base_B (Left) Rem Right
2475 -- Note: 2^32 mod 3 = 1
2478 return UI_From_Int (
2479 Sign * (Sum_Double_Digits (Left, 1) rem Int (3)));
2481 -- Note: 2^15 mod 7 = 1
2484 return UI_From_Int (
2485 Sign * (Sum_Digits (Left, 1) rem Int (7)));
2487 -- Note: 2^32 mod 5 = -1
2489 -- Alternating sums might be negative, but rem is always
2490 -- positive hence we must use mod here.
2493 Tmp := Sum_Double_Digits (Left, -1) mod Int (5);
2494 return UI_From_Int (Sign * Tmp);
2496 -- Note: 2^15 mod 9 = -1
2498 -- Alternating sums might be negative, but rem is always
2499 -- positive hence we must use mod here.
2502 Tmp := Sum_Digits (Left, -1) mod Int (9);
2503 return UI_From_Int (Sign * Tmp);
2505 -- Note: 2^15 mod 11 = -1
2507 -- Alternating sums might be negative, but rem is always
2508 -- positive hence we must use mod here.
2511 Tmp := Sum_Digits (Left, -1) mod Int (11);
2512 return UI_From_Int (Sign * Tmp);
2514 -- Now resort to Chinese Remainder theorem to reduce 6, 10,
2515 -- 12 to previous special cases
2517 -- There is no reason we could not add more cases like these
2518 -- if it proves useful.
2520 -- Perhaps we should go up to 16, however we have no "trick"
2523 -- To find u mod m we:
2526 -- GCD(m1, m2) = 1 AND m = (m1 * m2).
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
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
2537 -- m1 = 2; m2 = 3; M1 = 3; M2 = 4;
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));
2544 -- m1 = 2; m2 = 5; M1 = 5; M2 = 6;
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));
2551 -- m1 = 3; m2 = 4; M1 = 4; M2 = 9;
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));
2561 -- Else fall through to general case
2563 -- The special case Length (Left) = Length (Right) = 1 in Div
2564 -- looks slow. It uses UI_To_Int when Int should suffice. ???
2571 pragma Warnings (Off, Quotient);
2574 (Left, Right, Quotient, Remainder,
2575 Discard_Quotient => True,
2576 Discard_Remainder => False);
2585 function UI_Sub (Left : Int; Right : Uint) return Uint is
2587 return UI_Add (Left, -Right);
2590 function UI_Sub (Left : Uint; Right : Int) return Uint is
2592 return UI_Add (Left, -Right);
2595 function UI_Sub (Left : Uint; Right : Uint) return Uint is
2597 if Direct (Left) and then Direct (Right) then
2598 return UI_From_Int (Direct_Val (Left) - Direct_Val (Right));
2600 return UI_Add (Left, -Right);
2608 function UI_To_CC (Input : Uint) return Char_Code is
2610 if Direct (Input) then
2611 return Char_Code (Direct_Val (Input));
2613 -- Case of input is more than one digit
2617 In_Length : constant Int := N_Digits (Input);
2618 In_Vec : UI_Vector (1 .. In_Length);
2622 Init_Operand (Input, In_Vec);
2624 -- We assume value is positive
2627 for Idx in In_Vec'Range loop
2628 Ret_CC := Ret_CC * Char_Code (Base) +
2629 Char_Code (abs In_Vec (Idx));
2641 function UI_To_Int (Input : Uint) return Int is
2643 if Direct (Input) then
2644 return Direct_Val (Input);
2646 -- Case of input is more than one digit
2650 In_Length : constant Int := N_Digits (Input);
2651 In_Vec : UI_Vector (1 .. In_Length);
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.
2659 pragma Assert (UI_Is_In_Int_Range (Input));
2661 -- Otherwise, proceed ahead, we are OK
2663 Init_Operand (Input, In_Vec);
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???
2670 for Idx in In_Vec'Range loop
2671 Ret_Int := Ret_Int * Base - abs In_Vec (Idx);
2674 if In_Vec (1) < Int_0 then
2687 procedure UI_Write (Input : Uint; Format : UI_Format := Auto) is
2689 Image_Out (Input, False, Format);
2692 ---------------------
2693 -- Vector_To_Uint --
2694 ---------------------
2696 function Vector_To_Uint
2697 (In_Vec : UI_Vector;
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
2708 for J in In_Vec'Range loop
2709 if In_Vec (J) /= Int_0 then
2711 -- The length of the value is the length of the rest of the vector
2713 Size := In_Vec'Last - J + 1;
2715 -- One digit value can always be represented directly
2717 if Size = Int_1 then
2719 return Uint (Int (Uint_Direct_Bias) - In_Vec (J));
2721 return Uint (Int (Uint_Direct_Bias) + In_Vec (J));
2724 -- Positive two digit values may be in direct representation range
2726 elsif Size = Int_2 and then not Negative then
2727 Val := In_Vec (J) * Base + In_Vec (J + 1);
2729 if Val <= Max_Direct then
2730 return Uint (Int (Uint_Direct_Bias) + Val);
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.
2739 Uints.Append ((Length => Size, Loc => Udigits.Last + 1));
2747 Udigits.Append (Val);
2749 for K in 2 .. Size loop
2750 Udigits.Append (In_Vec (J + K - 1));
2757 -- Dropped through loop only if vector contained all zeros