1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
10 -- Copyright (C) 1992-2001 Free Software Foundation, Inc. --
12 -- GNAT is free software; you can redistribute it and/or modify it under --
13 -- terms of the GNU General Public License as published by the Free Soft- --
14 -- ware Foundation; either version 2, or (at your option) any later ver- --
15 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
16 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
17 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
18 -- for more details. You should have received a copy of the GNU General --
19 -- Public License distributed with GNAT; see file COPYING. If not, write --
20 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
21 -- MA 02111-1307, USA. --
23 -- As a special exception, if other files instantiate generics from this --
24 -- unit, or you link this unit with other files to produce an executable, --
25 -- this unit does not by itself cause the resulting executable to be --
26 -- covered by the GNU General Public License. This exception does not --
27 -- however invalidate any other reasons why the executable file might be --
28 -- covered by the GNU Public License. --
30 -- GNAT was originally developed by the GNAT team at New York University. --
31 -- Extensive contributions were provided by Ada Core Technologies Inc. --
33 ------------------------------------------------------------------------------
35 with Output
; use Output
;
36 with Tree_IO
; use Tree_IO
;
40 ------------------------
41 -- Local Declarations --
42 ------------------------
44 Uint_Int_First
: Uint
:= Uint_0
;
45 -- Uint value containing Int'First value, set by Initialize. The initial
46 -- value of Uint_0 is used for an assertion check that ensures that this
47 -- value is not used before it is initialized. This value is used in the
48 -- UI_Is_In_Int_Range predicate, and it is right that this is a host
49 -- value, since the issue is host representation of integer values.
52 -- Uint value containing Int'Last value set by Initialize.
54 UI_Power_2
: array (Int
range 0 .. 64) of Uint
;
55 -- This table is used to memoize exponentiations by powers of 2. The Nth
56 -- entry, if set, contains the Uint value 2 ** N. Initially UI_Power_2_Set
57 -- is zero and only the 0'th entry is set, the invariant being that all
58 -- entries in the range 0 .. UI_Power_2_Set are initialized.
61 -- Number of entries set in UI_Power_2;
63 UI_Power_10
: array (Int
range 0 .. 64) of Uint
;
64 -- This table is used to memoize exponentiations by powers of 10 in the
65 -- same manner as described above for UI_Power_2.
67 UI_Power_10_Set
: Nat
;
68 -- Number of entries set in UI_Power_10;
72 -- These values are used to make sure that the mark/release mechanism
73 -- does not destroy values saved in the U_Power tables. Whenever an
74 -- entry is made in the U_Power tables, Uints_Min and Udigits_Min are
75 -- updated to protect the entry, and Release never cuts back beyond
76 -- these minimum values.
78 Int_0
: constant Int
:= 0;
79 Int_1
: constant Int
:= 1;
80 Int_2
: constant Int
:= 2;
81 -- These values are used in some cases where the use of numeric literals
82 -- would cause ambiguities (integer vs Uint).
84 -----------------------
85 -- Local Subprograms --
86 -----------------------
88 function Direct
(U
: Uint
) return Boolean;
89 pragma Inline
(Direct
);
90 -- Returns True if U is represented directly
92 function Direct_Val
(U
: Uint
) return Int
;
93 -- U is a Uint for is represented directly. The returned result
94 -- is the value represented.
96 function GCD
(Jin
, Kin
: Int
) return Int
;
97 -- Compute GCD of two integers. Assumes that Jin >= Kin >= 0
103 -- Common processing for UI_Image and UI_Write, To_Buffer is set
104 -- True for UI_Image, and false for UI_Write, and Format is copied
105 -- from the Format parameter to UI_Image or UI_Write.
107 procedure Init_Operand
(UI
: Uint
; Vec
: out UI_Vector
);
108 pragma Inline
(Init_Operand
);
109 -- This procedure puts the value of UI into the vector in canonical
110 -- multiple precision format. The parameter should be of the correct
111 -- size as determined by a previous call to N_Digits (UI). The first
112 -- digit of Vec contains the sign, all other digits are always non-
113 -- negative. Note that the input may be directly represented, and in
114 -- this case Vec will contain the corresponding one or two digit value.
116 function Least_Sig_Digit
(Arg
: Uint
) return Int
;
117 pragma Inline
(Least_Sig_Digit
);
118 -- Returns the Least Significant Digit of Arg quickly. When the given
119 -- Uint is less than 2**15, the value returned is the input value, in
120 -- this case the result may be negative. It is expected that any use
121 -- will mask off unnecessary bits. This is used for finding Arg mod B
122 -- where B is a power of two. Hence the actual base is irrelevent as
123 -- long as it is a power of two.
125 procedure Most_Sig_2_Digits
129 Right_Hat
: out Int
);
130 -- Returns leading two significant digits from the given pair of Uint's.
131 -- Mathematically: returns Left / (Base ** K) and Right / (Base ** K)
132 -- where K is as small as possible S.T. Right_Hat < Base * Base.
133 -- It is required that Left > Right for the algorithm to work.
135 function N_Digits
(Input
: Uint
) return Int
;
136 pragma Inline
(N_Digits
);
137 -- Returns number of "digits" in a Uint
139 function Sum_Digits
(Left
: Uint
; Sign
: Int
) return Int
;
140 -- If Sign = 1 return the sum of the "digits" of Abs (Left). If the
141 -- total has more then one digit then return Sum_Digits of total.
143 function Sum_Double_Digits
(Left
: Uint
; Sign
: Int
) return Int
;
144 -- Same as above but work in New_Base = Base * Base
146 function Vector_To_Uint
150 -- Functions that calculate values in UI_Vectors, call this function
151 -- to create and return the Uint value. In_Vec contains the multiple
152 -- precision (Base) representation of a non-negative value. Leading
153 -- zeroes are permitted. Negative is set if the desired result is
154 -- the negative of the given value. The result will be either the
155 -- appropriate directly represented value, or a table entry in the
156 -- proper canonical format is created and returned.
158 -- Note that Init_Operand puts a signed value in the result vector,
159 -- but Vector_To_Uint is always presented with a non-negative value.
160 -- The processing of signs is something that is done by the caller
161 -- before calling Vector_To_Uint.
167 function Direct
(U
: Uint
) return Boolean is
169 return Int
(U
) <= Int
(Uint_Direct_Last
);
176 function Direct_Val
(U
: Uint
) return Int
is
178 pragma Assert
(Direct
(U
));
179 return Int
(U
) - Int
(Uint_Direct_Bias
);
186 function GCD
(Jin
, Kin
: Int
) return Int
is
190 pragma Assert
(Jin
>= Kin
);
191 pragma Assert
(Kin
>= Int_0
);
196 while K
/= Uint_0
loop
214 Marks
: constant Uintp
.Save_Mark
:= Uintp
.Mark
;
218 Digs_Output
: Natural := 0;
219 -- Counts digits output. In hex mode, but not in decimal mode, we
220 -- put an underline after every four hex digits that are output.
222 Exponent
: Natural := 0;
223 -- If the number is too long to fit in the buffer, we switch to an
224 -- approximate output format with an exponent. This variable records
225 -- the exponent value.
227 function Better_In_Hex
return Boolean;
228 -- Determines if it is better to generate digits in base 16 (result
229 -- is true) or base 10 (result is false). The choice is purely a
230 -- matter of convenience and aesthetics, so it does not matter which
231 -- value is returned from a correctness point of view.
233 procedure Image_Char
(C
: Character);
234 -- Internal procedure to output one character
236 procedure Image_Exponent
(N
: Natural);
237 -- Output non-zero exponent. Note that we only use the exponent
238 -- form in the buffer case, so we know that To_Buffer is true.
240 procedure Image_Uint
(U
: Uint
);
241 -- Internal procedure to output characters of non-negative Uint
247 function Better_In_Hex
return Boolean is
248 T16
: constant Uint
:= Uint_2
** Int
'(16);
254 -- Small values up to 2**16 can always be in decimal
260 -- Otherwise, see if we are a power of 2 or one less than a power
261 -- of 2. For the moment these are the only cases printed in hex.
263 if A mod Uint_2 = Uint_1 then
268 if A mod T16 /= Uint_0 then
278 while A > Uint_2 loop
279 if A mod Uint_2 /= Uint_0 then
294 procedure Image_Char (C : Character) is
297 if UI_Image_Length + 6 > UI_Image_Max then
298 Exponent := Exponent + 1;
300 UI_Image_Length := UI_Image_Length + 1;
301 UI_Image_Buffer (UI_Image_Length) := C;
312 procedure Image_Exponent (N : Natural) is
315 Image_Exponent (N / 10);
318 UI_Image_Length := UI_Image_Length + 1;
319 UI_Image_Buffer (UI_Image_Length) :=
320 Character'Val (Character'Pos ('0') + N mod 10);
327 procedure Image_Uint (U : Uint) is
328 H : array (Int range 0 .. 15) of Character := "0123456789ABCDEF";
332 Image_Uint (U / Base);
335 if Digs_Output = 4 and then Base = Uint_16 then
340 Image_Char (H (UI_To_Int (U rem Base)));
342 Digs_Output := Digs_Output + 1;
345 -- Start of processing for Image_Out
348 if Input = No_Uint then
353 UI_Image_Length := 0;
355 if Input < Uint_0 then
363 or else (Format = Auto and then Better_In_Hex)
377 if Exponent /= 0 then
378 UI_Image_Length := UI_Image_Length + 1;
379 UI_Image_Buffer (UI_Image_Length) := 'E
';
380 Image_Exponent (Exponent);
383 Uintp.Release (Marks);
390 procedure Init_Operand (UI : Uint; Vec : out UI_Vector) is
395 Vec (1) := Direct_Val (UI);
397 if Vec (1) >= Base then
398 Vec (2) := Vec (1) rem Base;
399 Vec (1) := Vec (1) / Base;
403 Loc := Uints.Table (UI).Loc;
405 for J in 1 .. Uints.Table (UI).Length loop
406 Vec (J) := Udigits.Table (Loc + J - 1);
415 procedure Initialize is
420 Uint_Int_First := UI_From_Int (Int'First);
421 Uint_Int_Last := UI_From_Int (Int'Last);
423 UI_Power_2 (0) := Uint_1;
426 UI_Power_10 (0) := Uint_1;
427 UI_Power_10_Set := 0;
429 Uints_Min := Uints.Last;
430 Udigits_Min := Udigits.Last;
434 ---------------------
435 -- Least_Sig_Digit --
436 ---------------------
438 function Least_Sig_Digit (Arg : Uint) return Int is
443 V := Direct_Val (Arg);
449 -- Note that this result may be negative
456 (Uints.Table (Arg).Loc + Uints.Table (Arg).Length - 1);
464 function Mark return Save_Mark is
466 return (Save_Uint => Uints.Last, Save_Udigit => Udigits.Last);
469 -----------------------
470 -- Most_Sig_2_Digits --
471 -----------------------
473 procedure Most_Sig_2_Digits
480 pragma Assert (Left >= Right);
482 if Direct (Left) then
483 Left_Hat := Direct_Val (Left);
484 Right_Hat := Direct_Val (Right);
490 Udigits.Table (Uints.Table (Left).Loc);
492 Udigits.Table (Uints.Table (Left).Loc + 1);
495 -- It is not so clear what to return when Arg is negative???
497 Left_Hat := abs (L1) * Base + L2;
502 Length_L : constant Int := Uints.Table (Left).Length;
509 if Direct (Right) then
510 T := Direct_Val (Left);
511 R1 := abs (T / Base);
516 R1 := abs (Udigits.Table (Uints.Table (Right).Loc));
517 R2 := Udigits.Table (Uints.Table (Right).Loc + 1);
518 Length_R := Uints.Table (Right).Length;
521 if Length_L = Length_R then
522 Right_Hat := R1 * Base + R2;
523 elsif Length_L = Length_R + Int_1 then
529 end Most_Sig_2_Digits;
535 -- Note: N_Digits returns 1 for No_Uint
537 function N_Digits (Input : Uint) return Int is
539 if Direct (Input) then
540 if Direct_Val (Input) >= Base then
547 return Uints.Table (Input).Length;
555 function Num_Bits (Input : Uint) return Nat is
560 if UI_Is_In_Int_Range (Input) then
561 Num := UI_To_Int (Input);
565 Bits := Base_Bits * (Uints.Table (Input).Length - 1);
566 Num := abs (Udigits.Table (Uints.Table (Input).Loc));
569 while Types.">" (Num, 0) loop
581 procedure pid (Input : Uint) is
583 UI_Write (Input, Decimal);
591 procedure pih (Input : Uint) is
593 UI_Write (Input, Hex);
601 procedure Release (M : Save_Mark) is
603 Uints.Set_Last (Uint'Max (M.Save_Uint, Uints_Min));
604 Udigits.Set_Last (Int'Max (M.Save_Udigit, Udigits_Min));
607 ----------------------
608 -- Release_And_Save --
609 ----------------------
611 procedure Release_And_Save (M : Save_Mark; UI : in out Uint) is
618 UE_Len : Pos := Uints.Table (UI).Length;
619 UE_Loc : Int := Uints.Table (UI).Loc;
621 UD : Udigits.Table_Type (1 .. UE_Len) :=
622 Udigits.Table (UE_Loc .. UE_Loc + UE_Len - 1);
627 Uints.Increment_Last;
630 Uints.Table (UI) := (UE_Len, Udigits.Last + 1);
632 for J in 1 .. UE_Len loop
633 Udigits.Increment_Last;
634 Udigits.Table (Udigits.Last) := UD (J);
638 end Release_And_Save;
640 procedure Release_And_Save (M : Save_Mark; UI1, UI2 : in out Uint) is
643 Release_And_Save (M, UI2);
645 elsif Direct (UI2) then
646 Release_And_Save (M, UI1);
650 UE1_Len : Pos := Uints.Table (UI1).Length;
651 UE1_Loc : Int := Uints.Table (UI1).Loc;
653 UD1 : Udigits.Table_Type (1 .. UE1_Len) :=
654 Udigits.Table (UE1_Loc .. UE1_Loc + UE1_Len - 1);
656 UE2_Len : Pos := Uints.Table (UI2).Length;
657 UE2_Loc : Int := Uints.Table (UI2).Loc;
659 UD2 : Udigits.Table_Type (1 .. UE2_Len) :=
660 Udigits.Table (UE2_Loc .. UE2_Loc + UE2_Len - 1);
665 Uints.Increment_Last;
668 Uints.Table (UI1) := (UE1_Len, Udigits.Last + 1);
670 for J in 1 .. UE1_Len loop
671 Udigits.Increment_Last;
672 Udigits.Table (Udigits.Last) := UD1 (J);
675 Uints.Increment_Last;
678 Uints.Table (UI2) := (UE2_Len, Udigits.Last + 1);
680 for J in 1 .. UE2_Len loop
681 Udigits.Increment_Last;
682 Udigits.Table (Udigits.Last) := UD2 (J);
686 end Release_And_Save;
692 -- This is done in one pass
694 -- Mathematically: assume base congruent to 1 and compute an equivelent
697 -- If Sign = -1 return the alternating sum of the "digits".
699 -- D1 - D2 + D3 - D4 + D5 . . .
701 -- (where D1 is Least Significant Digit)
703 -- Mathematically: assume base congruent to -1 and compute an equivelent
706 -- This is used in Rem and Base is assumed to be 2 ** 15
708 -- Note: The next two functions are very similar, any style changes made
709 -- to one should be reflected in both. These would be simpler if we
710 -- worked base 2 ** 32.
712 function Sum_Digits (Left : Uint; Sign : Int) return Int is
714 pragma Assert (Sign = Int_1 or Sign = Int (-1));
716 -- First try simple case;
718 if Direct (Left) then
720 Tmp_Int : Int := Direct_Val (Left);
723 if Tmp_Int >= Base then
724 Tmp_Int := (Tmp_Int / Base) +
725 Sign * (Tmp_Int rem Base);
727 -- Now Tmp_Int is in [-(Base - 1) .. 2 * (Base - 1)]
729 if Tmp_Int >= Base then
733 Tmp_Int := (Tmp_Int / Base) + 1;
737 -- Now Tmp_Int is in [-(Base - 1) .. (Base - 1)]
744 -- Otherwise full circuit is needed
748 L_Length : Int := N_Digits (Left);
749 L_Vec : UI_Vector (1 .. L_Length);
755 Init_Operand (Left, L_Vec);
756 L_Vec (1) := abs L_Vec (1);
761 for J in reverse 1 .. L_Length loop
762 Tmp_Int := Tmp_Int + Alt * (L_Vec (J) + Carry);
764 -- Tmp_Int is now between [-2 * Base + 1 .. 2 * Base - 1],
765 -- since old Tmp_Int is between [-(Base - 1) .. Base - 1]
766 -- and L_Vec is in [0 .. Base - 1] and Carry in [-1 .. 1]
768 if Tmp_Int >= Base then
769 Tmp_Int := Tmp_Int - Base;
772 elsif Tmp_Int <= -Base then
773 Tmp_Int := Tmp_Int + Base;
780 -- Tmp_Int is now between [-Base + 1 .. Base - 1]
785 Tmp_Int := Tmp_Int + Alt * Carry;
787 -- Tmp_Int is now between [-Base .. Base]
789 if Tmp_Int >= Base then
790 Tmp_Int := Tmp_Int - Base + Alt * Sign * 1;
792 elsif Tmp_Int <= -Base then
793 Tmp_Int := Tmp_Int + Base + Alt * Sign * (-1);
796 -- Now Tmp_Int is in [-(Base - 1) .. (Base - 1)]
803 -----------------------
804 -- Sum_Double_Digits --
805 -----------------------
807 -- Note: This is used in Rem, Base is assumed to be 2 ** 15
809 function Sum_Double_Digits (Left : Uint; Sign : Int) return Int is
811 -- First try simple case;
813 pragma Assert (Sign = Int_1 or Sign = Int (-1));
815 if Direct (Left) then
816 return Direct_Val (Left);
818 -- Otherwise full circuit is needed
822 L_Length : Int := N_Digits (Left);
823 L_Vec : UI_Vector (1 .. L_Length);
831 Init_Operand (Left, L_Vec);
832 L_Vec (1) := abs L_Vec (1);
841 Least_Sig_Int := Least_Sig_Int + Alt * (L_Vec (J) + Carry);
843 -- Least is in [-2 Base + 1 .. 2 * Base - 1]
844 -- Since L_Vec in [0 .. Base - 1] and Carry in [-1 .. 1]
845 -- and old Least in [-Base + 1 .. Base - 1]
847 if Least_Sig_Int >= Base then
848 Least_Sig_Int := Least_Sig_Int - Base;
851 elsif Least_Sig_Int <= -Base then
852 Least_Sig_Int := Least_Sig_Int + Base;
859 -- Least is now in [-Base + 1 .. Base - 1]
861 Most_Sig_Int := Most_Sig_Int + Alt * (L_Vec (J - 1) + Carry);
863 -- Most is in [-2 Base + 1 .. 2 * Base - 1]
864 -- Since L_Vec in [0 .. Base - 1] and Carry in [-1 .. 1]
865 -- and old Most in [-Base + 1 .. Base - 1]
867 if Most_Sig_Int >= Base then
868 Most_Sig_Int := Most_Sig_Int - Base;
871 elsif Most_Sig_Int <= -Base then
872 Most_Sig_Int := Most_Sig_Int + Base;
878 -- Most is now in [-Base + 1 .. Base - 1]
885 Least_Sig_Int := Least_Sig_Int + Alt * (L_Vec (J) + Carry);
887 Least_Sig_Int := Least_Sig_Int + Alt * Carry;
890 if Least_Sig_Int >= Base then
891 Least_Sig_Int := Least_Sig_Int - Base;
892 Most_Sig_Int := Most_Sig_Int + Alt * 1;
894 elsif Least_Sig_Int <= -Base then
895 Least_Sig_Int := Least_Sig_Int + Base;
896 Most_Sig_Int := Most_Sig_Int + Alt * (-1);
899 if Most_Sig_Int >= Base then
900 Most_Sig_Int := Most_Sig_Int - Base;
903 Least_Sig_Int + Alt * 1; -- cannot overflow again
905 elsif Most_Sig_Int <= -Base then
906 Most_Sig_Int := Most_Sig_Int + Base;
909 Least_Sig_Int + Alt * (-1); -- cannot overflow again.
912 return Most_Sig_Int * Base + Least_Sig_Int;
915 end Sum_Double_Digits;
921 procedure Tree_Read is
926 Tree_Read_Int (Int (Uint_Int_First));
927 Tree_Read_Int (Int (Uint_Int_Last));
928 Tree_Read_Int (UI_Power_2_Set);
929 Tree_Read_Int (UI_Power_10_Set);
930 Tree_Read_Int (Int (Uints_Min));
931 Tree_Read_Int (Udigits_Min);
933 for J in 0 .. UI_Power_2_Set loop
934 Tree_Read_Int (Int (UI_Power_2 (J)));
937 for J in 0 .. UI_Power_10_Set loop
938 Tree_Read_Int (Int (UI_Power_10 (J)));
947 procedure Tree_Write is
952 Tree_Write_Int (Int (Uint_Int_First));
953 Tree_Write_Int (Int (Uint_Int_Last));
954 Tree_Write_Int (UI_Power_2_Set);
955 Tree_Write_Int (UI_Power_10_Set);
956 Tree_Write_Int (Int (Uints_Min));
957 Tree_Write_Int (Udigits_Min);
959 for J in 0 .. UI_Power_2_Set loop
960 Tree_Write_Int (Int (UI_Power_2 (J)));
963 for J in 0 .. UI_Power_10_Set loop
964 Tree_Write_Int (Int (UI_Power_10 (J)));
973 function UI_Abs (Right : Uint) return Uint is
975 if Right < Uint_0 then
986 function UI_Add (Left : Int; Right : Uint) return Uint is
988 return UI_Add (UI_From_Int (Left), Right);
991 function UI_Add (Left : Uint; Right : Int) return Uint is
993 return UI_Add (Left, UI_From_Int (Right));
996 function UI_Add (Left : Uint; Right : Uint) return Uint is
998 -- Simple cases of direct operands and addition of zero
1000 if Direct (Left) then
1001 if Direct (Right) then
1002 return UI_From_Int (Direct_Val (Left) + Direct_Val (Right));
1004 elsif Int (Left) = Int (Uint_0) then
1008 elsif Direct (Right) and then Int (Right) = Int (Uint_0) then
1012 -- Otherwise full circuit is needed
1015 L_Length : Int := N_Digits (Left);
1016 R_Length : Int := N_Digits (Right);
1017 L_Vec : UI_Vector (1 .. L_Length);
1018 R_Vec : UI_Vector (1 .. R_Length);
1023 X_Bigger : Boolean := False;
1024 Y_Bigger : Boolean := False;
1025 Result_Neg : Boolean := False;
1028 Init_Operand (Left, L_Vec);
1029 Init_Operand (Right, R_Vec);
1031 -- At least one of the two operands is in multi-digit form.
1032 -- Calculate the number of digits sufficient to hold result.
1034 if L_Length > R_Length then
1035 Sum_Length := L_Length + 1;
1038 Sum_Length := R_Length + 1;
1039 if R_Length > L_Length then Y_Bigger := True; end if;
1042 -- Make copies of the absolute values of L_Vec and R_Vec into
1043 -- X and Y both with lengths equal to the maximum possibly
1044 -- needed. This makes looping over the digits much simpler.
1047 X : UI_Vector (1 .. Sum_Length);
1048 Y : UI_Vector (1 .. Sum_Length);
1049 Tmp_UI : UI_Vector (1 .. Sum_Length);
1052 for J in 1 .. Sum_Length - L_Length loop
1056 X (Sum_Length - L_Length + 1) := abs L_Vec (1);
1058 for J in 2 .. L_Length loop
1059 X (J + (Sum_Length - L_Length)) := L_Vec (J);
1062 for J in 1 .. Sum_Length - R_Length loop
1066 Y (Sum_Length - R_Length + 1) := abs R_Vec (1);
1068 for J in 2 .. R_Length loop
1069 Y (J + (Sum_Length - R_Length)) := R_Vec (J);
1072 if (L_Vec (1) < Int_0) = (R_Vec (1) < Int_0) then
1074 -- Same sign so just add
1077 for J in reverse 1 .. Sum_Length loop
1078 Tmp_Int := X (J) + Y (J) + Carry;
1080 if Tmp_Int >= Base then
1081 Tmp_Int := Tmp_Int - Base;
1090 return Vector_To_Uint (X, L_Vec (1) < Int_0);
1093 -- Find which one has bigger magnitude
1095 if not (X_Bigger or Y_Bigger) then
1096 for J in L_Vec'Range loop
1097 if abs L_Vec (J) > abs R_Vec (J) then
1100 elsif abs R_Vec (J) > abs L_Vec (J) then
1107 -- If they have identical magnitude, just return 0, else
1108 -- swap if necessary so that X had the bigger magnitude.
1109 -- Determine if result is negative at this time.
1111 Result_Neg := False;
1113 if not (X_Bigger or Y_Bigger) then
1117 if R_Vec (1) < Int_0 then
1126 if L_Vec (1) < Int_0 then
1131 -- Subtract Y from the bigger X
1135 for J in reverse 1 .. Sum_Length loop
1136 Tmp_Int := X (J) - Y (J) + Borrow;
1138 if Tmp_Int < Int_0 then
1139 Tmp_Int := Tmp_Int + Base;
1148 return Vector_To_Uint (X, Result_Neg);
1155 --------------------------
1156 -- UI_Decimal_Digits_Hi --
1157 --------------------------
1159 function UI_Decimal_Digits_Hi (U : Uint) return Nat is
1161 -- The maximum value of a "digit" is 32767, which is 5 decimal
1162 -- digits, so an N_Digit number could take up to 5 times this
1163 -- number of digits. This is certainly too high for large
1164 -- numbers but it is not worth worrying about.
1166 return 5 * N_Digits (U);
1167 end UI_Decimal_Digits_Hi;
1169 --------------------------
1170 -- UI_Decimal_Digits_Lo --
1171 --------------------------
1173 function UI_Decimal_Digits_Lo (U : Uint) return Nat is
1175 -- The maximum value of a "digit" is 32767, which is more than four
1176 -- decimal digits, but not a full five digits. The easily computed
1177 -- minimum number of decimal digits is thus 1 + 4 * the number of
1178 -- digits. This is certainly too low for large numbers but it is
1179 -- not worth worrying about.
1181 return 1 + 4 * (N_Digits (U) - 1);
1182 end UI_Decimal_Digits_Lo;
1188 function UI_Div (Left : Int; Right : Uint) return Uint is
1190 return UI_Div (UI_From_Int (Left), Right);
1193 function UI_Div (Left : Uint; Right : Int) return Uint is
1195 return UI_Div (Left, UI_From_Int (Right));
1198 function UI_Div (Left, Right : Uint) return Uint is
1200 pragma Assert (Right /= Uint_0);
1202 -- Cases where both operands are represented directly
1204 if Direct (Left) and then Direct (Right) then
1205 return UI_From_Int (Direct_Val (Left) / Direct_Val (Right));
1209 L_Length : constant Int := N_Digits (Left);
1210 R_Length : constant Int := N_Digits (Right);
1211 Q_Length : constant Int := L_Length - R_Length + 1;
1212 L_Vec : UI_Vector (1 .. L_Length);
1213 R_Vec : UI_Vector (1 .. R_Length);
1222 -- Result is zero if left operand is shorter than right
1224 if L_Length < R_Length then
1228 Init_Operand (Left, L_Vec);
1229 Init_Operand (Right, R_Vec);
1231 -- Case of right operand is single digit. Here we can simply divide
1232 -- each digit of the left operand by the divisor, from most to least
1233 -- significant, carrying the remainder to the next digit (just like
1234 -- ordinary long division by hand).
1236 if R_Length = Int_1 then
1238 Tmp_Divisor := abs R_Vec (1);
1241 Quotient : UI_Vector (1 .. L_Length);
1244 for J in L_Vec'Range loop
1245 Tmp_Int := Remainder * Base + abs L_Vec (J);
1246 Quotient (J) := Tmp_Int / Tmp_Divisor;
1247 Remainder := Tmp_Int rem Tmp_Divisor;
1252 (Quotient, (L_Vec (1) < Int_0 xor R_Vec (1) < Int_0));
1256 -- The possible simple cases have been exhausted. Now turn to the
1257 -- algorithm D from the section of Knuth mentioned at the top of
1260 Algorithm_D : declare
1261 Dividend : UI_Vector (1 .. L_Length + 1);
1262 Divisor : UI_Vector (1 .. R_Length);
1263 Quotient : UI_Vector (1 .. Q_Length);
1269 -- [ NORMALIZE ] (step D1 in the algorithm). First calculate the
1270 -- scale d, and then multiply Left and Right (u and v in the book)
1271 -- by d to get the dividend and divisor to work with.
1273 D := Base / (abs R_Vec (1) + 1);
1276 Dividend (2) := abs L_Vec (1);
1278 for J in 3 .. L_Length + Int_1 loop
1279 Dividend (J) := L_Vec (J - 1);
1282 Divisor (1) := abs R_Vec (1);
1284 for J in Int_2 .. R_Length loop
1285 Divisor (J) := R_Vec (J);
1290 -- Multiply Dividend by D
1293 for J in reverse Dividend'Range loop
1294 Tmp_Int := Dividend (J) * D + Carry;
1295 Dividend (J) := Tmp_Int rem Base;
1296 Carry := Tmp_Int / Base;
1299 -- Multiply Divisor by d.
1302 for J in reverse Divisor'Range loop
1303 Tmp_Int := Divisor (J) * D + Carry;
1304 Divisor (J) := Tmp_Int rem Base;
1305 Carry := Tmp_Int / Base;
1309 -- Main loop of long division algorithm.
1311 Divisor_Dig1 := Divisor (1);
1312 Divisor_Dig2 := Divisor (2);
1314 for J in Quotient'Range loop
1316 -- [ CALCULATE Q (hat) ] (step D3 in the algorithm).
1318 Tmp_Int := Dividend (J) * Base + Dividend (J + 1);
1322 if Dividend (J) = Divisor_Dig1 then
1323 Q_Guess := Base - 1;
1325 Q_Guess := Tmp_Int / Divisor_Dig1;
1330 while Divisor_Dig2 * Q_Guess >
1331 (Tmp_Int - Q_Guess * Divisor_Dig1) * Base +
1334 Q_Guess := Q_Guess - 1;
1337 -- [ MULTIPLY & SUBTRACT] (step D4). Q_Guess * Divisor is
1338 -- subtracted from the remaining dividend.
1341 for K in reverse Divisor'Range loop
1342 Tmp_Int := Dividend (J + K) - Q_Guess * Divisor (K) + Carry;
1343 Tmp_Dig := Tmp_Int rem Base;
1344 Carry := Tmp_Int / Base;
1346 if Tmp_Dig < Int_0 then
1347 Tmp_Dig := Tmp_Dig + Base;
1351 Dividend (J + K) := Tmp_Dig;
1354 Dividend (J) := Dividend (J) + Carry;
1356 -- [ TEST REMAINDER ] & [ ADD BACK ] (steps D5 and D6)
1357 -- Here there is a slight difference from the book: the last
1358 -- carry is always added in above and below (cancelling each
1359 -- other). In fact the dividend going negative is used as
1362 -- If the Dividend went negative, then Q_Guess was off by
1363 -- one, so it is decremented, and the divisor is added back
1364 -- into the relevant portion of the dividend.
1366 if Dividend (J) < Int_0 then
1367 Q_Guess := Q_Guess - 1;
1370 for K in reverse Divisor'Range loop
1371 Tmp_Int := Dividend (J + K) + Divisor (K) + Carry;
1373 if Tmp_Int >= Base then
1374 Tmp_Int := Tmp_Int - Base;
1380 Dividend (J + K) := Tmp_Int;
1383 Dividend (J) := Dividend (J) + Carry;
1386 -- Finally we can get the next quotient digit
1388 Quotient (J) := Q_Guess;
1391 return Vector_To_Uint
1392 (Quotient, (L_Vec (1) < Int_0 xor R_Vec (1) < Int_0));
1402 function UI_Eq (Left : Int; Right : Uint) return Boolean is
1404 return not UI_Ne (UI_From_Int (Left), Right);
1407 function UI_Eq (Left : Uint; Right : Int) return Boolean is
1409 return not UI_Ne (Left, UI_From_Int (Right));
1412 function UI_Eq (Left : Uint; Right : Uint) return Boolean is
1414 return not UI_Ne (Left, Right);
1421 function UI_Expon (Left : Int; Right : Uint) return Uint is
1423 return UI_Expon (UI_From_Int (Left), Right);
1426 function UI_Expon (Left : Uint; Right : Int) return Uint is
1428 return UI_Expon (Left, UI_From_Int (Right));
1431 function UI_Expon (Left : Int; Right : Int) return Uint is
1433 return UI_Expon (UI_From_Int (Left), UI_From_Int (Right));
1436 function UI_Expon (Left : Uint; Right : Uint) return Uint is
1438 pragma Assert (Right >= Uint_0);
1440 -- Any value raised to power of 0 is 1
1442 if Right = Uint_0 then
1445 -- 0 to any positive power is 0.
1447 elsif Left = Uint_0 then
1450 -- 1 to any power is 1
1452 elsif Left = Uint_1 then
1455 -- Any value raised to power of 1 is that value
1457 elsif Right = Uint_1 then
1460 -- Cases which can be done by table lookup
1462 elsif Right <= Uint_64 then
1464 -- 2 ** N for N in 2 .. 64
1466 if Left = Uint_2 then
1468 Right_Int : constant Int := Direct_Val (Right);
1471 if Right_Int > UI_Power_2_Set then
1472 for J in UI_Power_2_Set + Int_1 .. Right_Int loop
1473 UI_Power_2 (J) := UI_Power_2 (J - Int_1) * Int_2;
1474 Uints_Min := Uints.Last;
1475 Udigits_Min := Udigits.Last;
1478 UI_Power_2_Set := Right_Int;
1481 return UI_Power_2 (Right_Int);
1484 -- 10 ** N for N in 2 .. 64
1486 elsif Left = Uint_10 then
1488 Right_Int : constant Int := Direct_Val (Right);
1491 if Right_Int > UI_Power_10_Set then
1492 for J in UI_Power_10_Set + Int_1 .. Right_Int loop
1493 UI_Power_10 (J) := UI_Power_10 (J - Int_1) * Int (10);
1494 Uints_Min := Uints.Last;
1495 Udigits_Min := Udigits.Last;
1498 UI_Power_10_Set := Right_Int;
1501 return UI_Power_10 (Right_Int);
1506 -- If we fall through, then we have the general case (see Knuth 4.6.3)
1510 Squares : Uint := Left;
1511 Result : Uint := Uint_1;
1512 M : constant Uintp.Save_Mark := Uintp.Mark;
1516 if (Least_Sig_Digit (N) mod Int_2) = Int_1 then
1517 Result := Result * Squares;
1521 exit when N = Uint_0;
1522 Squares := Squares * Squares;
1525 Uintp.Release_And_Save (M, Result);
1534 function UI_From_Dint (Input : Dint) return Uint is
1537 if Dint (Min_Direct) <= Input and then Input <= Dint (Max_Direct) then
1538 return Uint (Dint (Uint_Direct_Bias) + Input);
1540 -- For values of larger magnitude, compute digits into a vector and
1541 -- call Vector_To_Uint.
1545 Max_For_Dint : constant := 5;
1546 -- Base is defined so that 5 Uint digits is sufficient
1547 -- to hold the largest possible Dint value.
1549 V : UI_Vector (1 .. Max_For_Dint);
1551 Temp_Integer : Dint;
1554 for J in V'Range loop
1558 Temp_Integer := Input;
1560 for J in reverse V'Range loop
1561 V (J) := Int (abs (Temp_Integer rem Dint (Base)));
1562 Temp_Integer := Temp_Integer / Dint (Base);
1565 return Vector_To_Uint (V, Input < Dint'(0));
1574 function UI_From_Int
(Input
: Int
) return Uint
is
1577 if Min_Direct
<= Input
and then Input
<= Max_Direct
then
1578 return Uint
(Int
(Uint_Direct_Bias
) + Input
);
1580 -- For values of larger magnitude, compute digits into a vector and
1581 -- call Vector_To_Uint.
1585 Max_For_Int
: constant := 3;
1586 -- Base is defined so that 3 Uint digits is sufficient
1587 -- to hold the largest possible Int value.
1589 V
: UI_Vector
(1 .. Max_For_Int
);
1594 for J
in V
'Range loop
1598 Temp_Integer
:= Input
;
1600 for J
in reverse V
'Range loop
1601 V
(J
) := abs (Temp_Integer
rem Base
);
1602 Temp_Integer
:= Temp_Integer
/ Base
;
1605 return Vector_To_Uint
(V
, Input
< Int_0
);
1614 -- Lehmer's algorithm for GCD.
1616 -- The idea is to avoid using multiple precision arithmetic wherever
1617 -- possible, substituting Int arithmetic instead. See Knuth volume II,
1618 -- Algorithm L (page 329).
1620 -- We use the same notation as Knuth (U_Hat standing for the obvious!)
1622 function UI_GCD
(Uin
, Vin
: Uint
) return Uint
is
1624 -- Copies of Uin and Vin
1627 -- The most Significant digits of U,V
1629 A
, B
, C
, D
, T
, Q
, Den1
, Den2
: Int
;
1632 Marks
: constant Uintp
.Save_Mark
:= Uintp
.Mark
;
1633 Iterations
: Integer := 0;
1636 pragma Assert
(Uin
>= Vin
);
1637 pragma Assert
(Vin
>= Uint_0
);
1643 Iterations
:= Iterations
+ 1;
1650 UI_From_Int
(GCD
(Direct_Val
(V
), UI_To_Int
(U
rem V
)));
1654 Most_Sig_2_Digits
(U
, V
, U_Hat
, V_Hat
);
1661 -- We might overflow and get division by zero here. This just
1662 -- means we can not take the single precision step
1666 exit when (Den1
* Den2
) = Int_0
;
1668 -- Compute Q, the trial quotient
1670 Q
:= (U_Hat
+ A
) / Den1
;
1672 exit when Q
/= ((U_Hat
+ B
) / Den2
);
1674 -- A single precision step Euclid step will give same answer as
1675 -- a multiprecision one.
1685 T
:= U_Hat
- (Q
* V_Hat
);
1691 -- Take a multiprecision Euclid step
1695 -- No single precision steps take a regular Euclid step.
1702 -- Use prior single precision steps to compute this Euclid step.
1704 -- Fixed bug 1415-008 spends 80% of its time working on this
1705 -- step. Perhaps we need a special case Int / Uint dot
1706 -- product to speed things up. ???
1708 -- Alternatively we could increase the single precision
1709 -- iterations to handle Uint's of some small size ( <5
1710 -- digits?). Then we would have more iterations on small Uint.
1711 -- Fixed bug 1415-008 only gets 5 (on average) single
1712 -- precision iterations per large iteration. ???
1714 Tmp_UI
:= (UI_From_Int
(A
) * U
) + (UI_From_Int
(B
) * V
);
1715 V
:= (UI_From_Int
(C
) * U
) + (UI_From_Int
(D
) * V
);
1719 -- If the operands are very different in magnitude, the loop
1720 -- will generate large amounts of short-lived data, which it is
1721 -- worth removing periodically.
1723 if Iterations
> 100 then
1724 Release_And_Save
(Marks
, U
, V
);
1734 function UI_Ge
(Left
: Int
; Right
: Uint
) return Boolean is
1736 return not UI_Lt
(UI_From_Int
(Left
), Right
);
1739 function UI_Ge
(Left
: Uint
; Right
: Int
) return Boolean is
1741 return not UI_Lt
(Left
, UI_From_Int
(Right
));
1744 function UI_Ge
(Left
: Uint
; Right
: Uint
) return Boolean is
1746 return not UI_Lt
(Left
, Right
);
1753 function UI_Gt
(Left
: Int
; Right
: Uint
) return Boolean is
1755 return UI_Lt
(Right
, UI_From_Int
(Left
));
1758 function UI_Gt
(Left
: Uint
; Right
: Int
) return Boolean is
1760 return UI_Lt
(UI_From_Int
(Right
), Left
);
1763 function UI_Gt
(Left
: Uint
; Right
: Uint
) return Boolean is
1765 return UI_Lt
(Right
, Left
);
1772 procedure UI_Image
(Input
: Uint
; Format
: UI_Format
:= Auto
) is
1774 Image_Out
(Input
, True, Format
);
1777 -------------------------
1778 -- UI_Is_In_Int_Range --
1779 -------------------------
1781 function UI_Is_In_Int_Range
(Input
: Uint
) return Boolean is
1783 -- Make sure we don't get called before Initialize
1785 pragma Assert
(Uint_Int_First
/= Uint_0
);
1787 if Direct
(Input
) then
1790 return Input
>= Uint_Int_First
1791 and then Input
<= Uint_Int_Last
;
1793 end UI_Is_In_Int_Range
;
1799 function UI_Le
(Left
: Int
; Right
: Uint
) return Boolean is
1801 return not UI_Lt
(Right
, UI_From_Int
(Left
));
1804 function UI_Le
(Left
: Uint
; Right
: Int
) return Boolean is
1806 return not UI_Lt
(UI_From_Int
(Right
), Left
);
1809 function UI_Le
(Left
: Uint
; Right
: Uint
) return Boolean is
1811 return not UI_Lt
(Right
, Left
);
1818 function UI_Lt
(Left
: Int
; Right
: Uint
) return Boolean is
1820 return UI_Lt
(UI_From_Int
(Left
), Right
);
1823 function UI_Lt
(Left
: Uint
; Right
: Int
) return Boolean is
1825 return UI_Lt
(Left
, UI_From_Int
(Right
));
1828 function UI_Lt
(Left
: Uint
; Right
: Uint
) return Boolean is
1830 -- Quick processing for identical arguments
1832 if Int
(Left
) = Int
(Right
) then
1835 -- Quick processing for both arguments directly represented
1837 elsif Direct
(Left
) and then Direct
(Right
) then
1838 return Int
(Left
) < Int
(Right
);
1840 -- At least one argument is more than one digit long
1844 L_Length
: constant Int
:= N_Digits
(Left
);
1845 R_Length
: constant Int
:= N_Digits
(Right
);
1847 L_Vec
: UI_Vector
(1 .. L_Length
);
1848 R_Vec
: UI_Vector
(1 .. R_Length
);
1851 Init_Operand
(Left
, L_Vec
);
1852 Init_Operand
(Right
, R_Vec
);
1854 if L_Vec
(1) < Int_0
then
1856 -- First argument negative, second argument non-negative
1858 if R_Vec
(1) >= Int_0
then
1861 -- Both arguments negative
1864 if L_Length
/= R_Length
then
1865 return L_Length
> R_Length
;
1867 elsif L_Vec
(1) /= R_Vec
(1) then
1868 return L_Vec
(1) < R_Vec
(1);
1871 for J
in 2 .. L_Vec
'Last loop
1872 if L_Vec
(J
) /= R_Vec
(J
) then
1873 return L_Vec
(J
) > R_Vec
(J
);
1882 -- First argument non-negative, second argument negative
1884 if R_Vec
(1) < Int_0
then
1887 -- Both arguments non-negative
1890 if L_Length
/= R_Length
then
1891 return L_Length
< R_Length
;
1893 for J
in L_Vec
'Range loop
1894 if L_Vec
(J
) /= R_Vec
(J
) then
1895 return L_Vec
(J
) < R_Vec
(J
);
1911 function UI_Max
(Left
: Int
; Right
: Uint
) return Uint
is
1913 return UI_Max
(UI_From_Int
(Left
), Right
);
1916 function UI_Max
(Left
: Uint
; Right
: Int
) return Uint
is
1918 return UI_Max
(Left
, UI_From_Int
(Right
));
1921 function UI_Max
(Left
: Uint
; Right
: Uint
) return Uint
is
1923 if Left
>= Right
then
1934 function UI_Min
(Left
: Int
; Right
: Uint
) return Uint
is
1936 return UI_Min
(UI_From_Int
(Left
), Right
);
1939 function UI_Min
(Left
: Uint
; Right
: Int
) return Uint
is
1941 return UI_Min
(Left
, UI_From_Int
(Right
));
1944 function UI_Min
(Left
: Uint
; Right
: Uint
) return Uint
is
1946 if Left
<= Right
then
1957 function UI_Mod
(Left
: Int
; Right
: Uint
) return Uint
is
1959 return UI_Mod
(UI_From_Int
(Left
), Right
);
1962 function UI_Mod
(Left
: Uint
; Right
: Int
) return Uint
is
1964 return UI_Mod
(Left
, UI_From_Int
(Right
));
1967 function UI_Mod
(Left
: Uint
; Right
: Uint
) return Uint
is
1968 Urem
: constant Uint
:= Left
rem Right
;
1971 if (Left
< Uint_0
) = (Right
< Uint_0
)
1972 or else Urem
= Uint_0
1976 return Right
+ Urem
;
1984 function UI_Mul
(Left
: Int
; Right
: Uint
) return Uint
is
1986 return UI_Mul
(UI_From_Int
(Left
), Right
);
1989 function UI_Mul
(Left
: Uint
; Right
: Int
) return Uint
is
1991 return UI_Mul
(Left
, UI_From_Int
(Right
));
1994 function UI_Mul
(Left
: Uint
; Right
: Uint
) return Uint
is
1996 -- Simple case of single length operands
1998 if Direct
(Left
) and then Direct
(Right
) then
2001 (Dint
(Direct_Val
(Left
)) * Dint
(Direct_Val
(Right
)));
2004 -- Otherwise we have the general case (Algorithm M in Knuth)
2007 L_Length
: constant Int
:= N_Digits
(Left
);
2008 R_Length
: constant Int
:= N_Digits
(Right
);
2009 L_Vec
: UI_Vector
(1 .. L_Length
);
2010 R_Vec
: UI_Vector
(1 .. R_Length
);
2014 Init_Operand
(Left
, L_Vec
);
2015 Init_Operand
(Right
, R_Vec
);
2016 Neg
:= (L_Vec
(1) < Int_0
) xor (R_Vec
(1) < Int_0
);
2017 L_Vec
(1) := abs (L_Vec
(1));
2018 R_Vec
(1) := abs (R_Vec
(1));
2020 Algorithm_M
: declare
2021 Product
: UI_Vector
(1 .. L_Length
+ R_Length
);
2026 for J
in Product
'Range loop
2030 for J
in reverse R_Vec
'Range loop
2032 for K
in reverse L_Vec
'Range loop
2034 L_Vec
(K
) * R_Vec
(J
) + Product
(J
+ K
) + Carry
;
2035 Product
(J
+ K
) := Tmp_Sum
rem Base
;
2036 Carry
:= Tmp_Sum
/ Base
;
2039 Product
(J
) := Carry
;
2042 return Vector_To_Uint
(Product
, Neg
);
2051 function UI_Ne
(Left
: Int
; Right
: Uint
) return Boolean is
2053 return UI_Ne
(UI_From_Int
(Left
), Right
);
2056 function UI_Ne
(Left
: Uint
; Right
: Int
) return Boolean is
2058 return UI_Ne
(Left
, UI_From_Int
(Right
));
2061 function UI_Ne
(Left
: Uint
; Right
: Uint
) return Boolean is
2063 -- Quick processing for identical arguments. Note that this takes
2064 -- care of the case of two No_Uint arguments.
2066 if Int
(Left
) = Int
(Right
) then
2070 -- See if left operand directly represented
2072 if Direct
(Left
) then
2074 -- If right operand directly represented then compare
2076 if Direct
(Right
) then
2077 return Int
(Left
) /= Int
(Right
);
2079 -- Left operand directly represented, right not, must be unequal
2085 -- Right operand directly represented, left not, must be unequal
2087 elsif Direct
(Right
) then
2091 -- Otherwise both multi-word, do comparison
2094 Size
: constant Int
:= N_Digits
(Left
);
2099 if Size
/= N_Digits
(Right
) then
2103 Left_Loc
:= Uints
.Table
(Left
).Loc
;
2104 Right_Loc
:= Uints
.Table
(Right
).Loc
;
2106 for J
in Int_0
.. Size
- Int_1
loop
2107 if Udigits
.Table
(Left_Loc
+ J
) /=
2108 Udigits
.Table
(Right_Loc
+ J
)
2122 function UI_Negate
(Right
: Uint
) return Uint
is
2124 -- Case where input is directly represented. Note that since the
2125 -- range of Direct values is non-symmetrical, the result may not
2126 -- be directly represented, this is taken care of in UI_From_Int.
2128 if Direct
(Right
) then
2129 return UI_From_Int
(-Direct_Val
(Right
));
2131 -- Full processing for multi-digit case. Note that we cannot just
2132 -- copy the value to the end of the table negating the first digit,
2133 -- since the range of Direct values is non-symmetrical, so we can
2134 -- have a negative value that is not Direct whose negation can be
2135 -- represented directly.
2139 R_Length
: constant Int
:= N_Digits
(Right
);
2140 R_Vec
: UI_Vector
(1 .. R_Length
);
2144 Init_Operand
(Right
, R_Vec
);
2145 Neg
:= R_Vec
(1) > Int_0
;
2146 R_Vec
(1) := abs R_Vec
(1);
2147 return Vector_To_Uint
(R_Vec
, Neg
);
2156 function UI_Rem
(Left
: Int
; Right
: Uint
) return Uint
is
2158 return UI_Rem
(UI_From_Int
(Left
), Right
);
2161 function UI_Rem
(Left
: Uint
; Right
: Int
) return Uint
is
2163 return UI_Rem
(Left
, UI_From_Int
(Right
));
2166 function UI_Rem
(Left
, Right
: Uint
) return Uint
is
2170 subtype Int1_12
is Integer range 1 .. 12;
2173 pragma Assert
(Right
/= Uint_0
);
2175 if Direct
(Right
) then
2176 if Direct
(Left
) then
2177 return UI_From_Int
(Direct_Val
(Left
) rem Direct_Val
(Right
));
2180 -- Special cases when Right is less than 13 and Left is larger
2181 -- larger than one digit. All of these algorithms depend on the
2182 -- base being 2 ** 15 We work with Abs (Left) and Abs(Right)
2183 -- then multiply result by Sign (Left)
2185 if (Right
<= Uint_12
) and then (Right
>= Uint_Minus_12
) then
2187 if (Left
< Uint_0
) then
2193 -- All cases are listed, grouped by mathematical method
2194 -- It is not inefficient to do have this case list out
2195 -- of order since GCC sorts the cases we list.
2197 case Int1_12
(abs (Direct_Val
(Right
))) is
2202 -- Powers of two are simple AND's with LS Left Digit
2203 -- GCC will recognise these constants as powers of 2
2204 -- and replace the rem with simpler operations where
2207 -- Least_Sig_Digit might return Negative numbers.
2210 return UI_From_Int
(
2211 Sign
* (Least_Sig_Digit
(Left
) mod 2));
2214 return UI_From_Int
(
2215 Sign
* (Least_Sig_Digit
(Left
) mod 4));
2218 return UI_From_Int
(
2219 Sign
* (Least_Sig_Digit
(Left
) mod 8));
2221 -- Some number theoretical tricks:
2223 -- If B Rem Right = 1 then
2224 -- Left Rem Right = Sum_Of_Digits_Base_B (Left) Rem Right
2226 -- Note: 2^32 mod 3 = 1
2229 return UI_From_Int
(
2230 Sign
* (Sum_Double_Digits
(Left
, 1) rem Int
(3)));
2232 -- Note: 2^15 mod 7 = 1
2235 return UI_From_Int
(
2236 Sign
* (Sum_Digits
(Left
, 1) rem Int
(7)));
2238 -- Note: 2^32 mod 5 = -1
2239 -- Alternating sums might be negative, but rem is always
2240 -- positive hence we must use mod here.
2243 Tmp
:= Sum_Double_Digits
(Left
, -1) mod Int
(5);
2244 return UI_From_Int
(Sign
* Tmp
);
2246 -- Note: 2^15 mod 9 = -1
2247 -- Alternating sums might be negative, but rem is always
2248 -- positive hence we must use mod here.
2251 Tmp
:= Sum_Digits
(Left
, -1) mod Int
(9);
2252 return UI_From_Int
(Sign
* Tmp
);
2254 -- Note: 2^15 mod 11 = -1
2255 -- Alternating sums might be negative, but rem is always
2256 -- positive hence we must use mod here.
2259 Tmp
:= Sum_Digits
(Left
, -1) mod Int
(11);
2260 return UI_From_Int
(Sign
* Tmp
);
2262 -- Now resort to Chinese Remainder theorem
2263 -- to reduce 6, 10, 12 to previous special cases
2265 -- There is no reason we could not add more cases
2266 -- like these if it proves useful.
2268 -- Perhaps we should go up to 16, however
2269 -- I have no "trick" for 13.
2271 -- To find u mod m we:
2273 -- GCD(m1, m2) = 1 AND m = (m1 * m2).
2274 -- Next we pick (Basis) M1, M2 small S.T.
2275 -- (M1 mod m1) = (M2 mod m2) = 1 AND
2276 -- (M1 mod m2) = (M2 mod m1) = 0
2278 -- So u mod m = (u1 * M1 + u2 * M2) mod m
2279 -- Where u1 = (u mod m1) AND u2 = (u mod m2);
2280 -- Under typical circumstances the last mod m
2281 -- can be done with a (possible) single subtraction.
2283 -- m1 = 2; m2 = 3; M1 = 3; M2 = 4;
2286 Tmp
:= 3 * (Least_Sig_Digit
(Left
) rem 2) +
2287 4 * (Sum_Double_Digits
(Left
, 1) rem 3);
2288 return UI_From_Int
(Sign
* (Tmp
rem 6));
2290 -- m1 = 2; m2 = 5; M1 = 5; M2 = 6;
2293 Tmp
:= 5 * (Least_Sig_Digit
(Left
) rem 2) +
2294 6 * (Sum_Double_Digits
(Left
, -1) mod 5);
2295 return UI_From_Int
(Sign
* (Tmp
rem 10));
2297 -- m1 = 3; m2 = 4; M1 = 4; M2 = 9;
2300 Tmp
:= 4 * (Sum_Double_Digits
(Left
, 1) rem 3) +
2301 9 * (Least_Sig_Digit
(Left
) rem 4);
2302 return UI_From_Int
(Sign
* (Tmp
rem 12));
2307 -- Else fall through to general case.
2309 -- ???This needs to be improved. We have the Rem when we do the
2310 -- Div. Div throws it away!
2312 -- The special case Length (Left) = Length(right) = 1 in Div
2313 -- looks slow. It uses UI_To_Int when Int should suffice. ???
2317 return Left
- (Left
/ Right
) * Right
;
2324 function UI_Sub
(Left
: Int
; Right
: Uint
) return Uint
is
2326 return UI_Add
(Left
, -Right
);
2329 function UI_Sub
(Left
: Uint
; Right
: Int
) return Uint
is
2331 return UI_Add
(Left
, -Right
);
2334 function UI_Sub
(Left
: Uint
; Right
: Uint
) return Uint
is
2336 if Direct
(Left
) and then Direct
(Right
) then
2337 return UI_From_Int
(Direct_Val
(Left
) - Direct_Val
(Right
));
2339 return UI_Add
(Left
, -Right
);
2347 function UI_To_Int
(Input
: Uint
) return Int
is
2349 if Direct
(Input
) then
2350 return Direct_Val
(Input
);
2352 -- Case of input is more than one digit
2356 In_Length
: constant Int
:= N_Digits
(Input
);
2357 In_Vec
: UI_Vector
(1 .. In_Length
);
2361 -- Uints of more than one digit could be outside the range for
2362 -- Ints. Caller should have checked for this if not certain.
2363 -- Fatal error to attempt to convert from value outside Int'Range.
2365 pragma Assert
(UI_Is_In_Int_Range
(Input
));
2367 -- Otherwise, proceed ahead, we are OK
2369 Init_Operand
(Input
, In_Vec
);
2372 -- Calculate -|Input| and then negates if value is positive.
2373 -- This handles our current definition of Int (based on
2374 -- 2s complement). Is it secure enough?
2376 for Idx
in In_Vec
'Range loop
2377 Ret_Int
:= Ret_Int
* Base
- abs In_Vec
(Idx
);
2380 if In_Vec
(1) < Int_0
then
2393 procedure UI_Write
(Input
: Uint
; Format
: UI_Format
:= Auto
) is
2395 Image_Out
(Input
, False, Format
);
2398 ---------------------
2399 -- Vector_To_Uint --
2400 ---------------------
2402 function Vector_To_Uint
2403 (In_Vec
: UI_Vector
;
2411 -- The vector can contain leading zeros. These are not stored in the
2412 -- table, so loop through the vector looking for first non-zero digit
2414 for J
in In_Vec
'Range loop
2415 if In_Vec
(J
) /= Int_0
then
2417 -- The length of the value is the length of the rest of the vector
2419 Size
:= In_Vec
'Last - J
+ 1;
2421 -- One digit value can always be represented directly
2423 if Size
= Int_1
then
2425 return Uint
(Int
(Uint_Direct_Bias
) - In_Vec
(J
));
2427 return Uint
(Int
(Uint_Direct_Bias
) + In_Vec
(J
));
2430 -- Positive two digit values may be in direct representation range
2432 elsif Size
= Int_2
and then not Negative
then
2433 Val
:= In_Vec
(J
) * Base
+ In_Vec
(J
+ 1);
2435 if Val
<= Max_Direct
then
2436 return Uint
(Int
(Uint_Direct_Bias
) + Val
);
2440 -- The value is outside the direct representation range and
2441 -- must therefore be stored in the table. Expand the table
2442 -- to contain the count and tigis. The index of the new table
2443 -- entry will be returned as the result.
2445 Uints
.Increment_Last
;
2446 Uints
.Table
(Uints
.Last
).Length
:= Size
;
2447 Uints
.Table
(Uints
.Last
).Loc
:= Udigits
.Last
+ 1;
2449 Udigits
.Increment_Last
;
2452 Udigits
.Table
(Udigits
.Last
) := -In_Vec
(J
);
2454 Udigits
.Table
(Udigits
.Last
) := +In_Vec
(J
);
2457 for K
in 2 .. Size
loop
2458 Udigits
.Increment_Last
;
2459 Udigits
.Table
(Udigits
.Last
) := In_Vec
(J
+ K
- 1);
2466 -- Dropped through loop only if vector contained all zeros