1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
11 -- Copyright (C) 1992-2001 Free Software Foundation, Inc. --
13 -- GNAT is free software; you can redistribute it and/or modify it under --
14 -- terms of the GNU General Public License as published by the Free Soft- --
15 -- ware Foundation; either version 2, or (at your option) any later ver- --
16 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
17 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
18 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
19 -- for more details. You should have received a copy of the GNU General --
20 -- Public License distributed with GNAT; see file COPYING. If not, write --
21 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
22 -- MA 02111-1307, USA. --
24 -- As a special exception, if other files instantiate generics from this --
25 -- unit, or you link this unit with other files to produce an executable, --
26 -- this unit does not by itself cause the resulting executable to be --
27 -- covered by the GNU General Public License. This exception does not --
28 -- however invalidate any other reasons why the executable file might be --
29 -- covered by the GNU Public License. --
31 -- GNAT was originally developed by the GNAT team at New York University. --
32 -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
34 ------------------------------------------------------------------------------
36 with Output
; use Output
;
37 with Tree_IO
; use Tree_IO
;
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
50 -- value, 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
74 -- does not destroy values saved in the U_Power tables. Whenever an
75 -- entry is made in the U_Power tables, Uints_Min and Udigits_Min are
76 -- updated to protect the entry, and Release never cuts back beyond
77 -- 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 -- Local Subprograms --
87 -----------------------
89 function Direct
(U
: Uint
) return Boolean;
90 pragma Inline
(Direct
);
91 -- Returns True if U is represented directly
93 function Direct_Val
(U
: Uint
) return Int
;
94 -- U is a Uint for is represented directly. The returned result
95 -- is the value represented.
97 function GCD
(Jin
, Kin
: Int
) return Int
;
98 -- Compute GCD of two integers. Assumes that Jin >= Kin >= 0
104 -- Common processing for UI_Image and UI_Write, To_Buffer is set
105 -- True for UI_Image, and false for UI_Write, and Format is copied
106 -- from the Format parameter to UI_Image or UI_Write.
108 procedure Init_Operand
(UI
: Uint
; Vec
: out UI_Vector
);
109 pragma Inline
(Init_Operand
);
110 -- This procedure puts the value of UI into the vector in canonical
111 -- multiple precision format. The parameter should be of the correct
112 -- size as determined by a previous call to N_Digits (UI). The first
113 -- digit of Vec contains the sign, all other digits are always non-
114 -- negative. Note that the input may be directly represented, and in
115 -- this case Vec will contain the corresponding one or two digit value.
117 function Least_Sig_Digit
(Arg
: Uint
) return Int
;
118 pragma Inline
(Least_Sig_Digit
);
119 -- Returns the Least Significant Digit of Arg quickly. When the given
120 -- Uint is less than 2**15, the value returned is the input value, in
121 -- this case the result may be negative. It is expected that any use
122 -- will mask off unnecessary bits. This is used for finding Arg mod B
123 -- where B is a power of two. Hence the actual base is irrelevent as
124 -- long as it is a power of two.
126 procedure Most_Sig_2_Digits
130 Right_Hat
: out Int
);
131 -- Returns leading two significant digits from the given pair of Uint's.
132 -- Mathematically: returns Left / (Base ** K) and Right / (Base ** K)
133 -- where K is as small as possible S.T. Right_Hat < Base * Base.
134 -- It is required that Left > Right for the algorithm to work.
136 function N_Digits
(Input
: Uint
) return Int
;
137 pragma Inline
(N_Digits
);
138 -- Returns number of "digits" in a Uint
140 function Sum_Digits
(Left
: Uint
; Sign
: Int
) return Int
;
141 -- If Sign = 1 return the sum of the "digits" of Abs (Left). If the
142 -- total has more then one digit then return Sum_Digits of total.
144 function Sum_Double_Digits
(Left
: Uint
; Sign
: Int
) return Int
;
145 -- Same as above but work in New_Base = Base * Base
147 function Vector_To_Uint
151 -- Functions that calculate values in UI_Vectors, call this function
152 -- to create and return the Uint value. In_Vec contains the multiple
153 -- precision (Base) representation of a non-negative value. Leading
154 -- zeroes are permitted. Negative is set if the desired result is
155 -- the negative of the given value. The result will be either the
156 -- appropriate directly represented value, or a table entry in the
157 -- proper canonical format is created and returned.
159 -- Note that Init_Operand puts a signed value in the result vector,
160 -- but Vector_To_Uint is always presented with a non-negative value.
161 -- The processing of signs is something that is done by the caller
162 -- before calling Vector_To_Uint.
168 function Direct
(U
: Uint
) return Boolean is
170 return Int
(U
) <= Int
(Uint_Direct_Last
);
177 function Direct_Val
(U
: Uint
) return Int
is
179 pragma Assert
(Direct
(U
));
180 return Int
(U
) - Int
(Uint_Direct_Bias
);
187 function GCD
(Jin
, Kin
: Int
) return Int
is
191 pragma Assert
(Jin
>= Kin
);
192 pragma Assert
(Kin
>= Int_0
);
197 while K
/= Uint_0
loop
215 Marks
: constant Uintp
.Save_Mark
:= Uintp
.Mark
;
219 Digs_Output
: Natural := 0;
220 -- Counts digits output. In hex mode, but not in decimal mode, we
221 -- put an underline after every four hex digits that are output.
223 Exponent
: Natural := 0;
224 -- If the number is too long to fit in the buffer, we switch to an
225 -- approximate output format with an exponent. This variable records
226 -- the exponent value.
228 function Better_In_Hex
return Boolean;
229 -- Determines if it is better to generate digits in base 16 (result
230 -- is true) or base 10 (result is false). The choice is purely a
231 -- matter of convenience and aesthetics, so it does not matter which
232 -- value is returned from a correctness point of view.
234 procedure Image_Char
(C
: Character);
235 -- Internal procedure to output one character
237 procedure Image_Exponent
(N
: Natural);
238 -- Output non-zero exponent. Note that we only use the exponent
239 -- form in the buffer case, so we know that To_Buffer is true.
241 procedure Image_Uint
(U
: Uint
);
242 -- Internal procedure to output characters of non-negative Uint
248 function Better_In_Hex
return Boolean is
249 T16
: constant Uint
:= Uint_2
** Int
'(16);
255 -- Small values up to 2**16 can always be in decimal
261 -- Otherwise, see if we are a power of 2 or one less than a power
262 -- of 2. For the moment these are the only cases printed in hex.
264 if A mod Uint_2 = Uint_1 then
269 if A mod T16 /= Uint_0 then
279 while A > Uint_2 loop
280 if A mod Uint_2 /= Uint_0 then
295 procedure Image_Char (C : Character) is
298 if UI_Image_Length + 6 > UI_Image_Max then
299 Exponent := Exponent + 1;
301 UI_Image_Length := UI_Image_Length + 1;
302 UI_Image_Buffer (UI_Image_Length) := C;
313 procedure Image_Exponent (N : Natural) is
316 Image_Exponent (N / 10);
319 UI_Image_Length := UI_Image_Length + 1;
320 UI_Image_Buffer (UI_Image_Length) :=
321 Character'Val (Character'Pos ('0') + N mod 10);
328 procedure Image_Uint (U : Uint) is
329 H : array (Int range 0 .. 15) of Character := "0123456789ABCDEF";
333 Image_Uint (U / Base);
336 if Digs_Output = 4 and then Base = Uint_16 then
341 Image_Char (H (UI_To_Int (U rem Base)));
343 Digs_Output := Digs_Output + 1;
346 -- Start of processing for Image_Out
349 if Input = No_Uint then
354 UI_Image_Length := 0;
356 if Input < Uint_0 then
364 or else (Format = Auto and then Better_In_Hex)
378 if Exponent /= 0 then
379 UI_Image_Length := UI_Image_Length + 1;
380 UI_Image_Buffer (UI_Image_Length) := 'E
';
381 Image_Exponent (Exponent);
384 Uintp.Release (Marks);
391 procedure Init_Operand (UI : Uint; Vec : out UI_Vector) is
396 Vec (1) := Direct_Val (UI);
398 if Vec (1) >= Base then
399 Vec (2) := Vec (1) rem Base;
400 Vec (1) := Vec (1) / Base;
404 Loc := Uints.Table (UI).Loc;
406 for J in 1 .. Uints.Table (UI).Length loop
407 Vec (J) := Udigits.Table (Loc + J - 1);
416 procedure Initialize is
421 Uint_Int_First := UI_From_Int (Int'First);
422 Uint_Int_Last := UI_From_Int (Int'Last);
424 UI_Power_2 (0) := Uint_1;
427 UI_Power_10 (0) := Uint_1;
428 UI_Power_10_Set := 0;
430 Uints_Min := Uints.Last;
431 Udigits_Min := Udigits.Last;
435 ---------------------
436 -- Least_Sig_Digit --
437 ---------------------
439 function Least_Sig_Digit (Arg : Uint) return Int is
444 V := Direct_Val (Arg);
450 -- Note that this result may be negative
457 (Uints.Table (Arg).Loc + Uints.Table (Arg).Length - 1);
465 function Mark return Save_Mark is
467 return (Save_Uint => Uints.Last, Save_Udigit => Udigits.Last);
470 -----------------------
471 -- Most_Sig_2_Digits --
472 -----------------------
474 procedure Most_Sig_2_Digits
481 pragma Assert (Left >= Right);
483 if Direct (Left) then
484 Left_Hat := Direct_Val (Left);
485 Right_Hat := Direct_Val (Right);
491 Udigits.Table (Uints.Table (Left).Loc);
493 Udigits.Table (Uints.Table (Left).Loc + 1);
496 -- It is not so clear what to return when Arg is negative???
498 Left_Hat := abs (L1) * Base + L2;
503 Length_L : constant Int := Uints.Table (Left).Length;
510 if Direct (Right) then
511 T := Direct_Val (Left);
512 R1 := abs (T / Base);
517 R1 := abs (Udigits.Table (Uints.Table (Right).Loc));
518 R2 := Udigits.Table (Uints.Table (Right).Loc + 1);
519 Length_R := Uints.Table (Right).Length;
522 if Length_L = Length_R then
523 Right_Hat := R1 * Base + R2;
524 elsif Length_L = Length_R + Int_1 then
530 end Most_Sig_2_Digits;
536 -- Note: N_Digits returns 1 for No_Uint
538 function N_Digits (Input : Uint) return Int is
540 if Direct (Input) then
541 if Direct_Val (Input) >= Base then
548 return Uints.Table (Input).Length;
556 function Num_Bits (Input : Uint) return Nat is
561 if UI_Is_In_Int_Range (Input) then
562 Num := UI_To_Int (Input);
566 Bits := Base_Bits * (Uints.Table (Input).Length - 1);
567 Num := abs (Udigits.Table (Uints.Table (Input).Loc));
570 while Types.">" (Num, 0) loop
582 procedure pid (Input : Uint) is
584 UI_Write (Input, Decimal);
592 procedure pih (Input : Uint) is
594 UI_Write (Input, Hex);
602 procedure Release (M : Save_Mark) is
604 Uints.Set_Last (Uint'Max (M.Save_Uint, Uints_Min));
605 Udigits.Set_Last (Int'Max (M.Save_Udigit, Udigits_Min));
608 ----------------------
609 -- Release_And_Save --
610 ----------------------
612 procedure Release_And_Save (M : Save_Mark; UI : in out Uint) is
619 UE_Len : Pos := Uints.Table (UI).Length;
620 UE_Loc : Int := Uints.Table (UI).Loc;
622 UD : Udigits.Table_Type (1 .. UE_Len) :=
623 Udigits.Table (UE_Loc .. UE_Loc + UE_Len - 1);
628 Uints.Increment_Last;
631 Uints.Table (UI) := (UE_Len, Udigits.Last + 1);
633 for J in 1 .. UE_Len loop
634 Udigits.Increment_Last;
635 Udigits.Table (Udigits.Last) := UD (J);
639 end Release_And_Save;
641 procedure Release_And_Save (M : Save_Mark; UI1, UI2 : in out Uint) is
644 Release_And_Save (M, UI2);
646 elsif Direct (UI2) then
647 Release_And_Save (M, UI1);
651 UE1_Len : Pos := Uints.Table (UI1).Length;
652 UE1_Loc : Int := Uints.Table (UI1).Loc;
654 UD1 : Udigits.Table_Type (1 .. UE1_Len) :=
655 Udigits.Table (UE1_Loc .. UE1_Loc + UE1_Len - 1);
657 UE2_Len : Pos := Uints.Table (UI2).Length;
658 UE2_Loc : Int := Uints.Table (UI2).Loc;
660 UD2 : Udigits.Table_Type (1 .. UE2_Len) :=
661 Udigits.Table (UE2_Loc .. UE2_Loc + UE2_Len - 1);
666 Uints.Increment_Last;
669 Uints.Table (UI1) := (UE1_Len, Udigits.Last + 1);
671 for J in 1 .. UE1_Len loop
672 Udigits.Increment_Last;
673 Udigits.Table (Udigits.Last) := UD1 (J);
676 Uints.Increment_Last;
679 Uints.Table (UI2) := (UE2_Len, Udigits.Last + 1);
681 for J in 1 .. UE2_Len loop
682 Udigits.Increment_Last;
683 Udigits.Table (Udigits.Last) := UD2 (J);
687 end Release_And_Save;
693 -- This is done in one pass
695 -- Mathematically: assume base congruent to 1 and compute an equivelent
698 -- If Sign = -1 return the alternating sum of the "digits".
700 -- D1 - D2 + D3 - D4 + D5 . . .
702 -- (where D1 is Least Significant Digit)
704 -- Mathematically: assume base congruent to -1 and compute an equivelent
707 -- This is used in Rem and Base is assumed to be 2 ** 15
709 -- Note: The next two functions are very similar, any style changes made
710 -- to one should be reflected in both. These would be simpler if we
711 -- worked base 2 ** 32.
713 function Sum_Digits (Left : Uint; Sign : Int) return Int is
715 pragma Assert (Sign = Int_1 or Sign = Int (-1));
717 -- First try simple case;
719 if Direct (Left) then
721 Tmp_Int : Int := Direct_Val (Left);
724 if Tmp_Int >= Base then
725 Tmp_Int := (Tmp_Int / Base) +
726 Sign * (Tmp_Int rem Base);
728 -- Now Tmp_Int is in [-(Base - 1) .. 2 * (Base - 1)]
730 if Tmp_Int >= Base then
734 Tmp_Int := (Tmp_Int / Base) + 1;
738 -- Now Tmp_Int is in [-(Base - 1) .. (Base - 1)]
745 -- Otherwise full circuit is needed
749 L_Length : Int := N_Digits (Left);
750 L_Vec : UI_Vector (1 .. L_Length);
756 Init_Operand (Left, L_Vec);
757 L_Vec (1) := abs L_Vec (1);
762 for J in reverse 1 .. L_Length loop
763 Tmp_Int := Tmp_Int + Alt * (L_Vec (J) + Carry);
765 -- Tmp_Int is now between [-2 * Base + 1 .. 2 * Base - 1],
766 -- since old Tmp_Int is between [-(Base - 1) .. Base - 1]
767 -- and L_Vec is in [0 .. Base - 1] and Carry in [-1 .. 1]
769 if Tmp_Int >= Base then
770 Tmp_Int := Tmp_Int - Base;
773 elsif Tmp_Int <= -Base then
774 Tmp_Int := Tmp_Int + Base;
781 -- Tmp_Int is now between [-Base + 1 .. Base - 1]
786 Tmp_Int := Tmp_Int + Alt * Carry;
788 -- Tmp_Int is now between [-Base .. Base]
790 if Tmp_Int >= Base then
791 Tmp_Int := Tmp_Int - Base + Alt * Sign * 1;
793 elsif Tmp_Int <= -Base then
794 Tmp_Int := Tmp_Int + Base + Alt * Sign * (-1);
797 -- Now Tmp_Int is in [-(Base - 1) .. (Base - 1)]
804 -----------------------
805 -- Sum_Double_Digits --
806 -----------------------
808 -- Note: This is used in Rem, Base is assumed to be 2 ** 15
810 function Sum_Double_Digits (Left : Uint; Sign : Int) return Int is
812 -- First try simple case;
814 pragma Assert (Sign = Int_1 or Sign = Int (-1));
816 if Direct (Left) then
817 return Direct_Val (Left);
819 -- Otherwise full circuit is needed
823 L_Length : Int := N_Digits (Left);
824 L_Vec : UI_Vector (1 .. L_Length);
832 Init_Operand (Left, L_Vec);
833 L_Vec (1) := abs L_Vec (1);
842 Least_Sig_Int := Least_Sig_Int + Alt * (L_Vec (J) + Carry);
844 -- Least is in [-2 Base + 1 .. 2 * Base - 1]
845 -- Since L_Vec in [0 .. Base - 1] and Carry in [-1 .. 1]
846 -- and old Least in [-Base + 1 .. Base - 1]
848 if Least_Sig_Int >= Base then
849 Least_Sig_Int := Least_Sig_Int - Base;
852 elsif Least_Sig_Int <= -Base then
853 Least_Sig_Int := Least_Sig_Int + Base;
860 -- Least is now in [-Base + 1 .. Base - 1]
862 Most_Sig_Int := Most_Sig_Int + Alt * (L_Vec (J - 1) + Carry);
864 -- Most is in [-2 Base + 1 .. 2 * Base - 1]
865 -- Since L_Vec in [0 .. Base - 1] and Carry in [-1 .. 1]
866 -- and old Most in [-Base + 1 .. Base - 1]
868 if Most_Sig_Int >= Base then
869 Most_Sig_Int := Most_Sig_Int - Base;
872 elsif Most_Sig_Int <= -Base then
873 Most_Sig_Int := Most_Sig_Int + Base;
879 -- Most is now in [-Base + 1 .. Base - 1]
886 Least_Sig_Int := Least_Sig_Int + Alt * (L_Vec (J) + Carry);
888 Least_Sig_Int := Least_Sig_Int + Alt * Carry;
891 if Least_Sig_Int >= Base then
892 Least_Sig_Int := Least_Sig_Int - Base;
893 Most_Sig_Int := Most_Sig_Int + Alt * 1;
895 elsif Least_Sig_Int <= -Base then
896 Least_Sig_Int := Least_Sig_Int + Base;
897 Most_Sig_Int := Most_Sig_Int + Alt * (-1);
900 if Most_Sig_Int >= Base then
901 Most_Sig_Int := Most_Sig_Int - Base;
904 Least_Sig_Int + Alt * 1; -- cannot overflow again
906 elsif Most_Sig_Int <= -Base then
907 Most_Sig_Int := Most_Sig_Int + Base;
910 Least_Sig_Int + Alt * (-1); -- cannot overflow again.
913 return Most_Sig_Int * Base + Least_Sig_Int;
916 end Sum_Double_Digits;
922 procedure Tree_Read is
927 Tree_Read_Int (Int (Uint_Int_First));
928 Tree_Read_Int (Int (Uint_Int_Last));
929 Tree_Read_Int (UI_Power_2_Set);
930 Tree_Read_Int (UI_Power_10_Set);
931 Tree_Read_Int (Int (Uints_Min));
932 Tree_Read_Int (Udigits_Min);
934 for J in 0 .. UI_Power_2_Set loop
935 Tree_Read_Int (Int (UI_Power_2 (J)));
938 for J in 0 .. UI_Power_10_Set loop
939 Tree_Read_Int (Int (UI_Power_10 (J)));
948 procedure Tree_Write is
953 Tree_Write_Int (Int (Uint_Int_First));
954 Tree_Write_Int (Int (Uint_Int_Last));
955 Tree_Write_Int (UI_Power_2_Set);
956 Tree_Write_Int (UI_Power_10_Set);
957 Tree_Write_Int (Int (Uints_Min));
958 Tree_Write_Int (Udigits_Min);
960 for J in 0 .. UI_Power_2_Set loop
961 Tree_Write_Int (Int (UI_Power_2 (J)));
964 for J in 0 .. UI_Power_10_Set loop
965 Tree_Write_Int (Int (UI_Power_10 (J)));
974 function UI_Abs (Right : Uint) return Uint is
976 if Right < Uint_0 then
987 function UI_Add (Left : Int; Right : Uint) return Uint is
989 return UI_Add (UI_From_Int (Left), Right);
992 function UI_Add (Left : Uint; Right : Int) return Uint is
994 return UI_Add (Left, UI_From_Int (Right));
997 function UI_Add (Left : Uint; Right : Uint) return Uint is
999 -- Simple cases of direct operands and addition of zero
1001 if Direct (Left) then
1002 if Direct (Right) then
1003 return UI_From_Int (Direct_Val (Left) + Direct_Val (Right));
1005 elsif Int (Left) = Int (Uint_0) then
1009 elsif Direct (Right) and then Int (Right) = Int (Uint_0) then
1013 -- Otherwise full circuit is needed
1016 L_Length : Int := N_Digits (Left);
1017 R_Length : Int := N_Digits (Right);
1018 L_Vec : UI_Vector (1 .. L_Length);
1019 R_Vec : UI_Vector (1 .. R_Length);
1024 X_Bigger : Boolean := False;
1025 Y_Bigger : Boolean := False;
1026 Result_Neg : Boolean := False;
1029 Init_Operand (Left, L_Vec);
1030 Init_Operand (Right, R_Vec);
1032 -- At least one of the two operands is in multi-digit form.
1033 -- Calculate the number of digits sufficient to hold result.
1035 if L_Length > R_Length then
1036 Sum_Length := L_Length + 1;
1039 Sum_Length := R_Length + 1;
1040 if R_Length > L_Length then Y_Bigger := True; end if;
1043 -- Make copies of the absolute values of L_Vec and R_Vec into
1044 -- X and Y both with lengths equal to the maximum possibly
1045 -- needed. This makes looping over the digits much simpler.
1048 X : UI_Vector (1 .. Sum_Length);
1049 Y : UI_Vector (1 .. Sum_Length);
1050 Tmp_UI : UI_Vector (1 .. Sum_Length);
1053 for J in 1 .. Sum_Length - L_Length loop
1057 X (Sum_Length - L_Length + 1) := abs L_Vec (1);
1059 for J in 2 .. L_Length loop
1060 X (J + (Sum_Length - L_Length)) := L_Vec (J);
1063 for J in 1 .. Sum_Length - R_Length loop
1067 Y (Sum_Length - R_Length + 1) := abs R_Vec (1);
1069 for J in 2 .. R_Length loop
1070 Y (J + (Sum_Length - R_Length)) := R_Vec (J);
1073 if (L_Vec (1) < Int_0) = (R_Vec (1) < Int_0) then
1075 -- Same sign so just add
1078 for J in reverse 1 .. Sum_Length loop
1079 Tmp_Int := X (J) + Y (J) + Carry;
1081 if Tmp_Int >= Base then
1082 Tmp_Int := Tmp_Int - Base;
1091 return Vector_To_Uint (X, L_Vec (1) < Int_0);
1094 -- Find which one has bigger magnitude
1096 if not (X_Bigger or Y_Bigger) then
1097 for J in L_Vec'Range loop
1098 if abs L_Vec (J) > abs R_Vec (J) then
1101 elsif abs R_Vec (J) > abs L_Vec (J) then
1108 -- If they have identical magnitude, just return 0, else
1109 -- swap if necessary so that X had the bigger magnitude.
1110 -- Determine if result is negative at this time.
1112 Result_Neg := False;
1114 if not (X_Bigger or Y_Bigger) then
1118 if R_Vec (1) < Int_0 then
1127 if L_Vec (1) < Int_0 then
1132 -- Subtract Y from the bigger X
1136 for J in reverse 1 .. Sum_Length loop
1137 Tmp_Int := X (J) - Y (J) + Borrow;
1139 if Tmp_Int < Int_0 then
1140 Tmp_Int := Tmp_Int + Base;
1149 return Vector_To_Uint (X, Result_Neg);
1156 --------------------------
1157 -- UI_Decimal_Digits_Hi --
1158 --------------------------
1160 function UI_Decimal_Digits_Hi (U : Uint) return Nat is
1162 -- The maximum value of a "digit" is 32767, which is 5 decimal
1163 -- digits, so an N_Digit number could take up to 5 times this
1164 -- number of digits. This is certainly too high for large
1165 -- numbers but it is not worth worrying about.
1167 return 5 * N_Digits (U);
1168 end UI_Decimal_Digits_Hi;
1170 --------------------------
1171 -- UI_Decimal_Digits_Lo --
1172 --------------------------
1174 function UI_Decimal_Digits_Lo (U : Uint) return Nat is
1176 -- The maximum value of a "digit" is 32767, which is more than four
1177 -- decimal digits, but not a full five digits. The easily computed
1178 -- minimum number of decimal digits is thus 1 + 4 * the number of
1179 -- digits. This is certainly too low for large numbers but it is
1180 -- not worth worrying about.
1182 return 1 + 4 * (N_Digits (U) - 1);
1183 end UI_Decimal_Digits_Lo;
1189 function UI_Div (Left : Int; Right : Uint) return Uint is
1191 return UI_Div (UI_From_Int (Left), Right);
1194 function UI_Div (Left : Uint; Right : Int) return Uint is
1196 return UI_Div (Left, UI_From_Int (Right));
1199 function UI_Div (Left, Right : Uint) return Uint is
1201 pragma Assert (Right /= Uint_0);
1203 -- Cases where both operands are represented directly
1205 if Direct (Left) and then Direct (Right) then
1206 return UI_From_Int (Direct_Val (Left) / Direct_Val (Right));
1210 L_Length : constant Int := N_Digits (Left);
1211 R_Length : constant Int := N_Digits (Right);
1212 Q_Length : constant Int := L_Length - R_Length + 1;
1213 L_Vec : UI_Vector (1 .. L_Length);
1214 R_Vec : UI_Vector (1 .. R_Length);
1223 -- Result is zero if left operand is shorter than right
1225 if L_Length < R_Length then
1229 Init_Operand (Left, L_Vec);
1230 Init_Operand (Right, R_Vec);
1232 -- Case of right operand is single digit. Here we can simply divide
1233 -- each digit of the left operand by the divisor, from most to least
1234 -- significant, carrying the remainder to the next digit (just like
1235 -- ordinary long division by hand).
1237 if R_Length = Int_1 then
1239 Tmp_Divisor := abs R_Vec (1);
1242 Quotient : UI_Vector (1 .. L_Length);
1245 for J in L_Vec'Range loop
1246 Tmp_Int := Remainder * Base + abs L_Vec (J);
1247 Quotient (J) := Tmp_Int / Tmp_Divisor;
1248 Remainder := Tmp_Int rem Tmp_Divisor;
1253 (Quotient, (L_Vec (1) < Int_0 xor R_Vec (1) < Int_0));
1257 -- The possible simple cases have been exhausted. Now turn to the
1258 -- algorithm D from the section of Knuth mentioned at the top of
1261 Algorithm_D : declare
1262 Dividend : UI_Vector (1 .. L_Length + 1);
1263 Divisor : UI_Vector (1 .. R_Length);
1264 Quotient : UI_Vector (1 .. Q_Length);
1270 -- [ NORMALIZE ] (step D1 in the algorithm). First calculate the
1271 -- scale d, and then multiply Left and Right (u and v in the book)
1272 -- by d to get the dividend and divisor to work with.
1274 D := Base / (abs R_Vec (1) + 1);
1277 Dividend (2) := abs L_Vec (1);
1279 for J in 3 .. L_Length + Int_1 loop
1280 Dividend (J) := L_Vec (J - 1);
1283 Divisor (1) := abs R_Vec (1);
1285 for J in Int_2 .. R_Length loop
1286 Divisor (J) := R_Vec (J);
1291 -- Multiply Dividend by D
1294 for J in reverse Dividend'Range loop
1295 Tmp_Int := Dividend (J) * D + Carry;
1296 Dividend (J) := Tmp_Int rem Base;
1297 Carry := Tmp_Int / Base;
1300 -- Multiply Divisor by d.
1303 for J in reverse Divisor'Range loop
1304 Tmp_Int := Divisor (J) * D + Carry;
1305 Divisor (J) := Tmp_Int rem Base;
1306 Carry := Tmp_Int / Base;
1310 -- Main loop of long division algorithm.
1312 Divisor_Dig1 := Divisor (1);
1313 Divisor_Dig2 := Divisor (2);
1315 for J in Quotient'Range loop
1317 -- [ CALCULATE Q (hat) ] (step D3 in the algorithm).
1319 Tmp_Int := Dividend (J) * Base + Dividend (J + 1);
1323 if Dividend (J) = Divisor_Dig1 then
1324 Q_Guess := Base - 1;
1326 Q_Guess := Tmp_Int / Divisor_Dig1;
1331 while Divisor_Dig2 * Q_Guess >
1332 (Tmp_Int - Q_Guess * Divisor_Dig1) * Base +
1335 Q_Guess := Q_Guess - 1;
1338 -- [ MULTIPLY & SUBTRACT] (step D4). Q_Guess * Divisor is
1339 -- subtracted from the remaining dividend.
1342 for K in reverse Divisor'Range loop
1343 Tmp_Int := Dividend (J + K) - Q_Guess * Divisor (K) + Carry;
1344 Tmp_Dig := Tmp_Int rem Base;
1345 Carry := Tmp_Int / Base;
1347 if Tmp_Dig < Int_0 then
1348 Tmp_Dig := Tmp_Dig + Base;
1352 Dividend (J + K) := Tmp_Dig;
1355 Dividend (J) := Dividend (J) + Carry;
1357 -- [ TEST REMAINDER ] & [ ADD BACK ] (steps D5 and D6)
1358 -- Here there is a slight difference from the book: the last
1359 -- carry is always added in above and below (cancelling each
1360 -- other). In fact the dividend going negative is used as
1363 -- If the Dividend went negative, then Q_Guess was off by
1364 -- one, so it is decremented, and the divisor is added back
1365 -- into the relevant portion of the dividend.
1367 if Dividend (J) < Int_0 then
1368 Q_Guess := Q_Guess - 1;
1371 for K in reverse Divisor'Range loop
1372 Tmp_Int := Dividend (J + K) + Divisor (K) + Carry;
1374 if Tmp_Int >= Base then
1375 Tmp_Int := Tmp_Int - Base;
1381 Dividend (J + K) := Tmp_Int;
1384 Dividend (J) := Dividend (J) + Carry;
1387 -- Finally we can get the next quotient digit
1389 Quotient (J) := Q_Guess;
1392 return Vector_To_Uint
1393 (Quotient, (L_Vec (1) < Int_0 xor R_Vec (1) < Int_0));
1403 function UI_Eq (Left : Int; Right : Uint) return Boolean is
1405 return not UI_Ne (UI_From_Int (Left), Right);
1408 function UI_Eq (Left : Uint; Right : Int) return Boolean is
1410 return not UI_Ne (Left, UI_From_Int (Right));
1413 function UI_Eq (Left : Uint; Right : Uint) return Boolean is
1415 return not UI_Ne (Left, Right);
1422 function UI_Expon (Left : Int; Right : Uint) return Uint is
1424 return UI_Expon (UI_From_Int (Left), Right);
1427 function UI_Expon (Left : Uint; Right : Int) return Uint is
1429 return UI_Expon (Left, UI_From_Int (Right));
1432 function UI_Expon (Left : Int; Right : Int) return Uint is
1434 return UI_Expon (UI_From_Int (Left), UI_From_Int (Right));
1437 function UI_Expon (Left : Uint; Right : Uint) return Uint is
1439 pragma Assert (Right >= Uint_0);
1441 -- Any value raised to power of 0 is 1
1443 if Right = Uint_0 then
1446 -- 0 to any positive power is 0.
1448 elsif Left = Uint_0 then
1451 -- 1 to any power is 1
1453 elsif Left = Uint_1 then
1456 -- Any value raised to power of 1 is that value
1458 elsif Right = Uint_1 then
1461 -- Cases which can be done by table lookup
1463 elsif Right <= Uint_64 then
1465 -- 2 ** N for N in 2 .. 64
1467 if Left = Uint_2 then
1469 Right_Int : constant Int := Direct_Val (Right);
1472 if Right_Int > UI_Power_2_Set then
1473 for J in UI_Power_2_Set + Int_1 .. Right_Int loop
1474 UI_Power_2 (J) := UI_Power_2 (J - Int_1) * Int_2;
1475 Uints_Min := Uints.Last;
1476 Udigits_Min := Udigits.Last;
1479 UI_Power_2_Set := Right_Int;
1482 return UI_Power_2 (Right_Int);
1485 -- 10 ** N for N in 2 .. 64
1487 elsif Left = Uint_10 then
1489 Right_Int : constant Int := Direct_Val (Right);
1492 if Right_Int > UI_Power_10_Set then
1493 for J in UI_Power_10_Set + Int_1 .. Right_Int loop
1494 UI_Power_10 (J) := UI_Power_10 (J - Int_1) * Int (10);
1495 Uints_Min := Uints.Last;
1496 Udigits_Min := Udigits.Last;
1499 UI_Power_10_Set := Right_Int;
1502 return UI_Power_10 (Right_Int);
1507 -- If we fall through, then we have the general case (see Knuth 4.6.3)
1511 Squares : Uint := Left;
1512 Result : Uint := Uint_1;
1513 M : constant Uintp.Save_Mark := Uintp.Mark;
1517 if (Least_Sig_Digit (N) mod Int_2) = Int_1 then
1518 Result := Result * Squares;
1522 exit when N = Uint_0;
1523 Squares := Squares * Squares;
1526 Uintp.Release_And_Save (M, Result);
1535 function UI_From_Dint (Input : Dint) return Uint is
1538 if Dint (Min_Direct) <= Input and then Input <= Dint (Max_Direct) then
1539 return Uint (Dint (Uint_Direct_Bias) + Input);
1541 -- For values of larger magnitude, compute digits into a vector and
1542 -- call Vector_To_Uint.
1546 Max_For_Dint : constant := 5;
1547 -- Base is defined so that 5 Uint digits is sufficient
1548 -- to hold the largest possible Dint value.
1550 V : UI_Vector (1 .. Max_For_Dint);
1552 Temp_Integer : Dint;
1555 for J in V'Range loop
1559 Temp_Integer := Input;
1561 for J in reverse V'Range loop
1562 V (J) := Int (abs (Temp_Integer rem Dint (Base)));
1563 Temp_Integer := Temp_Integer / Dint (Base);
1566 return Vector_To_Uint (V, Input < Dint'(0));
1575 function UI_From_Int
(Input
: Int
) return Uint
is
1578 if Min_Direct
<= Input
and then Input
<= Max_Direct
then
1579 return Uint
(Int
(Uint_Direct_Bias
) + Input
);
1581 -- For values of larger magnitude, compute digits into a vector and
1582 -- call Vector_To_Uint.
1586 Max_For_Int
: constant := 3;
1587 -- Base is defined so that 3 Uint digits is sufficient
1588 -- to hold the largest possible Int value.
1590 V
: UI_Vector
(1 .. Max_For_Int
);
1595 for J
in V
'Range loop
1599 Temp_Integer
:= Input
;
1601 for J
in reverse V
'Range loop
1602 V
(J
) := abs (Temp_Integer
rem Base
);
1603 Temp_Integer
:= Temp_Integer
/ Base
;
1606 return Vector_To_Uint
(V
, Input
< Int_0
);
1615 -- Lehmer's algorithm for GCD.
1617 -- The idea is to avoid using multiple precision arithmetic wherever
1618 -- possible, substituting Int arithmetic instead. See Knuth volume II,
1619 -- Algorithm L (page 329).
1621 -- We use the same notation as Knuth (U_Hat standing for the obvious!)
1623 function UI_GCD
(Uin
, Vin
: Uint
) return Uint
is
1625 -- Copies of Uin and Vin
1628 -- The most Significant digits of U,V
1630 A
, B
, C
, D
, T
, Q
, Den1
, Den2
: Int
;
1633 Marks
: constant Uintp
.Save_Mark
:= Uintp
.Mark
;
1634 Iterations
: Integer := 0;
1637 pragma Assert
(Uin
>= Vin
);
1638 pragma Assert
(Vin
>= Uint_0
);
1644 Iterations
:= Iterations
+ 1;
1651 UI_From_Int
(GCD
(Direct_Val
(V
), UI_To_Int
(U
rem V
)));
1655 Most_Sig_2_Digits
(U
, V
, U_Hat
, V_Hat
);
1662 -- We might overflow and get division by zero here. This just
1663 -- means we can not take the single precision step
1667 exit when (Den1
* Den2
) = Int_0
;
1669 -- Compute Q, the trial quotient
1671 Q
:= (U_Hat
+ A
) / Den1
;
1673 exit when Q
/= ((U_Hat
+ B
) / Den2
);
1675 -- A single precision step Euclid step will give same answer as
1676 -- a multiprecision one.
1686 T
:= U_Hat
- (Q
* V_Hat
);
1692 -- Take a multiprecision Euclid step
1696 -- No single precision steps take a regular Euclid step.
1703 -- Use prior single precision steps to compute this Euclid step.
1705 -- Fixed bug 1415-008 spends 80% of its time working on this
1706 -- step. Perhaps we need a special case Int / Uint dot
1707 -- product to speed things up. ???
1709 -- Alternatively we could increase the single precision
1710 -- iterations to handle Uint's of some small size ( <5
1711 -- digits?). Then we would have more iterations on small Uint.
1712 -- Fixed bug 1415-008 only gets 5 (on average) single
1713 -- precision iterations per large iteration. ???
1715 Tmp_UI
:= (UI_From_Int
(A
) * U
) + (UI_From_Int
(B
) * V
);
1716 V
:= (UI_From_Int
(C
) * U
) + (UI_From_Int
(D
) * V
);
1720 -- If the operands are very different in magnitude, the loop
1721 -- will generate large amounts of short-lived data, which it is
1722 -- worth removing periodically.
1724 if Iterations
> 100 then
1725 Release_And_Save
(Marks
, U
, V
);
1735 function UI_Ge
(Left
: Int
; Right
: Uint
) return Boolean is
1737 return not UI_Lt
(UI_From_Int
(Left
), Right
);
1740 function UI_Ge
(Left
: Uint
; Right
: Int
) return Boolean is
1742 return not UI_Lt
(Left
, UI_From_Int
(Right
));
1745 function UI_Ge
(Left
: Uint
; Right
: Uint
) return Boolean is
1747 return not UI_Lt
(Left
, Right
);
1754 function UI_Gt
(Left
: Int
; Right
: Uint
) return Boolean is
1756 return UI_Lt
(Right
, UI_From_Int
(Left
));
1759 function UI_Gt
(Left
: Uint
; Right
: Int
) return Boolean is
1761 return UI_Lt
(UI_From_Int
(Right
), Left
);
1764 function UI_Gt
(Left
: Uint
; Right
: Uint
) return Boolean is
1766 return UI_Lt
(Right
, Left
);
1773 procedure UI_Image
(Input
: Uint
; Format
: UI_Format
:= Auto
) is
1775 Image_Out
(Input
, True, Format
);
1778 -------------------------
1779 -- UI_Is_In_Int_Range --
1780 -------------------------
1782 function UI_Is_In_Int_Range
(Input
: Uint
) return Boolean is
1784 -- Make sure we don't get called before Initialize
1786 pragma Assert
(Uint_Int_First
/= Uint_0
);
1788 if Direct
(Input
) then
1791 return Input
>= Uint_Int_First
1792 and then Input
<= Uint_Int_Last
;
1794 end UI_Is_In_Int_Range
;
1800 function UI_Le
(Left
: Int
; Right
: Uint
) return Boolean is
1802 return not UI_Lt
(Right
, UI_From_Int
(Left
));
1805 function UI_Le
(Left
: Uint
; Right
: Int
) return Boolean is
1807 return not UI_Lt
(UI_From_Int
(Right
), Left
);
1810 function UI_Le
(Left
: Uint
; Right
: Uint
) return Boolean is
1812 return not UI_Lt
(Right
, Left
);
1819 function UI_Lt
(Left
: Int
; Right
: Uint
) return Boolean is
1821 return UI_Lt
(UI_From_Int
(Left
), Right
);
1824 function UI_Lt
(Left
: Uint
; Right
: Int
) return Boolean is
1826 return UI_Lt
(Left
, UI_From_Int
(Right
));
1829 function UI_Lt
(Left
: Uint
; Right
: Uint
) return Boolean is
1831 -- Quick processing for identical arguments
1833 if Int
(Left
) = Int
(Right
) then
1836 -- Quick processing for both arguments directly represented
1838 elsif Direct
(Left
) and then Direct
(Right
) then
1839 return Int
(Left
) < Int
(Right
);
1841 -- At least one argument is more than one digit long
1845 L_Length
: constant Int
:= N_Digits
(Left
);
1846 R_Length
: constant Int
:= N_Digits
(Right
);
1848 L_Vec
: UI_Vector
(1 .. L_Length
);
1849 R_Vec
: UI_Vector
(1 .. R_Length
);
1852 Init_Operand
(Left
, L_Vec
);
1853 Init_Operand
(Right
, R_Vec
);
1855 if L_Vec
(1) < Int_0
then
1857 -- First argument negative, second argument non-negative
1859 if R_Vec
(1) >= Int_0
then
1862 -- Both arguments negative
1865 if L_Length
/= R_Length
then
1866 return L_Length
> R_Length
;
1868 elsif L_Vec
(1) /= R_Vec
(1) then
1869 return L_Vec
(1) < R_Vec
(1);
1872 for J
in 2 .. L_Vec
'Last loop
1873 if L_Vec
(J
) /= R_Vec
(J
) then
1874 return L_Vec
(J
) > R_Vec
(J
);
1883 -- First argument non-negative, second argument negative
1885 if R_Vec
(1) < Int_0
then
1888 -- Both arguments non-negative
1891 if L_Length
/= R_Length
then
1892 return L_Length
< R_Length
;
1894 for J
in L_Vec
'Range loop
1895 if L_Vec
(J
) /= R_Vec
(J
) then
1896 return L_Vec
(J
) < R_Vec
(J
);
1912 function UI_Max
(Left
: Int
; Right
: Uint
) return Uint
is
1914 return UI_Max
(UI_From_Int
(Left
), Right
);
1917 function UI_Max
(Left
: Uint
; Right
: Int
) return Uint
is
1919 return UI_Max
(Left
, UI_From_Int
(Right
));
1922 function UI_Max
(Left
: Uint
; Right
: Uint
) return Uint
is
1924 if Left
>= Right
then
1935 function UI_Min
(Left
: Int
; Right
: Uint
) return Uint
is
1937 return UI_Min
(UI_From_Int
(Left
), Right
);
1940 function UI_Min
(Left
: Uint
; Right
: Int
) return Uint
is
1942 return UI_Min
(Left
, UI_From_Int
(Right
));
1945 function UI_Min
(Left
: Uint
; Right
: Uint
) return Uint
is
1947 if Left
<= Right
then
1958 function UI_Mod
(Left
: Int
; Right
: Uint
) return Uint
is
1960 return UI_Mod
(UI_From_Int
(Left
), Right
);
1963 function UI_Mod
(Left
: Uint
; Right
: Int
) return Uint
is
1965 return UI_Mod
(Left
, UI_From_Int
(Right
));
1968 function UI_Mod
(Left
: Uint
; Right
: Uint
) return Uint
is
1969 Urem
: constant Uint
:= Left
rem Right
;
1972 if (Left
< Uint_0
) = (Right
< Uint_0
)
1973 or else Urem
= Uint_0
1977 return Right
+ Urem
;
1985 function UI_Mul
(Left
: Int
; Right
: Uint
) return Uint
is
1987 return UI_Mul
(UI_From_Int
(Left
), Right
);
1990 function UI_Mul
(Left
: Uint
; Right
: Int
) return Uint
is
1992 return UI_Mul
(Left
, UI_From_Int
(Right
));
1995 function UI_Mul
(Left
: Uint
; Right
: Uint
) return Uint
is
1997 -- Simple case of single length operands
1999 if Direct
(Left
) and then Direct
(Right
) then
2002 (Dint
(Direct_Val
(Left
)) * Dint
(Direct_Val
(Right
)));
2005 -- Otherwise we have the general case (Algorithm M in Knuth)
2008 L_Length
: constant Int
:= N_Digits
(Left
);
2009 R_Length
: constant Int
:= N_Digits
(Right
);
2010 L_Vec
: UI_Vector
(1 .. L_Length
);
2011 R_Vec
: UI_Vector
(1 .. R_Length
);
2015 Init_Operand
(Left
, L_Vec
);
2016 Init_Operand
(Right
, R_Vec
);
2017 Neg
:= (L_Vec
(1) < Int_0
) xor (R_Vec
(1) < Int_0
);
2018 L_Vec
(1) := abs (L_Vec
(1));
2019 R_Vec
(1) := abs (R_Vec
(1));
2021 Algorithm_M
: declare
2022 Product
: UI_Vector
(1 .. L_Length
+ R_Length
);
2027 for J
in Product
'Range loop
2031 for J
in reverse R_Vec
'Range loop
2033 for K
in reverse L_Vec
'Range loop
2035 L_Vec
(K
) * R_Vec
(J
) + Product
(J
+ K
) + Carry
;
2036 Product
(J
+ K
) := Tmp_Sum
rem Base
;
2037 Carry
:= Tmp_Sum
/ Base
;
2040 Product
(J
) := Carry
;
2043 return Vector_To_Uint
(Product
, Neg
);
2052 function UI_Ne
(Left
: Int
; Right
: Uint
) return Boolean is
2054 return UI_Ne
(UI_From_Int
(Left
), Right
);
2057 function UI_Ne
(Left
: Uint
; Right
: Int
) return Boolean is
2059 return UI_Ne
(Left
, UI_From_Int
(Right
));
2062 function UI_Ne
(Left
: Uint
; Right
: Uint
) return Boolean is
2064 -- Quick processing for identical arguments. Note that this takes
2065 -- care of the case of two No_Uint arguments.
2067 if Int
(Left
) = Int
(Right
) then
2071 -- See if left operand directly represented
2073 if Direct
(Left
) then
2075 -- If right operand directly represented then compare
2077 if Direct
(Right
) then
2078 return Int
(Left
) /= Int
(Right
);
2080 -- Left operand directly represented, right not, must be unequal
2086 -- Right operand directly represented, left not, must be unequal
2088 elsif Direct
(Right
) then
2092 -- Otherwise both multi-word, do comparison
2095 Size
: constant Int
:= N_Digits
(Left
);
2100 if Size
/= N_Digits
(Right
) then
2104 Left_Loc
:= Uints
.Table
(Left
).Loc
;
2105 Right_Loc
:= Uints
.Table
(Right
).Loc
;
2107 for J
in Int_0
.. Size
- Int_1
loop
2108 if Udigits
.Table
(Left_Loc
+ J
) /=
2109 Udigits
.Table
(Right_Loc
+ J
)
2123 function UI_Negate
(Right
: Uint
) return Uint
is
2125 -- Case where input is directly represented. Note that since the
2126 -- range of Direct values is non-symmetrical, the result may not
2127 -- be directly represented, this is taken care of in UI_From_Int.
2129 if Direct
(Right
) then
2130 return UI_From_Int
(-Direct_Val
(Right
));
2132 -- Full processing for multi-digit case. Note that we cannot just
2133 -- copy the value to the end of the table negating the first digit,
2134 -- since the range of Direct values is non-symmetrical, so we can
2135 -- have a negative value that is not Direct whose negation can be
2136 -- represented directly.
2140 R_Length
: constant Int
:= N_Digits
(Right
);
2141 R_Vec
: UI_Vector
(1 .. R_Length
);
2145 Init_Operand
(Right
, R_Vec
);
2146 Neg
:= R_Vec
(1) > Int_0
;
2147 R_Vec
(1) := abs R_Vec
(1);
2148 return Vector_To_Uint
(R_Vec
, Neg
);
2157 function UI_Rem
(Left
: Int
; Right
: Uint
) return Uint
is
2159 return UI_Rem
(UI_From_Int
(Left
), Right
);
2162 function UI_Rem
(Left
: Uint
; Right
: Int
) return Uint
is
2164 return UI_Rem
(Left
, UI_From_Int
(Right
));
2167 function UI_Rem
(Left
, Right
: Uint
) return Uint
is
2171 subtype Int1_12
is Integer range 1 .. 12;
2174 pragma Assert
(Right
/= Uint_0
);
2176 if Direct
(Right
) then
2177 if Direct
(Left
) then
2178 return UI_From_Int
(Direct_Val
(Left
) rem Direct_Val
(Right
));
2181 -- Special cases when Right is less than 13 and Left is larger
2182 -- larger than one digit. All of these algorithms depend on the
2183 -- base being 2 ** 15 We work with Abs (Left) and Abs(Right)
2184 -- then multiply result by Sign (Left)
2186 if (Right
<= Uint_12
) and then (Right
>= Uint_Minus_12
) then
2188 if (Left
< Uint_0
) then
2194 -- All cases are listed, grouped by mathematical method
2195 -- It is not inefficient to do have this case list out
2196 -- of order since GCC sorts the cases we list.
2198 case Int1_12
(abs (Direct_Val
(Right
))) is
2203 -- Powers of two are simple AND's with LS Left Digit
2204 -- GCC will recognise these constants as powers of 2
2205 -- and replace the rem with simpler operations where
2208 -- Least_Sig_Digit might return Negative numbers.
2211 return UI_From_Int
(
2212 Sign
* (Least_Sig_Digit
(Left
) mod 2));
2215 return UI_From_Int
(
2216 Sign
* (Least_Sig_Digit
(Left
) mod 4));
2219 return UI_From_Int
(
2220 Sign
* (Least_Sig_Digit
(Left
) mod 8));
2222 -- Some number theoretical tricks:
2224 -- If B Rem Right = 1 then
2225 -- Left Rem Right = Sum_Of_Digits_Base_B (Left) Rem Right
2227 -- Note: 2^32 mod 3 = 1
2230 return UI_From_Int
(
2231 Sign
* (Sum_Double_Digits
(Left
, 1) rem Int
(3)));
2233 -- Note: 2^15 mod 7 = 1
2236 return UI_From_Int
(
2237 Sign
* (Sum_Digits
(Left
, 1) rem Int
(7)));
2239 -- Note: 2^32 mod 5 = -1
2240 -- Alternating sums might be negative, but rem is always
2241 -- positive hence we must use mod here.
2244 Tmp
:= Sum_Double_Digits
(Left
, -1) mod Int
(5);
2245 return UI_From_Int
(Sign
* Tmp
);
2247 -- Note: 2^15 mod 9 = -1
2248 -- Alternating sums might be negative, but rem is always
2249 -- positive hence we must use mod here.
2252 Tmp
:= Sum_Digits
(Left
, -1) mod Int
(9);
2253 return UI_From_Int
(Sign
* Tmp
);
2255 -- Note: 2^15 mod 11 = -1
2256 -- Alternating sums might be negative, but rem is always
2257 -- positive hence we must use mod here.
2260 Tmp
:= Sum_Digits
(Left
, -1) mod Int
(11);
2261 return UI_From_Int
(Sign
* Tmp
);
2263 -- Now resort to Chinese Remainder theorem
2264 -- to reduce 6, 10, 12 to previous special cases
2266 -- There is no reason we could not add more cases
2267 -- like these if it proves useful.
2269 -- Perhaps we should go up to 16, however
2270 -- I have no "trick" for 13.
2272 -- To find u mod m we:
2274 -- GCD(m1, m2) = 1 AND m = (m1 * m2).
2275 -- Next we pick (Basis) M1, M2 small S.T.
2276 -- (M1 mod m1) = (M2 mod m2) = 1 AND
2277 -- (M1 mod m2) = (M2 mod m1) = 0
2279 -- So u mod m = (u1 * M1 + u2 * M2) mod m
2280 -- Where u1 = (u mod m1) AND u2 = (u mod m2);
2281 -- Under typical circumstances the last mod m
2282 -- can be done with a (possible) single subtraction.
2284 -- m1 = 2; m2 = 3; M1 = 3; M2 = 4;
2287 Tmp
:= 3 * (Least_Sig_Digit
(Left
) rem 2) +
2288 4 * (Sum_Double_Digits
(Left
, 1) rem 3);
2289 return UI_From_Int
(Sign
* (Tmp
rem 6));
2291 -- m1 = 2; m2 = 5; M1 = 5; M2 = 6;
2294 Tmp
:= 5 * (Least_Sig_Digit
(Left
) rem 2) +
2295 6 * (Sum_Double_Digits
(Left
, -1) mod 5);
2296 return UI_From_Int
(Sign
* (Tmp
rem 10));
2298 -- m1 = 3; m2 = 4; M1 = 4; M2 = 9;
2301 Tmp
:= 4 * (Sum_Double_Digits
(Left
, 1) rem 3) +
2302 9 * (Least_Sig_Digit
(Left
) rem 4);
2303 return UI_From_Int
(Sign
* (Tmp
rem 12));
2308 -- Else fall through to general case.
2310 -- ???This needs to be improved. We have the Rem when we do the
2311 -- Div. Div throws it away!
2313 -- The special case Length (Left) = Length(right) = 1 in Div
2314 -- looks slow. It uses UI_To_Int when Int should suffice. ???
2318 return Left
- (Left
/ Right
) * Right
;
2325 function UI_Sub
(Left
: Int
; Right
: Uint
) return Uint
is
2327 return UI_Add
(Left
, -Right
);
2330 function UI_Sub
(Left
: Uint
; Right
: Int
) return Uint
is
2332 return UI_Add
(Left
, -Right
);
2335 function UI_Sub
(Left
: Uint
; Right
: Uint
) return Uint
is
2337 if Direct
(Left
) and then Direct
(Right
) then
2338 return UI_From_Int
(Direct_Val
(Left
) - Direct_Val
(Right
));
2340 return UI_Add
(Left
, -Right
);
2348 function UI_To_Int
(Input
: Uint
) return Int
is
2350 if Direct
(Input
) then
2351 return Direct_Val
(Input
);
2353 -- Case of input is more than one digit
2357 In_Length
: constant Int
:= N_Digits
(Input
);
2358 In_Vec
: UI_Vector
(1 .. In_Length
);
2362 -- Uints of more than one digit could be outside the range for
2363 -- Ints. Caller should have checked for this if not certain.
2364 -- Fatal error to attempt to convert from value outside Int'Range.
2366 pragma Assert
(UI_Is_In_Int_Range
(Input
));
2368 -- Otherwise, proceed ahead, we are OK
2370 Init_Operand
(Input
, In_Vec
);
2373 -- Calculate -|Input| and then negates if value is positive.
2374 -- This handles our current definition of Int (based on
2375 -- 2s complement). Is it secure enough?
2377 for Idx
in In_Vec
'Range loop
2378 Ret_Int
:= Ret_Int
* Base
- abs In_Vec
(Idx
);
2381 if In_Vec
(1) < Int_0
then
2394 procedure UI_Write
(Input
: Uint
; Format
: UI_Format
:= Auto
) is
2396 Image_Out
(Input
, False, Format
);
2399 ---------------------
2400 -- Vector_To_Uint --
2401 ---------------------
2403 function Vector_To_Uint
2404 (In_Vec
: UI_Vector
;
2412 -- The vector can contain leading zeros. These are not stored in the
2413 -- table, so loop through the vector looking for first non-zero digit
2415 for J
in In_Vec
'Range loop
2416 if In_Vec
(J
) /= Int_0
then
2418 -- The length of the value is the length of the rest of the vector
2420 Size
:= In_Vec
'Last - J
+ 1;
2422 -- One digit value can always be represented directly
2424 if Size
= Int_1
then
2426 return Uint
(Int
(Uint_Direct_Bias
) - In_Vec
(J
));
2428 return Uint
(Int
(Uint_Direct_Bias
) + In_Vec
(J
));
2431 -- Positive two digit values may be in direct representation range
2433 elsif Size
= Int_2
and then not Negative
then
2434 Val
:= In_Vec
(J
) * Base
+ In_Vec
(J
+ 1);
2436 if Val
<= Max_Direct
then
2437 return Uint
(Int
(Uint_Direct_Bias
) + Val
);
2441 -- The value is outside the direct representation range and
2442 -- must therefore be stored in the table. Expand the table
2443 -- to contain the count and tigis. The index of the new table
2444 -- entry will be returned as the result.
2446 Uints
.Increment_Last
;
2447 Uints
.Table
(Uints
.Last
).Length
:= Size
;
2448 Uints
.Table
(Uints
.Last
).Loc
:= Udigits
.Last
+ 1;
2450 Udigits
.Increment_Last
;
2453 Udigits
.Table
(Udigits
.Last
) := -In_Vec
(J
);
2455 Udigits
.Table
(Udigits
.Last
) := +In_Vec
(J
);
2458 for K
in 2 .. Size
loop
2459 Udigits
.Increment_Last
;
2460 Udigits
.Table
(Udigits
.Last
) := In_Vec
(J
+ K
- 1);
2467 -- Dropped through loop only if vector contained all zeros