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