1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2001 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, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, 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;
39 ------------------------
40 -- Local Declarations --
41 ------------------------
43 Uint_Int_First : Uint := Uint_0;
44 -- Uint value containing Int'First value, set by Initialize. The initial
45 -- value of Uint_0 is used for an assertion check that ensures that this
46 -- value is not used before it is initialized. This value is used in the
47 -- UI_Is_In_Int_Range predicate, and it is right that this is a host
48 -- value, since the issue is host representation of integer values.
51 -- Uint value containing Int'Last value set by Initialize.
53 UI_Power_2 : array (Int range 0 .. 64) of Uint;
54 -- This table is used to memoize exponentiations by powers of 2. The Nth
55 -- entry, if set, contains the Uint value 2 ** N. Initially UI_Power_2_Set
56 -- is zero and only the 0'th entry is set, the invariant being that all
57 -- entries in the range 0 .. UI_Power_2_Set are initialized.
60 -- Number of entries set in UI_Power_2;
62 UI_Power_10 : array (Int range 0 .. 64) of Uint;
63 -- This table is used to memoize exponentiations by powers of 10 in the
64 -- same manner as described above for UI_Power_2.
66 UI_Power_10_Set : Nat;
67 -- Number of entries set in UI_Power_10;
71 -- These values are used to make sure that the mark/release mechanism
72 -- does not destroy values saved in the U_Power tables. Whenever an
73 -- entry is made in the U_Power tables, Uints_Min and Udigits_Min are
74 -- updated to protect the entry, and Release never cuts back beyond
75 -- these minimum values.
77 Int_0 : constant Int := 0;
78 Int_1 : constant Int := 1;
79 Int_2 : constant Int := 2;
80 -- These values are used in some cases where the use of numeric literals
81 -- would cause ambiguities (integer vs Uint).
83 -----------------------
84 -- Local Subprograms --
85 -----------------------
87 function Direct (U : Uint) return Boolean;
88 pragma Inline (Direct);
89 -- Returns True if U is represented directly
91 function Direct_Val (U : Uint) return Int;
92 -- U is a Uint for is represented directly. The returned result
93 -- is the value represented.
95 function GCD (Jin, Kin : Int) return Int;
96 -- Compute GCD of two integers. Assumes that Jin >= Kin >= 0
102 -- Common processing for UI_Image and UI_Write, To_Buffer is set
103 -- True for UI_Image, and false for UI_Write, and Format is copied
104 -- from the Format parameter to UI_Image or UI_Write.
106 procedure Init_Operand (UI : Uint; Vec : out UI_Vector);
107 pragma Inline (Init_Operand);
108 -- This procedure puts the value of UI into the vector in canonical
109 -- multiple precision format. The parameter should be of the correct
110 -- size as determined by a previous call to N_Digits (UI). The first
111 -- digit of Vec contains the sign, all other digits are always non-
112 -- negative. Note that the input may be directly represented, and in
113 -- this case Vec will contain the corresponding one or two digit value.
115 function Least_Sig_Digit (Arg : Uint) return Int;
116 pragma Inline (Least_Sig_Digit);
117 -- Returns the Least Significant Digit of Arg quickly. When the given
118 -- Uint is less than 2**15, the value returned is the input value, in
119 -- this case the result may be negative. It is expected that any use
120 -- will mask off unnecessary bits. This is used for finding Arg mod B
121 -- where B is a power of two. Hence the actual base is irrelevent as
122 -- long as it is a power of two.
124 procedure Most_Sig_2_Digits
128 Right_Hat : out Int);
129 -- Returns leading two significant digits from the given pair of Uint's.
130 -- Mathematically: returns Left / (Base ** K) and Right / (Base ** K)
131 -- where K is as small as possible S.T. Right_Hat < Base * Base.
132 -- It is required that Left > Right for the algorithm to work.
134 function N_Digits (Input : Uint) return Int;
135 pragma Inline (N_Digits);
136 -- Returns number of "digits" in a Uint
138 function Sum_Digits (Left : Uint; Sign : Int) return Int;
139 -- If Sign = 1 return the sum of the "digits" of Abs (Left). If the
140 -- total has more then one digit then return Sum_Digits of total.
142 function Sum_Double_Digits (Left : Uint; Sign : Int) return Int;
143 -- Same as above but work in New_Base = Base * Base
145 function Vector_To_Uint
149 -- Functions that calculate values in UI_Vectors, call this function
150 -- to create and return the Uint value. In_Vec contains the multiple
151 -- precision (Base) representation of a non-negative value. Leading
152 -- zeroes are permitted. Negative is set if the desired result is
153 -- the negative of the given value. The result will be either the
154 -- appropriate directly represented value, or a table entry in the
155 -- proper canonical format is created and returned.
157 -- Note that Init_Operand puts a signed value in the result vector,
158 -- but Vector_To_Uint is always presented with a non-negative value.
159 -- The processing of signs is something that is done by the caller
160 -- before calling Vector_To_Uint.
166 function Direct (U : Uint) return Boolean is
168 return Int (U) <= Int (Uint_Direct_Last);
175 function Direct_Val (U : Uint) return Int is
177 pragma Assert (Direct (U));
178 return Int (U) - Int (Uint_Direct_Bias);
185 function GCD (Jin, Kin : Int) return Int is
189 pragma Assert (Jin >= Kin);
190 pragma Assert (Kin >= Int_0);
195 while K /= Uint_0 loop
213 Marks : constant Uintp.Save_Mark := Uintp.Mark;
217 Digs_Output : Natural := 0;
218 -- Counts digits output. In hex mode, but not in decimal mode, we
219 -- put an underline after every four hex digits that are output.
221 Exponent : Natural := 0;
222 -- If the number is too long to fit in the buffer, we switch to an
223 -- approximate output format with an exponent. This variable records
224 -- the exponent value.
226 function Better_In_Hex return Boolean;
227 -- Determines if it is better to generate digits in base 16 (result
228 -- is true) or base 10 (result is false). The choice is purely a
229 -- matter of convenience and aesthetics, so it does not matter which
230 -- value is returned from a correctness point of view.
232 procedure Image_Char (C : Character);
233 -- Internal procedure to output one character
235 procedure Image_Exponent (N : Natural);
236 -- Output non-zero exponent. Note that we only use the exponent
237 -- form in the buffer case, so we know that To_Buffer is true.
239 procedure Image_Uint (U : Uint);
240 -- Internal procedure to output characters of non-negative Uint
246 function Better_In_Hex return Boolean is
247 T16 : constant Uint := Uint_2 ** Int'(16);
253 -- Small values up to 2**16 can always be in decimal
259 -- Otherwise, see if we are a power of 2 or one less than a power
260 -- of 2. For the moment these are the only cases printed in hex.
262 if A mod Uint_2 = Uint_1 then
267 if A mod T16 /= Uint_0 then
277 while A > Uint_2 loop
278 if A mod Uint_2 /= Uint_0 then
293 procedure Image_Char (C : Character) is
296 if UI_Image_Length + 6 > UI_Image_Max then
297 Exponent := Exponent + 1;
299 UI_Image_Length := UI_Image_Length + 1;
300 UI_Image_Buffer (UI_Image_Length) := C;
311 procedure Image_Exponent (N : Natural) is
314 Image_Exponent (N / 10);
317 UI_Image_Length := UI_Image_Length + 1;
318 UI_Image_Buffer (UI_Image_Length) :=
319 Character'Val (Character'Pos ('0') + N mod 10);
326 procedure Image_Uint (U : Uint) is
327 H : array (Int range 0 .. 15) of Character := "0123456789ABCDEF";
331 Image_Uint (U / Base);
334 if Digs_Output = 4 and then Base = Uint_16 then
339 Image_Char (H (UI_To_Int (U rem Base)));
341 Digs_Output := Digs_Output + 1;
344 -- Start of processing for Image_Out
347 if Input = No_Uint then
352 UI_Image_Length := 0;
354 if Input < Uint_0 then
362 or else (Format = Auto and then Better_In_Hex)
376 if Exponent /= 0 then
377 UI_Image_Length := UI_Image_Length + 1;
378 UI_Image_Buffer (UI_Image_Length) := 'E';
379 Image_Exponent (Exponent);
382 Uintp.Release (Marks);
389 procedure Init_Operand (UI : Uint; Vec : out UI_Vector) is
394 Vec (1) := Direct_Val (UI);
396 if Vec (1) >= Base then
397 Vec (2) := Vec (1) rem Base;
398 Vec (1) := Vec (1) / Base;
402 Loc := Uints.Table (UI).Loc;
404 for J in 1 .. Uints.Table (UI).Length loop
405 Vec (J) := Udigits.Table (Loc + J - 1);
414 procedure Initialize is
419 Uint_Int_First := UI_From_Int (Int'First);
420 Uint_Int_Last := UI_From_Int (Int'Last);
422 UI_Power_2 (0) := Uint_1;
425 UI_Power_10 (0) := Uint_1;
426 UI_Power_10_Set := 0;
428 Uints_Min := Uints.Last;
429 Udigits_Min := Udigits.Last;
433 ---------------------
434 -- Least_Sig_Digit --
435 ---------------------
437 function Least_Sig_Digit (Arg : Uint) return Int is
442 V := Direct_Val (Arg);
448 -- Note that this result may be negative
455 (Uints.Table (Arg).Loc + Uints.Table (Arg).Length - 1);
463 function Mark return Save_Mark is
465 return (Save_Uint => Uints.Last, Save_Udigit => Udigits.Last);
468 -----------------------
469 -- Most_Sig_2_Digits --
470 -----------------------
472 procedure Most_Sig_2_Digits
479 pragma Assert (Left >= Right);
481 if Direct (Left) then
482 Left_Hat := Direct_Val (Left);
483 Right_Hat := Direct_Val (Right);
489 Udigits.Table (Uints.Table (Left).Loc);
491 Udigits.Table (Uints.Table (Left).Loc + 1);
494 -- It is not so clear what to return when Arg is negative???
496 Left_Hat := abs (L1) * Base + L2;
501 Length_L : constant Int := Uints.Table (Left).Length;
508 if Direct (Right) then
509 T := Direct_Val (Left);
510 R1 := abs (T / Base);
515 R1 := abs (Udigits.Table (Uints.Table (Right).Loc));
516 R2 := Udigits.Table (Uints.Table (Right).Loc + 1);
517 Length_R := Uints.Table (Right).Length;
520 if Length_L = Length_R then
521 Right_Hat := R1 * Base + R2;
522 elsif Length_L = Length_R + Int_1 then
528 end Most_Sig_2_Digits;
534 -- Note: N_Digits returns 1 for No_Uint
536 function N_Digits (Input : Uint) return Int is
538 if Direct (Input) then
539 if Direct_Val (Input) >= Base then
546 return Uints.Table (Input).Length;
554 function Num_Bits (Input : Uint) return Nat is
559 if UI_Is_In_Int_Range (Input) then
560 Num := UI_To_Int (Input);
564 Bits := Base_Bits * (Uints.Table (Input).Length - 1);
565 Num := abs (Udigits.Table (Uints.Table (Input).Loc));
568 while Types.">" (Num, 0) loop
580 procedure pid (Input : Uint) is
582 UI_Write (Input, Decimal);
590 procedure pih (Input : Uint) is
592 UI_Write (Input, Hex);
600 procedure Release (M : Save_Mark) is
602 Uints.Set_Last (Uint'Max (M.Save_Uint, Uints_Min));
603 Udigits.Set_Last (Int'Max (M.Save_Udigit, Udigits_Min));
606 ----------------------
607 -- Release_And_Save --
608 ----------------------
610 procedure Release_And_Save (M : Save_Mark; UI : in out Uint) is
617 UE_Len : Pos := Uints.Table (UI).Length;
618 UE_Loc : Int := Uints.Table (UI).Loc;
620 UD : Udigits.Table_Type (1 .. UE_Len) :=
621 Udigits.Table (UE_Loc .. UE_Loc + UE_Len - 1);
626 Uints.Increment_Last;
629 Uints.Table (UI) := (UE_Len, Udigits.Last + 1);
631 for J in 1 .. UE_Len loop
632 Udigits.Increment_Last;
633 Udigits.Table (Udigits.Last) := UD (J);
637 end Release_And_Save;
639 procedure Release_And_Save (M : Save_Mark; UI1, UI2 : in out Uint) is
642 Release_And_Save (M, UI2);
644 elsif Direct (UI2) then
645 Release_And_Save (M, UI1);
649 UE1_Len : Pos := Uints.Table (UI1).Length;
650 UE1_Loc : Int := Uints.Table (UI1).Loc;
652 UD1 : Udigits.Table_Type (1 .. UE1_Len) :=
653 Udigits.Table (UE1_Loc .. UE1_Loc + UE1_Len - 1);
655 UE2_Len : Pos := Uints.Table (UI2).Length;
656 UE2_Loc : Int := Uints.Table (UI2).Loc;
658 UD2 : Udigits.Table_Type (1 .. UE2_Len) :=
659 Udigits.Table (UE2_Loc .. UE2_Loc + UE2_Len - 1);
664 Uints.Increment_Last;
667 Uints.Table (UI1) := (UE1_Len, Udigits.Last + 1);
669 for J in 1 .. UE1_Len loop
670 Udigits.Increment_Last;
671 Udigits.Table (Udigits.Last) := UD1 (J);
674 Uints.Increment_Last;
677 Uints.Table (UI2) := (UE2_Len, Udigits.Last + 1);
679 for J in 1 .. UE2_Len loop
680 Udigits.Increment_Last;
681 Udigits.Table (Udigits.Last) := UD2 (J);
685 end Release_And_Save;
691 -- This is done in one pass
693 -- Mathematically: assume base congruent to 1 and compute an equivelent
696 -- If Sign = -1 return the alternating sum of the "digits".
698 -- D1 - D2 + D3 - D4 + D5 . . .
700 -- (where D1 is Least Significant Digit)
702 -- Mathematically: assume base congruent to -1 and compute an equivelent
705 -- This is used in Rem and Base is assumed to be 2 ** 15
707 -- Note: The next two functions are very similar, any style changes made
708 -- to one should be reflected in both. These would be simpler if we
709 -- worked base 2 ** 32.
711 function Sum_Digits (Left : Uint; Sign : Int) return Int is
713 pragma Assert (Sign = Int_1 or Sign = Int (-1));
715 -- First try simple case;
717 if Direct (Left) then
719 Tmp_Int : Int := Direct_Val (Left);
722 if Tmp_Int >= Base then
723 Tmp_Int := (Tmp_Int / Base) +
724 Sign * (Tmp_Int rem Base);
726 -- Now Tmp_Int is in [-(Base - 1) .. 2 * (Base - 1)]
728 if Tmp_Int >= Base then
732 Tmp_Int := (Tmp_Int / Base) + 1;
736 -- Now Tmp_Int is in [-(Base - 1) .. (Base - 1)]
743 -- Otherwise full circuit is needed
747 L_Length : Int := N_Digits (Left);
748 L_Vec : UI_Vector (1 .. L_Length);
754 Init_Operand (Left, L_Vec);
755 L_Vec (1) := abs L_Vec (1);
760 for J in reverse 1 .. L_Length loop
761 Tmp_Int := Tmp_Int + Alt * (L_Vec (J) + Carry);
763 -- Tmp_Int is now between [-2 * Base + 1 .. 2 * Base - 1],
764 -- since old Tmp_Int is between [-(Base - 1) .. Base - 1]
765 -- and L_Vec is in [0 .. Base - 1] and Carry in [-1 .. 1]
767 if Tmp_Int >= Base then
768 Tmp_Int := Tmp_Int - Base;
771 elsif Tmp_Int <= -Base then
772 Tmp_Int := Tmp_Int + Base;
779 -- Tmp_Int is now between [-Base + 1 .. Base - 1]
784 Tmp_Int := Tmp_Int + Alt * Carry;
786 -- Tmp_Int is now between [-Base .. Base]
788 if Tmp_Int >= Base then
789 Tmp_Int := Tmp_Int - Base + Alt * Sign * 1;
791 elsif Tmp_Int <= -Base then
792 Tmp_Int := Tmp_Int + Base + Alt * Sign * (-1);
795 -- Now Tmp_Int is in [-(Base - 1) .. (Base - 1)]
802 -----------------------
803 -- Sum_Double_Digits --
804 -----------------------
806 -- Note: This is used in Rem, Base is assumed to be 2 ** 15
808 function Sum_Double_Digits (Left : Uint; Sign : Int) return Int is
810 -- First try simple case;
812 pragma Assert (Sign = Int_1 or Sign = Int (-1));
814 if Direct (Left) then
815 return Direct_Val (Left);
817 -- Otherwise full circuit is needed
821 L_Length : Int := N_Digits (Left);
822 L_Vec : UI_Vector (1 .. L_Length);
830 Init_Operand (Left, L_Vec);
831 L_Vec (1) := abs L_Vec (1);
840 Least_Sig_Int := Least_Sig_Int + Alt * (L_Vec (J) + Carry);
842 -- Least is in [-2 Base + 1 .. 2 * Base - 1]
843 -- Since L_Vec in [0 .. Base - 1] and Carry in [-1 .. 1]
844 -- and old Least in [-Base + 1 .. Base - 1]
846 if Least_Sig_Int >= Base then
847 Least_Sig_Int := Least_Sig_Int - Base;
850 elsif Least_Sig_Int <= -Base then
851 Least_Sig_Int := Least_Sig_Int + Base;
858 -- Least is now in [-Base + 1 .. Base - 1]
860 Most_Sig_Int := Most_Sig_Int + Alt * (L_Vec (J - 1) + Carry);
862 -- Most is in [-2 Base + 1 .. 2 * Base - 1]
863 -- Since L_Vec in [0 .. Base - 1] and Carry in [-1 .. 1]
864 -- and old Most in [-Base + 1 .. Base - 1]
866 if Most_Sig_Int >= Base then
867 Most_Sig_Int := Most_Sig_Int - Base;
870 elsif Most_Sig_Int <= -Base then
871 Most_Sig_Int := Most_Sig_Int + Base;
877 -- Most is now in [-Base + 1 .. Base - 1]
884 Least_Sig_Int := Least_Sig_Int + Alt * (L_Vec (J) + Carry);
886 Least_Sig_Int := Least_Sig_Int + Alt * Carry;
889 if Least_Sig_Int >= Base then
890 Least_Sig_Int := Least_Sig_Int - Base;
891 Most_Sig_Int := Most_Sig_Int + Alt * 1;
893 elsif Least_Sig_Int <= -Base then
894 Least_Sig_Int := Least_Sig_Int + Base;
895 Most_Sig_Int := Most_Sig_Int + Alt * (-1);
898 if Most_Sig_Int >= Base then
899 Most_Sig_Int := Most_Sig_Int - Base;
902 Least_Sig_Int + Alt * 1; -- cannot overflow again
904 elsif Most_Sig_Int <= -Base then
905 Most_Sig_Int := Most_Sig_Int + Base;
908 Least_Sig_Int + Alt * (-1); -- cannot overflow again.
911 return Most_Sig_Int * Base + Least_Sig_Int;
914 end Sum_Double_Digits;
920 procedure Tree_Read is
925 Tree_Read_Int (Int (Uint_Int_First));
926 Tree_Read_Int (Int (Uint_Int_Last));
927 Tree_Read_Int (UI_Power_2_Set);
928 Tree_Read_Int (UI_Power_10_Set);
929 Tree_Read_Int (Int (Uints_Min));
930 Tree_Read_Int (Udigits_Min);
932 for J in 0 .. UI_Power_2_Set loop
933 Tree_Read_Int (Int (UI_Power_2 (J)));
936 for J in 0 .. UI_Power_10_Set loop
937 Tree_Read_Int (Int (UI_Power_10 (J)));
946 procedure Tree_Write is
951 Tree_Write_Int (Int (Uint_Int_First));
952 Tree_Write_Int (Int (Uint_Int_Last));
953 Tree_Write_Int (UI_Power_2_Set);
954 Tree_Write_Int (UI_Power_10_Set);
955 Tree_Write_Int (Int (Uints_Min));
956 Tree_Write_Int (Udigits_Min);
958 for J in 0 .. UI_Power_2_Set loop
959 Tree_Write_Int (Int (UI_Power_2 (J)));
962 for J in 0 .. UI_Power_10_Set loop
963 Tree_Write_Int (Int (UI_Power_10 (J)));
972 function UI_Abs (Right : Uint) return Uint is
974 if Right < Uint_0 then
985 function UI_Add (Left : Int; Right : Uint) return Uint is
987 return UI_Add (UI_From_Int (Left), Right);
990 function UI_Add (Left : Uint; Right : Int) return Uint is
992 return UI_Add (Left, UI_From_Int (Right));
995 function UI_Add (Left : Uint; Right : Uint) return Uint is
997 -- Simple cases of direct operands and addition of zero
999 if Direct (Left) then
1000 if Direct (Right) then
1001 return UI_From_Int (Direct_Val (Left) + Direct_Val (Right));
1003 elsif Int (Left) = Int (Uint_0) then
1007 elsif Direct (Right) and then Int (Right) = Int (Uint_0) then
1011 -- Otherwise full circuit is needed
1014 L_Length : Int := N_Digits (Left);
1015 R_Length : Int := N_Digits (Right);
1016 L_Vec : UI_Vector (1 .. L_Length);
1017 R_Vec : UI_Vector (1 .. R_Length);
1022 X_Bigger : Boolean := False;
1023 Y_Bigger : Boolean := False;
1024 Result_Neg : Boolean := False;
1027 Init_Operand (Left, L_Vec);
1028 Init_Operand (Right, R_Vec);
1030 -- At least one of the two operands is in multi-digit form.
1031 -- Calculate the number of digits sufficient to hold result.
1033 if L_Length > R_Length then
1034 Sum_Length := L_Length + 1;
1037 Sum_Length := R_Length + 1;
1038 if R_Length > L_Length then Y_Bigger := True; end if;
1041 -- Make copies of the absolute values of L_Vec and R_Vec into
1042 -- X and Y both with lengths equal to the maximum possibly
1043 -- needed. This makes looping over the digits much simpler.
1046 X : UI_Vector (1 .. Sum_Length);
1047 Y : UI_Vector (1 .. Sum_Length);
1048 Tmp_UI : UI_Vector (1 .. Sum_Length);
1051 for J in 1 .. Sum_Length - L_Length loop
1055 X (Sum_Length - L_Length + 1) := abs L_Vec (1);
1057 for J in 2 .. L_Length loop
1058 X (J + (Sum_Length - L_Length)) := L_Vec (J);
1061 for J in 1 .. Sum_Length - R_Length loop
1065 Y (Sum_Length - R_Length + 1) := abs R_Vec (1);
1067 for J in 2 .. R_Length loop
1068 Y (J + (Sum_Length - R_Length)) := R_Vec (J);
1071 if (L_Vec (1) < Int_0) = (R_Vec (1) < Int_0) then
1073 -- Same sign so just add
1076 for J in reverse 1 .. Sum_Length loop
1077 Tmp_Int := X (J) + Y (J) + Carry;
1079 if Tmp_Int >= Base then
1080 Tmp_Int := Tmp_Int - Base;
1089 return Vector_To_Uint (X, L_Vec (1) < Int_0);
1092 -- Find which one has bigger magnitude
1094 if not (X_Bigger or Y_Bigger) then
1095 for J in L_Vec'Range loop
1096 if abs L_Vec (J) > abs R_Vec (J) then
1099 elsif abs R_Vec (J) > abs L_Vec (J) then
1106 -- If they have identical magnitude, just return 0, else
1107 -- swap if necessary so that X had the bigger magnitude.
1108 -- Determine if result is negative at this time.
1110 Result_Neg := False;
1112 if not (X_Bigger or Y_Bigger) then
1116 if R_Vec (1) < Int_0 then
1125 if L_Vec (1) < Int_0 then
1130 -- Subtract Y from the bigger X
1134 for J in reverse 1 .. Sum_Length loop
1135 Tmp_Int := X (J) - Y (J) + Borrow;
1137 if Tmp_Int < Int_0 then
1138 Tmp_Int := Tmp_Int + Base;
1147 return Vector_To_Uint (X, Result_Neg);
1154 --------------------------
1155 -- UI_Decimal_Digits_Hi --
1156 --------------------------
1158 function UI_Decimal_Digits_Hi (U : Uint) return Nat is
1160 -- The maximum value of a "digit" is 32767, which is 5 decimal
1161 -- digits, so an N_Digit number could take up to 5 times this
1162 -- number of digits. This is certainly too high for large
1163 -- numbers but it is not worth worrying about.
1165 return 5 * N_Digits (U);
1166 end UI_Decimal_Digits_Hi;
1168 --------------------------
1169 -- UI_Decimal_Digits_Lo --
1170 --------------------------
1172 function UI_Decimal_Digits_Lo (U : Uint) return Nat is
1174 -- The maximum value of a "digit" is 32767, which is more than four
1175 -- decimal digits, but not a full five digits. The easily computed
1176 -- minimum number of decimal digits is thus 1 + 4 * the number of
1177 -- digits. This is certainly too low for large numbers but it is
1178 -- not worth worrying about.
1180 return 1 + 4 * (N_Digits (U) - 1);
1181 end UI_Decimal_Digits_Lo;
1187 function UI_Div (Left : Int; Right : Uint) return Uint is
1189 return UI_Div (UI_From_Int (Left), Right);
1192 function UI_Div (Left : Uint; Right : Int) return Uint is
1194 return UI_Div (Left, UI_From_Int (Right));
1197 function UI_Div (Left, Right : Uint) return Uint is
1199 pragma Assert (Right /= Uint_0);
1201 -- Cases where both operands are represented directly
1203 if Direct (Left) and then Direct (Right) then
1204 return UI_From_Int (Direct_Val (Left) / Direct_Val (Right));
1208 L_Length : constant Int := N_Digits (Left);
1209 R_Length : constant Int := N_Digits (Right);
1210 Q_Length : constant Int := L_Length - R_Length + 1;
1211 L_Vec : UI_Vector (1 .. L_Length);
1212 R_Vec : UI_Vector (1 .. R_Length);
1221 -- Result is zero if left operand is shorter than right
1223 if L_Length < R_Length then
1227 Init_Operand (Left, L_Vec);
1228 Init_Operand (Right, R_Vec);
1230 -- Case of right operand is single digit. Here we can simply divide
1231 -- each digit of the left operand by the divisor, from most to least
1232 -- significant, carrying the remainder to the next digit (just like
1233 -- ordinary long division by hand).
1235 if R_Length = Int_1 then
1237 Tmp_Divisor := abs R_Vec (1);
1240 Quotient : UI_Vector (1 .. L_Length);
1243 for J in L_Vec'Range loop
1244 Tmp_Int := Remainder * Base + abs L_Vec (J);
1245 Quotient (J) := Tmp_Int / Tmp_Divisor;
1246 Remainder := Tmp_Int rem Tmp_Divisor;
1251 (Quotient, (L_Vec (1) < Int_0 xor R_Vec (1) < Int_0));
1255 -- The possible simple cases have been exhausted. Now turn to the
1256 -- algorithm D from the section of Knuth mentioned at the top of
1259 Algorithm_D : declare
1260 Dividend : UI_Vector (1 .. L_Length + 1);
1261 Divisor : UI_Vector (1 .. R_Length);
1262 Quotient : UI_Vector (1 .. Q_Length);
1268 -- [ NORMALIZE ] (step D1 in the algorithm). First calculate the
1269 -- scale d, and then multiply Left and Right (u and v in the book)
1270 -- by d to get the dividend and divisor to work with.
1272 D := Base / (abs R_Vec (1) + 1);
1275 Dividend (2) := abs L_Vec (1);
1277 for J in 3 .. L_Length + Int_1 loop
1278 Dividend (J) := L_Vec (J - 1);
1281 Divisor (1) := abs R_Vec (1);
1283 for J in Int_2 .. R_Length loop
1284 Divisor (J) := R_Vec (J);
1289 -- Multiply Dividend by D
1292 for J in reverse Dividend'Range loop
1293 Tmp_Int := Dividend (J) * D + Carry;
1294 Dividend (J) := Tmp_Int rem Base;
1295 Carry := Tmp_Int / Base;
1298 -- Multiply Divisor by d.
1301 for J in reverse Divisor'Range loop
1302 Tmp_Int := Divisor (J) * D + Carry;
1303 Divisor (J) := Tmp_Int rem Base;
1304 Carry := Tmp_Int / Base;
1308 -- Main loop of long division algorithm.
1310 Divisor_Dig1 := Divisor (1);
1311 Divisor_Dig2 := Divisor (2);
1313 for J in Quotient'Range loop
1315 -- [ CALCULATE Q (hat) ] (step D3 in the algorithm).
1317 Tmp_Int := Dividend (J) * Base + Dividend (J + 1);
1321 if Dividend (J) = Divisor_Dig1 then
1322 Q_Guess := Base - 1;
1324 Q_Guess := Tmp_Int / Divisor_Dig1;
1329 while Divisor_Dig2 * Q_Guess >
1330 (Tmp_Int - Q_Guess * Divisor_Dig1) * Base +
1333 Q_Guess := Q_Guess - 1;
1336 -- [ MULTIPLY & SUBTRACT] (step D4). Q_Guess * Divisor is
1337 -- subtracted from the remaining dividend.
1340 for K in reverse Divisor'Range loop
1341 Tmp_Int := Dividend (J + K) - Q_Guess * Divisor (K) + Carry;
1342 Tmp_Dig := Tmp_Int rem Base;
1343 Carry := Tmp_Int / Base;
1345 if Tmp_Dig < Int_0 then
1346 Tmp_Dig := Tmp_Dig + Base;
1350 Dividend (J + K) := Tmp_Dig;
1353 Dividend (J) := Dividend (J) + Carry;
1355 -- [ TEST REMAINDER ] & [ ADD BACK ] (steps D5 and D6)
1356 -- Here there is a slight difference from the book: the last
1357 -- carry is always added in above and below (cancelling each
1358 -- other). In fact the dividend going negative is used as
1361 -- If the Dividend went negative, then Q_Guess was off by
1362 -- one, so it is decremented, and the divisor is added back
1363 -- into the relevant portion of the dividend.
1365 if Dividend (J) < Int_0 then
1366 Q_Guess := Q_Guess - 1;
1369 for K in reverse Divisor'Range loop
1370 Tmp_Int := Dividend (J + K) + Divisor (K) + Carry;
1372 if Tmp_Int >= Base then
1373 Tmp_Int := Tmp_Int - Base;
1379 Dividend (J + K) := Tmp_Int;
1382 Dividend (J) := Dividend (J) + Carry;
1385 -- Finally we can get the next quotient digit
1387 Quotient (J) := Q_Guess;
1390 return Vector_To_Uint
1391 (Quotient, (L_Vec (1) < Int_0 xor R_Vec (1) < Int_0));
1401 function UI_Eq (Left : Int; Right : Uint) return Boolean is
1403 return not UI_Ne (UI_From_Int (Left), Right);
1406 function UI_Eq (Left : Uint; Right : Int) return Boolean is
1408 return not UI_Ne (Left, UI_From_Int (Right));
1411 function UI_Eq (Left : Uint; Right : Uint) return Boolean is
1413 return not UI_Ne (Left, Right);
1420 function UI_Expon (Left : Int; Right : Uint) return Uint is
1422 return UI_Expon (UI_From_Int (Left), Right);
1425 function UI_Expon (Left : Uint; Right : Int) return Uint is
1427 return UI_Expon (Left, UI_From_Int (Right));
1430 function UI_Expon (Left : Int; Right : Int) return Uint is
1432 return UI_Expon (UI_From_Int (Left), UI_From_Int (Right));
1435 function UI_Expon (Left : Uint; Right : Uint) return Uint is
1437 pragma Assert (Right >= Uint_0);
1439 -- Any value raised to power of 0 is 1
1441 if Right = Uint_0 then
1444 -- 0 to any positive power is 0.
1446 elsif Left = Uint_0 then
1449 -- 1 to any power is 1
1451 elsif Left = Uint_1 then
1454 -- Any value raised to power of 1 is that value
1456 elsif Right = Uint_1 then
1459 -- Cases which can be done by table lookup
1461 elsif Right <= Uint_64 then
1463 -- 2 ** N for N in 2 .. 64
1465 if Left = Uint_2 then
1467 Right_Int : constant Int := Direct_Val (Right);
1470 if Right_Int > UI_Power_2_Set then
1471 for J in UI_Power_2_Set + Int_1 .. Right_Int loop
1472 UI_Power_2 (J) := UI_Power_2 (J - Int_1) * Int_2;
1473 Uints_Min := Uints.Last;
1474 Udigits_Min := Udigits.Last;
1477 UI_Power_2_Set := Right_Int;
1480 return UI_Power_2 (Right_Int);
1483 -- 10 ** N for N in 2 .. 64
1485 elsif Left = Uint_10 then
1487 Right_Int : constant Int := Direct_Val (Right);
1490 if Right_Int > UI_Power_10_Set then
1491 for J in UI_Power_10_Set + Int_1 .. Right_Int loop
1492 UI_Power_10 (J) := UI_Power_10 (J - Int_1) * Int (10);
1493 Uints_Min := Uints.Last;
1494 Udigits_Min := Udigits.Last;
1497 UI_Power_10_Set := Right_Int;
1500 return UI_Power_10 (Right_Int);
1505 -- If we fall through, then we have the general case (see Knuth 4.6.3)
1509 Squares : Uint := Left;
1510 Result : Uint := Uint_1;
1511 M : constant Uintp.Save_Mark := Uintp.Mark;
1515 if (Least_Sig_Digit (N) mod Int_2) = Int_1 then
1516 Result := Result * Squares;
1520 exit when N = Uint_0;
1521 Squares := Squares * Squares;
1524 Uintp.Release_And_Save (M, Result);
1533 function UI_From_Dint (Input : Dint) return Uint is
1536 if Dint (Min_Direct) <= Input and then Input <= Dint (Max_Direct) then
1537 return Uint (Dint (Uint_Direct_Bias) + Input);
1539 -- For values of larger magnitude, compute digits into a vector and
1540 -- call Vector_To_Uint.
1544 Max_For_Dint : constant := 5;
1545 -- Base is defined so that 5 Uint digits is sufficient
1546 -- to hold the largest possible Dint value.
1548 V : UI_Vector (1 .. Max_For_Dint);
1550 Temp_Integer : Dint;
1553 for J in V'Range loop
1557 Temp_Integer := Input;
1559 for J in reverse V'Range loop
1560 V (J) := Int (abs (Temp_Integer rem Dint (Base)));
1561 Temp_Integer := Temp_Integer / Dint (Base);
1564 return Vector_To_Uint (V, Input < Dint'(0));
1573 function UI_From_Int (Input : Int) return Uint is
1576 if Min_Direct <= Input and then Input <= Max_Direct then
1577 return Uint (Int (Uint_Direct_Bias) + Input);
1579 -- For values of larger magnitude, compute digits into a vector and
1580 -- call Vector_To_Uint.
1584 Max_For_Int : constant := 3;
1585 -- Base is defined so that 3 Uint digits is sufficient
1586 -- to hold the largest possible Int value.
1588 V : UI_Vector (1 .. Max_For_Int);
1593 for J in V'Range loop
1597 Temp_Integer := Input;
1599 for J in reverse V'Range loop
1600 V (J) := abs (Temp_Integer rem Base);
1601 Temp_Integer := Temp_Integer / Base;
1604 return Vector_To_Uint (V, Input < Int_0);
1613 -- Lehmer's algorithm for GCD.
1615 -- The idea is to avoid using multiple precision arithmetic wherever
1616 -- possible, substituting Int arithmetic instead. See Knuth volume II,
1617 -- Algorithm L (page 329).
1619 -- We use the same notation as Knuth (U_Hat standing for the obvious!)
1621 function UI_GCD (Uin, Vin : Uint) return Uint is
1623 -- Copies of Uin and Vin
1626 -- The most Significant digits of U,V
1628 A, B, C, D, T, Q, Den1, Den2 : Int;
1631 Marks : constant Uintp.Save_Mark := Uintp.Mark;
1632 Iterations : Integer := 0;
1635 pragma Assert (Uin >= Vin);
1636 pragma Assert (Vin >= Uint_0);
1642 Iterations := Iterations + 1;
1649 UI_From_Int (GCD (Direct_Val (V), UI_To_Int (U rem V)));
1653 Most_Sig_2_Digits (U, V, U_Hat, V_Hat);
1660 -- We might overflow and get division by zero here. This just
1661 -- means we can not take the single precision step
1665 exit when (Den1 * Den2) = Int_0;
1667 -- Compute Q, the trial quotient
1669 Q := (U_Hat + A) / Den1;
1671 exit when Q /= ((U_Hat + B) / Den2);
1673 -- A single precision step Euclid step will give same answer as
1674 -- a multiprecision one.
1684 T := U_Hat - (Q * V_Hat);
1690 -- Take a multiprecision Euclid step
1694 -- No single precision steps take a regular Euclid step.
1701 -- Use prior single precision steps to compute this Euclid step.
1703 -- Fixed bug 1415-008 spends 80% of its time working on this
1704 -- step. Perhaps we need a special case Int / Uint dot
1705 -- product to speed things up. ???
1707 -- Alternatively we could increase the single precision
1708 -- iterations to handle Uint's of some small size ( <5
1709 -- digits?). Then we would have more iterations on small Uint.
1710 -- Fixed bug 1415-008 only gets 5 (on average) single
1711 -- precision iterations per large iteration. ???
1713 Tmp_UI := (UI_From_Int (A) * U) + (UI_From_Int (B) * V);
1714 V := (UI_From_Int (C) * U) + (UI_From_Int (D) * V);
1718 -- If the operands are very different in magnitude, the loop
1719 -- will generate large amounts of short-lived data, which it is
1720 -- worth removing periodically.
1722 if Iterations > 100 then
1723 Release_And_Save (Marks, U, V);
1733 function UI_Ge (Left : Int; Right : Uint) return Boolean is
1735 return not UI_Lt (UI_From_Int (Left), Right);
1738 function UI_Ge (Left : Uint; Right : Int) return Boolean is
1740 return not UI_Lt (Left, UI_From_Int (Right));
1743 function UI_Ge (Left : Uint; Right : Uint) return Boolean is
1745 return not UI_Lt (Left, Right);
1752 function UI_Gt (Left : Int; Right : Uint) return Boolean is
1754 return UI_Lt (Right, UI_From_Int (Left));
1757 function UI_Gt (Left : Uint; Right : Int) return Boolean is
1759 return UI_Lt (UI_From_Int (Right), Left);
1762 function UI_Gt (Left : Uint; Right : Uint) return Boolean is
1764 return UI_Lt (Right, Left);
1771 procedure UI_Image (Input : Uint; Format : UI_Format := Auto) is
1773 Image_Out (Input, True, Format);
1776 -------------------------
1777 -- UI_Is_In_Int_Range --
1778 -------------------------
1780 function UI_Is_In_Int_Range (Input : Uint) return Boolean is
1782 -- Make sure we don't get called before Initialize
1784 pragma Assert (Uint_Int_First /= Uint_0);
1786 if Direct (Input) then
1789 return Input >= Uint_Int_First
1790 and then Input <= Uint_Int_Last;
1792 end UI_Is_In_Int_Range;
1798 function UI_Le (Left : Int; Right : Uint) return Boolean is
1800 return not UI_Lt (Right, UI_From_Int (Left));
1803 function UI_Le (Left : Uint; Right : Int) return Boolean is
1805 return not UI_Lt (UI_From_Int (Right), Left);
1808 function UI_Le (Left : Uint; Right : Uint) return Boolean is
1810 return not UI_Lt (Right, Left);
1817 function UI_Lt (Left : Int; Right : Uint) return Boolean is
1819 return UI_Lt (UI_From_Int (Left), Right);
1822 function UI_Lt (Left : Uint; Right : Int) return Boolean is
1824 return UI_Lt (Left, UI_From_Int (Right));
1827 function UI_Lt (Left : Uint; Right : Uint) return Boolean is
1829 -- Quick processing for identical arguments
1831 if Int (Left) = Int (Right) then
1834 -- Quick processing for both arguments directly represented
1836 elsif Direct (Left) and then Direct (Right) then
1837 return Int (Left) < Int (Right);
1839 -- At least one argument is more than one digit long
1843 L_Length : constant Int := N_Digits (Left);
1844 R_Length : constant Int := N_Digits (Right);
1846 L_Vec : UI_Vector (1 .. L_Length);
1847 R_Vec : UI_Vector (1 .. R_Length);
1850 Init_Operand (Left, L_Vec);
1851 Init_Operand (Right, R_Vec);
1853 if L_Vec (1) < Int_0 then
1855 -- First argument negative, second argument non-negative
1857 if R_Vec (1) >= Int_0 then
1860 -- Both arguments negative
1863 if L_Length /= R_Length then
1864 return L_Length > R_Length;
1866 elsif L_Vec (1) /= R_Vec (1) then
1867 return L_Vec (1) < R_Vec (1);
1870 for J in 2 .. L_Vec'Last loop
1871 if L_Vec (J) /= R_Vec (J) then
1872 return L_Vec (J) > R_Vec (J);
1881 -- First argument non-negative, second argument negative
1883 if R_Vec (1) < Int_0 then
1886 -- Both arguments non-negative
1889 if L_Length /= R_Length then
1890 return L_Length < R_Length;
1892 for J in L_Vec'Range loop
1893 if L_Vec (J) /= R_Vec (J) then
1894 return L_Vec (J) < R_Vec (J);
1910 function UI_Max (Left : Int; Right : Uint) return Uint is
1912 return UI_Max (UI_From_Int (Left), Right);
1915 function UI_Max (Left : Uint; Right : Int) return Uint is
1917 return UI_Max (Left, UI_From_Int (Right));
1920 function UI_Max (Left : Uint; Right : Uint) return Uint is
1922 if Left >= Right then
1933 function UI_Min (Left : Int; Right : Uint) return Uint is
1935 return UI_Min (UI_From_Int (Left), Right);
1938 function UI_Min (Left : Uint; Right : Int) return Uint is
1940 return UI_Min (Left, UI_From_Int (Right));
1943 function UI_Min (Left : Uint; Right : Uint) return Uint is
1945 if Left <= Right then
1956 function UI_Mod (Left : Int; Right : Uint) return Uint is
1958 return UI_Mod (UI_From_Int (Left), Right);
1961 function UI_Mod (Left : Uint; Right : Int) return Uint is
1963 return UI_Mod (Left, UI_From_Int (Right));
1966 function UI_Mod (Left : Uint; Right : Uint) return Uint is
1967 Urem : constant Uint := Left rem Right;
1970 if (Left < Uint_0) = (Right < Uint_0)
1971 or else Urem = Uint_0
1975 return Right + Urem;
1983 function UI_Mul (Left : Int; Right : Uint) return Uint is
1985 return UI_Mul (UI_From_Int (Left), Right);
1988 function UI_Mul (Left : Uint; Right : Int) return Uint is
1990 return UI_Mul (Left, UI_From_Int (Right));
1993 function UI_Mul (Left : Uint; Right : Uint) return Uint is
1995 -- Simple case of single length operands
1997 if Direct (Left) and then Direct (Right) then
2000 (Dint (Direct_Val (Left)) * Dint (Direct_Val (Right)));
2003 -- Otherwise we have the general case (Algorithm M in Knuth)
2006 L_Length : constant Int := N_Digits (Left);
2007 R_Length : constant Int := N_Digits (Right);
2008 L_Vec : UI_Vector (1 .. L_Length);
2009 R_Vec : UI_Vector (1 .. R_Length);
2013 Init_Operand (Left, L_Vec);
2014 Init_Operand (Right, R_Vec);
2015 Neg := (L_Vec (1) < Int_0) xor (R_Vec (1) < Int_0);
2016 L_Vec (1) := abs (L_Vec (1));
2017 R_Vec (1) := abs (R_Vec (1));
2019 Algorithm_M : declare
2020 Product : UI_Vector (1 .. L_Length + R_Length);
2025 for J in Product'Range loop
2029 for J in reverse R_Vec'Range loop
2031 for K in reverse L_Vec'Range loop
2033 L_Vec (K) * R_Vec (J) + Product (J + K) + Carry;
2034 Product (J + K) := Tmp_Sum rem Base;
2035 Carry := Tmp_Sum / Base;
2038 Product (J) := Carry;
2041 return Vector_To_Uint (Product, Neg);
2050 function UI_Ne (Left : Int; Right : Uint) return Boolean is
2052 return UI_Ne (UI_From_Int (Left), Right);
2055 function UI_Ne (Left : Uint; Right : Int) return Boolean is
2057 return UI_Ne (Left, UI_From_Int (Right));
2060 function UI_Ne (Left : Uint; Right : Uint) return Boolean is
2062 -- Quick processing for identical arguments. Note that this takes
2063 -- care of the case of two No_Uint arguments.
2065 if Int (Left) = Int (Right) then
2069 -- See if left operand directly represented
2071 if Direct (Left) then
2073 -- If right operand directly represented then compare
2075 if Direct (Right) then
2076 return Int (Left) /= Int (Right);
2078 -- Left operand directly represented, right not, must be unequal
2084 -- Right operand directly represented, left not, must be unequal
2086 elsif Direct (Right) then
2090 -- Otherwise both multi-word, do comparison
2093 Size : constant Int := N_Digits (Left);
2098 if Size /= N_Digits (Right) then
2102 Left_Loc := Uints.Table (Left).Loc;
2103 Right_Loc := Uints.Table (Right).Loc;
2105 for J in Int_0 .. Size - Int_1 loop
2106 if Udigits.Table (Left_Loc + J) /=
2107 Udigits.Table (Right_Loc + J)
2121 function UI_Negate (Right : Uint) return Uint is
2123 -- Case where input is directly represented. Note that since the
2124 -- range of Direct values is non-symmetrical, the result may not
2125 -- be directly represented, this is taken care of in UI_From_Int.
2127 if Direct (Right) then
2128 return UI_From_Int (-Direct_Val (Right));
2130 -- Full processing for multi-digit case. Note that we cannot just
2131 -- copy the value to the end of the table negating the first digit,
2132 -- since the range of Direct values is non-symmetrical, so we can
2133 -- have a negative value that is not Direct whose negation can be
2134 -- represented directly.
2138 R_Length : constant Int := N_Digits (Right);
2139 R_Vec : UI_Vector (1 .. R_Length);
2143 Init_Operand (Right, R_Vec);
2144 Neg := R_Vec (1) > Int_0;
2145 R_Vec (1) := abs R_Vec (1);
2146 return Vector_To_Uint (R_Vec, Neg);
2155 function UI_Rem (Left : Int; Right : Uint) return Uint is
2157 return UI_Rem (UI_From_Int (Left), Right);
2160 function UI_Rem (Left : Uint; Right : Int) return Uint is
2162 return UI_Rem (Left, UI_From_Int (Right));
2165 function UI_Rem (Left, Right : Uint) return Uint is
2169 subtype Int1_12 is Integer range 1 .. 12;
2172 pragma Assert (Right /= Uint_0);
2174 if Direct (Right) then
2175 if Direct (Left) then
2176 return UI_From_Int (Direct_Val (Left) rem Direct_Val (Right));
2179 -- Special cases when Right is less than 13 and Left is larger
2180 -- larger than one digit. All of these algorithms depend on the
2181 -- base being 2 ** 15 We work with Abs (Left) and Abs(Right)
2182 -- then multiply result by Sign (Left)
2184 if (Right <= Uint_12) and then (Right >= Uint_Minus_12) then
2186 if (Left < Uint_0) then
2192 -- All cases are listed, grouped by mathematical method
2193 -- It is not inefficient to do have this case list out
2194 -- of order since GCC sorts the cases we list.
2196 case Int1_12 (abs (Direct_Val (Right))) is
2201 -- Powers of two are simple AND's with LS Left Digit
2202 -- GCC will recognise these constants as powers of 2
2203 -- and replace the rem with simpler operations where
2206 -- Least_Sig_Digit might return Negative numbers.
2209 return UI_From_Int (
2210 Sign * (Least_Sig_Digit (Left) mod 2));
2213 return UI_From_Int (
2214 Sign * (Least_Sig_Digit (Left) mod 4));
2217 return UI_From_Int (
2218 Sign * (Least_Sig_Digit (Left) mod 8));
2220 -- Some number theoretical tricks:
2222 -- If B Rem Right = 1 then
2223 -- Left Rem Right = Sum_Of_Digits_Base_B (Left) Rem Right
2225 -- Note: 2^32 mod 3 = 1
2228 return UI_From_Int (
2229 Sign * (Sum_Double_Digits (Left, 1) rem Int (3)));
2231 -- Note: 2^15 mod 7 = 1
2234 return UI_From_Int (
2235 Sign * (Sum_Digits (Left, 1) rem Int (7)));
2237 -- Note: 2^32 mod 5 = -1
2238 -- Alternating sums might be negative, but rem is always
2239 -- positive hence we must use mod here.
2242 Tmp := Sum_Double_Digits (Left, -1) mod Int (5);
2243 return UI_From_Int (Sign * Tmp);
2245 -- Note: 2^15 mod 9 = -1
2246 -- Alternating sums might be negative, but rem is always
2247 -- positive hence we must use mod here.
2250 Tmp := Sum_Digits (Left, -1) mod Int (9);
2251 return UI_From_Int (Sign * Tmp);
2253 -- Note: 2^15 mod 11 = -1
2254 -- Alternating sums might be negative, but rem is always
2255 -- positive hence we must use mod here.
2258 Tmp := Sum_Digits (Left, -1) mod Int (11);
2259 return UI_From_Int (Sign * Tmp);
2261 -- Now resort to Chinese Remainder theorem
2262 -- to reduce 6, 10, 12 to previous special cases
2264 -- There is no reason we could not add more cases
2265 -- like these if it proves useful.
2267 -- Perhaps we should go up to 16, however
2268 -- I have no "trick" for 13.
2270 -- To find u mod m we:
2272 -- GCD(m1, m2) = 1 AND m = (m1 * m2).
2273 -- Next we pick (Basis) M1, M2 small S.T.
2274 -- (M1 mod m1) = (M2 mod m2) = 1 AND
2275 -- (M1 mod m2) = (M2 mod m1) = 0
2277 -- So u mod m = (u1 * M1 + u2 * M2) mod m
2278 -- Where u1 = (u mod m1) AND u2 = (u mod m2);
2279 -- Under typical circumstances the last mod m
2280 -- can be done with a (possible) single subtraction.
2282 -- m1 = 2; m2 = 3; M1 = 3; M2 = 4;
2285 Tmp := 3 * (Least_Sig_Digit (Left) rem 2) +
2286 4 * (Sum_Double_Digits (Left, 1) rem 3);
2287 return UI_From_Int (Sign * (Tmp rem 6));
2289 -- m1 = 2; m2 = 5; M1 = 5; M2 = 6;
2292 Tmp := 5 * (Least_Sig_Digit (Left) rem 2) +
2293 6 * (Sum_Double_Digits (Left, -1) mod 5);
2294 return UI_From_Int (Sign * (Tmp rem 10));
2296 -- m1 = 3; m2 = 4; M1 = 4; M2 = 9;
2299 Tmp := 4 * (Sum_Double_Digits (Left, 1) rem 3) +
2300 9 * (Least_Sig_Digit (Left) rem 4);
2301 return UI_From_Int (Sign * (Tmp rem 12));
2306 -- Else fall through to general case.
2308 -- ???This needs to be improved. We have the Rem when we do the
2309 -- Div. Div throws it away!
2311 -- The special case Length (Left) = Length(right) = 1 in Div
2312 -- looks slow. It uses UI_To_Int when Int should suffice. ???
2316 return Left - (Left / Right) * Right;
2323 function UI_Sub (Left : Int; Right : Uint) return Uint is
2325 return UI_Add (Left, -Right);
2328 function UI_Sub (Left : Uint; Right : Int) return Uint is
2330 return UI_Add (Left, -Right);
2333 function UI_Sub (Left : Uint; Right : Uint) return Uint is
2335 if Direct (Left) and then Direct (Right) then
2336 return UI_From_Int (Direct_Val (Left) - Direct_Val (Right));
2338 return UI_Add (Left, -Right);
2346 function UI_To_Int (Input : Uint) return Int is
2348 if Direct (Input) then
2349 return Direct_Val (Input);
2351 -- Case of input is more than one digit
2355 In_Length : constant Int := N_Digits (Input);
2356 In_Vec : UI_Vector (1 .. In_Length);
2360 -- Uints of more than one digit could be outside the range for
2361 -- Ints. Caller should have checked for this if not certain.
2362 -- Fatal error to attempt to convert from value outside Int'Range.
2364 pragma Assert (UI_Is_In_Int_Range (Input));
2366 -- Otherwise, proceed ahead, we are OK
2368 Init_Operand (Input, In_Vec);
2371 -- Calculate -|Input| and then negates if value is positive.
2372 -- This handles our current definition of Int (based on
2373 -- 2s complement). Is it secure enough?
2375 for Idx in In_Vec'Range loop
2376 Ret_Int := Ret_Int * Base - abs In_Vec (Idx);
2379 if In_Vec (1) < Int_0 then
2392 procedure UI_Write (Input : Uint; Format : UI_Format := Auto) is
2394 Image_Out (Input, False, Format);
2397 ---------------------
2398 -- Vector_To_Uint --
2399 ---------------------
2401 function Vector_To_Uint
2402 (In_Vec : UI_Vector;
2410 -- The vector can contain leading zeros. These are not stored in the
2411 -- table, so loop through the vector looking for first non-zero digit
2413 for J in In_Vec'Range loop
2414 if In_Vec (J) /= Int_0 then
2416 -- The length of the value is the length of the rest of the vector
2418 Size := In_Vec'Last - J + 1;
2420 -- One digit value can always be represented directly
2422 if Size = Int_1 then
2424 return Uint (Int (Uint_Direct_Bias) - In_Vec (J));
2426 return Uint (Int (Uint_Direct_Bias) + In_Vec (J));
2429 -- Positive two digit values may be in direct representation range
2431 elsif Size = Int_2 and then not Negative then
2432 Val := In_Vec (J) * Base + In_Vec (J + 1);
2434 if Val <= Max_Direct then
2435 return Uint (Int (Uint_Direct_Bias) + Val);
2439 -- The value is outside the direct representation range and
2440 -- must therefore be stored in the table. Expand the table
2441 -- to contain the count and tigis. The index of the new table
2442 -- entry will be returned as the result.
2444 Uints.Increment_Last;
2445 Uints.Table (Uints.Last).Length := Size;
2446 Uints.Table (Uints.Last).Loc := Udigits.Last + 1;
2448 Udigits.Increment_Last;
2451 Udigits.Table (Udigits.Last) := -In_Vec (J);
2453 Udigits.Table (Udigits.Last) := +In_Vec (J);
2456 for K in 2 .. Size loop
2457 Udigits.Increment_Last;
2458 Udigits.Table (Udigits.Last) := In_Vec (J + K - 1);
2465 -- Dropped through loop only if vector contained all zeros