Merge from the pain train
[official-gcc.git] / gcc / ada / uintp.ads
blob97206ade7d7b97b7e1b5011858f35e4f6fd4db93
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- U I N T P --
6 -- --
7 -- S p e c --
8 -- --
9 -- Copyright (C) 1992-2005, 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 2, 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. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, USA. --
21 -- --
22 -- As a special exception, if other files instantiate generics from this --
23 -- unit, or you link this unit with other files to produce an executable, --
24 -- this unit does not by itself cause the resulting executable to be --
25 -- covered by the GNU General Public License. This exception does not --
26 -- however invalidate any other reasons why the executable file might be --
27 -- covered by the GNU Public License. --
28 -- --
29 -- GNAT was originally developed by the GNAT team at New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc. --
31 -- --
32 ------------------------------------------------------------------------------
34 -- Support for universal integer arithmetic
36 -- WARNING: There is a C version of this package. Any changes to this
37 -- source file must be properly reflected in the C header file sinfo.h
39 with Alloc;
40 with Table;
41 pragma Elaborate_All (Table);
42 with Types; use Types;
44 package Uintp is
46 -------------------------------------------------
47 -- Basic Types and Constants for Uintp Package --
48 -------------------------------------------------
50 type Uint is private;
51 -- The basic universal integer type
53 No_Uint : constant Uint;
54 -- A constant value indicating a missing or unset Uint value
56 Uint_0 : constant Uint;
57 Uint_1 : constant Uint;
58 Uint_2 : constant Uint;
59 Uint_3 : constant Uint;
60 Uint_4 : constant Uint;
61 Uint_5 : constant Uint;
62 Uint_6 : constant Uint;
63 Uint_7 : constant Uint;
64 Uint_8 : constant Uint;
65 Uint_9 : constant Uint;
66 Uint_10 : constant Uint;
67 Uint_12 : constant Uint;
68 Uint_15 : constant Uint;
69 Uint_16 : constant Uint;
70 Uint_24 : constant Uint;
71 Uint_32 : constant Uint;
72 Uint_63 : constant Uint;
73 Uint_64 : constant Uint;
74 Uint_80 : constant Uint;
75 Uint_128 : constant Uint;
77 Uint_Minus_1 : constant Uint;
78 Uint_Minus_2 : constant Uint;
79 Uint_Minus_3 : constant Uint;
80 Uint_Minus_4 : constant Uint;
81 Uint_Minus_5 : constant Uint;
82 Uint_Minus_6 : constant Uint;
83 Uint_Minus_7 : constant Uint;
84 Uint_Minus_8 : constant Uint;
85 Uint_Minus_9 : constant Uint;
86 Uint_Minus_12 : constant Uint;
87 Uint_Minus_36 : constant Uint;
88 Uint_Minus_63 : constant Uint;
89 Uint_Minus_80 : constant Uint;
90 Uint_Minus_128 : constant Uint;
92 -----------------
93 -- Subprograms --
94 -----------------
96 procedure Initialize;
97 -- Initialize Uint tables. Note that Initialize must not be called if
98 -- Tree_Read is used. Note also that there is no lock routine in this
99 -- unit, these are among the few tables that can be expanded during
100 -- gigi processing.
102 procedure Tree_Read;
103 -- Initializes internal tables from current tree file using the relevant
104 -- Table.Tree_Read routines. Note that Initialize should not be called if
105 -- Tree_Read is used. Tree_Read includes all necessary initialization.
107 procedure Tree_Write;
108 -- Writes out internal tables to current tree file using the relevant
109 -- Table.Tree_Write routines.
111 function UI_Abs (Right : Uint) return Uint;
112 pragma Inline (UI_Abs);
113 -- Returns abs function of universal integer.
115 function UI_Add (Left : Uint; Right : Uint) return Uint;
116 function UI_Add (Left : Int; Right : Uint) return Uint;
117 function UI_Add (Left : Uint; Right : Int) return Uint;
118 -- Returns sum of two integer values.
120 function UI_Decimal_Digits_Hi (U : Uint) return Nat;
121 -- Returns an estimate of the number of decimal digits required to
122 -- represent the absolute value of U. This estimate is correct or high,
123 -- i.e. it never returns a value that is too low. The accuracy of the
124 -- estimate affects only the effectiveness of comparison optimizations
125 -- in Urealp.
127 function UI_Decimal_Digits_Lo (U : Uint) return Nat;
128 -- Returns an estimate of the number of decimal digits required to
129 -- represent the absolute value of U. This estimate is correct or low,
130 -- i.e. it never returns a value that is too high. The accuracy of the
131 -- estimate affects only the effectiveness of comparison optimizations
132 -- in Urealp.
134 function UI_Div (Left : Uint; Right : Uint) return Uint;
135 function UI_Div (Left : Int; Right : Uint) return Uint;
136 function UI_Div (Left : Uint; Right : Int) return Uint;
137 -- Returns quotient of two integer values. Fatal error if Right = 0
139 function UI_Eq (Left : Uint; Right : Uint) return Boolean;
140 function UI_Eq (Left : Int; Right : Uint) return Boolean;
141 function UI_Eq (Left : Uint; Right : Int) return Boolean;
142 pragma Inline (UI_Eq);
143 -- Compares integer values for equality.
145 function UI_Expon (Left : Uint; Right : Uint) return Uint;
146 function UI_Expon (Left : Int; Right : Uint) return Uint;
147 function UI_Expon (Left : Uint; Right : Int) return Uint;
148 function UI_Expon (Left : Int; Right : Int) return Uint;
149 -- Returns result of exponentiating two integer values
150 -- Fatal error if Right is negative.
152 function UI_GCD (Uin, Vin : Uint) return Uint;
153 -- Computes GCD of input values. Assumes Uin >= Vin >= 0.
155 function UI_Ge (Left : Uint; Right : Uint) return Boolean;
156 function UI_Ge (Left : Int; Right : Uint) return Boolean;
157 function UI_Ge (Left : Uint; Right : Int) return Boolean;
158 pragma Inline (UI_Ge);
159 -- Compares integer values for greater than or equal.
161 function UI_Gt (Left : Uint; Right : Uint) return Boolean;
162 function UI_Gt (Left : Int; Right : Uint) return Boolean;
163 function UI_Gt (Left : Uint; Right : Int) return Boolean;
164 pragma Inline (UI_Gt);
165 -- Compares integer values for greater than.
167 function UI_Is_In_Int_Range (Input : Uint) return Boolean;
168 pragma Inline (UI_Is_In_Int_Range);
169 -- Determines if universal integer is in Int range.
171 function UI_Le (Left : Uint; Right : Uint) return Boolean;
172 function UI_Le (Left : Int; Right : Uint) return Boolean;
173 function UI_Le (Left : Uint; Right : Int) return Boolean;
174 pragma Inline (UI_Le);
175 -- Compares integer values for less than or equal.
177 function UI_Lt (Left : Uint; Right : Uint) return Boolean;
178 function UI_Lt (Left : Int; Right : Uint) return Boolean;
179 function UI_Lt (Left : Uint; Right : Int) return Boolean;
180 -- Compares integer values for less than.
182 function UI_Max (Left : Uint; Right : Uint) return Uint;
183 function UI_Max (Left : Int; Right : Uint) return Uint;
184 function UI_Max (Left : Uint; Right : Int) return Uint;
185 -- Returns maximum of two integer values
187 function UI_Min (Left : Uint; Right : Uint) return Uint;
188 function UI_Min (Left : Int; Right : Uint) return Uint;
189 function UI_Min (Left : Uint; Right : Int) return Uint;
190 -- Returns minimum of two integer values.
192 function UI_Mod (Left : Uint; Right : Uint) return Uint;
193 function UI_Mod (Left : Int; Right : Uint) return Uint;
194 function UI_Mod (Left : Uint; Right : Int) return Uint;
195 pragma Inline (UI_Mod);
196 -- Returns mod function of two integer values.
198 function UI_Mul (Left : Uint; Right : Uint) return Uint;
199 function UI_Mul (Left : Int; Right : Uint) return Uint;
200 function UI_Mul (Left : Uint; Right : Int) return Uint;
201 -- Returns product of two integer values
203 function UI_Ne (Left : Uint; Right : Uint) return Boolean;
204 function UI_Ne (Left : Int; Right : Uint) return Boolean;
205 function UI_Ne (Left : Uint; Right : Int) return Boolean;
206 pragma Inline (UI_Ne);
207 -- Compares integer values for inequality.
209 function UI_Negate (Right : Uint) return Uint;
210 pragma Inline (UI_Negate);
211 -- Returns negative of universal integer.
213 function UI_Rem (Left : Uint; Right : Uint) return Uint;
214 function UI_Rem (Left : Int; Right : Uint) return Uint;
215 function UI_Rem (Left : Uint; Right : Int) return Uint;
216 -- Returns rem of two integer values.
218 function UI_Sub (Left : Uint; Right : Uint) return Uint;
219 function UI_Sub (Left : Int; Right : Uint) return Uint;
220 function UI_Sub (Left : Uint; Right : Int) return Uint;
221 pragma Inline (UI_Sub);
222 -- Returns difference of two integer values
224 function UI_From_Dint (Input : Dint) return Uint;
225 -- Converts Dint value to universal integer form
227 function UI_From_Int (Input : Int) return Uint;
228 -- Converts Int value to universal integer form
230 function UI_From_CC (Input : Char_Code) return Uint;
231 -- Converts Char_Code value to universal integer form
233 function UI_To_Int (Input : Uint) return Int;
234 -- Converts universal integer value to Int. Fatal error if value is not in
235 -- appropriate range.
237 function UI_To_CC (Input : Uint) return Char_Code;
238 -- Converts universal integer value to Char_Code. Fatal error if value is
239 -- not in Char_Code range.
241 function Num_Bits (Input : Uint) return Nat;
242 -- Approximate number of binary bits in given universal integer.
243 -- This function is used for capacity checks, and it can be one
244 -- bit off without affecting its usage.
246 ---------------------
247 -- Output Routines --
248 ---------------------
250 type UI_Format is (Hex, Decimal, Auto);
251 -- Used to determine whether UI_Image/UI_Write output is in hexadecimal
252 -- or decimal format. Auto, the default setting, lets the routine make
253 -- a decision based on the value.
255 UI_Image_Max : constant := 32;
256 UI_Image_Buffer : String (1 .. UI_Image_Max);
257 UI_Image_Length : Natural;
258 -- Buffer used for UI_Image as described below
260 procedure UI_Image (Input : Uint; Format : UI_Format := Auto);
261 -- Places a representation of Uint, consisting of a possible minus sign,
262 -- followed by the value in UI_Image_Buffer. The form of the value is an
263 -- integer literal in either decimal (no base) or hexadecimal (base 16)
264 -- format. If Hex is True on entry, then hex mode is forced, otherwise
265 -- UI_Image makes a guess at which output format is more convenient. The
266 -- value must fit in UI_Image_Buffer. If necessary, the result is an
267 -- approximation of the proper value, using an exponential format. The
268 -- image of No_Uint is output as a single question mark.
270 procedure UI_Write (Input : Uint; Format : UI_Format := Auto);
271 -- Writes a representation of Uint, consisting of a possible minus sign,
272 -- followed by the value to the output file. The form of the value is an
273 -- integer literal in either decimal (no base) or hexadecimal (base 16)
274 -- format as appropriate. UI_Format shows which format to use. Auto,
275 -- the default, asks UI_Write to make a guess at which output format
276 -- will be more convenient to read.
278 procedure pid (Input : Uint);
279 pragma Export (Ada, pid);
280 -- Writes representation of Uint in decimal with a terminating line
281 -- return. This is intended for use from the debugger.
283 procedure pih (Input : Uint);
284 pragma Export (Ada, pih);
285 -- Writes representation of Uint in hex with a terminating line return.
286 -- This is intended for use from the debugger.
288 ------------------------
289 -- Operator Renamings --
290 ------------------------
292 function "+" (Left : Uint; Right : Uint) return Uint renames UI_Add;
293 function "+" (Left : Int; Right : Uint) return Uint renames UI_Add;
294 function "+" (Left : Uint; Right : Int) return Uint renames UI_Add;
296 function "/" (Left : Uint; Right : Uint) return Uint renames UI_Div;
297 function "/" (Left : Int; Right : Uint) return Uint renames UI_Div;
298 function "/" (Left : Uint; Right : Int) return Uint renames UI_Div;
300 function "*" (Left : Uint; Right : Uint) return Uint renames UI_Mul;
301 function "*" (Left : Int; Right : Uint) return Uint renames UI_Mul;
302 function "*" (Left : Uint; Right : Int) return Uint renames UI_Mul;
304 function "-" (Left : Uint; Right : Uint) return Uint renames UI_Sub;
305 function "-" (Left : Int; Right : Uint) return Uint renames UI_Sub;
306 function "-" (Left : Uint; Right : Int) return Uint renames UI_Sub;
308 function "**" (Left : Uint; Right : Uint) return Uint renames UI_Expon;
309 function "**" (Left : Uint; Right : Int) return Uint renames UI_Expon;
310 function "**" (Left : Int; Right : Uint) return Uint renames UI_Expon;
311 function "**" (Left : Int; Right : Int) return Uint renames UI_Expon;
313 function "abs" (Real : Uint) return Uint renames UI_Abs;
315 function "mod" (Left : Uint; Right : Uint) return Uint renames UI_Mod;
316 function "mod" (Left : Int; Right : Uint) return Uint renames UI_Mod;
317 function "mod" (Left : Uint; Right : Int) return Uint renames UI_Mod;
319 function "rem" (Left : Uint; Right : Uint) return Uint renames UI_Rem;
320 function "rem" (Left : Int; Right : Uint) return Uint renames UI_Rem;
321 function "rem" (Left : Uint; Right : Int) return Uint renames UI_Rem;
323 function "-" (Real : Uint) return Uint renames UI_Negate;
325 function "=" (Left : Uint; Right : Uint) return Boolean renames UI_Eq;
326 function "=" (Left : Int; Right : Uint) return Boolean renames UI_Eq;
327 function "=" (Left : Uint; Right : Int) return Boolean renames UI_Eq;
329 function ">=" (Left : Uint; Right : Uint) return Boolean renames UI_Ge;
330 function ">=" (Left : Int; Right : Uint) return Boolean renames UI_Ge;
331 function ">=" (Left : Uint; Right : Int) return Boolean renames UI_Ge;
333 function ">" (Left : Uint; Right : Uint) return Boolean renames UI_Gt;
334 function ">" (Left : Int; Right : Uint) return Boolean renames UI_Gt;
335 function ">" (Left : Uint; Right : Int) return Boolean renames UI_Gt;
337 function "<=" (Left : Uint; Right : Uint) return Boolean renames UI_Le;
338 function "<=" (Left : Int; Right : Uint) return Boolean renames UI_Le;
339 function "<=" (Left : Uint; Right : Int) return Boolean renames UI_Le;
341 function "<" (Left : Uint; Right : Uint) return Boolean renames UI_Lt;
342 function "<" (Left : Int; Right : Uint) return Boolean renames UI_Lt;
343 function "<" (Left : Uint; Right : Int) return Boolean renames UI_Lt;
345 -----------------------------
346 -- Mark/Release Processing --
347 -----------------------------
349 -- The space used by Uint data is not automatically reclaimed. However,
350 -- a mark-release regime is implemented which allows storage to be
351 -- released back to a previously noted mark. This is used for example
352 -- when doing comparisons, where only intermediate results get stored
353 -- that do not need to be saved for future use.
355 type Save_Mark is private;
357 function Mark return Save_Mark;
358 -- Note mark point for future release
360 procedure Release (M : Save_Mark);
361 -- Release storage allocated since mark was noted
363 procedure Release_And_Save (M : Save_Mark; UI : in out Uint);
364 -- Like Release, except that the given Uint value (which is typically
365 -- among the data being released) is recopied after the release, so
366 -- that it is the most recent item, and UI is updated to point to
367 -- its copied location.
369 procedure Release_And_Save (M : Save_Mark; UI1, UI2 : in out Uint);
370 -- Like Release, except that the given Uint values (which are typically
371 -- among the data being released) are recopied after the release, so
372 -- that they are the most recent items, and UI1 and UI2 are updated if
373 -- necessary to point to the copied locations. This routine is careful
374 -- to do things in the right order, so that the values do not clobber
375 -- one another.
377 -----------------------------------
378 -- Representation of Uint Values --
379 -----------------------------------
381 private
383 type Uint is new Int range Uint_Low_Bound .. Uint_High_Bound;
384 for Uint'Size use 32;
386 No_Uint : constant Uint := Uint (Uint_Low_Bound);
388 -- Uint values are represented as multiple precision integers stored in
389 -- a multi-digit format using Base as the base. This value is chosen so
390 -- that the product Base*Base is within the range of allowed Int values.
392 -- Base is defined to allow efficient execution of the primitive
393 -- operations (a0, b0, c0) defined in the section "The Classical
394 -- Algorithms" (sec. 4.3.1) of Donald Knuth's "The Art of Computer
395 -- Programming", Vol. 2. These algorithms are used in this package.
397 Base_Bits : constant := 15;
398 -- Number of bits in base value
400 Base : constant Int := 2 ** Base_Bits;
402 -- Values in the range -(Base+1) .. maxdirect are encoded directly as
403 -- Uint values by adding a bias value. The value of maxdirect is chosen
404 -- so that a directly represented number always fits in two digits when
405 -- represented in base format.
407 Min_Direct : constant Int := -(Base - 1);
408 Max_Direct : constant Int := (Base - 1) * (Base - 1);
410 -- The following values define the bias used to store Uint values which
411 -- are in this range, as well as the biased values for the first and
412 -- last values in this range. We use a new derived type for these
413 -- constants to avoid accidental use of Uint arithmetic on these
414 -- values, which is never correct.
416 type Ctrl is range Int'First .. Int'Last;
418 Uint_Direct_Bias : constant Ctrl := Ctrl (Uint_Low_Bound) + Ctrl (Base);
419 Uint_Direct_First : constant Ctrl := Uint_Direct_Bias + Ctrl (Min_Direct);
420 Uint_Direct_Last : constant Ctrl := Uint_Direct_Bias + Ctrl (Max_Direct);
422 Uint_0 : constant Uint := Uint (Uint_Direct_Bias);
423 Uint_1 : constant Uint := Uint (Uint_Direct_Bias + 1);
424 Uint_2 : constant Uint := Uint (Uint_Direct_Bias + 2);
425 Uint_3 : constant Uint := Uint (Uint_Direct_Bias + 3);
426 Uint_4 : constant Uint := Uint (Uint_Direct_Bias + 4);
427 Uint_5 : constant Uint := Uint (Uint_Direct_Bias + 5);
428 Uint_6 : constant Uint := Uint (Uint_Direct_Bias + 6);
429 Uint_7 : constant Uint := Uint (Uint_Direct_Bias + 7);
430 Uint_8 : constant Uint := Uint (Uint_Direct_Bias + 8);
431 Uint_9 : constant Uint := Uint (Uint_Direct_Bias + 9);
432 Uint_10 : constant Uint := Uint (Uint_Direct_Bias + 10);
433 Uint_12 : constant Uint := Uint (Uint_Direct_Bias + 12);
434 Uint_15 : constant Uint := Uint (Uint_Direct_Bias + 15);
435 Uint_16 : constant Uint := Uint (Uint_Direct_Bias + 16);
436 Uint_24 : constant Uint := Uint (Uint_Direct_Bias + 24);
437 Uint_32 : constant Uint := Uint (Uint_Direct_Bias + 32);
438 Uint_63 : constant Uint := Uint (Uint_Direct_Bias + 63);
439 Uint_64 : constant Uint := Uint (Uint_Direct_Bias + 64);
440 Uint_80 : constant Uint := Uint (Uint_Direct_Bias + 80);
441 Uint_128 : constant Uint := Uint (Uint_Direct_Bias + 128);
443 Uint_Minus_1 : constant Uint := Uint (Uint_Direct_Bias - 1);
444 Uint_Minus_2 : constant Uint := Uint (Uint_Direct_Bias - 2);
445 Uint_Minus_3 : constant Uint := Uint (Uint_Direct_Bias - 3);
446 Uint_Minus_4 : constant Uint := Uint (Uint_Direct_Bias - 4);
447 Uint_Minus_5 : constant Uint := Uint (Uint_Direct_Bias - 5);
448 Uint_Minus_6 : constant Uint := Uint (Uint_Direct_Bias - 6);
449 Uint_Minus_7 : constant Uint := Uint (Uint_Direct_Bias - 7);
450 Uint_Minus_8 : constant Uint := Uint (Uint_Direct_Bias - 8);
451 Uint_Minus_9 : constant Uint := Uint (Uint_Direct_Bias - 9);
452 Uint_Minus_12 : constant Uint := Uint (Uint_Direct_Bias - 12);
453 Uint_Minus_36 : constant Uint := Uint (Uint_Direct_Bias - 36);
454 Uint_Minus_63 : constant Uint := Uint (Uint_Direct_Bias - 63);
455 Uint_Minus_80 : constant Uint := Uint (Uint_Direct_Bias - 80);
456 Uint_Minus_128 : constant Uint := Uint (Uint_Direct_Bias - 128);
458 type Save_Mark is record
459 Save_Uint : Uint;
460 Save_Udigit : Int;
461 end record;
463 -- Values outside the range that is represented directly are stored
464 -- using two tables. The secondary table Udigits contains sequences of
465 -- Int values consisting of the digits of the number in a radix Base
466 -- system. The digits are stored from most significant to least
467 -- significant with the first digit only carrying the sign.
469 -- There is one entry in the primary Uints table for each distinct Uint
470 -- value. This table entry contains the length (number of digits) and
471 -- a starting offset of the value in the Udigits table.
473 Uint_First_Entry : constant Uint := Uint (Uint_Table_Start);
475 -- Some subprograms defined in this package manipulate the Udigits
476 -- table directly, while for others it is more convenient to work with
477 -- locally defined arrays of the digits of the Universal Integers.
478 -- The type UI_Vector is defined for this purpose and some internal
479 -- subprograms used for converting from one to the other are defined.
481 type UI_Vector is array (Pos range <>) of Int;
482 -- Vector containing the integer values of a Uint value
484 -- Note: An earlier version of this package used pointers of arrays
485 -- of Ints (dynamically allocated) for the Uint type. The change
486 -- leads to a few less natural idioms used throughout this code, but
487 -- eliminates all uses of the heap except for the table package itself.
488 -- For example, Uint parameters are often converted to UI_Vectors for
489 -- internal manipulation. This is done by creating the local UI_Vector
490 -- using the function N_Digits on the Uint to find the size needed for
491 -- the vector, and then calling Init_Operand to copy the values out
492 -- of the table into the vector.
494 type Uint_Entry is record
495 Length : Pos;
496 -- Length of entry in Udigits table in digits (i.e. in words)
498 Loc : Int;
499 -- Starting location in Udigits table of this Uint value
500 end record;
502 package Uints is new Table.Table (
503 Table_Component_Type => Uint_Entry,
504 Table_Index_Type => Uint,
505 Table_Low_Bound => Uint_First_Entry,
506 Table_Initial => Alloc.Uints_Initial,
507 Table_Increment => Alloc.Uints_Increment,
508 Table_Name => "Uints");
510 package Udigits is new Table.Table (
511 Table_Component_Type => Int,
512 Table_Index_Type => Int,
513 Table_Low_Bound => 0,
514 Table_Initial => Alloc.Udigits_Initial,
515 Table_Increment => Alloc.Udigits_Increment,
516 Table_Name => "Udigits");
518 -- Note: the reason these tables are defined here in the private part of
519 -- the spec, rather than in the body, is that they are refrerenced
520 -- directly by gigi.
522 end Uintp;