1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2013, 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 3, 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. --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception, --
20 -- version 3.1, as published by the Free Software Foundation. --
22 -- You should have received a copy of the GNU General Public License and --
23 -- a copy of the GCC Runtime Library Exception along with this program; --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
25 -- <http://www.gnu.org/licenses/>. --
27 -- GNAT was originally developed by the GNAT team at New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
30 ------------------------------------------------------------------------------
32 with Output
; use Output
;
33 with Tree_IO
; use Tree_IO
;
35 with GNAT
.HTable
; use GNAT
.HTable
;
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 value,
48 -- 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 does
72 -- not destroy values saved in the U_Power tables or in the hash table used
73 -- by UI_From_Int. Whenever an entry is made in either of these tables,
74 -- Uints_Min and Udigits_Min are updated to protect the entry, and Release
75 -- never cuts back beyond 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 -- UI_From_Int Hash Table --
85 ----------------------------
87 -- UI_From_Int uses a hash table to avoid duplicating entries and wasting
88 -- storage. This is particularly important for complex cases of back
91 subtype Hnum
is Nat
range 0 .. 1022;
93 function Hash_Num
(F
: Int
) return Hnum
;
96 package UI_Ints
is new Simple_HTable
(
99 No_Element
=> No_Uint
,
104 -----------------------
105 -- Local Subprograms --
106 -----------------------
108 function Direct
(U
: Uint
) return Boolean;
109 pragma Inline
(Direct
);
110 -- Returns True if U is represented directly
112 function Direct_Val
(U
: Uint
) return Int
;
113 -- U is a Uint for is represented directly. The returned result is the
114 -- value represented.
116 function GCD
(Jin
, Kin
: Int
) return Int
;
117 -- Compute GCD of two integers. Assumes that Jin >= Kin >= 0
123 -- Common processing for UI_Image and UI_Write, To_Buffer is set True for
124 -- UI_Image, and false for UI_Write, and Format is copied from the Format
125 -- parameter to UI_Image or UI_Write.
127 procedure Init_Operand
(UI
: Uint
; Vec
: out UI_Vector
);
128 pragma Inline
(Init_Operand
);
129 -- This procedure puts the value of UI into the vector in canonical
130 -- multiple precision format. The parameter should be of the correct size
131 -- as determined by a previous call to N_Digits (UI). The first digit of
132 -- Vec contains the sign, all other digits are always non-negative. Note
133 -- that the input may be directly represented, and in this case Vec will
134 -- contain the corresponding one or two digit value. The low bound of Vec
137 function Least_Sig_Digit
(Arg
: Uint
) return Int
;
138 pragma Inline
(Least_Sig_Digit
);
139 -- Returns the Least Significant Digit of Arg quickly. When the given Uint
140 -- is less than 2**15, the value returned is the input value, in this case
141 -- the result may be negative. It is expected that any use will mask off
142 -- unnecessary bits. This is used for finding Arg mod B where B is a power
143 -- of two. Hence the actual base is irrelevant as long as it is a power of
146 procedure Most_Sig_2_Digits
150 Right_Hat
: out Int
);
151 -- Returns leading two significant digits from the given pair of Uint's.
152 -- Mathematically: returns Left / (Base ** K) and Right / (Base ** K) where
153 -- K is as small as possible S.T. Right_Hat < Base * Base. It is required
154 -- that Left > Right for the algorithm to work.
156 function N_Digits
(Input
: Uint
) return Int
;
157 pragma Inline
(N_Digits
);
158 -- Returns number of "digits" in a Uint
163 Remainder
: out Uint
;
164 Discard_Quotient
: Boolean := False;
165 Discard_Remainder
: Boolean := False);
166 -- Compute Euclidean division of Left by Right. If Discard_Quotient is
167 -- False then the quotient is returned in Quotient (otherwise Quotient is
168 -- set to No_Uint). If Discard_Remainder is False, then the remainder is
169 -- returned in Remainder (otherwise Remainder is set to No_Uint).
171 -- If Discard_Quotient is True, Quotient is set to No_Uint
172 -- If Discard_Remainder is True, Remainder is set to No_Uint
174 function Vector_To_Uint
176 Negative
: Boolean) return Uint
;
177 -- Functions that calculate values in UI_Vectors, call this function to
178 -- create and return the Uint value. In_Vec contains the multiple precision
179 -- (Base) representation of a non-negative value. Leading zeroes are
180 -- permitted. Negative is set if the desired result is the negative of the
181 -- given value. The result will be either the appropriate directly
182 -- represented value, or a table entry in the proper canonical format is
183 -- created and returned.
185 -- Note that Init_Operand puts a signed value in the result vector, but
186 -- Vector_To_Uint is always presented with a non-negative value. The
187 -- processing of signs is something that is done by the caller before
188 -- calling Vector_To_Uint.
194 function Direct
(U
: Uint
) return Boolean is
196 return Int
(U
) <= Int
(Uint_Direct_Last
);
203 function Direct_Val
(U
: Uint
) return Int
is
205 pragma Assert
(Direct
(U
));
206 return Int
(U
) - Int
(Uint_Direct_Bias
);
213 function GCD
(Jin
, Kin
: Int
) return Int
is
217 pragma Assert
(Jin
>= Kin
);
218 pragma Assert
(Kin
>= Int_0
);
222 while K
/= Uint_0
loop
235 function Hash_Num
(F
: Int
) return Hnum
is
237 return Types
."mod" (F
, Hnum
'Range_Length);
249 Marks
: constant Uintp
.Save_Mark
:= Uintp
.Mark
;
253 Digs_Output
: Natural := 0;
254 -- Counts digits output. In hex mode, but not in decimal mode, we
255 -- put an underline after every four hex digits that are output.
257 Exponent
: Natural := 0;
258 -- If the number is too long to fit in the buffer, we switch to an
259 -- approximate output format with an exponent. This variable records
260 -- the exponent value.
262 function Better_In_Hex
return Boolean;
263 -- Determines if it is better to generate digits in base 16 (result
264 -- is true) or base 10 (result is false). The choice is purely a
265 -- matter of convenience and aesthetics, so it does not matter which
266 -- value is returned from a correctness point of view.
268 procedure Image_Char
(C
: Character);
269 -- Internal procedure to output one character
271 procedure Image_Exponent
(N
: Natural);
272 -- Output non-zero exponent. Note that we only use the exponent form in
273 -- the buffer case, so we know that To_Buffer is true.
275 procedure Image_Uint
(U
: Uint
);
276 -- Internal procedure to output characters of non-negative Uint
282 function Better_In_Hex
return Boolean is
283 T16
: constant Uint
:= Uint_2
** Int
'(16);
289 -- Small values up to 2**16 can always be in decimal
295 -- Otherwise, see if we are a power of 2 or one less than a power
296 -- of 2. For the moment these are the only cases printed in hex.
298 if A mod Uint_2 = Uint_1 then
303 if A mod T16 /= Uint_0 then
313 while A > Uint_2 loop
314 if A mod Uint_2 /= Uint_0 then
329 procedure Image_Char (C : Character) is
332 if UI_Image_Length + 6 > UI_Image_Max then
333 Exponent := Exponent + 1;
335 UI_Image_Length := UI_Image_Length + 1;
336 UI_Image_Buffer (UI_Image_Length) := C;
347 procedure Image_Exponent (N : Natural) is
350 Image_Exponent (N / 10);
353 UI_Image_Length := UI_Image_Length + 1;
354 UI_Image_Buffer (UI_Image_Length) :=
355 Character'Val (Character'Pos ('0') + N mod 10);
362 procedure Image_Uint (U : Uint) is
363 H : constant array (Int range 0 .. 15) of Character :=
368 UI_Div_Rem (U, Base, Q, R);
374 if Digs_Output = 4 and then Base = Uint_16 then
379 Image_Char (H (UI_To_Int (R)));
381 Digs_Output := Digs_Output + 1;
384 -- Start of processing for Image_Out
387 if Input = No_Uint then
392 UI_Image_Length := 0;
394 if Input < Uint_0 then
402 or else (Format = Auto and then Better_In_Hex)
416 if Exponent /= 0 then
417 UI_Image_Length := UI_Image_Length + 1;
418 UI_Image_Buffer (UI_Image_Length) := 'E
';
419 Image_Exponent (Exponent);
422 Uintp.Release (Marks);
429 procedure Init_Operand (UI : Uint; Vec : out UI_Vector) is
432 pragma Assert (Vec'First = Int'(1));
436 Vec
(1) := Direct_Val
(UI
);
438 if Vec
(1) >= Base
then
439 Vec
(2) := Vec
(1) rem Base
;
440 Vec
(1) := Vec
(1) / Base
;
444 Loc
:= Uints
.Table
(UI
).Loc
;
446 for J
in 1 .. Uints
.Table
(UI
).Length
loop
447 Vec
(J
) := Udigits
.Table
(Loc
+ J
- 1);
456 procedure Initialize
is
461 Uint_Int_First
:= UI_From_Int
(Int
'First);
462 Uint_Int_Last
:= UI_From_Int
(Int
'Last);
464 UI_Power_2
(0) := Uint_1
;
467 UI_Power_10
(0) := Uint_1
;
468 UI_Power_10_Set
:= 0;
470 Uints_Min
:= Uints
.Last
;
471 Udigits_Min
:= Udigits
.Last
;
476 ---------------------
477 -- Least_Sig_Digit --
478 ---------------------
480 function Least_Sig_Digit
(Arg
: Uint
) return Int
is
485 V
:= Direct_Val
(Arg
);
491 -- Note that this result may be negative
498 (Uints
.Table
(Arg
).Loc
+ Uints
.Table
(Arg
).Length
- 1);
506 function Mark
return Save_Mark
is
508 return (Save_Uint
=> Uints
.Last
, Save_Udigit
=> Udigits
.Last
);
511 -----------------------
512 -- Most_Sig_2_Digits --
513 -----------------------
515 procedure Most_Sig_2_Digits
522 pragma Assert
(Left
>= Right
);
524 if Direct
(Left
) then
525 Left_Hat
:= Direct_Val
(Left
);
526 Right_Hat
:= Direct_Val
(Right
);
532 Udigits
.Table
(Uints
.Table
(Left
).Loc
);
534 Udigits
.Table
(Uints
.Table
(Left
).Loc
+ 1);
537 -- It is not so clear what to return when Arg is negative???
539 Left_Hat
:= abs (L1
) * Base
+ L2
;
544 Length_L
: constant Int
:= Uints
.Table
(Left
).Length
;
551 if Direct
(Right
) then
552 T
:= Direct_Val
(Left
);
553 R1
:= abs (T
/ Base
);
558 R1
:= abs (Udigits
.Table
(Uints
.Table
(Right
).Loc
));
559 R2
:= Udigits
.Table
(Uints
.Table
(Right
).Loc
+ 1);
560 Length_R
:= Uints
.Table
(Right
).Length
;
563 if Length_L
= Length_R
then
564 Right_Hat
:= R1
* Base
+ R2
;
565 elsif Length_L
= Length_R
+ Int_1
then
571 end Most_Sig_2_Digits
;
577 -- Note: N_Digits returns 1 for No_Uint
579 function N_Digits
(Input
: Uint
) return Int
is
581 if Direct
(Input
) then
582 if Direct_Val
(Input
) >= Base
then
589 return Uints
.Table
(Input
).Length
;
597 function Num_Bits
(Input
: Uint
) return Nat
is
602 -- Largest negative number has to be handled specially, since it is in
603 -- Int_Range, but we cannot take the absolute value.
605 if Input
= Uint_Int_First
then
608 -- For any other number in Int_Range, get absolute value of number
610 elsif UI_Is_In_Int_Range
(Input
) then
611 Num
:= abs (UI_To_Int
(Input
));
614 -- If not in Int_Range then initialize bit count for all low order
615 -- words, and set number to high order digit.
618 Bits
:= Base_Bits
* (Uints
.Table
(Input
).Length
- 1);
619 Num
:= abs (Udigits
.Table
(Uints
.Table
(Input
).Loc
));
622 -- Increase bit count for remaining value in Num
624 while Types
.">" (Num
, 0) loop
636 procedure pid
(Input
: Uint
) is
638 UI_Write
(Input
, Decimal
);
646 procedure pih
(Input
: Uint
) is
648 UI_Write
(Input
, Hex
);
656 procedure Release
(M
: Save_Mark
) is
658 Uints
.Set_Last
(Uint
'Max (M
.Save_Uint
, Uints_Min
));
659 Udigits
.Set_Last
(Int
'Max (M
.Save_Udigit
, Udigits_Min
));
662 ----------------------
663 -- Release_And_Save --
664 ----------------------
666 procedure Release_And_Save
(M
: Save_Mark
; UI
: in out Uint
) is
673 UE_Len
: constant Pos
:= Uints
.Table
(UI
).Length
;
674 UE_Loc
: constant Int
:= Uints
.Table
(UI
).Loc
;
676 UD
: constant Udigits
.Table_Type
(1 .. UE_Len
) :=
677 Udigits
.Table
(UE_Loc
.. UE_Loc
+ UE_Len
- 1);
682 Uints
.Append
((Length
=> UE_Len
, Loc
=> Udigits
.Last
+ 1));
685 for J
in 1 .. UE_Len
loop
686 Udigits
.Append
(UD
(J
));
690 end Release_And_Save
;
692 procedure Release_And_Save
(M
: Save_Mark
; UI1
, UI2
: in out Uint
) is
695 Release_And_Save
(M
, UI2
);
697 elsif Direct
(UI2
) then
698 Release_And_Save
(M
, UI1
);
702 UE1_Len
: constant Pos
:= Uints
.Table
(UI1
).Length
;
703 UE1_Loc
: constant Int
:= Uints
.Table
(UI1
).Loc
;
705 UD1
: constant Udigits
.Table_Type
(1 .. UE1_Len
) :=
706 Udigits
.Table
(UE1_Loc
.. UE1_Loc
+ UE1_Len
- 1);
708 UE2_Len
: constant Pos
:= Uints
.Table
(UI2
).Length
;
709 UE2_Loc
: constant Int
:= Uints
.Table
(UI2
).Loc
;
711 UD2
: constant Udigits
.Table_Type
(1 .. UE2_Len
) :=
712 Udigits
.Table
(UE2_Loc
.. UE2_Loc
+ UE2_Len
- 1);
717 Uints
.Append
((Length
=> UE1_Len
, Loc
=> Udigits
.Last
+ 1));
720 for J
in 1 .. UE1_Len
loop
721 Udigits
.Append
(UD1
(J
));
724 Uints
.Append
((Length
=> UE2_Len
, Loc
=> Udigits
.Last
+ 1));
727 for J
in 1 .. UE2_Len
loop
728 Udigits
.Append
(UD2
(J
));
732 end Release_And_Save
;
738 procedure Tree_Read
is
743 Tree_Read_Int
(Int
(Uint_Int_First
));
744 Tree_Read_Int
(Int
(Uint_Int_Last
));
745 Tree_Read_Int
(UI_Power_2_Set
);
746 Tree_Read_Int
(UI_Power_10_Set
);
747 Tree_Read_Int
(Int
(Uints_Min
));
748 Tree_Read_Int
(Udigits_Min
);
750 for J
in 0 .. UI_Power_2_Set
loop
751 Tree_Read_Int
(Int
(UI_Power_2
(J
)));
754 for J
in 0 .. UI_Power_10_Set
loop
755 Tree_Read_Int
(Int
(UI_Power_10
(J
)));
764 procedure Tree_Write
is
769 Tree_Write_Int
(Int
(Uint_Int_First
));
770 Tree_Write_Int
(Int
(Uint_Int_Last
));
771 Tree_Write_Int
(UI_Power_2_Set
);
772 Tree_Write_Int
(UI_Power_10_Set
);
773 Tree_Write_Int
(Int
(Uints_Min
));
774 Tree_Write_Int
(Udigits_Min
);
776 for J
in 0 .. UI_Power_2_Set
loop
777 Tree_Write_Int
(Int
(UI_Power_2
(J
)));
780 for J
in 0 .. UI_Power_10_Set
loop
781 Tree_Write_Int
(Int
(UI_Power_10
(J
)));
790 function UI_Abs
(Right
: Uint
) return Uint
is
792 if Right
< Uint_0
then
803 function UI_Add
(Left
: Int
; Right
: Uint
) return Uint
is
805 return UI_Add
(UI_From_Int
(Left
), Right
);
808 function UI_Add
(Left
: Uint
; Right
: Int
) return Uint
is
810 return UI_Add
(Left
, UI_From_Int
(Right
));
813 function UI_Add
(Left
: Uint
; Right
: Uint
) return Uint
is
815 -- Simple cases of direct operands and addition of zero
817 if Direct
(Left
) then
818 if Direct
(Right
) then
819 return UI_From_Int
(Direct_Val
(Left
) + Direct_Val
(Right
));
821 elsif Int
(Left
) = Int
(Uint_0
) then
825 elsif Direct
(Right
) and then Int
(Right
) = Int
(Uint_0
) then
829 -- Otherwise full circuit is needed
832 L_Length
: constant Int
:= N_Digits
(Left
);
833 R_Length
: constant Int
:= N_Digits
(Right
);
834 L_Vec
: UI_Vector
(1 .. L_Length
);
835 R_Vec
: UI_Vector
(1 .. R_Length
);
840 X_Bigger
: Boolean := False;
841 Y_Bigger
: Boolean := False;
842 Result_Neg
: Boolean := False;
845 Init_Operand
(Left
, L_Vec
);
846 Init_Operand
(Right
, R_Vec
);
848 -- At least one of the two operands is in multi-digit form.
849 -- Calculate the number of digits sufficient to hold result.
851 if L_Length
> R_Length
then
852 Sum_Length
:= L_Length
+ 1;
855 Sum_Length
:= R_Length
+ 1;
857 if R_Length
> L_Length
then
862 -- Make copies of the absolute values of L_Vec and R_Vec into X and Y
863 -- both with lengths equal to the maximum possibly needed. This makes
864 -- looping over the digits much simpler.
867 X
: UI_Vector
(1 .. Sum_Length
);
868 Y
: UI_Vector
(1 .. Sum_Length
);
869 Tmp_UI
: UI_Vector
(1 .. Sum_Length
);
872 for J
in 1 .. Sum_Length
- L_Length
loop
876 X
(Sum_Length
- L_Length
+ 1) := abs L_Vec
(1);
878 for J
in 2 .. L_Length
loop
879 X
(J
+ (Sum_Length
- L_Length
)) := L_Vec
(J
);
882 for J
in 1 .. Sum_Length
- R_Length
loop
886 Y
(Sum_Length
- R_Length
+ 1) := abs R_Vec
(1);
888 for J
in 2 .. R_Length
loop
889 Y
(J
+ (Sum_Length
- R_Length
)) := R_Vec
(J
);
892 if (L_Vec
(1) < Int_0
) = (R_Vec
(1) < Int_0
) then
894 -- Same sign so just add
897 for J
in reverse 1 .. Sum_Length
loop
898 Tmp_Int
:= X
(J
) + Y
(J
) + Carry
;
900 if Tmp_Int
>= Base
then
901 Tmp_Int
:= Tmp_Int
- Base
;
910 return Vector_To_Uint
(X
, L_Vec
(1) < Int_0
);
913 -- Find which one has bigger magnitude
915 if not (X_Bigger
or Y_Bigger
) then
916 for J
in L_Vec
'Range loop
917 if abs L_Vec
(J
) > abs R_Vec
(J
) then
920 elsif abs R_Vec
(J
) > abs L_Vec
(J
) then
927 -- If they have identical magnitude, just return 0, else swap
928 -- if necessary so that X had the bigger magnitude. Determine
929 -- if result is negative at this time.
933 if not (X_Bigger
or Y_Bigger
) then
937 if R_Vec
(1) < Int_0
then
946 if L_Vec
(1) < Int_0
then
951 -- Subtract Y from the bigger X
955 for J
in reverse 1 .. Sum_Length
loop
956 Tmp_Int
:= X
(J
) - Y
(J
) + Borrow
;
958 if Tmp_Int
< Int_0
then
959 Tmp_Int
:= Tmp_Int
+ Base
;
968 return Vector_To_Uint
(X
, Result_Neg
);
975 --------------------------
976 -- UI_Decimal_Digits_Hi --
977 --------------------------
979 function UI_Decimal_Digits_Hi
(U
: Uint
) return Nat
is
981 -- The maximum value of a "digit" is 32767, which is 5 decimal digits,
982 -- so an N_Digit number could take up to 5 times this number of digits.
983 -- This is certainly too high for large numbers but it is not worth
986 return 5 * N_Digits
(U
);
987 end UI_Decimal_Digits_Hi
;
989 --------------------------
990 -- UI_Decimal_Digits_Lo --
991 --------------------------
993 function UI_Decimal_Digits_Lo
(U
: Uint
) return Nat
is
995 -- The maximum value of a "digit" is 32767, which is more than four
996 -- decimal digits, but not a full five digits. The easily computed
997 -- minimum number of decimal digits is thus 1 + 4 * the number of
998 -- digits. This is certainly too low for large numbers but it is not
999 -- worth worrying about.
1001 return 1 + 4 * (N_Digits
(U
) - 1);
1002 end UI_Decimal_Digits_Lo
;
1008 function UI_Div
(Left
: Int
; Right
: Uint
) return Uint
is
1010 return UI_Div
(UI_From_Int
(Left
), Right
);
1013 function UI_Div
(Left
: Uint
; Right
: Int
) return Uint
is
1015 return UI_Div
(Left
, UI_From_Int
(Right
));
1018 function UI_Div
(Left
, Right
: Uint
) return Uint
is
1021 pragma Warnings
(Off
, Remainder
);
1025 Quotient
, Remainder
,
1026 Discard_Remainder
=> True);
1034 procedure UI_Div_Rem
1035 (Left
, Right
: Uint
;
1036 Quotient
: out Uint
;
1037 Remainder
: out Uint
;
1038 Discard_Quotient
: Boolean := False;
1039 Discard_Remainder
: Boolean := False)
1042 pragma Assert
(Right
/= Uint_0
);
1044 Quotient
:= No_Uint
;
1045 Remainder
:= No_Uint
;
1047 -- Cases where both operands are represented directly
1049 if Direct
(Left
) and then Direct
(Right
) then
1051 DV_Left
: constant Int
:= Direct_Val
(Left
);
1052 DV_Right
: constant Int
:= Direct_Val
(Right
);
1055 if not Discard_Quotient
then
1056 Quotient
:= UI_From_Int
(DV_Left
/ DV_Right
);
1059 if not Discard_Remainder
then
1060 Remainder
:= UI_From_Int
(DV_Left
rem DV_Right
);
1068 L_Length
: constant Int
:= N_Digits
(Left
);
1069 R_Length
: constant Int
:= N_Digits
(Right
);
1070 Q_Length
: constant Int
:= L_Length
- R_Length
+ 1;
1071 L_Vec
: UI_Vector
(1 .. L_Length
);
1072 R_Vec
: UI_Vector
(1 .. R_Length
);
1080 procedure UI_Div_Vector
1083 Quotient
: out UI_Vector
;
1084 Remainder
: out Int
);
1085 pragma Inline
(UI_Div_Vector
);
1086 -- Specialised variant for case where the divisor is a single digit
1088 procedure UI_Div_Vector
1091 Quotient
: out UI_Vector
;
1092 Remainder
: out Int
)
1098 for J
in L_Vec
'Range loop
1099 Tmp_Int
:= Remainder
* Base
+ abs L_Vec
(J
);
1100 Quotient
(Quotient
'First + J
- L_Vec
'First) := Tmp_Int
/ R_Int
;
1101 Remainder
:= Tmp_Int
rem R_Int
;
1104 if L_Vec
(L_Vec
'First) < Int_0
then
1105 Remainder
:= -Remainder
;
1109 -- Start of processing for UI_Div_Rem
1112 -- Result is zero if left operand is shorter than right
1114 if L_Length
< R_Length
then
1115 if not Discard_Quotient
then
1119 if not Discard_Remainder
then
1126 Init_Operand
(Left
, L_Vec
);
1127 Init_Operand
(Right
, R_Vec
);
1129 -- Case of right operand is single digit. Here we can simply divide
1130 -- each digit of the left operand by the divisor, from most to least
1131 -- significant, carrying the remainder to the next digit (just like
1132 -- ordinary long division by hand).
1134 if R_Length
= Int_1
then
1135 Tmp_Divisor
:= abs R_Vec
(1);
1138 Quotient_V
: UI_Vector
(1 .. L_Length
);
1141 UI_Div_Vector
(L_Vec
, Tmp_Divisor
, Quotient_V
, Remainder_I
);
1143 if not Discard_Quotient
then
1146 (Quotient_V
, (L_Vec
(1) < Int_0
xor R_Vec
(1) < Int_0
));
1149 if not Discard_Remainder
then
1150 Remainder
:= UI_From_Int
(Remainder_I
);
1157 -- The possible simple cases have been exhausted. Now turn to the
1158 -- algorithm D from the section of Knuth mentioned at the top of
1161 Algorithm_D
: declare
1162 Dividend
: UI_Vector
(1 .. L_Length
+ 1);
1163 Divisor
: UI_Vector
(1 .. R_Length
);
1164 Quotient_V
: UI_Vector
(1 .. Q_Length
);
1171 -- [ NORMALIZE ] (step D1 in the algorithm). First calculate the
1172 -- scale d, and then multiply Left and Right (u and v in the book)
1173 -- by d to get the dividend and divisor to work with.
1175 D
:= Base
/ (abs R_Vec
(1) + 1);
1178 Dividend
(2) := abs L_Vec
(1);
1180 for J
in 3 .. L_Length
+ Int_1
loop
1181 Dividend
(J
) := L_Vec
(J
- 1);
1184 Divisor
(1) := abs R_Vec
(1);
1186 for J
in Int_2
.. R_Length
loop
1187 Divisor
(J
) := R_Vec
(J
);
1192 -- Multiply Dividend by d
1195 for J
in reverse Dividend
'Range loop
1196 Tmp_Int
:= Dividend
(J
) * D
+ Carry
;
1197 Dividend
(J
) := Tmp_Int
rem Base
;
1198 Carry
:= Tmp_Int
/ Base
;
1201 -- Multiply Divisor by d
1204 for J
in reverse Divisor
'Range loop
1205 Tmp_Int
:= Divisor
(J
) * D
+ Carry
;
1206 Divisor
(J
) := Tmp_Int
rem Base
;
1207 Carry
:= Tmp_Int
/ Base
;
1211 -- Main loop of long division algorithm
1213 Divisor_Dig1
:= Divisor
(1);
1214 Divisor_Dig2
:= Divisor
(2);
1216 for J
in Quotient_V
'Range loop
1218 -- [ CALCULATE Q (hat) ] (step D3 in the algorithm)
1220 -- Note: this version of step D3 is from the original published
1221 -- algorithm, which is known to have a bug causing overflows.
1222 -- See: http://www-cs-faculty.stanford.edu/~uno/err2-2e.ps.gz
1223 -- and http://www-cs-faculty.stanford.edu/~uno/all2-pre.ps.gz.
1224 -- The code below is the fixed version of this step.
1226 Tmp_Int
:= Dividend
(J
) * Base
+ Dividend
(J
+ 1);
1230 Q_Guess
:= Tmp_Int
/ Divisor_Dig1
;
1231 R_Guess
:= Tmp_Int
rem Divisor_Dig1
;
1235 while Q_Guess
>= Base
1236 or else Divisor_Dig2
* Q_Guess
>
1237 R_Guess
* Base
+ Dividend
(J
+ 2)
1239 Q_Guess
:= Q_Guess
- 1;
1240 R_Guess
:= R_Guess
+ Divisor_Dig1
;
1241 exit when R_Guess
>= Base
;
1244 -- [ MULTIPLY & SUBTRACT ] (step D4). Q_Guess * Divisor is
1245 -- subtracted from the remaining dividend.
1248 for K
in reverse Divisor
'Range loop
1249 Tmp_Int
:= Dividend
(J
+ K
) - Q_Guess
* Divisor
(K
) + Carry
;
1250 Tmp_Dig
:= Tmp_Int
rem Base
;
1251 Carry
:= Tmp_Int
/ Base
;
1253 if Tmp_Dig
< Int_0
then
1254 Tmp_Dig
:= Tmp_Dig
+ Base
;
1258 Dividend
(J
+ K
) := Tmp_Dig
;
1261 Dividend
(J
) := Dividend
(J
) + Carry
;
1263 -- [ TEST REMAINDER ] & [ ADD BACK ] (steps D5 and D6)
1265 -- Here there is a slight difference from the book: the last
1266 -- carry is always added in above and below (cancelling each
1267 -- other). In fact the dividend going negative is used as
1270 -- If the Dividend went negative, then Q_Guess was off by
1271 -- one, so it is decremented, and the divisor is added back
1272 -- into the relevant portion of the dividend.
1274 if Dividend
(J
) < Int_0
then
1275 Q_Guess
:= Q_Guess
- 1;
1278 for K
in reverse Divisor
'Range loop
1279 Tmp_Int
:= Dividend
(J
+ K
) + Divisor
(K
) + Carry
;
1281 if Tmp_Int
>= Base
then
1282 Tmp_Int
:= Tmp_Int
- Base
;
1288 Dividend
(J
+ K
) := Tmp_Int
;
1291 Dividend
(J
) := Dividend
(J
) + Carry
;
1294 -- Finally we can get the next quotient digit
1296 Quotient_V
(J
) := Q_Guess
;
1299 -- [ UNNORMALIZE ] (step D8)
1301 if not Discard_Quotient
then
1302 Quotient
:= Vector_To_Uint
1303 (Quotient_V
, (L_Vec
(1) < Int_0
xor R_Vec
(1) < Int_0
));
1306 if not Discard_Remainder
then
1308 Remainder_V
: UI_Vector
(1 .. R_Length
);
1310 pragma Warnings
(Off
, Discard_Int
);
1313 (Dividend
(Dividend
'Last - R_Length
+ 1 .. Dividend
'Last),
1315 Remainder_V
, Discard_Int
);
1316 Remainder
:= Vector_To_Uint
(Remainder_V
, L_Vec
(1) < Int_0
);
1327 function UI_Eq
(Left
: Int
; Right
: Uint
) return Boolean is
1329 return not UI_Ne
(UI_From_Int
(Left
), Right
);
1332 function UI_Eq
(Left
: Uint
; Right
: Int
) return Boolean is
1334 return not UI_Ne
(Left
, UI_From_Int
(Right
));
1337 function UI_Eq
(Left
: Uint
; Right
: Uint
) return Boolean is
1339 return not UI_Ne
(Left
, Right
);
1346 function UI_Expon
(Left
: Int
; Right
: Uint
) return Uint
is
1348 return UI_Expon
(UI_From_Int
(Left
), Right
);
1351 function UI_Expon
(Left
: Uint
; Right
: Int
) return Uint
is
1353 return UI_Expon
(Left
, UI_From_Int
(Right
));
1356 function UI_Expon
(Left
: Int
; Right
: Int
) return Uint
is
1358 return UI_Expon
(UI_From_Int
(Left
), UI_From_Int
(Right
));
1361 function UI_Expon
(Left
: Uint
; Right
: Uint
) return Uint
is
1363 pragma Assert
(Right
>= Uint_0
);
1365 -- Any value raised to power of 0 is 1
1367 if Right
= Uint_0
then
1370 -- 0 to any positive power is 0
1372 elsif Left
= Uint_0
then
1375 -- 1 to any power is 1
1377 elsif Left
= Uint_1
then
1380 -- Any value raised to power of 1 is that value
1382 elsif Right
= Uint_1
then
1385 -- Cases which can be done by table lookup
1387 elsif Right
<= Uint_64
then
1389 -- 2 ** N for N in 2 .. 64
1391 if Left
= Uint_2
then
1393 Right_Int
: constant Int
:= Direct_Val
(Right
);
1396 if Right_Int
> UI_Power_2_Set
then
1397 for J
in UI_Power_2_Set
+ Int_1
.. Right_Int
loop
1398 UI_Power_2
(J
) := UI_Power_2
(J
- Int_1
) * Int_2
;
1399 Uints_Min
:= Uints
.Last
;
1400 Udigits_Min
:= Udigits
.Last
;
1403 UI_Power_2_Set
:= Right_Int
;
1406 return UI_Power_2
(Right_Int
);
1409 -- 10 ** N for N in 2 .. 64
1411 elsif Left
= Uint_10
then
1413 Right_Int
: constant Int
:= Direct_Val
(Right
);
1416 if Right_Int
> UI_Power_10_Set
then
1417 for J
in UI_Power_10_Set
+ Int_1
.. Right_Int
loop
1418 UI_Power_10
(J
) := UI_Power_10
(J
- Int_1
) * Int
(10);
1419 Uints_Min
:= Uints
.Last
;
1420 Udigits_Min
:= Udigits
.Last
;
1423 UI_Power_10_Set
:= Right_Int
;
1426 return UI_Power_10
(Right_Int
);
1431 -- If we fall through, then we have the general case (see Knuth 4.6.3)
1435 Squares
: Uint
:= Left
;
1436 Result
: Uint
:= Uint_1
;
1437 M
: constant Uintp
.Save_Mark
:= Uintp
.Mark
;
1441 if (Least_Sig_Digit
(N
) mod Int_2
) = Int_1
then
1442 Result
:= Result
* Squares
;
1446 exit when N
= Uint_0
;
1447 Squares
:= Squares
* Squares
;
1450 Uintp
.Release_And_Save
(M
, Result
);
1459 function UI_From_CC
(Input
: Char_Code
) return Uint
is
1461 return UI_From_Int
(Int
(Input
));
1468 function UI_From_Int
(Input
: Int
) return Uint
is
1472 if Min_Direct
<= Input
and then Input
<= Max_Direct
then
1473 return Uint
(Int
(Uint_Direct_Bias
) + Input
);
1476 -- If already in the hash table, return entry
1478 U
:= UI_Ints
.Get
(Input
);
1480 if U
/= No_Uint
then
1484 -- For values of larger magnitude, compute digits into a vector and call
1488 Max_For_Int
: constant := 3;
1489 -- Base is defined so that 3 Uint digits is sufficient to hold the
1490 -- largest possible Int value.
1492 V
: UI_Vector
(1 .. Max_For_Int
);
1494 Temp_Integer
: Int
:= Input
;
1497 for J
in reverse V
'Range loop
1498 V
(J
) := abs (Temp_Integer
rem Base
);
1499 Temp_Integer
:= Temp_Integer
/ Base
;
1502 U
:= Vector_To_Uint
(V
, Input
< Int_0
);
1503 UI_Ints
.Set
(Input
, U
);
1504 Uints_Min
:= Uints
.Last
;
1505 Udigits_Min
:= Udigits
.Last
;
1514 -- Lehmer's algorithm for GCD
1516 -- The idea is to avoid using multiple precision arithmetic wherever
1517 -- possible, substituting Int arithmetic instead. See Knuth volume II,
1518 -- Algorithm L (page 329).
1520 -- We use the same notation as Knuth (U_Hat standing for the obvious)
1522 function UI_GCD
(Uin
, Vin
: Uint
) return Uint
is
1524 -- Copies of Uin and Vin
1527 -- The most Significant digits of U,V
1529 A
, B
, C
, D
, T
, Q
, Den1
, Den2
: Int
;
1532 Marks
: constant Uintp
.Save_Mark
:= Uintp
.Mark
;
1533 Iterations
: Integer := 0;
1536 pragma Assert
(Uin
>= Vin
);
1537 pragma Assert
(Vin
>= Uint_0
);
1543 Iterations
:= Iterations
+ 1;
1550 UI_From_Int
(GCD
(Direct_Val
(V
), UI_To_Int
(U
rem V
)));
1554 Most_Sig_2_Digits
(U
, V
, U_Hat
, V_Hat
);
1561 -- We might overflow and get division by zero here. This just
1562 -- means we cannot take the single precision step
1566 exit when Den1
= Int_0
or else Den2
= Int_0
;
1568 -- Compute Q, the trial quotient
1570 Q
:= (U_Hat
+ A
) / Den1
;
1572 exit when Q
/= ((U_Hat
+ B
) / Den2
);
1574 -- A single precision step Euclid step will give same answer as a
1575 -- multiprecision one.
1585 T
:= U_Hat
- (Q
* V_Hat
);
1591 -- Take a multiprecision Euclid step
1595 -- No single precision steps take a regular Euclid step
1602 -- Use prior single precision steps to compute this Euclid step
1604 -- For constructs such as:
1605 -- sqrt_2: constant := 1.41421_35623_73095_04880_16887_24209_698;
1606 -- sqrt_eps: constant long_float := long_float( 1.0 / sqrt_2)
1607 -- ** long_float'machine_mantissa;
1609 -- we spend 80% of our time working on this step. Perhaps we need
1610 -- a special case Int / Uint dot product to speed things up. ???
1612 -- Alternatively we could increase the single precision iterations
1613 -- to handle Uint's of some small size ( <5 digits?). Then we
1614 -- would have more iterations on small Uint. On the code above, we
1615 -- only get 5 (on average) single precision iterations per large
1618 Tmp_UI
:= (UI_From_Int
(A
) * U
) + (UI_From_Int
(B
) * V
);
1619 V
:= (UI_From_Int
(C
) * U
) + (UI_From_Int
(D
) * V
);
1623 -- If the operands are very different in magnitude, the loop will
1624 -- generate large amounts of short-lived data, which it is worth
1625 -- removing periodically.
1627 if Iterations
> 100 then
1628 Release_And_Save
(Marks
, U
, V
);
1638 function UI_Ge
(Left
: Int
; Right
: Uint
) return Boolean is
1640 return not UI_Lt
(UI_From_Int
(Left
), Right
);
1643 function UI_Ge
(Left
: Uint
; Right
: Int
) return Boolean is
1645 return not UI_Lt
(Left
, UI_From_Int
(Right
));
1648 function UI_Ge
(Left
: Uint
; Right
: Uint
) return Boolean is
1650 return not UI_Lt
(Left
, Right
);
1657 function UI_Gt
(Left
: Int
; Right
: Uint
) return Boolean is
1659 return UI_Lt
(Right
, UI_From_Int
(Left
));
1662 function UI_Gt
(Left
: Uint
; Right
: Int
) return Boolean is
1664 return UI_Lt
(UI_From_Int
(Right
), Left
);
1667 function UI_Gt
(Left
: Uint
; Right
: Uint
) return Boolean is
1669 return UI_Lt
(Left
=> Right
, Right
=> Left
);
1676 procedure UI_Image
(Input
: Uint
; Format
: UI_Format
:= Auto
) is
1678 Image_Out
(Input
, True, Format
);
1681 -------------------------
1682 -- UI_Is_In_Int_Range --
1683 -------------------------
1685 function UI_Is_In_Int_Range
(Input
: Uint
) return Boolean is
1687 -- Make sure we don't get called before Initialize
1689 pragma Assert
(Uint_Int_First
/= Uint_0
);
1691 if Direct
(Input
) then
1694 return Input
>= Uint_Int_First
1695 and then Input
<= Uint_Int_Last
;
1697 end UI_Is_In_Int_Range
;
1703 function UI_Le
(Left
: Int
; Right
: Uint
) return Boolean is
1705 return not UI_Lt
(Right
, UI_From_Int
(Left
));
1708 function UI_Le
(Left
: Uint
; Right
: Int
) return Boolean is
1710 return not UI_Lt
(UI_From_Int
(Right
), Left
);
1713 function UI_Le
(Left
: Uint
; Right
: Uint
) return Boolean is
1715 return not UI_Lt
(Left
=> Right
, Right
=> Left
);
1722 function UI_Lt
(Left
: Int
; Right
: Uint
) return Boolean is
1724 return UI_Lt
(UI_From_Int
(Left
), Right
);
1727 function UI_Lt
(Left
: Uint
; Right
: Int
) return Boolean is
1729 return UI_Lt
(Left
, UI_From_Int
(Right
));
1732 function UI_Lt
(Left
: Uint
; Right
: Uint
) return Boolean is
1734 -- Quick processing for identical arguments
1736 if Int
(Left
) = Int
(Right
) then
1739 -- Quick processing for both arguments directly represented
1741 elsif Direct
(Left
) and then Direct
(Right
) then
1742 return Int
(Left
) < Int
(Right
);
1744 -- At least one argument is more than one digit long
1748 L_Length
: constant Int
:= N_Digits
(Left
);
1749 R_Length
: constant Int
:= N_Digits
(Right
);
1751 L_Vec
: UI_Vector
(1 .. L_Length
);
1752 R_Vec
: UI_Vector
(1 .. R_Length
);
1755 Init_Operand
(Left
, L_Vec
);
1756 Init_Operand
(Right
, R_Vec
);
1758 if L_Vec
(1) < Int_0
then
1760 -- First argument negative, second argument non-negative
1762 if R_Vec
(1) >= Int_0
then
1765 -- Both arguments negative
1768 if L_Length
/= R_Length
then
1769 return L_Length
> R_Length
;
1771 elsif L_Vec
(1) /= R_Vec
(1) then
1772 return L_Vec
(1) < R_Vec
(1);
1775 for J
in 2 .. L_Vec
'Last loop
1776 if L_Vec
(J
) /= R_Vec
(J
) then
1777 return L_Vec
(J
) > R_Vec
(J
);
1786 -- First argument non-negative, second argument negative
1788 if R_Vec
(1) < Int_0
then
1791 -- Both arguments non-negative
1794 if L_Length
/= R_Length
then
1795 return L_Length
< R_Length
;
1797 for J
in L_Vec
'Range loop
1798 if L_Vec
(J
) /= R_Vec
(J
) then
1799 return L_Vec
(J
) < R_Vec
(J
);
1815 function UI_Max
(Left
: Int
; Right
: Uint
) return Uint
is
1817 return UI_Max
(UI_From_Int
(Left
), Right
);
1820 function UI_Max
(Left
: Uint
; Right
: Int
) return Uint
is
1822 return UI_Max
(Left
, UI_From_Int
(Right
));
1825 function UI_Max
(Left
: Uint
; Right
: Uint
) return Uint
is
1827 if Left
>= Right
then
1838 function UI_Min
(Left
: Int
; Right
: Uint
) return Uint
is
1840 return UI_Min
(UI_From_Int
(Left
), Right
);
1843 function UI_Min
(Left
: Uint
; Right
: Int
) return Uint
is
1845 return UI_Min
(Left
, UI_From_Int
(Right
));
1848 function UI_Min
(Left
: Uint
; Right
: Uint
) return Uint
is
1850 if Left
<= Right
then
1861 function UI_Mod
(Left
: Int
; Right
: Uint
) return Uint
is
1863 return UI_Mod
(UI_From_Int
(Left
), Right
);
1866 function UI_Mod
(Left
: Uint
; Right
: Int
) return Uint
is
1868 return UI_Mod
(Left
, UI_From_Int
(Right
));
1871 function UI_Mod
(Left
: Uint
; Right
: Uint
) return Uint
is
1872 Urem
: constant Uint
:= Left
rem Right
;
1875 if (Left
< Uint_0
) = (Right
< Uint_0
)
1876 or else Urem
= Uint_0
1880 return Right
+ Urem
;
1884 -------------------------------
1885 -- UI_Modular_Exponentiation --
1886 -------------------------------
1888 function UI_Modular_Exponentiation
1891 Modulo
: Uint
) return Uint
1893 M
: constant Save_Mark
:= Mark
;
1895 Result
: Uint
:= Uint_1
;
1897 Exponent
: Uint
:= E
;
1900 while Exponent
/= Uint_0
loop
1901 if Least_Sig_Digit
(Exponent
) rem Int
'(2) = Int'(1) then
1902 Result
:= (Result
* Base
) rem Modulo
;
1905 Exponent
:= Exponent
/ Uint_2
;
1906 Base
:= (Base
* Base
) rem Modulo
;
1909 Release_And_Save
(M
, Result
);
1911 end UI_Modular_Exponentiation
;
1913 ------------------------
1914 -- UI_Modular_Inverse --
1915 ------------------------
1917 function UI_Modular_Inverse
(N
: Uint
; Modulo
: Uint
) return Uint
is
1918 M
: constant Save_Mark
:= Mark
;
1936 UI_Div_Rem
(U
, V
, Quotient
=> Q
, Remainder
=> R
);
1946 exit when R
= Uint_1
;
1949 if S
= Int
'(-1) then
1953 Release_And_Save (M, X);
1955 end UI_Modular_Inverse;
1961 function UI_Mul (Left : Int; Right : Uint) return Uint is
1963 return UI_Mul (UI_From_Int (Left), Right);
1966 function UI_Mul (Left : Uint; Right : Int) return Uint is
1968 return UI_Mul (Left, UI_From_Int (Right));
1971 function UI_Mul (Left : Uint; Right : Uint) return Uint is
1973 -- Case where product fits in the range of a 32-bit integer
1975 if Int (Left) <= Int (Uint_Max_Simple_Mul)
1977 Int (Right) <= Int (Uint_Max_Simple_Mul)
1979 return UI_From_Int (Direct_Val (Left) * Direct_Val (Right));
1982 -- Otherwise we have the general case (Algorithm M in Knuth)
1985 L_Length : constant Int := N_Digits (Left);
1986 R_Length : constant Int := N_Digits (Right);
1987 L_Vec : UI_Vector (1 .. L_Length);
1988 R_Vec : UI_Vector (1 .. R_Length);
1992 Init_Operand (Left, L_Vec);
1993 Init_Operand (Right, R_Vec);
1994 Neg := (L_Vec (1) < Int_0) xor (R_Vec (1) < Int_0);
1995 L_Vec (1) := abs (L_Vec (1));
1996 R_Vec (1) := abs (R_Vec (1));
1998 Algorithm_M : declare
1999 Product : UI_Vector (1 .. L_Length + R_Length);
2004 for J in Product'Range loop
2008 for J in reverse R_Vec'Range loop
2010 for K in reverse L_Vec'Range loop
2012 L_Vec (K) * R_Vec (J) + Product (J + K) + Carry;
2013 Product (J + K) := Tmp_Sum rem Base;
2014 Carry := Tmp_Sum / Base;
2017 Product (J) := Carry;
2020 return Vector_To_Uint (Product, Neg);
2029 function UI_Ne (Left : Int; Right : Uint) return Boolean is
2031 return UI_Ne (UI_From_Int (Left), Right);
2034 function UI_Ne (Left : Uint; Right : Int) return Boolean is
2036 return UI_Ne (Left, UI_From_Int (Right));
2039 function UI_Ne (Left : Uint; Right : Uint) return Boolean is
2041 -- Quick processing for identical arguments. Note that this takes
2042 -- care of the case of two No_Uint arguments.
2044 if Int (Left) = Int (Right) then
2048 -- See if left operand directly represented
2050 if Direct (Left) then
2052 -- If right operand directly represented then compare
2054 if Direct (Right) then
2055 return Int (Left) /= Int (Right);
2057 -- Left operand directly represented, right not, must be unequal
2063 -- Right operand directly represented, left not, must be unequal
2065 elsif Direct (Right) then
2069 -- Otherwise both multi-word, do comparison
2072 Size : constant Int := N_Digits (Left);
2077 if Size /= N_Digits (Right) then
2081 Left_Loc := Uints.Table (Left).Loc;
2082 Right_Loc := Uints.Table (Right).Loc;
2084 for J in Int_0 .. Size - Int_1 loop
2085 if Udigits.Table (Left_Loc + J) /=
2086 Udigits.Table (Right_Loc + J)
2100 function UI_Negate (Right : Uint) return Uint is
2102 -- Case where input is directly represented. Note that since the range
2103 -- of Direct values is non-symmetrical, the result may not be directly
2104 -- represented, this is taken care of in UI_From_Int.
2106 if Direct (Right) then
2107 return UI_From_Int (-Direct_Val (Right));
2109 -- Full processing for multi-digit case. Note that we cannot just copy
2110 -- the value to the end of the table negating the first digit, since the
2111 -- range of Direct values is non-symmetrical, so we can have a negative
2112 -- value that is not Direct whose negation can be represented directly.
2116 R_Length : constant Int := N_Digits (Right);
2117 R_Vec : UI_Vector (1 .. R_Length);
2121 Init_Operand (Right, R_Vec);
2122 Neg := R_Vec (1) > Int_0;
2123 R_Vec (1) := abs R_Vec (1);
2124 return Vector_To_Uint (R_Vec, Neg);
2133 function UI_Rem (Left : Int; Right : Uint) return Uint is
2135 return UI_Rem (UI_From_Int (Left), Right);
2138 function UI_Rem (Left : Uint; Right : Int) return Uint is
2140 return UI_Rem (Left, UI_From_Int (Right));
2143 function UI_Rem (Left, Right : Uint) return Uint is
2146 pragma Warnings (Off, Quotient);
2149 pragma Assert (Right /= Uint_0);
2151 if Direct (Right) and then Direct (Left) then
2152 return UI_From_Int (Direct_Val (Left) rem Direct_Val (Right));
2156 (Left, Right, Quotient, Remainder, Discard_Quotient => True);
2165 function UI_Sub (Left : Int; Right : Uint) return Uint is
2167 return UI_Add (Left, -Right);
2170 function UI_Sub (Left : Uint; Right : Int) return Uint is
2172 return UI_Add (Left, -Right);
2175 function UI_Sub (Left : Uint; Right : Uint) return Uint is
2177 if Direct (Left) and then Direct (Right) then
2178 return UI_From_Int (Direct_Val (Left) - Direct_Val (Right));
2180 return UI_Add (Left, -Right);
2188 function UI_To_CC (Input : Uint) return Char_Code is
2190 if Direct (Input) then
2191 return Char_Code (Direct_Val (Input));
2193 -- Case of input is more than one digit
2197 In_Length : constant Int := N_Digits (Input);
2198 In_Vec : UI_Vector (1 .. In_Length);
2202 Init_Operand (Input, In_Vec);
2204 -- We assume value is positive
2207 for Idx in In_Vec'Range loop
2208 Ret_CC := Ret_CC * Char_Code (Base) +
2209 Char_Code (abs In_Vec (Idx));
2221 function UI_To_Int (Input : Uint) return Int is
2222 pragma Assert (Input /= No_Uint);
2225 if Direct (Input) then
2226 return Direct_Val (Input);
2228 -- Case of input is more than one digit
2232 In_Length : constant Int := N_Digits (Input);
2233 In_Vec : UI_Vector (1 .. In_Length);
2237 -- Uints of more than one digit could be outside the range for
2238 -- Ints. Caller should have checked for this if not certain.
2239 -- Fatal error to attempt to convert from value outside Int'Range.
2241 pragma Assert (UI_Is_In_Int_Range (Input));
2243 -- Otherwise, proceed ahead, we are OK
2245 Init_Operand (Input, In_Vec);
2248 -- Calculate -|Input| and then negates if value is positive. This
2249 -- handles our current definition of Int (based on 2s complement).
2250 -- Is it secure enough???
2252 for Idx in In_Vec'Range loop
2253 Ret_Int := Ret_Int * Base - abs In_Vec (Idx);
2256 if In_Vec (1) < Int_0 then
2269 procedure UI_Write (Input : Uint; Format : UI_Format := Auto) is
2271 Image_Out (Input, False, Format);
2274 ---------------------
2275 -- Vector_To_Uint --
2276 ---------------------
2278 function Vector_To_Uint
2279 (In_Vec : UI_Vector;
2287 -- The vector can contain leading zeros. These are not stored in the
2288 -- table, so loop through the vector looking for first non-zero digit
2290 for J in In_Vec'Range loop
2291 if In_Vec (J) /= Int_0 then
2293 -- The length of the value is the length of the rest of the vector
2295 Size := In_Vec'Last - J + 1;
2297 -- One digit value can always be represented directly
2299 if Size = Int_1 then
2301 return Uint (Int (Uint_Direct_Bias) - In_Vec (J));
2303 return Uint (Int (Uint_Direct_Bias) + In_Vec (J));
2306 -- Positive two digit values may be in direct representation range
2308 elsif Size = Int_2 and then not Negative then
2309 Val := In_Vec (J) * Base + In_Vec (J + 1);
2311 if Val <= Max_Direct then
2312 return Uint (Int (Uint_Direct_Bias) + Val);
2316 -- The value is outside the direct representation range and must
2317 -- therefore be stored in the table. Expand the table to contain
2318 -- the count and digits. The index of the new table entry will be
2319 -- returned as the result.
2321 Uints.Append ((Length => Size, Loc => Udigits.Last + 1));
2329 Udigits.Append (Val);
2331 for K in 2 .. Size loop
2332 Udigits.Append (In_Vec (J + K - 1));
2339 -- Dropped through loop only if vector contained all zeros