]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/ada/uintp.adb
a083cebe503f79e75c65b2fd92be1b252962f3f8
[thirdparty/gcc.git] / gcc / ada / uintp.adb
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- U I N T P --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2020, Free Software Foundation, Inc. --
10 -- --
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. --
17 -- --
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. --
21 -- --
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/>. --
26 -- --
27 -- GNAT was originally developed by the GNAT team at New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
29 -- --
30 ------------------------------------------------------------------------------
31
32 with Output; use Output;
33 with Tree_IO; use Tree_IO;
34
35 with GNAT.HTable; use GNAT.HTable;
36
37 package body Uintp is
38
39 ------------------------
40 -- Local Declarations --
41 ------------------------
42
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.
49
50 Uint_Int_Last : Uint;
51 -- Uint value containing Int'Last value set by Initialize
52
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.
58
59 UI_Power_2_Set : Nat;
60 -- Number of entries set in UI_Power_2;
61
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.
65
66 UI_Power_10_Set : Nat;
67 -- Number of entries set in UI_Power_10;
68
69 Uints_Min : Uint;
70 Udigits_Min : Int;
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.
76
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).
82
83 ----------------------------
84 -- UI_From_Int Hash Table --
85 ----------------------------
86
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
89 -- annotation.
90
91 subtype Hnum is Nat range 0 .. 1022;
92
93 function Hash_Num (F : Int) return Hnum;
94 -- Hashing function
95
96 package UI_Ints is new Simple_HTable (
97 Header_Num => Hnum,
98 Element => Uint,
99 No_Element => No_Uint,
100 Key => Int,
101 Hash => Hash_Num,
102 Equal => "=");
103
104 -----------------------
105 -- Local Subprograms --
106 -----------------------
107
108 function Direct (U : Uint) return Boolean;
109 pragma Inline (Direct);
110 -- Returns True if U is represented directly
111
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.
115
116 function GCD (Jin, Kin : Int) return Int;
117 -- Compute GCD of two integers. Assumes that Jin >= Kin >= 0
118
119 procedure Image_Out
120 (Input : Uint;
121 To_Buffer : Boolean;
122 Format : UI_Format);
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.
126
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
135 -- is always 1.
136
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
144 -- two.
145
146 procedure Most_Sig_2_Digits
147 (Left : Uint;
148 Right : Uint;
149 Left_Hat : out Int;
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.
155
156 function N_Digits (Input : Uint) return Int;
157 pragma Inline (N_Digits);
158 -- Returns number of "digits" in a Uint
159
160 procedure UI_Div_Rem
161 (Left, Right : Uint;
162 Quotient : out Uint;
163 Remainder : out Uint;
164 Discard_Quotient : Boolean := False;
165 Discard_Remainder : Boolean := False);
166 -- Compute Euclidean division of Left by Right. If Discard_Quotient is
167 -- False then the quotient is returned in Quotient (otherwise Quotient is
168 -- set to No_Uint). If Discard_Remainder is False, then the remainder is
169 -- returned in Remainder (otherwise Remainder is set to No_Uint).
170 --
171 -- If Discard_Quotient is True, Quotient is set to No_Uint
172 -- If Discard_Remainder is True, Remainder is set to No_Uint
173
174 ------------
175 -- Direct --
176 ------------
177
178 function Direct (U : Uint) return Boolean is
179 begin
180 return Int (U) <= Int (Uint_Direct_Last);
181 end Direct;
182
183 ----------------
184 -- Direct_Val --
185 ----------------
186
187 function Direct_Val (U : Uint) return Int is
188 begin
189 pragma Assert (Direct (U));
190 return Int (U) - Int (Uint_Direct_Bias);
191 end Direct_Val;
192
193 ---------
194 -- GCD --
195 ---------
196
197 function GCD (Jin, Kin : Int) return Int is
198 J, K, Tmp : Int;
199
200 begin
201 pragma Assert (Jin >= Kin);
202 pragma Assert (Kin >= Int_0);
203
204 J := Jin;
205 K := Kin;
206 while K /= Uint_0 loop
207 Tmp := J mod K;
208 J := K;
209 K := Tmp;
210 end loop;
211
212 return J;
213 end GCD;
214
215 --------------
216 -- Hash_Num --
217 --------------
218
219 function Hash_Num (F : Int) return Hnum is
220 begin
221 return Types."mod" (F, Hnum'Range_Length);
222 end Hash_Num;
223
224 ---------------
225 -- Image_Out --
226 ---------------
227
228 procedure Image_Out
229 (Input : Uint;
230 To_Buffer : Boolean;
231 Format : UI_Format)
232 is
233 Marks : constant Uintp.Save_Mark := Uintp.Mark;
234 Base : Uint;
235 Ainput : Uint;
236
237 Digs_Output : Natural := 0;
238 -- Counts digits output. In hex mode, but not in decimal mode, we
239 -- put an underline after every four hex digits that are output.
240
241 Exponent : Natural := 0;
242 -- If the number is too long to fit in the buffer, we switch to an
243 -- approximate output format with an exponent. This variable records
244 -- the exponent value.
245
246 function Better_In_Hex return Boolean;
247 -- Determines if it is better to generate digits in base 16 (result
248 -- is true) or base 10 (result is false). The choice is purely a
249 -- matter of convenience and aesthetics, so it does not matter which
250 -- value is returned from a correctness point of view.
251
252 procedure Image_Char (C : Character);
253 -- Internal procedure to output one character
254
255 procedure Image_Exponent (N : Natural);
256 -- Output non-zero exponent. Note that we only use the exponent form in
257 -- the buffer case, so we know that To_Buffer is true.
258
259 procedure Image_Uint (U : Uint);
260 -- Internal procedure to output characters of non-negative Uint
261
262 -------------------
263 -- Better_In_Hex --
264 -------------------
265
266 function Better_In_Hex return Boolean is
267 T16 : constant Uint := Uint_2**Int'(16);
268 A : Uint;
269
270 begin
271 A := UI_Abs (Input);
272
273 -- Small values up to 2**16 can always be in decimal
274
275 if A < T16 then
276 return False;
277 end if;
278
279 -- Otherwise, see if we are a power of 2 or one less than a power
280 -- of 2. For the moment these are the only cases printed in hex.
281
282 if A mod Uint_2 = Uint_1 then
283 A := A + Uint_1;
284 end if;
285
286 loop
287 if A mod T16 /= Uint_0 then
288 return False;
289
290 else
291 A := A / T16;
292 end if;
293
294 exit when A < T16;
295 end loop;
296
297 while A > Uint_2 loop
298 if A mod Uint_2 /= Uint_0 then
299 return False;
300
301 else
302 A := A / Uint_2;
303 end if;
304 end loop;
305
306 return True;
307 end Better_In_Hex;
308
309 ----------------
310 -- Image_Char --
311 ----------------
312
313 procedure Image_Char (C : Character) is
314 begin
315 if To_Buffer then
316 if UI_Image_Length + 6 > UI_Image_Max then
317 Exponent := Exponent + 1;
318 else
319 UI_Image_Length := UI_Image_Length + 1;
320 UI_Image_Buffer (UI_Image_Length) := C;
321 end if;
322 else
323 Write_Char (C);
324 end if;
325 end Image_Char;
326
327 --------------------
328 -- Image_Exponent --
329 --------------------
330
331 procedure Image_Exponent (N : Natural) is
332 begin
333 if N >= 10 then
334 Image_Exponent (N / 10);
335 end if;
336
337 UI_Image_Length := UI_Image_Length + 1;
338 UI_Image_Buffer (UI_Image_Length) :=
339 Character'Val (Character'Pos ('0') + N mod 10);
340 end Image_Exponent;
341
342 ----------------
343 -- Image_Uint --
344 ----------------
345
346 procedure Image_Uint (U : Uint) is
347 H : constant array (Int range 0 .. 15) of Character :=
348 "0123456789ABCDEF";
349
350 Q, R : Uint;
351 begin
352 UI_Div_Rem (U, Base, Q, R);
353
354 if Q > Uint_0 then
355 Image_Uint (Q);
356 end if;
357
358 if Digs_Output = 4 and then Base = Uint_16 then
359 Image_Char ('_');
360 Digs_Output := 0;
361 end if;
362
363 Image_Char (H (UI_To_Int (R)));
364
365 Digs_Output := Digs_Output + 1;
366 end Image_Uint;
367
368 -- Start of processing for Image_Out
369
370 begin
371 if Input = No_Uint then
372 Image_Char ('?');
373 return;
374 end if;
375
376 UI_Image_Length := 0;
377
378 if Input < Uint_0 then
379 Image_Char ('-');
380 Ainput := -Input;
381 else
382 Ainput := Input;
383 end if;
384
385 if Format = Hex
386 or else (Format = Auto and then Better_In_Hex)
387 then
388 Base := Uint_16;
389 Image_Char ('1');
390 Image_Char ('6');
391 Image_Char ('#');
392 Image_Uint (Ainput);
393 Image_Char ('#');
394
395 else
396 Base := Uint_10;
397 Image_Uint (Ainput);
398 end if;
399
400 if Exponent /= 0 then
401 UI_Image_Length := UI_Image_Length + 1;
402 UI_Image_Buffer (UI_Image_Length) := 'E';
403 Image_Exponent (Exponent);
404 end if;
405
406 Uintp.Release (Marks);
407 end Image_Out;
408
409 -------------------
410 -- Init_Operand --
411 -------------------
412
413 procedure Init_Operand (UI : Uint; Vec : out UI_Vector) is
414 Loc : Int;
415
416 pragma Assert (Vec'First = Int'(1));
417
418 begin
419 if Direct (UI) then
420 Vec (1) := Direct_Val (UI);
421
422 if Vec (1) >= Base then
423 Vec (2) := Vec (1) rem Base;
424 Vec (1) := Vec (1) / Base;
425 end if;
426
427 else
428 Loc := Uints.Table (UI).Loc;
429
430 for J in 1 .. Uints.Table (UI).Length loop
431 Vec (J) := Udigits.Table (Loc + J - 1);
432 end loop;
433 end if;
434 end Init_Operand;
435
436 ----------------
437 -- Initialize --
438 ----------------
439
440 procedure Initialize is
441 begin
442 Uints.Init;
443 Udigits.Init;
444
445 Uint_Int_First := UI_From_Int (Int'First);
446 Uint_Int_Last := UI_From_Int (Int'Last);
447
448 UI_Power_2 (0) := Uint_1;
449 UI_Power_2_Set := 0;
450
451 UI_Power_10 (0) := Uint_1;
452 UI_Power_10_Set := 0;
453
454 Uints_Min := Uints.Last;
455 Udigits_Min := Udigits.Last;
456
457 UI_Ints.Reset;
458 end Initialize;
459
460 ---------------------
461 -- Least_Sig_Digit --
462 ---------------------
463
464 function Least_Sig_Digit (Arg : Uint) return Int is
465 V : Int;
466
467 begin
468 if Direct (Arg) then
469 V := Direct_Val (Arg);
470
471 if V >= Base then
472 V := V mod Base;
473 end if;
474
475 -- Note that this result may be negative
476
477 return V;
478
479 else
480 return
481 Udigits.Table
482 (Uints.Table (Arg).Loc + Uints.Table (Arg).Length - 1);
483 end if;
484 end Least_Sig_Digit;
485
486 ----------
487 -- Mark --
488 ----------
489
490 function Mark return Save_Mark is
491 begin
492 return (Save_Uint => Uints.Last, Save_Udigit => Udigits.Last);
493 end Mark;
494
495 -----------------------
496 -- Most_Sig_2_Digits --
497 -----------------------
498
499 procedure Most_Sig_2_Digits
500 (Left : Uint;
501 Right : Uint;
502 Left_Hat : out Int;
503 Right_Hat : out Int)
504 is
505 begin
506 pragma Assert (Left >= Right);
507
508 if Direct (Left) then
509 pragma Assert (Direct (Right));
510 Left_Hat := Direct_Val (Left);
511 Right_Hat := Direct_Val (Right);
512 return;
513
514 else
515 declare
516 L1 : constant Int :=
517 Udigits.Table (Uints.Table (Left).Loc);
518 L2 : constant Int :=
519 Udigits.Table (Uints.Table (Left).Loc + 1);
520
521 begin
522 -- It is not so clear what to return when Arg is negative???
523
524 Left_Hat := abs (L1) * Base + L2;
525 end;
526 end if;
527
528 declare
529 Length_L : constant Int := Uints.Table (Left).Length;
530 Length_R : Int;
531 R1 : Int;
532 R2 : Int;
533 T : Int;
534
535 begin
536 if Direct (Right) then
537 T := Direct_Val (Right);
538 R1 := abs (T / Base);
539 R2 := T rem Base;
540 Length_R := 2;
541
542 else
543 R1 := abs (Udigits.Table (Uints.Table (Right).Loc));
544 R2 := Udigits.Table (Uints.Table (Right).Loc + 1);
545 Length_R := Uints.Table (Right).Length;
546 end if;
547
548 if Length_L = Length_R then
549 Right_Hat := R1 * Base + R2;
550 elsif Length_L = Length_R + Int_1 then
551 Right_Hat := R1;
552 else
553 Right_Hat := 0;
554 end if;
555 end;
556 end Most_Sig_2_Digits;
557
558 ---------------
559 -- N_Digits --
560 ---------------
561
562 -- Note: N_Digits returns 1 for No_Uint
563
564 function N_Digits (Input : Uint) return Int is
565 begin
566 if Direct (Input) then
567 if Direct_Val (Input) >= Base then
568 return 2;
569 else
570 return 1;
571 end if;
572
573 else
574 return Uints.Table (Input).Length;
575 end if;
576 end N_Digits;
577
578 --------------
579 -- Num_Bits --
580 --------------
581
582 function Num_Bits (Input : Uint) return Nat is
583 Bits : Nat;
584 Num : Nat;
585
586 begin
587 -- Largest negative number has to be handled specially, since it is in
588 -- Int_Range, but we cannot take the absolute value.
589
590 if Input = Uint_Int_First then
591 return Int'Size;
592
593 -- For any other number in Int_Range, get absolute value of number
594
595 elsif UI_Is_In_Int_Range (Input) then
596 Num := abs (UI_To_Int (Input));
597 Bits := 0;
598
599 -- If not in Int_Range then initialize bit count for all low order
600 -- words, and set number to high order digit.
601
602 else
603 Bits := Base_Bits * (Uints.Table (Input).Length - 1);
604 Num := abs (Udigits.Table (Uints.Table (Input).Loc));
605 end if;
606
607 -- Increase bit count for remaining value in Num
608
609 while Types.">" (Num, 0) loop
610 Num := Num / 2;
611 Bits := Bits + 1;
612 end loop;
613
614 return Bits;
615 end Num_Bits;
616
617 ---------
618 -- pid --
619 ---------
620
621 procedure pid (Input : Uint) is
622 begin
623 UI_Write (Input, Decimal);
624 Write_Eol;
625 end pid;
626
627 ---------
628 -- pih --
629 ---------
630
631 procedure pih (Input : Uint) is
632 begin
633 UI_Write (Input, Hex);
634 Write_Eol;
635 end pih;
636
637 -------------
638 -- Release --
639 -------------
640
641 procedure Release (M : Save_Mark) is
642 begin
643 Uints.Set_Last (Uint'Max (M.Save_Uint, Uints_Min));
644 Udigits.Set_Last (Int'Max (M.Save_Udigit, Udigits_Min));
645 end Release;
646
647 ----------------------
648 -- Release_And_Save --
649 ----------------------
650
651 procedure Release_And_Save (M : Save_Mark; UI : in out Uint) is
652 begin
653 if Direct (UI) then
654 Release (M);
655
656 else
657 declare
658 UE_Len : constant Pos := Uints.Table (UI).Length;
659 UE_Loc : constant Int := Uints.Table (UI).Loc;
660
661 UD : constant Udigits.Table_Type (1 .. UE_Len) :=
662 Udigits.Table (UE_Loc .. UE_Loc + UE_Len - 1);
663
664 begin
665 Release (M);
666
667 Uints.Append ((Length => UE_Len, Loc => Udigits.Last + 1));
668 UI := Uints.Last;
669
670 for J in 1 .. UE_Len loop
671 Udigits.Append (UD (J));
672 end loop;
673 end;
674 end if;
675 end Release_And_Save;
676
677 procedure Release_And_Save (M : Save_Mark; UI1, UI2 : in out Uint) is
678 begin
679 if Direct (UI1) then
680 Release_And_Save (M, UI2);
681
682 elsif Direct (UI2) then
683 Release_And_Save (M, UI1);
684
685 else
686 declare
687 UE1_Len : constant Pos := Uints.Table (UI1).Length;
688 UE1_Loc : constant Int := Uints.Table (UI1).Loc;
689
690 UD1 : constant Udigits.Table_Type (1 .. UE1_Len) :=
691 Udigits.Table (UE1_Loc .. UE1_Loc + UE1_Len - 1);
692
693 UE2_Len : constant Pos := Uints.Table (UI2).Length;
694 UE2_Loc : constant Int := Uints.Table (UI2).Loc;
695
696 UD2 : constant Udigits.Table_Type (1 .. UE2_Len) :=
697 Udigits.Table (UE2_Loc .. UE2_Loc + UE2_Len - 1);
698
699 begin
700 Release (M);
701
702 Uints.Append ((Length => UE1_Len, Loc => Udigits.Last + 1));
703 UI1 := Uints.Last;
704
705 for J in 1 .. UE1_Len loop
706 Udigits.Append (UD1 (J));
707 end loop;
708
709 Uints.Append ((Length => UE2_Len, Loc => Udigits.Last + 1));
710 UI2 := Uints.Last;
711
712 for J in 1 .. UE2_Len loop
713 Udigits.Append (UD2 (J));
714 end loop;
715 end;
716 end if;
717 end Release_And_Save;
718
719 ---------------
720 -- Tree_Read --
721 ---------------
722
723 procedure Tree_Read is
724 begin
725 Uints.Tree_Read;
726 Udigits.Tree_Read;
727
728 Tree_Read_Int (Int (Uint_Int_First));
729 Tree_Read_Int (Int (Uint_Int_Last));
730 Tree_Read_Int (UI_Power_2_Set);
731 Tree_Read_Int (UI_Power_10_Set);
732 Tree_Read_Int (Int (Uints_Min));
733 Tree_Read_Int (Udigits_Min);
734
735 for J in 0 .. UI_Power_2_Set loop
736 Tree_Read_Int (Int (UI_Power_2 (J)));
737 end loop;
738
739 for J in 0 .. UI_Power_10_Set loop
740 Tree_Read_Int (Int (UI_Power_10 (J)));
741 end loop;
742
743 end Tree_Read;
744
745 ----------------
746 -- Tree_Write --
747 ----------------
748
749 procedure Tree_Write is
750 begin
751 Uints.Tree_Write;
752 Udigits.Tree_Write;
753
754 Tree_Write_Int (Int (Uint_Int_First));
755 Tree_Write_Int (Int (Uint_Int_Last));
756 Tree_Write_Int (UI_Power_2_Set);
757 Tree_Write_Int (UI_Power_10_Set);
758 Tree_Write_Int (Int (Uints_Min));
759 Tree_Write_Int (Udigits_Min);
760
761 for J in 0 .. UI_Power_2_Set loop
762 Tree_Write_Int (Int (UI_Power_2 (J)));
763 end loop;
764
765 for J in 0 .. UI_Power_10_Set loop
766 Tree_Write_Int (Int (UI_Power_10 (J)));
767 end loop;
768
769 end Tree_Write;
770
771 -------------
772 -- UI_Abs --
773 -------------
774
775 function UI_Abs (Right : Uint) return Uint is
776 begin
777 if Right < Uint_0 then
778 return -Right;
779 else
780 return Right;
781 end if;
782 end UI_Abs;
783
784 -------------
785 -- UI_Add --
786 -------------
787
788 function UI_Add (Left : Int; Right : Uint) return Uint is
789 begin
790 return UI_Add (UI_From_Int (Left), Right);
791 end UI_Add;
792
793 function UI_Add (Left : Uint; Right : Int) return Uint is
794 begin
795 return UI_Add (Left, UI_From_Int (Right));
796 end UI_Add;
797
798 function UI_Add (Left : Uint; Right : Uint) return Uint is
799 begin
800 -- Simple cases of direct operands and addition of zero
801
802 if Direct (Left) then
803 if Direct (Right) then
804 return UI_From_Int (Direct_Val (Left) + Direct_Val (Right));
805
806 elsif Int (Left) = Int (Uint_0) then
807 return Right;
808 end if;
809
810 elsif Direct (Right) and then Int (Right) = Int (Uint_0) then
811 return Left;
812 end if;
813
814 -- Otherwise full circuit is needed
815
816 declare
817 L_Length : constant Int := N_Digits (Left);
818 R_Length : constant Int := N_Digits (Right);
819 L_Vec : UI_Vector (1 .. L_Length);
820 R_Vec : UI_Vector (1 .. R_Length);
821 Sum_Length : Int;
822 Tmp_Int : Int;
823 Carry : Int;
824 Borrow : Int;
825 X_Bigger : Boolean := False;
826 Y_Bigger : Boolean := False;
827 Result_Neg : Boolean := False;
828
829 begin
830 Init_Operand (Left, L_Vec);
831 Init_Operand (Right, R_Vec);
832
833 -- At least one of the two operands is in multi-digit form.
834 -- Calculate the number of digits sufficient to hold result.
835
836 if L_Length > R_Length then
837 Sum_Length := L_Length + 1;
838 X_Bigger := True;
839 else
840 Sum_Length := R_Length + 1;
841
842 if R_Length > L_Length then
843 Y_Bigger := True;
844 end if;
845 end if;
846
847 -- Make copies of the absolute values of L_Vec and R_Vec into X and Y
848 -- both with lengths equal to the maximum possibly needed. This makes
849 -- looping over the digits much simpler.
850
851 declare
852 X : UI_Vector (1 .. Sum_Length);
853 Y : UI_Vector (1 .. Sum_Length);
854 Tmp_UI : UI_Vector (1 .. Sum_Length);
855
856 begin
857 for J in 1 .. Sum_Length - L_Length loop
858 X (J) := 0;
859 end loop;
860
861 X (Sum_Length - L_Length + 1) := abs L_Vec (1);
862
863 for J in 2 .. L_Length loop
864 X (J + (Sum_Length - L_Length)) := L_Vec (J);
865 end loop;
866
867 for J in 1 .. Sum_Length - R_Length loop
868 Y (J) := 0;
869 end loop;
870
871 Y (Sum_Length - R_Length + 1) := abs R_Vec (1);
872
873 for J in 2 .. R_Length loop
874 Y (J + (Sum_Length - R_Length)) := R_Vec (J);
875 end loop;
876
877 if (L_Vec (1) < Int_0) = (R_Vec (1) < Int_0) then
878
879 -- Same sign so just add
880
881 Carry := 0;
882 for J in reverse 1 .. Sum_Length loop
883 Tmp_Int := X (J) + Y (J) + Carry;
884
885 if Tmp_Int >= Base then
886 Tmp_Int := Tmp_Int - Base;
887 Carry := 1;
888 else
889 Carry := 0;
890 end if;
891
892 X (J) := Tmp_Int;
893 end loop;
894
895 return Vector_To_Uint (X, L_Vec (1) < Int_0);
896
897 else
898 -- Find which one has bigger magnitude
899
900 if not (X_Bigger or Y_Bigger) then
901 for J in L_Vec'Range loop
902 if abs L_Vec (J) > abs R_Vec (J) then
903 X_Bigger := True;
904 exit;
905 elsif abs R_Vec (J) > abs L_Vec (J) then
906 Y_Bigger := True;
907 exit;
908 end if;
909 end loop;
910 end if;
911
912 -- If they have identical magnitude, just return 0, else swap
913 -- if necessary so that X had the bigger magnitude. Determine
914 -- if result is negative at this time.
915
916 Result_Neg := False;
917
918 if not (X_Bigger or Y_Bigger) then
919 return Uint_0;
920
921 elsif Y_Bigger then
922 if R_Vec (1) < Int_0 then
923 Result_Neg := True;
924 end if;
925
926 Tmp_UI := X;
927 X := Y;
928 Y := Tmp_UI;
929
930 else
931 if L_Vec (1) < Int_0 then
932 Result_Neg := True;
933 end if;
934 end if;
935
936 -- Subtract Y from the bigger X
937
938 Borrow := 0;
939
940 for J in reverse 1 .. Sum_Length loop
941 Tmp_Int := X (J) - Y (J) + Borrow;
942
943 if Tmp_Int < Int_0 then
944 Tmp_Int := Tmp_Int + Base;
945 Borrow := -1;
946 else
947 Borrow := 0;
948 end if;
949
950 X (J) := Tmp_Int;
951 end loop;
952
953 return Vector_To_Uint (X, Result_Neg);
954
955 end if;
956 end;
957 end;
958 end UI_Add;
959
960 --------------------------
961 -- UI_Decimal_Digits_Hi --
962 --------------------------
963
964 function UI_Decimal_Digits_Hi (U : Uint) return Nat is
965 begin
966 -- The maximum value of a "digit" is 32767, which is 5 decimal digits,
967 -- so an N_Digit number could take up to 5 times this number of digits.
968 -- This is certainly too high for large numbers but it is not worth
969 -- worrying about.
970
971 return 5 * N_Digits (U);
972 end UI_Decimal_Digits_Hi;
973
974 --------------------------
975 -- UI_Decimal_Digits_Lo --
976 --------------------------
977
978 function UI_Decimal_Digits_Lo (U : Uint) return Nat is
979 begin
980 -- The maximum value of a "digit" is 32767, which is more than four
981 -- decimal digits, but not a full five digits. The easily computed
982 -- minimum number of decimal digits is thus 1 + 4 * the number of
983 -- digits. This is certainly too low for large numbers but it is not
984 -- worth worrying about.
985
986 return 1 + 4 * (N_Digits (U) - 1);
987 end UI_Decimal_Digits_Lo;
988
989 ------------
990 -- UI_Div --
991 ------------
992
993 function UI_Div (Left : Int; Right : Uint) return Uint is
994 begin
995 return UI_Div (UI_From_Int (Left), Right);
996 end UI_Div;
997
998 function UI_Div (Left : Uint; Right : Int) return Uint is
999 begin
1000 return UI_Div (Left, UI_From_Int (Right));
1001 end UI_Div;
1002
1003 function UI_Div (Left, Right : Uint) return Uint is
1004 Quotient : Uint;
1005 Remainder : Uint;
1006 pragma Warnings (Off, Remainder);
1007 begin
1008 UI_Div_Rem
1009 (Left, Right,
1010 Quotient, Remainder,
1011 Discard_Remainder => True);
1012 return Quotient;
1013 end UI_Div;
1014
1015 ----------------
1016 -- UI_Div_Rem --
1017 ----------------
1018
1019 procedure UI_Div_Rem
1020 (Left, Right : Uint;
1021 Quotient : out Uint;
1022 Remainder : out Uint;
1023 Discard_Quotient : Boolean := False;
1024 Discard_Remainder : Boolean := False)
1025 is
1026 begin
1027 pragma Assert (Right /= Uint_0);
1028
1029 Quotient := No_Uint;
1030 Remainder := No_Uint;
1031
1032 -- Cases where both operands are represented directly
1033
1034 if Direct (Left) and then Direct (Right) then
1035 declare
1036 DV_Left : constant Int := Direct_Val (Left);
1037 DV_Right : constant Int := Direct_Val (Right);
1038
1039 begin
1040 if not Discard_Quotient then
1041 Quotient := UI_From_Int (DV_Left / DV_Right);
1042 end if;
1043
1044 if not Discard_Remainder then
1045 Remainder := UI_From_Int (DV_Left rem DV_Right);
1046 end if;
1047
1048 return;
1049 end;
1050 end if;
1051
1052 declare
1053 L_Length : constant Int := N_Digits (Left);
1054 R_Length : constant Int := N_Digits (Right);
1055 Q_Length : constant Int := L_Length - R_Length + 1;
1056 L_Vec : UI_Vector (1 .. L_Length);
1057 R_Vec : UI_Vector (1 .. R_Length);
1058 D : Int;
1059 Remainder_I : Int;
1060 Tmp_Divisor : Int;
1061 Carry : Int;
1062 Tmp_Int : Int;
1063 Tmp_Dig : Int;
1064
1065 procedure UI_Div_Vector
1066 (L_Vec : UI_Vector;
1067 R_Int : Int;
1068 Quotient : out UI_Vector;
1069 Remainder : out Int);
1070 pragma Inline (UI_Div_Vector);
1071 -- Specialised variant for case where the divisor is a single digit
1072
1073 procedure UI_Div_Vector
1074 (L_Vec : UI_Vector;
1075 R_Int : Int;
1076 Quotient : out UI_Vector;
1077 Remainder : out Int)
1078 is
1079 Tmp_Int : Int;
1080
1081 begin
1082 Remainder := 0;
1083 for J in L_Vec'Range loop
1084 Tmp_Int := Remainder * Base + abs L_Vec (J);
1085 Quotient (Quotient'First + J - L_Vec'First) := Tmp_Int / R_Int;
1086 Remainder := Tmp_Int rem R_Int;
1087 end loop;
1088
1089 if L_Vec (L_Vec'First) < Int_0 then
1090 Remainder := -Remainder;
1091 end if;
1092 end UI_Div_Vector;
1093
1094 -- Start of processing for UI_Div_Rem
1095
1096 begin
1097 -- Result is zero if left operand is shorter than right
1098
1099 if L_Length < R_Length then
1100 if not Discard_Quotient then
1101 Quotient := Uint_0;
1102 end if;
1103
1104 if not Discard_Remainder then
1105 Remainder := Left;
1106 end if;
1107
1108 return;
1109 end if;
1110
1111 Init_Operand (Left, L_Vec);
1112 Init_Operand (Right, R_Vec);
1113
1114 -- Case of right operand is single digit. Here we can simply divide
1115 -- each digit of the left operand by the divisor, from most to least
1116 -- significant, carrying the remainder to the next digit (just like
1117 -- ordinary long division by hand).
1118
1119 if R_Length = Int_1 then
1120 Tmp_Divisor := abs R_Vec (1);
1121
1122 declare
1123 Quotient_V : UI_Vector (1 .. L_Length);
1124
1125 begin
1126 UI_Div_Vector (L_Vec, Tmp_Divisor, Quotient_V, Remainder_I);
1127
1128 if not Discard_Quotient then
1129 Quotient :=
1130 Vector_To_Uint
1131 (Quotient_V, (L_Vec (1) < Int_0 xor R_Vec (1) < Int_0));
1132 end if;
1133
1134 if not Discard_Remainder then
1135 Remainder := UI_From_Int (Remainder_I);
1136 end if;
1137
1138 return;
1139 end;
1140 end if;
1141
1142 -- The possible simple cases have been exhausted. Now turn to the
1143 -- algorithm D from the section of Knuth mentioned at the top of
1144 -- this package.
1145
1146 Algorithm_D : declare
1147 Dividend : UI_Vector (1 .. L_Length + 1);
1148 Divisor : UI_Vector (1 .. R_Length);
1149 Quotient_V : UI_Vector (1 .. Q_Length);
1150 Divisor_Dig1 : Int;
1151 Divisor_Dig2 : Int;
1152 Q_Guess : Int;
1153 R_Guess : Int;
1154
1155 begin
1156 -- [ NORMALIZE ] (step D1 in the algorithm). First calculate the
1157 -- scale d, and then multiply Left and Right (u and v in the book)
1158 -- by d to get the dividend and divisor to work with.
1159
1160 D := Base / (abs R_Vec (1) + 1);
1161
1162 Dividend (1) := 0;
1163 Dividend (2) := abs L_Vec (1);
1164
1165 for J in 3 .. L_Length + Int_1 loop
1166 Dividend (J) := L_Vec (J - 1);
1167 end loop;
1168
1169 Divisor (1) := abs R_Vec (1);
1170
1171 for J in Int_2 .. R_Length loop
1172 Divisor (J) := R_Vec (J);
1173 end loop;
1174
1175 if D > Int_1 then
1176
1177 -- Multiply Dividend by d
1178
1179 Carry := 0;
1180 for J in reverse Dividend'Range loop
1181 Tmp_Int := Dividend (J) * D + Carry;
1182 Dividend (J) := Tmp_Int rem Base;
1183 Carry := Tmp_Int / Base;
1184 end loop;
1185
1186 -- Multiply Divisor by d
1187
1188 Carry := 0;
1189 for J in reverse Divisor'Range loop
1190 Tmp_Int := Divisor (J) * D + Carry;
1191 Divisor (J) := Tmp_Int rem Base;
1192 Carry := Tmp_Int / Base;
1193 end loop;
1194 end if;
1195
1196 -- Main loop of long division algorithm
1197
1198 Divisor_Dig1 := Divisor (1);
1199 Divisor_Dig2 := Divisor (2);
1200
1201 for J in Quotient_V'Range loop
1202
1203 -- [ CALCULATE Q (hat) ] (step D3 in the algorithm)
1204
1205 -- Note: this version of step D3 is from the original published
1206 -- algorithm, which is known to have a bug causing overflows.
1207 -- See: http://www-cs-faculty.stanford.edu/~uno/err2-2e.ps.gz
1208 -- and http://www-cs-faculty.stanford.edu/~uno/all2-pre.ps.gz.
1209 -- The code below is the fixed version of this step.
1210
1211 Tmp_Int := Dividend (J) * Base + Dividend (J + 1);
1212
1213 -- Initial guess
1214
1215 Q_Guess := Tmp_Int / Divisor_Dig1;
1216 R_Guess := Tmp_Int rem Divisor_Dig1;
1217
1218 -- Refine the guess
1219
1220 while Q_Guess >= Base
1221 or else Divisor_Dig2 * Q_Guess >
1222 R_Guess * Base + Dividend (J + 2)
1223 loop
1224 Q_Guess := Q_Guess - 1;
1225 R_Guess := R_Guess + Divisor_Dig1;
1226 exit when R_Guess >= Base;
1227 end loop;
1228
1229 -- [ MULTIPLY & SUBTRACT ] (step D4). Q_Guess * Divisor is
1230 -- subtracted from the remaining dividend.
1231
1232 Carry := 0;
1233 for K in reverse Divisor'Range loop
1234 Tmp_Int := Dividend (J + K) - Q_Guess * Divisor (K) + Carry;
1235 Tmp_Dig := Tmp_Int rem Base;
1236 Carry := Tmp_Int / Base;
1237
1238 if Tmp_Dig < Int_0 then
1239 Tmp_Dig := Tmp_Dig + Base;
1240 Carry := Carry - 1;
1241 end if;
1242
1243 Dividend (J + K) := Tmp_Dig;
1244 end loop;
1245
1246 Dividend (J) := Dividend (J) + Carry;
1247
1248 -- [ TEST REMAINDER ] & [ ADD BACK ] (steps D5 and D6)
1249
1250 -- Here there is a slight difference from the book: the last
1251 -- carry is always added in above and below (cancelling each
1252 -- other). In fact the dividend going negative is used as
1253 -- the test.
1254
1255 -- If the Dividend went negative, then Q_Guess was off by
1256 -- one, so it is decremented, and the divisor is added back
1257 -- into the relevant portion of the dividend.
1258
1259 if Dividend (J) < Int_0 then
1260 Q_Guess := Q_Guess - 1;
1261
1262 Carry := 0;
1263 for K in reverse Divisor'Range loop
1264 Tmp_Int := Dividend (J + K) + Divisor (K) + Carry;
1265
1266 if Tmp_Int >= Base then
1267 Tmp_Int := Tmp_Int - Base;
1268 Carry := 1;
1269 else
1270 Carry := 0;
1271 end if;
1272
1273 Dividend (J + K) := Tmp_Int;
1274 end loop;
1275
1276 Dividend (J) := Dividend (J) + Carry;
1277 end if;
1278
1279 -- Finally we can get the next quotient digit
1280
1281 Quotient_V (J) := Q_Guess;
1282 end loop;
1283
1284 -- [ UNNORMALIZE ] (step D8)
1285
1286 if not Discard_Quotient then
1287 Quotient := Vector_To_Uint
1288 (Quotient_V, (L_Vec (1) < Int_0 xor R_Vec (1) < Int_0));
1289 end if;
1290
1291 if not Discard_Remainder then
1292 declare
1293 Remainder_V : UI_Vector (1 .. R_Length);
1294 Discard_Int : Int;
1295 pragma Warnings (Off, Discard_Int);
1296 begin
1297 pragma Assert (D /= Int'(0));
1298 UI_Div_Vector
1299 (Dividend (Dividend'Last - R_Length + 1 .. Dividend'Last),
1300 D,
1301 Remainder_V, Discard_Int);
1302 Remainder := Vector_To_Uint (Remainder_V, L_Vec (1) < Int_0);
1303 end;
1304 end if;
1305 end Algorithm_D;
1306 end;
1307 end UI_Div_Rem;
1308
1309 ------------
1310 -- UI_Eq --
1311 ------------
1312
1313 function UI_Eq (Left : Int; Right : Uint) return Boolean is
1314 begin
1315 return not UI_Ne (UI_From_Int (Left), Right);
1316 end UI_Eq;
1317
1318 function UI_Eq (Left : Uint; Right : Int) return Boolean is
1319 begin
1320 return not UI_Ne (Left, UI_From_Int (Right));
1321 end UI_Eq;
1322
1323 function UI_Eq (Left : Uint; Right : Uint) return Boolean is
1324 begin
1325 return not UI_Ne (Left, Right);
1326 end UI_Eq;
1327
1328 --------------
1329 -- UI_Expon --
1330 --------------
1331
1332 function UI_Expon (Left : Int; Right : Uint) return Uint is
1333 begin
1334 return UI_Expon (UI_From_Int (Left), Right);
1335 end UI_Expon;
1336
1337 function UI_Expon (Left : Uint; Right : Int) return Uint is
1338 begin
1339 return UI_Expon (Left, UI_From_Int (Right));
1340 end UI_Expon;
1341
1342 function UI_Expon (Left : Int; Right : Int) return Uint is
1343 begin
1344 return UI_Expon (UI_From_Int (Left), UI_From_Int (Right));
1345 end UI_Expon;
1346
1347 function UI_Expon (Left : Uint; Right : Uint) return Uint is
1348 begin
1349 pragma Assert (Right >= Uint_0);
1350
1351 -- Any value raised to power of 0 is 1
1352
1353 if Right = Uint_0 then
1354 return Uint_1;
1355
1356 -- 0 to any positive power is 0
1357
1358 elsif Left = Uint_0 then
1359 return Uint_0;
1360
1361 -- 1 to any power is 1
1362
1363 elsif Left = Uint_1 then
1364 return Uint_1;
1365
1366 -- Any value raised to power of 1 is that value
1367
1368 elsif Right = Uint_1 then
1369 return Left;
1370
1371 -- Cases which can be done by table lookup
1372
1373 elsif Right <= Uint_64 then
1374
1375 -- 2**N for N in 2 .. 64
1376
1377 if Left = Uint_2 then
1378 declare
1379 Right_Int : constant Int := Direct_Val (Right);
1380
1381 begin
1382 if Right_Int > UI_Power_2_Set then
1383 for J in UI_Power_2_Set + Int_1 .. Right_Int loop
1384 UI_Power_2 (J) := UI_Power_2 (J - Int_1) * Int_2;
1385 Uints_Min := Uints.Last;
1386 Udigits_Min := Udigits.Last;
1387 end loop;
1388
1389 UI_Power_2_Set := Right_Int;
1390 end if;
1391
1392 return UI_Power_2 (Right_Int);
1393 end;
1394
1395 -- 10**N for N in 2 .. 64
1396
1397 elsif Left = Uint_10 then
1398 declare
1399 Right_Int : constant Int := Direct_Val (Right);
1400
1401 begin
1402 if Right_Int > UI_Power_10_Set then
1403 for J in UI_Power_10_Set + Int_1 .. Right_Int loop
1404 UI_Power_10 (J) := UI_Power_10 (J - Int_1) * Int (10);
1405 Uints_Min := Uints.Last;
1406 Udigits_Min := Udigits.Last;
1407 end loop;
1408
1409 UI_Power_10_Set := Right_Int;
1410 end if;
1411
1412 return UI_Power_10 (Right_Int);
1413 end;
1414 end if;
1415 end if;
1416
1417 -- If we fall through, then we have the general case (see Knuth 4.6.3)
1418
1419 declare
1420 N : Uint := Right;
1421 Squares : Uint := Left;
1422 Result : Uint := Uint_1;
1423 M : constant Uintp.Save_Mark := Uintp.Mark;
1424
1425 begin
1426 loop
1427 if (Least_Sig_Digit (N) mod Int_2) = Int_1 then
1428 Result := Result * Squares;
1429 end if;
1430
1431 N := N / Uint_2;
1432 exit when N = Uint_0;
1433 Squares := Squares * Squares;
1434 end loop;
1435
1436 Uintp.Release_And_Save (M, Result);
1437 return Result;
1438 end;
1439 end UI_Expon;
1440
1441 ----------------
1442 -- UI_From_CC --
1443 ----------------
1444
1445 function UI_From_CC (Input : Char_Code) return Uint is
1446 begin
1447 return UI_From_Int (Int (Input));
1448 end UI_From_CC;
1449
1450 -----------------
1451 -- UI_From_Int --
1452 -----------------
1453
1454 function UI_From_Int (Input : Int) return Uint is
1455 U : Uint;
1456
1457 begin
1458 if Min_Direct <= Input and then Input <= Max_Direct then
1459 return Uint (Int (Uint_Direct_Bias) + Input);
1460 end if;
1461
1462 -- If already in the hash table, return entry
1463
1464 U := UI_Ints.Get (Input);
1465
1466 if U /= No_Uint then
1467 return U;
1468 end if;
1469
1470 -- For values of larger magnitude, compute digits into a vector and call
1471 -- Vector_To_Uint.
1472
1473 declare
1474 Max_For_Int : constant := 3;
1475 -- Base is defined so that 3 Uint digits is sufficient to hold the
1476 -- largest possible Int value.
1477
1478 V : UI_Vector (1 .. Max_For_Int);
1479
1480 Temp_Integer : Int := Input;
1481
1482 begin
1483 for J in reverse V'Range loop
1484 V (J) := abs (Temp_Integer rem Base);
1485 Temp_Integer := Temp_Integer / Base;
1486 end loop;
1487
1488 U := Vector_To_Uint (V, Input < Int_0);
1489 UI_Ints.Set (Input, U);
1490 Uints_Min := Uints.Last;
1491 Udigits_Min := Udigits.Last;
1492 return U;
1493 end;
1494 end UI_From_Int;
1495
1496 ----------------------
1497 -- UI_From_Integral --
1498 ----------------------
1499
1500 function UI_From_Integral (Input : In_T) return Uint is
1501 begin
1502 -- If in range of our normal conversion function, use it so we can use
1503 -- direct access and our cache.
1504
1505 if In_T'Size <= Int'Size
1506 or else Input in In_T (Int'First) .. In_T (Int'Last)
1507 then
1508 return UI_From_Int (Int (Input));
1509
1510 else
1511 -- For values of larger magnitude, compute digits into a vector and
1512 -- call Vector_To_Uint.
1513
1514 declare
1515 Max_For_In_T : constant Int := 3 * In_T'Size / Int'Size;
1516 Our_Base : constant In_T := In_T (Base);
1517 Temp_Integer : In_T := Input;
1518 -- Base is defined so that 3 Uint digits is sufficient to hold the
1519 -- largest possible Int value.
1520
1521 U : Uint;
1522 V : UI_Vector (1 .. Max_For_In_T);
1523
1524 begin
1525 for J in reverse V'Range loop
1526 V (J) := Int (abs (Temp_Integer rem Our_Base));
1527 Temp_Integer := Temp_Integer / Our_Base;
1528 end loop;
1529
1530 U := Vector_To_Uint (V, Input < 0);
1531 Uints_Min := Uints.Last;
1532 Udigits_Min := Udigits.Last;
1533
1534 return U;
1535 end;
1536 end if;
1537 end UI_From_Integral;
1538
1539 ------------
1540 -- UI_GCD --
1541 ------------
1542
1543 -- Lehmer's algorithm for GCD
1544
1545 -- The idea is to avoid using multiple precision arithmetic wherever
1546 -- possible, substituting Int arithmetic instead. See Knuth volume II,
1547 -- Algorithm L (page 329).
1548
1549 -- We use the same notation as Knuth (U_Hat standing for the obvious)
1550
1551 function UI_GCD (Uin, Vin : Uint) return Uint is
1552 U, V : Uint;
1553 -- Copies of Uin and Vin
1554
1555 U_Hat, V_Hat : Int;
1556 -- The most Significant digits of U,V
1557
1558 A, B, C, D, T, Q, Den1, Den2 : Int;
1559
1560 Tmp_UI : Uint;
1561 Marks : constant Uintp.Save_Mark := Uintp.Mark;
1562 Iterations : Integer := 0;
1563
1564 begin
1565 pragma Assert (Uin >= Vin);
1566 pragma Assert (Vin >= Uint_0);
1567
1568 U := Uin;
1569 V := Vin;
1570
1571 loop
1572 Iterations := Iterations + 1;
1573
1574 if Direct (V) then
1575 if V = Uint_0 then
1576 return U;
1577 else
1578 return
1579 UI_From_Int (GCD (Direct_Val (V), UI_To_Int (U rem V)));
1580 end if;
1581 end if;
1582
1583 Most_Sig_2_Digits (U, V, U_Hat, V_Hat);
1584 A := 1;
1585 B := 0;
1586 C := 0;
1587 D := 1;
1588
1589 loop
1590 -- We might overflow and get division by zero here. This just
1591 -- means we cannot take the single precision step
1592
1593 Den1 := V_Hat + C;
1594 Den2 := V_Hat + D;
1595 exit when Den1 = Int_0 or else Den2 = Int_0;
1596
1597 -- Compute Q, the trial quotient
1598
1599 Q := (U_Hat + A) / Den1;
1600
1601 exit when Q /= ((U_Hat + B) / Den2);
1602
1603 -- A single precision step Euclid step will give same answer as a
1604 -- multiprecision one.
1605
1606 T := A - (Q * C);
1607 A := C;
1608 C := T;
1609
1610 T := B - (Q * D);
1611 B := D;
1612 D := T;
1613
1614 T := U_Hat - (Q * V_Hat);
1615 U_Hat := V_Hat;
1616 V_Hat := T;
1617
1618 end loop;
1619
1620 -- Take a multiprecision Euclid step
1621
1622 if B = Int_0 then
1623
1624 -- No single precision steps take a regular Euclid step
1625
1626 Tmp_UI := U rem V;
1627 U := V;
1628 V := Tmp_UI;
1629
1630 else
1631 -- Use prior single precision steps to compute this Euclid step
1632
1633 Tmp_UI := (UI_From_Int (A) * U) + (UI_From_Int (B) * V);
1634 V := (UI_From_Int (C) * U) + (UI_From_Int (D) * V);
1635 U := Tmp_UI;
1636 end if;
1637
1638 -- If the operands are very different in magnitude, the loop will
1639 -- generate large amounts of short-lived data, which it is worth
1640 -- removing periodically.
1641
1642 if Iterations > 100 then
1643 Release_And_Save (Marks, U, V);
1644 Iterations := 0;
1645 end if;
1646 end loop;
1647 end UI_GCD;
1648
1649 ------------
1650 -- UI_Ge --
1651 ------------
1652
1653 function UI_Ge (Left : Int; Right : Uint) return Boolean is
1654 begin
1655 return not UI_Lt (UI_From_Int (Left), Right);
1656 end UI_Ge;
1657
1658 function UI_Ge (Left : Uint; Right : Int) return Boolean is
1659 begin
1660 return not UI_Lt (Left, UI_From_Int (Right));
1661 end UI_Ge;
1662
1663 function UI_Ge (Left : Uint; Right : Uint) return Boolean is
1664 begin
1665 return not UI_Lt (Left, Right);
1666 end UI_Ge;
1667
1668 ------------
1669 -- UI_Gt --
1670 ------------
1671
1672 function UI_Gt (Left : Int; Right : Uint) return Boolean is
1673 begin
1674 return UI_Lt (Right, UI_From_Int (Left));
1675 end UI_Gt;
1676
1677 function UI_Gt (Left : Uint; Right : Int) return Boolean is
1678 begin
1679 return UI_Lt (UI_From_Int (Right), Left);
1680 end UI_Gt;
1681
1682 function UI_Gt (Left : Uint; Right : Uint) return Boolean is
1683 begin
1684 return UI_Lt (Left => Right, Right => Left);
1685 end UI_Gt;
1686
1687 ---------------
1688 -- UI_Image --
1689 ---------------
1690
1691 procedure UI_Image (Input : Uint; Format : UI_Format := Auto) is
1692 begin
1693 Image_Out (Input, True, Format);
1694 end UI_Image;
1695
1696 function UI_Image
1697 (Input : Uint;
1698 Format : UI_Format := Auto) return String
1699 is
1700 begin
1701 Image_Out (Input, True, Format);
1702 return UI_Image_Buffer (1 .. UI_Image_Length);
1703 end UI_Image;
1704
1705 -------------------------
1706 -- UI_Is_In_Int_Range --
1707 -------------------------
1708
1709 function UI_Is_In_Int_Range (Input : Uint) return Boolean is
1710 begin
1711 -- Make sure we don't get called before Initialize
1712
1713 pragma Assert (Uint_Int_First /= Uint_0);
1714
1715 if Direct (Input) then
1716 return True;
1717 else
1718 return Input >= Uint_Int_First
1719 and then Input <= Uint_Int_Last;
1720 end if;
1721 end UI_Is_In_Int_Range;
1722
1723 ------------
1724 -- UI_Le --
1725 ------------
1726
1727 function UI_Le (Left : Int; Right : Uint) return Boolean is
1728 begin
1729 return not UI_Lt (Right, UI_From_Int (Left));
1730 end UI_Le;
1731
1732 function UI_Le (Left : Uint; Right : Int) return Boolean is
1733 begin
1734 return not UI_Lt (UI_From_Int (Right), Left);
1735 end UI_Le;
1736
1737 function UI_Le (Left : Uint; Right : Uint) return Boolean is
1738 begin
1739 return not UI_Lt (Left => Right, Right => Left);
1740 end UI_Le;
1741
1742 ------------
1743 -- UI_Lt --
1744 ------------
1745
1746 function UI_Lt (Left : Int; Right : Uint) return Boolean is
1747 begin
1748 return UI_Lt (UI_From_Int (Left), Right);
1749 end UI_Lt;
1750
1751 function UI_Lt (Left : Uint; Right : Int) return Boolean is
1752 begin
1753 return UI_Lt (Left, UI_From_Int (Right));
1754 end UI_Lt;
1755
1756 function UI_Lt (Left : Uint; Right : Uint) return Boolean is
1757 begin
1758 -- Quick processing for identical arguments
1759
1760 if Int (Left) = Int (Right) then
1761 return False;
1762
1763 -- Quick processing for both arguments directly represented
1764
1765 elsif Direct (Left) and then Direct (Right) then
1766 return Int (Left) < Int (Right);
1767
1768 -- At least one argument is more than one digit long
1769
1770 else
1771 declare
1772 L_Length : constant Int := N_Digits (Left);
1773 R_Length : constant Int := N_Digits (Right);
1774
1775 L_Vec : UI_Vector (1 .. L_Length);
1776 R_Vec : UI_Vector (1 .. R_Length);
1777
1778 begin
1779 Init_Operand (Left, L_Vec);
1780 Init_Operand (Right, R_Vec);
1781
1782 if L_Vec (1) < Int_0 then
1783
1784 -- First argument negative, second argument non-negative
1785
1786 if R_Vec (1) >= Int_0 then
1787 return True;
1788
1789 -- Both arguments negative
1790
1791 else
1792 if L_Length /= R_Length then
1793 return L_Length > R_Length;
1794
1795 elsif L_Vec (1) /= R_Vec (1) then
1796 return L_Vec (1) < R_Vec (1);
1797
1798 else
1799 for J in 2 .. L_Vec'Last loop
1800 if L_Vec (J) /= R_Vec (J) then
1801 return L_Vec (J) > R_Vec (J);
1802 end if;
1803 end loop;
1804
1805 return False;
1806 end if;
1807 end if;
1808
1809 else
1810 -- First argument non-negative, second argument negative
1811
1812 if R_Vec (1) < Int_0 then
1813 return False;
1814
1815 -- Both arguments non-negative
1816
1817 else
1818 if L_Length /= R_Length then
1819 return L_Length < R_Length;
1820 else
1821 for J in L_Vec'Range loop
1822 if L_Vec (J) /= R_Vec (J) then
1823 return L_Vec (J) < R_Vec (J);
1824 end if;
1825 end loop;
1826
1827 return False;
1828 end if;
1829 end if;
1830 end if;
1831 end;
1832 end if;
1833 end UI_Lt;
1834
1835 ------------
1836 -- UI_Max --
1837 ------------
1838
1839 function UI_Max (Left : Int; Right : Uint) return Uint is
1840 begin
1841 return UI_Max (UI_From_Int (Left), Right);
1842 end UI_Max;
1843
1844 function UI_Max (Left : Uint; Right : Int) return Uint is
1845 begin
1846 return UI_Max (Left, UI_From_Int (Right));
1847 end UI_Max;
1848
1849 function UI_Max (Left : Uint; Right : Uint) return Uint is
1850 begin
1851 if Left >= Right then
1852 return Left;
1853 else
1854 return Right;
1855 end if;
1856 end UI_Max;
1857
1858 ------------
1859 -- UI_Min --
1860 ------------
1861
1862 function UI_Min (Left : Int; Right : Uint) return Uint is
1863 begin
1864 return UI_Min (UI_From_Int (Left), Right);
1865 end UI_Min;
1866
1867 function UI_Min (Left : Uint; Right : Int) return Uint is
1868 begin
1869 return UI_Min (Left, UI_From_Int (Right));
1870 end UI_Min;
1871
1872 function UI_Min (Left : Uint; Right : Uint) return Uint is
1873 begin
1874 if Left <= Right then
1875 return Left;
1876 else
1877 return Right;
1878 end if;
1879 end UI_Min;
1880
1881 -------------
1882 -- UI_Mod --
1883 -------------
1884
1885 function UI_Mod (Left : Int; Right : Uint) return Uint is
1886 begin
1887 return UI_Mod (UI_From_Int (Left), Right);
1888 end UI_Mod;
1889
1890 function UI_Mod (Left : Uint; Right : Int) return Uint is
1891 begin
1892 return UI_Mod (Left, UI_From_Int (Right));
1893 end UI_Mod;
1894
1895 function UI_Mod (Left : Uint; Right : Uint) return Uint is
1896 Urem : constant Uint := Left rem Right;
1897
1898 begin
1899 if (Left < Uint_0) = (Right < Uint_0)
1900 or else Urem = Uint_0
1901 then
1902 return Urem;
1903 else
1904 return Right + Urem;
1905 end if;
1906 end UI_Mod;
1907
1908 -------------------------------
1909 -- UI_Modular_Exponentiation --
1910 -------------------------------
1911
1912 function UI_Modular_Exponentiation
1913 (B : Uint;
1914 E : Uint;
1915 Modulo : Uint) return Uint
1916 is
1917 M : constant Save_Mark := Mark;
1918
1919 Result : Uint := Uint_1;
1920 Base : Uint := B;
1921 Exponent : Uint := E;
1922
1923 begin
1924 while Exponent /= Uint_0 loop
1925 if Least_Sig_Digit (Exponent) rem Int'(2) = Int'(1) then
1926 Result := (Result * Base) rem Modulo;
1927 end if;
1928
1929 Exponent := Exponent / Uint_2;
1930 Base := (Base * Base) rem Modulo;
1931 end loop;
1932
1933 Release_And_Save (M, Result);
1934 return Result;
1935 end UI_Modular_Exponentiation;
1936
1937 ------------------------
1938 -- UI_Modular_Inverse --
1939 ------------------------
1940
1941 function UI_Modular_Inverse (N : Uint; Modulo : Uint) return Uint is
1942 M : constant Save_Mark := Mark;
1943 U : Uint;
1944 V : Uint;
1945 Q : Uint;
1946 R : Uint;
1947 X : Uint;
1948 Y : Uint;
1949 T : Uint;
1950 S : Int := 1;
1951
1952 begin
1953 U := Modulo;
1954 V := N;
1955
1956 X := Uint_1;
1957 Y := Uint_0;
1958
1959 loop
1960 UI_Div_Rem (U, V, Quotient => Q, Remainder => R);
1961
1962 U := V;
1963 V := R;
1964
1965 T := X;
1966 X := Y + Q * X;
1967 Y := T;
1968 S := -S;
1969
1970 exit when R = Uint_1;
1971 end loop;
1972
1973 if S = Int'(-1) then
1974 X := Modulo - X;
1975 end if;
1976
1977 Release_And_Save (M, X);
1978 return X;
1979 end UI_Modular_Inverse;
1980
1981 ------------
1982 -- UI_Mul --
1983 ------------
1984
1985 function UI_Mul (Left : Int; Right : Uint) return Uint is
1986 begin
1987 return UI_Mul (UI_From_Int (Left), Right);
1988 end UI_Mul;
1989
1990 function UI_Mul (Left : Uint; Right : Int) return Uint is
1991 begin
1992 return UI_Mul (Left, UI_From_Int (Right));
1993 end UI_Mul;
1994
1995 function UI_Mul (Left : Uint; Right : Uint) return Uint is
1996 begin
1997 -- Case where product fits in the range of a 32-bit integer
1998
1999 if Int (Left) <= Int (Uint_Max_Simple_Mul)
2000 and then
2001 Int (Right) <= Int (Uint_Max_Simple_Mul)
2002 then
2003 return UI_From_Int (Direct_Val (Left) * Direct_Val (Right));
2004 end if;
2005
2006 -- Otherwise we have the general case (Algorithm M in Knuth)
2007
2008 declare
2009 L_Length : constant Int := N_Digits (Left);
2010 R_Length : constant Int := N_Digits (Right);
2011 L_Vec : UI_Vector (1 .. L_Length);
2012 R_Vec : UI_Vector (1 .. R_Length);
2013 Neg : Boolean;
2014
2015 begin
2016 Init_Operand (Left, L_Vec);
2017 Init_Operand (Right, R_Vec);
2018 Neg := (L_Vec (1) < Int_0) xor (R_Vec (1) < Int_0);
2019 L_Vec (1) := abs (L_Vec (1));
2020 R_Vec (1) := abs (R_Vec (1));
2021
2022 Algorithm_M : declare
2023 Product : UI_Vector (1 .. L_Length + R_Length);
2024 Tmp_Sum : Int;
2025 Carry : Int;
2026
2027 begin
2028 for J in Product'Range loop
2029 Product (J) := 0;
2030 end loop;
2031
2032 for J in reverse R_Vec'Range loop
2033 Carry := 0;
2034 for K in reverse L_Vec'Range loop
2035 Tmp_Sum :=
2036 L_Vec (K) * R_Vec (J) + Product (J + K) + Carry;
2037 Product (J + K) := Tmp_Sum rem Base;
2038 Carry := Tmp_Sum / Base;
2039 end loop;
2040
2041 Product (J) := Carry;
2042 end loop;
2043
2044 return Vector_To_Uint (Product, Neg);
2045 end Algorithm_M;
2046 end;
2047 end UI_Mul;
2048
2049 ------------
2050 -- UI_Ne --
2051 ------------
2052
2053 function UI_Ne (Left : Int; Right : Uint) return Boolean is
2054 begin
2055 return UI_Ne (UI_From_Int (Left), Right);
2056 end UI_Ne;
2057
2058 function UI_Ne (Left : Uint; Right : Int) return Boolean is
2059 begin
2060 return UI_Ne (Left, UI_From_Int (Right));
2061 end UI_Ne;
2062
2063 function UI_Ne (Left : Uint; Right : Uint) return Boolean is
2064 begin
2065 -- Quick processing for identical arguments. Note that this takes
2066 -- care of the case of two No_Uint arguments.
2067
2068 if Int (Left) = Int (Right) then
2069 return False;
2070 end if;
2071
2072 -- See if left operand directly represented
2073
2074 if Direct (Left) then
2075
2076 -- If right operand directly represented then compare
2077
2078 if Direct (Right) then
2079 return Int (Left) /= Int (Right);
2080
2081 -- Left operand directly represented, right not, must be unequal
2082
2083 else
2084 return True;
2085 end if;
2086
2087 -- Right operand directly represented, left not, must be unequal
2088
2089 elsif Direct (Right) then
2090 return True;
2091 end if;
2092
2093 -- Otherwise both multi-word, do comparison
2094
2095 declare
2096 Size : constant Int := N_Digits (Left);
2097 Left_Loc : Int;
2098 Right_Loc : Int;
2099
2100 begin
2101 if Size /= N_Digits (Right) then
2102 return True;
2103 end if;
2104
2105 Left_Loc := Uints.Table (Left).Loc;
2106 Right_Loc := Uints.Table (Right).Loc;
2107
2108 for J in Int_0 .. Size - Int_1 loop
2109 if Udigits.Table (Left_Loc + J) /=
2110 Udigits.Table (Right_Loc + J)
2111 then
2112 return True;
2113 end if;
2114 end loop;
2115
2116 return False;
2117 end;
2118 end UI_Ne;
2119
2120 ----------------
2121 -- UI_Negate --
2122 ----------------
2123
2124 function UI_Negate (Right : Uint) return Uint is
2125 begin
2126 -- Case where input is directly represented. Note that since the range
2127 -- of Direct values is non-symmetrical, the result may not be directly
2128 -- represented, this is taken care of in UI_From_Int.
2129
2130 if Direct (Right) then
2131 return UI_From_Int (-Direct_Val (Right));
2132
2133 -- Full processing for multi-digit case. Note that we cannot just copy
2134 -- the value to the end of the table negating the first digit, since the
2135 -- range of Direct values is non-symmetrical, so we can have a negative
2136 -- value that is not Direct whose negation can be represented directly.
2137
2138 else
2139 declare
2140 R_Length : constant Int := N_Digits (Right);
2141 R_Vec : UI_Vector (1 .. R_Length);
2142 Neg : Boolean;
2143
2144 begin
2145 Init_Operand (Right, R_Vec);
2146 Neg := R_Vec (1) > Int_0;
2147 R_Vec (1) := abs R_Vec (1);
2148 return Vector_To_Uint (R_Vec, Neg);
2149 end;
2150 end if;
2151 end UI_Negate;
2152
2153 -------------
2154 -- UI_Rem --
2155 -------------
2156
2157 function UI_Rem (Left : Int; Right : Uint) return Uint is
2158 begin
2159 return UI_Rem (UI_From_Int (Left), Right);
2160 end UI_Rem;
2161
2162 function UI_Rem (Left : Uint; Right : Int) return Uint is
2163 begin
2164 return UI_Rem (Left, UI_From_Int (Right));
2165 end UI_Rem;
2166
2167 function UI_Rem (Left, Right : Uint) return Uint is
2168 Remainder : Uint;
2169 Quotient : Uint;
2170 pragma Warnings (Off, Quotient);
2171
2172 begin
2173 pragma Assert (Right /= Uint_0);
2174
2175 if Direct (Right) and then Direct (Left) then
2176 return UI_From_Int (Direct_Val (Left) rem Direct_Val (Right));
2177
2178 else
2179 UI_Div_Rem
2180 (Left, Right, Quotient, Remainder, Discard_Quotient => True);
2181 return Remainder;
2182 end if;
2183 end UI_Rem;
2184
2185 ------------
2186 -- UI_Sub --
2187 ------------
2188
2189 function UI_Sub (Left : Int; Right : Uint) return Uint is
2190 begin
2191 return UI_Add (Left, -Right);
2192 end UI_Sub;
2193
2194 function UI_Sub (Left : Uint; Right : Int) return Uint is
2195 begin
2196 return UI_Add (Left, -Right);
2197 end UI_Sub;
2198
2199 function UI_Sub (Left : Uint; Right : Uint) return Uint is
2200 begin
2201 if Direct (Left) and then Direct (Right) then
2202 return UI_From_Int (Direct_Val (Left) - Direct_Val (Right));
2203 else
2204 return UI_Add (Left, -Right);
2205 end if;
2206 end UI_Sub;
2207
2208 --------------
2209 -- UI_To_CC --
2210 --------------
2211
2212 function UI_To_CC (Input : Uint) return Char_Code is
2213 begin
2214 if Direct (Input) then
2215 return Char_Code (Direct_Val (Input));
2216
2217 -- Case of input is more than one digit
2218
2219 else
2220 declare
2221 In_Length : constant Int := N_Digits (Input);
2222 In_Vec : UI_Vector (1 .. In_Length);
2223 Ret_CC : Char_Code;
2224
2225 begin
2226 Init_Operand (Input, In_Vec);
2227
2228 -- We assume value is positive
2229
2230 Ret_CC := 0;
2231 for Idx in In_Vec'Range loop
2232 Ret_CC := Ret_CC * Char_Code (Base) +
2233 Char_Code (abs In_Vec (Idx));
2234 end loop;
2235
2236 return Ret_CC;
2237 end;
2238 end if;
2239 end UI_To_CC;
2240
2241 ----------------
2242 -- UI_To_Int --
2243 ----------------
2244
2245 function UI_To_Int (Input : Uint) return Int is
2246 pragma Assert (Input /= No_Uint);
2247
2248 begin
2249 if Direct (Input) then
2250 return Direct_Val (Input);
2251
2252 -- Case of input is more than one digit
2253
2254 else
2255 declare
2256 In_Length : constant Int := N_Digits (Input);
2257 In_Vec : UI_Vector (1 .. In_Length);
2258 Ret_Int : Int;
2259
2260 begin
2261 -- Uints of more than one digit could be outside the range for
2262 -- Ints. Caller should have checked for this if not certain.
2263 -- Constraint_Error to attempt to convert from value outside
2264 -- Int'Range.
2265
2266 if not UI_Is_In_Int_Range (Input) then
2267 raise Constraint_Error;
2268 end if;
2269
2270 -- Otherwise, proceed ahead, we are OK
2271
2272 Init_Operand (Input, In_Vec);
2273 Ret_Int := 0;
2274
2275 -- Calculate -|Input| and then negates if value is positive. This
2276 -- handles our current definition of Int (based on 2s complement).
2277 -- Is it secure enough???
2278
2279 for Idx in In_Vec'Range loop
2280 Ret_Int := Ret_Int * Base - abs In_Vec (Idx);
2281 end loop;
2282
2283 if In_Vec (1) < Int_0 then
2284 return Ret_Int;
2285 else
2286 return -Ret_Int;
2287 end if;
2288 end;
2289 end if;
2290 end UI_To_Int;
2291
2292 --------------
2293 -- UI_Write --
2294 --------------
2295
2296 procedure UI_Write (Input : Uint; Format : UI_Format := Auto) is
2297 begin
2298 Image_Out (Input, False, Format);
2299 end UI_Write;
2300
2301 ---------------------
2302 -- Vector_To_Uint --
2303 ---------------------
2304
2305 function Vector_To_Uint
2306 (In_Vec : UI_Vector;
2307 Negative : Boolean)
2308 return Uint
2309 is
2310 Size : Int;
2311 Val : Int;
2312
2313 begin
2314 -- The vector can contain leading zeros. These are not stored in the
2315 -- table, so loop through the vector looking for first non-zero digit
2316
2317 for J in In_Vec'Range loop
2318 if In_Vec (J) /= Int_0 then
2319
2320 -- The length of the value is the length of the rest of the vector
2321
2322 Size := In_Vec'Last - J + 1;
2323
2324 -- One digit value can always be represented directly
2325
2326 if Size = Int_1 then
2327 if Negative then
2328 return Uint (Int (Uint_Direct_Bias) - In_Vec (J));
2329 else
2330 return Uint (Int (Uint_Direct_Bias) + In_Vec (J));
2331 end if;
2332
2333 -- Positive two digit values may be in direct representation range
2334
2335 elsif Size = Int_2 and then not Negative then
2336 Val := In_Vec (J) * Base + In_Vec (J + 1);
2337
2338 if Val <= Max_Direct then
2339 return Uint (Int (Uint_Direct_Bias) + Val);
2340 end if;
2341 end if;
2342
2343 -- The value is outside the direct representation range and must
2344 -- therefore be stored in the table. Expand the table to contain
2345 -- the count and digits. The index of the new table entry will be
2346 -- returned as the result.
2347
2348 Uints.Append ((Length => Size, Loc => Udigits.Last + 1));
2349
2350 if Negative then
2351 Val := -In_Vec (J);
2352 else
2353 Val := +In_Vec (J);
2354 end if;
2355
2356 Udigits.Append (Val);
2357
2358 for K in 2 .. Size loop
2359 Udigits.Append (In_Vec (J + K - 1));
2360 end loop;
2361
2362 return Uints.Last;
2363 end if;
2364 end loop;
2365
2366 -- Dropped through loop only if vector contained all zeros
2367
2368 return Uint_0;
2369 end Vector_To_Uint;
2370
2371 end Uintp;