1 ------------------------------------------------------------------------------
3 -- GNAT RUN-TIME COMPONENTS --
5 -- S Y S T E M . A R I T H _ 6 4 --
9 -- Copyright (C) 1992-2007, 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 2, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
22 -- As a special exception, if other files instantiate generics from this --
23 -- unit, or you link this unit with other files to produce an executable, --
24 -- this unit does not by itself cause the resulting executable to be --
25 -- covered by the GNU General Public License. This exception does not --
26 -- however invalidate any other reasons why the executable file might be --
27 -- covered by the GNU Public License. --
29 -- GNAT was originally developed by the GNAT team at New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc. --
32 ------------------------------------------------------------------------------
34 with Interfaces; use Interfaces;
35 with Ada.Unchecked_Conversion;
37 package body System.Arith_64 is
39 pragma Suppress (Overflow_Check);
40 pragma Suppress (Range_Check);
42 subtype Uns64 is Unsigned_64;
43 function To_Uns is new Ada.Unchecked_Conversion (Int64, Uns64);
44 function To_Int is new Ada.Unchecked_Conversion (Uns64, Int64);
46 subtype Uns32 is Unsigned_32;
48 -----------------------
49 -- Local Subprograms --
50 -----------------------
52 function "+" (A, B : Uns32) return Uns64;
53 function "+" (A : Uns64; B : Uns32) return Uns64;
55 -- Length doubling additions
57 function "*" (A, B : Uns32) return Uns64;
59 -- Length doubling multiplication
61 function "/" (A : Uns64; B : Uns32) return Uns64;
63 -- Length doubling division
65 function "rem" (A : Uns64; B : Uns32) return Uns64;
66 pragma Inline ("rem");
67 -- Length doubling remainder
69 function "&" (Hi, Lo : Uns32) return Uns64;
71 -- Concatenate hi, lo values to form 64-bit result
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;
78 -- Low order half of 64-bit value
80 function Hi (A : Uns64) return Uns32;
82 -- High order half of 64-bit value
84 procedure Sub3 (X1, X2, X3 : in out Uns32; Y1, Y2, Y3 : Uns32);
85 -- Computes X1&X2&X3 := X1&X2&X3 - Y1&Y1&Y3 with mod 2**96 wrap
87 function To_Neg_Int (A : Uns64) return Int64;
88 -- Convert to negative integer equivalent. If the input is in the range
89 -- 0 .. 2 ** 63, then the corresponding negative signed integer (obtained
90 -- by negating the given value) is returned, otherwise constraint error
93 function To_Pos_Int (A : Uns64) return Int64;
94 -- Convert to positive integer equivalent. If the input is in the range
95 -- 0 .. 2 ** 63-1, then the corresponding non-negative signed integer is
96 -- returned, otherwise constraint error is raised.
98 procedure Raise_Error;
99 pragma No_Return (Raise_Error);
100 -- Raise constraint error with appropriate message
106 function "&" (Hi, Lo : Uns32) return Uns64 is
108 return Shift_Left (Uns64 (Hi), 32) or Uns64 (Lo);
115 function "*" (A, B : Uns32) return Uns64 is
117 return Uns64 (A) * Uns64 (B);
124 function "+" (A, B : Uns32) return Uns64 is
126 return Uns64 (A) + Uns64 (B);
129 function "+" (A : Uns64; B : Uns32) return Uns64 is
131 return A + Uns64 (B);
138 function "/" (A : Uns64; B : Uns32) return Uns64 is
140 return A / Uns64 (B);
147 function "rem" (A : Uns64; B : Uns32) return Uns64 is
149 return A rem Uns64 (B);
152 --------------------------
153 -- Add_With_Ovflo_Check --
154 --------------------------
156 function Add_With_Ovflo_Check (X, Y : Int64) return Int64 is
157 R : constant Int64 := To_Int (To_Uns (X) + To_Uns (Y));
161 if Y < 0 or else R >= 0 then
166 if Y > 0 or else R < 0 then
172 end Add_With_Ovflo_Check;
178 procedure Double_Divide
183 Xu : constant Uns64 := To_Uns (abs X);
184 Yu : constant Uns64 := To_Uns (abs Y);
186 Yhi : constant Uns32 := Hi (Yu);
187 Ylo : constant Uns32 := Lo (Yu);
189 Zu : constant Uns64 := To_Uns (abs Z);
190 Zhi : constant Uns32 := Hi (Zu);
191 Zlo : constant Uns32 := Lo (Zu);
198 if Yu = 0 or else Zu = 0 then
202 -- Compute Y * Z. Note that if the result overflows 64 bits unsigned,
203 -- then the rounded result is clearly zero (since the dividend is at
204 -- most 2**63 - 1, the extra bit of precision is nice here!)
232 Du := Lo (T2) & Lo (T1);
234 -- Set final signs (RM 4.5.5(27-30))
236 Den_Pos := (Y < 0) = (Z < 0);
238 -- Check overflow case of largest negative number divided by 1
240 if X = Int64'First and then Du = 1 and then not Den_Pos then
244 -- Perform the actual division
249 -- Deal with rounding case
251 if Round and then Ru > (Du - Uns64'(1)) / Uns64'(2) then
252 Qu := Qu + Uns64'(1);
255 -- Case of dividend (X) sign positive
266 -- Case of dividend (X) sign negative
283 function Hi (A : Uns64) return Uns32 is
285 return Uns32 (Shift_Right (A, 32));
292 function Le3 (X1, X2, X3 : Uns32; Y1, Y2, Y3 : Uns32) return Boolean is
311 function Lo (A : Uns64) return Uns32 is
313 return Uns32 (A and 16#FFFF_FFFF#);
316 -------------------------------
317 -- Multiply_With_Ovflo_Check --
318 -------------------------------
320 function Multiply_With_Ovflo_Check (X, Y : Int64) return Int64 is
321 Xu : constant Uns64 := To_Uns (abs X);
322 Xhi : constant Uns32 := Hi (Xu);
323 Xlo : constant Uns32 := Lo (Xu);
325 Yu : constant Uns64 := To_Uns (abs Y);
326 Yhi : constant Uns32 := Hi (Yu);
327 Ylo : constant Uns32 := Lo (Yu);
342 else -- Yhi = Xhi = 0
346 -- Here we have T2 set to the contribution to the upper half
347 -- of the result from the upper halves of the input values.
356 T2 := Lo (T2) & Lo (T1);
360 return To_Pos_Int (T2);
362 return To_Neg_Int (T2);
366 return To_Pos_Int (T2);
368 return To_Neg_Int (T2);
372 end Multiply_With_Ovflo_Check;
378 procedure Raise_Error is
380 raise Constraint_Error with "64-bit arithmetic overflow";
387 procedure Scaled_Divide
392 Xu : constant Uns64 := To_Uns (abs X);
393 Xhi : constant Uns32 := Hi (Xu);
394 Xlo : constant Uns32 := Lo (Xu);
396 Yu : constant Uns64 := To_Uns (abs Y);
397 Yhi : constant Uns32 := Hi (Yu);
398 Ylo : constant Uns32 := Lo (Yu);
400 Zu : Uns64 := To_Uns (abs Z);
401 Zhi : Uns32 := Hi (Zu);
402 Zlo : Uns32 := Lo (Zu);
404 D : array (1 .. 4) of Uns32;
405 -- The dividend, four digits (D(1) is high order)
407 Qd : array (1 .. 2) of Uns32;
408 -- The quotient digits, two digits (Qd(1) is high order)
411 -- Value to subtract, three digits (S1 is high order)
415 -- Unsigned quotient and remainder
418 -- Scaling factor used for multiple-precision divide. Dividend and
419 -- Divisor are multiplied by 2 ** Scale, and the final remainder
420 -- is divided by the scaling factor. The reason for this scaling
421 -- is to allow more accurate estimation of quotient digits.
427 -- First do the multiplication, giving the four digit dividend
435 T2 := D (3) + Lo (T1);
437 D (2) := Hi (T1) + Hi (T2);
441 T2 := D (3) + Lo (T1);
443 T3 := D (2) + Hi (T1);
448 T1 := (D (1) & D (2)) + Uns64'(Xhi * Yhi);
459 T2 := D (3) + Lo (T1);
461 D (2) := Hi (T1) + Hi (T2);
470 -- Now it is time for the dreaded multiple precision division. First
471 -- an easy case, check for the simple case of a one digit divisor.
474 if D (1) /= 0 or else D (2) >= Zlo then
477 -- Here we are dividing at most three digits by one digit
481 T2 := Lo (T1 rem Zlo) & D (4);
483 Qu := Lo (T1 / Zlo) & Lo (T2 / Zlo);
487 -- If divisor is double digit and too large, raise error
489 elsif (D (1) & D (2)) >= Zu then
492 -- This is the complex case where we definitely have a double digit
493 -- divisor and a dividend of at least three digits. We use the classical
494 -- multiple division algorithm (see section (4.3.1) of Knuth's "The Art
495 -- of Computer Programming", Vol. 2 for a description (algorithm D).
498 -- First normalize the divisor so that it has the leading bit on.
499 -- We do this by finding the appropriate left shift amount.
503 if (Zhi and 16#FFFF0000#) = 0 then
505 Zu := Shift_Left (Zu, 16);
508 if (Hi (Zu) and 16#FF00_0000#) = 0 then
510 Zu := Shift_Left (Zu, 8);
513 if (Hi (Zu) and 16#F000_0000#) = 0 then
515 Zu := Shift_Left (Zu, 4);
518 if (Hi (Zu) and 16#C000_0000#) = 0 then
520 Zu := Shift_Left (Zu, 2);
523 if (Hi (Zu) and 16#8000_0000#) = 0 then
525 Zu := Shift_Left (Zu, 1);
531 -- Note that when we scale up the dividend, it still fits in four
532 -- digits, since we already tested for overflow, and scaling does
533 -- not change the invariant that (D (1) & D (2)) >= Zu.
535 T1 := Shift_Left (D (1) & D (2), Scale);
537 T2 := Shift_Left (0 & D (3), Scale);
538 D (2) := Lo (T1) or Hi (T2);
539 T3 := Shift_Left (0 & D (4), Scale);
540 D (3) := Lo (T2) or Hi (T3);
543 -- Loop to compute quotient digits, runs twice for Qd(1) and Qd(2)
547 -- Compute next quotient digit. We have to divide three digits by
548 -- two digits. We estimate the quotient by dividing the leading
549 -- two digits by the leading digit. Given the scaling we did above
550 -- which ensured the first bit of the divisor is set, this gives
551 -- an estimate of the quotient that is at most two too high.
553 if D (J + 1) = Zhi then
554 Qd (J + 1) := 2 ** 32 - 1;
556 Qd (J + 1) := Lo ((D (J + 1) & D (J + 2)) / Zhi);
559 -- Compute amount to subtract
561 T1 := Qd (J + 1) * Zlo;
562 T2 := Qd (J + 1) * Zhi;
564 T1 := Hi (T1) + Lo (T2);
566 S1 := Hi (T1) + Hi (T2);
568 -- Adjust quotient digit if it was too high
571 exit when Le3 (S1, S2, S3, D (J + 1), D (J + 2), D (J + 3));
572 Qd (J + 1) := Qd (J + 1) - 1;
573 Sub3 (S1, S2, S3, 0, Zhi, Zlo);
576 -- Now subtract S1&S2&S3 from D1&D2&D3 ready for next step
578 Sub3 (D (J + 1), D (J + 2), D (J + 3), S1, S2, S3);
581 -- The two quotient digits are now set, and the remainder of the
582 -- scaled division is in D3&D4. To get the remainder for the
583 -- original unscaled division, we rescale this dividend.
585 -- We rescale the divisor as well, to make the proper comparison
586 -- for rounding below.
588 Qu := Qd (1) & Qd (2);
589 Ru := Shift_Right (D (3) & D (4), Scale);
590 Zu := Shift_Right (Zu, Scale);
593 -- Deal with rounding case
595 if Round and then Ru > (Zu - Uns64'(1)) / Uns64'(2) then
596 Qu := Qu + Uns64 (1);
599 -- Set final signs (RM 4.5.5(27-30))
601 -- Case of dividend (X * Y) sign positive
603 if (X >= 0 and then Y >= 0)
604 or else (X < 0 and then Y < 0)
606 R := To_Pos_Int (Ru);
609 Q := To_Pos_Int (Qu);
611 Q := To_Neg_Int (Qu);
614 -- Case of dividend (X * Y) sign negative
617 R := To_Neg_Int (Ru);
620 Q := To_Neg_Int (Qu);
622 Q := To_Pos_Int (Qu);
631 procedure Sub3 (X1, X2, X3 : in out Uns32; Y1, Y2, Y3 : Uns32) is
651 -------------------------------
652 -- Subtract_With_Ovflo_Check --
653 -------------------------------
655 function Subtract_With_Ovflo_Check (X, Y : Int64) return Int64 is
656 R : constant Int64 := To_Int (To_Uns (X) - To_Uns (Y));
660 if Y > 0 or else R >= 0 then
665 if Y <= 0 or else R < 0 then
671 end Subtract_With_Ovflo_Check;
677 function To_Neg_Int (A : Uns64) return Int64 is
678 R : constant Int64 := -To_Int (A);
692 function To_Pos_Int (A : Uns64) return Int64 is
693 R : constant Int64 := To_Int (A);