| 1 |
------------------------------------------------------------------------------ |
| 2 |
-- -- |
| 3 |
-- GNAT COMPILER COMPONENTS -- |
| 4 |
-- -- |
| 5 |
-- U I N T P -- |
| 6 |
-- -- |
| 7 |
-- S p e c -- |
| 8 |
-- -- |
| 9 |
-- Copyright (C) 1992-2025, 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. 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 COPYING3. If not, go to -- |
| 19 |
-- http://www.gnu.org/licenses for a complete copy of the license. -- |
| 20 |
-- -- |
| 21 |
-- GNAT was originally developed by the GNAT team at New York University. -- |
| 22 |
-- Extensive contributions were provided by Ada Core Technologies Inc. -- |
| 23 |
-- -- |
| 24 |
------------------------------------------------------------------------------ |
| 25 |
|
| 26 |
-- Support for universal integer arithmetic |
| 27 |
|
| 28 |
-- WARNING: There is a C version of this package. Any changes to this |
| 29 |
-- source file must be properly reflected in the C header file uintp.h |
| 30 |
|
| 31 |
with Alloc; |
| 32 |
with Table; |
| 33 |
pragma Elaborate_All (Table); |
| 34 |
with Types; use Types; |
| 35 |
|
| 36 |
package Uintp is |
| 37 |
|
| 38 |
------------------------------------------------- |
| 39 |
-- Basic Types and Constants for Uintp Package -- |
| 40 |
------------------------------------------------- |
| 41 |
|
| 42 |
type Uint is private; |
| 43 |
-- The basic universal integer type |
| 44 |
|
| 45 |
No_Uint : constant Uint; |
| 46 |
-- A constant value indicating a missing or unset Uint value |
| 47 |
|
| 48 |
Uint_0 : constant Uint; |
| 49 |
Uint_1 : constant Uint; |
| 50 |
Uint_2 : constant Uint; |
| 51 |
Uint_3 : constant Uint; |
| 52 |
Uint_4 : constant Uint; |
| 53 |
Uint_5 : constant Uint; |
| 54 |
Uint_6 : constant Uint; |
| 55 |
Uint_7 : constant Uint; |
| 56 |
Uint_8 : constant Uint; |
| 57 |
Uint_9 : constant Uint; |
| 58 |
Uint_10 : constant Uint; |
| 59 |
Uint_11 : constant Uint; |
| 60 |
Uint_12 : constant Uint; |
| 61 |
Uint_13 : constant Uint; |
| 62 |
Uint_14 : constant Uint; |
| 63 |
Uint_15 : constant Uint; |
| 64 |
Uint_16 : constant Uint; |
| 65 |
Uint_24 : constant Uint; |
| 66 |
Uint_31 : constant Uint; |
| 67 |
Uint_32 : constant Uint; |
| 68 |
Uint_63 : constant Uint; |
| 69 |
Uint_64 : constant Uint; |
| 70 |
Uint_80 : constant Uint; |
| 71 |
Uint_127 : constant Uint; |
| 72 |
Uint_128 : constant Uint; |
| 73 |
Uint_256 : constant Uint; |
| 74 |
|
| 75 |
Uint_Minus_1 : constant Uint; |
| 76 |
Uint_Minus_2 : constant Uint; |
| 77 |
Uint_Minus_3 : constant Uint; |
| 78 |
Uint_Minus_4 : constant Uint; |
| 79 |
Uint_Minus_5 : constant Uint; |
| 80 |
Uint_Minus_6 : constant Uint; |
| 81 |
Uint_Minus_7 : constant Uint; |
| 82 |
Uint_Minus_8 : constant Uint; |
| 83 |
Uint_Minus_9 : constant Uint; |
| 84 |
Uint_Minus_12 : constant Uint; |
| 85 |
Uint_Minus_18 : constant Uint; |
| 86 |
Uint_Minus_31 : constant Uint; |
| 87 |
Uint_Minus_36 : constant Uint; |
| 88 |
Uint_Minus_63 : constant Uint; |
| 89 |
Uint_Minus_76 : constant Uint; |
| 90 |
Uint_Minus_80 : constant Uint; |
| 91 |
Uint_Minus_127 : constant Uint; |
| 92 |
Uint_Minus_128 : constant Uint; |
| 93 |
|
| 94 |
-- Functions for detecting No_Uint. Note that clients of this package |
| 95 |
-- cannot use "=" and "/=" to compare with No_Uint; they must use No |
| 96 |
-- and Present instead. |
| 97 |
|
| 98 |
function No (X : Uint) return Boolean is (X = No_Uint); |
| 99 |
-- Note that this is using the predefined "=", not the "=" declared below, |
| 100 |
-- which would blow up on No_Uint. |
| 101 |
|
| 102 |
function Present (X : Uint) return Boolean is (not No (X)); |
| 103 |
|
| 104 |
subtype Valid_Uint is Uint with Predicate => Present (Valid_Uint); |
| 105 |
subtype Unat is Valid_Uint with Predicate => Unat >= Uint_0; -- natural |
| 106 |
subtype Upos is Valid_Uint with Predicate => Upos >= Uint_1; -- positive |
| 107 |
subtype Nonzero_Uint is Valid_Uint with Predicate => Nonzero_Uint /= Uint_0; |
| 108 |
subtype Unegative is Valid_Uint with Predicate => Unegative < Uint_0; |
| 109 |
subtype Ubool is Valid_Uint with |
| 110 |
Predicate => Ubool = Uint_0 or else Ubool = Uint_1; |
| 111 |
subtype Opt_Ubool is Uint with |
| 112 |
Predicate => No (Opt_Ubool) or else Opt_Ubool in Ubool; |
| 113 |
|
| 114 |
----------------- |
| 115 |
-- Subprograms -- |
| 116 |
----------------- |
| 117 |
|
| 118 |
procedure Initialize; |
| 119 |
-- Initialize Uint tables. Note also that there is no lock routine in this |
| 120 |
-- unit, these are among the few tables that can be expanded during |
| 121 |
-- gigi processing. |
| 122 |
|
| 123 |
function UI_Abs (Right : Valid_Uint) return Unat; |
| 124 |
pragma Inline (UI_Abs); |
| 125 |
-- Returns abs function of universal integer |
| 126 |
|
| 127 |
function UI_Add (Left : Valid_Uint; Right : Valid_Uint) return Valid_Uint; |
| 128 |
function UI_Add (Left : Int; Right : Valid_Uint) return Valid_Uint; |
| 129 |
function UI_Add (Left : Valid_Uint; Right : Int) return Valid_Uint; |
| 130 |
-- Returns sum of two integer values |
| 131 |
|
| 132 |
function UI_Decimal_Digits_Hi (U : Valid_Uint) return Nat; |
| 133 |
-- Returns an estimate of the number of decimal digits required to |
| 134 |
-- represent the absolute value of U. This estimate is correct or high, |
| 135 |
-- i.e. it never returns a value that is too low. The accuracy of the |
| 136 |
-- estimate affects only the effectiveness of comparison optimizations |
| 137 |
-- in Urealp. |
| 138 |
|
| 139 |
function UI_Decimal_Digits_Lo (U : Valid_Uint) return Nat; |
| 140 |
-- Returns an estimate of the number of decimal digits required to |
| 141 |
-- represent the absolute value of U. This estimate is correct or low, |
| 142 |
-- i.e. it never returns a value that is too high. The accuracy of the |
| 143 |
-- estimate affects only the effectiveness of comparison optimizations |
| 144 |
-- in Urealp. |
| 145 |
|
| 146 |
function UI_Div (Left : Valid_Uint; Right : Nonzero_Uint) return Valid_Uint; |
| 147 |
function UI_Div (Left : Int; Right : Nonzero_Uint) return Valid_Uint; |
| 148 |
function UI_Div (Left : Valid_Uint; Right : Nonzero_Int) return Valid_Uint; |
| 149 |
-- Returns quotient of two integer values. Fatal error if Right = 0 |
| 150 |
|
| 151 |
function UI_Eq (Left : Valid_Uint; Right : Valid_Uint) return Boolean; |
| 152 |
function UI_Eq (Left : Int; Right : Valid_Uint) return Boolean; |
| 153 |
function UI_Eq (Left : Valid_Uint; Right : Int) return Boolean; |
| 154 |
pragma Inline (UI_Eq); |
| 155 |
-- Compares integer values for equality |
| 156 |
|
| 157 |
function UI_Expon (Left : Valid_Uint; Right : Unat) return Valid_Uint; |
| 158 |
function UI_Expon (Left : Int; Right : Unat) return Valid_Uint; |
| 159 |
function UI_Expon (Left : Valid_Uint; Right : Nat) return Valid_Uint; |
| 160 |
function UI_Expon (Left : Int; Right : Nat) return Valid_Uint; |
| 161 |
-- Returns result of exponentiating two integer values. |
| 162 |
-- Fatal error if Right is negative. |
| 163 |
|
| 164 |
function UI_GCD (Uin, Vin : Valid_Uint) return Valid_Uint; |
| 165 |
-- Computes GCD of input values. Assumes Uin >= Vin >= 0 |
| 166 |
|
| 167 |
function UI_Ge (Left : Valid_Uint; Right : Valid_Uint) return Boolean; |
| 168 |
function UI_Ge (Left : Int; Right : Valid_Uint) return Boolean; |
| 169 |
function UI_Ge (Left : Valid_Uint; Right : Int) return Boolean; |
| 170 |
pragma Inline (UI_Ge); |
| 171 |
-- Compares integer values for greater than or equal |
| 172 |
|
| 173 |
function UI_Gt (Left : Valid_Uint; Right : Valid_Uint) return Boolean; |
| 174 |
function UI_Gt (Left : Int; Right : Valid_Uint) return Boolean; |
| 175 |
function UI_Gt (Left : Valid_Uint; Right : Int) return Boolean; |
| 176 |
pragma Inline (UI_Gt); |
| 177 |
-- Compares integer values for greater than |
| 178 |
|
| 179 |
function UI_Is_In_Int_Range (Input : Valid_Uint) return Boolean; |
| 180 |
pragma Inline (UI_Is_In_Int_Range); |
| 181 |
-- Determines if universal integer is in Int range. |
| 182 |
|
| 183 |
function UI_Le (Left : Valid_Uint; Right : Valid_Uint) return Boolean; |
| 184 |
function UI_Le (Left : Int; Right : Valid_Uint) return Boolean; |
| 185 |
function UI_Le (Left : Valid_Uint; Right : Int) return Boolean; |
| 186 |
pragma Inline (UI_Le); |
| 187 |
-- Compares integer values for less than or equal |
| 188 |
|
| 189 |
function UI_Lt (Left : Valid_Uint; Right : Valid_Uint) return Boolean; |
| 190 |
function UI_Lt (Left : Int; Right : Valid_Uint) return Boolean; |
| 191 |
function UI_Lt (Left : Valid_Uint; Right : Int) return Boolean; |
| 192 |
-- Compares integer values for less than |
| 193 |
|
| 194 |
function UI_Max (Left : Valid_Uint; Right : Valid_Uint) return Valid_Uint; |
| 195 |
function UI_Max (Left : Int; Right : Valid_Uint) return Valid_Uint; |
| 196 |
function UI_Max (Left : Valid_Uint; Right : Int) return Valid_Uint; |
| 197 |
-- Returns maximum of two integer values |
| 198 |
|
| 199 |
function UI_Min (Left : Valid_Uint; Right : Valid_Uint) return Valid_Uint; |
| 200 |
function UI_Min (Left : Int; Right : Valid_Uint) return Valid_Uint; |
| 201 |
function UI_Min (Left : Valid_Uint; Right : Int) return Valid_Uint; |
| 202 |
-- Returns minimum of two integer values |
| 203 |
|
| 204 |
function UI_Mod (Left : Valid_Uint; Right : Nonzero_Uint) return Valid_Uint; |
| 205 |
function UI_Mod (Left : Int; Right : Nonzero_Uint) return Valid_Uint; |
| 206 |
function UI_Mod (Left : Valid_Uint; Right : Nonzero_Int) return Valid_Uint; |
| 207 |
pragma Inline (UI_Mod); |
| 208 |
-- Returns mod function of two integer values |
| 209 |
|
| 210 |
function UI_Mul (Left : Valid_Uint; Right : Valid_Uint) return Valid_Uint; |
| 211 |
function UI_Mul (Left : Int; Right : Valid_Uint) return Valid_Uint; |
| 212 |
function UI_Mul (Left : Valid_Uint; Right : Int) return Valid_Uint; |
| 213 |
-- Returns product of two integer values |
| 214 |
|
| 215 |
function UI_Ne (Left : Valid_Uint; Right : Valid_Uint) return Boolean; |
| 216 |
function UI_Ne (Left : Int; Right : Valid_Uint) return Boolean; |
| 217 |
function UI_Ne (Left : Valid_Uint; Right : Int) return Boolean; |
| 218 |
pragma Inline (UI_Ne); |
| 219 |
-- Compares integer values for inequality |
| 220 |
|
| 221 |
function UI_Negate (Right : Valid_Uint) return Valid_Uint; |
| 222 |
pragma Inline (UI_Negate); |
| 223 |
-- Returns negative of universal integer |
| 224 |
|
| 225 |
function UI_Rem (Left : Valid_Uint; Right : Nonzero_Uint) return Valid_Uint; |
| 226 |
function UI_Rem (Left : Int; Right : Nonzero_Uint) return Valid_Uint; |
| 227 |
function UI_Rem (Left : Valid_Uint; Right : Nonzero_Int) return Valid_Uint; |
| 228 |
-- Returns rem of two integer values |
| 229 |
|
| 230 |
function UI_Sub (Left : Valid_Uint; Right : Valid_Uint) return Valid_Uint; |
| 231 |
function UI_Sub (Left : Int; Right : Valid_Uint) return Valid_Uint; |
| 232 |
function UI_Sub (Left : Valid_Uint; Right : Int) return Valid_Uint; |
| 233 |
pragma Inline (UI_Sub); |
| 234 |
-- Returns difference of two integer values |
| 235 |
|
| 236 |
function UI_From_Int (Input : Int) return Valid_Uint; |
| 237 |
-- Converts Int value to universal integer form |
| 238 |
|
| 239 |
generic |
| 240 |
type In_T is range <>; |
| 241 |
function UI_From_Integral (Input : In_T) return Valid_Uint; |
| 242 |
-- Likewise, but converts from any integer type. Must not be applied to |
| 243 |
-- biased types (instantiation will provide a warning if actual is a biased |
| 244 |
-- type). |
| 245 |
|
| 246 |
function UI_From_CC (Input : Char_Code) return Valid_Uint; |
| 247 |
-- Converts Char_Code value to universal integer form |
| 248 |
|
| 249 |
function UI_To_Int (Input : Valid_Uint) return Int; |
| 250 |
-- Converts universal integer value to Int. Constraint_Error if value is |
| 251 |
-- not in appropriate range. |
| 252 |
|
| 253 |
type Unsigned_64 is mod 2**64; |
| 254 |
function UI_To_Unsigned_64 (Input : Valid_Uint) return Unsigned_64; |
| 255 |
-- Converts universal integer value to Unsigned_64. Constraint_Error if |
| 256 |
-- value is not in appropriate range. |
| 257 |
|
| 258 |
function UI_To_CC (Input : Valid_Uint) return Char_Code; |
| 259 |
-- Converts universal integer value to Char_Code. Constraint_Error if value |
| 260 |
-- is not in Char_Code range. |
| 261 |
|
| 262 |
function Num_Bits (Input : Valid_Uint) return Nat; |
| 263 |
-- Approximate number of binary bits in given universal integer. This |
| 264 |
-- function is used for capacity checks, and it can be one bit off |
| 265 |
-- without affecting its usage. |
| 266 |
|
| 267 |
function Type_Size_For (Input : Valid_Uint) return Nat; |
| 268 |
-- Returns the size of an integer type that can accommodate Input |
| 269 |
|
| 270 |
--------------------- |
| 271 |
-- Output Routines -- |
| 272 |
--------------------- |
| 273 |
|
| 274 |
type UI_Format is (Hex, Decimal, Auto); |
| 275 |
-- Used to determine whether UI_Image/UI_Write output is in hexadecimal |
| 276 |
-- or decimal format. Auto, the default setting, lets the routine make a |
| 277 |
-- decision based on the value. |
| 278 |
|
| 279 |
UI_Image_Max : constant := 1024; |
| 280 |
UI_Image_Buffer : String (1 .. UI_Image_Max); |
| 281 |
UI_Image_Length : Natural; |
| 282 |
-- Buffer used for UI_Image as described below |
| 283 |
|
| 284 |
procedure UI_Image (Input : Uint; Format : UI_Format := Auto); |
| 285 |
-- Places a representation of Uint, consisting of a possible minus sign, |
| 286 |
-- followed by the value in UI_Image_Buffer. The form of the value is an |
| 287 |
-- integer literal in either decimal (no base) or hexadecimal (base 16) |
| 288 |
-- format. If Hex is True on entry, then hex mode is forced, otherwise |
| 289 |
-- UI_Image makes a guess at which output format is more convenient. The |
| 290 |
-- value must fit in UI_Image_Buffer. The actual length of the result is |
| 291 |
-- returned in UI_Image_Length. If necessary to meet this requirement, the |
| 292 |
-- result is an approximation of the proper value, using an exponential |
| 293 |
-- format. The image of No_Uint is output as a single question mark. |
| 294 |
|
| 295 |
function UI_Image (Input : Uint; Format : UI_Format := Auto) return String; |
| 296 |
-- Functional form, in which the result is returned as a string. This call |
| 297 |
-- also leaves the result in UI_Image_Buffer/Length as described above. |
| 298 |
|
| 299 |
procedure UI_Write (Input : Uint; Format : UI_Format := Auto); |
| 300 |
-- Writes a representation of Uint, consisting of a possible minus sign, |
| 301 |
-- followed by the value to the output file. The form of the value is an |
| 302 |
-- integer literal in either decimal (no base) or hexadecimal (base 16) |
| 303 |
-- format as appropriate. UI_Format shows which format to use. Auto, the |
| 304 |
-- default, asks UI_Write to make a guess at which output format will be |
| 305 |
-- more convenient to read. |
| 306 |
|
| 307 |
procedure pid (Input : Uint); |
| 308 |
pragma Export (Ada, pid); |
| 309 |
-- Writes representation of Uint in decimal with a terminating line |
| 310 |
-- return. This is intended for use from the debugger. |
| 311 |
|
| 312 |
procedure pih (Input : Uint); |
| 313 |
pragma Export (Ada, pih); |
| 314 |
-- Writes representation of Uint in hex with a terminating line return. |
| 315 |
-- This is intended for use from the debugger. |
| 316 |
|
| 317 |
------------------------ |
| 318 |
-- Operator Renamings -- |
| 319 |
------------------------ |
| 320 |
|
| 321 |
function "+" (Left : Valid_Uint; Right : Valid_Uint) return Valid_Uint |
| 322 |
renames UI_Add; |
| 323 |
function "+" (Left : Int; Right : Valid_Uint) return Valid_Uint |
| 324 |
renames UI_Add; |
| 325 |
function "+" (Left : Valid_Uint; Right : Int) return Valid_Uint |
| 326 |
renames UI_Add; |
| 327 |
|
| 328 |
function "/" (Left : Valid_Uint; Right : Nonzero_Uint) return Valid_Uint |
| 329 |
renames UI_Div; |
| 330 |
function "/" (Left : Int; Right : Nonzero_Uint) return Valid_Uint |
| 331 |
renames UI_Div; |
| 332 |
function "/" (Left : Valid_Uint; Right : Nonzero_Int) return Valid_Uint |
| 333 |
renames UI_Div; |
| 334 |
|
| 335 |
function "*" (Left : Valid_Uint; Right : Valid_Uint) return Valid_Uint |
| 336 |
renames UI_Mul; |
| 337 |
function "*" (Left : Int; Right : Valid_Uint) return Valid_Uint |
| 338 |
renames UI_Mul; |
| 339 |
function "*" (Left : Valid_Uint; Right : Int) return Valid_Uint |
| 340 |
renames UI_Mul; |
| 341 |
|
| 342 |
function "-" (Left : Valid_Uint; Right : Valid_Uint) return Valid_Uint |
| 343 |
renames UI_Sub; |
| 344 |
function "-" (Left : Int; Right : Valid_Uint) return Valid_Uint |
| 345 |
renames UI_Sub; |
| 346 |
function "-" (Left : Valid_Uint; Right : Int) return Valid_Uint |
| 347 |
renames UI_Sub; |
| 348 |
|
| 349 |
function "**" (Left : Valid_Uint; Right : Unat) return Valid_Uint |
| 350 |
renames UI_Expon; |
| 351 |
function "**" (Left : Valid_Uint; Right : Nat) return Valid_Uint |
| 352 |
renames UI_Expon; |
| 353 |
function "**" (Left : Int; Right : Unat) return Valid_Uint |
| 354 |
renames UI_Expon; |
| 355 |
function "**" (Left : Int; Right : Nat) return Valid_Uint |
| 356 |
renames UI_Expon; |
| 357 |
|
| 358 |
function "abs" (Real : Valid_Uint) return Unat |
| 359 |
renames UI_Abs; |
| 360 |
|
| 361 |
function "mod" (Left : Valid_Uint; Right : Nonzero_Uint) return Valid_Uint |
| 362 |
renames UI_Mod; |
| 363 |
function "mod" (Left : Int; Right : Nonzero_Uint) return Valid_Uint |
| 364 |
renames UI_Mod; |
| 365 |
function "mod" (Left : Valid_Uint; Right : Nonzero_Int) return Valid_Uint |
| 366 |
renames UI_Mod; |
| 367 |
|
| 368 |
function "rem" (Left : Valid_Uint; Right : Nonzero_Uint) return Valid_Uint |
| 369 |
renames UI_Rem; |
| 370 |
function "rem" (Left : Int; Right : Nonzero_Uint) return Valid_Uint |
| 371 |
renames UI_Rem; |
| 372 |
function "rem" (Left : Valid_Uint; Right : Nonzero_Int) return Valid_Uint |
| 373 |
renames UI_Rem; |
| 374 |
|
| 375 |
function "-" (Real : Valid_Uint) return Valid_Uint |
| 376 |
renames UI_Negate; |
| 377 |
|
| 378 |
function "=" (Left : Valid_Uint; Right : Valid_Uint) return Boolean |
| 379 |
renames UI_Eq; |
| 380 |
function "=" (Left : Int; Right : Valid_Uint) return Boolean |
| 381 |
renames UI_Eq; |
| 382 |
function "=" (Left : Valid_Uint; Right : Int) return Boolean |
| 383 |
renames UI_Eq; |
| 384 |
|
| 385 |
function ">=" (Left : Valid_Uint; Right : Valid_Uint) return Boolean |
| 386 |
renames UI_Ge; |
| 387 |
function ">=" (Left : Int; Right : Valid_Uint) return Boolean |
| 388 |
renames UI_Ge; |
| 389 |
function ">=" (Left : Valid_Uint; Right : Int) return Boolean |
| 390 |
renames UI_Ge; |
| 391 |
|
| 392 |
function ">" (Left : Valid_Uint; Right : Valid_Uint) return Boolean |
| 393 |
renames UI_Gt; |
| 394 |
function ">" (Left : Int; Right : Valid_Uint) return Boolean |
| 395 |
renames UI_Gt; |
| 396 |
function ">" (Left : Valid_Uint; Right : Int) return Boolean |
| 397 |
renames UI_Gt; |
| 398 |
|
| 399 |
function "<=" (Left : Valid_Uint; Right : Valid_Uint) return Boolean |
| 400 |
renames UI_Le; |
| 401 |
function "<=" (Left : Int; Right : Valid_Uint) return Boolean |
| 402 |
renames UI_Le; |
| 403 |
function "<=" (Left : Valid_Uint; Right : Int) return Boolean |
| 404 |
renames UI_Le; |
| 405 |
|
| 406 |
function "<" (Left : Valid_Uint; Right : Valid_Uint) return Boolean |
| 407 |
renames UI_Lt; |
| 408 |
function "<" (Left : Int; Right : Valid_Uint) return Boolean |
| 409 |
renames UI_Lt; |
| 410 |
function "<" (Left : Valid_Uint; Right : Int) return Boolean |
| 411 |
renames UI_Lt; |
| 412 |
|
| 413 |
----------------------------- |
| 414 |
-- Mark/Release Processing -- |
| 415 |
----------------------------- |
| 416 |
|
| 417 |
-- The space used by Uint data is not automatically reclaimed. However, a |
| 418 |
-- mark-release regime is implemented which allows storage to be released |
| 419 |
-- back to a previously noted mark. This is used for example when doing |
| 420 |
-- comparisons, where only intermediate results get stored that do not |
| 421 |
-- need to be saved for future use. |
| 422 |
|
| 423 |
type Save_Mark is private; |
| 424 |
|
| 425 |
function Mark return Save_Mark; |
| 426 |
-- Note mark point for future release |
| 427 |
|
| 428 |
procedure Release (M : Save_Mark); |
| 429 |
-- Release storage allocated since mark was noted |
| 430 |
|
| 431 |
procedure Release_And_Save (M : Save_Mark; UI : in out Valid_Uint); |
| 432 |
-- Like Release, except that the given Uint value (which is typically among |
| 433 |
-- the data being released) is recopied after the release, so that it is |
| 434 |
-- the most recent item, and UI is updated to point to its copied location. |
| 435 |
|
| 436 |
procedure Release_And_Save (M : Save_Mark; UI1, UI2 : in out Valid_Uint); |
| 437 |
-- Like Release, except that the given Uint values (which are typically |
| 438 |
-- among the data being released) are recopied after the release, so that |
| 439 |
-- they are the most recent items, and UI1 and UI2 are updated if necessary |
| 440 |
-- to point to the copied locations. This routine is careful to do things |
| 441 |
-- in the right order, so that the values do not clobber one another. |
| 442 |
|
| 443 |
----------------------------------- |
| 444 |
-- Representation of Uint Values -- |
| 445 |
----------------------------------- |
| 446 |
|
| 447 |
private |
| 448 |
|
| 449 |
type Uint is new Int range Uint_Low_Bound .. Uint_High_Bound; |
| 450 |
for Uint'Size use 32; |
| 451 |
|
| 452 |
No_Uint : constant Uint := Uint (Uint_Low_Bound); |
| 453 |
|
| 454 |
-- Uint values are represented as multiple precision integers stored in |
| 455 |
-- a multi-digit format using Base as the base. This value is chosen so |
| 456 |
-- that the product Base*Base is within the range of allowed Int values. |
| 457 |
|
| 458 |
-- Base is defined to allow efficient execution of the primitive operations |
| 459 |
-- (a0, b0, c0) defined in the section "The Classical Algorithms" |
| 460 |
-- (sec. 4.3.1) of Donald Knuth's "The Art of Computer Programming", |
| 461 |
-- Vol. 2. These algorithms are used in this package. In particular, |
| 462 |
-- the product of two single digits in this base fits in a 32-bit integer. |
| 463 |
|
| 464 |
Base_Bits : constant := 15; |
| 465 |
-- Number of bits in base value |
| 466 |
|
| 467 |
Base : constant Int := 2**Base_Bits; |
| 468 |
|
| 469 |
-- Values in the range -(Base-1) .. Max_Direct are encoded directly as |
| 470 |
-- Uint values by adding a bias value. The value of Max_Direct is chosen |
| 471 |
-- so that a directly represented number always fits in two digits when |
| 472 |
-- represented in base format. |
| 473 |
|
| 474 |
Min_Direct : constant Int := -(Base - 1); |
| 475 |
Max_Direct : constant Int := (Base - 1) * (Base - 1); |
| 476 |
|
| 477 |
-- The following values define the bias used to store Uint values which |
| 478 |
-- are in this range, as well as the biased values for the first and last |
| 479 |
-- values in this range. We use a new derived type for these constants to |
| 480 |
-- avoid accidental use of Uint arithmetic on these values, which is never |
| 481 |
-- correct. |
| 482 |
|
| 483 |
type Ctrl is new Int; |
| 484 |
|
| 485 |
Uint_Direct_Bias : constant Ctrl := Ctrl (Uint_Low_Bound) + Ctrl (Base); |
| 486 |
Uint_Direct_First : constant Ctrl := Uint_Direct_Bias + Ctrl (Min_Direct); |
| 487 |
Uint_Direct_Last : constant Ctrl := Uint_Direct_Bias + Ctrl (Max_Direct); |
| 488 |
|
| 489 |
Uint_0 : constant Uint := Uint (Uint_Direct_Bias + 0); |
| 490 |
Uint_1 : constant Uint := Uint (Uint_Direct_Bias + 1); |
| 491 |
Uint_2 : constant Uint := Uint (Uint_Direct_Bias + 2); |
| 492 |
Uint_3 : constant Uint := Uint (Uint_Direct_Bias + 3); |
| 493 |
Uint_4 : constant Uint := Uint (Uint_Direct_Bias + 4); |
| 494 |
Uint_5 : constant Uint := Uint (Uint_Direct_Bias + 5); |
| 495 |
Uint_6 : constant Uint := Uint (Uint_Direct_Bias + 6); |
| 496 |
Uint_7 : constant Uint := Uint (Uint_Direct_Bias + 7); |
| 497 |
Uint_8 : constant Uint := Uint (Uint_Direct_Bias + 8); |
| 498 |
Uint_9 : constant Uint := Uint (Uint_Direct_Bias + 9); |
| 499 |
Uint_10 : constant Uint := Uint (Uint_Direct_Bias + 10); |
| 500 |
Uint_11 : constant Uint := Uint (Uint_Direct_Bias + 11); |
| 501 |
Uint_12 : constant Uint := Uint (Uint_Direct_Bias + 12); |
| 502 |
Uint_13 : constant Uint := Uint (Uint_Direct_Bias + 13); |
| 503 |
Uint_14 : constant Uint := Uint (Uint_Direct_Bias + 14); |
| 504 |
Uint_15 : constant Uint := Uint (Uint_Direct_Bias + 15); |
| 505 |
Uint_16 : constant Uint := Uint (Uint_Direct_Bias + 16); |
| 506 |
Uint_24 : constant Uint := Uint (Uint_Direct_Bias + 24); |
| 507 |
Uint_31 : constant Uint := Uint (Uint_Direct_Bias + 31); |
| 508 |
Uint_32 : constant Uint := Uint (Uint_Direct_Bias + 32); |
| 509 |
Uint_63 : constant Uint := Uint (Uint_Direct_Bias + 63); |
| 510 |
Uint_64 : constant Uint := Uint (Uint_Direct_Bias + 64); |
| 511 |
Uint_80 : constant Uint := Uint (Uint_Direct_Bias + 80); |
| 512 |
Uint_127 : constant Uint := Uint (Uint_Direct_Bias + 127); |
| 513 |
Uint_128 : constant Uint := Uint (Uint_Direct_Bias + 128); |
| 514 |
Uint_256 : constant Uint := Uint (Uint_Direct_Bias + 256); |
| 515 |
|
| 516 |
Uint_Minus_1 : constant Uint := Uint (Uint_Direct_Bias - 1); |
| 517 |
Uint_Minus_2 : constant Uint := Uint (Uint_Direct_Bias - 2); |
| 518 |
Uint_Minus_3 : constant Uint := Uint (Uint_Direct_Bias - 3); |
| 519 |
Uint_Minus_4 : constant Uint := Uint (Uint_Direct_Bias - 4); |
| 520 |
Uint_Minus_5 : constant Uint := Uint (Uint_Direct_Bias - 5); |
| 521 |
Uint_Minus_6 : constant Uint := Uint (Uint_Direct_Bias - 6); |
| 522 |
Uint_Minus_7 : constant Uint := Uint (Uint_Direct_Bias - 7); |
| 523 |
Uint_Minus_8 : constant Uint := Uint (Uint_Direct_Bias - 8); |
| 524 |
Uint_Minus_9 : constant Uint := Uint (Uint_Direct_Bias - 9); |
| 525 |
Uint_Minus_12 : constant Uint := Uint (Uint_Direct_Bias - 12); |
| 526 |
Uint_Minus_18 : constant Uint := Uint (Uint_Direct_Bias - 18); |
| 527 |
Uint_Minus_31 : constant Uint := Uint (Uint_Direct_Bias - 31); |
| 528 |
Uint_Minus_36 : constant Uint := Uint (Uint_Direct_Bias - 36); |
| 529 |
Uint_Minus_63 : constant Uint := Uint (Uint_Direct_Bias - 63); |
| 530 |
Uint_Minus_76 : constant Uint := Uint (Uint_Direct_Bias - 76); |
| 531 |
Uint_Minus_80 : constant Uint := Uint (Uint_Direct_Bias - 80); |
| 532 |
Uint_Minus_127 : constant Uint := Uint (Uint_Direct_Bias - 127); |
| 533 |
Uint_Minus_128 : constant Uint := Uint (Uint_Direct_Bias - 128); |
| 534 |
|
| 535 |
Uint_Max_Simple_Mul : constant := Uint_Direct_Bias + 2**15; |
| 536 |
-- If two values are directly represented and less than or equal to this |
| 537 |
-- value, then we know the product fits in a 32-bit integer. This allows |
| 538 |
-- UI_Mul to efficiently compute the product in this case. |
| 539 |
|
| 540 |
type Save_Mark is record |
| 541 |
Save_Uint : Valid_Uint; |
| 542 |
Save_Udigit : Int; |
| 543 |
end record; |
| 544 |
|
| 545 |
-- Values outside the range that is represented directly are stored using |
| 546 |
-- two tables. The secondary table Udigits contains sequences of Int values |
| 547 |
-- consisting of the digits of the number in a radix Base system. The |
| 548 |
-- digits are stored from most significant to least significant with the |
| 549 |
-- first digit only carrying the sign. |
| 550 |
|
| 551 |
-- There is one entry in the primary Uints table for each distinct Uint |
| 552 |
-- value. This table entry contains the length (number of digits) and |
| 553 |
-- a starting offset of the value in the Udigits table. |
| 554 |
|
| 555 |
Uint_First_Entry : constant Uint := Uint (Uint_Table_Start); |
| 556 |
|
| 557 |
-- Some subprograms defined in this package manipulate the Udigits table |
| 558 |
-- directly, while for others it is more convenient to work with locally |
| 559 |
-- defined arrays of the digits of the Universal Integers. The type |
| 560 |
-- UI_Vector is declared in the package body for this purpose and some |
| 561 |
-- internal subprograms used for converting from one to the other are |
| 562 |
-- defined. |
| 563 |
|
| 564 |
type Uint_Entry is record |
| 565 |
Length : aliased Pos; |
| 566 |
-- Length of entry in Udigits table in digits (i.e. in words) |
| 567 |
|
| 568 |
Loc : aliased Int; |
| 569 |
-- Starting location in Udigits table of this Uint value |
| 570 |
end record; |
| 571 |
|
| 572 |
package Uints is new Table.Table ( |
| 573 |
Table_Component_Type => Uint_Entry, |
| 574 |
Table_Index_Type => Uint'Base, |
| 575 |
Table_Low_Bound => Uint_First_Entry, |
| 576 |
Table_Initial => Alloc.Uints_Initial, |
| 577 |
Table_Increment => Alloc.Uints_Increment, |
| 578 |
Table_Name => "Uints"); |
| 579 |
|
| 580 |
package Udigits is new Table.Table ( |
| 581 |
Table_Component_Type => Int, |
| 582 |
Table_Index_Type => Int, |
| 583 |
Table_Low_Bound => 0, |
| 584 |
Table_Initial => Alloc.Udigits_Initial, |
| 585 |
Table_Increment => Alloc.Udigits_Increment, |
| 586 |
Table_Name => "Udigits"); |
| 587 |
|
| 588 |
-- Note: the reason these tables are defined here in the private part of |
| 589 |
-- the spec, rather than in the body, is that they are referenced directly |
| 590 |
-- by gigi. |
| 591 |
|
| 592 |
end Uintp; |