1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2009, 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
160 function Sum_Digits
(Left
: Uint
; Sign
: Int
) return Int
;
161 -- If Sign = 1 return the sum of the "digits" of Abs (Left). If the total
162 -- has more then one digit then return Sum_Digits of total.
164 function Sum_Double_Digits
(Left
: Uint
; Sign
: Int
) return Int
;
165 -- Same as above but work in New_Base = Base * Base
170 Remainder
: out Uint
;
171 Discard_Quotient
: Boolean;
172 Discard_Remainder
: Boolean);
173 -- Compute Euclidean division of Left by Right, and return Quotient and
174 -- signed Remainder (Left rem Right).
176 -- If Discard_Quotient is True, Quotient is left unchanged.
177 -- If Discard_Remainder is True, Remainder is left unchanged.
179 function Vector_To_Uint
181 Negative
: Boolean) return Uint
;
182 -- Functions that calculate values in UI_Vectors, call this function to
183 -- create and return the Uint value. In_Vec contains the multiple precision
184 -- (Base) representation of a non-negative value. Leading zeroes are
185 -- permitted. Negative is set if the desired result is the negative of the
186 -- given value. The result will be either the appropriate directly
187 -- represented value, or a table entry in the proper canonical format is
188 -- created and returned.
190 -- Note that Init_Operand puts a signed value in the result vector, but
191 -- Vector_To_Uint is always presented with a non-negative value. The
192 -- processing of signs is something that is done by the caller before
193 -- calling Vector_To_Uint.
199 function Direct
(U
: Uint
) return Boolean is
201 return Int
(U
) <= Int
(Uint_Direct_Last
);
208 function Direct_Val
(U
: Uint
) return Int
is
210 pragma Assert
(Direct
(U
));
211 return Int
(U
) - Int
(Uint_Direct_Bias
);
218 function GCD
(Jin
, Kin
: Int
) return Int
is
222 pragma Assert
(Jin
>= Kin
);
223 pragma Assert
(Kin
>= Int_0
);
227 while K
/= Uint_0
loop
240 function Hash_Num
(F
: Int
) return Hnum
is
242 return Standard
."mod" (F
, Hnum
'Range_Length);
254 Marks
: constant Uintp
.Save_Mark
:= Uintp
.Mark
;
258 Digs_Output
: Natural := 0;
259 -- Counts digits output. In hex mode, but not in decimal mode, we
260 -- put an underline after every four hex digits that are output.
262 Exponent
: Natural := 0;
263 -- If the number is too long to fit in the buffer, we switch to an
264 -- approximate output format with an exponent. This variable records
265 -- the exponent value.
267 function Better_In_Hex
return Boolean;
268 -- Determines if it is better to generate digits in base 16 (result
269 -- is true) or base 10 (result is false). The choice is purely a
270 -- matter of convenience and aesthetics, so it does not matter which
271 -- value is returned from a correctness point of view.
273 procedure Image_Char
(C
: Character);
274 -- Internal procedure to output one character
276 procedure Image_Exponent
(N
: Natural);
277 -- Output non-zero exponent. Note that we only use the exponent form in
278 -- the buffer case, so we know that To_Buffer is true.
280 procedure Image_Uint
(U
: Uint
);
281 -- Internal procedure to output characters of non-negative Uint
287 function Better_In_Hex
return Boolean is
288 T16
: constant Uint
:= Uint_2
** Int
'(16);
294 -- Small values up to 2**16 can always be in decimal
300 -- Otherwise, see if we are a power of 2 or one less than a power
301 -- of 2. For the moment these are the only cases printed in hex.
303 if A mod Uint_2 = Uint_1 then
308 if A mod T16 /= Uint_0 then
318 while A > Uint_2 loop
319 if A mod Uint_2 /= Uint_0 then
334 procedure Image_Char (C : Character) is
337 if UI_Image_Length + 6 > UI_Image_Max then
338 Exponent := Exponent + 1;
340 UI_Image_Length := UI_Image_Length + 1;
341 UI_Image_Buffer (UI_Image_Length) := C;
352 procedure Image_Exponent (N : Natural) is
355 Image_Exponent (N / 10);
358 UI_Image_Length := UI_Image_Length + 1;
359 UI_Image_Buffer (UI_Image_Length) :=
360 Character'Val (Character'Pos ('0') + N mod 10);
367 procedure Image_Uint (U : Uint) is
368 H : constant array (Int range 0 .. 15) of Character :=
373 Image_Uint (U / Base);
376 if Digs_Output = 4 and then Base = Uint_16 then
381 Image_Char (H (UI_To_Int (U rem Base)));
383 Digs_Output := Digs_Output + 1;
386 -- Start of processing for Image_Out
389 if Input = No_Uint then
394 UI_Image_Length := 0;
396 if Input < Uint_0 then
404 or else (Format = Auto and then Better_In_Hex)
418 if Exponent /= 0 then
419 UI_Image_Length := UI_Image_Length + 1;
420 UI_Image_Buffer (UI_Image_Length) := 'E
';
421 Image_Exponent (Exponent);
424 Uintp.Release (Marks);
431 procedure Init_Operand (UI : Uint; Vec : out UI_Vector) is
434 pragma Assert (Vec'First = Int'(1));
438 Vec
(1) := Direct_Val
(UI
);
440 if Vec
(1) >= Base
then
441 Vec
(2) := Vec
(1) rem Base
;
442 Vec
(1) := Vec
(1) / Base
;
446 Loc
:= Uints
.Table
(UI
).Loc
;
448 for J
in 1 .. Uints
.Table
(UI
).Length
loop
449 Vec
(J
) := Udigits
.Table
(Loc
+ J
- 1);
458 procedure Initialize
is
463 Uint_Int_First
:= UI_From_Int
(Int
'First);
464 Uint_Int_Last
:= UI_From_Int
(Int
'Last);
466 UI_Power_2
(0) := Uint_1
;
469 UI_Power_10
(0) := Uint_1
;
470 UI_Power_10_Set
:= 0;
472 Uints_Min
:= Uints
.Last
;
473 Udigits_Min
:= Udigits
.Last
;
478 ---------------------
479 -- Least_Sig_Digit --
480 ---------------------
482 function Least_Sig_Digit
(Arg
: Uint
) return Int
is
487 V
:= Direct_Val
(Arg
);
493 -- Note that this result may be negative
500 (Uints
.Table
(Arg
).Loc
+ Uints
.Table
(Arg
).Length
- 1);
508 function Mark
return Save_Mark
is
510 return (Save_Uint
=> Uints
.Last
, Save_Udigit
=> Udigits
.Last
);
513 -----------------------
514 -- Most_Sig_2_Digits --
515 -----------------------
517 procedure Most_Sig_2_Digits
524 pragma Assert
(Left
>= Right
);
526 if Direct
(Left
) then
527 Left_Hat
:= Direct_Val
(Left
);
528 Right_Hat
:= Direct_Val
(Right
);
534 Udigits
.Table
(Uints
.Table
(Left
).Loc
);
536 Udigits
.Table
(Uints
.Table
(Left
).Loc
+ 1);
539 -- It is not so clear what to return when Arg is negative???
541 Left_Hat
:= abs (L1
) * Base
+ L2
;
546 Length_L
: constant Int
:= Uints
.Table
(Left
).Length
;
553 if Direct
(Right
) then
554 T
:= Direct_Val
(Left
);
555 R1
:= abs (T
/ Base
);
560 R1
:= abs (Udigits
.Table
(Uints
.Table
(Right
).Loc
));
561 R2
:= Udigits
.Table
(Uints
.Table
(Right
).Loc
+ 1);
562 Length_R
:= Uints
.Table
(Right
).Length
;
565 if Length_L
= Length_R
then
566 Right_Hat
:= R1
* Base
+ R2
;
567 elsif Length_L
= Length_R
+ Int_1
then
573 end Most_Sig_2_Digits
;
579 -- Note: N_Digits returns 1 for No_Uint
581 function N_Digits
(Input
: Uint
) return Int
is
583 if Direct
(Input
) then
584 if Direct_Val
(Input
) >= Base
then
591 return Uints
.Table
(Input
).Length
;
599 function Num_Bits
(Input
: Uint
) return Nat
is
604 -- Largest negative number has to be handled specially, since it is in
605 -- Int_Range, but we cannot take the absolute value.
607 if Input
= Uint_Int_First
then
610 -- For any other number in Int_Range, get absolute value of number
612 elsif UI_Is_In_Int_Range
(Input
) then
613 Num
:= abs (UI_To_Int
(Input
));
616 -- If not in Int_Range then initialize bit count for all low order
617 -- words, and set number to high order digit.
620 Bits
:= Base_Bits
* (Uints
.Table
(Input
).Length
- 1);
621 Num
:= abs (Udigits
.Table
(Uints
.Table
(Input
).Loc
));
624 -- Increase bit count for remaining value in Num
626 while Types
.">" (Num
, 0) loop
638 procedure pid
(Input
: Uint
) is
640 UI_Write
(Input
, Decimal
);
648 procedure pih
(Input
: Uint
) is
650 UI_Write
(Input
, Hex
);
658 procedure Release
(M
: Save_Mark
) is
660 Uints
.Set_Last
(Uint
'Max (M
.Save_Uint
, Uints_Min
));
661 Udigits
.Set_Last
(Int
'Max (M
.Save_Udigit
, Udigits_Min
));
664 ----------------------
665 -- Release_And_Save --
666 ----------------------
668 procedure Release_And_Save
(M
: Save_Mark
; UI
: in out Uint
) is
675 UE_Len
: constant Pos
:= Uints
.Table
(UI
).Length
;
676 UE_Loc
: constant Int
:= Uints
.Table
(UI
).Loc
;
678 UD
: constant Udigits
.Table_Type
(1 .. UE_Len
) :=
679 Udigits
.Table
(UE_Loc
.. UE_Loc
+ UE_Len
- 1);
684 Uints
.Append
((Length
=> UE_Len
, Loc
=> Udigits
.Last
+ 1));
687 for J
in 1 .. UE_Len
loop
688 Udigits
.Append
(UD
(J
));
692 end Release_And_Save
;
694 procedure Release_And_Save
(M
: Save_Mark
; UI1
, UI2
: in out Uint
) is
697 Release_And_Save
(M
, UI2
);
699 elsif Direct
(UI2
) then
700 Release_And_Save
(M
, UI1
);
704 UE1_Len
: constant Pos
:= Uints
.Table
(UI1
).Length
;
705 UE1_Loc
: constant Int
:= Uints
.Table
(UI1
).Loc
;
707 UD1
: constant Udigits
.Table_Type
(1 .. UE1_Len
) :=
708 Udigits
.Table
(UE1_Loc
.. UE1_Loc
+ UE1_Len
- 1);
710 UE2_Len
: constant Pos
:= Uints
.Table
(UI2
).Length
;
711 UE2_Loc
: constant Int
:= Uints
.Table
(UI2
).Loc
;
713 UD2
: constant Udigits
.Table_Type
(1 .. UE2_Len
) :=
714 Udigits
.Table
(UE2_Loc
.. UE2_Loc
+ UE2_Len
- 1);
719 Uints
.Append
((Length
=> UE1_Len
, Loc
=> Udigits
.Last
+ 1));
722 for J
in 1 .. UE1_Len
loop
723 Udigits
.Append
(UD1
(J
));
726 Uints
.Append
((Length
=> UE2_Len
, Loc
=> Udigits
.Last
+ 1));
729 for J
in 1 .. UE2_Len
loop
730 Udigits
.Append
(UD2
(J
));
734 end Release_And_Save
;
740 -- This is done in one pass
742 -- Mathematically: assume base congruent to 1 and compute an equivalent
745 -- If Sign = -1 return the alternating sum of the "digits"
747 -- D1 - D2 + D3 - D4 + D5 ...
749 -- (where D1 is Least Significant Digit)
751 -- Mathematically: assume base congruent to -1 and compute an equivalent
754 -- This is used in Rem and Base is assumed to be 2 ** 15
756 -- Note: The next two functions are very similar, any style changes made
757 -- to one should be reflected in both. These would be simpler if we
758 -- worked base 2 ** 32.
760 function Sum_Digits
(Left
: Uint
; Sign
: Int
) return Int
is
762 pragma Assert
(Sign
= Int_1
or else Sign
= Int
(-1));
764 -- First try simple case;
766 if Direct
(Left
) then
768 Tmp_Int
: Int
:= Direct_Val
(Left
);
771 if Tmp_Int
>= Base
then
772 Tmp_Int
:= (Tmp_Int
/ Base
) +
773 Sign
* (Tmp_Int
rem Base
);
775 -- Now Tmp_Int is in [-(Base - 1) .. 2 * (Base - 1)]
777 if Tmp_Int
>= Base
then
781 Tmp_Int
:= (Tmp_Int
/ Base
) + 1;
785 -- Now Tmp_Int is in [-(Base - 1) .. (Base - 1)]
792 -- Otherwise full circuit is needed
796 L_Length
: constant Int
:= N_Digits
(Left
);
797 L_Vec
: UI_Vector
(1 .. L_Length
);
803 Init_Operand
(Left
, L_Vec
);
804 L_Vec
(1) := abs L_Vec
(1);
809 for J
in reverse 1 .. L_Length
loop
810 Tmp_Int
:= Tmp_Int
+ Alt
* (L_Vec
(J
) + Carry
);
812 -- Tmp_Int is now between [-2 * Base + 1 .. 2 * Base - 1],
813 -- since old Tmp_Int is between [-(Base - 1) .. Base - 1]
814 -- and L_Vec is in [0 .. Base - 1] and Carry in [-1 .. 1]
816 if Tmp_Int
>= Base
then
817 Tmp_Int
:= Tmp_Int
- Base
;
820 elsif Tmp_Int
<= -Base
then
821 Tmp_Int
:= Tmp_Int
+ Base
;
828 -- Tmp_Int is now between [-Base + 1 .. Base - 1]
833 Tmp_Int
:= Tmp_Int
+ Alt
* Carry
;
835 -- Tmp_Int is now between [-Base .. Base]
837 if Tmp_Int
>= Base
then
838 Tmp_Int
:= Tmp_Int
- Base
+ Alt
* Sign
* 1;
840 elsif Tmp_Int
<= -Base
then
841 Tmp_Int
:= Tmp_Int
+ Base
+ Alt
* Sign
* (-1);
844 -- Now Tmp_Int is in [-(Base - 1) .. (Base - 1)]
851 -----------------------
852 -- Sum_Double_Digits --
853 -----------------------
855 -- Note: This is used in Rem, Base is assumed to be 2 ** 15
857 function Sum_Double_Digits
(Left
: Uint
; Sign
: Int
) return Int
is
859 -- First try simple case;
861 pragma Assert
(Sign
= Int_1
or else Sign
= Int
(-1));
863 if Direct
(Left
) then
864 return Direct_Val
(Left
);
866 -- Otherwise full circuit is needed
870 L_Length
: constant Int
:= N_Digits
(Left
);
871 L_Vec
: UI_Vector
(1 .. L_Length
);
879 Init_Operand
(Left
, L_Vec
);
880 L_Vec
(1) := abs L_Vec
(1);
888 Least_Sig_Int
:= Least_Sig_Int
+ Alt
* (L_Vec
(J
) + Carry
);
890 -- Least is in [-2 Base + 1 .. 2 * Base - 1]
891 -- Since L_Vec in [0 .. Base - 1] and Carry in [-1 .. 1]
892 -- and old Least in [-Base + 1 .. Base - 1]
894 if Least_Sig_Int
>= Base
then
895 Least_Sig_Int
:= Least_Sig_Int
- Base
;
898 elsif Least_Sig_Int
<= -Base
then
899 Least_Sig_Int
:= Least_Sig_Int
+ Base
;
906 -- Least is now in [-Base + 1 .. Base - 1]
908 Most_Sig_Int
:= Most_Sig_Int
+ Alt
* (L_Vec
(J
- 1) + Carry
);
910 -- Most is in [-2 Base + 1 .. 2 * Base - 1]
911 -- Since L_Vec in [0 .. Base - 1] and Carry in [-1 .. 1]
912 -- and old Most in [-Base + 1 .. Base - 1]
914 if Most_Sig_Int
>= Base
then
915 Most_Sig_Int
:= Most_Sig_Int
- Base
;
918 elsif Most_Sig_Int
<= -Base
then
919 Most_Sig_Int
:= Most_Sig_Int
+ Base
;
925 -- Most is now in [-Base + 1 .. Base - 1]
932 Least_Sig_Int
:= Least_Sig_Int
+ Alt
* (L_Vec
(J
) + Carry
);
934 Least_Sig_Int
:= Least_Sig_Int
+ Alt
* Carry
;
937 if Least_Sig_Int
>= Base
then
938 Least_Sig_Int
:= Least_Sig_Int
- Base
;
939 Most_Sig_Int
:= Most_Sig_Int
+ Alt
* 1;
941 elsif Least_Sig_Int
<= -Base
then
942 Least_Sig_Int
:= Least_Sig_Int
+ Base
;
943 Most_Sig_Int
:= Most_Sig_Int
+ Alt
* (-1);
946 if Most_Sig_Int
>= Base
then
947 Most_Sig_Int
:= Most_Sig_Int
- Base
;
950 Least_Sig_Int
+ Alt
* 1; -- cannot overflow again
952 elsif Most_Sig_Int
<= -Base
then
953 Most_Sig_Int
:= Most_Sig_Int
+ Base
;
956 Least_Sig_Int
+ Alt
* (-1); -- cannot overflow again.
959 return Most_Sig_Int
* Base
+ Least_Sig_Int
;
962 end Sum_Double_Digits
;
968 procedure Tree_Read
is
973 Tree_Read_Int
(Int
(Uint_Int_First
));
974 Tree_Read_Int
(Int
(Uint_Int_Last
));
975 Tree_Read_Int
(UI_Power_2_Set
);
976 Tree_Read_Int
(UI_Power_10_Set
);
977 Tree_Read_Int
(Int
(Uints_Min
));
978 Tree_Read_Int
(Udigits_Min
);
980 for J
in 0 .. UI_Power_2_Set
loop
981 Tree_Read_Int
(Int
(UI_Power_2
(J
)));
984 for J
in 0 .. UI_Power_10_Set
loop
985 Tree_Read_Int
(Int
(UI_Power_10
(J
)));
994 procedure Tree_Write
is
999 Tree_Write_Int
(Int
(Uint_Int_First
));
1000 Tree_Write_Int
(Int
(Uint_Int_Last
));
1001 Tree_Write_Int
(UI_Power_2_Set
);
1002 Tree_Write_Int
(UI_Power_10_Set
);
1003 Tree_Write_Int
(Int
(Uints_Min
));
1004 Tree_Write_Int
(Udigits_Min
);
1006 for J
in 0 .. UI_Power_2_Set
loop
1007 Tree_Write_Int
(Int
(UI_Power_2
(J
)));
1010 for J
in 0 .. UI_Power_10_Set
loop
1011 Tree_Write_Int
(Int
(UI_Power_10
(J
)));
1020 function UI_Abs
(Right
: Uint
) return Uint
is
1022 if Right
< Uint_0
then
1033 function UI_Add
(Left
: Int
; Right
: Uint
) return Uint
is
1035 return UI_Add
(UI_From_Int
(Left
), Right
);
1038 function UI_Add
(Left
: Uint
; Right
: Int
) return Uint
is
1040 return UI_Add
(Left
, UI_From_Int
(Right
));
1043 function UI_Add
(Left
: Uint
; Right
: Uint
) return Uint
is
1045 -- Simple cases of direct operands and addition of zero
1047 if Direct
(Left
) then
1048 if Direct
(Right
) then
1049 return UI_From_Int
(Direct_Val
(Left
) + Direct_Val
(Right
));
1051 elsif Int
(Left
) = Int
(Uint_0
) then
1055 elsif Direct
(Right
) and then Int
(Right
) = Int
(Uint_0
) then
1059 -- Otherwise full circuit is needed
1062 L_Length
: constant Int
:= N_Digits
(Left
);
1063 R_Length
: constant Int
:= N_Digits
(Right
);
1064 L_Vec
: UI_Vector
(1 .. L_Length
);
1065 R_Vec
: UI_Vector
(1 .. R_Length
);
1070 X_Bigger
: Boolean := False;
1071 Y_Bigger
: Boolean := False;
1072 Result_Neg
: Boolean := False;
1075 Init_Operand
(Left
, L_Vec
);
1076 Init_Operand
(Right
, R_Vec
);
1078 -- At least one of the two operands is in multi-digit form.
1079 -- Calculate the number of digits sufficient to hold result.
1081 if L_Length
> R_Length
then
1082 Sum_Length
:= L_Length
+ 1;
1085 Sum_Length
:= R_Length
+ 1;
1087 if R_Length
> L_Length
then
1092 -- Make copies of the absolute values of L_Vec and R_Vec into X and Y
1093 -- both with lengths equal to the maximum possibly needed. This makes
1094 -- looping over the digits much simpler.
1097 X
: UI_Vector
(1 .. Sum_Length
);
1098 Y
: UI_Vector
(1 .. Sum_Length
);
1099 Tmp_UI
: UI_Vector
(1 .. Sum_Length
);
1102 for J
in 1 .. Sum_Length
- L_Length
loop
1106 X
(Sum_Length
- L_Length
+ 1) := abs L_Vec
(1);
1108 for J
in 2 .. L_Length
loop
1109 X
(J
+ (Sum_Length
- L_Length
)) := L_Vec
(J
);
1112 for J
in 1 .. Sum_Length
- R_Length
loop
1116 Y
(Sum_Length
- R_Length
+ 1) := abs R_Vec
(1);
1118 for J
in 2 .. R_Length
loop
1119 Y
(J
+ (Sum_Length
- R_Length
)) := R_Vec
(J
);
1122 if (L_Vec
(1) < Int_0
) = (R_Vec
(1) < Int_0
) then
1124 -- Same sign so just add
1127 for J
in reverse 1 .. Sum_Length
loop
1128 Tmp_Int
:= X
(J
) + Y
(J
) + Carry
;
1130 if Tmp_Int
>= Base
then
1131 Tmp_Int
:= Tmp_Int
- Base
;
1140 return Vector_To_Uint
(X
, L_Vec
(1) < Int_0
);
1143 -- Find which one has bigger magnitude
1145 if not (X_Bigger
or Y_Bigger
) then
1146 for J
in L_Vec
'Range loop
1147 if abs L_Vec
(J
) > abs R_Vec
(J
) then
1150 elsif abs R_Vec
(J
) > abs L_Vec
(J
) then
1157 -- If they have identical magnitude, just return 0, else swap
1158 -- if necessary so that X had the bigger magnitude. Determine
1159 -- if result is negative at this time.
1161 Result_Neg
:= False;
1163 if not (X_Bigger
or Y_Bigger
) then
1167 if R_Vec
(1) < Int_0
then
1176 if L_Vec
(1) < Int_0
then
1181 -- Subtract Y from the bigger X
1185 for J
in reverse 1 .. Sum_Length
loop
1186 Tmp_Int
:= X
(J
) - Y
(J
) + Borrow
;
1188 if Tmp_Int
< Int_0
then
1189 Tmp_Int
:= Tmp_Int
+ Base
;
1198 return Vector_To_Uint
(X
, Result_Neg
);
1205 --------------------------
1206 -- UI_Decimal_Digits_Hi --
1207 --------------------------
1209 function UI_Decimal_Digits_Hi
(U
: Uint
) return Nat
is
1211 -- The maximum value of a "digit" is 32767, which is 5 decimal digits,
1212 -- so an N_Digit number could take up to 5 times this number of digits.
1213 -- This is certainly too high for large numbers but it is not worth
1216 return 5 * N_Digits
(U
);
1217 end UI_Decimal_Digits_Hi
;
1219 --------------------------
1220 -- UI_Decimal_Digits_Lo --
1221 --------------------------
1223 function UI_Decimal_Digits_Lo
(U
: Uint
) return Nat
is
1225 -- The maximum value of a "digit" is 32767, which is more than four
1226 -- decimal digits, but not a full five digits. The easily computed
1227 -- minimum number of decimal digits is thus 1 + 4 * the number of
1228 -- digits. This is certainly too low for large numbers but it is not
1229 -- worth worrying about.
1231 return 1 + 4 * (N_Digits
(U
) - 1);
1232 end UI_Decimal_Digits_Lo
;
1238 function UI_Div
(Left
: Int
; Right
: Uint
) return Uint
is
1240 return UI_Div
(UI_From_Int
(Left
), Right
);
1243 function UI_Div
(Left
: Uint
; Right
: Int
) return Uint
is
1245 return UI_Div
(Left
, UI_From_Int
(Right
));
1248 function UI_Div
(Left
, Right
: Uint
) return Uint
is
1251 pragma Warnings
(Off
, Remainder
);
1255 Quotient
, Remainder
,
1256 Discard_Quotient
=> False,
1257 Discard_Remainder
=> True);
1265 procedure UI_Div_Rem
1266 (Left
, Right
: Uint
;
1267 Quotient
: out Uint
;
1268 Remainder
: out Uint
;
1269 Discard_Quotient
: Boolean;
1270 Discard_Remainder
: Boolean)
1272 pragma Warnings
(Off
, Quotient
);
1273 pragma Warnings
(Off
, Remainder
);
1275 pragma Assert
(Right
/= Uint_0
);
1277 -- Cases where both operands are represented directly
1279 if Direct
(Left
) and then Direct
(Right
) then
1281 DV_Left
: constant Int
:= Direct_Val
(Left
);
1282 DV_Right
: constant Int
:= Direct_Val
(Right
);
1285 if not Discard_Quotient
then
1286 Quotient
:= UI_From_Int
(DV_Left
/ DV_Right
);
1289 if not Discard_Remainder
then
1290 Remainder
:= UI_From_Int
(DV_Left
rem DV_Right
);
1298 L_Length
: constant Int
:= N_Digits
(Left
);
1299 R_Length
: constant Int
:= N_Digits
(Right
);
1300 Q_Length
: constant Int
:= L_Length
- R_Length
+ 1;
1301 L_Vec
: UI_Vector
(1 .. L_Length
);
1302 R_Vec
: UI_Vector
(1 .. R_Length
);
1310 procedure UI_Div_Vector
1313 Quotient
: out UI_Vector
;
1314 Remainder
: out Int
);
1315 pragma Inline
(UI_Div_Vector
);
1316 -- Specialised variant for case where the divisor is a single digit
1318 procedure UI_Div_Vector
1321 Quotient
: out UI_Vector
;
1322 Remainder
: out Int
)
1328 for J
in L_Vec
'Range loop
1329 Tmp_Int
:= Remainder
* Base
+ abs L_Vec
(J
);
1330 Quotient
(Quotient
'First + J
- L_Vec
'First) := Tmp_Int
/ R_Int
;
1331 Remainder
:= Tmp_Int
rem R_Int
;
1334 if L_Vec
(L_Vec
'First) < Int_0
then
1335 Remainder
:= -Remainder
;
1339 -- Start of processing for UI_Div_Rem
1342 -- Result is zero if left operand is shorter than right
1344 if L_Length
< R_Length
then
1345 if not Discard_Quotient
then
1348 if not Discard_Remainder
then
1354 Init_Operand
(Left
, L_Vec
);
1355 Init_Operand
(Right
, R_Vec
);
1357 -- Case of right operand is single digit. Here we can simply divide
1358 -- each digit of the left operand by the divisor, from most to least
1359 -- significant, carrying the remainder to the next digit (just like
1360 -- ordinary long division by hand).
1362 if R_Length
= Int_1
then
1363 Tmp_Divisor
:= abs R_Vec
(1);
1366 Quotient_V
: UI_Vector
(1 .. L_Length
);
1369 UI_Div_Vector
(L_Vec
, Tmp_Divisor
, Quotient_V
, Remainder_I
);
1371 if not Discard_Quotient
then
1374 (Quotient_V
, (L_Vec
(1) < Int_0
xor R_Vec
(1) < Int_0
));
1377 if not Discard_Remainder
then
1378 Remainder
:= UI_From_Int
(Remainder_I
);
1384 -- The possible simple cases have been exhausted. Now turn to the
1385 -- algorithm D from the section of Knuth mentioned at the top of
1388 Algorithm_D
: declare
1389 Dividend
: UI_Vector
(1 .. L_Length
+ 1);
1390 Divisor
: UI_Vector
(1 .. R_Length
);
1391 Quotient_V
: UI_Vector
(1 .. Q_Length
);
1397 -- [ NORMALIZE ] (step D1 in the algorithm). First calculate the
1398 -- scale d, and then multiply Left and Right (u and v in the book)
1399 -- by d to get the dividend and divisor to work with.
1401 D
:= Base
/ (abs R_Vec
(1) + 1);
1404 Dividend
(2) := abs L_Vec
(1);
1406 for J
in 3 .. L_Length
+ Int_1
loop
1407 Dividend
(J
) := L_Vec
(J
- 1);
1410 Divisor
(1) := abs R_Vec
(1);
1412 for J
in Int_2
.. R_Length
loop
1413 Divisor
(J
) := R_Vec
(J
);
1418 -- Multiply Dividend by D
1421 for J
in reverse Dividend
'Range loop
1422 Tmp_Int
:= Dividend
(J
) * D
+ Carry
;
1423 Dividend
(J
) := Tmp_Int
rem Base
;
1424 Carry
:= Tmp_Int
/ Base
;
1427 -- Multiply Divisor by d
1430 for J
in reverse Divisor
'Range loop
1431 Tmp_Int
:= Divisor
(J
) * D
+ Carry
;
1432 Divisor
(J
) := Tmp_Int
rem Base
;
1433 Carry
:= Tmp_Int
/ Base
;
1437 -- Main loop of long division algorithm
1439 Divisor_Dig1
:= Divisor
(1);
1440 Divisor_Dig2
:= Divisor
(2);
1442 for J
in Quotient_V
'Range loop
1444 -- [ CALCULATE Q (hat) ] (step D3 in the algorithm)
1446 Tmp_Int
:= Dividend
(J
) * Base
+ Dividend
(J
+ 1);
1450 if Dividend
(J
) = Divisor_Dig1
then
1451 Q_Guess
:= Base
- 1;
1453 Q_Guess
:= Tmp_Int
/ Divisor_Dig1
;
1458 while Divisor_Dig2
* Q_Guess
>
1459 (Tmp_Int
- Q_Guess
* Divisor_Dig1
) * Base
+
1462 Q_Guess
:= Q_Guess
- 1;
1465 -- [ MULTIPLY & SUBTRACT ] (step D4). Q_Guess * Divisor is
1466 -- subtracted from the remaining dividend.
1469 for K
in reverse Divisor
'Range loop
1470 Tmp_Int
:= Dividend
(J
+ K
) - Q_Guess
* Divisor
(K
) + Carry
;
1471 Tmp_Dig
:= Tmp_Int
rem Base
;
1472 Carry
:= Tmp_Int
/ Base
;
1474 if Tmp_Dig
< Int_0
then
1475 Tmp_Dig
:= Tmp_Dig
+ Base
;
1479 Dividend
(J
+ K
) := Tmp_Dig
;
1482 Dividend
(J
) := Dividend
(J
) + Carry
;
1484 -- [ TEST REMAINDER ] & [ ADD BACK ] (steps D5 and D6)
1486 -- Here there is a slight difference from the book: the last
1487 -- carry is always added in above and below (cancelling each
1488 -- other). In fact the dividend going negative is used as
1491 -- If the Dividend went negative, then Q_Guess was off by
1492 -- one, so it is decremented, and the divisor is added back
1493 -- into the relevant portion of the dividend.
1495 if Dividend
(J
) < Int_0
then
1496 Q_Guess
:= Q_Guess
- 1;
1499 for K
in reverse Divisor
'Range loop
1500 Tmp_Int
:= Dividend
(J
+ K
) + Divisor
(K
) + Carry
;
1502 if Tmp_Int
>= Base
then
1503 Tmp_Int
:= Tmp_Int
- Base
;
1509 Dividend
(J
+ K
) := Tmp_Int
;
1512 Dividend
(J
) := Dividend
(J
) + Carry
;
1515 -- Finally we can get the next quotient digit
1517 Quotient_V
(J
) := Q_Guess
;
1520 -- [ UNNORMALIZE ] (step D8)
1522 if not Discard_Quotient
then
1523 Quotient
:= Vector_To_Uint
1524 (Quotient_V
, (L_Vec
(1) < Int_0
xor R_Vec
(1) < Int_0
));
1527 if not Discard_Remainder
then
1529 Remainder_V
: UI_Vector
(1 .. R_Length
);
1531 pragma Warnings
(Off
, Discard_Int
);
1534 (Dividend
(Dividend
'Last - R_Length
+ 1 .. Dividend
'Last),
1536 Remainder_V
, Discard_Int
);
1537 Remainder
:= Vector_To_Uint
(Remainder_V
, L_Vec
(1) < Int_0
);
1548 function UI_Eq
(Left
: Int
; Right
: Uint
) return Boolean is
1550 return not UI_Ne
(UI_From_Int
(Left
), Right
);
1553 function UI_Eq
(Left
: Uint
; Right
: Int
) return Boolean is
1555 return not UI_Ne
(Left
, UI_From_Int
(Right
));
1558 function UI_Eq
(Left
: Uint
; Right
: Uint
) return Boolean is
1560 return not UI_Ne
(Left
, Right
);
1567 function UI_Expon
(Left
: Int
; Right
: Uint
) return Uint
is
1569 return UI_Expon
(UI_From_Int
(Left
), Right
);
1572 function UI_Expon
(Left
: Uint
; Right
: Int
) return Uint
is
1574 return UI_Expon
(Left
, UI_From_Int
(Right
));
1577 function UI_Expon
(Left
: Int
; Right
: Int
) return Uint
is
1579 return UI_Expon
(UI_From_Int
(Left
), UI_From_Int
(Right
));
1582 function UI_Expon
(Left
: Uint
; Right
: Uint
) return Uint
is
1584 pragma Assert
(Right
>= Uint_0
);
1586 -- Any value raised to power of 0 is 1
1588 if Right
= Uint_0
then
1591 -- 0 to any positive power is 0
1593 elsif Left
= Uint_0
then
1596 -- 1 to any power is 1
1598 elsif Left
= Uint_1
then
1601 -- Any value raised to power of 1 is that value
1603 elsif Right
= Uint_1
then
1606 -- Cases which can be done by table lookup
1608 elsif Right
<= Uint_64
then
1610 -- 2 ** N for N in 2 .. 64
1612 if Left
= Uint_2
then
1614 Right_Int
: constant Int
:= Direct_Val
(Right
);
1617 if Right_Int
> UI_Power_2_Set
then
1618 for J
in UI_Power_2_Set
+ Int_1
.. Right_Int
loop
1619 UI_Power_2
(J
) := UI_Power_2
(J
- Int_1
) * Int_2
;
1620 Uints_Min
:= Uints
.Last
;
1621 Udigits_Min
:= Udigits
.Last
;
1624 UI_Power_2_Set
:= Right_Int
;
1627 return UI_Power_2
(Right_Int
);
1630 -- 10 ** N for N in 2 .. 64
1632 elsif Left
= Uint_10
then
1634 Right_Int
: constant Int
:= Direct_Val
(Right
);
1637 if Right_Int
> UI_Power_10_Set
then
1638 for J
in UI_Power_10_Set
+ Int_1
.. Right_Int
loop
1639 UI_Power_10
(J
) := UI_Power_10
(J
- Int_1
) * Int
(10);
1640 Uints_Min
:= Uints
.Last
;
1641 Udigits_Min
:= Udigits
.Last
;
1644 UI_Power_10_Set
:= Right_Int
;
1647 return UI_Power_10
(Right_Int
);
1652 -- If we fall through, then we have the general case (see Knuth 4.6.3)
1656 Squares
: Uint
:= Left
;
1657 Result
: Uint
:= Uint_1
;
1658 M
: constant Uintp
.Save_Mark
:= Uintp
.Mark
;
1662 if (Least_Sig_Digit
(N
) mod Int_2
) = Int_1
then
1663 Result
:= Result
* Squares
;
1667 exit when N
= Uint_0
;
1668 Squares
:= Squares
* Squares
;
1671 Uintp
.Release_And_Save
(M
, Result
);
1680 function UI_From_CC
(Input
: Char_Code
) return Uint
is
1682 return UI_From_Dint
(Dint
(Input
));
1689 function UI_From_Dint
(Input
: Dint
) return Uint
is
1692 if Dint
(Min_Direct
) <= Input
and then Input
<= Dint
(Max_Direct
) then
1693 return Uint
(Dint
(Uint_Direct_Bias
) + Input
);
1695 -- For values of larger magnitude, compute digits into a vector and call
1700 Max_For_Dint
: constant := 5;
1701 -- Base is defined so that 5 Uint digits is sufficient to hold the
1702 -- largest possible Dint value.
1704 V
: UI_Vector
(1 .. Max_For_Dint
);
1706 Temp_Integer
: Dint
;
1709 for J
in V
'Range loop
1713 Temp_Integer
:= Input
;
1715 for J
in reverse V
'Range loop
1716 V
(J
) := Int
(abs (Temp_Integer
rem Dint
(Base
)));
1717 Temp_Integer
:= Temp_Integer
/ Dint
(Base
);
1720 return Vector_To_Uint
(V
, Input
< Dint
'(0));
1729 function UI_From_Int (Input : Int) return Uint is
1733 if Min_Direct <= Input and then Input <= Max_Direct then
1734 return Uint (Int (Uint_Direct_Bias) + Input);
1737 -- If already in the hash table, return entry
1739 U := UI_Ints.Get (Input);
1741 if U /= No_Uint then
1745 -- For values of larger magnitude, compute digits into a vector and call
1749 Max_For_Int : constant := 3;
1750 -- Base is defined so that 3 Uint digits is sufficient to hold the
1751 -- largest possible Int value.
1753 V : UI_Vector (1 .. Max_For_Int);
1758 for J in V'Range loop
1762 Temp_Integer := Input;
1764 for J in reverse V'Range loop
1765 V (J) := abs (Temp_Integer rem Base);
1766 Temp_Integer := Temp_Integer / Base;
1769 U := Vector_To_Uint (V, Input < Int_0);
1770 UI_Ints.Set (Input, U);
1771 Uints_Min := Uints.Last;
1772 Udigits_Min := Udigits.Last;
1781 -- Lehmer's algorithm for GCD
1783 -- The idea is to avoid using multiple precision arithmetic wherever
1784 -- possible, substituting Int arithmetic instead. See Knuth volume II,
1785 -- Algorithm L (page 329).
1787 -- We use the same notation as Knuth (U_Hat standing for the obvious!)
1789 function UI_GCD (Uin, Vin : Uint) return Uint is
1791 -- Copies of Uin and Vin
1794 -- The most Significant digits of U,V
1796 A, B, C, D, T, Q, Den1, Den2 : Int;
1799 Marks : constant Uintp.Save_Mark := Uintp.Mark;
1800 Iterations : Integer := 0;
1803 pragma Assert (Uin >= Vin);
1804 pragma Assert (Vin >= Uint_0);
1810 Iterations := Iterations + 1;
1817 UI_From_Int (GCD (Direct_Val (V), UI_To_Int (U rem V)));
1821 Most_Sig_2_Digits (U, V, U_Hat, V_Hat);
1828 -- We might overflow and get division by zero here. This just
1829 -- means we cannot take the single precision step
1833 exit when Den1 = Int_0 or else Den2 = Int_0;
1835 -- Compute Q, the trial quotient
1837 Q := (U_Hat + A) / Den1;
1839 exit when Q /= ((U_Hat + B) / Den2);
1841 -- A single precision step Euclid step will give same answer as a
1842 -- multiprecision one.
1852 T := U_Hat - (Q * V_Hat);
1858 -- Take a multiprecision Euclid step
1862 -- No single precision steps take a regular Euclid step
1869 -- Use prior single precision steps to compute this Euclid step
1871 -- For constructs such as:
1872 -- sqrt_2: constant := 1.41421_35623_73095_04880_16887_24209_698;
1873 -- sqrt_eps: constant long_float := long_float( 1.0 / sqrt_2)
1874 -- ** long_float'machine_mantissa;
1876 -- we spend 80% of our time working on this step. Perhaps we need
1877 -- a special case Int / Uint dot product to speed things up. ???
1879 -- Alternatively we could increase the single precision iterations
1880 -- to handle Uint's of some small size ( <5 digits?). Then we
1881 -- would have more iterations on small Uint. On the code above, we
1882 -- only get 5 (on average) single precision iterations per large
1885 Tmp_UI := (UI_From_Int (A) * U) + (UI_From_Int (B) * V);
1886 V := (UI_From_Int (C) * U) + (UI_From_Int (D) * V);
1890 -- If the operands are very different in magnitude, the loop will
1891 -- generate large amounts of short-lived data, which it is worth
1892 -- removing periodically.
1894 if Iterations > 100 then
1895 Release_And_Save (Marks, U, V);
1905 function UI_Ge (Left : Int; Right : Uint) return Boolean is
1907 return not UI_Lt (UI_From_Int (Left), Right);
1910 function UI_Ge (Left : Uint; Right : Int) return Boolean is
1912 return not UI_Lt (Left, UI_From_Int (Right));
1915 function UI_Ge (Left : Uint; Right : Uint) return Boolean is
1917 return not UI_Lt (Left, Right);
1924 function UI_Gt (Left : Int; Right : Uint) return Boolean is
1926 return UI_Lt (Right, UI_From_Int (Left));
1929 function UI_Gt (Left : Uint; Right : Int) return Boolean is
1931 return UI_Lt (UI_From_Int (Right), Left);
1934 function UI_Gt (Left : Uint; Right : Uint) return Boolean is
1936 return UI_Lt (Left => Right, Right => Left);
1943 procedure UI_Image (Input : Uint; Format : UI_Format := Auto) is
1945 Image_Out (Input, True, Format);
1948 -------------------------
1949 -- UI_Is_In_Int_Range --
1950 -------------------------
1952 function UI_Is_In_Int_Range (Input : Uint) return Boolean is
1954 -- Make sure we don't get called before Initialize
1956 pragma Assert (Uint_Int_First /= Uint_0);
1958 if Direct (Input) then
1961 return Input >= Uint_Int_First
1962 and then Input <= Uint_Int_Last;
1964 end UI_Is_In_Int_Range;
1970 function UI_Le (Left : Int; Right : Uint) return Boolean is
1972 return not UI_Lt (Right, UI_From_Int (Left));
1975 function UI_Le (Left : Uint; Right : Int) return Boolean is
1977 return not UI_Lt (UI_From_Int (Right), Left);
1980 function UI_Le (Left : Uint; Right : Uint) return Boolean is
1982 return not UI_Lt (Left => Right, Right => Left);
1989 function UI_Lt (Left : Int; Right : Uint) return Boolean is
1991 return UI_Lt (UI_From_Int (Left), Right);
1994 function UI_Lt (Left : Uint; Right : Int) return Boolean is
1996 return UI_Lt (Left, UI_From_Int (Right));
1999 function UI_Lt (Left : Uint; Right : Uint) return Boolean is
2001 -- Quick processing for identical arguments
2003 if Int (Left) = Int (Right) then
2006 -- Quick processing for both arguments directly represented
2008 elsif Direct (Left) and then Direct (Right) then
2009 return Int (Left) < Int (Right);
2011 -- At least one argument is more than one digit long
2015 L_Length : constant Int := N_Digits (Left);
2016 R_Length : constant Int := N_Digits (Right);
2018 L_Vec : UI_Vector (1 .. L_Length);
2019 R_Vec : UI_Vector (1 .. R_Length);
2022 Init_Operand (Left, L_Vec);
2023 Init_Operand (Right, R_Vec);
2025 if L_Vec (1) < Int_0 then
2027 -- First argument negative, second argument non-negative
2029 if R_Vec (1) >= Int_0 then
2032 -- Both arguments negative
2035 if L_Length /= R_Length then
2036 return L_Length > R_Length;
2038 elsif L_Vec (1) /= R_Vec (1) then
2039 return L_Vec (1) < R_Vec (1);
2042 for J in 2 .. L_Vec'Last loop
2043 if L_Vec (J) /= R_Vec (J) then
2044 return L_Vec (J) > R_Vec (J);
2053 -- First argument non-negative, second argument negative
2055 if R_Vec (1) < Int_0 then
2058 -- Both arguments non-negative
2061 if L_Length /= R_Length then
2062 return L_Length < R_Length;
2064 for J in L_Vec'Range loop
2065 if L_Vec (J) /= R_Vec (J) then
2066 return L_Vec (J) < R_Vec (J);
2082 function UI_Max (Left : Int; Right : Uint) return Uint is
2084 return UI_Max (UI_From_Int (Left), Right);
2087 function UI_Max (Left : Uint; Right : Int) return Uint is
2089 return UI_Max (Left, UI_From_Int (Right));
2092 function UI_Max (Left : Uint; Right : Uint) return Uint is
2094 if Left >= Right then
2105 function UI_Min (Left : Int; Right : Uint) return Uint is
2107 return UI_Min (UI_From_Int (Left), Right);
2110 function UI_Min (Left : Uint; Right : Int) return Uint is
2112 return UI_Min (Left, UI_From_Int (Right));
2115 function UI_Min (Left : Uint; Right : Uint) return Uint is
2117 if Left <= Right then
2128 function UI_Mod (Left : Int; Right : Uint) return Uint is
2130 return UI_Mod (UI_From_Int (Left), Right);
2133 function UI_Mod (Left : Uint; Right : Int) return Uint is
2135 return UI_Mod (Left, UI_From_Int (Right));
2138 function UI_Mod (Left : Uint; Right : Uint) return Uint is
2139 Urem : constant Uint := Left rem Right;
2142 if (Left < Uint_0) = (Right < Uint_0)
2143 or else Urem = Uint_0
2147 return Right + Urem;
2151 -------------------------------
2152 -- UI_Modular_Exponentiation --
2153 -------------------------------
2155 function UI_Modular_Exponentiation
2158 Modulo : Uint) return Uint
2160 M : constant Save_Mark := Mark;
2162 Result : Uint := Uint_1;
2164 Exponent : Uint := E;
2167 while Exponent /= Uint_0 loop
2168 if Least_Sig_Digit (Exponent) rem Int'(2) = Int
'(1) then
2169 Result := (Result * Base) rem Modulo;
2172 Exponent := Exponent / Uint_2;
2173 Base := (Base * Base) rem Modulo;
2176 Release_And_Save (M, Result);
2178 end UI_Modular_Exponentiation;
2180 ------------------------
2181 -- UI_Modular_Inverse --
2182 ------------------------
2184 function UI_Modular_Inverse (N : Uint; Modulo : Uint) return Uint is
2185 M : constant Save_Mark := Mark;
2205 Quotient => Q, Remainder => R,
2206 Discard_Quotient => False,
2207 Discard_Remainder => False);
2217 exit when R = Uint_1;
2220 if S = Int'(-1) then
2224 Release_And_Save
(M
, X
);
2226 end UI_Modular_Inverse
;
2232 function UI_Mul
(Left
: Int
; Right
: Uint
) return Uint
is
2234 return UI_Mul
(UI_From_Int
(Left
), Right
);
2237 function UI_Mul
(Left
: Uint
; Right
: Int
) return Uint
is
2239 return UI_Mul
(Left
, UI_From_Int
(Right
));
2242 function UI_Mul
(Left
: Uint
; Right
: Uint
) return Uint
is
2244 -- Simple case of single length operands
2246 if Direct
(Left
) and then Direct
(Right
) then
2249 (Dint
(Direct_Val
(Left
)) * Dint
(Direct_Val
(Right
)));
2252 -- Otherwise we have the general case (Algorithm M in Knuth)
2255 L_Length
: constant Int
:= N_Digits
(Left
);
2256 R_Length
: constant Int
:= N_Digits
(Right
);
2257 L_Vec
: UI_Vector
(1 .. L_Length
);
2258 R_Vec
: UI_Vector
(1 .. R_Length
);
2262 Init_Operand
(Left
, L_Vec
);
2263 Init_Operand
(Right
, R_Vec
);
2264 Neg
:= (L_Vec
(1) < Int_0
) xor (R_Vec
(1) < Int_0
);
2265 L_Vec
(1) := abs (L_Vec
(1));
2266 R_Vec
(1) := abs (R_Vec
(1));
2268 Algorithm_M
: declare
2269 Product
: UI_Vector
(1 .. L_Length
+ R_Length
);
2274 for J
in Product
'Range loop
2278 for J
in reverse R_Vec
'Range loop
2280 for K
in reverse L_Vec
'Range loop
2282 L_Vec
(K
) * R_Vec
(J
) + Product
(J
+ K
) + Carry
;
2283 Product
(J
+ K
) := Tmp_Sum
rem Base
;
2284 Carry
:= Tmp_Sum
/ Base
;
2287 Product
(J
) := Carry
;
2290 return Vector_To_Uint
(Product
, Neg
);
2299 function UI_Ne
(Left
: Int
; Right
: Uint
) return Boolean is
2301 return UI_Ne
(UI_From_Int
(Left
), Right
);
2304 function UI_Ne
(Left
: Uint
; Right
: Int
) return Boolean is
2306 return UI_Ne
(Left
, UI_From_Int
(Right
));
2309 function UI_Ne
(Left
: Uint
; Right
: Uint
) return Boolean is
2311 -- Quick processing for identical arguments. Note that this takes
2312 -- care of the case of two No_Uint arguments.
2314 if Int
(Left
) = Int
(Right
) then
2318 -- See if left operand directly represented
2320 if Direct
(Left
) then
2322 -- If right operand directly represented then compare
2324 if Direct
(Right
) then
2325 return Int
(Left
) /= Int
(Right
);
2327 -- Left operand directly represented, right not, must be unequal
2333 -- Right operand directly represented, left not, must be unequal
2335 elsif Direct
(Right
) then
2339 -- Otherwise both multi-word, do comparison
2342 Size
: constant Int
:= N_Digits
(Left
);
2347 if Size
/= N_Digits
(Right
) then
2351 Left_Loc
:= Uints
.Table
(Left
).Loc
;
2352 Right_Loc
:= Uints
.Table
(Right
).Loc
;
2354 for J
in Int_0
.. Size
- Int_1
loop
2355 if Udigits
.Table
(Left_Loc
+ J
) /=
2356 Udigits
.Table
(Right_Loc
+ J
)
2370 function UI_Negate
(Right
: Uint
) return Uint
is
2372 -- Case where input is directly represented. Note that since the range
2373 -- of Direct values is non-symmetrical, the result may not be directly
2374 -- represented, this is taken care of in UI_From_Int.
2376 if Direct
(Right
) then
2377 return UI_From_Int
(-Direct_Val
(Right
));
2379 -- Full processing for multi-digit case. Note that we cannot just copy
2380 -- the value to the end of the table negating the first digit, since the
2381 -- range of Direct values is non-symmetrical, so we can have a negative
2382 -- value that is not Direct whose negation can be represented directly.
2386 R_Length
: constant Int
:= N_Digits
(Right
);
2387 R_Vec
: UI_Vector
(1 .. R_Length
);
2391 Init_Operand
(Right
, R_Vec
);
2392 Neg
:= R_Vec
(1) > Int_0
;
2393 R_Vec
(1) := abs R_Vec
(1);
2394 return Vector_To_Uint
(R_Vec
, Neg
);
2403 function UI_Rem
(Left
: Int
; Right
: Uint
) return Uint
is
2405 return UI_Rem
(UI_From_Int
(Left
), Right
);
2408 function UI_Rem
(Left
: Uint
; Right
: Int
) return Uint
is
2410 return UI_Rem
(Left
, UI_From_Int
(Right
));
2413 function UI_Rem
(Left
, Right
: Uint
) return Uint
is
2417 subtype Int1_12
is Integer range 1 .. 12;
2420 pragma Assert
(Right
/= Uint_0
);
2422 if Direct
(Right
) then
2423 if Direct
(Left
) then
2424 return UI_From_Int
(Direct_Val
(Left
) rem Direct_Val
(Right
));
2428 -- Special cases when Right is less than 13 and Left is larger
2429 -- larger than one digit. All of these algorithms depend on the
2430 -- base being 2 ** 15 We work with Abs (Left) and Abs(Right)
2431 -- then multiply result by Sign (Left)
2433 if (Right
<= Uint_12
) and then (Right
>= Uint_Minus_12
) then
2435 if Left
< Uint_0
then
2441 -- All cases are listed, grouped by mathematical method It is
2442 -- not inefficient to do have this case list out of order since
2443 -- GCC sorts the cases we list.
2445 case Int1_12
(abs (Direct_Val
(Right
))) is
2450 -- Powers of two are simple AND's with LS Left Digit GCC
2451 -- will recognise these constants as powers of 2 and replace
2452 -- the rem with simpler operations where possible.
2454 -- Least_Sig_Digit might return Negative numbers
2457 return UI_From_Int
(
2458 Sign
* (Least_Sig_Digit
(Left
) mod 2));
2461 return UI_From_Int
(
2462 Sign
* (Least_Sig_Digit
(Left
) mod 4));
2465 return UI_From_Int
(
2466 Sign
* (Least_Sig_Digit
(Left
) mod 8));
2468 -- Some number theoretical tricks:
2470 -- If B Rem Right = 1 then
2471 -- Left Rem Right = Sum_Of_Digits_Base_B (Left) Rem Right
2473 -- Note: 2^32 mod 3 = 1
2476 return UI_From_Int
(
2477 Sign
* (Sum_Double_Digits
(Left
, 1) rem Int
(3)));
2479 -- Note: 2^15 mod 7 = 1
2482 return UI_From_Int
(
2483 Sign
* (Sum_Digits
(Left
, 1) rem Int
(7)));
2485 -- Note: 2^32 mod 5 = -1
2487 -- Alternating sums might be negative, but rem is always
2488 -- positive hence we must use mod here.
2491 Tmp
:= Sum_Double_Digits
(Left
, -1) mod Int
(5);
2492 return UI_From_Int
(Sign
* Tmp
);
2494 -- Note: 2^15 mod 9 = -1
2496 -- Alternating sums might be negative, but rem is always
2497 -- positive hence we must use mod here.
2500 Tmp
:= Sum_Digits
(Left
, -1) mod Int
(9);
2501 return UI_From_Int
(Sign
* Tmp
);
2503 -- Note: 2^15 mod 11 = -1
2505 -- Alternating sums might be negative, but rem is always
2506 -- positive hence we must use mod here.
2509 Tmp
:= Sum_Digits
(Left
, -1) mod Int
(11);
2510 return UI_From_Int
(Sign
* Tmp
);
2512 -- Now resort to Chinese Remainder theorem to reduce 6, 10,
2513 -- 12 to previous special cases
2515 -- There is no reason we could not add more cases like these
2516 -- if it proves useful.
2518 -- Perhaps we should go up to 16, however we have no "trick"
2521 -- To find u mod m we:
2524 -- GCD(m1, m2) = 1 AND m = (m1 * m2).
2526 -- Next we pick (Basis) M1, M2 small S.T.
2527 -- (M1 mod m1) = (M2 mod m2) = 1 AND
2528 -- (M1 mod m2) = (M2 mod m1) = 0
2530 -- So u mod m = (u1 * M1 + u2 * M2) mod m Where u1 = (u mod
2531 -- m1) AND u2 = (u mod m2); Under typical circumstances the
2532 -- last mod m can be done with a (possible) single
2535 -- m1 = 2; m2 = 3; M1 = 3; M2 = 4;
2538 Tmp
:= 3 * (Least_Sig_Digit
(Left
) rem 2) +
2539 4 * (Sum_Double_Digits
(Left
, 1) rem 3);
2540 return UI_From_Int
(Sign
* (Tmp
rem 6));
2542 -- m1 = 2; m2 = 5; M1 = 5; M2 = 6;
2545 Tmp
:= 5 * (Least_Sig_Digit
(Left
) rem 2) +
2546 6 * (Sum_Double_Digits
(Left
, -1) mod 5);
2547 return UI_From_Int
(Sign
* (Tmp
rem 10));
2549 -- m1 = 3; m2 = 4; M1 = 4; M2 = 9;
2552 Tmp
:= 4 * (Sum_Double_Digits
(Left
, 1) rem 3) +
2553 9 * (Least_Sig_Digit
(Left
) rem 4);
2554 return UI_From_Int
(Sign
* (Tmp
rem 12));
2559 -- Else fall through to general case
2561 -- The special case Length (Left) = Length (Right) = 1 in Div
2562 -- looks slow. It uses UI_To_Int when Int should suffice. ???
2569 pragma Warnings
(Off
, Quotient
);
2572 (Left
, Right
, Quotient
, Remainder
,
2573 Discard_Quotient
=> True,
2574 Discard_Remainder
=> False);
2583 function UI_Sub
(Left
: Int
; Right
: Uint
) return Uint
is
2585 return UI_Add
(Left
, -Right
);
2588 function UI_Sub
(Left
: Uint
; Right
: Int
) return Uint
is
2590 return UI_Add
(Left
, -Right
);
2593 function UI_Sub
(Left
: Uint
; Right
: Uint
) return Uint
is
2595 if Direct
(Left
) and then Direct
(Right
) then
2596 return UI_From_Int
(Direct_Val
(Left
) - Direct_Val
(Right
));
2598 return UI_Add
(Left
, -Right
);
2606 function UI_To_CC
(Input
: Uint
) return Char_Code
is
2608 if Direct
(Input
) then
2609 return Char_Code
(Direct_Val
(Input
));
2611 -- Case of input is more than one digit
2615 In_Length
: constant Int
:= N_Digits
(Input
);
2616 In_Vec
: UI_Vector
(1 .. In_Length
);
2620 Init_Operand
(Input
, In_Vec
);
2622 -- We assume value is positive
2625 for Idx
in In_Vec
'Range loop
2626 Ret_CC
:= Ret_CC
* Char_Code
(Base
) +
2627 Char_Code
(abs In_Vec
(Idx
));
2639 function UI_To_Int
(Input
: Uint
) return Int
is
2641 if Direct
(Input
) then
2642 return Direct_Val
(Input
);
2644 -- Case of input is more than one digit
2648 In_Length
: constant Int
:= N_Digits
(Input
);
2649 In_Vec
: UI_Vector
(1 .. In_Length
);
2653 -- Uints of more than one digit could be outside the range for
2654 -- Ints. Caller should have checked for this if not certain.
2655 -- Fatal error to attempt to convert from value outside Int'Range.
2657 pragma Assert
(UI_Is_In_Int_Range
(Input
));
2659 -- Otherwise, proceed ahead, we are OK
2661 Init_Operand
(Input
, In_Vec
);
2664 -- Calculate -|Input| and then negates if value is positive. This
2665 -- handles our current definition of Int (based on 2s complement).
2666 -- Is it secure enough???
2668 for Idx
in In_Vec
'Range loop
2669 Ret_Int
:= Ret_Int
* Base
- abs In_Vec
(Idx
);
2672 if In_Vec
(1) < Int_0
then
2685 procedure UI_Write
(Input
: Uint
; Format
: UI_Format
:= Auto
) is
2687 Image_Out
(Input
, False, Format
);
2690 ---------------------
2691 -- Vector_To_Uint --
2692 ---------------------
2694 function Vector_To_Uint
2695 (In_Vec
: UI_Vector
;
2703 -- The vector can contain leading zeros. These are not stored in the
2704 -- table, so loop through the vector looking for first non-zero digit
2706 for J
in In_Vec
'Range loop
2707 if In_Vec
(J
) /= Int_0
then
2709 -- The length of the value is the length of the rest of the vector
2711 Size
:= In_Vec
'Last - J
+ 1;
2713 -- One digit value can always be represented directly
2715 if Size
= Int_1
then
2717 return Uint
(Int
(Uint_Direct_Bias
) - In_Vec
(J
));
2719 return Uint
(Int
(Uint_Direct_Bias
) + In_Vec
(J
));
2722 -- Positive two digit values may be in direct representation range
2724 elsif Size
= Int_2
and then not Negative
then
2725 Val
:= In_Vec
(J
) * Base
+ In_Vec
(J
+ 1);
2727 if Val
<= Max_Direct
then
2728 return Uint
(Int
(Uint_Direct_Bias
) + Val
);
2732 -- The value is outside the direct representation range and must
2733 -- therefore be stored in the table. Expand the table to contain
2734 -- the count and digits. The index of the new table entry will be
2735 -- returned as the result.
2737 Uints
.Append
((Length
=> Size
, Loc
=> Udigits
.Last
+ 1));
2745 Udigits
.Append
(Val
);
2747 for K
in 2 .. Size
loop
2748 Udigits
.Append
(In_Vec
(J
+ K
- 1));
2755 -- Dropped through loop only if vector contained all zeros