1 ------------------------------------------------------------------------------
3 -- GNAT RUN-TIME COMPONENTS --
5 -- S Y S T E M . A R I T H _ 6 4 --
9 -- Copyright (C) 1992-2009, 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;
33 with Ada.Unchecked_Conversion;
35 package body System.Arith_64 is
37 pragma Suppress (Overflow_Check);
38 pragma Suppress (Range_Check);
40 subtype Uns64 is Unsigned_64;
41 function To_Uns is new Ada.Unchecked_Conversion (Int64, Uns64);
42 function To_Int is new Ada.Unchecked_Conversion (Uns64, Int64);
44 subtype Uns32 is Unsigned_32;
46 -----------------------
47 -- Local Subprograms --
48 -----------------------
50 function "+" (A, B : Uns32) return Uns64;
51 function "+" (A : Uns64; B : Uns32) return Uns64;
53 -- Length doubling additions
55 function "*" (A, B : Uns32) return Uns64;
57 -- Length doubling multiplication
59 function "/" (A : Uns64; B : Uns32) return Uns64;
61 -- Length doubling division
63 function "rem" (A : Uns64; B : Uns32) return Uns64;
64 pragma Inline ("rem");
65 -- Length doubling remainder
67 function "&" (Hi, Lo : Uns32) return Uns64;
69 -- Concatenate hi, lo values to form 64-bit result
71 function Le3 (X1, X2, X3 : Uns32; Y1, Y2, Y3 : Uns32) return Boolean;
72 -- Determines if 96 bit value X1&X2&X3 <= Y1&Y2&Y3
74 function Lo (A : Uns64) return Uns32;
76 -- Low order half of 64-bit value
78 function Hi (A : Uns64) return Uns32;
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;
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;
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;
97 pragma No_Return (Raise_Error);
98 -- Raise constraint error with appropriate message
104 function "&" (Hi, Lo : Uns32) return Uns64 is
106 return Shift_Left (Uns64 (Hi), 32) or Uns64 (Lo);
113 function "*" (A, B : Uns32) return Uns64 is
115 return Uns64 (A) * Uns64 (B);
122 function "+" (A, B : Uns32) return Uns64 is
124 return Uns64 (A) + Uns64 (B);
127 function "+" (A : Uns64; B : Uns32) return Uns64 is
129 return A + Uns64 (B);
136 function "/" (A : Uns64; B : Uns32) return Uns64 is
138 return A / Uns64 (B);
145 function "rem" (A : Uns64; B : Uns32) return Uns64 is
147 return A rem Uns64 (B);
150 --------------------------
151 -- Add_With_Ovflo_Check --
152 --------------------------
154 function Add_With_Ovflo_Check (X, Y : Int64) return Int64 is
155 R : constant Int64 := To_Int (To_Uns (X) + To_Uns (Y));
159 if Y < 0 or else R >= 0 then
164 if Y > 0 or else R < 0 then
170 end Add_With_Ovflo_Check;
176 procedure Double_Divide
181 Xu : constant Uns64 := To_Uns (abs X);
182 Yu : constant Uns64 := To_Uns (abs Y);
184 Yhi : constant Uns32 := Hi (Yu);
185 Ylo : constant Uns32 := Lo (Yu);
187 Zu : constant Uns64 := To_Uns (abs Z);
188 Zhi : constant Uns32 := Hi (Zu);
189 Zlo : constant Uns32 := Lo (Zu);
196 if Yu = 0 or else Zu = 0 then
200 -- Compute Y * Z. Note that if the result overflows 64 bits unsigned,
201 -- then the rounded result is clearly zero (since the dividend is at
202 -- most 2**63 - 1, the extra bit of precision is nice here!)
230 Du := Lo (T2) & Lo (T1);
232 -- Set final signs (RM 4.5.5(27-30))
234 Den_Pos := (Y < 0) = (Z < 0);
236 -- Check overflow case of largest negative number divided by 1
238 if X = Int64'First and then Du = 1 and then not Den_Pos then
242 -- Perform the actual division
247 -- Deal with rounding case
249 if Round and then Ru > (Du - Uns64'(1)) / Uns64'(2) then
250 Qu := Qu + Uns64'(1);
253 -- Case of dividend (X) sign positive
264 -- Case of dividend (X) sign negative
281 function Hi (A : Uns64) return Uns32 is
283 return Uns32 (Shift_Right (A, 32));
290 function Le3 (X1, X2, X3 : Uns32; Y1, Y2, Y3 : Uns32) return Boolean is
309 function Lo (A : Uns64) return Uns32 is
311 return Uns32 (A and 16#FFFF_FFFF#);
314 -------------------------------
315 -- Multiply_With_Ovflo_Check --
316 -------------------------------
318 function Multiply_With_Ovflo_Check (X, Y : Int64) return Int64 is
319 Xu : constant Uns64 := To_Uns (abs X);
320 Xhi : constant Uns32 := Hi (Xu);
321 Xlo : constant Uns32 := Lo (Xu);
323 Yu : constant Uns64 := To_Uns (abs Y);
324 Yhi : constant Uns32 := Hi (Yu);
325 Ylo : constant Uns32 := Lo (Yu);
340 else -- Yhi = Xhi = 0
344 -- Here we have T2 set to the contribution to the upper half
345 -- of the result from the upper halves of the input values.
354 T2 := Lo (T2) & Lo (T1);
358 return To_Pos_Int (T2);
360 return To_Neg_Int (T2);
364 return To_Pos_Int (T2);
366 return To_Neg_Int (T2);
370 end Multiply_With_Ovflo_Check;
376 procedure Raise_Error is
378 raise Constraint_Error with "64-bit arithmetic overflow";
385 procedure Scaled_Divide
390 Xu : constant Uns64 := To_Uns (abs X);
391 Xhi : constant Uns32 := Hi (Xu);
392 Xlo : constant Uns32 := Lo (Xu);
394 Yu : constant Uns64 := To_Uns (abs Y);
395 Yhi : constant Uns32 := Hi (Yu);
396 Ylo : constant Uns32 := Lo (Yu);
398 Zu : Uns64 := To_Uns (abs Z);
399 Zhi : Uns32 := Hi (Zu);
400 Zlo : Uns32 := Lo (Zu);
402 D : array (1 .. 4) of Uns32;
403 -- The dividend, four digits (D(1) is high order)
405 Qd : array (1 .. 2) of Uns32;
406 -- The quotient digits, two digits (Qd(1) is high order)
409 -- Value to subtract, three digits (S1 is high order)
413 -- Unsigned quotient and remainder
416 -- Scaling factor used for multiple-precision divide. Dividend and
417 -- Divisor are multiplied by 2 ** Scale, and the final remainder
418 -- is divided by the scaling factor. The reason for this scaling
419 -- is to allow more accurate estimation of quotient digits.
425 -- First do the multiplication, giving the four digit dividend
433 T2 := D (3) + Lo (T1);
435 D (2) := Hi (T1) + Hi (T2);
439 T2 := D (3) + Lo (T1);
441 T3 := D (2) + Hi (T1);
446 T1 := (D (1) & D (2)) + Uns64'(Xhi * Yhi);
457 T2 := D (3) + Lo (T1);
459 D (2) := Hi (T1) + Hi (T2);
468 -- Now it is time for the dreaded multiple precision division. First
469 -- an easy case, check for the simple case of a one digit divisor.
472 if D (1) /= 0 or else D (2) >= Zlo then
475 -- Here we are dividing at most three digits by one digit
479 T2 := Lo (T1 rem Zlo) & D (4);
481 Qu := Lo (T1 / Zlo) & Lo (T2 / Zlo);
485 -- If divisor is double digit and too large, raise error
487 elsif (D (1) & D (2)) >= Zu then
490 -- This is the complex case where we definitely have a double digit
491 -- divisor and a dividend of at least three digits. We use the classical
492 -- multiple division algorithm (see section (4.3.1) of Knuth's "The Art
493 -- of Computer Programming", Vol. 2 for a description (algorithm D).
496 -- First normalize the divisor so that it has the leading bit on.
497 -- We do this by finding the appropriate left shift amount.
501 if (Zhi and 16#FFFF0000#) = 0 then
503 Zu := Shift_Left (Zu, 16);
506 if (Hi (Zu) and 16#FF00_0000#) = 0 then
508 Zu := Shift_Left (Zu, 8);
511 if (Hi (Zu) and 16#F000_0000#) = 0 then
513 Zu := Shift_Left (Zu, 4);
516 if (Hi (Zu) and 16#C000_0000#) = 0 then
518 Zu := Shift_Left (Zu, 2);
521 if (Hi (Zu) and 16#8000_0000#) = 0 then
523 Zu := Shift_Left (Zu, 1);
529 -- Note that when we scale up the dividend, it still fits in four
530 -- digits, since we already tested for overflow, and scaling does
531 -- not change the invariant that (D (1) & D (2)) >= Zu.
533 T1 := Shift_Left (D (1) & D (2), Scale);
535 T2 := Shift_Left (0 & D (3), Scale);
536 D (2) := Lo (T1) or Hi (T2);
537 T3 := Shift_Left (0 & D (4), Scale);
538 D (3) := Lo (T2) or Hi (T3);
541 -- Loop to compute quotient digits, runs twice for Qd(1) and Qd(2)
545 -- Compute next quotient digit. We have to divide three digits by
546 -- two digits. We estimate the quotient by dividing the leading
547 -- two digits by the leading digit. Given the scaling we did above
548 -- which ensured the first bit of the divisor is set, this gives
549 -- an estimate of the quotient that is at most two too high.
551 if D (J + 1) = Zhi then
552 Qd (J + 1) := 2 ** 32 - 1;
554 Qd (J + 1) := Lo ((D (J + 1) & D (J + 2)) / Zhi);
557 -- Compute amount to subtract
559 T1 := Qd (J + 1) * Zlo;
560 T2 := Qd (J + 1) * Zhi;
562 T1 := Hi (T1) + Lo (T2);
564 S1 := Hi (T1) + Hi (T2);
566 -- Adjust quotient digit if it was too high
569 exit when Le3 (S1, S2, S3, D (J + 1), D (J + 2), D (J + 3));
570 Qd (J + 1) := Qd (J + 1) - 1;
571 Sub3 (S1, S2, S3, 0, Zhi, Zlo);
574 -- Now subtract S1&S2&S3 from D1&D2&D3 ready for next step
576 Sub3 (D (J + 1), D (J + 2), D (J + 3), S1, S2, S3);
579 -- The two quotient digits are now set, and the remainder of the
580 -- scaled division is in D3&D4. To get the remainder for the
581 -- original unscaled division, we rescale this dividend.
583 -- We rescale the divisor as well, to make the proper comparison
584 -- for rounding below.
586 Qu := Qd (1) & Qd (2);
587 Ru := Shift_Right (D (3) & D (4), Scale);
588 Zu := Shift_Right (Zu, Scale);
591 -- Deal with rounding case
593 if Round and then Ru > (Zu - Uns64'(1)) / Uns64'(2) then
594 Qu := Qu + Uns64 (1);
597 -- Set final signs (RM 4.5.5(27-30))
599 -- Case of dividend (X * Y) sign positive
601 if (X >= 0 and then Y >= 0)
602 or else (X < 0 and then Y < 0)
604 R := To_Pos_Int (Ru);
607 Q := To_Pos_Int (Qu);
609 Q := To_Neg_Int (Qu);
612 -- Case of dividend (X * Y) sign negative
615 R := To_Neg_Int (Ru);
618 Q := To_Neg_Int (Qu);
620 Q := To_Pos_Int (Qu);
629 procedure Sub3 (X1, X2, X3 : in out Uns32; Y1, Y2, Y3 : Uns32) is
649 -------------------------------
650 -- Subtract_With_Ovflo_Check --
651 -------------------------------
653 function Subtract_With_Ovflo_Check (X, Y : Int64) return Int64 is
654 R : constant Int64 := To_Int (To_Uns (X) - To_Uns (Y));
658 if Y > 0 or else R >= 0 then
663 if Y <= 0 or else R < 0 then
669 end Subtract_With_Ovflo_Check;
675 function To_Neg_Int (A : Uns64) return Int64 is
676 R : constant Int64 := -To_Int (A);
690 function To_Pos_Int (A : Uns64) return Int64 is
691 R : constant Int64 := To_Int (A);