Debian Codegraph @

gcc/ada/libgnat/s-aridou.adb

raw
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT RUN-TIME COMPONENTS --
4 -- --
5 -- S Y S T E M . A R I T H _ D O U B L E --
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. --
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 pragma Annotate (Gnatcheck, Exempt_On, "Metrics_LSLOC",
33 "limit exceeded due to proof code");
34
35 with Ada.Unchecked_Conversion;
36 with System.SPARK.Cut_Operations; use System.SPARK.Cut_Operations;
37
38 package body System.Arith_Double
39 with SPARK_Mode
40 is
41 -- Contracts, ghost code, loop invariants and assertions in this unit are
42 -- meant for analysis only, not for run-time checking, as it would be too
43 -- costly otherwise. This is enforced by setting the assertion policy to
44 -- Ignore.
45
46 pragma Assertion_Policy (Pre => Ignore,
47 Post => Ignore,
48 Contract_Cases => Ignore,
49 Ghost => Ignore,
50 Loop_Invariant => Ignore,
51 Assert => Ignore,
52 Assert_And_Cut => Ignore);
53
54 pragma Suppress (Overflow_Check);
55 pragma Suppress (Range_Check);
56
57 pragma Warnings
58 (Off, "statement has no effect",
59 Reason => "Ghost code on dead paths is used for verification only");
60
61 function To_Uns is new Ada.Unchecked_Conversion (Double_Int, Double_Uns);
62 function To_Int is new Ada.Unchecked_Conversion (Double_Uns, Double_Int);
63
64 Double_Size : constant Natural := Double_Int'Size;
65 Single_Size : constant Natural := Double_Int'Size / 2;
66
67 -- Log of Single_Size in base 2, so that Single_Size = 2 ** Log_Single_Size
68 Log_Single_Size : constant Natural :=
69 (case Single_Size is
70 when 32 => 5,
71 when 64 => 6,
72 when 128 => 7,
73 when others => raise Program_Error)
74 with Ghost;
75
76 -- Power-of-two constants
77
78 pragma Warnings
79 (Off, "non-preelaborable call not allowed in preelaborated unit",
80 Reason => "Ghost code is not compiled");
81 pragma Warnings
82 (Off, "non-static constant in preelaborated unit",
83 Reason => "Ghost code is not compiled");
84 Big_0 : constant Big_Integer :=
85 Big (Double_Uns'(0))
86 with Ghost;
87 Big_2xxSingle : constant Big_Integer :=
88 Big (Double_Int'(2 ** Single_Size))
89 with Ghost;
90 Big_2xxDouble_Minus_1 : constant Big_Integer :=
91 Big (Double_Uns'(2 ** (Double_Size - 1)))
92 with Ghost;
93 Big_2xxDouble : constant Big_Integer :=
94 Big (Double_Uns'(2 ** Double_Size - 1)) + 1
95 with Ghost;
96 pragma Warnings
97 (On, "non-preelaborable call not allowed in preelaborated unit");
98 pragma Warnings (On, "non-static constant in preelaborated unit");
99
100 pragma Annotate (Gnatcheck, Exempt_On, "Improper_Returns",
101 "early returns for performance");
102
103 -----------------------
104 -- Local Subprograms --
105 -----------------------
106
107 function "+" (A, B : Single_Uns) return Double_Uns is
108 (Double_Uns (A) + Double_Uns (B));
109 function "+" (A : Double_Uns; B : Single_Uns) return Double_Uns is
110 (A + Double_Uns (B));
111 -- Length doubling additions
112
113 function "*" (A, B : Single_Uns) return Double_Uns is
114 (Double_Uns (A) * Double_Uns (B));
115 -- Length doubling multiplication
116
117 function "/" (A : Double_Uns; B : Single_Uns) return Double_Uns is
118 (A / Double_Uns (B))
119 with
120 Pre => B /= 0;
121 -- Length doubling division
122
123 function "&" (Hi, Lo : Single_Uns) return Double_Uns is
124 (Shift_Left (Double_Uns (Hi), Single_Size) or Double_Uns (Lo));
125 -- Concatenate hi, lo values to form double result
126
127 function "abs" (X : Double_Int) return Double_Uns is
128 (if X = Double_Int'First
129 then Double_Uns'(2 ** (Double_Size - 1))
130 else Double_Uns (Double_Int'(abs X)))
131 with Post => abs Big (X) = Big ("abs"'Result),
132 Annotate => (GNATprove, Hide_Info, "Expression_Function_Body");
133 -- Convert absolute value of X to unsigned. Note that we can't just use
134 -- the expression of the Else since it overflows for X = Double_Int'First.
135
136 function "rem" (A : Double_Uns; B : Single_Uns) return Double_Uns is
137 (A rem Double_Uns (B))
138 with
139 Pre => B /= 0;
140 -- Length doubling remainder
141
142 function Big_2xx (N : Natural) return Big_Positive is
143 (Big (Double_Uns'(2 ** N)))
144 with
145 Ghost,
146 Pre => N < Double_Size,
147 Post => Big_2xx'Result > 0;
148 -- 2**N as a big integer
149
150 function Big3 (X1, X2, X3 : Single_Uns) return Big_Natural is
151 (Big_2xxSingle * Big_2xxSingle * Big (Double_Uns (X1))
152 + Big_2xxSingle * Big (Double_Uns (X2))
153 + Big (Double_Uns (X3)))
154 with
155 Ghost;
156 -- X1&X2&X3 as a big integer
157
158 function Le3 (X1, X2, X3, Y1, Y2, Y3 : Single_Uns) return Boolean
159 with
160 Post => Le3'Result = (Big3 (X1, X2, X3) <= Big3 (Y1, Y2, Y3));
161 -- Determines if (3 * Single_Size)-bit value X1&X2&X3 <= Y1&Y2&Y3
162
163 function Lo (A : Double_Uns) return Single_Uns is
164 (Single_Uns (A and (2 ** Single_Size - 1)));
165 -- Low order half of double value
166
167 function Hi (A : Double_Uns) return Single_Uns is
168 (Single_Uns (Shift_Right (A, Single_Size)));
169 -- High order half of double value
170
171 procedure Sub3 (X1, X2, X3 : in out Single_Uns; Y1, Y2, Y3 : Single_Uns)
172 with
173 Pre => Big3 (X1, X2, X3) >= Big3 (Y1, Y2, Y3),
174 Post => Big3 (X1, X2, X3) = Big3 (X1, X2, X3)'Old - Big3 (Y1, Y2, Y3);
175 -- Computes X1&X2&X3 := X1&X2&X3 - Y1&Y1&Y3 mod 2 ** (3 * Single_Size)
176
177 function To_Neg_Int (A : Double_Uns) return Double_Int
178 with
179 Pre => In_Double_Int_Range (-Big (A)),
180 Post => Big (To_Neg_Int'Result) = -Big (A);
181 -- Convert to negative integer equivalent. If the input is in the range
182 -- 0 .. 2 ** (Double_Size - 1), then the corresponding nonpositive signed
183 -- integer (obtained by negating the given value) is returned, otherwise
184 -- constraint error is raised.
185
186 function To_Pos_Int (A : Double_Uns) return Double_Int
187 with
188 Pre => In_Double_Int_Range (Big (A)),
189 Post => Big (To_Pos_Int'Result) = Big (A);
190 -- Convert to positive integer equivalent. If the input is in the range
191 -- 0 .. 2 ** (Double_Size - 1) - 1, then the corresponding non-negative
192 -- signed integer is returned, otherwise constraint error is raised.
193
194 procedure Raise_Error with
195 Exceptional_Cases => (Constraint_Error => True);
196 pragma No_Return (Raise_Error);
197 -- Raise constraint error with appropriate message
198
199 ------------------
200 -- Local Lemmas --
201 ------------------
202
203 procedure Inline_Le3 (X1, X2, X3, Y1, Y2, Y3 : Single_Uns)
204 with
205 Ghost,
206 Pre => Le3 (X1, X2, X3, Y1, Y2, Y3),
207 Post => Big3 (X1, X2, X3) <= Big3 (Y1, Y2, Y3);
208
209 procedure Lemma_Abs_Commutation (X : Double_Int)
210 with
211 Ghost,
212 Post => abs Big (X) = Big (Double_Uns'(abs X));
213
214 procedure Lemma_Abs_Div_Commutation (X, Y : Big_Integer)
215 with
216 Ghost,
217 Pre => Y /= 0,
218 Post => abs (X / Y) = abs X / abs Y;
219
220 procedure Lemma_Abs_Mult_Commutation (X, Y : Big_Integer)
221 with
222 Ghost,
223 Post => abs (X * Y) = abs X * abs Y;
224
225 procedure Lemma_Abs_Range (X : Big_Integer)
226 with
227 Ghost,
228 Pre => In_Double_Int_Range (X),
229 Post => abs X <= Big_2xxDouble_Minus_1
230 and then In_Double_Int_Range (-abs X);
231
232 procedure Lemma_Abs_Rem_Commutation (X, Y : Big_Integer)
233 with
234 Ghost,
235 Pre => Y /= 0,
236 Post => abs (X rem Y) = (abs X) rem (abs Y);
237
238 procedure Lemma_Add_Commutation (X : Double_Uns; Y : Single_Uns)
239 with
240 Ghost,
241 Pre => X <= 2 ** Double_Size - 2 ** Single_Size,
242 Post => Big (X) + Big (Double_Uns (Y)) = Big (X + Double_Uns (Y));
243
244 procedure Lemma_Add_One (X : Double_Uns)
245 with
246 Ghost,
247 Pre => X /= Double_Uns'Last,
248 Post => Big (X + Double_Uns'(1)) = Big (X) + 1;
249
250 procedure Lemma_Big_Of_Double_Uns (X : Double_Uns)
251 with
252 Ghost,
253 Post => Big (X) < Big_2xxDouble;
254
255 procedure Lemma_Big_Of_Double_Uns_Of_Single_Uns (X : Single_Uns)
256 with
257 Ghost,
258 Post => Big (Double_Uns (X)) >= 0
259 and then Big (Double_Uns (X)) < Big_2xxSingle;
260
261 procedure Lemma_Bounded_Powers_Of_2_Increasing (M, N : Natural)
262 with
263 Ghost,
264 Pre => M < N and then N < Double_Size,
265 Post => Double_Uns'(2)**M < Double_Uns'(2)**N;
266
267 procedure Lemma_Concat_Definition (X, Y : Single_Uns)
268 with
269 Ghost,
270 Post => Big (X & Y) = Big_2xxSingle * Big (Double_Uns (X))
271 + Big (Double_Uns (Y));
272
273 procedure Lemma_Deep_Mult_Commutation
274 (Factor : Big_Integer;
275 X, Y : Single_Uns)
276 with
277 Ghost,
278 Post =>
279 Factor * Big (Double_Uns (X)) * Big (Double_Uns (Y)) =
280 Factor * Big (Double_Uns (X) * Double_Uns (Y));
281
282 procedure Lemma_Div_Commutation (X, Y : Double_Uns)
283 with
284 Ghost,
285 Pre => Y /= 0,
286 Post => Big (X) / Big (Y) = Big (X / Y);
287
288 procedure Lemma_Div_Definition
289 (A : Double_Uns;
290 B : Single_Uns;
291 Q : Double_Uns;
292 R : Double_Uns)
293 with
294 Ghost,
295 Pre => B /= 0 and then Q = A / B and then R = A rem B,
296 Post => Big (A) = Big (Double_Uns (B)) * Big (Q) + Big (R);
297
298 procedure Lemma_Div_Ge (X, Y, Z : Big_Integer)
299 with
300 Ghost,
301 Pre => Z > 0 and then X >= Y * Z,
302 Post => X / Z >= Y;
303
304 procedure Lemma_Div_Lt (X, Y, Z : Big_Natural)
305 with
306 Ghost,
307 Pre => Z > 0 and then X < Y * Z,
308 Post => X / Z < Y;
309
310 procedure Lemma_Div_Eq (A, B, S, R : Big_Integer)
311 with
312 Ghost,
313 Pre => A * S = B * S + R and then S /= 0,
314 Post => A = B + R / S;
315
316 procedure Lemma_Div_Mult (X : Big_Natural; Y : Big_Positive)
317 with
318 Ghost,
319 Post => X / Y * Y > X - Y;
320
321 procedure Lemma_Double_Big_2xxSingle
322 with
323 Ghost,
324 Post => Big_2xxSingle * Big_2xxSingle = Big_2xxDouble;
325
326 procedure Lemma_Double_Shift (X : Double_Uns; S, S1 : Double_Uns)
327 with
328 Ghost,
329 Pre => S <= Double_Uns (Double_Size)
330 and then S1 <= Double_Uns (Double_Size),
331 Post => Shift_Left (Shift_Left (X, Natural (S)), Natural (S1)) =
332 Shift_Left (X, Natural (S + S1));
333
334 procedure Lemma_Double_Shift (X : Single_Uns; S, S1 : Natural)
335 with
336 Ghost,
337 Pre => S <= Single_Size - S1,
338 Post => Shift_Left (Shift_Left (X, S), S1) = Shift_Left (X, S + S1);
339
340 procedure Lemma_Double_Shift (X : Double_Uns; S, S1 : Natural)
341 with
342 Ghost,
343 Pre => S <= Double_Size - S1,
344 Post => Shift_Left (Shift_Left (X, S), S1) = Shift_Left (X, S + S1);
345
346 procedure Lemma_Double_Shift_Left (X : Double_Uns; S, S1 : Double_Uns)
347 with
348 Ghost,
349 Pre => S <= Double_Uns (Double_Size)
350 and then S1 <= Double_Uns (Double_Size),
351 Post => Shift_Left (Shift_Left (X, Natural (S)), Natural (S1)) =
352 Shift_Left (X, Natural (S + S1));
353
354 procedure Lemma_Double_Shift_Left (X : Double_Uns; S, S1 : Natural)
355 with
356 Ghost,
357 Pre => S <= Double_Size - S1,
358 Post => Shift_Left (Shift_Left (X, S), S1) = Shift_Left (X, S + S1);
359
360 procedure Lemma_Double_Shift_Right (X : Double_Uns; S, S1 : Double_Uns)
361 with
362 Ghost,
363 Pre => S <= Double_Uns (Double_Size)
364 and then S1 <= Double_Uns (Double_Size),
365 Post => Shift_Right (Shift_Right (X, Natural (S)), Natural (S1)) =
366 Shift_Right (X, Natural (S + S1));
367
368 procedure Lemma_Double_Shift_Right (X : Double_Uns; S, S1 : Natural)
369 with
370 Ghost,
371 Pre => S <= Double_Size - S1,
372 Post => Shift_Right (Shift_Right (X, S), S1) = Shift_Right (X, S + S1);
373
374 procedure Lemma_Ge_Commutation (A, B : Double_Uns)
375 with
376 Ghost,
377 Pre => A >= B,
378 Post => Big (A) >= Big (B);
379
380 procedure Lemma_Ge_Mult (A, B, C, D : Big_Integer)
381 with
382 Ghost,
383 Pre => A >= B and then B * C >= D and then C > 0,
384 Post => A * C >= D;
385
386 procedure Lemma_Gt_Commutation (A, B : Double_Uns)
387 with
388 Ghost,
389 Pre => A > B,
390 Post => Big (A) > Big (B);
391
392 procedure Lemma_Gt_Mult (A, B, C, D : Big_Integer)
393 with
394 Ghost,
395 Pre => A >= B and then B * C > D and then C > 0,
396 Post => A * C > D;
397
398 procedure Lemma_Hi_Lo (Xu : Double_Uns; Xhi, Xlo : Single_Uns)
399 with
400 Ghost,
401 Pre => Xhi = Hi (Xu) and Xlo = Lo (Xu),
402 Post => Big (Xu) =
403 Big_2xxSingle * Big (Double_Uns (Xhi)) + Big (Double_Uns (Xlo));
404
405 procedure Lemma_Hi_Lo_3 (Xu : Double_Uns; Xhi, Xlo : Single_Uns)
406 with
407 Ghost,
408 Pre => Xhi = Hi (Xu) and then Xlo = Lo (Xu),
409 Post => Big (Xu) = Big3 (0, Xhi, Xlo);
410
411 procedure Lemma_Lo_Is_Ident (X : Double_Uns)
412 with
413 Ghost,
414 Pre => Big (X) < Big_2xxSingle,
415 Post => Double_Uns (Lo (X)) = X;
416
417 procedure Lemma_Lt_Commutation (A, B : Double_Uns)
418 with
419 Ghost,
420 Pre => A < B,
421 Post => Big (A) < Big (B);
422
423 procedure Lemma_Lt_Mult (A, B, C, D : Big_Integer)
424 with
425 Ghost,
426 Pre => A < B and then B * C <= D and then C > 0,
427 Post => A * C < D;
428
429 procedure Lemma_Mult_Commutation (X, Y : Single_Uns)
430 with
431 Ghost,
432 Post =>
433 Big (Double_Uns (X)) * Big (Double_Uns (Y)) =
434 Big (Double_Uns (X) * Double_Uns (Y));
435
436 procedure Lemma_Mult_Commutation (X, Y : Double_Int)
437 with
438 Ghost,
439 Pre => In_Double_Int_Range (Big (X) * Big (Y)),
440 Post => Big (X) * Big (Y) = Big (X * Y);
441
442 procedure Lemma_Mult_Commutation (X, Y, Z : Double_Uns)
443 with
444 Ghost,
445 Pre => Big (X) * Big (Y) < Big_2xxDouble and then Z = X * Y,
446 Post => Big (X) * Big (Y) = Big (Z);
447
448 procedure Lemma_Mult_Decomposition
449 (Mult : Big_Integer;
450 Xu, Yu : Double_Uns;
451 Xhi, Xlo, Yhi, Ylo : Single_Uns)
452 with
453 Ghost,
454 Pre => Mult = Big (Xu) * Big (Yu)
455 and then Xhi = Hi (Xu)
456 and then Xlo = Lo (Xu)
457 and then Yhi = Hi (Yu)
458 and then Ylo = Lo (Yu),
459 Post => Mult =
460 Big_2xxSingle * Big_2xxSingle * (Big (Double_Uns'(Xhi * Yhi)))
461 + Big_2xxSingle * (Big (Double_Uns'(Xhi * Ylo)))
462 + Big_2xxSingle * (Big (Double_Uns'(Xlo * Yhi)))
463 + (Big (Double_Uns'(Xlo * Ylo)));
464
465 procedure Lemma_Mult_Distribution (X, Y, Z : Big_Integer)
466 with
467 Ghost,
468 Post => X * (Y + Z) = X * Y + X * Z;
469
470 procedure Lemma_Mult_Div (A, B : Big_Integer)
471 with
472 Ghost,
473 Pre => B /= 0,
474 Post => A * B / B = A;
475
476 procedure Lemma_Mult_Non_Negative (X, Y : Big_Integer)
477 with
478 Ghost,
479 Pre => (X >= 0 and then Y >= 0)
480 or else (X <= 0 and then Y <= 0),
481 Post => X * Y >= 0;
482
483 procedure Lemma_Mult_Non_Positive (X, Y : Big_Integer)
484 with
485 Ghost,
486 Pre => (X <= Big_0 and then Y >= Big_0)
487 or else (X >= Big_0 and then Y <= Big_0),
488 Post => X * Y <= Big_0;
489
490 procedure Lemma_Mult_Positive (X, Y : Big_Integer)
491 with
492 Ghost,
493 Pre => (X > Big_0 and then Y > Big_0)
494 or else (X < Big_0 and then Y < Big_0),
495 Post => X * Y > Big_0;
496
497 procedure Lemma_Neg_Div (X, Y : Big_Integer)
498 with
499 Ghost,
500 Pre => Y /= 0,
501 Post => X / Y = (-X) / (-Y);
502
503 procedure Lemma_Neg_Rem (X, Y : Big_Integer)
504 with
505 Ghost,
506 Pre => Y /= 0,
507 Post => X rem Y = X rem (-Y);
508
509 procedure Lemma_Not_In_Range_Big2xx64
510 with
511 Post => not In_Double_Int_Range (Big_2xxDouble)
512 and then not In_Double_Int_Range (-Big_2xxDouble);
513
514 procedure Lemma_Powers (A : Big_Natural; B, C : Natural)
515 with
516 Ghost,
517 Pre => B <= Natural'Last - C,
518 Post => A**B * A**C = A**(B + C);
519
520 procedure Lemma_Powers_Of_2 (M, N : Natural)
521 with
522 Ghost,
523 Pre => M < Double_Size
524 and then N < Double_Size
525 and then M + N <= Double_Size,
526 Post =>
527 Big_2xx (M) * Big_2xx (N) =
528 (if M + N = Double_Size then Big_2xxDouble else Big_2xx (M + N));
529
530 procedure Lemma_Powers_Of_2_Commutation (M : Natural)
531 with
532 Ghost,
533 Subprogram_Variant => (Decreases => M),
534 Pre => M <= Double_Size,
535 Post => Big (Double_Uns'(2))**M =
536 (if M < Double_Size then Big_2xx (M) else Big_2xxDouble);
537
538 procedure Lemma_Powers_Of_2_Increasing (M, N : Natural)
539 with
540 Ghost,
541 Subprogram_Variant => (Increases => M),
542 Pre => M < N,
543 Post => Big (Double_Uns'(2))**M < Big (Double_Uns'(2))**N;
544
545 procedure Lemma_Rem_Abs (X, Y : Big_Integer)
546 with
547 Ghost,
548 Pre => Y /= 0,
549 Post => X rem Y = X rem (abs Y);
550 pragma Unreferenced (Lemma_Rem_Abs);
551
552 procedure Lemma_Rem_Commutation (X, Y : Double_Uns)
553 with
554 Ghost,
555 Pre => Y /= 0,
556 Post => Big (X) rem Big (Y) = Big (X rem Y);
557
558 procedure Lemma_Rem_Is_Ident (X, Y : Big_Integer)
559 with
560 Ghost,
561 Pre => abs X < abs Y,
562 Post => X rem Y = X;
563 pragma Unreferenced (Lemma_Rem_Is_Ident);
564
565 procedure Lemma_Rem_Sign (X, Y : Big_Integer)
566 with
567 Ghost,
568 Pre => Y /= 0,
569 Post => Same_Sign (X rem Y, X);
570 pragma Unreferenced (Lemma_Rem_Sign);
571
572 procedure Lemma_Rev_Div_Definition (A, B, Q, R : Big_Natural)
573 with
574 Ghost,
575 Pre => A = B * Q + R and then R < B,
576 Post => Q = A / B and then R = A rem B;
577
578 procedure Lemma_Shift_Left (X : Double_Uns; Shift : Natural)
579 with
580 Ghost,
581 Pre => Shift < Double_Size
582 and then Big (X) * Big_2xx (Shift) < Big_2xxDouble,
583 Post => Big (Shift_Left (X, Shift)) = Big (X) * Big_2xx (Shift);
584
585 procedure Lemma_Shift_Right (X : Double_Uns; Shift : Natural)
586 with
587 Ghost,
588 Pre => Shift < Double_Size,
589 Post => Big (Shift_Right (X, Shift)) = Big (X) / Big_2xx (Shift);
590
591 procedure Lemma_Shift_Without_Drop
592 (X, Y : Double_Uns;
593 Mask : Single_Uns;
594 Shift : Natural)
595 with
596 Ghost,
597 Pre => (Hi (X) and Mask) = 0 -- X has the first Shift bits off
598 and then Shift <= Single_Size
599 and then Mask = Shift_Left (Single_Uns'Last, Single_Size - Shift)
600 and then Y = Shift_Left (X, Shift),
601 Post => Big (Y) = Big_2xx (Shift) * Big (X);
602
603 procedure Lemma_Simplify (X, Y : Big_Integer)
604 with
605 Ghost,
606 Pre => Y /= 0,
607 Post => X * Y / Y = X;
608
609 procedure Lemma_Substitution (A, B, C, C1, D : Big_Integer)
610 with
611 Ghost,
612 Pre => C = C1 and then A = B * C + D,
613 Post => A = B * C1 + D;
614
615 procedure Lemma_Subtract_Commutation (X, Y : Double_Uns)
616 with
617 Ghost,
618 Pre => X >= Y,
619 Post => Big (X) - Big (Y) = Big (X - Y);
620
621 procedure Lemma_Subtract_Double_Uns (X, Y : Double_Int)
622 with
623 Ghost,
624 Pre => X >= 0 and then X <= Y,
625 Post => Double_Uns (Y - X) = Double_Uns (Y) - Double_Uns (X);
626
627 procedure Lemma_Word_Commutation (X : Single_Uns)
628 with
629 Ghost,
630 Post => Big_2xxSingle * Big (Double_Uns (X))
631 = Big (2**Single_Size * Double_Uns (X));
632
633 -----------------------------
634 -- Local lemma null bodies --
635 -----------------------------
636
637 procedure Inline_Le3 (X1, X2, X3, Y1, Y2, Y3 : Single_Uns) is null;
638 procedure Lemma_Abs_Commutation (X : Double_Int) is null;
639 procedure Lemma_Abs_Mult_Commutation (X, Y : Big_Integer) is null;
640 procedure Lemma_Abs_Range (X : Big_Integer) is null;
641 procedure Lemma_Add_Commutation (X : Double_Uns; Y : Single_Uns) is null;
642 procedure Lemma_Add_One (X : Double_Uns) is null;
643 procedure Lemma_Big_Of_Double_Uns (X : Double_Uns) is null;
644 procedure Lemma_Big_Of_Double_Uns_Of_Single_Uns (X : Single_Uns) is null;
645 procedure Lemma_Bounded_Powers_Of_2_Increasing (M, N : Natural) is null;
646 procedure Lemma_Deep_Mult_Commutation
647 (Factor : Big_Integer;
648 X, Y : Single_Uns)
649 is null;
650 procedure Lemma_Div_Commutation (X, Y : Double_Uns) is null;
651 procedure Lemma_Div_Definition
652 (A : Double_Uns;
653 B : Single_Uns;
654 Q : Double_Uns;
655 R : Double_Uns)
656 is null;
657 procedure Lemma_Div_Ge (X, Y, Z : Big_Integer) is null;
658 procedure Lemma_Div_Lt (X, Y, Z : Big_Natural) is null;
659 procedure Lemma_Div_Mult (X : Big_Natural; Y : Big_Positive) is null;
660 procedure Lemma_Double_Big_2xxSingle is null;
661 procedure Lemma_Double_Shift (X : Double_Uns; S, S1 : Double_Uns) is null;
662 procedure Lemma_Double_Shift (X : Single_Uns; S, S1 : Natural) is null;
663 procedure Lemma_Double_Shift_Left (X : Double_Uns; S, S1 : Double_Uns)
664 is null;
665 procedure Lemma_Double_Shift_Right (X : Double_Uns; S, S1 : Double_Uns)
666 is null;
667 procedure Lemma_Ge_Commutation (A, B : Double_Uns) is null;
668 procedure Lemma_Ge_Mult (A, B, C, D : Big_Integer) is null;
669 procedure Lemma_Gt_Commutation (A, B : Double_Uns) is null;
670 procedure Lemma_Gt_Mult (A, B, C, D : Big_Integer) is null;
671 procedure Lemma_Lo_Is_Ident (X : Double_Uns) is null;
672 procedure Lemma_Lt_Commutation (A, B : Double_Uns) is null;
673 procedure Lemma_Lt_Mult (A, B, C, D : Big_Integer) is null;
674 procedure Lemma_Mult_Commutation (X, Y : Single_Uns) is null;
675 procedure Lemma_Mult_Commutation (X, Y : Double_Int) is null;
676 procedure Lemma_Mult_Commutation (X, Y, Z : Double_Uns) is null;
677 procedure Lemma_Mult_Distribution (X, Y, Z : Big_Integer) is null;
678 procedure Lemma_Mult_Non_Negative (X, Y : Big_Integer) is null;
679 procedure Lemma_Mult_Non_Positive (X, Y : Big_Integer) is null;
680 procedure Lemma_Mult_Positive (X, Y : Big_Integer) is null;
681 procedure Lemma_Neg_Rem (X, Y : Big_Integer) is null;
682 procedure Lemma_Not_In_Range_Big2xx64 is null;
683 procedure Lemma_Powers (A : Big_Natural; B, C : Natural) is null;
684 procedure Lemma_Rem_Commutation (X, Y : Double_Uns) is null;
685 procedure Lemma_Rem_Is_Ident (X, Y : Big_Integer) is null;
686 procedure Lemma_Rem_Sign (X, Y : Big_Integer) is null;
687 procedure Lemma_Rev_Div_Definition (A, B, Q, R : Big_Natural) is null;
688 procedure Lemma_Simplify (X, Y : Big_Integer) is null;
689 procedure Lemma_Substitution (A, B, C, C1, D : Big_Integer) is null;
690 procedure Lemma_Subtract_Commutation (X, Y : Double_Uns) is null;
691 procedure Lemma_Subtract_Double_Uns (X, Y : Double_Int) is null;
692 procedure Lemma_Word_Commutation (X : Single_Uns) is null;
693
694 --------------------------
695 -- Add_With_Ovflo_Check --
696 --------------------------
697
698 function Add_With_Ovflo_Check (X, Y : Double_Int) return Double_Int is
699 R : constant Double_Int := To_Int (To_Uns (X) + To_Uns (Y));
700
701 -- Local lemmas
702
703 procedure Prove_Negative_X
704 with
705 Ghost,
706 Pre => X < 0 and then (Y > 0 or else R < 0),
707 Post => R = X + Y;
708
709 procedure Prove_Non_Negative_X
710 with
711 Ghost,
712 Pre => X >= 0 and then (Y < 0 or else R >= 0),
713 Post => R = X + Y;
714
715 procedure Prove_Overflow_Case
716 with
717 Ghost,
718 Pre =>
719 (if X >= 0 then Y >= 0 and then R < 0
720 else Y <= 0 and then R >= 0),
721 Post => not In_Double_Int_Range (Big (X) + Big (Y));
722
723 ----------------------
724 -- Prove_Negative_X --
725 ----------------------
726
727 procedure Prove_Negative_X is
728 begin
729 if X = Double_Int'First then
730 if Y > 0 then
731 null;
732 else
733 pragma Assert
734 (To_Uns (X) + To_Uns (Y) =
735 2 ** (Double_Size - 1) - Double_Uns (-Y));
736 pragma Assert -- as R < 0
737 (To_Uns (X) + To_Uns (Y) >= 2 ** (Double_Size - 1));
738 pragma Assert (Y = 0);
739 end if;
740
741 elsif Y = Double_Int'First then
742 pragma Assert
743 (To_Uns (X) + To_Uns (Y) =
744 2 ** (Double_Size - 1) - Double_Uns (-X));
745 pragma Assert (False);
746
747 elsif Y <= 0 then
748 pragma Assert
749 (To_Uns (X) + To_Uns (Y) = -Double_Uns (-X) - Double_Uns (-Y));
750
751 else -- Y > 0, 0 > X > Double_Int'First
752 declare
753 Ru : constant Double_Uns := To_Uns (X) + To_Uns (Y);
754 begin
755 pragma Assert (Ru = -Double_Uns (-X) + Double_Uns (Y));
756 if Ru < 2 ** (Double_Size - 1) then -- R >= 0
757 Lemma_Subtract_Double_Uns (-X, Y);
758 pragma Assert (Ru = Double_Uns (X + Y));
759
760 elsif Ru = 2 ** (Double_Size - 1) then
761 pragma Assert (Double_Uns (Y) < 2 ** (Double_Size - 1));
762 pragma Assert (Double_Uns (-X) < 2 ** (Double_Size - 1));
763 pragma Assert (False);
764
765 else
766 pragma Assert
767 (R = -Double_Int (-(-Double_Uns (-X) + Double_Uns (Y))));
768 pragma Assert
769 (R = -Double_Int (-Double_Uns (Y) + Double_Uns (-X)));
770 end if;
771 end;
772 end if;
773 end Prove_Negative_X;
774
775 --------------------------
776 -- Prove_Non_Negative_X --
777 --------------------------
778
779 procedure Prove_Non_Negative_X is
780 begin
781 if Y >= 0 or else Y = Double_Int'First then
782 null;
783 else
784 pragma Assert
785 (To_Uns (X) + To_Uns (Y) = Double_Uns (X) - Double_Uns (-Y));
786 end if;
787 end Prove_Non_Negative_X;
788
789 -------------------------
790 -- Prove_Overflow_Case --
791 -------------------------
792
793 procedure Prove_Overflow_Case is
794 begin
795 if X < 0 and then X /= Double_Int'First and then Y /= Double_Int'First
796 then
797 pragma Assert
798 (To_Uns (X) + To_Uns (Y) = -Double_Uns (-X) - Double_Uns (-Y));
799 end if;
800 end Prove_Overflow_Case;
801
802 -- Start of processing for Add_With_Ovflo_Check
803
804 begin
805 if X >= 0 then
806 if Y < 0 or else R >= 0 then
807 Prove_Non_Negative_X;
808 return R;
809 end if;
810
811 else -- X < 0
812 if Y > 0 or else R < 0 then
813 Prove_Negative_X;
814 return R;
815 end if;
816 end if;
817
818 Prove_Overflow_Case;
819 Raise_Error;
820 end Add_With_Ovflo_Check;
821
822 -------------------
823 -- Double_Divide --
824 -------------------
825
826 pragma Annotate (Gnatcheck, Exempt_On, "Metrics_Cyclomatic_Complexity",
827 "limit exceeded due to proof code");
828 procedure Double_Divide
829 (X, Y, Z : Double_Int;
830 Q, R : out Double_Int;
831 Round : Boolean)
832 is
833 Xu : constant Double_Uns := abs X;
834 Yu : constant Double_Uns := abs Y;
835
836 Yhi : constant Single_Uns := Hi (Yu);
837 Ylo : constant Single_Uns := Lo (Yu);
838
839 Zu : constant Double_Uns := abs Z;
840 Zhi : constant Single_Uns := Hi (Zu);
841 Zlo : constant Single_Uns := Lo (Zu);
842
843 T1, T2 : Double_Uns;
844 Du, Qu, Ru : Double_Uns;
845 Den_Pos : constant Boolean := (Y < 0) = (Z < 0);
846
847 -- Local ghost variables
848
849 Mult : constant Big_Integer := abs (Big (Y) * Big (Z)) with Ghost;
850 Quot : Big_Integer with Ghost;
851 Big_R : Big_Integer with Ghost;
852 Big_Q : Big_Integer with Ghost;
853
854 -- Local lemmas
855
856 procedure Prove_Overflow_Case
857 with
858 Ghost,
859 Pre => X = Double_Int'First and then Big (Y) * Big (Z) = -1,
860 Post => not In_Double_Int_Range (Big (X) / (Big (Y) * Big (Z)))
861 and then not In_Double_Int_Range
862 (Round_Quotient (Big (X), Big (Y) * Big (Z),
863 Big (X) / (Big (Y) * Big (Z)),
864 Big (X) rem (Big (Y) * Big (Z))));
865 -- Proves the special case where -2**(Double_Size - 1) is divided by -1,
866 -- generating an overflow.
867
868 procedure Prove_Quotient_Zero
869 with
870 Ghost,
871 Pre => Mult >= Big_2xxDouble
872 and then
873 not (Mult = Big_2xxDouble
874 and then X = Double_Int'First
875 and then Round)
876 and then Q = 0
877 and then R = X,
878 Post => Big (R) = Big (X) rem (Big (Y) * Big (Z))
879 and then
880 (if Round then
881 Big (Q) = Round_Quotient (Big (X), Big (Y) * Big (Z),
882 Big (X) / (Big (Y) * Big (Z)),
883 Big (R))
884 else Big (Q) = Big (X) / (Big (Y) * Big (Z)));
885 -- Proves the general case where divisor doesn't fit in Double_Uns and
886 -- quotient is 0.
887
888 procedure Prove_Round_To_One
889 with
890 Ghost,
891 Pre => Mult = Big_2xxDouble
892 and then X = Double_Int'First
893 and then Q = (if Den_Pos then -1 else 1)
894 and then R = X
895 and then Round,
896 Post => Big (R) = Big (X) rem (Big (Y) * Big (Z))
897 and then Big (Q) = Round_Quotient (Big (X), Big (Y) * Big (Z),
898 Big (X) / (Big (Y) * Big (Z)),
899 Big (R));
900 -- Proves the special case where the divisor doesn't fit in Double_Uns
901 -- but quotient is still 1 or -1 due to rounding
902 -- (abs (Y*Z) = 2**Double_Size and X = -2**(Double_Size - 1) and Round).
903
904 procedure Prove_Rounding_Case
905 with
906 Ghost,
907 Pre => Mult /= 0
908 and then Quot = Big (X) / (Big (Y) * Big (Z))
909 and then Big_R = Big (X) rem (Big (Y) * Big (Z))
910 and then Big_Q =
911 Round_Quotient (Big (X), Big (Y) * Big (Z), Quot, Big_R)
912 and then Big (Ru) = abs Big_R
913 and then Big (Du) = Mult
914 and then Big (Qu) =
915 (if Ru > (Du - Double_Uns'(1)) / Double_Uns'(2)
916 then abs Quot + 1
917 else abs Quot),
918 Post => abs Big_Q = Big (Qu);
919 -- Proves correctness of the rounding of the unsigned quotient
920
921 procedure Prove_Sign_Quotient
922 with
923 Ghost,
924 Pre => Mult /= 0
925 and then Quot = Big (X) / (Big (Y) * Big (Z))
926 and then Big_R = Big (X) rem (Big (Y) * Big (Z))
927 and then Big_Q =
928 (if Round then
929 Round_Quotient (Big (X), Big (Y) * Big (Z), Quot, Big_R)
930 else Quot),
931 Post =>
932 (if X >= 0 then
933 (if Den_Pos then Big_Q >= 0 else Big_Q <= 0)
934 else
935 (if Den_Pos then Big_Q <= 0 else Big_Q >= 0));
936 -- Proves the correct sign of the signed quotient Big_Q
937
938 procedure Prove_Signs
939 with
940 Ghost,
941 Pre => Mult /= 0
942 and then Quot = Big (X) / (Big (Y) * Big (Z))
943 and then Big_R = Big (X) rem (Big (Y) * Big (Z))
944 and then Big_Q =
945 (if Round then
946 Round_Quotient (Big (X), Big (Y) * Big (Z), Quot, Big_R)
947 else Quot)
948 and then Big (Ru) = abs Big_R
949 and then Big (Qu) = abs Big_Q
950 and then R = (if X >= 0 then To_Int (Ru) else To_Int (-Ru))
951 and then
952 Q = (if (X >= 0) = Den_Pos then To_Int (Qu) else To_Int (-Qu))
953 and then not (X = Double_Int'First and then Big (Y) * Big (Z) = -1),
954 Post => Big (R) = Big (X) rem (Big (Y) * Big (Z))
955 and then
956 (if Round then
957 Big (Q) = Round_Quotient (Big (X), Big (Y) * Big (Z),
958 Big (X) / (Big (Y) * Big (Z)),
959 Big (R))
960 else Big (Q) = Big (X) / (Big (Y) * Big (Z)));
961 -- Proves final signs match the intended result after the unsigned
962 -- division is done.
963
964 -----------------------------
965 -- Local lemma null bodies --
966 -----------------------------
967
968 procedure Prove_Overflow_Case is null;
969 procedure Prove_Quotient_Zero is null;
970 procedure Prove_Round_To_One is null;
971 procedure Prove_Sign_Quotient is null;
972
973 -------------------------
974 -- Prove_Rounding_Case --
975 -------------------------
976
977 procedure Prove_Rounding_Case is
978 begin
979 if Same_Sign (Big (X), Big (Y) * Big (Z)) then
980 pragma Assert (abs Big_Q = Big (Qu));
981 end if;
982 end Prove_Rounding_Case;
983
984 -----------------
985 -- Prove_Signs --
986 -----------------
987
988 procedure Prove_Signs is
989 begin
990 if (X >= 0) = Den_Pos then
991 pragma Assert (Quot >= 0);
992 pragma Assert (Big_Q >= 0);
993 pragma Assert (Q >= 0);
994 pragma Assert (Big (Q) = Big_Q);
995 else
996 pragma Assert ((X >= 0) /= (Big (Y) * Big (Z) >= 0));
997 pragma Assert (Quot <= 0);
998 pragma Assert (Big_Q <= 0);
999 pragma Assert (if X >= 0 then R >= 0);
1000 pragma Assert (if X < 0 then R <= 0);
1001 pragma Assert (Big (R) = Big_R);
1002 end if;
1003 end Prove_Signs;
1004
1005 -- Start of processing for Double_Divide
1006
1007 begin
1008 if Yu = 0 or else Zu = 0 then
1009 Raise_Error;
1010 end if;
1011
1012 pragma Assert (Mult /= 0);
1013 pragma Assert (Den_Pos = (Big (Y) * Big (Z) > 0));
1014 Quot := Big (X) / (Big (Y) * Big (Z));
1015 Big_R := Big (X) rem (Big (Y) * Big (Z));
1016 if Round then
1017 Big_Q := Round_Quotient (Big (X), Big (Y) * Big (Z), Quot, Big_R);
1018 else
1019 Big_Q := Quot;
1020 end if;
1021 Lemma_Abs_Mult_Commutation (Big (Y), Big (Z));
1022 Lemma_Mult_Decomposition (Mult, Yu, Zu, Yhi, Ylo, Zhi, Zlo);
1023
1024 -- Compute Y * Z. Note that if the result overflows Double_Uns, then
1025 -- the rounded result is zero, except for the very special case where
1026 -- X = -2 ** (Double_Size - 1) and abs (Y * Z) = 2 ** Double_Size, when
1027 -- Round is True.
1028
1029 if Yhi /= 0 then
1030 if Zhi /= 0 then
1031 R := X;
1032
1033 -- Handle the special case when Round is True
1034
1035 if Yhi = 1
1036 and then Zhi = 1
1037 and then Ylo = 0
1038 and then Zlo = 0
1039 and then X = Double_Int'First
1040 and then Round
1041 then
1042 Q := (if Den_Pos then -1 else 1);
1043
1044 Prove_Round_To_One;
1045
1046 else
1047 Q := 0;
1048
1049 pragma Assert (Double_Uns'(Yhi * Zhi) >= Double_Uns (Yhi));
1050 pragma Assert (Double_Uns'(Yhi * Zhi) >= Double_Uns (Zhi));
1051 pragma Assert (Big (Double_Uns'(Yhi * Zhi)) >= 1);
1052 if Yhi > 1 or else Zhi > 1 then
1053 pragma Assert (Big (Double_Uns'(Yhi * Zhi)) > 1);
1054 pragma Assert (if X = Double_Int'First and then Round then
1055 Mult > Big_2xxDouble);
1056 elsif Zlo > 0 then
1057 pragma Assert (Big (Double_Uns'(Yhi * Zlo)) > 0);
1058 pragma Assert (if X = Double_Int'First and then Round then
1059 Mult > Big_2xxDouble);
1060 elsif Ylo > 0 then
1061 pragma Assert (Double_Uns'(Ylo * Zhi) > 0);
1062 pragma Assert (Big (Double_Uns'(Ylo * Zhi)) > 0);
1063 pragma Assert (if X = Double_Int'First and then Round then
1064 Mult > Big_2xxDouble);
1065 else
1066 pragma Assert (not (X = Double_Int'First and then Round));
1067 end if;
1068 Prove_Quotient_Zero;
1069 end if;
1070
1071 return;
1072 else
1073 T2 := Yhi * Zlo;
1074 pragma Assert (Big (T2) = Big (Double_Uns'(Yhi * Zlo)));
1075 pragma Assert (Big_0 = Big (Double_Uns'(Ylo * Zhi)));
1076 end if;
1077
1078 else
1079 T2 := Ylo * Zhi;
1080 pragma Assert (Big (T2) = Big (Double_Uns'(Ylo * Zhi)));
1081 pragma Assert (Big_0 = Big (Double_Uns'(Yhi * Zlo)));
1082 end if;
1083
1084 T1 := Ylo * Zlo;
1085
1086 Lemma_Mult_Distribution (Big_2xxSingle,
1087 Big (Double_Uns'(Yhi * Zlo)),
1088 Big (Double_Uns'(Ylo * Zhi)));
1089 Lemma_Hi_Lo (T1, Hi (T1), Lo (T1));
1090 Lemma_Mult_Distribution (Big_2xxSingle,
1091 Big (T2),
1092 Big (Double_Uns (Hi (T1))));
1093 Lemma_Add_Commutation (T2, Hi (T1));
1094
1095 T2 := T2 + Hi (T1);
1096
1097 Lemma_Hi_Lo (T2, Hi (T2), Lo (T2));
1098 Lemma_Mult_Distribution (Big_2xxSingle,
1099 Big (Double_Uns (Hi (T2))),
1100 Big (Double_Uns (Lo (T2))));
1101 Lemma_Double_Big_2xxSingle;
1102
1103 if Hi (T2) /= 0 then
1104 R := X;
1105
1106 -- Handle the special case when Round is True
1107
1108 if Hi (T2) = 1
1109 and then Lo (T2) = 0
1110 and then Lo (T1) = 0
1111 and then X = Double_Int'First
1112 and then Round
1113 then
1114 Q := (if Den_Pos then -1 else 1);
1115
1116 Prove_Round_To_One;
1117
1118 else
1119 Q := 0;
1120
1121 pragma Assert (Big (Double_Uns (Hi (T2))) >= 1);
1122 pragma Assert (Big (Double_Uns (Lo (T2))) >= 0);
1123 pragma Assert (Big (Double_Uns (Lo (T1))) >= 0);
1124 pragma Assert (Big_2xxSingle * Big (Double_Uns (Lo (T2)))
1125 + Big (Double_Uns (Lo (T1))) >= 0);
1126 pragma Assert (Mult >= Big_2xxDouble * Big (Double_Uns (Hi (T2))));
1127 pragma Assert (Mult >= Big_2xxDouble);
1128 if Hi (T2) > 1 then
1129 pragma Assert (Big (Double_Uns (Hi (T2))) > 1);
1130 pragma Assert (if X = Double_Int'First and then Round then
1131 Mult > Big_2xxDouble);
1132 elsif Lo (T2) > 0 then
1133 pragma Assert (Big (Double_Uns (Lo (T2))) > 0);
1134 pragma Assert (Big_2xxSingle > 0);
1135 pragma Assert (Big_2xxSingle * Big (Double_Uns (Lo (T2))) > 0);
1136 pragma Assert (Big_2xxSingle * Big (Double_Uns (Lo (T2)))
1137 + Big (Double_Uns (Lo (T1))) > 0);
1138 pragma Assert (if X = Double_Int'First and then Round then
1139 Mult > Big_2xxDouble);
1140 elsif Lo (T1) > 0 then
1141 pragma Assert (Double_Uns (Lo (T1)) > 0);
1142 Lemma_Gt_Commutation (Double_Uns (Lo (T1)), 0);
1143 pragma Assert (Big (Double_Uns (Lo (T1))) > 0);
1144 pragma Assert (if X = Double_Int'First and then Round then
1145 Mult > Big_2xxDouble);
1146 else
1147 pragma Assert (not (X = Double_Int'First and then Round));
1148 end if;
1149 Prove_Quotient_Zero;
1150 end if;
1151
1152 return;
1153 end if;
1154
1155 Du := Lo (T2) & Lo (T1);
1156
1157 Lemma_Hi_Lo (Du, Lo (T2), Lo (T1));
1158 pragma Assert (Mult = Big (Du));
1159 pragma Assert (Du /= 0);
1160 -- Multiplication of 2-limb arguments Yu and Zu leads to 4-limb result
1161 -- (where each limb is a single value). Cases where 4 limbs are needed
1162 -- require Yhi /= 0 and Zhi /= 0 and lead to early exit. Remaining cases
1163 -- where 3 limbs are needed correspond to Hi(T2) /= 0 and lead to early
1164 -- exit. Thus, at this point, the result fits in 2 limbs which are
1165 -- exactly Lo (T2) and Lo (T1), which corresponds to the value of Du.
1166 -- As the case where one of Yu or Zu is null also led to early exit,
1167 -- we have Du /= 0 here.
1168
1169 -- Check overflow case of largest negative number divided by -1
1170
1171 if X = Double_Int'First and then Du = 1 and then not Den_Pos then
1172 Prove_Overflow_Case;
1173 Raise_Error;
1174 end if;
1175
1176 -- Perform the actual division
1177
1178 pragma Assert (Du /= 0);
1179 -- Multiplication of 2-limb arguments Yu and Zu leads to 4-limb result
1180 -- (where each limb is a single value). Cases where 4 limbs are needed
1181 -- require Yhi/=0 and Zhi/=0 and lead to early exit. Remaining cases
1182 -- where 3 limbs are needed correspond to Hi(T2)/=0 and lead to early
1183 -- exit. Thus, at this point, the result fits in 2 limbs which are
1184 -- exactly Lo(T2) and Lo(T1), which corresponds to the value of Du.
1185 -- As the case where one of Yu or Zu is null also led to early exit,
1186 -- we have Du/=0 here.
1187
1188 Qu := Xu / Du;
1189 Ru := Xu rem Du;
1190
1191 Lemma_Div_Commutation (Xu, Du);
1192 Lemma_Abs_Div_Commutation (Big (X), Big (Y) * Big (Z));
1193 Lemma_Abs_Commutation (X);
1194 pragma Assert (abs Quot = Big (Qu));
1195 Lemma_Rem_Commutation (Xu, Du);
1196 Lemma_Abs_Rem_Commutation (Big (X), Big (Y) * Big (Z));
1197 pragma Assert (abs Big_R = Big (Ru));
1198
1199 -- Deal with rounding case
1200
1201 if Round then
1202 if Ru > (Du - Double_Uns'(1)) / Double_Uns'(2) then
1203 Lemma_Add_Commutation (Qu, 1);
1204
1205 Qu := Qu + Double_Uns'(1);
1206 end if;
1207
1208 Prove_Rounding_Case;
1209 end if;
1210
1211 pragma Assert (abs Big_Q = Big (Qu));
1212 Prove_Sign_Quotient;
1213
1214 -- Set final signs (RM 4.5.5(27-30))
1215
1216 -- Case of dividend (X) sign positive
1217
1218 if X >= 0 then
1219 R := To_Int (Ru);
1220 Q := (if Den_Pos then To_Int (Qu) else To_Int (-Qu));
1221
1222 -- We perform the unary minus operation on the unsigned value
1223 -- before conversion to signed, to avoid a possible overflow
1224 -- for value -2 ** (Double_Size - 1), both for computing R and Q.
1225
1226 -- Case of dividend (X) sign negative
1227
1228 else
1229 R := To_Int (-Ru);
1230 Q := (if Den_Pos then To_Int (-Qu) else To_Int (Qu));
1231 end if;
1232
1233 Prove_Signs;
1234 end Double_Divide;
1235 pragma Annotate (Gnatcheck, Exempt_Off, "Metrics_Cyclomatic_Complexity");
1236
1237 ---------
1238 -- Le3 --
1239 ---------
1240
1241 function Le3 (X1, X2, X3, Y1, Y2, Y3 : Single_Uns) return Boolean is
1242 begin
1243 if X1 < Y1 then
1244 return True;
1245 elsif X1 > Y1 then
1246 return False;
1247 elsif X2 < Y2 then
1248 return True;
1249 elsif X2 > Y2 then
1250 return False;
1251 else
1252 return X3 <= Y3;
1253 end if;
1254 end Le3;
1255
1256 -------------------------------
1257 -- Lemma_Abs_Div_Commutation --
1258 -------------------------------
1259
1260 procedure Lemma_Abs_Div_Commutation (X, Y : Big_Integer) is
1261 begin
1262 if Y < 0 then
1263 if X < 0 then
1264 pragma Assert (abs (X / Y) = abs (X / (-Y)));
1265 else
1266 Lemma_Neg_Div (X, Y);
1267 pragma Assert (abs (X / Y) = abs ((-X) / (-Y)));
1268 end if;
1269 end if;
1270 end Lemma_Abs_Div_Commutation;
1271
1272 -------------------------------
1273 -- Lemma_Abs_Rem_Commutation --
1274 -------------------------------
1275
1276 procedure Lemma_Abs_Rem_Commutation (X, Y : Big_Integer) is
1277 begin
1278 if Y < 0 then
1279 Lemma_Neg_Rem (X, Y);
1280 if X < 0 then
1281 pragma Assert (X rem Y = -((-X) rem (-Y)));
1282 pragma Assert (abs (X rem Y) = (abs X) rem (abs Y));
1283 else
1284 pragma Assert (abs (X rem Y) = (abs X) rem (abs Y));
1285 end if;
1286 end if;
1287 end Lemma_Abs_Rem_Commutation;
1288
1289 -----------------------------
1290 -- Lemma_Concat_Definition --
1291 -----------------------------
1292
1293 procedure Lemma_Concat_Definition (X, Y : Single_Uns) is
1294 Hi : constant Double_Uns := Shift_Left (Double_Uns (X), Single_Size);
1295 Lo : constant Double_Uns := Double_Uns (Y);
1296 begin
1297 pragma Assert (Hi = Double_Uns'(2 ** Single_Size) * Double_Uns (X));
1298 pragma Assert ((Hi or Lo) = Hi + Lo);
1299 end Lemma_Concat_Definition;
1300
1301 ------------------
1302 -- Lemma_Div_Eq --
1303 ------------------
1304
1305 procedure Lemma_Div_Eq (A, B, S, R : Big_Integer) is
1306 begin
1307 pragma Assert ((A - B) * S = R);
1308 pragma Assert ((A - B) * S / S = R / S);
1309 Lemma_Mult_Div (A - B, S);
1310 pragma Assert (A - B = R / S);
1311 end Lemma_Div_Eq;
1312
1313 ------------------------
1314 -- Lemma_Double_Shift --
1315 ------------------------
1316
1317 procedure Lemma_Double_Shift (X : Double_Uns; S, S1 : Natural) is
1318 begin
1319 Lemma_Double_Shift (X, Double_Uns (S), Double_Uns (S1));
1320 pragma Assert (Shift_Left (Shift_Left (X, S), S1)
1321 = Shift_Left (Shift_Left (X, S), Natural (Double_Uns (S1))));
1322 pragma Assert (Shift_Left (X, S + S1)
1323 = Shift_Left (X, Natural (Double_Uns (S + S1))));
1324 end Lemma_Double_Shift;
1325
1326 -----------------------------
1327 -- Lemma_Double_Shift_Left --
1328 -----------------------------
1329
1330 procedure Lemma_Double_Shift_Left (X : Double_Uns; S, S1 : Natural) is
1331 begin
1332 Lemma_Double_Shift_Left (X, Double_Uns (S), Double_Uns (S1));
1333 pragma Assert (Shift_Left (Shift_Left (X, S), S1)
1334 = Shift_Left (Shift_Left (X, S), Natural (Double_Uns (S1))));
1335 pragma Assert (Shift_Left (X, S + S1)
1336 = Shift_Left (X, Natural (Double_Uns (S + S1))));
1337 end Lemma_Double_Shift_Left;
1338
1339 ------------------------------
1340 -- Lemma_Double_Shift_Right --
1341 ------------------------------
1342
1343 procedure Lemma_Double_Shift_Right (X : Double_Uns; S, S1 : Natural) is
1344 begin
1345 Lemma_Double_Shift_Right (X, Double_Uns (S), Double_Uns (S1));
1346 pragma Assert (Shift_Right (Shift_Right (X, S), S1)
1347 = Shift_Right (Shift_Right (X, S), Natural (Double_Uns (S1))));
1348 pragma Assert (Shift_Right (X, S + S1)
1349 = Shift_Right (X, Natural (Double_Uns (S + S1))));
1350 end Lemma_Double_Shift_Right;
1351
1352 -----------------
1353 -- Lemma_Hi_Lo --
1354 -----------------
1355
1356 procedure Lemma_Hi_Lo (Xu : Double_Uns; Xhi, Xlo : Single_Uns) is
1357 begin
1358 pragma Assert (Double_Uns (Xhi) = Xu / Double_Uns'(2 ** Single_Size));
1359 pragma Assert (Double_Uns (Xlo) = Xu mod 2 ** Single_Size);
1360 end Lemma_Hi_Lo;
1361
1362 -------------------
1363 -- Lemma_Hi_Lo_3 --
1364 -------------------
1365
1366 procedure Lemma_Hi_Lo_3 (Xu : Double_Uns; Xhi, Xlo : Single_Uns) is
1367 begin
1368 Lemma_Hi_Lo (Xu, Xhi, Xlo);
1369 end Lemma_Hi_Lo_3;
1370
1371 ------------------------------
1372 -- Lemma_Mult_Decomposition --
1373 ------------------------------
1374
1375 procedure Lemma_Mult_Decomposition
1376 (Mult : Big_Integer;
1377 Xu, Yu : Double_Uns;
1378 Xhi, Xlo, Yhi, Ylo : Single_Uns)
1379 is
1380 begin
1381 Lemma_Hi_Lo (Xu, Xhi, Xlo);
1382 Lemma_Hi_Lo (Yu, Yhi, Ylo);
1383
1384 pragma Assert
1385 (Mult =
1386 (Big_2xxSingle * Big (Double_Uns (Xhi)) + Big (Double_Uns (Xlo))) *
1387 (Big_2xxSingle * Big (Double_Uns (Yhi)) + Big (Double_Uns (Ylo))));
1388 pragma Assert (Mult =
1389 Big_2xxSingle
1390 * Big_2xxSingle * Big (Double_Uns (Xhi)) * Big (Double_Uns (Yhi))
1391 + Big_2xxSingle * Big (Double_Uns (Xhi)) * Big (Double_Uns (Ylo))
1392 + Big_2xxSingle * Big (Double_Uns (Xlo)) * Big (Double_Uns (Yhi))
1393 + Big (Double_Uns (Xlo)) * Big (Double_Uns (Ylo)));
1394 Lemma_Deep_Mult_Commutation (Big_2xxSingle * Big_2xxSingle, Xhi, Yhi);
1395 Lemma_Deep_Mult_Commutation (Big_2xxSingle, Xhi, Ylo);
1396 Lemma_Deep_Mult_Commutation (Big_2xxSingle, Xlo, Yhi);
1397 Lemma_Mult_Commutation (Xlo, Ylo);
1398 pragma Assert (Mult =
1399 Big_2xxSingle * Big_2xxSingle * Big (Double_Uns'(Xhi * Yhi))
1400 + Big_2xxSingle * Big (Double_Uns'(Xhi * Ylo))
1401 + Big_2xxSingle * Big (Double_Uns'(Xlo * Yhi))
1402 + Big (Double_Uns'(Xlo * Ylo)));
1403 end Lemma_Mult_Decomposition;
1404
1405 --------------------
1406 -- Lemma_Mult_Div --
1407 --------------------
1408
1409 procedure Lemma_Mult_Div (A, B : Big_Integer) is
1410 begin
1411 if B > 0 then
1412 pragma Assert (A * B / B = A);
1413 else
1414 pragma Assert (A * (-B) / (-B) = A);
1415 end if;
1416 end Lemma_Mult_Div;
1417
1418 -------------------
1419 -- Lemma_Neg_Div --
1420 -------------------
1421
1422 procedure Lemma_Neg_Div (X, Y : Big_Integer) is
1423 begin
1424 pragma Assert ((-X) / (-Y) = -(X / (-Y)));
1425 pragma Assert (X / (-Y) = -(X / Y));
1426 end Lemma_Neg_Div;
1427
1428 -----------------------
1429 -- Lemma_Powers_Of_2 --
1430 -----------------------
1431
1432 procedure Lemma_Powers_Of_2 (M, N : Natural) is
1433 begin
1434 if M + N < Double_Size then
1435 pragma Assert (Double_Uns'(2**M) * Double_Uns'(2**N)
1436 = Double_Uns'(2**(M + N)));
1437 end if;
1438
1439 Lemma_Powers_Of_2_Commutation (M);
1440 Lemma_Powers_Of_2_Commutation (N);
1441 Lemma_Powers_Of_2_Commutation (M + N);
1442 Lemma_Powers (Big (Double_Uns'(2)), M, N);
1443
1444 if M + N < Double_Size then
1445 pragma Assert (Big (Double_Uns'(2))**M * Big (Double_Uns'(2))**N
1446 = Big (Double_Uns'(2))**(M + N));
1447 Lemma_Powers_Of_2_Increasing (M + N, Double_Size);
1448 Lemma_Mult_Commutation (2 ** M, 2 ** N, 2 ** (M + N));
1449 else
1450 pragma Assert (Big (Double_Uns'(2))**M * Big (Double_Uns'(2))**N
1451 = Big (Double_Uns'(2))**(M + N));
1452 end if;
1453 end Lemma_Powers_Of_2;
1454
1455 -----------------------------------
1456 -- Lemma_Powers_Of_2_Commutation --
1457 -----------------------------------
1458
1459 procedure Lemma_Powers_Of_2_Commutation (M : Natural) is
1460 begin
1461 if M > 0 then
1462 Lemma_Powers_Of_2_Commutation (M - 1);
1463 pragma Assert (Big (Double_Uns'(2))**(M - 1) = Big_2xx (M - 1));
1464 pragma Assert (Big (Double_Uns'(2))**M = Big_2xx (M - 1) * 2);
1465 if M < Double_Size then
1466 Lemma_Powers_Of_2_Increasing (M - 1, Double_Size - 1);
1467 Lemma_Bounded_Powers_Of_2_Increasing (M - 1, Double_Size - 1);
1468 pragma Assert (Double_Uns'(2 ** (M - 1)) * 2 = Double_Uns'(2**M));
1469 Lemma_Mult_Commutation
1470 (Double_Uns'(2 ** (M - 1)), 2, Double_Uns'(2**M));
1471 pragma Assert (Big (Double_Uns'(2))**M = Big_2xx (M));
1472 end if;
1473 else
1474 pragma Assert (Big (Double_Uns'(2))**M = Big_2xx (M));
1475 end if;
1476 end Lemma_Powers_Of_2_Commutation;
1477
1478 ----------------------------------
1479 -- Lemma_Powers_Of_2_Increasing --
1480 ----------------------------------
1481
1482 procedure Lemma_Powers_Of_2_Increasing (M, N : Natural) is
1483 begin
1484 if M + 1 < N then
1485 Lemma_Powers_Of_2_Increasing (M + 1, N);
1486 end if;
1487 end Lemma_Powers_Of_2_Increasing;
1488
1489 -------------------
1490 -- Lemma_Rem_Abs --
1491 -------------------
1492
1493 procedure Lemma_Rem_Abs (X, Y : Big_Integer) is
1494 begin
1495 Lemma_Neg_Rem (X, Y);
1496 end Lemma_Rem_Abs;
1497
1498 ----------------------
1499 -- Lemma_Shift_Left --
1500 ----------------------
1501
1502 procedure Lemma_Shift_Left (X : Double_Uns; Shift : Natural) is
1503
1504 procedure Lemma_Mult_Pow2 (X : Double_Uns; I : Natural)
1505 with
1506 Ghost,
1507 Pre => I < Double_Size - 1,
1508 Post => X * Double_Uns'(2) ** I * Double_Uns'(2)
1509 = X * Double_Uns'(2) ** (I + 1);
1510
1511 procedure Lemma_Mult_Pow2 (X : Double_Uns; I : Natural) is
1512 Mul1 : constant Double_Uns := Double_Uns'(2) ** I;
1513 Mul2 : constant Double_Uns := Double_Uns'(2);
1514 Left : constant Double_Uns := X * Mul1 * Mul2;
1515 begin
1516 pragma Assert (Left = X * (Mul1 * Mul2));
1517 pragma Assert (Mul1 * Mul2 = Double_Uns'(2) ** (I + 1));
1518 end Lemma_Mult_Pow2;
1519
1520 XX : Double_Uns := X;
1521
1522 begin
1523 for J in 1 .. Shift loop
1524 declare
1525 Cur_XX : constant Double_Uns := XX;
1526 begin
1527 XX := Shift_Left (XX, 1);
1528 pragma Assert (XX = Cur_XX * Double_Uns'(2));
1529 Lemma_Mult_Pow2 (X, J - 1);
1530 end;
1531 Lemma_Double_Shift_Left (X, J - 1, 1);
1532 pragma Loop_Invariant (XX = Shift_Left (X, J));
1533 pragma Loop_Invariant (XX = X * Double_Uns'(2) ** J);
1534 end loop;
1535 end Lemma_Shift_Left;
1536
1537 -----------------------
1538 -- Lemma_Shift_Right --
1539 -----------------------
1540
1541 procedure Lemma_Shift_Right (X : Double_Uns; Shift : Natural) is
1542
1543 procedure Lemma_Div_Pow2 (X : Double_Uns; I : Natural)
1544 with
1545 Ghost,
1546 Pre => I < Double_Size - 1,
1547 Post => X / Double_Uns'(2) ** I / Double_Uns'(2)
1548 = X / Double_Uns'(2) ** (I + 1);
1549
1550 procedure Lemma_Quot_Rem (X, Div, Q, R : Double_Uns)
1551 with
1552 Ghost,
1553 Pre => Div /= 0
1554 and then X = Q * Div + R
1555 and then Q <= Double_Uns'Last / Div
1556 and then R <= Double_Uns'Last - Q * Div
1557 and then R < Div,
1558 Post => Q = X / Div;
1559 pragma Annotate (GNATprove, False_Positive, "postcondition might fail",
1560 "Q is the quotient of X by Div");
1561
1562 procedure Lemma_Div_Pow2 (X : Double_Uns; I : Natural) is
1563
1564 -- Local lemmas
1565
1566 procedure Lemma_Mult_Le (X, Y, Z : Double_Uns)
1567 with
1568 Ghost,
1569 Pre => X <= 1,
1570 Post => X * Z <= Z;
1571
1572 procedure Lemma_Mult_Le (X, Y, Z : Double_Uns) is null;
1573
1574 -- Local variables
1575
1576 Div1 : constant Double_Uns := Double_Uns'(2) ** I;
1577 Div2 : constant Double_Uns := Double_Uns'(2);
1578 Left : constant Double_Uns := X / Div1 / Div2;
1579 R2 : constant Double_Uns := X / Div1 - Left * Div2;
1580 pragma Assert (R2 <= Div2 - 1);
1581 R1 : constant Double_Uns := X - X / Div1 * Div1;
1582 pragma Assert (R1 < Div1);
1583
1584 -- Start of processing for Lemma_Div_Pow2
1585
1586 begin
1587 pragma Assert (X = Left * (Div1 * Div2) + R2 * Div1 + R1);
1588 Lemma_Mult_Le (R2, Div2 - 1, Div1);
1589 pragma Assert (R2 * Div1 + R1 < Div1 * Div2);
1590 Lemma_Quot_Rem (X, Div1 * Div2, Left, R2 * Div1 + R1);
1591 pragma Assert (Left = X / (Div1 * Div2));
1592 pragma Assert (Div1 * Div2 = Double_Uns'(2) ** (I + 1));
1593 end Lemma_Div_Pow2;
1594
1595 procedure Lemma_Quot_Rem (X, Div, Q, R : Double_Uns) is null;
1596
1597 XX : Double_Uns := X;
1598
1599 begin
1600 for J in 1 .. Shift loop
1601 declare
1602 Cur_XX : constant Double_Uns := XX;
1603 begin
1604 XX := Shift_Right (XX, 1);
1605 pragma Assert (XX = Cur_XX / Double_Uns'(2));
1606 Lemma_Div_Pow2 (X, J - 1);
1607 end;
1608 Lemma_Double_Shift_Right (X, J - 1, 1);
1609 pragma Loop_Invariant (XX = Shift_Right (X, J));
1610 pragma Loop_Invariant (XX = X / Double_Uns'(2) ** J);
1611 end loop;
1612 Lemma_Div_Commutation (X, Double_Uns'(2) ** Shift);
1613 end Lemma_Shift_Right;
1614
1615 ------------------------------
1616 -- Lemma_Shift_Without_Drop --
1617 ------------------------------
1618
1619 procedure Lemma_Shift_Without_Drop
1620 (X, Y : Double_Uns;
1621 Mask : Single_Uns;
1622 Shift : Natural)
1623 is
1624 pragma Unreferenced (Mask);
1625
1626 procedure Lemma_Bound
1627 with
1628 Pre => Shift <= Single_Size
1629 and then X <= 2**Single_Size
1630 * Double_Uns'(2**(Single_Size - Shift) - 1)
1631 + Single_Uns'(2**Single_Size - 1),
1632 Post => X <= 2**(Double_Size - Shift) - 1;
1633
1634 procedure Lemma_Exp_Pos (N : Integer)
1635 with
1636 Pre => N in 0 .. Double_Size - 1,
1637 Post => Double_Uns'(2**N) > 0;
1638
1639 -----------------------------
1640 -- Local lemma null bodies --
1641 -----------------------------
1642
1643 procedure Lemma_Bound is null;
1644 procedure Lemma_Exp_Pos (N : Integer) is null;
1645
1646 -- Start of processing for Lemma_Shift_Without_Drop
1647
1648 begin
1649 if Shift = 0 then
1650 pragma Assert (Big (Y) = Big_2xx (Shift) * Big (X));
1651 return;
1652 end if;
1653
1654 Lemma_Bound;
1655 Lemma_Exp_Pos (Double_Size - Shift);
1656 pragma Assert (X < 2**(Double_Size - Shift));
1657 pragma Assert (Big (X) < Big_2xx (Double_Size - Shift));
1658 pragma Assert (Y = 2**Shift * X);
1659 Lemma_Lt_Mult (Big (X), Big_2xx (Double_Size - Shift), Big_2xx (Shift),
1660 Big_2xx (Shift) * Big_2xx (Double_Size - Shift));
1661 pragma Assert (Big_2xx (Shift) * Big (X)
1662 < Big_2xx (Shift) * Big_2xx (Double_Size - Shift));
1663 Lemma_Powers_Of_2 (Shift, Double_Size - Shift);
1664 Lemma_Mult_Commutation (2**Shift, X, Y);
1665 pragma Assert (Big (Y) = Big_2xx (Shift) * Big (X));
1666 end Lemma_Shift_Without_Drop;
1667
1668 -------------------------------
1669 -- Multiply_With_Ovflo_Check --
1670 -------------------------------
1671
1672 function Multiply_With_Ovflo_Check (X, Y : Double_Int) return Double_Int is
1673 Xu : constant Double_Uns := abs X;
1674 Xhi : constant Single_Uns := Hi (Xu);
1675 Xlo : constant Single_Uns := Lo (Xu);
1676
1677 Yu : constant Double_Uns := abs Y;
1678 Yhi : constant Single_Uns := Hi (Yu);
1679 Ylo : constant Single_Uns := Lo (Yu);
1680
1681 T1, T2 : Double_Uns;
1682
1683 -- Local ghost variables
1684
1685 Mult : constant Big_Integer := abs (Big (X) * Big (Y)) with Ghost;
1686
1687 -- Local lemmas
1688
1689 procedure Prove_Both_Too_Large
1690 with
1691 Ghost,
1692 Pre => Xhi /= 0
1693 and then Yhi /= 0
1694 and then Mult =
1695 Big_2xxSingle * Big_2xxSingle * (Big (Double_Uns'(Xhi * Yhi)))
1696 + Big_2xxSingle * (Big (Double_Uns'(Xhi * Ylo)))
1697 + Big_2xxSingle * (Big (Double_Uns'(Xlo * Yhi)))
1698 + (Big (Double_Uns'(Xlo * Ylo))),
1699 Post => not In_Double_Int_Range (Big (X) * Big (Y));
1700
1701 procedure Prove_Final_Decomposition
1702 with
1703 Ghost,
1704 Pre => In_Double_Int_Range (Big (X) * Big (Y))
1705 and then Mult = Big_2xxSingle * Big (T2) + Big (Double_Uns (Lo (T1)))
1706 and then Hi (T2) = 0,
1707 Post => Mult = Big (Lo (T2) & Lo (T1));
1708
1709 procedure Prove_Neg_Int
1710 with
1711 Ghost,
1712 Pre => In_Double_Int_Range (Big (X) * Big (Y))
1713 and then Mult = Big (T2)
1714 and then ((X >= 0 and then Y < 0) or else (X < 0 and then Y >= 0)),
1715 Post => To_Neg_Int (T2) = X * Y;
1716
1717 procedure Prove_Pos_Int
1718 with
1719 Ghost,
1720 Pre => In_Double_Int_Range (Big (X) * Big (Y))
1721 and then Mult = Big (T2)
1722 and then ((X >= 0 and then Y >= 0) or else (X < 0 and then Y < 0)),
1723 Post => In_Double_Int_Range (Big (T2))
1724 and then To_Pos_Int (T2) = X * Y;
1725
1726 procedure Prove_Result_Too_Large
1727 with
1728 Ghost,
1729 Pre => Mult = Big_2xxSingle * Big (T2) + Big (Double_Uns (Lo (T1)))
1730 and then Hi (T2) /= 0,
1731 Post => not In_Double_Int_Range (Big (X) * Big (Y));
1732
1733 procedure Prove_Too_Large
1734 with
1735 Ghost,
1736 Pre => abs (Big (X) * Big (Y)) >= Big_2xxDouble,
1737 Post => not In_Double_Int_Range (Big (X) * Big (Y));
1738
1739 --------------------------
1740 -- Prove_Both_Too_Large --
1741 --------------------------
1742
1743 procedure Prove_Both_Too_Large is
1744 begin
1745 pragma Assert (Mult >=
1746 Big_2xxSingle * Big_2xxSingle * Big (Double_Uns'(Xhi * Yhi)));
1747 pragma Assert (Double_Uns (Xhi) * Double_Uns (Yhi) >= 1);
1748 pragma Assert (Mult >= Big_2xxSingle * Big_2xxSingle);
1749 Prove_Too_Large;
1750 end Prove_Both_Too_Large;
1751
1752 -------------------------------
1753 -- Prove_Final_Decomposition --
1754 -------------------------------
1755
1756 procedure Prove_Final_Decomposition is
1757 begin
1758 Lemma_Hi_Lo (T2, Hi (T2), Lo (T2));
1759 pragma Assert (Mult = Big_2xxSingle * Big (Double_Uns (Lo (T2)))
1760 + Big (Double_Uns (Lo (T1))));
1761 pragma Assert (Mult <= Big_2xxDouble_Minus_1);
1762 Lemma_Mult_Commutation (X, Y);
1763 pragma Assert (Mult = abs Big (X * Y));
1764 Lemma_Word_Commutation (Lo (T2));
1765 pragma Assert (Mult = Big (Double_Uns'(2 ** Single_Size)
1766 * Double_Uns (Lo (T2)))
1767 + Big (Double_Uns (Lo (T1))));
1768 Lemma_Add_Commutation (Double_Uns'(2 ** Single_Size)
1769 * Double_Uns (Lo (T2)),
1770 Lo (T1));
1771 pragma Assert (Mult = Big (Double_Uns'(2 ** Single_Size)
1772 * Double_Uns (Lo (T2)) + Lo (T1)));
1773 pragma Assert (Lo (T2) & Lo (T1) = Double_Uns'(2 ** Single_Size)
1774 * Double_Uns (Lo (T2)) + Lo (T1));
1775 end Prove_Final_Decomposition;
1776
1777 -------------------
1778 -- Prove_Neg_Int --
1779 -------------------
1780
1781 procedure Prove_Neg_Int is
1782 begin
1783 pragma Assert (X * Y <= 0);
1784 pragma Assert (Mult = -Big (X * Y));
1785 end Prove_Neg_Int;
1786
1787 -------------------
1788 -- Prove_Pos_Int --
1789 -------------------
1790
1791 procedure Prove_Pos_Int is
1792 begin
1793 pragma Assert (X * Y >= 0);
1794 pragma Assert (Mult = Big (X * Y));
1795 end Prove_Pos_Int;
1796
1797 ----------------------------
1798 -- Prove_Result_Too_Large --
1799 ----------------------------
1800
1801 procedure Prove_Result_Too_Large is
1802 begin
1803 pragma Assert (Mult >= Big_2xxSingle * Big (T2));
1804 Lemma_Hi_Lo (T2, Hi (T2), Lo (T2));
1805 pragma Assert (Mult >=
1806 Big_2xxSingle * Big_2xxSingle * Big (Double_Uns (Hi (T2))));
1807 pragma Assert (Double_Uns (Hi (T2)) >= 1);
1808 pragma Assert (Mult >= Big_2xxSingle * Big_2xxSingle);
1809 Prove_Too_Large;
1810 end Prove_Result_Too_Large;
1811
1812 ---------------------
1813 -- Prove_Too_Large --
1814 ---------------------
1815
1816 procedure Prove_Too_Large is null;
1817
1818 -- Start of processing for Multiply_With_Ovflo_Check
1819
1820 begin
1821 Lemma_Mult_Decomposition (Mult, Xu, Yu, Xhi, Xlo, Yhi, Ylo);
1822
1823 if Xhi /= 0 then
1824 if Yhi /= 0 then
1825 Prove_Both_Too_Large;
1826 Raise_Error;
1827 else
1828 T2 := Xhi * Ylo;
1829 pragma Assert (Big (T2) = Big (Double_Uns'(Xhi * Ylo))
1830 + Big (Double_Uns'(Xlo * Yhi)));
1831 end if;
1832
1833 elsif Yhi /= 0 then
1834 T2 := Xlo * Yhi;
1835 pragma Assert (Big (T2) = Big (Double_Uns'(Xhi * Ylo))
1836 + Big (Double_Uns'(Xlo * Yhi)));
1837
1838 else -- Yhi = Xhi = 0
1839 T2 := 0;
1840 end if;
1841
1842 -- Here we have T2 set to the contribution to the upper half of the
1843 -- result from the upper halves of the input values.
1844
1845 T1 := Xlo * Ylo;
1846
1847 pragma Assert (Big (T2) = Big (Double_Uns'(Xhi * Ylo))
1848 + Big (Double_Uns'(Xlo * Yhi)));
1849 Lemma_Mult_Distribution (Big_2xxSingle, Big (Double_Uns'(Xhi * Ylo)),
1850 Big (Double_Uns'(Xlo * Yhi)));
1851 pragma Assert (Mult = Big_2xxSingle * Big (T2) + Big (T1));
1852 Lemma_Add_Commutation (T2, Hi (T1));
1853 pragma Assert
1854 (Big (T2 + Hi (T1)) = Big (T2) + Big (Double_Uns (Hi (T1))));
1855
1856 T2 := T2 + Hi (T1);
1857
1858 Lemma_Hi_Lo (T1, Hi (T1), Lo (T1));
1859 pragma Assert
1860 (Mult = Big_2xxSingle * Big (T2) + Big (Double_Uns (Lo (T1))));
1861
1862 if Hi (T2) /= 0 then
1863 Prove_Result_Too_Large;
1864 Raise_Error;
1865 end if;
1866
1867 Prove_Final_Decomposition;
1868
1869 T2 := Lo (T2) & Lo (T1);
1870
1871 pragma Assert (Mult = Big (T2));
1872
1873 if X >= 0 then
1874 if Y >= 0 then
1875 Prove_Pos_Int;
1876 return To_Pos_Int (T2);
1877 pragma Annotate (CodePeer, Intentional, "precondition",
1878 "Intentional Unsigned->Signed conversion");
1879 else
1880 Prove_Neg_Int;
1881 Lemma_Abs_Range (Big (X) * Big (Y));
1882 return To_Neg_Int (T2);
1883 end if;
1884 else -- X < 0
1885 if Y < 0 then
1886 Prove_Pos_Int;
1887 return To_Pos_Int (T2);
1888 pragma Annotate (CodePeer, Intentional, "precondition",
1889 "Intentional Unsigned->Signed conversion");
1890 else
1891 Prove_Neg_Int;
1892 Lemma_Abs_Range (Big (X) * Big (Y));
1893 return To_Neg_Int (T2);
1894 end if;
1895 end if;
1896
1897 end Multiply_With_Ovflo_Check;
1898
1899 -----------------
1900 -- Raise_Error --
1901 -----------------
1902
1903 procedure Raise_Error is
1904 begin
1905 raise Constraint_Error with "Double arithmetic overflow";
1906 end Raise_Error;
1907
1908 -------------------
1909 -- Scaled_Divide --
1910 -------------------
1911
1912 pragma Annotate (Gnatcheck, Exempt_On, "Metrics_Cyclomatic_Complexity",
1913 "limit exceeded due to proof code");
1914 procedure Scaled_Divide
1915 (X, Y, Z : Double_Int;
1916 Q, R : out Double_Int;
1917 Round : Boolean)
1918 is
1919 Xu : constant Double_Uns := abs X;
1920 Xhi : constant Single_Uns := Hi (Xu);
1921 Xlo : constant Single_Uns := Lo (Xu);
1922
1923 Yu : constant Double_Uns := abs Y;
1924 Yhi : constant Single_Uns := Hi (Yu);
1925 Ylo : constant Single_Uns := Lo (Yu);
1926
1927 Zu : Double_Uns := abs Z;
1928 Zhi : Single_Uns := Hi (Zu);
1929 Zlo : Single_Uns := Lo (Zu);
1930
1931 D : array (1 .. 4) of Single_Uns with Relaxed_Initialization;
1932 -- The dividend, four digits (D(1) is high order)
1933
1934 Qd : array (1 .. 2) of Single_Uns with Relaxed_Initialization;
1935 -- The quotient digits, two digits (Qd(1) is high order)
1936
1937 S1, S2, S3 : Single_Uns;
1938 -- Value to subtract, three digits (S1 is high order)
1939
1940 Qu : Double_Uns;
1941 Ru : Double_Uns;
1942 -- Unsigned quotient and remainder
1943
1944 Mask : Single_Uns;
1945 -- Mask of bits used to compute the scaling factor below
1946
1947 Scale : Natural;
1948 -- Scaling factor used for multiple-precision divide. Dividend and
1949 -- Divisor are multiplied by 2 ** Scale, and the final remainder is
1950 -- divided by the scaling factor. The reason for this scaling is to
1951 -- allow more accurate estimation of quotient digits.
1952
1953 Shift : Natural;
1954 -- Shift factor used to compute the scaling factor above
1955
1956 T1, T2, T3 : Double_Uns;
1957 -- Temporary values
1958
1959 -- Local ghost variables
1960
1961 Mult : constant Big_Natural := abs (Big (X) * Big (Y)) with Ghost;
1962 Quot : Big_Integer with Ghost;
1963 Big_R : Big_Integer with Ghost;
1964 Big_Q : Big_Integer with Ghost;
1965 Inter : Natural with Ghost;
1966
1967 -- Local ghost functions
1968
1969 function Is_Mult_Decomposition
1970 (D1, D2, D3, D4 : Big_Integer)
1971 return Boolean
1972 is
1973 (Mult = Big_2xxSingle * Big_2xxSingle * Big_2xxSingle * D1
1974 + Big_2xxSingle * Big_2xxSingle * D2
1975 + Big_2xxSingle * D3
1976 + D4)
1977 with
1978 Ghost,
1979 Annotate => (GNATprove, Inline_For_Proof);
1980
1981 function Is_Scaled_Mult_Decomposition
1982 (D1, D2, D3, D4 : Big_Integer)
1983 return Boolean
1984 is
1985 (Mult * Big_2xx (Scale)
1986 = Big_2xxSingle * Big_2xxSingle * Big_2xxSingle * D1
1987 + Big_2xxSingle * Big_2xxSingle * D2
1988 + Big_2xxSingle * D3
1989 + D4)
1990 with
1991 Ghost,
1992 Annotate => (GNATprove, Inline_For_Proof),
1993 Pre => Scale < Double_Size;
1994
1995 -- Local lemmas
1996
1997 procedure Prove_Dividend_Scaling
1998 with
1999 Ghost,
2000 Pre => D'Initialized
2001 and then Scale <= Single_Size
2002 and then Is_Mult_Decomposition (Big (Double_Uns (D (1))),
2003 Big (Double_Uns (D (2))),
2004 Big (Double_Uns (D (3))),
2005 Big (Double_Uns (D (4))))
2006 and then Big (D (1) & D (2)) * Big_2xx (Scale) < Big_2xxDouble
2007 and then T1 = Shift_Left (D (1) & D (2), Scale)
2008 and then T2 = Shift_Left (Double_Uns (D (3)), Scale)
2009 and then T3 = Shift_Left (Double_Uns (D (4)), Scale),
2010 Post => Is_Scaled_Mult_Decomposition
2011 (Big (Double_Uns (Hi (T1))),
2012 Big (Double_Uns (Lo (T1) or Hi (T2))),
2013 Big (Double_Uns (Lo (T2) or Hi (T3))),
2014 Big (Double_Uns (Lo (T3))));
2015 -- Proves the scaling of the 4-digit dividend actually multiplies it by
2016 -- 2**Scale.
2017
2018 procedure Prove_Multiplication (Q : Single_Uns)
2019 with
2020 Ghost,
2021 Pre => T1 = Q * Lo (Zu)
2022 and then T2 = Q * Hi (Zu)
2023 and then S3 = Lo (T1)
2024 and then T3 = Hi (T1) + Lo (T2)
2025 and then S2 = Lo (T3)
2026 and then S1 = Hi (T3) + Hi (T2),
2027 Post => Big3 (S1, S2, S3) = Big (Double_Uns (Q)) * Big (Zu);
2028 -- Proves correctness of the multiplication of divisor by quotient to
2029 -- compute amount to subtract.
2030
2031 procedure Prove_Mult_Decomposition_Split2
2032 (D1, D2, D2_Hi, D2_Lo, D3, D4 : Big_Integer)
2033 with
2034 Ghost,
2035 Pre => Is_Mult_Decomposition (D1, D2, D3, D4)
2036 and then D2 = Big_2xxSingle * D2_Hi + D2_Lo,
2037 Post => Is_Mult_Decomposition (D1 + D2_Hi, D2_Lo, D3, D4);
2038 -- Proves decomposition of Mult after splitting second component
2039
2040 procedure Prove_Mult_Decomposition_Split3
2041 (D1, D2, D3, D3_Hi, D3_Lo, D4 : Big_Integer)
2042 with
2043 Ghost,
2044 Pre => Is_Mult_Decomposition (D1, D2, D3, D4)
2045 and then D3 = Big_2xxSingle * D3_Hi + D3_Lo,
2046 Post => Is_Mult_Decomposition (D1, D2 + D3_Hi, D3_Lo, D4);
2047 -- Proves decomposition of Mult after splitting third component
2048
2049 procedure Prove_Negative_Dividend
2050 with
2051 Ghost,
2052 Pre => Z /= 0
2053 and then Big (Qu) = abs Big_Q
2054 and then In_Double_Int_Range (Big_Q)
2055 and then Big (Ru) = abs Big_R
2056 and then ((X >= 0 and Y < 0) or (X < 0 and Y >= 0))
2057 and then Big_Q =
2058 (if Round then Round_Quotient (Big (X) * Big (Y), Big (Z),
2059 Big (X) * Big (Y) / Big (Z),
2060 Big (X) * Big (Y) rem Big (Z))
2061 else Big (X) * Big (Y) / Big (Z))
2062 and then Big_R = Big (X) * Big (Y) rem Big (Z),
2063 Post =>
2064 (if Z > 0 then Big_Q <= Big_0
2065 and then In_Double_Int_Range (-Big (Qu))
2066 else Big_Q >= Big_0
2067 and then In_Double_Int_Range (Big (Qu)))
2068 and then In_Double_Int_Range (-Big (Ru));
2069 -- Proves the sign of rounded quotient when dividend is non-positive
2070
2071 procedure Prove_Overflow
2072 with
2073 Ghost,
2074 Pre => Z /= 0
2075 and then Mult >= Big_2xxDouble * Big (Double_Uns'(abs Z)),
2076 Post => not In_Double_Int_Range (Big (X) * Big (Y) / Big (Z))
2077 and then not In_Double_Int_Range
2078 (Round_Quotient (Big (X) * Big (Y), Big (Z),
2079 Big (X) * Big (Y) / Big (Z),
2080 Big (X) * Big (Y) rem Big (Z)));
2081 -- Proves overflow case when the quotient has at least 3 digits
2082
2083 procedure Prove_Positive_Dividend
2084 with
2085 Ghost,
2086 Pre => Z /= 0
2087 and then Big (Qu) = abs Big_Q
2088 and then In_Double_Int_Range (Big_Q)
2089 and then Big (Ru) = abs Big_R
2090 and then ((X >= 0 and Y >= 0) or (X < 0 and Y < 0))
2091 and then Big_Q =
2092 (if Round then Round_Quotient (Big (X) * Big (Y), Big (Z),
2093 Big (X) * Big (Y) / Big (Z),
2094 Big (X) * Big (Y) rem Big (Z))
2095 else Big (X) * Big (Y) / Big (Z))
2096 and then Big_R = Big (X) * Big (Y) rem Big (Z),
2097 Post =>
2098 (if Z > 0 then Big_Q >= Big_0
2099 and then In_Double_Int_Range (Big (Qu))
2100 else Big_Q <= Big_0
2101 and then In_Double_Int_Range (-Big (Qu)))
2102 and then In_Double_Int_Range (Big (Ru));
2103 -- Proves the sign of rounded quotient when dividend is non-negative
2104
2105 procedure Prove_Qd_Calculation_Part_1 (J : Integer)
2106 with
2107 Ghost,
2108 Pre => J in 1 .. 2
2109 and then D'Initialized
2110 and then D (J) < Zhi
2111 and then Hi (Zu) = Zhi
2112 and then Qd (J)'Initialized
2113 and then Qd (J) = Lo ((D (J) & D (J + 1)) / Zhi),
2114 Post => Big (Double_Uns (Qd (J))) >=
2115 Big3 (D (J), D (J + 1), D (J + 2)) / Big (Zu);
2116 -- When dividing 3 digits by 2 digits, proves the initial calculation
2117 -- of the quotient given by dividing the first 2 digits of the dividend
2118 -- by the first digit of the divisor is not an underestimate (so
2119 -- readjusting down works).
2120
2121 procedure Prove_Q_Too_Big
2122 with
2123 Ghost,
2124 Pre => In_Double_Int_Range (Big_Q)
2125 and then abs Big_Q = Big_2xxDouble,
2126 Post => False;
2127 -- Proves the inconsistency when Q is equal to Big_2xx64
2128
2129 procedure Prove_Rescaling
2130 with
2131 Ghost,
2132 Pre => Scale <= Single_Size
2133 and then Z /= 0
2134 and then Mult * Big_2xx (Scale) = Big (Zu) * Big (Qu) + Big (Ru)
2135 and then Big (Ru) < Big (Zu)
2136 and then Big (Zu) = Big (Double_Uns'(abs Z)) * Big_2xx (Scale)
2137 and then Quot = Big (X) * Big (Y) / Big (Z)
2138 and then Big_R = Big (X) * Big (Y) rem Big (Z),
2139 Post => abs Quot = Big (Qu)
2140 and then abs Big_R = Big (Shift_Right (Ru, Scale));
2141 -- Proves scaling back only the remainder is the right thing to do after
2142 -- computing the scaled division.
2143
2144 procedure Prove_Rounding_Case
2145 with
2146 Ghost,
2147 Pre => Z /= 0
2148 and then Quot = Big (X) * Big (Y) / Big (Z)
2149 and then Big_R = Big (X) * Big (Y) rem Big (Z)
2150 and then Big_Q =
2151 Round_Quotient (Big (X) * Big (Y), Big (Z), Quot, Big_R)
2152 and then Big (Ru) = abs Big_R
2153 and then Big (Zu) = Big (Double_Uns'(abs Z)),
2154 Post => abs Big_Q =
2155 (if Ru > (Zu - Double_Uns'(1)) / Double_Uns'(2)
2156 then abs Quot + 1
2157 else abs Quot);
2158 -- Proves correctness of the rounding of the unsigned quotient
2159
2160 procedure Prove_Scaled_Mult_Decomposition_Regroup24
2161 (D1, D2, D3, D4 : Big_Integer)
2162 with
2163 Ghost,
2164 Pre => Scale < Double_Size
2165 and then Is_Scaled_Mult_Decomposition (D1, D2, D3, D4),
2166 Post => Is_Scaled_Mult_Decomposition
2167 (0, Big_2xxSingle * D1 + D2, 0, Big_2xxSingle * D3 + D4);
2168 -- Proves scaled decomposition of Mult after regrouping on second and
2169 -- fourth component.
2170
2171 procedure Prove_Scaled_Mult_Decomposition_Regroup3
2172 (D1, D2, D3, D4 : Single_Uns)
2173 with
2174 Ghost,
2175 Pre => Scale < Double_Size
2176 and then Is_Scaled_Mult_Decomposition
2177 (Big (Double_Uns (D1)), Big (Double_Uns (D2)),
2178 Big (Double_Uns (D3)), Big (Double_Uns (D4))),
2179 Post => Is_Scaled_Mult_Decomposition (0, 0, Big3 (D1, D2, D3),
2180 Big (Double_Uns (D4)));
2181 -- Proves scaled decomposition of Mult after regrouping on third
2182 -- component.
2183
2184 procedure Prove_Sign_R
2185 with
2186 Ghost,
2187 Pre => Z /= 0 and then Big_R = Big (X) * Big (Y) rem Big (Z),
2188 Post => In_Double_Int_Range (Big_R);
2189
2190 procedure Prove_Signs
2191 with
2192 Ghost,
2193 Pre => Z /= 0
2194 and then Quot = Big (X) * Big (Y) / Big (Z)
2195 and then Big_R = Big (X) * Big (Y) rem Big (Z)
2196 and then Big_Q =
2197 (if Round then
2198 Round_Quotient (Big (X) * Big (Y), Big (Z), Quot, Big_R)
2199 else Quot)
2200 and then Big (Ru) = abs Big_R
2201 and then Big (Qu) = abs Big_Q
2202 and then In_Double_Int_Range (Big_Q)
2203 and then In_Double_Int_Range (Big_R)
2204 and then R =
2205 (if (X >= 0) = (Y >= 0) then To_Pos_Int (Ru) else To_Neg_Int (Ru))
2206 and then Q =
2207 (if ((X >= 0) = (Y >= 0)) = (Z >= 0) then To_Pos_Int (Qu)
2208 else To_Neg_Int (Qu)), -- need to ensure To_Pos_Int precondition
2209 Post => Big (R) = Big_R and then Big (Q) = Big_Q;
2210 -- Proves final signs match the intended result after the unsigned
2211 -- division is done.
2212
2213 procedure Prove_Z_Low
2214 with
2215 Ghost,
2216 Pre => Z /= 0
2217 and then D'Initialized
2218 and then Hi (abs Z) = 0
2219 and then Lo (abs Z) = Zlo
2220 and then Mult =
2221 Big_2xxSingle * Big_2xxSingle * Big (Double_Uns (D (2)))
2222 + Big_2xxSingle * Big (Double_Uns (D (3)))
2223 + Big (Double_Uns (D (4)))
2224 and then D (2) < Zlo
2225 and then Quot = (Big (X) * Big (Y)) / Big (Z)
2226 and then Big_R = (Big (X) * Big (Y)) rem Big (Z)
2227 and then T1 = D (2) & D (3)
2228 and then T2 = Lo (T1 rem Zlo) & D (4)
2229 and then Qu = Lo (T1 / Zlo) & Lo (T2 / Zlo)
2230 and then Ru = T2 rem Zlo,
2231 Post => Big (Qu) = abs Quot
2232 and then Big (Ru) = abs Big_R;
2233 -- Proves the case where the divisor is only one digit
2234
2235 ----------------------------
2236 -- Prove_Dividend_Scaling --
2237 ----------------------------
2238
2239 procedure Prove_Dividend_Scaling is
2240 Big_D12 : constant Big_Integer :=
2241 Big_2xx (Scale) * Big (D (1) & D (2));
2242 Big_T1 : constant Big_Integer := Big (T1);
2243 Big_D3 : constant Big_Integer :=
2244 Big_2xx (Scale) * Big (Double_Uns (D (3)));
2245 Big_T2 : constant Big_Integer := Big (T2);
2246 Big_D4 : constant Big_Integer :=
2247 Big_2xx (Scale) * Big (Double_Uns (D (4)));
2248 Big_T3 : constant Big_Integer := Big (T3);
2249
2250 begin
2251 Lemma_Shift_Left (D (1) & D (2), Scale);
2252 Lemma_Ge_Mult (Big_2xxSingle, Big_2xx (Scale), Big_2xxSingle,
2253 Big_2xxSingle * Big_2xx (Scale));
2254 Lemma_Lt_Mult (Big (Double_Uns (D (3))), Big_2xxSingle,
2255 Big_2xx (Scale), Big_2xxDouble);
2256 Lemma_Shift_Left (Double_Uns (D (3)), Scale);
2257 Lemma_Lt_Mult (Big (Double_Uns (D (4))), Big_2xxSingle,
2258 Big_2xx (Scale), Big_2xxDouble);
2259 Lemma_Shift_Left (Double_Uns (D (4)), Scale);
2260 Lemma_Hi_Lo (D (1) & D (2), D (1), D (2));
2261 pragma Assert (Mult * Big_2xx (Scale) =
2262 Big_2xxSingle * Big_2xxSingle * Big_D12
2263 + Big_2xxSingle * Big_D3
2264 + Big_D4);
2265 pragma Assert (Big_2xx (Scale) > 0);
2266 declare
2267 Two_xx_Scale : constant Double_Uns := Double_Uns'(2 ** Scale);
2268 D12 : constant Double_Uns := D (1) & D (2);
2269 begin
2270 pragma Assert (Big_2xx (Scale) * Big (D12) < Big_2xxDouble);
2271 pragma Assert (Big (Two_xx_Scale) * Big (D12) < Big_2xxDouble);
2272 Lemma_Mult_Commutation (Two_xx_Scale, D12, T1);
2273 end;
2274 pragma Assert (Big_D12 = Big_T1);
2275 pragma Assert (Big_2xxSingle * Big_2xxSingle * Big_D12
2276 = Big_2xxSingle * Big_2xxSingle * Big_T1);
2277 Lemma_Mult_Commutation (2 ** Scale, Double_Uns (D (3)), T2);
2278 pragma Assert (Big_D3 = Big_T2);
2279 pragma Assert (Big_2xxSingle * Big_D3 = Big_2xxSingle * Big_T2);
2280 Lemma_Mult_Commutation (2 ** Scale, Double_Uns (D (4)), T3);
2281 pragma Assert
2282 (Is_Scaled_Mult_Decomposition (0, Big_T1, Big_T2, Big_T3));
2283 Lemma_Hi_Lo (T1, Hi (T1), Lo (T1));
2284 Lemma_Hi_Lo (T2, Hi (T2), Lo (T2));
2285 Lemma_Hi_Lo (T3, Hi (T3), Lo (T3));
2286 Lemma_Mult_Distribution (Big_2xxSingle * Big_2xxSingle,
2287 Big_2xxSingle * Big (Double_Uns (Hi (T1))),
2288 Big (Double_Uns (Lo (T1))));
2289 Lemma_Mult_Distribution (Big_2xxSingle,
2290 Big_2xxSingle * Big (Double_Uns (Hi (T2))),
2291 Big (Double_Uns (Lo (T2))));
2292 Lemma_Mult_Distribution (Big_2xxSingle * Big_2xxSingle,
2293 Big (Double_Uns (Lo (T1))),
2294 Big (Double_Uns (Hi (T2))));
2295 Lemma_Mult_Distribution (Big_2xxSingle,
2296 Big (Double_Uns (Lo (T2))),
2297 Big (Double_Uns (Hi (T3))));
2298 Lemma_Mult_Distribution (Big_2xxSingle * Big_2xxSingle,
2299 Big (Double_Uns (Lo (T1))),
2300 Big (Double_Uns (Hi (T2))));
2301 pragma Assert (Double_Uns (Lo (T1) or Hi (T2)) =
2302 Double_Uns (Lo (T1)) + Double_Uns (Hi (T2)));
2303 pragma Assert (Double_Uns (Lo (T2) or Hi (T3)) =
2304 Double_Uns (Lo (T2)) + Double_Uns (Hi (T3)));
2305 Lemma_Add_Commutation (Double_Uns (Lo (T1)), Hi (T2));
2306 Lemma_Add_Commutation (Double_Uns (Lo (T2)), Hi (T3));
2307 end Prove_Dividend_Scaling;
2308
2309 --------------------------
2310 -- Prove_Multiplication --
2311 --------------------------
2312
2313 procedure Prove_Multiplication (Q : Single_Uns) is
2314 begin
2315 Lemma_Hi_Lo (Zu, Hi (Zu), Lo (Zu));
2316 Lemma_Hi_Lo (T1, Hi (T1), S3);
2317 Lemma_Hi_Lo (T2, Hi (T2), Lo (T2));
2318 Lemma_Hi_Lo (T3, Hi (T3), S2);
2319 Lemma_Mult_Commutation (Double_Uns (Q), Double_Uns (Lo (Zu)), T1);
2320 Lemma_Mult_Commutation (Double_Uns (Q), Double_Uns (Hi (Zu)), T2);
2321 Lemma_Mult_Distribution (Big (Double_Uns (Q)),
2322 Big_2xxSingle * Big (Double_Uns (Hi (Zu))),
2323 Big (Double_Uns (Lo (Zu))));
2324 Lemma_Substitution
2325 (Big (Double_Uns (Q)) * Big (Zu),
2326 Big (Double_Uns (Q)),
2327 Big (Zu),
2328 Big_2xxSingle * Big (Double_Uns (Hi (Zu)))
2329 + Big (Double_Uns (Lo (Zu))),
2330 Big_0);
2331 pragma Assert (Big (Double_Uns (Q)) * Big (Zu) =
2332 Big_2xxSingle * Big_2xxSingle * Big (Double_Uns (Hi (T2)))
2333 + Big_2xxSingle * Big (Double_Uns (Lo (T2)))
2334 + Big_2xxSingle * Big (Double_Uns (Hi (T1)))
2335 + Big (Double_Uns (S3)));
2336 Lemma_Add_Commutation (Double_Uns (Lo (T2)), Hi (T1));
2337 pragma Assert
2338 (By (Big (Double_Uns (Q)) * Big (Zu) =
2339 Big_2xxSingle * Big_2xxSingle * Big (Double_Uns (Hi (T2)))
2340 + Big_2xxSingle * Big (T3)
2341 + Big (Double_Uns (S3)),
2342 By (Big_2xxSingle * Big (Double_Uns (Lo (T2)))
2343 + Big_2xxSingle * Big (Double_Uns (Hi (T1)))
2344 = Big_2xxSingle * Big (T3),
2345 Double_Uns (Lo (T2))
2346 + Double_Uns (Hi (T1)) = T3)));
2347 pragma Assert (Double_Uns (Hi (T3)) + Hi (T2) = Double_Uns (S1));
2348 Lemma_Add_Commutation (Double_Uns (Hi (T3)), Hi (T2));
2349 pragma Assert
2350 (Big (Double_Uns (Hi (T3))) + Big (Double_Uns (Hi (T2))) =
2351 Big (Double_Uns (S1)));
2352 Lemma_Mult_Distribution (Big_2xxSingle * Big_2xxSingle,
2353 Big (Double_Uns (Hi (T3))),
2354 Big (Double_Uns (Hi (T2))));
2355 end Prove_Multiplication;
2356
2357 -------------------------------------
2358 -- Prove_Mult_Decomposition_Split2 --
2359 -------------------------------------
2360
2361 procedure Prove_Mult_Decomposition_Split2
2362 (D1, D2, D2_Hi, D2_Lo, D3, D4 : Big_Integer)
2363 is null;
2364
2365 -------------------------------------
2366 -- Prove_Mult_Decomposition_Split3 --
2367 -------------------------------------
2368
2369 procedure Prove_Mult_Decomposition_Split3
2370 (D1, D2, D3, D3_Hi, D3_Lo, D4 : Big_Integer)
2371 is null;
2372
2373 -----------------------------
2374 -- Prove_Negative_Dividend --
2375 -----------------------------
2376
2377 procedure Prove_Negative_Dividend is
2378 begin
2379 Lemma_Mult_Non_Positive (Big (X), Big (Y));
2380 end Prove_Negative_Dividend;
2381
2382 --------------------
2383 -- Prove_Overflow --
2384 --------------------
2385
2386 procedure Prove_Overflow is
2387 begin
2388 Lemma_Div_Ge (Mult, Big_2xxDouble, Big (Double_Uns'(abs Z)));
2389 Lemma_Abs_Commutation (Z);
2390 Lemma_Abs_Div_Commutation (Big (X) * Big (Y), Big (Z));
2391 end Prove_Overflow;
2392
2393 -----------------------------
2394 -- Prove_Positive_Dividend --
2395 -----------------------------
2396
2397 procedure Prove_Positive_Dividend is
2398 begin
2399 Lemma_Mult_Non_Negative (Big (X), Big (Y));
2400 end Prove_Positive_Dividend;
2401
2402 ---------------------------------
2403 -- Prove_Qd_Calculation_Part_1 --
2404 ---------------------------------
2405
2406 procedure Prove_Qd_Calculation_Part_1 (J : Integer) is
2407 begin
2408 Lemma_Hi_Lo (D (J) & D (J + 1), D (J), D (J + 1));
2409 Lemma_Lt_Commutation (Double_Uns (D (J)), Double_Uns (Zhi));
2410 Lemma_Gt_Mult (Big (Double_Uns (Zhi)),
2411 Big (Double_Uns (D (J))) + 1,
2412 Big_2xxSingle, Big (D (J) & D (J + 1)));
2413 Lemma_Div_Lt
2414 (Big (D (J) & D (J + 1)), Big_2xxSingle, Big (Double_Uns (Zhi)));
2415 Lemma_Div_Commutation (D (J) & D (J + 1), Double_Uns (Zhi));
2416 Lemma_Lo_Is_Ident ((D (J) & D (J + 1)) / Zhi);
2417 Lemma_Div_Definition (D (J) & D (J + 1), Zhi, Double_Uns (Qd (J)),
2418 (D (J) & D (J + 1)) rem Zhi);
2419 Lemma_Lt_Commutation
2420 ((D (J) & D (J + 1)) rem Zhi, Double_Uns (Zhi));
2421 Lemma_Gt_Mult
2422 ((Big (Double_Uns (Qd (J))) + 1) * Big (Double_Uns (Zhi)),
2423 Big (D (J) & D (J + 1)) + 1, Big_2xxSingle,
2424 Big3 (D (J), D (J + 1), D (J + 2)));
2425 Lemma_Hi_Lo (Zu, Zhi, Lo (Zu));
2426 Lemma_Gt_Mult (Big (Zu), Big_2xxSingle * Big (Double_Uns (Zhi)),
2427 Big (Double_Uns (Qd (J))) + 1,
2428 Big3 (D (J), D (J + 1), D (J + 2)));
2429 Lemma_Div_Lt (Big3 (D (J), D (J + 1), D (J + 2)),
2430 Big (Double_Uns (Qd (J))) + 1, Big (Zu));
2431 end Prove_Qd_Calculation_Part_1;
2432
2433 ---------------------
2434 -- Prove_Q_Too_Big --
2435 ---------------------
2436
2437 procedure Prove_Q_Too_Big is
2438 begin
2439 pragma Assert (Big_Q = Big_2xxDouble or Big_Q = -Big_2xxDouble);
2440 Lemma_Not_In_Range_Big2xx64;
2441 end Prove_Q_Too_Big;
2442
2443 ---------------------
2444 -- Prove_Rescaling --
2445 ---------------------
2446
2447 procedure Prove_Rescaling is
2448 begin
2449 Lemma_Div_Lt (Big (Ru), Big (Double_Uns'(abs Z)), Big_2xx (Scale));
2450 Lemma_Div_Eq (Mult, Big (Double_Uns'(abs Z)) * Big (Qu),
2451 Big_2xx (Scale), Big (Ru));
2452 Lemma_Rev_Div_Definition (Mult, Big (Double_Uns'(abs Z)),
2453 Big (Qu), Big (Ru) / Big_2xx (Scale));
2454 Lemma_Abs_Div_Commutation (Big (X) * Big (Y), Big (Z));
2455 Lemma_Abs_Rem_Commutation (Big (X) * Big (Y), Big (Z));
2456 Lemma_Abs_Commutation (Z);
2457 Lemma_Shift_Right (Ru, Scale);
2458 end Prove_Rescaling;
2459
2460 -------------------------
2461 -- Prove_Rounding_Case --
2462 -------------------------
2463
2464 procedure Prove_Rounding_Case is
2465 begin
2466 if Same_Sign (Big (X) * Big (Y), Big (Z)) then
2467 pragma Assert
2468 (abs Big_Q =
2469 (if Ru > (Zu - Double_Uns'(1)) / Double_Uns'(2)
2470 then abs Quot + 1
2471 else abs Quot));
2472 end if;
2473 end Prove_Rounding_Case;
2474
2475 -----------------------------------------------
2476 -- Prove_Scaled_Mult_Decomposition_Regroup24 --
2477 -----------------------------------------------
2478
2479 procedure Prove_Scaled_Mult_Decomposition_Regroup24
2480 (D1, D2, D3, D4 : Big_Integer)
2481 is null;
2482
2483 ----------------------------------------------
2484 -- Prove_Scaled_Mult_Decomposition_Regroup3 --
2485 ----------------------------------------------
2486
2487 procedure Prove_Scaled_Mult_Decomposition_Regroup3
2488 (D1, D2, D3, D4 : Single_Uns)
2489 is null;
2490
2491 ------------------
2492 -- Prove_Sign_R --
2493 ------------------
2494
2495 procedure Prove_Sign_R is
2496 begin
2497 pragma Assert (In_Double_Int_Range (Big (Z)));
2498 end Prove_Sign_R;
2499
2500 -----------------
2501 -- Prove_Signs --
2502 -----------------
2503
2504 procedure Prove_Signs is null;
2505
2506 -----------------
2507 -- Prove_Z_Low --
2508 -----------------
2509
2510 procedure Prove_Z_Low is
2511 begin
2512 Lemma_Hi_Lo (T1, D (2), D (3));
2513 Lemma_Add_Commutation (Double_Uns (D (2)), 1);
2514 pragma Assert
2515 (Big (Double_Uns (D (2))) + 1 <= Big (Double_Uns (Zlo)));
2516 Lemma_Div_Definition (T1, Zlo, T1 / Zlo, T1 rem Zlo);
2517 pragma Assert
2518 (By (Lo (T1 rem Zlo) = Hi (T2),
2519 By (Double_Uns (Lo (T1 rem Zlo)) = T1 rem Zlo,
2520 T1 rem Zlo <= Double_Uns (Zlo))));
2521 Lemma_Hi_Lo (T2, Lo (T1 rem Zlo), D (4));
2522 pragma Assert (T1 rem Zlo < Double_Uns (Zlo));
2523 pragma Assert (T1 rem Zlo + Double_Uns'(1) <= Double_Uns (Zlo));
2524 Lemma_Ge_Commutation (Double_Uns (Zlo), T1 rem Zlo + Double_Uns'(1));
2525 Lemma_Add_Commutation (T1 rem Zlo, 1);
2526 pragma Assert (Big (T1 rem Zlo) + 1 <= Big (Double_Uns (Zlo)));
2527 Lemma_Div_Definition (T2, Zlo, T2 / Zlo, Ru);
2528 pragma Assert
2529 (By (Big_2xxSingle * Big (Double_Uns (D (2)))
2530 + Big (Double_Uns (D (3)))
2531 < Big_2xxSingle * (Big (Double_Uns (D (2))) + 1),
2532 Mult = Big (Double_Uns (Zlo)) *
2533 (Big_2xxSingle * Big (T1 / Zlo) + Big (T2 / Zlo)) + Big (Ru)));
2534 Lemma_Div_Lt (Big (T1), Big_2xxSingle, Big (Double_Uns (Zlo)));
2535 Lemma_Div_Commutation (T1, Double_Uns (Zlo));
2536 Lemma_Lo_Is_Ident (T1 / Zlo);
2537 pragma Assert
2538 (Big (T2) <= Big_2xxSingle * (Big (Double_Uns (Zlo)) - 1)
2539 + Big (Double_Uns (D (4))));
2540 Lemma_Hi_Lo (Qu, Lo (T1 / Zlo), Lo (T2 / Zlo));
2541 Lemma_Div_Lt (Big (T2), Big_2xxSingle, Big (Double_Uns (Zlo)));
2542 Lemma_Div_Commutation (T2, Double_Uns (Zlo));
2543 Lemma_Lo_Is_Ident (T2 / Zlo);
2544 Lemma_Substitution (Mult, Big (Double_Uns (Zlo)),
2545 Big_2xxSingle * Big (T1 / Zlo) + Big (T2 / Zlo),
2546 Big (Qu), Big (Ru));
2547 pragma Assert
2548 (By (Ru < Double_Uns (Zlo), Ru = T2 rem Zlo));
2549 Lemma_Lt_Commutation (Ru, Double_Uns (Zlo));
2550 Lemma_Rev_Div_Definition
2551 (Mult, Big (Double_Uns (Zlo)), Big (Qu), Big (Ru));
2552 pragma Assert (Double_Uns (Zlo) = abs Z);
2553 Lemma_Abs_Commutation (Z);
2554 Lemma_Abs_Div_Commutation (Big (X) * Big (Y), Big (Z));
2555 Lemma_Abs_Rem_Commutation (Big (X) * Big (Y), Big (Z));
2556 end Prove_Z_Low;
2557
2558 -- Start of processing for Scaled_Divide
2559
2560 begin
2561 if Z = 0 then
2562 Raise_Error;
2563 end if;
2564
2565 Quot := Big (X) * Big (Y) / Big (Z);
2566 Big_R := Big (X) * Big (Y) rem Big (Z);
2567 if Round then
2568 Big_Q := Round_Quotient (Big (X) * Big (Y), Big (Z), Quot, Big_R);
2569 else
2570 Big_Q := Quot;
2571 end if;
2572
2573 -- First do the multiplication, giving the four digit dividend
2574
2575 Lemma_Abs_Mult_Commutation (Big (X), Big (Y));
2576 Lemma_Abs_Commutation (X);
2577 Lemma_Abs_Commutation (Y);
2578 Lemma_Mult_Decomposition (Mult, Xu, Yu, Xhi, Xlo, Yhi, Ylo);
2579
2580 T1 := Xlo * Ylo;
2581 D (4) := Lo (T1);
2582 D (3) := Hi (T1);
2583
2584 Lemma_Hi_Lo (T1, D (3), D (4));
2585
2586 if Yhi /= 0 then
2587 T1 := Xlo * Yhi;
2588
2589 Lemma_Hi_Lo (T1, Hi (T1), Lo (T1));
2590
2591 T2 := D (3) + Lo (T1);
2592
2593 Lemma_Add_Commutation (Double_Uns (Lo (T1)), D (3));
2594 Lemma_Mult_Distribution (Big_2xxSingle,
2595 Big (Double_Uns (D (3))),
2596 Big (Double_Uns (Lo (T1))));
2597 Lemma_Hi_Lo (T2, Hi (T2), Lo (T2));
2598
2599 D (3) := Lo (T2);
2600 D (2) := Hi (T1) + Hi (T2);
2601
2602 pragma Assert (Double_Uns (Hi (T1)) + Hi (T2) = Double_Uns (D (2)));
2603 Lemma_Add_Commutation (Double_Uns (Hi (T1)), Hi (T2));
2604 pragma Assert
2605 (Big (Double_Uns (Hi (T1))) + Big (Double_Uns (Hi (T2))) =
2606 Big (Double_Uns (D (2))));
2607
2608 if Xhi /= 0 then
2609 T1 := Xhi * Ylo;
2610
2611 Lemma_Hi_Lo (T1, Hi (T1), Lo (T1));
2612
2613 T2 := D (3) + Lo (T1);
2614
2615 Lemma_Add_Commutation (Double_Uns (D (3)), Lo (T1));
2616 Lemma_Hi_Lo (T2, Hi (T2), Lo (T2));
2617 Prove_Mult_Decomposition_Split3
2618 (D1 => 0,
2619 D2 => Big (Double_Uns'(Xhi * Yhi)) + Big (Double_Uns (D (2)))
2620 + Big (Double_Uns (Hi (T1))),
2621 D3 => Big (T2),
2622 D3_Hi => Big (Double_Uns (Hi (T2))),
2623 D3_Lo => Big (Double_Uns (Lo (T2))),
2624 D4 => Big (Double_Uns (D (4))));
2625
2626 D (3) := Lo (T2);
2627 T3 := D (2) + Hi (T1);
2628
2629 Lemma_Add_Commutation (Double_Uns (D (2)), Hi (T1));
2630 Lemma_Add_Commutation (T3, Hi (T2));
2631
2632 T3 := T3 + Hi (T2);
2633 T2 := Double_Uns'(Xhi * Yhi);
2634
2635 Lemma_Hi_Lo (T2, Hi (T2), Lo (T2));
2636 pragma Assert
2637 (Is_Mult_Decomposition
2638 (D1 => Big (Double_Uns (Hi (T2))),
2639 D2 => Big (T3) + Big (Double_Uns (Lo (T2))),
2640 D3 => Big (Double_Uns (D (3))),
2641 D4 => Big (Double_Uns (D (4)))));
2642
2643 T1 := T3 + Lo (T2);
2644 D (2) := Lo (T1);
2645
2646 Lemma_Add_Commutation (T3, Lo (T2));
2647 Lemma_Hi_Lo (T1, Hi (T1), Lo (T1));
2648 Prove_Mult_Decomposition_Split2
2649 (D1 => Big (Double_Uns (Hi (T2))),
2650 D2 => Big (T1),
2651 D2_Lo => Big (Double_Uns (Lo (T1))),
2652 D2_Hi => Big (Double_Uns (Hi (T1))),
2653 D3 => Big (Double_Uns (D (3))),
2654 D4 => Big (Double_Uns (D (4))));
2655
2656 D (1) := Hi (T2) + Hi (T1);
2657
2658 pragma Assert_And_Cut
2659 (D'Initialized
2660 and Is_Mult_Decomposition (D1 => Big (Double_Uns (D (1))),
2661 D2 => Big (Double_Uns (D (2))),
2662 D3 => Big (Double_Uns (D (3))),
2663 D4 => Big (Double_Uns (D (4)))));
2664 else
2665 pragma Assert
2666 (Is_Mult_Decomposition
2667 (D1 => 0,
2668 D2 => Big (Double_Uns (D (2))),
2669 D3 => Big (Double_Uns (D (3)))
2670 + Big (Double_Uns (Xhi)) * Big (Yu),
2671 D4 => Big (Double_Uns (D (4)))));
2672
2673 D (1) := 0;
2674
2675 pragma Assert_And_Cut
2676 (D'Initialized
2677 and Is_Mult_Decomposition (D1 => Big (Double_Uns (D (1))),
2678 D2 => Big (Double_Uns (D (2))),
2679 D3 => Big (Double_Uns (D (3))),
2680 D4 => Big (Double_Uns (D (4)))));
2681 end if;
2682
2683 else
2684 if Xhi /= 0 then
2685 T1 := Xhi * Ylo;
2686
2687 Lemma_Hi_Lo (T1, Hi (T1), Lo (T1));
2688 pragma Assert
2689 (Is_Mult_Decomposition
2690 (D1 => 0,
2691 D2 => Big (Double_Uns (Hi (T1))),
2692 D3 => Big (Double_Uns (Lo (T1))) + Big (Double_Uns (D (3))),
2693 D4 => Big (Double_Uns (D (4)))));
2694
2695 T2 := D (3) + Lo (T1);
2696
2697 Lemma_Add_Commutation (Double_Uns (Lo (T1)), D (3));
2698 pragma Assert
2699 (Is_Mult_Decomposition
2700 (D1 => 0,
2701 D2 => Big (Double_Uns (Hi (T1))),
2702 D3 => Big (T2),
2703 D4 => Big (Double_Uns (D (4)))));
2704 Lemma_Hi_Lo (T2, Hi (T2), Lo (T2));
2705
2706 D (3) := Lo (T2);
2707 D (2) := Hi (T1) + Hi (T2);
2708
2709 pragma Assert
2710 (Double_Uns (Hi (T1)) + Hi (T2) = Double_Uns (D (2)));
2711 Lemma_Add_Commutation (Double_Uns (Hi (T1)), Hi (T2));
2712 pragma Assert
2713 (Big (Double_Uns (Hi (T1))) + Big (Double_Uns (Hi (T2))) =
2714 Big (Double_Uns (D (2))));
2715 pragma Assert
2716 (Is_Mult_Decomposition
2717 (D1 => 0,
2718 D2 => Big (Double_Uns (D (2))),
2719 D3 => Big (Double_Uns (D (3))),
2720 D4 => Big (Double_Uns (D (4)))));
2721 else
2722 D (2) := 0;
2723
2724 pragma Assert
2725 (Is_Mult_Decomposition
2726 (D1 => 0,
2727 D2 => Big (Double_Uns (D (2))),
2728 D3 => Big (Double_Uns (D (3))),
2729 D4 => Big (Double_Uns (D (4)))));
2730 end if;
2731
2732 D (1) := 0;
2733
2734 pragma Assert_And_Cut
2735 (D'Initialized
2736 and Is_Mult_Decomposition (D1 => Big (Double_Uns (D (1))),
2737 D2 => Big (Double_Uns (D (2))),
2738 D3 => Big (Double_Uns (D (3))),
2739 D4 => Big (Double_Uns (D (4)))));
2740 end if;
2741
2742 pragma Assert_And_Cut
2743 -- Restate the precondition
2744 (Z /= 0
2745 and then In_Double_Int_Range
2746 (if Round then Round_Quotient (Big (X) * Big (Y), Big (Z),
2747 Big (X) * Big (Y) / Big (Z),
2748 Big (X) * Big (Y) rem Big (Z))
2749 else Big (X) * Big (Y) / Big (Z))
2750 -- Restate the value of local variables
2751 and then Zu = abs Z
2752 and then Zhi = Hi (Zu)
2753 and then Zlo = Lo (Zu)
2754 and then Mult = abs (Big (X) * Big (Y))
2755 and then Quot = Big (X) * Big (Y) / Big (Z)
2756 and then Big_R = Big (X) * Big (Y) rem Big (Z)
2757 and then
2758 (if Round then
2759 Big_Q = Round_Quotient (Big (X) * Big (Y), Big (Z), Quot, Big_R)
2760 else
2761 Big_Q = Quot)
2762 -- Summarize first part of the procedure
2763 and then D'Initialized
2764 and then Is_Mult_Decomposition (D1 => Big (Double_Uns (D (1))),
2765 D2 => Big (Double_Uns (D (2))),
2766 D3 => Big (Double_Uns (D (3))),
2767 D4 => Big (Double_Uns (D (4)))));
2768
2769 -- Now it is time for the dreaded multiple precision division. First an
2770 -- easy case, check for the simple case of a one digit divisor.
2771
2772 if Zhi = 0 then
2773 if D (1) /= 0 or else D (2) >= Zlo then
2774 if D (1) > 0 then
2775 Lemma_Double_Big_2xxSingle;
2776 Lemma_Mult_Positive (Big_2xxDouble, Big_2xxSingle);
2777 Lemma_Ge_Mult (Big (Double_Uns (D (1))),
2778 1,
2779 Big_2xxDouble * Big_2xxSingle,
2780 Big_2xxDouble * Big_2xxSingle);
2781 Lemma_Mult_Positive (Big_2xxSingle, Big (Double_Uns (D (1))));
2782 Lemma_Ge_Mult (Big_2xxSingle * Big_2xxSingle, Big_2xxDouble,
2783 Big_2xxSingle * Big (Double_Uns (D (1))),
2784 Big_2xxDouble * Big_2xxSingle);
2785 pragma Assert (Mult >= Big_2xxDouble * Big_2xxSingle);
2786 Lemma_Ge_Commutation (2 ** Single_Size, Zu);
2787 Lemma_Ge_Mult (Big_2xxSingle, Big (Zu), Big_2xxDouble,
2788 Big_2xxDouble * Big (Zu));
2789 pragma Assert (Mult >= Big_2xxDouble * Big (Zu));
2790 else
2791 Lemma_Ge_Commutation (Double_Uns (D (2)), Zu);
2792 pragma Assert (Mult >= Big_2xxDouble * Big (Zu));
2793 end if;
2794
2795 Prove_Overflow;
2796 Raise_Error;
2797
2798 -- Here we are dividing at most three digits by one digit
2799
2800 else
2801 T1 := D (2) & D (3);
2802 T2 := Lo (T1 rem Zlo) & D (4);
2803
2804 Qu := Lo (T1 / Zlo) & Lo (T2 / Zlo);
2805 Ru := T2 rem Zlo;
2806
2807 Prove_Z_Low;
2808 end if;
2809
2810 -- If divisor is double digit and dividend is too large, raise error
2811
2812 elsif (D (1) & D (2)) >= Zu then
2813 Lemma_Hi_Lo (D (1) & D (2), D (1), D (2));
2814 Lemma_Ge_Commutation (D (1) & D (2), Zu);
2815 pragma Assert
2816 (Mult >= Big_2xxSingle * Big_2xxSingle * Big (D (1) & D (2)));
2817 Prove_Overflow;
2818 Raise_Error;
2819
2820 -- This is the complex case where we definitely have a double digit
2821 -- divisor and a dividend of at least three digits. We use the classical
2822 -- multiple-precision division algorithm (see section (4.3.1) of Knuth's
2823 -- "The Art of Computer Programming", Vol. 2 for a description
2824 -- (algorithm D).
2825
2826 else
2827 -- First normalize the divisor so that it has the leading bit on.
2828 -- We do this by finding the appropriate left shift amount.
2829
2830 Lemma_Hi_Lo (D (1) & D (2), D (1), D (2));
2831 Lemma_Lt_Commutation (D (1) & D (2), Zu);
2832 pragma Assert
2833 (Mult < Big_2xxDouble * Big (Zu));
2834
2835 Shift := Single_Size;
2836 Mask := Single_Uns'Last;
2837 Scale := 0;
2838
2839 Inter := 0;
2840 pragma Assert (Big_2xx (Scale) = 1);
2841
2842 while Shift > 1 loop
2843 pragma Loop_Invariant (Scale <= Single_Size - Shift);
2844 pragma Loop_Invariant ((Hi (Zu) and Mask) /= 0);
2845 pragma Loop_Invariant
2846 (Mask = Shift_Left (Single_Uns'Last, Single_Size - Shift));
2847 pragma Loop_Invariant (Zu = Shift_Left (abs Z, Scale));
2848 pragma Loop_Invariant (Big (Zu) =
2849 Big (Double_Uns'(abs Z)) * Big_2xx (Scale));
2850 pragma Loop_Invariant (Inter in 0 .. Log_Single_Size - 1);
2851 pragma Loop_Invariant (Shift = 2 ** (Log_Single_Size - Inter));
2852 pragma Loop_Invariant (Shift mod 2 = 0);
2853
2854 declare
2855 -- Local ghost variables
2856
2857 Shift_Prev : constant Natural := Shift with Ghost;
2858 Mask_Prev : constant Single_Uns := Mask with Ghost;
2859 Zu_Prev : constant Double_Uns := Zu with Ghost;
2860
2861 -- Local lemmas
2862
2863 procedure Prove_Power
2864 with
2865 Ghost,
2866 Pre => Inter in 0 .. Log_Single_Size - 1
2867 and then Shift = 2 ** (Log_Single_Size - Inter),
2868 Post => Shift / 2 = 2 ** (Log_Single_Size - (Inter + 1))
2869 and then (Shift = 2 or (Shift / 2) mod 2 = 0);
2870
2871 procedure Prove_Prev_And_Mask (Prev, Mask : Single_Uns)
2872 with
2873 Ghost,
2874 Pre => Prev /= 0
2875 and then (Prev and Mask) = 0,
2876 Post => (Prev and not Mask) /= 0;
2877
2878 procedure Prove_Shift_Progress
2879 with
2880 Ghost,
2881 Pre => Shift <= Single_Size / 2
2882 and then Shift_Prev = 2 * Shift
2883 and then Mask_Prev =
2884 Shift_Left (Single_Uns'Last, Single_Size - Shift_Prev)
2885 and then Mask =
2886 Shift_Left (Single_Uns'Last,
2887 Single_Size - Shift_Prev + Shift),
2888 Post => Mask_Prev =
2889 Shift_Left (Single_Uns'Last, Single_Size - 2 * Shift)
2890 and then Mask =
2891 Shift_Left (Single_Uns'Last, Single_Size - Shift);
2892
2893 procedure Prove_Shifting
2894 with
2895 Ghost,
2896 Pre => Shift <= Single_Size / 2
2897 and then Zu = Shift_Left (Zu_Prev, Shift)
2898 and then Mask_Prev =
2899 Shift_Left (Single_Uns'Last, Single_Size - 2 * Shift)
2900 and then Mask =
2901 Shift_Left (Single_Uns'Last, Single_Size - Shift)
2902 and then (Hi (Zu_Prev) and Mask_Prev and not Mask) /= 0,
2903 Post => (Hi (Zu) and Mask) /= 0;
2904
2905 -----------------------------
2906 -- Local lemma null bodies --
2907 -----------------------------
2908
2909 procedure Prove_Prev_And_Mask (Prev, Mask : Single_Uns) is null;
2910 procedure Prove_Power is null;
2911 procedure Prove_Shifting is null;
2912 procedure Prove_Shift_Progress is null;
2913
2914 begin
2915 pragma Assert (Mask = Shift_Left (Single_Uns'Last,
2916 Single_Size - Shift_Prev));
2917 Prove_Power;
2918
2919 Shift := Shift / 2;
2920
2921 Inter := Inter + 1;
2922 pragma Assert (Shift_Prev = 2 * Shift);
2923
2924 Mask := Shift_Left (Mask, Shift);
2925
2926 Lemma_Double_Shift
2927 (Single_Uns'Last, Single_Size - Shift_Prev, Shift);
2928 Prove_Shift_Progress;
2929
2930 if (Hi (Zu) and Mask) = 0 then
2931 Zu := Shift_Left (Zu, Shift);
2932
2933 pragma Assert ((Hi (Zu_Prev) and Mask_Prev) /= 0);
2934 pragma Assert
2935 (By ((Hi (Zu_Prev) and Mask_Prev and Mask) = 0,
2936 (Hi (Zu_Prev) and Mask) = 0
2937 and then
2938 (Hi (Zu_Prev) and Mask_Prev and Mask)
2939 = (Hi (Zu_Prev) and Mask and Mask_Prev)
2940 ));
2941 Prove_Prev_And_Mask (Hi (Zu_Prev) and Mask_Prev, Mask);
2942 Prove_Shifting;
2943 pragma Assert (Big (Zu_Prev) =
2944 Big (Double_Uns'(abs Z)) * Big_2xx (Scale));
2945 Lemma_Shift_Without_Drop (Zu_Prev, Zu, Mask, Shift);
2946 Lemma_Substitution
2947 (Big (Zu), Big_2xx (Shift),
2948 Big (Zu_Prev), Big (Double_Uns'(abs Z)) * Big_2xx (Scale),
2949 0);
2950 Lemma_Powers_Of_2 (Shift, Scale);
2951 Lemma_Substitution
2952 (Big (Zu), Big (Double_Uns'(abs Z)),
2953 Big_2xx (Shift) * Big_2xx (Scale),
2954 Big_2xx (Shift + Scale), 0);
2955 Lemma_Double_Shift (abs Z, Scale, Shift);
2956
2957 Scale := Scale + Shift;
2958
2959 pragma Assert (Zu = Shift_Left (abs Z, Scale));
2960 pragma Assert
2961 (Big (Zu) = Big (Double_Uns'(abs Z)) * Big_2xx (Scale));
2962 end if;
2963
2964 pragma Assert
2965 (Big (Zu) = Big (Double_Uns'(abs Z)) * Big_2xx (Scale));
2966 end;
2967 end loop;
2968 pragma Assert_And_Cut
2969 (Scale <= Single_Size - 1
2970 and then (Hi (Zu) and Mask) /= 0
2971 and then Mask = Shift_Left (Single_Uns'Last, Single_Size - 1)
2972 and then Zu = Shift_Left (abs Z, Scale)
2973 and then Big (Zu) = Big (Double_Uns'(abs Z)) * Big_2xx (Scale)
2974 and then Mult < Big_2xxDouble * Big (Double_Uns'(abs Z)));
2975
2976 Zhi := Hi (Zu);
2977 Zlo := Lo (Zu);
2978
2979 pragma Assert ((Zhi and Mask) /= 0);
2980 pragma Assert (Zhi >= 2 ** (Single_Size - 1));
2981 pragma Assert (Big (Zu) = Big (Double_Uns'(abs Z)) * Big_2xx (Scale));
2982 -- We have Hi (Zu) /= 0 before normalization. The sequence of
2983 -- Shift_Left operations results in the leading bit of Zu being 1 by
2984 -- moving the leftmost 1-bit in Zu to leading position, thus
2985 -- Zhi = Hi (Zu) >= 2 ** (Single_Size - 1) here.
2986
2987 -- Note that when we scale up the dividend, it still fits in four
2988 -- digits, since we already tested for overflow, and scaling does
2989 -- not change the invariant that (D (1) & D (2)) < Zu.
2990
2991 Lemma_Lt_Commutation (D (1) & D (2), abs Z);
2992 Lemma_Big_Of_Double_Uns (Zu);
2993 Lemma_Lt_Mult (Big (D (1) & D (2)),
2994 Big (Double_Uns'(abs Z)), Big_2xx (Scale),
2995 Big_2xxDouble);
2996
2997 T1 := Shift_Left (D (1) & D (2), Scale);
2998 T2 := Shift_Left (Double_Uns (D (3)), Scale);
2999 T3 := Shift_Left (Double_Uns (D (4)), Scale);
3000
3001 Prove_Dividend_Scaling;
3002
3003 D (1) := Hi (T1);
3004 D (2) := Lo (T1) or Hi (T2);
3005 D (3) := Lo (T2) or Hi (T3);
3006 D (4) := Lo (T3);
3007
3008 pragma Assert (D (1) = Hi (T1) and D (2) = (Lo (T1) or Hi (T2))
3009 and D (3) = (Lo (T2) or Hi (T3)) and D (4) = Lo (T3));
3010 Lemma_Substitution (Big_2xxDouble * Big (Zu), Big_2xxDouble, Big (Zu),
3011 Big (Double_Uns'(abs Z)) * Big_2xx (Scale), 0);
3012 pragma Assert (Mult < Big_2xxDouble * Big (Double_Uns'(abs Z)));
3013 Lemma_Lt_Mult (Mult, Big_2xxDouble * Big (Double_Uns'(abs Z)),
3014 Big_2xx (Scale), Big_2xxDouble * Big (Zu));
3015 pragma Assert (Mult >= Big_0);
3016 pragma Assert (Big_2xx (Scale) >= Big_0);
3017 Lemma_Mult_Non_Negative (Mult, Big_2xx (Scale));
3018 Lemma_Div_Lt (Mult * Big_2xx (Scale), Big (Zu), Big_2xxDouble);
3019 Lemma_Concat_Definition (D (1), D (2));
3020 Lemma_Double_Big_2xxSingle;
3021 Prove_Scaled_Mult_Decomposition_Regroup24
3022 (Big (Double_Uns (D (1))),
3023 Big (Double_Uns (D (2))),
3024 Big (Double_Uns (D (3))),
3025 Big (Double_Uns (D (4))));
3026 Lemma_Substitution
3027 (Mult * Big_2xx (Scale), Big_2xxSingle * Big_2xxSingle,
3028 Big_2xxSingle * Big (Double_Uns (D (1)))
3029 + Big (Double_Uns (D (2))),
3030 Big (D (1) & D (2)),
3031 Big_2xxSingle * Big (Double_Uns (D (3)))
3032 + Big (Double_Uns (D (4))));
3033 pragma Assert
3034 (By (Big (D (1) & D (2)) < Big (Zu),
3035 Big_2xxDouble * (Big (Zu) - Big (D (1) & D (2))) >
3036 Big_2xxSingle * Big (Double_Uns (D (3)))
3037 + Big (Double_Uns (D (4)))));
3038
3039 -- Loop to compute quotient digits, runs twice for Qd (1) and Qd (2)
3040
3041 declare
3042 -- Local lemmas
3043
3044 procedure Prove_First_Iteration (X1, X2, X3, X4 : Single_Uns)
3045 with
3046 Ghost,
3047 Pre => X1 = 0,
3048 Post =>
3049 Big_2xxSingle * Big3 (X1, X2, X3) + Big (Double_Uns (X4))
3050 = Big3 (X2, X3, X4);
3051
3052 ---------------------------
3053 -- Prove_First_Iteration --
3054 ---------------------------
3055
3056 procedure Prove_First_Iteration (X1, X2, X3, X4 : Single_Uns) is
3057 null;
3058
3059 -- Local ghost variables
3060
3061 Qd1 : Single_Uns := 0 with Ghost;
3062 D234 : Big_Integer with Ghost, Relaxed_Initialization;
3063 D123 : constant Big_Integer := Big3 (D (1), D (2), D (3))
3064 with Ghost;
3065 D4 : constant Big_Integer := Big (Double_Uns (D (4)))
3066 with Ghost;
3067
3068 begin
3069 Prove_Scaled_Mult_Decomposition_Regroup3
3070 (D (1), D (2), D (3), D (4));
3071 pragma Assert
3072 (By (Mult * Big_2xx (Scale) = Big_2xxSingle * D123 + D4,
3073 Is_Scaled_Mult_Decomposition (0, 0, D123, D4)));
3074
3075 for J in 1 .. 2 loop
3076 Lemma_Hi_Lo (D (J) & D (J + 1), D (J), D (J + 1));
3077 pragma Assert (Big (D (J) & D (J + 1)) < Big (Zu));
3078
3079 -- Compute next quotient digit. We have to divide three digits
3080 -- by two digits. We estimate the quotient by dividing the
3081 -- leading two digits by the leading digit. Given the scaling
3082 -- we did above which ensured the first bit of the divisor is
3083 -- set, this gives an estimate of the quotient that is at most
3084 -- two too high.
3085
3086 if D (J) > Zhi then
3087 Lemma_Lt_Commutation (Zu, D (J) & D (J + 1));
3088 pragma Assert (False);
3089
3090 elsif D (J) = Zhi then
3091 Qd (J) := Single_Uns'Last;
3092
3093 Lemma_Concat_Definition (D (J), D (J + 1));
3094 Lemma_Big_Of_Double_Uns_Of_Single_Uns (D (J + 2));
3095 pragma Assert (Big_2xxSingle > Big (Double_Uns (D (J + 2))));
3096 pragma Assert
3097 (By (Big3 (D (J), D (J + 1), 0) + Big_2xxSingle
3098 > Big3 (D (J), D (J + 1), D (J + 2)),
3099 Big3 (D (J), D (J + 1), 0) =
3100 Big_2xxSingle * Big_2xxSingle * Big (Double_Uns (D (J)))
3101 + Big_2xxSingle * Big (Double_Uns (D (J + 1)))));
3102 pragma Assert (Big (Double_Uns'(0)) = 0);
3103 pragma Assert (Big (D (J) & D (J + 1)) * Big_2xxSingle =
3104 Big_2xxSingle * (Big_2xxSingle * Big (Double_Uns (D (J)))
3105 + Big (Double_Uns (D (J + 1)))));
3106 pragma Assert (Big (D (J) & D (J + 1)) * Big_2xxSingle =
3107 Big_2xxSingle * Big_2xxSingle * Big (Double_Uns (D (J)))
3108 + Big_2xxSingle * Big (Double_Uns (D (J + 1))));
3109 pragma Assert (Big (D (J) & D (J + 1)) * Big_2xxSingle
3110 = Big3 (D (J), D (J + 1), 0));
3111 pragma Assert ((Big (D (J) & D (J + 1)) + 1) * Big_2xxSingle
3112 = Big3 (D (J), D (J + 1), 0) + Big_2xxSingle);
3113 Lemma_Gt_Mult (Big (Zu), Big (D (J) & D (J + 1)) + 1,
3114 Big_2xxSingle,
3115 Big3 (D (J), D (J + 1), D (J + 2)));
3116 Lemma_Div_Lt
3117 (Big3 (D (J), D (J + 1), D (J + 2)),
3118 Big_2xxSingle, Big (Zu));
3119 pragma Assert
3120 (By (Big (Double_Uns (Qd (J))) >=
3121 Big3 (D (J), D (J + 1), D (J + 2)) / Big (Zu),
3122 Big (Double_Uns (Qd (J))) = Big_2xxSingle - 1));
3123
3124 else
3125 Qd (J) := Lo ((D (J) & D (J + 1)) / Zhi);
3126
3127 Prove_Qd_Calculation_Part_1 (J);
3128 end if;
3129
3130 pragma Assert (for all K in 1 .. J => Qd (K)'Initialized);
3131 Lemma_Div_Mult (Big3 (D (J), D (J + 1), D (J + 2)), Big (Zu));
3132 Lemma_Gt_Mult
3133 (Big (Double_Uns (Qd (J))),
3134 Big3 (D (J), D (J + 1), D (J + 2)) / Big (Zu),
3135 Big (Zu), Big3 (D (J), D (J + 1), D (J + 2)) - Big (Zu));
3136
3137 -- Compute amount to subtract
3138
3139 T1 := Qd (J) * Zlo;
3140 T2 := Qd (J) * Zhi;
3141 S3 := Lo (T1);
3142 T3 := Hi (T1) + Lo (T2);
3143 S2 := Lo (T3);
3144 S1 := Hi (T3) + Hi (T2);
3145
3146 Prove_Multiplication (Qd (J));
3147
3148 -- Adjust quotient digit if it was too high
3149
3150 -- We use the version of the algorithm in the 2nd Edition
3151 -- of "The Art of Computer Programming". This had a bug not
3152 -- discovered till 1995, see Vol 2 errata:
3153 -- http://www-cs-faculty.stanford.edu/~uno/err2-2e.ps.gz.
3154 -- Under rare circumstances the expression in the test could
3155 -- overflow. This version was further corrected in 2005, see
3156 -- Vol 2 errata:
3157 -- http://www-cs-faculty.stanford.edu/~uno/all2-pre.ps.gz.
3158 -- This implementation is not impacted by these bugs, due
3159 -- to the use of a word-size comparison done in function Le3
3160 -- instead of a comparison on two-word integer quantities in
3161 -- the original algorithm.
3162
3163 Lemma_Hi_Lo_3 (Zu, Zhi, Zlo);
3164
3165 while not Le3 (S1, S2, S3, D (J), D (J + 1), D (J + 2)) loop
3166 pragma Loop_Invariant
3167 (Qd (1)'Initialized
3168 and (if J = 2 then Qd (2)'Initialized));
3169 pragma Loop_Invariant (if J = 2 then Qd (1) = Qd1);
3170 pragma Loop_Invariant
3171 (Big3 (S1, S2, S3) = Big (Double_Uns (Qd (J))) * Big (Zu));
3172 pragma Loop_Invariant
3173 (Big3 (S1, S2, S3) > Big3 (D (J), D (J + 1), D (J + 2)));
3174 pragma Assert (Big3 (S1, S2, S3) > 0);
3175 if Qd (J) = 0 then
3176 pragma Assert (Big3 (S1, S2, S3) = 0);
3177 pragma Assert (False);
3178 end if;
3179 Lemma_Ge_Commutation (Double_Uns (Qd (J)), 1);
3180 Lemma_Ge_Mult
3181 (Big (Double_Uns (Qd (J))), 1, Big (Zu), Big (Zu));
3182
3183 Sub3 (S1, S2, S3, 0, Zhi, Zlo);
3184
3185 pragma Assert
3186 (Big3 (S1, S2, S3) >
3187 Big3 (D (J), D (J + 1), D (J + 2)) - Big (Zu));
3188 Lemma_Subtract_Commutation (Double_Uns (Qd (J)), 1);
3189 pragma Assert (Double_Uns (Qd (J)) - Double_Uns'(1)
3190 = Double_Uns (Qd (J) - 1));
3191 pragma Assert (Big (Double_Uns'(1)) = 1);
3192
3193 declare
3194 Prev : constant Single_Uns := Qd (J) with Ghost;
3195 begin
3196 Qd (J) := Qd (J) - 1;
3197 Lemma_Substitution (Big3 (S1, S2, S3), Big (Zu),
3198 Big (Double_Uns (Prev)) - 1,
3199 Big (Double_Uns (Qd (J))), 0);
3200 end;
3201
3202 pragma Assert
3203 (Big3 (S1, S2, S3) = Big (Double_Uns (Qd (J))) * Big (Zu));
3204 end loop;
3205
3206 pragma Assert_And_Cut
3207 (Qd (1)'Initialized
3208 and then (if J = 2 then Qd (2)'Initialized and Qd (1) = Qd1)
3209 and then D'Initialized
3210 and then (if J = 2 then D234'Initialized)
3211 and then Big3 (D (J), D (J + 1), D (J + 2)) =
3212 (if J = 1 then D123 else D234)
3213 and then (if J = 1 then D4 = Big (Double_Uns (D (4))))
3214 and then Big3 (S1, S2, S3) =
3215 Big (Double_Uns (Qd (J))) * Big (Zu)
3216 and then Le3 (S1, S2, S3, D (J), D (J + 1), D (J + 2))
3217 and then Big3 (D (J), D (J + 1), D (J + 2)) -
3218 Big3 (S1, S2, S3) < Big (Zu));
3219
3220 -- Now subtract S1&S2&S3 from D1&D2&D3 ready for next step
3221
3222 Inline_Le3 (S1, S2, S3, D (J), D (J + 1), D (J + 2));
3223
3224 declare
3225 D4_G : constant Single_Uns := D (4) with Ghost;
3226 begin
3227 Sub3 (D (J), D (J + 1), D (J + 2), S1, S2, S3);
3228 pragma Assert (if J = 1 then D (4) = D4_G);
3229 pragma Assert
3230 (By
3231 (D'Initialized,
3232 D (1)'Initialized and D (2)'Initialized
3233 and D (3)'Initialized and D (4)'Initialized));
3234 pragma Assert
3235 (Big3 (D (J), D (J + 1), D (J + 2)) =
3236 (if J = 1 then D123 else D234)
3237 - Big3 (S1, S2, S3));
3238 end;
3239
3240 pragma Assert
3241 (Big3 (D (J), D (J + 1), D (J + 2)) < Big (Zu));
3242
3243 if D (J) > 0 then
3244 Lemma_Double_Big_2xxSingle;
3245 pragma Assert (Big3 (D (J), D (J + 1), D (J + 2)) =
3246 Big_2xxSingle
3247 * Big_2xxSingle * Big (Double_Uns (D (J)))
3248 + Big_2xxSingle * Big (Double_Uns (D (J + 1)))
3249 + Big (Double_Uns (D (J + 2))));
3250 pragma Assert (Big_2xxSingle >= 0);
3251 Lemma_Big_Of_Double_Uns_Of_Single_Uns (D (J + 1));
3252 pragma Assert (Big (Double_Uns (D (J + 1))) >= 0);
3253 Lemma_Mult_Non_Negative
3254 (Big_2xxSingle, Big (Double_Uns (D (J + 1))));
3255 pragma Assert
3256 (Big3 (D (J), D (J + 1), D (J + 2)) >=
3257 Big_2xxSingle * Big_2xxSingle
3258 * Big (Double_Uns (D (J))));
3259 Lemma_Ge_Commutation (Double_Uns (D (J)), Double_Uns'(1));
3260 Lemma_Ge_Mult (Big (Double_Uns (D (J))),
3261 Big (Double_Uns'(1)),
3262 Big_2xxDouble,
3263 Big (Double_Uns'(1)) * Big_2xxDouble);
3264 pragma Assert
3265 (Big_2xxDouble * Big (Double_Uns'(1)) = Big_2xxDouble);
3266 pragma Assert
3267 (Big3 (D (J), D (J + 1), D (J + 2)) >= Big_2xxDouble);
3268 pragma Assert (False);
3269 end if;
3270
3271 if J = 1 then
3272 Qd1 := Qd (1);
3273 D234 := Big3 (D (2), D (3), D (4));
3274 pragma Assert (D4 = Big (Double_Uns (D (4))));
3275 Lemma_Substitution
3276 (Mult * Big_2xx (Scale), Big_2xxSingle, D123,
3277 Big3 (D (1), D (2), D (3)) + Big3 (S1, S2, S3),
3278 Big (Double_Uns (D (4))));
3279 Prove_First_Iteration (D (1), D (2), D (3), D (4));
3280 Lemma_Substitution (Mult * Big_2xx (Scale), Big_2xxSingle,
3281 Big3 (S1, S2, S3),
3282 Big (Double_Uns (Qd1)) * Big (Zu),
3283 D234);
3284 else
3285 pragma Assert (Qd1 = Qd (1));
3286 pragma Assert
3287 (By (Mult * Big_2xx (Scale) =
3288 Big_2xxSingle * Big (Double_Uns (Qd (1))) * Big (Zu)
3289 + Big (Double_Uns (Qd (2))) * Big (Zu)
3290 + Big_2xxSingle * Big (Double_Uns (D (3)))
3291 + Big (Double_Uns (D (4))),
3292 By (Mult * Big_2xx (Scale) =
3293 Big_2xxSingle * Big (Double_Uns (Qd (1))) * Big (Zu)
3294 + Big3 (D (2), D (3), D (4)) + Big3 (S1, S2, S3),
3295 Mult * Big_2xx (Scale) =
3296 Big_2xxSingle * Big (Double_Uns (Qd (1))) * Big (Zu)
3297 + D234)));
3298
3299 end if;
3300 end loop;
3301
3302 pragma Assert_And_Cut
3303 (Qd (1)'Initialized and then Qd (2)'Initialized
3304 and then D'Initialized
3305 and then Big_2xxSingle * Big (Double_Uns (D (3)))
3306 + Big (Double_Uns (D (4))) < Big (Zu)
3307 and then Mult * Big_2xx (Scale) =
3308 Big_2xxSingle * Big (Double_Uns (Qd (1))) * Big (Zu)
3309 + Big (Double_Uns (Qd (2))) * Big (Zu)
3310 + Big_2xxSingle * Big (Double_Uns (D (3)))
3311 + Big (Double_Uns (D (4))));
3312 end;
3313
3314 -- The two quotient digits are now set, and the remainder of the
3315 -- scaled division is in D3&D4. To get the remainder for the
3316 -- original unscaled division, we rescale this dividend.
3317
3318 -- We rescale the divisor as well, to make the proper comparison
3319 -- for rounding below.
3320
3321 Qu := Qd (1) & Qd (2);
3322 Ru := D (3) & D (4);
3323
3324 Lemma_Hi_Lo (Qu, Qd (1), Qd (2));
3325 Lemma_Hi_Lo (Ru, D (3), D (4));
3326 Lemma_Substitution
3327 (Mult * Big_2xx (Scale), Big (Zu),
3328 Big_2xxSingle * Big (Double_Uns (Qd (1)))
3329 + Big (Double_Uns (Qd (2))),
3330 Big (Qu), Big (Ru));
3331 Prove_Rescaling;
3332
3333 Ru := Shift_Right (Ru, Scale);
3334
3335 declare
3336 -- Local lemma required to help automatic provers
3337 procedure Lemma_Div_Congruent
3338 (X, Y : Big_Natural;
3339 Z : Big_Positive)
3340 with
3341 Ghost,
3342 Pre => X = Y,
3343 Post => X / Z = Y / Z;
3344
3345 procedure Lemma_Div_Congruent
3346 (X, Y : Big_Natural;
3347 Z : Big_Positive)
3348 is null;
3349
3350 begin
3351 Lemma_Shift_Right (Zu, Scale);
3352 Lemma_Div_Congruent (Big (Zu),
3353 Big (Double_Uns'(abs Z)) * Big_2xx (Scale),
3354 Big_2xx (Scale));
3355
3356 Zu := Shift_Right (Zu, Scale);
3357
3358 Lemma_Simplify (Big (Double_Uns'(abs Z)), Big_2xx (Scale));
3359 pragma Assert (Big (Zu) = Big (Double_Uns'(abs Z)));
3360 end;
3361 end if;
3362
3363 pragma Assert (Big (Ru) = abs Big_R);
3364 pragma Assert (Big (Qu) = abs Quot);
3365 pragma Assert (Big (Zu) = Big (Double_Uns'(abs Z)));
3366
3367 -- Deal with rounding case
3368
3369 if Round then
3370 Prove_Rounding_Case;
3371
3372 if Ru > (Zu - Double_Uns'(1)) / Double_Uns'(2) then
3373 pragma Assert (abs Big_Q = Big (Qu) + 1);
3374
3375 -- Protect against wrapping around when rounding, by signaling
3376 -- an overflow when the quotient is too large.
3377
3378 if Qu = Double_Uns'Last then
3379 Prove_Q_Too_Big;
3380 Raise_Error;
3381 end if;
3382
3383 Lemma_Add_One (Qu);
3384
3385 Qu := Qu + Double_Uns'(1);
3386 end if;
3387 end if;
3388
3389 pragma Assert (Big (Qu) = abs Big_Q);
3390
3391 -- Set final signs (RM 4.5.5(27-30))
3392
3393 -- Case of dividend (X * Y) sign positive
3394
3395 if (X >= 0 and then Y >= 0) or else (X < 0 and then Y < 0) then
3396 Prove_Positive_Dividend;
3397
3398 R := To_Pos_Int (Ru);
3399 Q := (if Z > 0 then To_Pos_Int (Qu) else To_Neg_Int (Qu));
3400
3401 -- Case of dividend (X * Y) sign negative
3402
3403 else
3404 Prove_Negative_Dividend;
3405
3406 R := To_Neg_Int (Ru);
3407 Q := (if Z > 0 then To_Neg_Int (Qu) else To_Pos_Int (Qu));
3408 end if;
3409
3410 Prove_Sign_R;
3411 Prove_Signs;
3412 end Scaled_Divide;
3413 pragma Annotate (Gnatcheck, Exempt_Off, "Metrics_Cyclomatic_Complexity");
3414
3415 ----------
3416 -- Sub3 --
3417 ----------
3418
3419 procedure Sub3 (X1, X2, X3 : in out Single_Uns; Y1, Y2, Y3 : Single_Uns) is
3420
3421 -- Local ghost variables
3422
3423 XX1 : constant Single_Uns := X1 with Ghost;
3424 XX2 : constant Single_Uns := X2 with Ghost;
3425 XX3 : constant Single_Uns := X3 with Ghost;
3426
3427 -- Local lemmas
3428
3429 procedure Lemma_Add3_No_Carry (X1, X2, X3, Y1, Y2, Y3 : Single_Uns)
3430 with
3431 Ghost,
3432 Pre => X1 <= Single_Uns'Last - Y1
3433 and then X2 <= Single_Uns'Last - Y2
3434 and then X3 <= Single_Uns'Last - Y3,
3435 Post => Big3 (X1 + Y1, X2 + Y2, X3 + Y3)
3436 = Big3 (X1, X2, X3) + Big3 (Y1, Y2, Y3);
3437
3438 procedure Lemma_Ge_Expand (X1, X2, X3, Y1, Y2, Y3 : Single_Uns)
3439 with
3440 Ghost,
3441 Pre => Big3 (X1, X2, X3) >= Big3 (Y1, Y2, Y3),
3442 Post => X1 > Y1
3443 or else (X1 = Y1 and then X2 > Y2)
3444 or else (X1 = Y1 and then X2 = Y2 and then X3 >= Y3);
3445
3446 procedure Lemma_Sub3_No_Carry (X1, X2, X3, Y1, Y2, Y3 : Single_Uns)
3447 with
3448 Ghost,
3449 Pre => X1 >= Y1 and then X2 >= Y2 and then X3 >= Y3,
3450 Post => Big3 (X1 - Y1, X2 - Y2, X3 - Y3)
3451 = Big3 (X1, X2, X3) - Big3 (Y1, Y2, Y3);
3452
3453 procedure Lemma_Sub3_With_Carry2 (X1, X2, X3, Y2 : Single_Uns)
3454 with
3455 Ghost,
3456 Pre => X2 < Y2,
3457 Post => Big3 (X1, X2 - Y2, X3)
3458 = Big3 (X1, X2, X3) + Big3 (Single_Uns'(1), 0, 0) - Big3 (0, Y2, 0);
3459
3460 procedure Lemma_Sub3_With_Carry3 (X1, X2, X3, Y3 : Single_Uns)
3461 with
3462 Ghost,
3463 Pre => X3 < Y3,
3464 Post => Big3 (X1, X2, X3 - Y3)
3465 = Big3 (X1, X2, X3) + Big3 (Single_Uns'(0), 1, 0) - Big3 (0, 0, Y3);
3466
3467 -------------------------
3468 -- Lemma_Add3_No_Carry --
3469 -------------------------
3470
3471 procedure Lemma_Add3_No_Carry (X1, X2, X3, Y1, Y2, Y3 : Single_Uns) is
3472 begin
3473 Lemma_Add_Commutation (Double_Uns (X1), Y1);
3474 Lemma_Add_Commutation (Double_Uns (X2), Y2);
3475 Lemma_Add_Commutation (Double_Uns (X3), Y3);
3476 end Lemma_Add3_No_Carry;
3477
3478 ---------------------
3479 -- Lemma_Ge_Expand --
3480 ---------------------
3481
3482 procedure Lemma_Ge_Expand (X1, X2, X3, Y1, Y2, Y3 : Single_Uns) is null;
3483
3484 -------------------------
3485 -- Lemma_Sub3_No_Carry --
3486 -------------------------
3487
3488 procedure Lemma_Sub3_No_Carry (X1, X2, X3, Y1, Y2, Y3 : Single_Uns) is
3489 begin
3490 Lemma_Subtract_Commutation (Double_Uns (X1), Double_Uns (Y1));
3491 Lemma_Subtract_Commutation (Double_Uns (X2), Double_Uns (Y2));
3492 Lemma_Subtract_Commutation (Double_Uns (X3), Double_Uns (Y3));
3493 end Lemma_Sub3_No_Carry;
3494
3495 ----------------------------
3496 -- Lemma_Sub3_With_Carry2 --
3497 ----------------------------
3498
3499 procedure Lemma_Sub3_With_Carry2 (X1, X2, X3, Y2 : Single_Uns) is
3500 pragma Unreferenced (X1, X3);
3501 begin
3502 Lemma_Add_Commutation
3503 (Double_Uns'(2 ** Single_Size) - Double_Uns (Y2), X2);
3504 Lemma_Subtract_Commutation
3505 (Double_Uns'(2 ** Single_Size), Double_Uns (Y2));
3506 end Lemma_Sub3_With_Carry2;
3507
3508 ----------------------------
3509 -- Lemma_Sub3_With_Carry3 --
3510 ----------------------------
3511
3512 procedure Lemma_Sub3_With_Carry3 (X1, X2, X3, Y3 : Single_Uns) is
3513 pragma Unreferenced (X1, X2);
3514 begin
3515 Lemma_Add_Commutation
3516 (Double_Uns'(2 ** Single_Size) - Double_Uns (Y3), X3);
3517 Lemma_Subtract_Commutation
3518 (Double_Uns'(2 ** Single_Size), Double_Uns (Y3));
3519 end Lemma_Sub3_With_Carry3;
3520
3521 -- Start of processing for Sub3
3522
3523 begin
3524 Lemma_Ge_Expand (X1, X2, X3, Y1, Y2, Y3);
3525
3526 if Y3 > X3 then
3527 if X2 = 0 then
3528 pragma Assert (X1 >= 1);
3529 Lemma_Sub3_No_Carry (X1, X2, X3, 1, 0, 0);
3530
3531 X1 := X1 - 1;
3532
3533 pragma Assert
3534 (Big3 (X1, X2, X3) =
3535 Big3 (XX1, XX2, XX3) - Big3 (Single_Uns'(1), 0, 0));
3536 pragma Assert
3537 (Big3 (X1, X2, X3) = Big3 (XX1, XX2, XX3)
3538 - Big3 (Single_Uns'(0), Single_Uns'Last, 0)
3539 - Big3 (Single_Uns'(0), 1, 0));
3540 Lemma_Add3_No_Carry (X1, X2, X3, 0, Single_Uns'Last, 0);
3541 else
3542 Lemma_Sub3_No_Carry (X1, X2, X3, 0, 1, 0);
3543 end if;
3544
3545 X2 := X2 - 1;
3546
3547 pragma Assert
3548 (Big3 (X1, X2, X3) =
3549 Big3 (XX1, XX2, XX3) - Big3 (Single_Uns'(0), 1, 0));
3550 Lemma_Sub3_With_Carry3 (X1, X2, X3, Y3);
3551 else
3552 Lemma_Sub3_No_Carry (X1, X2, X3, 0, 0, Y3);
3553 end if;
3554
3555 X3 := X3 - Y3;
3556
3557 pragma Assert
3558 (Big3 (X1, X2, X3) = Big3 (XX1, XX2, XX3) - Big3 (0, 0, Y3));
3559
3560 if Y2 > X2 then
3561 pragma Assert (X1 >= 1);
3562 Lemma_Sub3_No_Carry (X1, X2, X3, 1, 0, 0);
3563
3564 X1 := X1 - 1;
3565
3566 pragma Assert
3567 (Big3 (X1, X2, X3) = Big3 (XX1, XX2, XX3)
3568 - Big3 (0, 0, Y3) - Big3 (Single_Uns'(1), 0, 0));
3569 Lemma_Sub3_With_Carry2 (X1, X2, X3, Y2);
3570 else
3571 Lemma_Sub3_No_Carry (X1, X2, X3, 0, Y2, 0);
3572 end if;
3573
3574 X2 := X2 - Y2;
3575
3576 pragma Assert
3577 (Big3 (X1, X2, X3) = Big3 (XX1, XX2, XX3) - Big3 (0, Y2, Y3));
3578 pragma Assert (X1 >= Y1);
3579 Lemma_Sub3_No_Carry (X1, Y2, X3, Y1, 0, 0);
3580
3581 X1 := X1 - Y1;
3582
3583 pragma Assert
3584 (Big3 (X1, X2, X3) = Big3 (XX1, XX2, XX3)
3585 - Big3 (0, Y2, Y3) - Big3 (Y1, 0, 0));
3586 Lemma_Add3_No_Carry (0, Y2, Y3, Y1, 0, 0);
3587 pragma Assert
3588 (Big3 (X1, X2, X3) = Big3 (XX1, XX2, XX3) - Big3 (Y1, Y2, Y3));
3589 end Sub3;
3590
3591 -------------------------------
3592 -- Subtract_With_Ovflo_Check --
3593 -------------------------------
3594
3595 function Subtract_With_Ovflo_Check (X, Y : Double_Int) return Double_Int is
3596 R : constant Double_Int := To_Int (To_Uns (X) - To_Uns (Y));
3597
3598 -- Local lemmas
3599
3600 procedure Prove_Negative_X
3601 with
3602 Ghost,
3603 Pre => X < 0 and then (Y <= 0 or else R < 0),
3604 Post => R = X - Y;
3605
3606 procedure Prove_Non_Negative_X
3607 with
3608 Ghost,
3609 Pre => X >= 0 and then (Y > 0 or else R >= 0),
3610 Post => R = X - Y;
3611
3612 procedure Prove_Overflow_Case
3613 with
3614 Ghost,
3615 Pre =>
3616 (if X >= 0 then Y <= 0 and then R < 0
3617 else Y > 0 and then R >= 0),
3618 Post => not In_Double_Int_Range (Big (X) - Big (Y));
3619
3620 ----------------------
3621 -- Prove_Negative_X --
3622 ----------------------
3623
3624 procedure Prove_Negative_X is
3625 begin
3626 if X = Double_Int'First then
3627 if Y = Double_Int'First or else Y > 0 then
3628 null;
3629 else
3630 pragma Assert
3631 (To_Uns (X) - To_Uns (Y) =
3632 2 ** (Double_Size - 1) + Double_Uns (-Y));
3633 end if;
3634
3635 elsif Y >= 0 or else Y = Double_Int'First then
3636 null;
3637
3638 else
3639 pragma Assert
3640 (To_Uns (X) - To_Uns (Y) = -Double_Uns (-X) + Double_Uns (-Y));
3641 end if;
3642 end Prove_Negative_X;
3643
3644 --------------------------
3645 -- Prove_Non_Negative_X --
3646 --------------------------
3647
3648 procedure Prove_Non_Negative_X is
3649 begin
3650 if Y > 0 then
3651 declare
3652 Ru : constant Double_Uns := To_Uns (X) - To_Uns (Y);
3653 begin
3654 pragma Assert (Ru = Double_Uns (X) - Double_Uns (Y));
3655 if Ru < 2 ** (Double_Size - 1) then -- R >= 0
3656 pragma Assert (To_Uns (Y) <= To_Uns (X));
3657 Lemma_Subtract_Double_Uns (X => Y, Y => X);
3658 pragma Assert (Ru = Double_Uns (X - Y));
3659
3660 elsif Ru = 2 ** (Double_Size - 1) then
3661 pragma Assert (Double_Uns (Y) < 2 ** (Double_Size - 1));
3662 pragma Assert (False);
3663
3664 else
3665 pragma Assert
3666 (R = -Double_Int (-(Double_Uns (X) - Double_Uns (Y))));
3667 pragma Assert
3668 (R = -Double_Int (-Double_Uns (X) + Double_Uns (Y)));
3669 pragma Assert
3670 (R = -Double_Int (Double_Uns (Y) - Double_Uns (X)));
3671 end if;
3672 end;
3673
3674 elsif Y = Double_Int'First then
3675 pragma Assert
3676 (To_Uns (X) - To_Uns (Y) =
3677 Double_Uns (X) - 2 ** (Double_Size - 1));
3678 pragma Assert (False);
3679
3680 else
3681 pragma Assert
3682 (To_Uns (X) - To_Uns (Y) = Double_Uns (X) + Double_Uns (-Y));
3683 end if;
3684 end Prove_Non_Negative_X;
3685
3686 -------------------------
3687 -- Prove_Overflow_Case --
3688 -------------------------
3689
3690 procedure Prove_Overflow_Case is
3691 begin
3692 if X >= 0 and then Y /= Double_Int'First then
3693 pragma Assert
3694 (To_Uns (X) - To_Uns (Y) = Double_Uns (X) + Double_Uns (-Y));
3695
3696 elsif X < 0 and then X /= Double_Int'First then
3697 pragma Assert
3698 (To_Uns (X) - To_Uns (Y) = -Double_Uns (-X) - Double_Uns (Y));
3699 end if;
3700 end Prove_Overflow_Case;
3701
3702 -- Start of processing for Subtract_With_Ovflo_Check
3703
3704 begin
3705 if X >= 0 then
3706 if Y > 0 or else R >= 0 then
3707 Prove_Non_Negative_X;
3708 return R;
3709 end if;
3710
3711 else -- X < 0
3712 if Y <= 0 or else R < 0 then
3713 Prove_Negative_X;
3714 return R;
3715 end if;
3716 end if;
3717
3718 Prove_Overflow_Case;
3719 Raise_Error;
3720 end Subtract_With_Ovflo_Check;
3721
3722 ----------------
3723 -- To_Neg_Int --
3724 ----------------
3725
3726 function To_Neg_Int (A : Double_Uns) return Double_Int is
3727 R : constant Double_Int :=
3728 (if A = 2 ** (Double_Size - 1) then Double_Int'First else -To_Int (A));
3729 -- Note that we can't just use the expression of the Else, because it
3730 -- overflows for A = 2 ** (Double_Size - 1).
3731 begin
3732 if R <= 0 then
3733 return R;
3734 else
3735 Raise_Error;
3736 end if;
3737 end To_Neg_Int;
3738
3739 ----------------
3740 -- To_Pos_Int --
3741 ----------------
3742
3743 function To_Pos_Int (A : Double_Uns) return Double_Int is
3744 R : constant Double_Int := To_Int (A);
3745 begin
3746 if R >= 0 then
3747 return R;
3748 else
3749 Raise_Error;
3750 end if;
3751 end To_Pos_Int;
3752
3753 pragma Annotate (Gnatcheck, Exempt_Off, "Improper_Returns");
3754 end System.Arith_Double;
3755
3756 pragma Annotate (Gnatcheck, Exempt_Off, "Metrics_LSLOC");