| 1 |
------------------------------------------------------------------------------ |
| 2 |
-- -- |
| 3 |
-- GNAT RUN-TIME COMPONENTS -- |
| 4 |
-- -- |
| 5 |
-- S Y S T E M . A R I T H _ 3 2 -- |
| 6 |
-- -- |
| 7 |
-- B o d y -- |
| 8 |
-- -- |
| 9 |
-- Copyright (C) 2020-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. -- |
| 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 |
-- Preconditions, postconditions, ghost code, loop invariants and assertions |
| 33 |
-- in this unit are meant for analysis only, not for run-time checking, as it |
| 34 |
-- would be too costly otherwise. This is enforced by setting the assertion |
| 35 |
-- policy to Ignore. |
| 36 |
|
| 37 |
pragma Assertion_Policy (Pre => Ignore, |
| 38 |
Post => Ignore, |
| 39 |
Ghost => Ignore, |
| 40 |
Loop_Invariant => Ignore, |
| 41 |
Assert => Ignore); |
| 42 |
|
| 43 |
with Ada.Numerics.Big_Numbers.Big_Integers_Ghost; |
| 44 |
use Ada.Numerics.Big_Numbers.Big_Integers_Ghost; |
| 45 |
with Ada.Unchecked_Conversion; |
| 46 |
|
| 47 |
package body System.Arith_32 |
| 48 |
with SPARK_Mode |
| 49 |
is |
| 50 |
|
| 51 |
pragma Suppress (Overflow_Check); |
| 52 |
pragma Suppress (Range_Check); |
| 53 |
|
| 54 |
subtype Uns32 is Interfaces.Unsigned_32; |
| 55 |
subtype Uns64 is Interfaces.Unsigned_64; |
| 56 |
|
| 57 |
use Interfaces; |
| 58 |
|
| 59 |
function To_Int is new Ada.Unchecked_Conversion (Uns32, Int32); |
| 60 |
|
| 61 |
package Unsigned_Conversion is new Unsigned_Conversions (Int => Uns32); |
| 62 |
|
| 63 |
function Big (Arg : Uns32) return Big_Integer is |
| 64 |
(Unsigned_Conversion.To_Big_Integer (Arg)) |
| 65 |
with Ghost; |
| 66 |
|
| 67 |
package Unsigned_Conversion_64 is new Unsigned_Conversions (Int => Uns64); |
| 68 |
|
| 69 |
function Big (Arg : Uns64) return Big_Integer is |
| 70 |
(Unsigned_Conversion_64.To_Big_Integer (Arg)) |
| 71 |
with Ghost; |
| 72 |
|
| 73 |
pragma Warnings |
| 74 |
(Off, "non-preelaborable call not allowed in preelaborated unit", |
| 75 |
Reason => "Ghost code is not compiled"); |
| 76 |
Big_0 : constant Big_Integer := |
| 77 |
Big (Uns32'(0)) |
| 78 |
with Ghost; |
| 79 |
Big_2xx32 : constant Big_Integer := |
| 80 |
Big (Uns32'(2 ** 32 - 1)) + 1 |
| 81 |
with Ghost; |
| 82 |
Big_2xx64 : constant Big_Integer := |
| 83 |
Big (Uns64'(2 ** 64 - 1)) + 1 |
| 84 |
with Ghost; |
| 85 |
pragma Warnings |
| 86 |
(On, "non-preelaborable call not allowed in preelaborated unit"); |
| 87 |
|
| 88 |
----------------------- |
| 89 |
-- Local Subprograms -- |
| 90 |
----------------------- |
| 91 |
|
| 92 |
function "abs" (X : Int32) return Uns32 is |
| 93 |
(if X = Int32'First |
| 94 |
then Uns32'(2**31) |
| 95 |
else Uns32 (Int32'(abs X))); |
| 96 |
-- Convert absolute value of X to unsigned. Note that we can't just use |
| 97 |
-- the expression of the Else since it overflows for X = Int32'First. |
| 98 |
|
| 99 |
function Lo (A : Uns64) return Uns32 is (Uns32 (A and (2 ** 32 - 1))); |
| 100 |
-- Low order half of 64-bit value |
| 101 |
|
| 102 |
function Hi (A : Uns64) return Uns32 is (Uns32 (Shift_Right (A, 32))); |
| 103 |
-- High order half of 64-bit value |
| 104 |
|
| 105 |
function To_Neg_Int (A : Uns32) return Int32 |
| 106 |
with |
| 107 |
Pre => In_Int32_Range (-Big (A)), |
| 108 |
Post => Big (To_Neg_Int'Result) = -Big (A); |
| 109 |
-- Convert to negative integer equivalent. If the input is in the range |
| 110 |
-- 0 .. 2**31, then the corresponding nonpositive signed integer (obtained |
| 111 |
-- by negating the given value) is returned, otherwise constraint error is |
| 112 |
-- raised. |
| 113 |
|
| 114 |
function To_Pos_Int (A : Uns32) return Int32 |
| 115 |
with |
| 116 |
Pre => In_Int32_Range (Big (A)), |
| 117 |
Post => Big (To_Pos_Int'Result) = Big (A); |
| 118 |
-- Convert to positive integer equivalent. If the input is in the range |
| 119 |
-- 0 .. 2**31 - 1, then the corresponding nonnegative signed integer is |
| 120 |
-- returned, otherwise constraint error is raised. |
| 121 |
|
| 122 |
procedure Raise_Error with |
| 123 |
Always_Terminates, |
| 124 |
Exceptional_Cases => (Constraint_Error => True); |
| 125 |
pragma No_Return (Raise_Error); |
| 126 |
-- Raise constraint error with appropriate message |
| 127 |
|
| 128 |
------------------ |
| 129 |
-- Local Lemmas -- |
| 130 |
------------------ |
| 131 |
|
| 132 |
procedure Lemma_Abs_Commutation (X : Int32) |
| 133 |
with |
| 134 |
Ghost, |
| 135 |
Post => abs Big (X) = Big (Uns32'(abs X)); |
| 136 |
|
| 137 |
procedure Lemma_Abs_Div_Commutation (X, Y : Big_Integer) |
| 138 |
with |
| 139 |
Ghost, |
| 140 |
Pre => Y /= 0, |
| 141 |
Post => abs (X / Y) = abs X / abs Y; |
| 142 |
|
| 143 |
procedure Lemma_Abs_Mult_Commutation (X, Y : Big_Integer) |
| 144 |
with |
| 145 |
Ghost, |
| 146 |
Post => abs (X * Y) = abs X * abs Y; |
| 147 |
|
| 148 |
procedure Lemma_Abs_Rem_Commutation (X, Y : Big_Integer) |
| 149 |
with |
| 150 |
Ghost, |
| 151 |
Pre => Y /= 0, |
| 152 |
Post => abs (X rem Y) = (abs X) rem (abs Y); |
| 153 |
|
| 154 |
procedure Lemma_Div_Commutation (X, Y : Uns64) |
| 155 |
with |
| 156 |
Ghost, |
| 157 |
Pre => Y /= 0, |
| 158 |
Post => Big (X) / Big (Y) = Big (X / Y); |
| 159 |
|
| 160 |
procedure Lemma_Div_Ge (X, Y, Z : Big_Integer) |
| 161 |
with |
| 162 |
Ghost, |
| 163 |
Pre => Z > 0 and then X >= Y * Z, |
| 164 |
Post => X / Z >= Y; |
| 165 |
|
| 166 |
procedure Lemma_Ge_Commutation (A, B : Uns32) |
| 167 |
with |
| 168 |
Ghost, |
| 169 |
Pre => A >= B, |
| 170 |
Post => Big (A) >= Big (B); |
| 171 |
|
| 172 |
procedure Lemma_Hi_Lo (Xu : Uns64; Xhi, Xlo : Uns32) |
| 173 |
with |
| 174 |
Ghost, |
| 175 |
Pre => Xhi = Hi (Xu) and Xlo = Lo (Xu), |
| 176 |
Post => Big (Xu) = Big_2xx32 * Big (Xhi) + Big (Xlo); |
| 177 |
|
| 178 |
procedure Lemma_Mult_Commutation (X, Y, Z : Uns64) |
| 179 |
with |
| 180 |
Ghost, |
| 181 |
Pre => Big (X) * Big (Y) < Big_2xx64 and then Z = X * Y, |
| 182 |
Post => Big (X) * Big (Y) = Big (Z); |
| 183 |
|
| 184 |
procedure Lemma_Mult_Non_Negative (X, Y : Big_Integer) |
| 185 |
with |
| 186 |
Ghost, |
| 187 |
Pre => (X >= Big_0 and then Y >= Big_0) |
| 188 |
or else (X <= Big_0 and then Y <= Big_0), |
| 189 |
Post => X * Y >= Big_0; |
| 190 |
|
| 191 |
procedure Lemma_Mult_Non_Positive (X, Y : Big_Integer) |
| 192 |
with |
| 193 |
Ghost, |
| 194 |
Pre => (X <= Big_0 and then Y >= Big_0) |
| 195 |
or else (X >= Big_0 and then Y <= Big_0), |
| 196 |
Post => X * Y <= Big_0; |
| 197 |
|
| 198 |
procedure Lemma_Neg_Rem (X, Y : Big_Integer) |
| 199 |
with |
| 200 |
Ghost, |
| 201 |
Pre => Y /= 0, |
| 202 |
Post => X rem Y = X rem (-Y); |
| 203 |
|
| 204 |
procedure Lemma_Not_In_Range_Big2xx32 |
| 205 |
with |
| 206 |
Post => not In_Int32_Range (Big_2xx32) |
| 207 |
and then not In_Int32_Range (-Big_2xx32); |
| 208 |
|
| 209 |
procedure Lemma_Rem_Commutation (X, Y : Uns64) |
| 210 |
with |
| 211 |
Ghost, |
| 212 |
Pre => Y /= 0, |
| 213 |
Post => Big (X) rem Big (Y) = Big (X rem Y); |
| 214 |
|
| 215 |
----------------------------- |
| 216 |
-- Local lemma null bodies -- |
| 217 |
----------------------------- |
| 218 |
|
| 219 |
procedure Lemma_Abs_Commutation (X : Int32) is null; |
| 220 |
procedure Lemma_Abs_Div_Commutation (X, Y : Big_Integer) is null; |
| 221 |
procedure Lemma_Abs_Mult_Commutation (X, Y : Big_Integer) is null; |
| 222 |
procedure Lemma_Div_Commutation (X, Y : Uns64) is null; |
| 223 |
procedure Lemma_Div_Ge (X, Y, Z : Big_Integer) is null; |
| 224 |
procedure Lemma_Ge_Commutation (A, B : Uns32) is null; |
| 225 |
procedure Lemma_Mult_Commutation (X, Y, Z : Uns64) is null; |
| 226 |
procedure Lemma_Mult_Non_Negative (X, Y : Big_Integer) is null; |
| 227 |
procedure Lemma_Mult_Non_Positive (X, Y : Big_Integer) is null; |
| 228 |
procedure Lemma_Neg_Rem (X, Y : Big_Integer) is null; |
| 229 |
procedure Lemma_Not_In_Range_Big2xx32 is null; |
| 230 |
procedure Lemma_Rem_Commutation (X, Y : Uns64) is null; |
| 231 |
|
| 232 |
------------------------------- |
| 233 |
-- Lemma_Abs_Rem_Commutation -- |
| 234 |
------------------------------- |
| 235 |
|
| 236 |
procedure Lemma_Abs_Rem_Commutation (X, Y : Big_Integer) is |
| 237 |
begin |
| 238 |
if Y < 0 then |
| 239 |
Lemma_Neg_Rem (X, Y); |
| 240 |
if X < 0 then |
| 241 |
pragma Assert (X rem Y = -((-X) rem (-Y))); |
| 242 |
pragma Assert (abs (X rem Y) = (abs X) rem (abs Y)); |
| 243 |
else |
| 244 |
pragma Assert (abs (X rem Y) = (abs X) rem (abs Y)); |
| 245 |
end if; |
| 246 |
end if; |
| 247 |
end Lemma_Abs_Rem_Commutation; |
| 248 |
|
| 249 |
----------------- |
| 250 |
-- Lemma_Hi_Lo -- |
| 251 |
----------------- |
| 252 |
|
| 253 |
procedure Lemma_Hi_Lo (Xu : Uns64; Xhi, Xlo : Uns32) is |
| 254 |
begin |
| 255 |
pragma Assert (Uns64 (Xhi) = Xu / Uns64'(2 ** 32)); |
| 256 |
pragma Assert (Uns64 (Xlo) = Xu mod 2 ** 32); |
| 257 |
end Lemma_Hi_Lo; |
| 258 |
|
| 259 |
----------------- |
| 260 |
-- Raise_Error -- |
| 261 |
----------------- |
| 262 |
|
| 263 |
procedure Raise_Error is |
| 264 |
begin |
| 265 |
raise Constraint_Error with "32-bit arithmetic overflow"; |
| 266 |
pragma Annotate |
| 267 |
(GNATprove, Intentional, "exception might be raised", |
| 268 |
"Procedure Raise_Error is called to signal input errors"); |
| 269 |
end Raise_Error; |
| 270 |
|
| 271 |
------------------- |
| 272 |
-- Scaled_Divide -- |
| 273 |
------------------- |
| 274 |
|
| 275 |
procedure Scaled_Divide32 |
| 276 |
(X, Y, Z : Int32; |
| 277 |
Q, R : out Int32; |
| 278 |
Round : Boolean) |
| 279 |
is |
| 280 |
Xu : constant Uns32 := abs X; |
| 281 |
Yu : constant Uns32 := abs Y; |
| 282 |
Zu : constant Uns32 := abs Z; |
| 283 |
|
| 284 |
D : Uns64; |
| 285 |
-- The dividend |
| 286 |
|
| 287 |
Qu : Uns32; |
| 288 |
Ru : Uns32; |
| 289 |
-- Unsigned quotient and remainder |
| 290 |
|
| 291 |
-- Local ghost variables |
| 292 |
|
| 293 |
Mult : constant Big_Integer := abs (Big (X) * Big (Y)) with Ghost; |
| 294 |
Quot : Big_Integer with Ghost; |
| 295 |
Big_R : Big_Integer with Ghost; |
| 296 |
Big_Q : Big_Integer with Ghost; |
| 297 |
|
| 298 |
-- Local lemmas |
| 299 |
|
| 300 |
procedure Prove_Negative_Dividend |
| 301 |
with |
| 302 |
Ghost, |
| 303 |
Pre => Z /= 0 |
| 304 |
and then ((X >= 0 and Y < 0) or (X < 0 and Y >= 0)) |
| 305 |
and then Big_Q = |
| 306 |
(if Round then Round_Quotient (Big (X) * Big (Y), Big (Z), |
| 307 |
Big (X) * Big (Y) / Big (Z), |
| 308 |
Big (X) * Big (Y) rem Big (Z)) |
| 309 |
else Big (X) * Big (Y) / Big (Z)), |
| 310 |
Post => |
| 311 |
(if Z > 0 then Big_Q <= Big_0 else Big_Q >= Big_0); |
| 312 |
-- Proves the sign of rounded quotient when dividend is non-positive |
| 313 |
|
| 314 |
procedure Prove_Overflow |
| 315 |
with |
| 316 |
Ghost, |
| 317 |
Pre => Z /= 0 and then Mult >= Big_2xx32 * Big (Uns32'(abs Z)), |
| 318 |
Post => not In_Int32_Range (Big (X) * Big (Y) / Big (Z)) |
| 319 |
and then not In_Int32_Range |
| 320 |
(Round_Quotient (Big (X) * Big (Y), Big (Z), |
| 321 |
Big (X) * Big (Y) / Big (Z), |
| 322 |
Big (X) * Big (Y) rem Big (Z))); |
| 323 |
-- Proves overflow case |
| 324 |
|
| 325 |
procedure Prove_Positive_Dividend |
| 326 |
with |
| 327 |
Ghost, |
| 328 |
Pre => Z /= 0 |
| 329 |
and then ((X >= 0 and Y >= 0) or (X < 0 and Y < 0)) |
| 330 |
and then Big_Q = |
| 331 |
(if Round then Round_Quotient (Big (X) * Big (Y), Big (Z), |
| 332 |
Big (X) * Big (Y) / Big (Z), |
| 333 |
Big (X) * Big (Y) rem Big (Z)) |
| 334 |
else Big (X) * Big (Y) / Big (Z)), |
| 335 |
Post => |
| 336 |
(if Z > 0 then Big_Q >= Big_0 else Big_Q <= Big_0); |
| 337 |
-- Proves the sign of rounded quotient when dividend is non-negative |
| 338 |
|
| 339 |
procedure Prove_Rounding_Case |
| 340 |
with |
| 341 |
Ghost, |
| 342 |
Pre => Z /= 0 |
| 343 |
and then Quot = Big (X) * Big (Y) / Big (Z) |
| 344 |
and then Big_R = Big (X) * Big (Y) rem Big (Z) |
| 345 |
and then Big_Q = |
| 346 |
Round_Quotient (Big (X) * Big (Y), Big (Z), Quot, Big_R) |
| 347 |
and then Big (Ru) = abs Big_R |
| 348 |
and then Big (Zu) = Big (Uns32'(abs Z)), |
| 349 |
Post => abs Big_Q = |
| 350 |
(if Ru > (Zu - Uns32'(1)) / Uns32'(2) |
| 351 |
then abs Quot + 1 |
| 352 |
else abs Quot); |
| 353 |
-- Proves correctness of the rounding of the unsigned quotient |
| 354 |
|
| 355 |
procedure Prove_Sign_R |
| 356 |
with |
| 357 |
Ghost, |
| 358 |
Pre => Z /= 0 and then Big_R = Big (X) * Big (Y) rem Big (Z), |
| 359 |
Post => In_Int32_Range (Big_R); |
| 360 |
|
| 361 |
procedure Prove_Signs |
| 362 |
with |
| 363 |
Ghost, |
| 364 |
Pre => Z /= 0 |
| 365 |
and then Quot = Big (X) * Big (Y) / Big (Z) |
| 366 |
and then Big_R = Big (X) * Big (Y) rem Big (Z) |
| 367 |
and then Big_Q = |
| 368 |
(if Round then |
| 369 |
Round_Quotient (Big (X) * Big (Y), Big (Z), Quot, Big_R) |
| 370 |
else Quot) |
| 371 |
and then Big (Ru) = abs Big_R |
| 372 |
and then Big (Qu) = abs Big_Q |
| 373 |
and then In_Int32_Range (Big_Q) |
| 374 |
and then In_Int32_Range (Big_R) |
| 375 |
and then R = |
| 376 |
(if (X >= 0) = (Y >= 0) then To_Pos_Int (Ru) else To_Neg_Int (Ru)) |
| 377 |
and then Q = |
| 378 |
(if ((X >= 0) = (Y >= 0)) = (Z >= 0) then To_Pos_Int (Qu) |
| 379 |
else To_Neg_Int (Qu)), -- need to ensure To_Pos_Int precondition |
| 380 |
Post => Big (R) = Big_R and then Big (Q) = Big_Q; |
| 381 |
-- Proves final signs match the intended result after the unsigned |
| 382 |
-- division is done. |
| 383 |
|
| 384 |
----------------------------- |
| 385 |
-- Prove_Negative_Dividend -- |
| 386 |
----------------------------- |
| 387 |
|
| 388 |
procedure Prove_Negative_Dividend is |
| 389 |
begin |
| 390 |
Lemma_Mult_Non_Positive (Big (X), Big (Y)); |
| 391 |
end Prove_Negative_Dividend; |
| 392 |
|
| 393 |
-------------------- |
| 394 |
-- Prove_Overflow -- |
| 395 |
-------------------- |
| 396 |
|
| 397 |
procedure Prove_Overflow is |
| 398 |
begin |
| 399 |
Lemma_Div_Ge (Mult, Big_2xx32, Big (Uns32'(abs Z))); |
| 400 |
Lemma_Abs_Commutation (Z); |
| 401 |
Lemma_Abs_Div_Commutation (Big (X) * Big (Y), Big (Z)); |
| 402 |
end Prove_Overflow; |
| 403 |
|
| 404 |
----------------------------- |
| 405 |
-- Prove_Positive_Dividend -- |
| 406 |
----------------------------- |
| 407 |
|
| 408 |
procedure Prove_Positive_Dividend is |
| 409 |
begin |
| 410 |
Lemma_Mult_Non_Negative (Big (X), Big (Y)); |
| 411 |
end Prove_Positive_Dividend; |
| 412 |
|
| 413 |
------------------------- |
| 414 |
-- Prove_Rounding_Case -- |
| 415 |
------------------------- |
| 416 |
|
| 417 |
procedure Prove_Rounding_Case is |
| 418 |
begin |
| 419 |
if Same_Sign (Big (X) * Big (Y), Big (Z)) then |
| 420 |
pragma Assert |
| 421 |
(abs Big_Q = |
| 422 |
(if Ru > (Zu - Uns32'(1)) / Uns32'(2) |
| 423 |
then abs Quot + 1 |
| 424 |
else abs Quot)); |
| 425 |
end if; |
| 426 |
end Prove_Rounding_Case; |
| 427 |
|
| 428 |
------------------ |
| 429 |
-- Prove_Sign_R -- |
| 430 |
------------------ |
| 431 |
|
| 432 |
procedure Prove_Sign_R is |
| 433 |
begin |
| 434 |
pragma Assert (In_Int32_Range (Big (Z))); |
| 435 |
end Prove_Sign_R; |
| 436 |
|
| 437 |
----------------- |
| 438 |
-- Prove_Signs -- |
| 439 |
----------------- |
| 440 |
|
| 441 |
procedure Prove_Signs is |
| 442 |
begin |
| 443 |
if (X >= 0) = (Y >= 0) then |
| 444 |
pragma Assert (Big (R) = Big_R and then Big (Q) = Big_Q); |
| 445 |
else |
| 446 |
pragma Assert (Big (R) = Big_R and then Big (Q) = Big_Q); |
| 447 |
end if; |
| 448 |
end Prove_Signs; |
| 449 |
|
| 450 |
-- Start of processing for Scaled_Divide32 |
| 451 |
|
| 452 |
begin |
| 453 |
-- First do the 64-bit multiplication |
| 454 |
|
| 455 |
D := Uns64 (Xu) * Uns64 (Yu); |
| 456 |
|
| 457 |
Lemma_Abs_Mult_Commutation (Big (X), Big (Y)); |
| 458 |
pragma Assert (Mult = Big (D)); |
| 459 |
Lemma_Hi_Lo (D, Hi (D), Lo (D)); |
| 460 |
pragma Assert (Mult = Big_2xx32 * Big (Hi (D)) + Big (Lo (D))); |
| 461 |
|
| 462 |
-- If divisor is zero, raise error |
| 463 |
|
| 464 |
if Z = 0 then |
| 465 |
Raise_Error; |
| 466 |
end if; |
| 467 |
|
| 468 |
Quot := Big (X) * Big (Y) / Big (Z); |
| 469 |
Big_R := Big (X) * Big (Y) rem Big (Z); |
| 470 |
if Round then |
| 471 |
Big_Q := Round_Quotient (Big (X) * Big (Y), Big (Z), Quot, Big_R); |
| 472 |
else |
| 473 |
Big_Q := Quot; |
| 474 |
end if; |
| 475 |
|
| 476 |
-- If dividend is too large, raise error |
| 477 |
|
| 478 |
if Hi (D) >= Zu then |
| 479 |
Lemma_Ge_Commutation (Hi (D), Zu); |
| 480 |
pragma Assert (Mult >= Big_2xx32 * Big (Zu)); |
| 481 |
Prove_Overflow; |
| 482 |
Raise_Error; |
| 483 |
end if; |
| 484 |
|
| 485 |
-- Then do the 64-bit division |
| 486 |
|
| 487 |
Qu := Uns32 (D / Uns64 (Zu)); |
| 488 |
Ru := Uns32 (D rem Uns64 (Zu)); |
| 489 |
|
| 490 |
Lemma_Abs_Div_Commutation (Big (X) * Big (Y), Big (Z)); |
| 491 |
Lemma_Abs_Rem_Commutation (Big (X) * Big (Y), Big (Z)); |
| 492 |
Lemma_Abs_Commutation (X); |
| 493 |
Lemma_Abs_Commutation (Y); |
| 494 |
Lemma_Abs_Commutation (Z); |
| 495 |
Lemma_Mult_Commutation (Uns64 (Xu), Uns64 (Yu), D); |
| 496 |
Lemma_Div_Commutation (D, Uns64 (Zu)); |
| 497 |
Lemma_Rem_Commutation (D, Uns64 (Zu)); |
| 498 |
|
| 499 |
pragma Assert (Uns64 (Qu) = D / Uns64 (Zu)); |
| 500 |
pragma Assert (Uns64 (Ru) = D rem Uns64 (Zu)); |
| 501 |
pragma Assert (Big (Ru) = abs Big_R); |
| 502 |
pragma Assert (Big (Qu) = abs Quot); |
| 503 |
pragma Assert (Big (Zu) = Big (Uns32'(abs Z))); |
| 504 |
|
| 505 |
-- Deal with rounding case |
| 506 |
|
| 507 |
if Round then |
| 508 |
Prove_Rounding_Case; |
| 509 |
|
| 510 |
if Ru > (Zu - Uns32'(1)) / Uns32'(2) then |
| 511 |
pragma Assert (abs Big_Q = Big (Qu) + 1); |
| 512 |
|
| 513 |
-- Protect against wrapping around when rounding, by signaling |
| 514 |
-- an overflow when the quotient is too large. |
| 515 |
|
| 516 |
if Qu = Uns32'Last then |
| 517 |
pragma Assert (abs Big_Q = Big_2xx32); |
| 518 |
Lemma_Not_In_Range_Big2xx32; |
| 519 |
Raise_Error; |
| 520 |
end if; |
| 521 |
|
| 522 |
Qu := Qu + Uns32'(1); |
| 523 |
end if; |
| 524 |
end if; |
| 525 |
|
| 526 |
pragma Assert (In_Int32_Range (Big_Q)); |
| 527 |
pragma Assert (Big (Qu) = abs Big_Q); |
| 528 |
pragma Assert (Big (Ru) = abs Big_R); |
| 529 |
Prove_Sign_R; |
| 530 |
|
| 531 |
-- Set final signs (RM 4.5.5(27-30)) |
| 532 |
|
| 533 |
-- Case of dividend (X * Y) sign positive |
| 534 |
|
| 535 |
if (X >= 0 and then Y >= 0) or else (X < 0 and then Y < 0) then |
| 536 |
Prove_Positive_Dividend; |
| 537 |
|
| 538 |
R := To_Pos_Int (Ru); |
| 539 |
Q := (if Z > 0 then To_Pos_Int (Qu) else To_Neg_Int (Qu)); |
| 540 |
|
| 541 |
-- Case of dividend (X * Y) sign negative |
| 542 |
|
| 543 |
else |
| 544 |
Prove_Negative_Dividend; |
| 545 |
|
| 546 |
R := To_Neg_Int (Ru); |
| 547 |
Q := (if Z > 0 then To_Neg_Int (Qu) else To_Pos_Int (Qu)); |
| 548 |
end if; |
| 549 |
|
| 550 |
Prove_Signs; |
| 551 |
end Scaled_Divide32; |
| 552 |
|
| 553 |
---------------- |
| 554 |
-- To_Neg_Int -- |
| 555 |
---------------- |
| 556 |
|
| 557 |
function To_Neg_Int (A : Uns32) return Int32 is |
| 558 |
R : constant Int32 := |
| 559 |
(if A = 2**31 then Int32'First else -To_Int (A)); |
| 560 |
-- Note that we can't just use the expression of the Else, because it |
| 561 |
-- overflows for A = 2**31. |
| 562 |
begin |
| 563 |
if R <= 0 then |
| 564 |
return R; |
| 565 |
else |
| 566 |
Raise_Error; |
| 567 |
end if; |
| 568 |
end To_Neg_Int; |
| 569 |
|
| 570 |
---------------- |
| 571 |
-- To_Pos_Int -- |
| 572 |
---------------- |
| 573 |
|
| 574 |
function To_Pos_Int (A : Uns32) return Int32 is |
| 575 |
R : constant Int32 := To_Int (A); |
| 576 |
begin |
| 577 |
if R >= 0 then |
| 578 |
return R; |
| 579 |
else |
| 580 |
Raise_Error; |
| 581 |
end if; |
| 582 |
end To_Pos_Int; |
| 583 |
|
| 584 |
end System.Arith_32; |