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