1 ------------------------------------------------------------------------------
3 -- GNAT RUN-TIME COMPONENTS --
5 -- S Y S T E M . A R I T H _ 6 4 --
9 -- Copyright (C) 1992-2019, Free Software Foundation, Inc. --
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. --
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. --
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/>. --
27 -- GNAT was originally developed by the GNAT team at New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
30 ------------------------------------------------------------------------------
32 with Interfaces; use Interfaces;
34 with Ada.Unchecked_Conversion;
36 package body System.Arith_64 is
38 pragma Suppress (Overflow_Check);
39 pragma Suppress (Range_Check);
41 subtype Uns64 is Unsigned_64;
42 function To_Uns is new Ada.Unchecked_Conversion (Int64, Uns64);
43 function To_Int is new Ada.Unchecked_Conversion (Uns64, Int64);
45 subtype Uns32 is Unsigned_32;
47 -----------------------
48 -- Local Subprograms --
49 -----------------------
51 function "+" (A, B : Uns32) return Uns64 is (Uns64 (A) + Uns64 (B));
52 function "+" (A : Uns64; B : Uns32) return Uns64 is (A + Uns64 (B));
53 -- Length doubling additions
55 function "*" (A, B : Uns32) return Uns64 is (Uns64 (A) * Uns64 (B));
56 -- Length doubling multiplication
58 function "/" (A : Uns64; B : Uns32) return Uns64 is (A / Uns64 (B));
59 -- Length doubling division
61 function "&" (Hi, Lo : Uns32) return Uns64 is
62 (Shift_Left (Uns64 (Hi), 32) or Uns64 (Lo));
63 -- Concatenate hi, lo values to form 64-bit result
65 function "abs" (X : Int64) return Uns64 is
66 (if X = Int64'First then 2**63 else Uns64 (Int64'(abs X)));
67 -- Convert absolute value of X to unsigned. Note that we can't just use
68 -- the expression of the Else, because it overflows for X = Int64'First.
70 function "rem" (A : Uns64; B : Uns32) return Uns64 is (A rem Uns64 (B));
71 -- Length doubling remainder
73 function Le3 (X1, X2, X3 : Uns32; Y1, Y2, Y3 : Uns32) return Boolean;
74 -- Determines if 96 bit value X1&X2&X3 <= Y1&Y2&Y3
76 function Lo (A : Uns64) return Uns32 is (Uns32 (A and 16#FFFF_FFFF#));
77 -- Low order half of 64-bit value
79 function Hi (A : Uns64) return Uns32 is (Uns32 (Shift_Right (A, 32)));
80 -- High order half of 64-bit value
82 procedure Sub3 (X1, X2, X3 : in out Uns32; Y1, Y2, Y3 : Uns32);
83 -- Computes X1&X2&X3 := X1&X2&X3 - Y1&Y1&Y3 with mod 2**96 wrap
85 function To_Neg_Int (A : Uns64) return Int64 with Inline;
86 -- Convert to negative integer equivalent. If the input is in the range
87 -- 0 .. 2 ** 63, then the corresponding negative signed integer (obtained
88 -- by negating the given value) is returned, otherwise constraint error
91 function To_Pos_Int (A : Uns64) return Int64 with Inline;
92 -- Convert to positive integer equivalent. If the input is in the range
93 -- 0 .. 2 ** 63-1, then the corresponding non-negative signed integer is
94 -- returned, otherwise constraint error is raised.
96 procedure Raise_Error with Inline;
97 pragma No_Return (Raise_Error);
98 -- Raise constraint error with appropriate message
100 --------------------------
101 -- Add_With_Ovflo_Check --
102 --------------------------
104 function Add_With_Ovflo_Check (X, Y : Int64) return Int64 is
105 R : constant Int64 := To_Int (To_Uns (X) + To_Uns (Y));
109 if Y < 0 or else R >= 0 then
114 if Y > 0 or else R < 0 then
120 end Add_With_Ovflo_Check;
126 procedure Double_Divide
131 Xu : constant Uns64 := abs X;
132 Yu : constant Uns64 := abs Y;
134 Yhi : constant Uns32 := Hi (Yu);
135 Ylo : constant Uns32 := Lo (Yu);
137 Zu : constant Uns64 := abs Z;
138 Zhi : constant Uns32 := Hi (Zu);
139 Zlo : constant Uns32 := Lo (Zu);
146 if Yu = 0 or else Zu = 0 then
150 -- Set final signs (RM 4.5.5(27-30))
152 Den_Pos := (Y < 0) = (Z < 0);
154 -- Compute Y * Z. Note that if the result overflows 64 bits unsigned,
155 -- then the rounded result is zero, except for the very special case
156 -- where X = -2**63 and abs(Y*Z) = 2**64, when Round is True.
161 -- Handle the special case when Round is True
167 and then X = Int64'First
170 Q := (if Den_Pos then -1 else 1);
190 -- Handle the special case when Round is True
195 and then X = Int64'First
198 Q := (if Den_Pos then -1 else 1);
207 Du := Lo (T2) & Lo (T1);
209 -- Check overflow case of largest negative number divided by -1
211 if X = Int64'First and then Du = 1 and then not Den_Pos then
215 -- Perform the actual division
220 -- Deal with rounding case
222 if Round and then Ru > (Du - Uns64'(1)) / Uns64'(2) then
223 Qu := Qu + Uns64'(1);
226 -- Case of dividend (X) sign positive
230 Q := (if Den_Pos then To_Int (Qu) else -To_Int (Qu));
232 -- Case of dividend (X) sign negative
234 -- We perform the unary minus operation on the unsigned value
235 -- before conversion to signed, to avoid a possible overflow for
236 -- value -2**63, both for computing R and Q.
240 Q := (if Den_Pos then To_Int (-Qu) else To_Int (Qu));
248 function Le3 (X1, X2, X3 : Uns32; Y1, Y2, Y3 : Uns32) return Boolean is
263 -------------------------------
264 -- Multiply_With_Ovflo_Check --
265 -------------------------------
267 function Multiply_With_Ovflo_Check (X, Y : Int64) return Int64 is
268 Xu : constant Uns64 := abs X;
269 Xhi : constant Uns32 := Hi (Xu);
270 Xlo : constant Uns32 := Lo (Xu);
272 Yu : constant Uns64 := abs Y;
273 Yhi : constant Uns32 := Hi (Yu);
274 Ylo : constant Uns32 := Lo (Yu);
289 else -- Yhi = Xhi = 0
293 -- Here we have T2 set to the contribution to the upper half of the
294 -- result from the upper halves of the input values.
303 T2 := Lo (T2) & Lo (T1);
307 return To_Pos_Int (T2);
309 return To_Neg_Int (T2);
313 return To_Pos_Int (T2);
315 return To_Neg_Int (T2);
319 end Multiply_With_Ovflo_Check;
325 procedure Raise_Error is
327 raise Constraint_Error with "64-bit arithmetic overflow";
334 procedure Scaled_Divide
339 Xu : constant Uns64 := abs X;
340 Xhi : constant Uns32 := Hi (Xu);
341 Xlo : constant Uns32 := Lo (Xu);
343 Yu : constant Uns64 := abs Y;
344 Yhi : constant Uns32 := Hi (Yu);
345 Ylo : constant Uns32 := Lo (Yu);
348 Zhi : Uns32 := Hi (Zu);
349 Zlo : Uns32 := Lo (Zu);
351 D : array (1 .. 4) of Uns32;
352 -- The dividend, four digits (D(1) is high order)
354 Qd : array (1 .. 2) of Uns32;
355 -- The quotient digits, two digits (Qd(1) is high order)
358 -- Value to subtract, three digits (S1 is high order)
362 -- Unsigned quotient and remainder
365 -- Scaling factor used for multiple-precision divide. Dividend and
366 -- Divisor are multiplied by 2 ** Scale, and the final remainder is
367 -- divided by the scaling factor. The reason for this scaling is to
368 -- allow more accurate estimation of quotient digits.
374 -- First do the multiplication, giving the four digit dividend
382 T2 := D (3) + Lo (T1);
384 D (2) := Hi (T1) + Hi (T2);
388 T2 := D (3) + Lo (T1);
390 T3 := D (2) + Hi (T1);
395 T1 := (D (1) & D (2)) + Uns64'(Xhi * Yhi);
406 T2 := D (3) + Lo (T1);
408 D (2) := Hi (T1) + Hi (T2);
417 -- Now it is time for the dreaded multiple precision division. First an
418 -- easy case, check for the simple case of a one digit divisor.
421 if D (1) /= 0 or else D (2) >= Zlo then
424 -- Here we are dividing at most three digits by one digit
428 T2 := Lo (T1 rem Zlo) & D (4);
430 Qu := Lo (T1 / Zlo) & Lo (T2 / Zlo);
434 -- If divisor is double digit and dividend is too large, raise error
436 elsif (D (1) & D (2)) >= Zu then
439 -- This is the complex case where we definitely have a double digit
440 -- divisor and a dividend of at least three digits. We use the classical
441 -- multiple-precision division algorithm (see section (4.3.1) of Knuth's
442 -- "The Art of Computer Programming", Vol. 2 for a description
446 -- First normalize the divisor so that it has the leading bit on.
447 -- We do this by finding the appropriate left shift amount.
451 if (Zhi and 16#FFFF0000#) = 0 then
453 Zu := Shift_Left (Zu, 16);
456 if (Hi (Zu) and 16#FF00_0000#) = 0 then
458 Zu := Shift_Left (Zu, 8);
461 if (Hi (Zu) and 16#F000_0000#) = 0 then
463 Zu := Shift_Left (Zu, 4);
466 if (Hi (Zu) and 16#C000_0000#) = 0 then
468 Zu := Shift_Left (Zu, 2);
471 if (Hi (Zu) and 16#8000_0000#) = 0 then
473 Zu := Shift_Left (Zu, 1);
479 -- Note that when we scale up the dividend, it still fits in four
480 -- digits, since we already tested for overflow, and scaling does
481 -- not change the invariant that (D (1) & D (2)) < Zu.
483 T1 := Shift_Left (D (1) & D (2), Scale);
485 T2 := Shift_Left (0 & D (3), Scale);
486 D (2) := Lo (T1) or Hi (T2);
487 T3 := Shift_Left (0 & D (4), Scale);
488 D (3) := Lo (T2) or Hi (T3);
491 -- Loop to compute quotient digits, runs twice for Qd(1) and Qd(2)
495 -- Compute next quotient digit. We have to divide three digits by
496 -- two digits. We estimate the quotient by dividing the leading
497 -- two digits by the leading digit. Given the scaling we did above
498 -- which ensured the first bit of the divisor is set, this gives
499 -- an estimate of the quotient that is at most two too high.
501 Qd (J + 1) := (if D (J + 1) = Zhi
503 else Lo ((D (J + 1) & D (J + 2)) / Zhi));
505 -- Compute amount to subtract
507 T1 := Qd (J + 1) * Zlo;
508 T2 := Qd (J + 1) * Zhi;
510 T1 := Hi (T1) + Lo (T2);
512 S1 := Hi (T1) + Hi (T2);
514 -- Adjust quotient digit if it was too high
516 -- We use the version of the algorithm in the 2nd Edition of
517 -- "The Art of Computer Programming". This had a bug not
518 -- discovered till 1995, see Vol 2 errata:
519 -- http://www-cs-faculty.stanford.edu/~uno/err2-2e.ps.gz.
520 -- Under rare circumstances the expression in the test could
521 -- overflow. This version was further corrected in 2005, see
523 -- http://www-cs-faculty.stanford.edu/~uno/all2-pre.ps.gz.
524 -- This implementation is not impacted by these bugs, due to the
525 -- use of a word-size comparison done in function Le3 instead of
526 -- a comparison on two-word integer quantities in the original
530 exit when Le3 (S1, S2, S3, D (J + 1), D (J + 2), D (J + 3));
531 Qd (J + 1) := Qd (J + 1) - 1;
532 Sub3 (S1, S2, S3, 0, Zhi, Zlo);
535 -- Now subtract S1&S2&S3 from D1&D2&D3 ready for next step
537 Sub3 (D (J + 1), D (J + 2), D (J + 3), S1, S2, S3);
540 -- The two quotient digits are now set, and the remainder of the
541 -- scaled division is in D3&D4. To get the remainder for the
542 -- original unscaled division, we rescale this dividend.
544 -- We rescale the divisor as well, to make the proper comparison
545 -- for rounding below.
547 Qu := Qd (1) & Qd (2);
548 Ru := Shift_Right (D (3) & D (4), Scale);
549 Zu := Shift_Right (Zu, Scale);
552 -- Deal with rounding case
554 if Round and then Ru > (Zu - Uns64'(1)) / Uns64'(2) then
556 -- Protect against wrapping around when rounding, by signaling
557 -- an overflow when the quotient is too large.
559 if Qu = Uns64'Last then
563 Qu := Qu + Uns64 (1);
566 -- Set final signs (RM 4.5.5(27-30))
568 -- Case of dividend (X * Y) sign positive
570 if (X >= 0 and then Y >= 0) or else (X < 0 and then Y < 0) then
571 R := To_Pos_Int (Ru);
572 Q := (if Z > 0 then To_Pos_Int (Qu) else To_Neg_Int (Qu));
574 -- Case of dividend (X * Y) sign negative
577 R := To_Neg_Int (Ru);
578 Q := (if Z > 0 then To_Neg_Int (Qu) else To_Pos_Int (Qu));
586 procedure Sub3 (X1, X2, X3 : in out Uns32; Y1, Y2, Y3 : Uns32) is
606 -------------------------------
607 -- Subtract_With_Ovflo_Check --
608 -------------------------------
610 function Subtract_With_Ovflo_Check (X, Y : Int64) return Int64 is
611 R : constant Int64 := To_Int (To_Uns (X) - To_Uns (Y));
615 if Y > 0 or else R >= 0 then
620 if Y <= 0 or else R < 0 then
626 end Subtract_With_Ovflo_Check;
632 function To_Neg_Int (A : Uns64) return Int64 is
633 R : constant Int64 := (if A = 2**63 then Int64'First else -To_Int (A));
634 -- Note that we can't just use the expression of the Else, because it
635 -- overflows for A = 2**63.
648 function To_Pos_Int (A : Uns64) return Int64 is
649 R : constant Int64 := To_Int (A);