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